org.el 893 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Carsten Dominik <carsten at orgmode 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. ;;; Commentary:
  24. ;;
  25. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  26. ;; project planning with a fast and effective plain-text system.
  27. ;;
  28. ;; Org-mode develops organizational tasks around NOTES files that contain
  29. ;; information about projects as plain text. Org-mode is implemented on
  30. ;; top of outline-mode, which makes it possible to keep the content of
  31. ;; large files well structured. Visibility cycling and structure editing
  32. ;; help to work with the tree. Tables are easily created with a built-in
  33. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  34. ;; and scheduling. It dynamically compiles entries into an agenda that
  35. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  36. ;; Plain text URL-like links connect to websites, emails, Usenet
  37. ;; messages, BBDB entries, and any files related to the projects. For
  38. ;; printing and sharing of notes, an Org-mode file can be exported as a
  39. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  40. ;; iCalendar file. It can also serve as a publishing tool for a set of
  41. ;; linked webpages.
  42. ;;
  43. ;; Installation and Activation
  44. ;; ---------------------------
  45. ;; See the corresponding sections in the manual at
  46. ;;
  47. ;; http://orgmode.org/org.html#Installation
  48. ;;
  49. ;; Documentation
  50. ;; -------------
  51. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  52. ;; distribution also contains a PDF version of it. At the homepage of
  53. ;; Org-mode, you can read the same text online as HTML. There is also an
  54. ;; excellent reference card made by Philip Rooke. This card can be found
  55. ;; in the etc/ directory of Emacs 22.
  56. ;;
  57. ;; A list of recent changes can be found at
  58. ;; http://orgmode.org/Changes.html
  59. ;;
  60. ;;; Code:
  61. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  62. (defvar org-table-formula-constants-local nil
  63. "Local version of `org-table-formula-constants'.")
  64. (make-variable-buffer-local 'org-table-formula-constants-local)
  65. ;;;; Require other packages
  66. (eval-when-compile
  67. (require 'cl)
  68. (require 'gnus-sum))
  69. (require 'calendar)
  70. (require 'find-func)
  71. (require 'format-spec)
  72. (load "org-loaddefs.el" t t t)
  73. (require 'org-macs)
  74. (require 'org-compat)
  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 a 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. (unless (boundp 'calendar-view-holidays-initially-flag)
  93. (org-defvaralias 'calendar-view-holidays-initially-flag
  94. 'view-calendar-holidays-initially))
  95. (unless (boundp 'calendar-view-diary-initially-flag)
  96. (org-defvaralias 'calendar-view-diary-initially-flag
  97. 'view-diary-entries-initially))
  98. (unless (boundp 'diary-fancy-buffer)
  99. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  100. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  101. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  102. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  103. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  104. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  105. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  106. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  107. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  108. (declare-function orgtbl-mode "org-table" (&optional arg))
  109. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  110. (declare-function org-beamer-mode "ox-beamer" ())
  111. (declare-function org-table-edit-field "org-table" (arg))
  112. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  113. (declare-function org-table-set-constants "org-table" ())
  114. (declare-function org-id-get-create "org-id" (&optional force))
  115. (declare-function org-id-find-id-file "org-id" (id))
  116. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  117. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  118. (declare-function org-agenda-redo "org-agenda" (&optional all))
  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. (declare-function org-element--parse-objects "org-element"
  124. (beg end acc restriction))
  125. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  126. (declare-function org-element-contents "org-element" (element))
  127. (declare-function org-element-context "org-element" (&optional element))
  128. (declare-function org-element-interpret-data "org-element"
  129. (data &optional parent))
  130. (declare-function org-element-map "org-element"
  131. (data types fun &optional info first-match no-recursion))
  132. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  133. (declare-function org-element-parse-buffer "org-element"
  134. (&optional granularity visible-only))
  135. (declare-function org-element-property "org-element" (property element))
  136. (declare-function org-element-put-property "org-element"
  137. (element property value))
  138. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  139. (declare-function org-element--parse-objects "org-element"
  140. (beg end acc restriction))
  141. (declare-function org-element-parse-buffer "org-element"
  142. (&optional granularity visible-only))
  143. (declare-function org-element-restriction "org-element" (element))
  144. (declare-function org-element-type "org-element" (element))
  145. ;; load languages based on value of `org-babel-load-languages'
  146. (defvar org-babel-load-languages)
  147. ;;;###autoload
  148. (defun org-babel-do-load-languages (sym value)
  149. "Load the languages defined in `org-babel-load-languages'."
  150. (set-default sym value)
  151. (mapc (lambda (pair)
  152. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  153. (if active
  154. (progn
  155. (require (intern (concat "ob-" lang))))
  156. (progn
  157. (funcall 'fmakunbound
  158. (intern (concat "org-babel-execute:" lang)))
  159. (funcall 'fmakunbound
  160. (intern (concat "org-babel-expand-body:" lang)))))))
  161. org-babel-load-languages))
  162. ;;;###autoload
  163. (defun org-babel-load-file (file &optional compile)
  164. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  165. This function exports the source code using `org-babel-tangle'
  166. and then loads the resulting file using `load-file'. With prefix
  167. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  168. file to byte-code before it is loaded."
  169. (interactive "fFile to load: \nP")
  170. (require 'ob-core)
  171. (let* ((age (lambda (file)
  172. (float-time
  173. (time-subtract (current-time)
  174. (nth 5 (or (file-attributes (file-truename file))
  175. (file-attributes file)))))))
  176. (base-name (file-name-sans-extension file))
  177. (exported-file (concat base-name ".el")))
  178. ;; tangle if the org-mode file is newer than the elisp file
  179. (unless (and (file-exists-p exported-file)
  180. (> (funcall age file) (funcall age exported-file)))
  181. (org-babel-tangle-file file exported-file "emacs-lisp"))
  182. (message "%s %s"
  183. (if compile
  184. (progn (byte-compile-file exported-file 'load)
  185. "Compiled and loaded")
  186. (progn (load-file exported-file) "Loaded"))
  187. exported-file)))
  188. (defcustom org-babel-load-languages '((emacs-lisp . t))
  189. "Languages which can be evaluated in Org-mode buffers.
  190. This list can be used to load support for any of the languages
  191. below, note that each language will depend on a different set of
  192. system executables and/or Emacs modes. When a language is
  193. \"loaded\", then code blocks in that language can be evaluated
  194. with `org-babel-execute-src-block' bound by default to C-c
  195. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  196. be set to remove code block evaluation from the C-c C-c
  197. keybinding. By default only Emacs Lisp (which has no
  198. requirements) is loaded."
  199. :group 'org-babel
  200. :set 'org-babel-do-load-languages
  201. :version "24.1"
  202. :type '(alist :tag "Babel Languages"
  203. :key-type
  204. (choice
  205. (const :tag "Awk" awk)
  206. (const :tag "C" C)
  207. (const :tag "R" R)
  208. (const :tag "Asymptote" asymptote)
  209. (const :tag "Calc" calc)
  210. (const :tag "Clojure" clojure)
  211. (const :tag "CSS" css)
  212. (const :tag "Ditaa" ditaa)
  213. (const :tag "Dot" dot)
  214. (const :tag "Emacs Lisp" emacs-lisp)
  215. (const :tag "Fortran" fortran)
  216. (const :tag "Gnuplot" gnuplot)
  217. (const :tag "Haskell" haskell)
  218. (const :tag "IO" io)
  219. (const :tag "Java" java)
  220. (const :tag "Javascript" js)
  221. (const :tag "LaTeX" latex)
  222. (const :tag "Ledger" ledger)
  223. (const :tag "Lilypond" lilypond)
  224. (const :tag "Lisp" lisp)
  225. (const :tag "Makefile" makefile)
  226. (const :tag "Maxima" maxima)
  227. (const :tag "Matlab" matlab)
  228. (const :tag "Mscgen" mscgen)
  229. (const :tag "Ocaml" ocaml)
  230. (const :tag "Octave" octave)
  231. (const :tag "Org" org)
  232. (const :tag "Perl" perl)
  233. (const :tag "Pico Lisp" picolisp)
  234. (const :tag "PlantUML" plantuml)
  235. (const :tag "Python" python)
  236. (const :tag "Ruby" ruby)
  237. (const :tag "Sass" sass)
  238. (const :tag "Scala" scala)
  239. (const :tag "Scheme" scheme)
  240. (const :tag "Screen" screen)
  241. (const :tag "Shell Script" sh)
  242. (const :tag "Shen" shen)
  243. (const :tag "Sql" sql)
  244. (const :tag "Sqlite" sqlite))
  245. :value-type (boolean :tag "Activate" :value t)))
  246. ;;;; Customization variables
  247. (defcustom org-clone-delete-id nil
  248. "Remove ID property of clones of a subtree.
  249. When non-nil, clones of a subtree don't inherit the ID property.
  250. Otherwise they inherit the ID property with a new unique
  251. identifier."
  252. :type 'boolean
  253. :version "24.1"
  254. :group 'org-id)
  255. ;;; Version
  256. (org-check-version)
  257. ;;;###autoload
  258. (defun org-version (&optional here full message)
  259. "Show the org-mode version in the echo area.
  260. With prefix argument HERE, insert it at point.
  261. When FULL is non-nil, use a verbose version string.
  262. When MESSAGE is non-nil, display a message with the version."
  263. (interactive "P")
  264. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  265. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  266. (load-suffixes (list ".el"))
  267. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  268. (org-trash (or
  269. (and (fboundp 'org-release) (fboundp 'org-git-version))
  270. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  271. (load-suffixes save-load-suffixes)
  272. (org-version (org-release))
  273. (git-version (org-git-version))
  274. (version (format "Org-mode version %s (%s @ %s)"
  275. org-version
  276. git-version
  277. (if org-install-dir
  278. (if (string= org-dir org-install-dir)
  279. org-install-dir
  280. (concat "mixed installation! " org-install-dir " and " org-dir))
  281. "org-loaddefs.el can not be found!")))
  282. (_version (if full version org-version)))
  283. (if (org-called-interactively-p 'interactive)
  284. (if here
  285. (insert version)
  286. (message version))
  287. (if message (message _version))
  288. _version)))
  289. (defconst org-version (org-version))
  290. ;;; Compatibility constants
  291. ;;; The custom variables
  292. (defgroup org nil
  293. "Outline-based notes management and organizer."
  294. :tag "Org"
  295. :group 'outlines
  296. :group 'calendar)
  297. (defcustom org-mode-hook nil
  298. "Mode hook for Org-mode, run after the mode was turned on."
  299. :group 'org
  300. :type 'hook)
  301. (defcustom org-load-hook nil
  302. "Hook that is run after org.el has been loaded."
  303. :group 'org
  304. :type 'hook)
  305. (defcustom org-log-buffer-setup-hook nil
  306. "Hook that is run after an Org log buffer is created."
  307. :group 'org
  308. :version "24.1"
  309. :type 'hook)
  310. (defvar org-modules) ; defined below
  311. (defvar org-modules-loaded nil
  312. "Have the modules been loaded already?")
  313. (defun org-load-modules-maybe (&optional force)
  314. "Load all extensions listed in `org-modules'."
  315. (when (or force (not org-modules-loaded))
  316. (mapc (lambda (ext)
  317. (condition-case nil (require ext)
  318. (error (message "Problems while trying to load feature `%s'" ext))))
  319. org-modules)
  320. (setq org-modules-loaded t)))
  321. (defun org-set-modules (var value)
  322. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  323. (set var value)
  324. (when (featurep 'org)
  325. (org-load-modules-maybe 'force)))
  326. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  327. "Modules that should always be loaded together with org.el.
  328. If a description starts with <C>, the file is not part of Emacs
  329. and loading it will require that you have downloaded and properly
  330. installed the Org mode distribution.
  331. You can also use this system to load external packages (i.e. neither Org
  332. core modules, nor modules from the CONTRIB directory). Just add symbols
  333. to the end of the list. If the package is called org-xyz.el, then you need
  334. to add the symbol `xyz', and the package must have a call to:
  335. \(provide 'org-xyz)
  336. For export specific modules, see also `org-export-backends'."
  337. :group 'org
  338. :set 'org-set-modules
  339. :version "24.4"
  340. :package-version '(Org . "8.0")
  341. :type
  342. '(set :greedy t
  343. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  344. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  345. (const :tag " crypt: Encryption of subtrees" org-crypt)
  346. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  347. (const :tag " docview: Links to doc-view buffers" org-docview)
  348. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  349. (const :tag " habit: Track your consistency with habits" org-habit)
  350. (const :tag " id: Global IDs for identifying entries" org-id)
  351. (const :tag " info: Links to Info nodes" org-info)
  352. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  353. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  354. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  355. (const :tag " mouse: Additional mouse support" org-mouse)
  356. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  357. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  358. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  359. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  360. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  361. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  362. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  363. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  364. (const :tag "C collector: Collect properties into tables" org-collector)
  365. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  366. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  367. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  368. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  369. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  370. (const :tag "C eval: Include command output as text" org-eval)
  371. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  372. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  373. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  374. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  375. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  376. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  377. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  378. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  379. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  380. (const :tag "C mac-message: Links to messages in Apple Mail" org-mac-message)
  381. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  382. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  383. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  384. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  385. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  386. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  387. (const :tag "C registry: A registry for Org-mode links" org-registry)
  388. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  389. (const :tag "C secretary: Team management with org-mode" org-secretary)
  390. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  391. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  392. (const :tag "C track: Keep up with Org-mode development" org-track)
  393. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  394. (const :tag "C vm: Links to VM folders/messages" org-vm)
  395. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  396. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  397. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  398. (defvar org-export-registered-backends) ; From ox.el
  399. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  400. (defcustom org-export-backends '(ascii html icalendar latex)
  401. "List of export back-ends that should be always available.
  402. If a description starts with <C>, the file is not part of Emacs
  403. and loading it will require that you have downloaded and properly
  404. installed the Org mode distribution.
  405. Unlike to `org-modules', libraries in this list will not be
  406. loaded along with Org, but only once the export framework is
  407. needed.
  408. This variable needs to be set before org.el is loaded. If you
  409. need to make a change while Emacs is running, use the customize
  410. interface or run the following code, where VALUE stands for the
  411. new value of the variable, after updating it:
  412. \(progn
  413. \(setq org-export-registered-backends
  414. \(org-remove-if-not
  415. \(lambda (backend)
  416. \(or (memq backend val)
  417. \(catch 'parentp
  418. \(mapc
  419. \(lambda (b)
  420. \(and (org-export-derived-backend-p b (car backend))
  421. \(throw 'parentp t)))
  422. val)
  423. nil)))
  424. org-export-registered-backends))
  425. \(let ((new-list (mapcar 'car org-export-registered-backends)))
  426. \(dolist (backend val)
  427. \(cond
  428. \((not (load (format \"ox-%s\" backend) t t))
  429. \(message \"Problems while trying to load export back-end `%s'\"
  430. backend))
  431. \((not (memq backend new-list)) (push backend new-list))))
  432. \(set-default var new-list)))
  433. Adding a back-end to this list will also pull the back-end it
  434. depends on, if any."
  435. :group 'org
  436. :group 'org-export
  437. :version "24.4"
  438. :package-version '(Org . "8.0")
  439. :initialize 'custom-initialize-set
  440. :set (lambda (var val)
  441. (if (not (featurep 'ox)) (set-default var val)
  442. ;; Any back-end not required anymore (not present in VAL and not
  443. ;; a parent of any back-end in the new value) is removed from the
  444. ;; list of registered back-ends.
  445. (setq org-export-registered-backends
  446. (org-remove-if-not
  447. (lambda (backend)
  448. (or (memq backend val)
  449. (catch 'parentp
  450. (mapc
  451. (lambda (b)
  452. (and (org-export-derived-backend-p b (car backend))
  453. (throw 'parentp t)))
  454. val)
  455. nil)))
  456. org-export-registered-backends))
  457. ;; Now build NEW-LIST of both new back-ends and required
  458. ;; parents.
  459. (let ((new-list (mapcar 'car org-export-registered-backends)))
  460. (dolist (backend val)
  461. (cond
  462. ((not (load (format "ox-%s" backend) t t))
  463. (message "Problems while trying to load export back-end `%s'"
  464. backend))
  465. ((not (memq backend new-list)) (push backend new-list))))
  466. ;; Set VAR to that list with fixed dependencies.
  467. (set-default var new-list))))
  468. :type '(set :greedy t
  469. (const :tag " ascii Export buffer to ASCII format" ascii)
  470. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  471. (const :tag " html Export buffer to HTML format" html)
  472. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  473. (const :tag " latex Export buffer to LaTeX format" latex)
  474. (const :tag " man Export buffer to MAN format" man)
  475. (const :tag " md Export buffer to Markdown format" md)
  476. (const :tag " odt Export buffer to ODT format" odt)
  477. (const :tag " org Export buffer to Org format" org)
  478. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  479. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  480. (const :tag "C deck Export buffer to deck.js presentations" deck)
  481. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  482. (const :tag "C groff Export buffer to Groff format" groff)
  483. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  484. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  485. (const :tag "C s5 Export buffer to s5 presentations" s5)
  486. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  487. (eval-after-load 'ox
  488. '(mapc
  489. (lambda (backend)
  490. (condition-case nil (require (intern (format "ox-%s" backend)))
  491. (error (message "Problems while trying to load export back-end `%s'"
  492. backend))))
  493. org-export-backends))
  494. (defcustom org-support-shift-select nil
  495. "Non-nil means make shift-cursor commands select text when possible.
  496. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  497. start selecting a region, or enlarge regions started in this way.
  498. In Org-mode, in special contexts, these same keys are used for
  499. other purposes, important enough to compete with shift selection.
  500. Org tries to balance these needs by supporting `shift-select-mode'
  501. outside these special contexts, under control of this variable.
  502. The default of this variable is nil, to avoid confusing behavior. Shifted
  503. cursor keys will then execute Org commands in the following contexts:
  504. - on a headline, changing TODO state (left/right) and priority (up/down)
  505. - on a time stamp, changing the time
  506. - in a plain list item, changing the bullet type
  507. - in a property definition line, switching between allowed values
  508. - in the BEGIN line of a clock table (changing the time block).
  509. Outside these contexts, the commands will throw an error.
  510. When this variable is t and the cursor is not in a special
  511. context, Org-mode will support shift-selection for making and
  512. enlarging regions. To make this more effective, the bullet
  513. cycling will no longer happen anywhere in an item line, but only
  514. if the cursor is exactly on the bullet.
  515. If you set this variable to the symbol `always', then the keys
  516. will not be special in headlines, property lines, and item lines,
  517. to make shift selection work there as well. If this is what you
  518. want, you can use the following alternative commands: `C-c C-t'
  519. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  520. can be used to switch TODO sets, `C-c -' to cycle item bullet
  521. types, and properties can be edited by hand or in column view.
  522. However, when the cursor is on a timestamp, shift-cursor commands
  523. will still edit the time stamp - this is just too good to give up.
  524. XEmacs user should have this variable set to nil, because
  525. `shift-select-mode' is in Emacs 23 or later only."
  526. :group 'org
  527. :type '(choice
  528. (const :tag "Never" nil)
  529. (const :tag "When outside special context" t)
  530. (const :tag "Everywhere except timestamps" always)))
  531. (defcustom org-loop-over-headlines-in-active-region nil
  532. "Shall some commands act upon headlines in the active region?
  533. When set to `t', some commands will be performed in all headlines
  534. within the active region.
  535. When set to `start-level', some commands will be performed in all
  536. headlines within the active region, provided that these headlines
  537. are of the same level than the first one.
  538. When set to a string, those commands will be performed on the
  539. matching headlines within the active region. Such string must be
  540. a tags/property/todo match as it is used in the agenda tags view.
  541. The list of commands is: `org-schedule', `org-deadline',
  542. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  543. `org-archive-to-archive-sibling'. The archiving commands skip
  544. already archived entries."
  545. :type '(choice (const :tag "Don't loop" nil)
  546. (const :tag "All headlines in active region" t)
  547. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  548. (string :tag "Tags/Property/Todo matcher"))
  549. :version "24.1"
  550. :group 'org-todo
  551. :group 'org-archive)
  552. (defgroup org-startup nil
  553. "Options concerning startup of Org-mode."
  554. :tag "Org Startup"
  555. :group 'org)
  556. (defcustom org-startup-folded t
  557. "Non-nil means entering Org-mode will switch to OVERVIEW.
  558. This can also be configured on a per-file basis by adding one of
  559. the following lines anywhere in the buffer:
  560. #+STARTUP: fold (or `overview', this is equivalent)
  561. #+STARTUP: nofold (or `showall', this is equivalent)
  562. #+STARTUP: content
  563. #+STARTUP: showeverything
  564. By default, this option is ignored when Org opens agenda files
  565. for the first time. If you want the agenda to honor the startup
  566. option, set `org-agenda-inhibit-startup' to nil."
  567. :group 'org-startup
  568. :type '(choice
  569. (const :tag "nofold: show all" nil)
  570. (const :tag "fold: overview" t)
  571. (const :tag "content: all headlines" content)
  572. (const :tag "show everything, even drawers" showeverything)))
  573. (defcustom org-startup-truncated t
  574. "Non-nil means entering Org-mode will set `truncate-lines'.
  575. This is useful since some lines containing links can be very long and
  576. uninteresting. Also tables look terrible when wrapped."
  577. :group 'org-startup
  578. :type 'boolean)
  579. (defcustom org-startup-indented nil
  580. "Non-nil means turn on `org-indent-mode' on startup.
  581. This can also be configured on a per-file basis by adding one of
  582. the following lines anywhere in the buffer:
  583. #+STARTUP: indent
  584. #+STARTUP: noindent"
  585. :group 'org-structure
  586. :type '(choice
  587. (const :tag "Not" nil)
  588. (const :tag "Globally (slow on startup in large files)" t)))
  589. (defcustom org-use-sub-superscripts t
  590. "Non-nil means interpret \"_\" and \"^\" for display.
  591. When this option is turned on, you can use TeX-like syntax for sub- and
  592. superscripts. Several characters after \"_\" or \"^\" will be
  593. considered as a single item - so grouping with {} is normally not
  594. needed. For example, the following things will be parsed as single
  595. sub- or superscripts.
  596. 10^24 or 10^tau several digits will be considered 1 item.
  597. 10^-12 or 10^-tau a leading sign with digits or a word
  598. x^2-y^3 will be read as x^2 - y^3, because items are
  599. terminated by almost any nonword/nondigit char.
  600. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  601. Still, ambiguity is possible - so when in doubt use {} to enclose
  602. the sub/superscript. If you set this variable to the symbol
  603. `{}', the braces are *required* in order to trigger
  604. interpretations as sub/superscript. This can be helpful in
  605. documents that need \"_\" frequently in plain text."
  606. :group 'org-startup
  607. :version "24.1"
  608. :type '(choice
  609. (const :tag "Always interpret" t)
  610. (const :tag "Only with braces" {})
  611. (const :tag "Never interpret" nil)))
  612. (defcustom org-startup-with-beamer-mode nil
  613. "Non-nil means turn on `org-beamer-mode' on startup.
  614. This can also be configured on a per-file basis by adding one of
  615. the following lines anywhere in the buffer:
  616. #+STARTUP: beamer"
  617. :group 'org-startup
  618. :version "24.1"
  619. :type 'boolean)
  620. (defcustom org-startup-align-all-tables nil
  621. "Non-nil means align all tables when visiting a file.
  622. This is useful when the column width in tables is forced with <N> cookies
  623. in table fields. Such tables will look correct only after the first re-align.
  624. This can also be configured on a per-file basis by adding one of
  625. the following lines anywhere in the buffer:
  626. #+STARTUP: align
  627. #+STARTUP: noalign"
  628. :group 'org-startup
  629. :type 'boolean)
  630. (defcustom org-startup-with-inline-images nil
  631. "Non-nil means show inline images when loading a new Org file.
  632. This can also be configured on a per-file basis by adding one of
  633. the following lines anywhere in the buffer:
  634. #+STARTUP: inlineimages
  635. #+STARTUP: noinlineimages"
  636. :group 'org-startup
  637. :version "24.1"
  638. :type 'boolean)
  639. (defcustom org-startup-with-latex-preview nil
  640. "Non-nil means preview LaTeX fragments when loading a new Org file.
  641. This can also be configured on a per-file basis by adding one of
  642. the followinglines anywhere in the buffer:
  643. #+STARTUP: latexpreview
  644. #+STARTUP: nolatexpreview"
  645. :group 'org-startup
  646. :version "24.4"
  647. :package-version '(Org . "8.0")
  648. :type 'boolean)
  649. (defcustom org-insert-mode-line-in-empty-file nil
  650. "Non-nil means insert the first line setting Org-mode in empty files.
  651. When the function `org-mode' is called interactively in an empty file, this
  652. normally means that the file name does not automatically trigger Org-mode.
  653. To ensure that the file will always be in Org-mode in the future, a
  654. line enforcing Org-mode will be inserted into the buffer, if this option
  655. has been set."
  656. :group 'org-startup
  657. :type 'boolean)
  658. (defcustom org-replace-disputed-keys nil
  659. "Non-nil means use alternative key bindings for some keys.
  660. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  661. These keys are also used by other packages like shift-selection-mode'
  662. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  663. If you want to use Org-mode together with one of these other modes,
  664. or more generally if you would like to move some Org-mode commands to
  665. other keys, set this variable and configure the keys with the variable
  666. `org-disputed-keys'.
  667. This option is only relevant at load-time of Org-mode, and must be set
  668. *before* org.el is loaded. Changing it requires a restart of Emacs to
  669. become effective."
  670. :group 'org-startup
  671. :type 'boolean)
  672. (defcustom org-use-extra-keys nil
  673. "Non-nil means use extra key sequence definitions for certain commands.
  674. This happens automatically if you run XEmacs or if `window-system'
  675. is nil. This variable lets you do the same manually. You must
  676. set it before loading org.
  677. Example: on Carbon Emacs 22 running graphically, with an external
  678. keyboard on a Powerbook, the default way of setting M-left might
  679. not work for either Alt or ESC. Setting this variable will make
  680. it work for ESC."
  681. :group 'org-startup
  682. :type 'boolean)
  683. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  684. (defcustom org-disputed-keys
  685. '(([(shift up)] . [(meta p)])
  686. ([(shift down)] . [(meta n)])
  687. ([(shift left)] . [(meta -)])
  688. ([(shift right)] . [(meta +)])
  689. ([(control shift right)] . [(meta shift +)])
  690. ([(control shift left)] . [(meta shift -)]))
  691. "Keys for which Org-mode and other modes compete.
  692. This is an alist, cars are the default keys, second element specifies
  693. the alternative to use when `org-replace-disputed-keys' is t.
  694. Keys can be specified in any syntax supported by `define-key'.
  695. The value of this option takes effect only at Org-mode's startup,
  696. therefore you'll have to restart Emacs to apply it after changing."
  697. :group 'org-startup
  698. :type 'alist)
  699. (defun org-key (key)
  700. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  701. Or return the original if not disputed.
  702. Also apply the translations defined in `org-xemacs-key-equivalents'."
  703. (when org-replace-disputed-keys
  704. (let* ((nkey (key-description key))
  705. (x (org-find-if (lambda (x)
  706. (equal (key-description (car x)) nkey))
  707. org-disputed-keys)))
  708. (setq key (if x (cdr x) key))))
  709. (when (featurep 'xemacs)
  710. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  711. key)
  712. (defun org-find-if (predicate seq)
  713. (catch 'exit
  714. (while seq
  715. (if (funcall predicate (car seq))
  716. (throw 'exit (car seq))
  717. (pop seq)))))
  718. (defun org-defkey (keymap key def)
  719. "Define a key, possibly translated, as returned by `org-key'."
  720. (define-key keymap (org-key key) def))
  721. (defcustom org-ellipsis nil
  722. "The ellipsis to use in the Org-mode outline.
  723. When nil, just use the standard three dots. When a string, use that instead,
  724. When a face, use the standard 3 dots, but with the specified face.
  725. The change affects only Org-mode (which will then use its own display table).
  726. Changing this requires executing `M-x org-mode' in a buffer to become
  727. effective."
  728. :group 'org-startup
  729. :type '(choice (const :tag "Default" nil)
  730. (face :tag "Face" :value org-warning)
  731. (string :tag "String" :value "...#")))
  732. (defvar org-display-table nil
  733. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  734. (defgroup org-keywords nil
  735. "Keywords in Org-mode."
  736. :tag "Org Keywords"
  737. :group 'org)
  738. (defcustom org-deadline-string "DEADLINE:"
  739. "String to mark deadline entries.
  740. A deadline is this string, followed by a time stamp. Should be a word,
  741. terminated by a colon. You can insert a schedule keyword and
  742. a timestamp with \\[org-deadline].
  743. Changes become only effective after restarting Emacs."
  744. :group 'org-keywords
  745. :type 'string)
  746. (defcustom org-scheduled-string "SCHEDULED:"
  747. "String to mark scheduled TODO entries.
  748. A schedule is this string, followed by a time stamp. Should be a word,
  749. terminated by a colon. You can insert a schedule keyword and
  750. a timestamp with \\[org-schedule].
  751. Changes become only effective after restarting Emacs."
  752. :group 'org-keywords
  753. :type 'string)
  754. (defcustom org-closed-string "CLOSED:"
  755. "String used as the prefix for timestamps logging closing a TODO entry."
  756. :group 'org-keywords
  757. :type 'string)
  758. (defcustom org-clock-string "CLOCK:"
  759. "String used as prefix for timestamps clocking work hours on an item."
  760. :group 'org-keywords
  761. :type 'string)
  762. (defcustom org-closed-keep-when-no-todo nil
  763. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  764. :group 'org-todo
  765. :group 'org-keywords
  766. :version "24.4"
  767. :package-version '(Org . "8.0")
  768. :type 'boolean)
  769. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  770. org-scheduled-string "\\|"
  771. org-deadline-string "\\|"
  772. org-closed-string "\\|"
  773. org-clock-string "\\)")
  774. "Matches a line with planning or clock info.")
  775. (defcustom org-comment-string "COMMENT"
  776. "Entries starting with this keyword will never be exported.
  777. An entry can be toggled between COMMENT and normal with
  778. \\[org-toggle-comment].
  779. Changes become only effective after restarting Emacs."
  780. :group 'org-keywords
  781. :type 'string)
  782. (defcustom org-quote-string "QUOTE"
  783. "Entries starting with this keyword will be exported in fixed-width font.
  784. Quoting applies only to the text in the entry following the headline, and does
  785. not extend beyond the next headline, even if that is lower level.
  786. An entry can be toggled between QUOTE and normal with
  787. \\[org-toggle-fixed-width-section]."
  788. :group 'org-keywords
  789. :type 'string)
  790. (defconst org-repeat-re
  791. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  792. "Regular expression for specifying repeated events.
  793. After a match, group 1 contains the repeat expression.")
  794. (defgroup org-structure nil
  795. "Options concerning the general structure of Org-mode files."
  796. :tag "Org Structure"
  797. :group 'org)
  798. (defgroup org-reveal-location nil
  799. "Options about how to make context of a location visible."
  800. :tag "Org Reveal Location"
  801. :group 'org-structure)
  802. (defconst org-context-choice
  803. '(choice
  804. (const :tag "Always" t)
  805. (const :tag "Never" nil)
  806. (repeat :greedy t :tag "Individual contexts"
  807. (cons
  808. (choice :tag "Context"
  809. (const agenda)
  810. (const org-goto)
  811. (const occur-tree)
  812. (const tags-tree)
  813. (const link-search)
  814. (const mark-goto)
  815. (const bookmark-jump)
  816. (const isearch)
  817. (const default))
  818. (boolean))))
  819. "Contexts for the reveal options.")
  820. (defcustom org-show-hierarchy-above '((default . t))
  821. "Non-nil means show full hierarchy when revealing a location.
  822. Org-mode often shows locations in an org-mode file which might have
  823. been invisible before. When this is set, the hierarchy of headings
  824. above the exposed location is shown.
  825. Turning this off for example for sparse trees makes them very compact.
  826. Instead of t, this can also be an alist specifying this option for different
  827. contexts. Valid contexts are
  828. agenda when exposing an entry from the agenda
  829. org-goto when using the command `org-goto' on key C-c C-j
  830. occur-tree when using the command `org-occur' on key C-c /
  831. tags-tree when constructing a sparse tree based on tags matches
  832. link-search when exposing search matches associated with a link
  833. mark-goto when exposing the jump goal of a mark
  834. bookmark-jump when exposing a bookmark location
  835. isearch when exiting from an incremental search
  836. default default for all contexts not set explicitly"
  837. :group 'org-reveal-location
  838. :type org-context-choice)
  839. (defcustom org-show-following-heading '((default . nil))
  840. "Non-nil means show following heading when revealing a location.
  841. Org-mode often shows locations in an org-mode file which might have
  842. been invisible before. When this is set, the heading following the
  843. match is shown.
  844. Turning this off for example for sparse trees makes them very compact,
  845. but makes it harder to edit the location of the match. In such a case,
  846. use the command \\[org-reveal] to show more context.
  847. Instead of t, this can also be an alist specifying this option for different
  848. contexts. See `org-show-hierarchy-above' for valid contexts."
  849. :group 'org-reveal-location
  850. :type org-context-choice)
  851. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  852. "Non-nil means show all sibling heading when revealing a location.
  853. Org-mode often shows locations in an org-mode file which might have
  854. been invisible before. When this is set, the sibling of the current entry
  855. heading are all made visible. If `org-show-hierarchy-above' is t,
  856. the same happens on each level of the hierarchy above the current entry.
  857. By default this is on for the isearch context, off for all other contexts.
  858. Turning this off for example for sparse trees makes them very compact,
  859. but makes it harder to edit the location of the match. In such a case,
  860. use the command \\[org-reveal] to show more context.
  861. Instead of t, this can also be an alist specifying this option for different
  862. contexts. See `org-show-hierarchy-above' for valid contexts."
  863. :group 'org-reveal-location
  864. :type org-context-choice
  865. :version "24.4"
  866. :package-version '(Org . "8.0"))
  867. (defcustom org-show-entry-below '((default . nil))
  868. "Non-nil means show the entry below a headline when revealing a location.
  869. Org-mode often shows locations in an org-mode file which might have
  870. been invisible before. When this is set, the text below the headline that is
  871. exposed is also shown.
  872. By default this is off for all contexts.
  873. Instead of t, this can also be an alist specifying this option for different
  874. contexts. See `org-show-hierarchy-above' for valid contexts."
  875. :group 'org-reveal-location
  876. :type org-context-choice)
  877. (defcustom org-indirect-buffer-display 'other-window
  878. "How should indirect tree buffers be displayed?
  879. This applies to indirect buffers created with the commands
  880. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  881. Valid values are:
  882. current-window Display in the current window
  883. other-window Just display in another window.
  884. dedicated-frame Create one new frame, and re-use it each time.
  885. new-frame Make a new frame each time. Note that in this case
  886. previously-made indirect buffers are kept, and you need to
  887. kill these buffers yourself."
  888. :group 'org-structure
  889. :group 'org-agenda-windows
  890. :type '(choice
  891. (const :tag "In current window" current-window)
  892. (const :tag "In current frame, other window" other-window)
  893. (const :tag "Each time a new frame" new-frame)
  894. (const :tag "One dedicated frame" dedicated-frame)))
  895. (defcustom org-use-speed-commands nil
  896. "Non-nil means activate single letter commands at beginning of a headline.
  897. This may also be a function to test for appropriate locations where speed
  898. commands should be active."
  899. :group 'org-structure
  900. :type '(choice
  901. (const :tag "Never" nil)
  902. (const :tag "At beginning of headline stars" t)
  903. (function)))
  904. (defcustom org-speed-commands-user nil
  905. "Alist of additional speed commands.
  906. This list will be checked before `org-speed-commands-default'
  907. when the variable `org-use-speed-commands' is non-nil
  908. and when the cursor is at the beginning of a headline.
  909. The car if each entry is a string with a single letter, which must
  910. be assigned to `self-insert-command' in the global map.
  911. The cdr is either a command to be called interactively, a function
  912. to be called, or a form to be evaluated.
  913. An entry that is just a list with a single string will be interpreted
  914. as a descriptive headline that will be added when listing the speed
  915. commands in the Help buffer using the `?' speed command."
  916. :group 'org-structure
  917. :type '(repeat :value ("k" . ignore)
  918. (choice :value ("k" . ignore)
  919. (list :tag "Descriptive Headline" (string :tag "Headline"))
  920. (cons :tag "Letter and Command"
  921. (string :tag "Command letter")
  922. (choice
  923. (function)
  924. (sexp))))))
  925. (defgroup org-cycle nil
  926. "Options concerning visibility cycling in Org-mode."
  927. :tag "Org Cycle"
  928. :group 'org-structure)
  929. (defcustom org-cycle-skip-children-state-if-no-children t
  930. "Non-nil means skip CHILDREN state in entries that don't have any."
  931. :group 'org-cycle
  932. :type 'boolean)
  933. (defcustom org-cycle-max-level nil
  934. "Maximum level which should still be subject to visibility cycling.
  935. Levels higher than this will, for cycling, be treated as text, not a headline.
  936. When `org-odd-levels-only' is set, a value of N in this variable actually
  937. means 2N-1 stars as the limiting headline.
  938. When nil, cycle all levels.
  939. Note that the limiting level of cycling is also influenced by
  940. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  941. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  942. than its value."
  943. :group 'org-cycle
  944. :type '(choice
  945. (const :tag "No limit" nil)
  946. (integer :tag "Maximum level")))
  947. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  948. "Names of drawers. Drawers are not opened by cycling on the headline above.
  949. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  950. this:
  951. :DRAWERNAME:
  952. .....
  953. :END:
  954. The drawer \"PROPERTIES\" is special for capturing properties through
  955. the property API.
  956. Drawers can be defined on the per-file basis with a line like:
  957. #+DRAWERS: HIDDEN STATE PROPERTIES"
  958. :group 'org-structure
  959. :group 'org-cycle
  960. :type '(repeat (string :tag "Drawer Name")))
  961. (defcustom org-hide-block-startup nil
  962. "Non-nil means entering Org-mode will fold all blocks.
  963. This can also be set in on a per-file basis with
  964. #+STARTUP: hideblocks
  965. #+STARTUP: showblocks"
  966. :group 'org-startup
  967. :group 'org-cycle
  968. :type 'boolean)
  969. (defcustom org-cycle-global-at-bob nil
  970. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  971. This makes it possible to do global cycling without having to use S-TAB or
  972. \\[universal-argument] TAB. For this special case to work, the first line
  973. of the buffer must not be a headline -- it may be empty or some other text.
  974. When used in this way, `org-cycle-hook' is disabled temporarily to make
  975. sure the cursor stays at the beginning of the buffer. When this option is
  976. nil, don't do anything special at the beginning of the buffer."
  977. :group 'org-cycle
  978. :type 'boolean)
  979. (defcustom org-cycle-level-after-item/entry-creation t
  980. "Non-nil means cycle entry level or item indentation in new empty entries.
  981. When the cursor is at the end of an empty headline, i.e., with only stars
  982. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  983. and then all possible ancestor states, before returning to the original state.
  984. This makes data entry extremely fast: M-RET to create a new headline,
  985. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  986. When the cursor is at the end of an empty plain list item, one TAB will
  987. make it a subitem, two or more tabs will back up to make this an item
  988. higher up in the item hierarchy."
  989. :group 'org-cycle
  990. :type 'boolean)
  991. (defcustom org-cycle-emulate-tab t
  992. "Where should `org-cycle' emulate TAB.
  993. nil Never
  994. white Only in completely white lines
  995. whitestart Only at the beginning of lines, before the first non-white char
  996. t Everywhere except in headlines
  997. exc-hl-bol Everywhere except at the start of a headline
  998. If TAB is used in a place where it does not emulate TAB, the current subtree
  999. visibility is cycled."
  1000. :group 'org-cycle
  1001. :type '(choice (const :tag "Never" nil)
  1002. (const :tag "Only in completely white lines" white)
  1003. (const :tag "Before first char in a line" whitestart)
  1004. (const :tag "Everywhere except in headlines" t)
  1005. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1006. (defcustom org-cycle-separator-lines 2
  1007. "Number of empty lines needed to keep an empty line between collapsed trees.
  1008. If you leave an empty line between the end of a subtree and the following
  1009. headline, this empty line is hidden when the subtree is folded.
  1010. Org-mode will leave (exactly) one empty line visible if the number of
  1011. empty lines is equal or larger to the number given in this variable.
  1012. So the default 2 means at least 2 empty lines after the end of a subtree
  1013. are needed to produce free space between a collapsed subtree and the
  1014. following headline.
  1015. If the number is negative, and the number of empty lines is at least -N,
  1016. all empty lines are shown.
  1017. Special case: when 0, never leave empty lines in collapsed view."
  1018. :group 'org-cycle
  1019. :type 'integer)
  1020. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1021. (defcustom org-pre-cycle-hook nil
  1022. "Hook that is run before visibility cycling is happening.
  1023. The function(s) in this hook must accept a single argument which indicates
  1024. the new state that will be set right after running this hook. The
  1025. argument is a symbol. Before a global state change, it can have the values
  1026. `overview', `content', or `all'. Before a local state change, it can have
  1027. the values `folded', `children', or `subtree'."
  1028. :group 'org-cycle
  1029. :type 'hook)
  1030. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1031. org-cycle-hide-drawers
  1032. org-cycle-hide-inline-tasks
  1033. org-cycle-show-empty-lines
  1034. org-optimize-window-after-visibility-change)
  1035. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1036. The function(s) in this hook must accept a single argument which indicates
  1037. the new state that was set by the most recent `org-cycle' command. The
  1038. argument is a symbol. After a global state change, it can have the values
  1039. `overview', `contents', or `all'. After a local state change, it can have
  1040. the values `folded', `children', or `subtree'."
  1041. :group 'org-cycle
  1042. :type 'hook)
  1043. (defgroup org-edit-structure nil
  1044. "Options concerning structure editing in Org-mode."
  1045. :tag "Org Edit Structure"
  1046. :group 'org-structure)
  1047. (defcustom org-odd-levels-only nil
  1048. "Non-nil means skip even levels and only use odd levels for the outline.
  1049. This has the effect that two stars are being added/taken away in
  1050. promotion/demotion commands. It also influences how levels are
  1051. handled by the exporters.
  1052. Changing it requires restart of `font-lock-mode' to become effective
  1053. for fontification also in regions already fontified.
  1054. You may also set this on a per-file basis by adding one of the following
  1055. lines to the buffer:
  1056. #+STARTUP: odd
  1057. #+STARTUP: oddeven"
  1058. :group 'org-edit-structure
  1059. :group 'org-appearance
  1060. :type 'boolean)
  1061. (defcustom org-adapt-indentation t
  1062. "Non-nil means adapt indentation to outline node level.
  1063. When this variable is set, Org assumes that you write outlines by
  1064. indenting text in each node to align with the headline (after the stars).
  1065. The following issues are influenced by this variable:
  1066. - When this is set and the *entire* text in an entry is indented, the
  1067. indentation is increased by one space in a demotion command, and
  1068. decreased by one in a promotion command. If any line in the entry
  1069. body starts with text at column 0, indentation is not changed at all.
  1070. - Property drawers and planning information is inserted indented when
  1071. this variable s set. When nil, they will not be indented.
  1072. - TAB indents a line relative to context. The lines below a headline
  1073. will be indented when this variable is set.
  1074. Note that this is all about true indentation, by adding and removing
  1075. space characters. See also `org-indent.el' which does level-dependent
  1076. indentation in a virtual way, i.e. at display time in Emacs."
  1077. :group 'org-edit-structure
  1078. :type 'boolean)
  1079. (defcustom org-special-ctrl-a/e nil
  1080. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1081. When t, `C-a' will bring back the cursor to the beginning of the
  1082. headline text, i.e. after the stars and after a possible TODO
  1083. keyword. In an item, this will be the position after bullet and
  1084. check-box, if any. When the cursor is already at that position,
  1085. another `C-a' will bring it to the beginning of the line.
  1086. `C-e' will jump to the end of the headline, ignoring the presence
  1087. of tags in the headline. A second `C-e' will then jump to the
  1088. true end of the line, after any tags. This also means that, when
  1089. this variable is non-nil, `C-e' also will never jump beyond the
  1090. end of the heading of a folded section, i.e. not after the
  1091. ellipses.
  1092. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1093. normally, going to the true line boundary first. Only a directly
  1094. following, identical keypress will bring the cursor to the
  1095. special positions.
  1096. This may also be a cons cell where the behavior for `C-a' and
  1097. `C-e' is set separately."
  1098. :group 'org-edit-structure
  1099. :type '(choice
  1100. (const :tag "off" nil)
  1101. (const :tag "on: after stars/bullet and before tags first" t)
  1102. (const :tag "reversed: true line boundary first" reversed)
  1103. (cons :tag "Set C-a and C-e separately"
  1104. (choice :tag "Special C-a"
  1105. (const :tag "off" nil)
  1106. (const :tag "on: after stars/bullet first" t)
  1107. (const :tag "reversed: before stars/bullet first" reversed))
  1108. (choice :tag "Special C-e"
  1109. (const :tag "off" nil)
  1110. (const :tag "on: before tags first" t)
  1111. (const :tag "reversed: after tags first" reversed)))))
  1112. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1113. (defcustom org-special-ctrl-k nil
  1114. "Non-nil means `C-k' will behave specially in headlines.
  1115. When nil, `C-k' will call the default `kill-line' command.
  1116. When t, the following will happen while the cursor is in the headline:
  1117. - When the cursor is at the beginning of a headline, kill the entire
  1118. line and possible the folded subtree below the line.
  1119. - When in the middle of the headline text, kill the headline up to the tags.
  1120. - When after the headline text, kill the tags."
  1121. :group 'org-edit-structure
  1122. :type 'boolean)
  1123. (defcustom org-ctrl-k-protect-subtree nil
  1124. "Non-nil means, do not delete a hidden subtree with C-k.
  1125. When set to the symbol `error', simply throw an error when C-k is
  1126. used to kill (part-of) a headline that has hidden text behind it.
  1127. Any other non-nil value will result in a query to the user, if it is
  1128. OK to kill that hidden subtree. When nil, kill without remorse."
  1129. :group 'org-edit-structure
  1130. :version "24.1"
  1131. :type '(choice
  1132. (const :tag "Do not protect hidden subtrees" nil)
  1133. (const :tag "Protect hidden subtrees with a security query" t)
  1134. (const :tag "Never kill a hidden subtree with C-k" error)))
  1135. (defcustom org-catch-invisible-edits nil
  1136. "Check if in invisible region before inserting or deleting a character.
  1137. Valid values are:
  1138. nil Do not check, so just do invisible edits.
  1139. error Throw an error and do nothing.
  1140. show Make point visible, and do the requested edit.
  1141. show-and-error Make point visible, then throw an error and abort the edit.
  1142. smart Make point visible, and do insertion/deletion if it is
  1143. adjacent to visible text and the change feels predictable.
  1144. Never delete a previously invisible character or add in the
  1145. middle or right after an invisible region. Basically, this
  1146. allows insertion and backward-delete right before ellipses.
  1147. FIXME: maybe in this case we should not even show?"
  1148. :group 'org-edit-structure
  1149. :version "24.1"
  1150. :type '(choice
  1151. (const :tag "Do not check" nil)
  1152. (const :tag "Throw error when trying to edit" error)
  1153. (const :tag "Unhide, but do not do the edit" show-and-error)
  1154. (const :tag "Show invisible part and do the edit" show)
  1155. (const :tag "Be smart and do the right thing" smart)))
  1156. (defcustom org-yank-folded-subtrees t
  1157. "Non-nil means when yanking subtrees, fold them.
  1158. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1159. it starts with a heading and all other headings in it are either children
  1160. or siblings, then fold all the subtrees. However, do this only if no
  1161. text after the yank would be swallowed into a folded tree by this action."
  1162. :group 'org-edit-structure
  1163. :type 'boolean)
  1164. (defcustom org-yank-adjusted-subtrees nil
  1165. "Non-nil means when yanking subtrees, adjust the level.
  1166. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1167. this function for details."
  1168. :group 'org-edit-structure
  1169. :type 'boolean)
  1170. (defcustom org-M-RET-may-split-line '((default . t))
  1171. "Non-nil means M-RET will split the line at the cursor position.
  1172. When nil, it will go to the end of the line before making a
  1173. new line.
  1174. You may also set this option in a different way for different
  1175. contexts. Valid contexts are:
  1176. headline when creating a new headline
  1177. item when creating a new item
  1178. table in a table field
  1179. default the value to be used for all contexts not explicitly
  1180. customized"
  1181. :group 'org-structure
  1182. :group 'org-table
  1183. :type '(choice
  1184. (const :tag "Always" t)
  1185. (const :tag "Never" nil)
  1186. (repeat :greedy t :tag "Individual contexts"
  1187. (cons
  1188. (choice :tag "Context"
  1189. (const headline)
  1190. (const item)
  1191. (const table)
  1192. (const default))
  1193. (boolean)))))
  1194. (defcustom org-insert-heading-respect-content nil
  1195. "Non-nil means insert new headings after the current subtree.
  1196. When nil, the new heading is created directly after the current line.
  1197. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1198. this variable on for the duration of the command."
  1199. :group 'org-structure
  1200. :type 'boolean)
  1201. (defcustom org-blank-before-new-entry '((heading . auto)
  1202. (plain-list-item . auto))
  1203. "Should `org-insert-heading' leave a blank line before new heading/item?
  1204. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1205. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1206. which case Org will look at the surrounding headings/items and try to
  1207. make an intelligent decision whether to insert a blank line or not.
  1208. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1209. the setting here is ignored and no empty line is inserted to avoid breaking
  1210. the list structure."
  1211. :group 'org-edit-structure
  1212. :type '(list
  1213. (cons (const heading)
  1214. (choice (const :tag "Never" nil)
  1215. (const :tag "Always" t)
  1216. (const :tag "Auto" auto)))
  1217. (cons (const plain-list-item)
  1218. (choice (const :tag "Never" nil)
  1219. (const :tag "Always" t)
  1220. (const :tag "Auto" auto)))))
  1221. (defcustom org-insert-heading-hook nil
  1222. "Hook being run after inserting a new heading."
  1223. :group 'org-edit-structure
  1224. :type 'hook)
  1225. (defcustom org-enable-fixed-width-editor t
  1226. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1227. This currently only means they are never auto-wrapped.
  1228. When nil, such lines will be treated like ordinary lines.
  1229. See also the QUOTE keyword."
  1230. :group 'org-edit-structure
  1231. :type 'boolean)
  1232. (defcustom org-goto-auto-isearch t
  1233. "Non-nil means typing characters in `org-goto' starts incremental search.
  1234. When nil, you can use these keybindings to navigate the buffer:
  1235. q Quit the org-goto interface
  1236. n Go to the next visible heading
  1237. p Go to the previous visible heading
  1238. f Go one heading forward on same level
  1239. b Go one heading backward on same level
  1240. u Go one heading up"
  1241. :group 'org-edit-structure
  1242. :type 'boolean)
  1243. (defgroup org-sparse-trees nil
  1244. "Options concerning sparse trees in Org-mode."
  1245. :tag "Org Sparse Trees"
  1246. :group 'org-structure)
  1247. (defcustom org-highlight-sparse-tree-matches t
  1248. "Non-nil means highlight all matches that define a sparse tree.
  1249. The highlights will automatically disappear the next time the buffer is
  1250. changed by an edit command."
  1251. :group 'org-sparse-trees
  1252. :type 'boolean)
  1253. (defcustom org-remove-highlights-with-change t
  1254. "Non-nil means any change to the buffer will remove temporary highlights.
  1255. Such highlights are created by `org-occur' and `org-clock-display'.
  1256. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1257. The highlights created by `org-preview-latex-fragment' always need
  1258. `C-c C-c' to be removed."
  1259. :group 'org-sparse-trees
  1260. :group 'org-time
  1261. :type 'boolean)
  1262. (defcustom org-occur-hook '(org-first-headline-recenter)
  1263. "Hook that is run after `org-occur' has constructed a sparse tree.
  1264. This can be used to recenter the window to show as much of the structure
  1265. as possible."
  1266. :group 'org-sparse-trees
  1267. :type 'hook)
  1268. (defgroup org-imenu-and-speedbar nil
  1269. "Options concerning imenu and speedbar in Org-mode."
  1270. :tag "Org Imenu and Speedbar"
  1271. :group 'org-structure)
  1272. (defcustom org-imenu-depth 2
  1273. "The maximum level for Imenu access to Org-mode headlines.
  1274. This also applied for speedbar access."
  1275. :group 'org-imenu-and-speedbar
  1276. :type 'integer)
  1277. (defgroup org-table nil
  1278. "Options concerning tables in Org-mode."
  1279. :tag "Org Table"
  1280. :group 'org)
  1281. (defcustom org-enable-table-editor 'optimized
  1282. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1283. When nil, such lines will be treated like ordinary lines.
  1284. When equal to the symbol `optimized', the table editor will be optimized to
  1285. do the following:
  1286. - Automatic overwrite mode in front of whitespace in table fields.
  1287. This makes the structure of the table stay in tact as long as the edited
  1288. field does not exceed the column width.
  1289. - Minimize the number of realigns. Normally, the table is aligned each time
  1290. TAB or RET are pressed to move to another field. With optimization this
  1291. happens only if changes to a field might have changed the column width.
  1292. Optimization requires replacing the functions `self-insert-command',
  1293. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1294. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1295. very good at guessing when a re-align will be necessary, but you can always
  1296. force one with \\[org-ctrl-c-ctrl-c].
  1297. If you would like to use the optimized version in Org-mode, but the
  1298. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1299. This variable can be used to turn on and off the table editor during a session,
  1300. but in order to toggle optimization, a restart is required.
  1301. See also the variable `org-table-auto-blank-field'."
  1302. :group 'org-table
  1303. :type '(choice
  1304. (const :tag "off" nil)
  1305. (const :tag "on" t)
  1306. (const :tag "on, optimized" optimized)))
  1307. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1308. (version<= emacs-version "24.1"))
  1309. "Non-nil means cluster self-insert commands for undo when possible.
  1310. If this is set, then, like in the Emacs command loop, 20 consecutive
  1311. characters will be undone together.
  1312. This is configurable, because there is some impact on typing performance."
  1313. :group 'org-table
  1314. :type 'boolean)
  1315. (defcustom org-table-tab-recognizes-table.el t
  1316. "Non-nil means TAB will automatically notice a table.el table.
  1317. When it sees such a table, it moves point into it and - if necessary -
  1318. calls `table-recognize-table'."
  1319. :group 'org-table-editing
  1320. :type 'boolean)
  1321. (defgroup org-link nil
  1322. "Options concerning links in Org-mode."
  1323. :tag "Org Link"
  1324. :group 'org)
  1325. (defvar org-link-abbrev-alist-local nil
  1326. "Buffer-local version of `org-link-abbrev-alist', which see.
  1327. The value of this is taken from the #+LINK lines.")
  1328. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1329. (defcustom org-link-abbrev-alist nil
  1330. "Alist of link abbreviations.
  1331. The car of each element is a string, to be replaced at the start of a link.
  1332. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1333. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1334. [[linkkey:tag][description]]
  1335. The 'linkkey' must be a word word, starting with a letter, followed
  1336. by letters, numbers, '-' or '_'.
  1337. If REPLACE is a string, the tag will simply be appended to create the link.
  1338. If the string contains \"%s\", the tag will be inserted there. If the string
  1339. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1340. at that point (see the function `url-hexify-string'). If the string contains
  1341. the specifier \"%(my-function)\", then the custom function `my-function' will
  1342. be invoked: this function takes the tag as its only argument and must return
  1343. a string.
  1344. REPLACE may also be a function that will be called with the tag as the
  1345. only argument to create the link, which should be returned as a string.
  1346. See the manual for examples."
  1347. :group 'org-link
  1348. :type '(repeat
  1349. (cons
  1350. (string :tag "Protocol")
  1351. (choice
  1352. (string :tag "Format")
  1353. (function)))))
  1354. (defcustom org-descriptive-links t
  1355. "Non-nil means Org will display descriptive links.
  1356. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1357. \"Org Website\", hiding the link itself and just displaying its
  1358. description. When set to `nil', Org will display the full links
  1359. literally.
  1360. You can interactively set the value of this variable by calling
  1361. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1362. :group 'org-link
  1363. :type 'boolean)
  1364. (defcustom org-link-file-path-type 'adaptive
  1365. "How the path name in file links should be stored.
  1366. Valid values are:
  1367. relative Relative to the current directory, i.e. the directory of the file
  1368. into which the link is being inserted.
  1369. absolute Absolute path, if possible with ~ for home directory.
  1370. noabbrev Absolute path, no abbreviation of home directory.
  1371. adaptive Use relative path for files in the current directory and sub-
  1372. directories of it. For other files, use an absolute path."
  1373. :group 'org-link
  1374. :type '(choice
  1375. (const relative)
  1376. (const absolute)
  1377. (const noabbrev)
  1378. (const adaptive)))
  1379. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1380. "Types of links that should be activated in Org-mode files.
  1381. This is a list of symbols, each leading to the activation of a certain link
  1382. type. In principle, it does not hurt to turn on most link types - there may
  1383. be a small gain when turning off unused link types. The types are:
  1384. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1385. angle Links in angular brackets that may contain whitespace like
  1386. <bbdb:Carsten Dominik>.
  1387. plain Plain links in normal text, no whitespace, like http://google.com.
  1388. radio Text that is matched by a radio target, see manual for details.
  1389. tag Tag settings in a headline (link to tag search).
  1390. date Time stamps (link to calendar).
  1391. footnote Footnote labels.
  1392. Changing this variable requires a restart of Emacs to become effective."
  1393. :group 'org-link
  1394. :type '(set :greedy t
  1395. (const :tag "Double bracket links" bracket)
  1396. (const :tag "Angular bracket links" angle)
  1397. (const :tag "Plain text links" plain)
  1398. (const :tag "Radio target matches" radio)
  1399. (const :tag "Tags" tag)
  1400. (const :tag "Timestamps" date)
  1401. (const :tag "Footnotes" footnote)))
  1402. (defcustom org-make-link-description-function nil
  1403. "Function to use for generating link descriptions from links.
  1404. When nil, the link location will be used. This function must take
  1405. two parameters: the first one is the link, the second one is the
  1406. description generated by `org-insert-link'. The function should
  1407. return the description to use."
  1408. :group 'org-link
  1409. :type '(choice (const nil) (function)))
  1410. (defgroup org-link-store nil
  1411. "Options concerning storing links in Org-mode."
  1412. :tag "Org Store Link"
  1413. :group 'org-link)
  1414. (defcustom org-url-hexify-p t
  1415. "When non-nil, hexify URL when creating a link."
  1416. :type 'boolean
  1417. :version "24.3"
  1418. :group 'org-link-store)
  1419. (defcustom org-email-link-description-format "Email %c: %.30s"
  1420. "Format of the description part of a link to an email or usenet message.
  1421. The following %-escapes will be replaced by corresponding information:
  1422. %F full \"From\" field
  1423. %f name, taken from \"From\" field, address if no name
  1424. %T full \"To\" field
  1425. %t first name in \"To\" field, address if no name
  1426. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1427. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1428. %s subject
  1429. %d date
  1430. %m message-id.
  1431. You may use normal field width specification between the % and the letter.
  1432. This is for example useful to limit the length of the subject.
  1433. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1434. :group 'org-link-store
  1435. :type 'string)
  1436. (defcustom org-from-is-user-regexp
  1437. (let (r1 r2)
  1438. (when (and user-mail-address (not (string= user-mail-address "")))
  1439. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1440. (when (and user-full-name (not (string= user-full-name "")))
  1441. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1442. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1443. "Regexp matched against the \"From:\" header of an email or usenet message.
  1444. It should match if the message is from the user him/herself."
  1445. :group 'org-link-store
  1446. :type 'regexp)
  1447. (defcustom org-context-in-file-links t
  1448. "Non-nil means file links from `org-store-link' contain context.
  1449. A search string will be added to the file name with :: as separator and
  1450. used to find the context when the link is activated by the command
  1451. `org-open-at-point'. When this option is t, the entire active region
  1452. will be placed in the search string of the file link. If set to a
  1453. positive integer, only the first n lines of context will be stored.
  1454. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1455. negates this setting for the duration of the command."
  1456. :group 'org-link-store
  1457. :type '(choice boolean integer))
  1458. (defcustom org-keep-stored-link-after-insertion nil
  1459. "Non-nil means keep link in list for entire session.
  1460. The command `org-store-link' adds a link pointing to the current
  1461. location to an internal list. These links accumulate during a session.
  1462. The command `org-insert-link' can be used to insert links into any
  1463. Org-mode file (offering completion for all stored links). When this
  1464. option is nil, every link which has been inserted once using \\[org-insert-link]
  1465. will be removed from the list, to make completing the unused links
  1466. more efficient."
  1467. :group 'org-link-store
  1468. :type 'boolean)
  1469. (defgroup org-link-follow nil
  1470. "Options concerning following links in Org-mode."
  1471. :tag "Org Follow Link"
  1472. :group 'org-link)
  1473. (defcustom org-link-translation-function nil
  1474. "Function to translate links with different syntax to Org syntax.
  1475. This can be used to translate links created for example by the Planner
  1476. or emacs-wiki packages to Org syntax.
  1477. The function must accept two parameters, a TYPE containing the link
  1478. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1479. which is everything after the link protocol. It should return a cons
  1480. with possibly modified values of type and path.
  1481. Org contains a function for this, so if you set this variable to
  1482. `org-translate-link-from-planner', you should be able follow many
  1483. links created by planner."
  1484. :group 'org-link-follow
  1485. :type '(choice (const nil) (function)))
  1486. (defcustom org-follow-link-hook nil
  1487. "Hook that is run after a link has been followed."
  1488. :group 'org-link-follow
  1489. :type 'hook)
  1490. (defcustom org-tab-follows-link nil
  1491. "Non-nil means on links TAB will follow the link.
  1492. Needs to be set before org.el is loaded.
  1493. This really should not be used, it does not make sense, and the
  1494. implementation is bad."
  1495. :group 'org-link-follow
  1496. :type 'boolean)
  1497. (defcustom org-return-follows-link nil
  1498. "Non-nil means on links RET will follow the link.
  1499. In tables, the special behavior of RET has precedence."
  1500. :group 'org-link-follow
  1501. :type 'boolean)
  1502. (defcustom org-mouse-1-follows-link
  1503. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1504. "Non-nil means mouse-1 on a link will follow the link.
  1505. A longer mouse click will still set point. Does not work on XEmacs.
  1506. Needs to be set before org.el is loaded."
  1507. :group 'org-link-follow
  1508. :type 'boolean)
  1509. (defcustom org-mark-ring-length 4
  1510. "Number of different positions to be recorded in the ring.
  1511. Changing this requires a restart of Emacs to work correctly."
  1512. :group 'org-link-follow
  1513. :type 'integer)
  1514. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1515. "Non-nil means internal links in Org files must exactly match a headline.
  1516. When nil, the link search tries to match a phrase with all words
  1517. in the search text."
  1518. :group 'org-link-follow
  1519. :version "24.1"
  1520. :type '(choice
  1521. (const :tag "Use fuzzy text search" nil)
  1522. (const :tag "Match only exact headline" t)
  1523. (const :tag "Match exact headline or query to create it"
  1524. query-to-create)))
  1525. (defcustom org-link-frame-setup
  1526. '((vm . vm-visit-folder-other-frame)
  1527. (vm-imap . vm-visit-imap-folder-other-frame)
  1528. (gnus . org-gnus-no-new-news)
  1529. (file . find-file-other-window)
  1530. (wl . wl-other-frame))
  1531. "Setup the frame configuration for following links.
  1532. When following a link with Emacs, it may often be useful to display
  1533. this link in another window or frame. This variable can be used to
  1534. set this up for the different types of links.
  1535. For VM, use any of
  1536. `vm-visit-folder'
  1537. `vm-visit-folder-other-window'
  1538. `vm-visit-folder-other-frame'
  1539. For Gnus, use any of
  1540. `gnus'
  1541. `gnus-other-frame'
  1542. `org-gnus-no-new-news'
  1543. For FILE, use any of
  1544. `find-file'
  1545. `find-file-other-window'
  1546. `find-file-other-frame'
  1547. For Wanderlust use any of
  1548. `wl'
  1549. `wl-other-frame'
  1550. For the calendar, use the variable `calendar-setup'.
  1551. For BBDB, it is currently only possible to display the matches in
  1552. another window."
  1553. :group 'org-link-follow
  1554. :type '(list
  1555. (cons (const vm)
  1556. (choice
  1557. (const vm-visit-folder)
  1558. (const vm-visit-folder-other-window)
  1559. (const vm-visit-folder-other-frame)))
  1560. (cons (const vm-imap)
  1561. (choice
  1562. (const vm-visit-imap-folder)
  1563. (const vm-visit-imap-folder-other-window)
  1564. (const vm-visit-imap-folder-other-frame)))
  1565. (cons (const gnus)
  1566. (choice
  1567. (const gnus)
  1568. (const gnus-other-frame)
  1569. (const org-gnus-no-new-news)))
  1570. (cons (const file)
  1571. (choice
  1572. (const find-file)
  1573. (const find-file-other-window)
  1574. (const find-file-other-frame)))
  1575. (cons (const wl)
  1576. (choice
  1577. (const wl)
  1578. (const wl-other-frame)))))
  1579. (defcustom org-display-internal-link-with-indirect-buffer nil
  1580. "Non-nil means use indirect buffer to display infile links.
  1581. Activating internal links (from one location in a file to another location
  1582. in the same file) normally just jumps to the location. When the link is
  1583. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1584. is displayed in
  1585. another window. When this option is set, the other window actually displays
  1586. an indirect buffer clone of the current buffer, to avoid any visibility
  1587. changes to the current buffer."
  1588. :group 'org-link-follow
  1589. :type 'boolean)
  1590. (defcustom org-open-non-existing-files nil
  1591. "Non-nil means `org-open-file' will open non-existing files.
  1592. When nil, an error will be generated.
  1593. This variable applies only to external applications because they
  1594. might choke on non-existing files. If the link is to a file that
  1595. will be opened in Emacs, the variable is ignored."
  1596. :group 'org-link-follow
  1597. :type 'boolean)
  1598. (defcustom org-open-directory-means-index-dot-org nil
  1599. "Non-nil means a link to a directory really means to index.org.
  1600. When nil, following a directory link will run dired or open a finder/explorer
  1601. window on that directory."
  1602. :group 'org-link-follow
  1603. :type 'boolean)
  1604. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1605. "Function and arguments to call for following mailto links.
  1606. This is a list with the first element being a Lisp function, and the
  1607. remaining elements being arguments to the function. In string arguments,
  1608. %a will be replaced by the address, and %s will be replaced by the subject
  1609. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1610. :group 'org-link-follow
  1611. :type '(choice
  1612. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1613. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1614. (const :tag "message-mail" (message-mail "%a" "%s"))
  1615. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1616. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1617. "Non-nil means ask for confirmation before executing shell links.
  1618. Shell links can be dangerous: just think about a link
  1619. [[shell:rm -rf ~/*][Google Search]]
  1620. This link would show up in your Org-mode document as \"Google Search\",
  1621. but really it would remove your entire home directory.
  1622. Therefore we advise against setting this variable to nil.
  1623. Just change it to `y-or-n-p' if you want to confirm with a
  1624. single keystroke rather than having to type \"yes\"."
  1625. :group 'org-link-follow
  1626. :type '(choice
  1627. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1628. (const :tag "with y-or-n (faster)" y-or-n-p)
  1629. (const :tag "no confirmation (dangerous)" nil)))
  1630. (put 'org-confirm-shell-link-function
  1631. 'safe-local-variable
  1632. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1633. (defcustom org-confirm-shell-link-not-regexp ""
  1634. "A regexp to skip confirmation for shell links."
  1635. :group 'org-link-follow
  1636. :version "24.1"
  1637. :type 'regexp)
  1638. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1639. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1640. Elisp links can be dangerous: just think about a link
  1641. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1642. This link would show up in your Org-mode document as \"Google Search\",
  1643. but really it would remove your entire home directory.
  1644. Therefore we advise against setting this variable to nil.
  1645. Just change it to `y-or-n-p' if you want to confirm with a
  1646. single keystroke rather than having to type \"yes\"."
  1647. :group 'org-link-follow
  1648. :type '(choice
  1649. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1650. (const :tag "with y-or-n (faster)" y-or-n-p)
  1651. (const :tag "no confirmation (dangerous)" nil)))
  1652. (put 'org-confirm-shell-link-function
  1653. 'safe-local-variable
  1654. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1655. (defcustom org-confirm-elisp-link-not-regexp ""
  1656. "A regexp to skip confirmation for Elisp links."
  1657. :group 'org-link-follow
  1658. :version "24.1"
  1659. :type 'regexp)
  1660. (defconst org-file-apps-defaults-gnu
  1661. '((remote . emacs)
  1662. (system . mailcap)
  1663. (t . mailcap))
  1664. "Default file applications on a UNIX or GNU/Linux system.
  1665. See `org-file-apps'.")
  1666. (defconst org-file-apps-defaults-macosx
  1667. '((remote . emacs)
  1668. (t . "open %s")
  1669. (system . "open %s")
  1670. ("ps.gz" . "gv %s")
  1671. ("eps.gz" . "gv %s")
  1672. ("dvi" . "xdvi %s")
  1673. ("fig" . "xfig %s"))
  1674. "Default file applications on a MacOS X system.
  1675. The system \"open\" is known as a default, but we use X11 applications
  1676. for some files for which the OS does not have a good default.
  1677. See `org-file-apps'.")
  1678. (defconst org-file-apps-defaults-windowsnt
  1679. (list
  1680. '(remote . emacs)
  1681. (cons t
  1682. (list (if (featurep 'xemacs)
  1683. 'mswindows-shell-execute
  1684. 'w32-shell-execute)
  1685. "open" 'file))
  1686. (cons 'system
  1687. (list (if (featurep 'xemacs)
  1688. 'mswindows-shell-execute
  1689. 'w32-shell-execute)
  1690. "open" 'file)))
  1691. "Default file applications on a Windows NT system.
  1692. The system \"open\" is used for most files.
  1693. See `org-file-apps'.")
  1694. (defcustom org-file-apps
  1695. '((auto-mode . emacs)
  1696. ("\\.mm\\'" . default)
  1697. ("\\.x?html?\\'" . default)
  1698. ("\\.pdf\\'" . default))
  1699. "External applications for opening `file:path' items in a document.
  1700. Org-mode uses system defaults for different file types, but
  1701. you can use this variable to set the application for a given file
  1702. extension. The entries in this list are cons cells where the car identifies
  1703. files and the cdr the corresponding command. Possible values for the
  1704. file identifier are
  1705. \"string\" A string as a file identifier can be interpreted in different
  1706. ways, depending on its contents:
  1707. - Alphanumeric characters only:
  1708. Match links with this file extension.
  1709. Example: (\"pdf\" . \"evince %s\")
  1710. to open PDFs with evince.
  1711. - Regular expression: Match links where the
  1712. filename matches the regexp. If you want to
  1713. use groups here, use shy groups.
  1714. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1715. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1716. to open *.html and *.xhtml with firefox.
  1717. - Regular expression which contains (non-shy) groups:
  1718. Match links where the whole link, including \"::\", and
  1719. anything after that, matches the regexp.
  1720. In a custom command string, %1, %2, etc. are replaced with
  1721. the parts of the link that were matched by the groups.
  1722. For backwards compatibility, if a command string is given
  1723. that does not use any of the group matches, this case is
  1724. handled identically to the second one (i.e. match against
  1725. file name only).
  1726. In a custom lisp form, you can access the group matches with
  1727. (match-string n link).
  1728. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1729. to open [[file:document.pdf::5]] with evince at page 5.
  1730. `directory' Matches a directory
  1731. `remote' Matches a remote file, accessible through tramp or efs.
  1732. Remote files most likely should be visited through Emacs
  1733. because external applications cannot handle such paths.
  1734. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1735. so all files Emacs knows how to handle. Using this with
  1736. command `emacs' will open most files in Emacs. Beware that this
  1737. will also open html files inside Emacs, unless you add
  1738. (\"html\" . default) to the list as well.
  1739. t Default for files not matched by any of the other options.
  1740. `system' The system command to open files, like `open' on Windows
  1741. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1742. that will be selected if you call `C-c C-o' with a double
  1743. \\[universal-argument] \\[universal-argument] prefix.
  1744. Possible values for the command are:
  1745. `emacs' The file will be visited by the current Emacs process.
  1746. `default' Use the default application for this file type, which is the
  1747. association for t in the list, most likely in the system-specific
  1748. part.
  1749. This can be used to overrule an unwanted setting in the
  1750. system-specific variable.
  1751. `system' Use the system command for opening files, like \"open\".
  1752. This command is specified by the entry whose car is `system'.
  1753. Most likely, the system-specific version of this variable
  1754. does define this command, but you can overrule/replace it
  1755. here.
  1756. string A command to be executed by a shell; %s will be replaced
  1757. by the path to the file.
  1758. sexp A Lisp form which will be evaluated. The file path will
  1759. be available in the Lisp variable `file'.
  1760. For more examples, see the system specific constants
  1761. `org-file-apps-defaults-macosx'
  1762. `org-file-apps-defaults-windowsnt'
  1763. `org-file-apps-defaults-gnu'."
  1764. :group 'org-link-follow
  1765. :type '(repeat
  1766. (cons (choice :value ""
  1767. (string :tag "Extension")
  1768. (const :tag "System command to open files" system)
  1769. (const :tag "Default for unrecognized files" t)
  1770. (const :tag "Remote file" remote)
  1771. (const :tag "Links to a directory" directory)
  1772. (const :tag "Any files that have Emacs modes"
  1773. auto-mode))
  1774. (choice :value ""
  1775. (const :tag "Visit with Emacs" emacs)
  1776. (const :tag "Use default" default)
  1777. (const :tag "Use the system command" system)
  1778. (string :tag "Command")
  1779. (sexp :tag "Lisp form")))))
  1780. (defcustom org-doi-server-url "http://dx.doi.org/"
  1781. "The URL of the DOI server."
  1782. :type 'string
  1783. :version "24.3"
  1784. :group 'org-link-follow)
  1785. (defgroup org-refile nil
  1786. "Options concerning refiling entries in Org-mode."
  1787. :tag "Org Refile"
  1788. :group 'org)
  1789. (defcustom org-directory "~/org"
  1790. "Directory with org files.
  1791. This is just a default location to look for Org files. There is no need
  1792. at all to put your files into this directory. It is only used in the
  1793. following situations:
  1794. 1. When a capture template specifies a target file that is not an
  1795. absolute path. The path will then be interpreted relative to
  1796. `org-directory'
  1797. 2. When a capture note is filed away in an interactive way (when exiting the
  1798. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1799. with `org-directory' as the default path."
  1800. :group 'org-refile
  1801. :group 'org-remember
  1802. :group 'org-capture
  1803. :type 'directory)
  1804. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1805. "Default target for storing notes.
  1806. Used as a fall back file for org-remember.el and org-capture.el, for
  1807. templates that do not specify a target file."
  1808. :group 'org-refile
  1809. :group 'org-remember
  1810. :group 'org-capture
  1811. :type '(choice
  1812. (const :tag "Default from remember-data-file" nil)
  1813. file))
  1814. (defcustom org-goto-interface 'outline
  1815. "The default interface to be used for `org-goto'.
  1816. Allowed values are:
  1817. outline The interface shows an outline of the relevant file
  1818. and the correct heading is found by moving through
  1819. the outline or by searching with incremental search.
  1820. outline-path-completion Headlines in the current buffer are offered via
  1821. completion. This is the interface also used by
  1822. the refile command."
  1823. :group 'org-refile
  1824. :type '(choice
  1825. (const :tag "Outline" outline)
  1826. (const :tag "Outline-path-completion" outline-path-completion)))
  1827. (defcustom org-goto-max-level 5
  1828. "Maximum target level when running `org-goto' with refile interface."
  1829. :group 'org-refile
  1830. :type 'integer)
  1831. (defcustom org-reverse-note-order nil
  1832. "Non-nil means store new notes at the beginning of a file or entry.
  1833. When nil, new notes will be filed to the end of a file or entry.
  1834. This can also be a list with cons cells of regular expressions that
  1835. are matched against file names, and values."
  1836. :group 'org-remember
  1837. :group 'org-capture
  1838. :group 'org-refile
  1839. :type '(choice
  1840. (const :tag "Reverse always" t)
  1841. (const :tag "Reverse never" nil)
  1842. (repeat :tag "By file name regexp"
  1843. (cons regexp boolean))))
  1844. (defcustom org-log-refile nil
  1845. "Information to record when a task is refiled.
  1846. Possible values are:
  1847. nil Don't add anything
  1848. time Add a time stamp to the task
  1849. note Prompt for a note and add it with template `org-log-note-headings'
  1850. This option can also be set with on a per-file-basis with
  1851. #+STARTUP: nologrefile
  1852. #+STARTUP: logrefile
  1853. #+STARTUP: lognoterefile
  1854. You can have local logging settings for a subtree by setting the LOGGING
  1855. property to one or more of these keywords.
  1856. When bulk-refiling from the agenda, the value `note' is forbidden and
  1857. will temporarily be changed to `time'."
  1858. :group 'org-refile
  1859. :group 'org-progress
  1860. :version "24.1"
  1861. :type '(choice
  1862. (const :tag "No logging" nil)
  1863. (const :tag "Record timestamp" time)
  1864. (const :tag "Record timestamp with note." note)))
  1865. (defcustom org-refile-targets nil
  1866. "Targets for refiling entries with \\[org-refile].
  1867. This is a list of cons cells. Each cell contains:
  1868. - a specification of the files to be considered, either a list of files,
  1869. or a symbol whose function or variable value will be used to retrieve
  1870. a file name or a list of file names. If you use `org-agenda-files' for
  1871. that, all agenda files will be scanned for targets. Nil means consider
  1872. headings in the current buffer.
  1873. - A specification of how to find candidate refile targets. This may be
  1874. any of:
  1875. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1876. This tag has to be present in all target headlines, inheritance will
  1877. not be considered.
  1878. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1879. todo keyword.
  1880. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1881. headlines that are refiling targets.
  1882. - a cons cell (:level . N). Any headline of level N is considered a target.
  1883. Note that, when `org-odd-levels-only' is set, level corresponds to
  1884. order in hierarchy, not to the number of stars.
  1885. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1886. Note that, when `org-odd-levels-only' is set, level corresponds to
  1887. order in hierarchy, not to the number of stars.
  1888. Each element of this list generates a set of possible targets.
  1889. The union of these sets is presented (with completion) to
  1890. the user by `org-refile'.
  1891. You can set the variable `org-refile-target-verify-function' to a function
  1892. to verify each headline found by the simple criteria above.
  1893. When this variable is nil, all top-level headlines in the current buffer
  1894. are used, equivalent to the value `((nil . (:level . 1))'."
  1895. :group 'org-refile
  1896. :type '(repeat
  1897. (cons
  1898. (choice :value org-agenda-files
  1899. (const :tag "All agenda files" org-agenda-files)
  1900. (const :tag "Current buffer" nil)
  1901. (function) (variable) (file))
  1902. (choice :tag "Identify target headline by"
  1903. (cons :tag "Specific tag" (const :value :tag) (string))
  1904. (cons :tag "TODO keyword" (const :value :todo) (string))
  1905. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1906. (cons :tag "Level number" (const :value :level) (integer))
  1907. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1908. (defcustom org-refile-target-verify-function nil
  1909. "Function to verify if the headline at point should be a refile target.
  1910. The function will be called without arguments, with point at the
  1911. beginning of the headline. It should return t and leave point
  1912. where it is if the headline is a valid target for refiling.
  1913. If the target should not be selected, the function must return nil.
  1914. In addition to this, it may move point to a place from where the search
  1915. should be continued. For example, the function may decide that the entire
  1916. subtree of the current entry should be excluded and move point to the end
  1917. of the subtree."
  1918. :group 'org-refile
  1919. :type '(choice
  1920. (const nil)
  1921. (function)))
  1922. (defcustom org-refile-use-cache nil
  1923. "Non-nil means cache refile targets to speed up the process.
  1924. The cache for a particular file will be updated automatically when
  1925. the buffer has been killed, or when any of the marker used for flagging
  1926. refile targets no longer points at a live buffer.
  1927. If you have added new entries to a buffer that might themselves be targets,
  1928. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1929. find that easier, `C-u C-u C-u C-c C-w'."
  1930. :group 'org-refile
  1931. :version "24.1"
  1932. :type 'boolean)
  1933. (defcustom org-refile-use-outline-path nil
  1934. "Non-nil means provide refile targets as paths.
  1935. So a level 3 headline will be available as level1/level2/level3.
  1936. When the value is `file', also include the file name (without directory)
  1937. into the path. In this case, you can also stop the completion after
  1938. the file name, to get entries inserted as top level in the file.
  1939. When `full-file-path', include the full file path."
  1940. :group 'org-refile
  1941. :type '(choice
  1942. (const :tag "Not" nil)
  1943. (const :tag "Yes" t)
  1944. (const :tag "Start with file name" file)
  1945. (const :tag "Start with full file path" full-file-path)))
  1946. (defcustom org-outline-path-complete-in-steps t
  1947. "Non-nil means complete the outline path in hierarchical steps.
  1948. When Org-mode uses the refile interface to select an outline path
  1949. \(see variable `org-refile-use-outline-path'), the completion of
  1950. the path can be done is a single go, or if can be done in steps down
  1951. the headline hierarchy. Going in steps is probably the best if you
  1952. do not use a special completion package like `ido' or `icicles'.
  1953. However, when using these packages, going in one step can be very
  1954. fast, while still showing the whole path to the entry."
  1955. :group 'org-refile
  1956. :type 'boolean)
  1957. (defcustom org-refile-allow-creating-parent-nodes nil
  1958. "Non-nil means allow to create new nodes as refile targets.
  1959. New nodes are then created by adding \"/new node name\" to the completion
  1960. of an existing node. When the value of this variable is `confirm',
  1961. new node creation must be confirmed by the user (recommended)
  1962. When nil, the completion must match an existing entry.
  1963. Note that, if the new heading is not seen by the criteria
  1964. listed in `org-refile-targets', multiple instances of the same
  1965. heading would be created by trying again to file under the new
  1966. heading."
  1967. :group 'org-refile
  1968. :type '(choice
  1969. (const :tag "Never" nil)
  1970. (const :tag "Always" t)
  1971. (const :tag "Prompt for confirmation" confirm)))
  1972. (defcustom org-refile-active-region-within-subtree nil
  1973. "Non-nil means also refile active region within a subtree.
  1974. By default `org-refile' doesn't allow refiling regions if they
  1975. don't contain a set of subtrees, but it might be convenient to
  1976. do so sometimes: in that case, the first line of the region is
  1977. converted to a headline before refiling."
  1978. :group 'org-refile
  1979. :version "24.1"
  1980. :type 'boolean)
  1981. (defgroup org-todo nil
  1982. "Options concerning TODO items in Org-mode."
  1983. :tag "Org TODO"
  1984. :group 'org)
  1985. (defgroup org-progress nil
  1986. "Options concerning Progress logging in Org-mode."
  1987. :tag "Org Progress"
  1988. :group 'org-time)
  1989. (defvar org-todo-interpretation-widgets
  1990. '((:tag "Sequence (cycling hits every state)" sequence)
  1991. (:tag "Type (cycling directly to DONE)" type))
  1992. "The available interpretation symbols for customizing `org-todo-keywords'.
  1993. Interested libraries should add to this list.")
  1994. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1995. "List of TODO entry keyword sequences and their interpretation.
  1996. \\<org-mode-map>This is a list of sequences.
  1997. Each sequence starts with a symbol, either `sequence' or `type',
  1998. indicating if the keywords should be interpreted as a sequence of
  1999. action steps, or as different types of TODO items. The first
  2000. keywords are states requiring action - these states will select a headline
  2001. for inclusion into the global TODO list Org-mode produces. If one of
  2002. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2003. signify that no further action is necessary. If \"|\" is not found,
  2004. the last keyword is treated as the only DONE state of the sequence.
  2005. The command \\[org-todo] cycles an entry through these states, and one
  2006. additional state where no keyword is present. For details about this
  2007. cycling, see the manual.
  2008. TODO keywords and interpretation can also be set on a per-file basis with
  2009. the special #+SEQ_TODO and #+TYP_TODO lines.
  2010. Each keyword can optionally specify a character for fast state selection
  2011. \(in combination with the variable `org-use-fast-todo-selection')
  2012. and specifiers for state change logging, using the same syntax that
  2013. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2014. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2015. indicates to record a time stamp each time this state is selected.
  2016. Each keyword may also specify if a timestamp or a note should be
  2017. recorded when entering or leaving the state, by adding additional
  2018. characters in the parenthesis after the keyword. This looks like this:
  2019. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2020. record only the time of the state change. With X and Y being either
  2021. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2022. Y when leaving the state if and only if the *target* state does not
  2023. define X. You may omit any of the fast-selection key or X or /Y,
  2024. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2025. For backward compatibility, this variable may also be just a list
  2026. of keywords. In this case the interpretation (sequence or type) will be
  2027. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2028. :group 'org-todo
  2029. :group 'org-keywords
  2030. :type '(choice
  2031. (repeat :tag "Old syntax, just keywords"
  2032. (string :tag "Keyword"))
  2033. (repeat :tag "New syntax"
  2034. (cons
  2035. (choice
  2036. :tag "Interpretation"
  2037. ;;Quick and dirty way to see
  2038. ;;`org-todo-interpretations'. This takes the
  2039. ;;place of item arguments
  2040. :convert-widget
  2041. (lambda (widget)
  2042. (widget-put widget
  2043. :args (mapcar
  2044. #'(lambda (x)
  2045. (widget-convert
  2046. (cons 'const x)))
  2047. org-todo-interpretation-widgets))
  2048. widget))
  2049. (repeat
  2050. (string :tag "Keyword"))))))
  2051. (defvar org-todo-keywords-1 nil
  2052. "All TODO and DONE keywords active in a buffer.")
  2053. (make-variable-buffer-local 'org-todo-keywords-1)
  2054. (defvar org-todo-keywords-for-agenda nil)
  2055. (defvar org-done-keywords-for-agenda nil)
  2056. (defvar org-drawers-for-agenda nil)
  2057. (defvar org-todo-keyword-alist-for-agenda nil)
  2058. (defvar org-tag-alist-for-agenda nil
  2059. "Alist of all tags from all agenda files.")
  2060. (defvar org-tag-groups-alist-for-agenda nil
  2061. "Alist of all groups tags from all current agenda files.")
  2062. (defvar org-tag-groups-alist nil)
  2063. (make-variable-buffer-local 'org-tag-groups-alist)
  2064. (defvar org-agenda-contributing-files nil)
  2065. (defvar org-not-done-keywords nil)
  2066. (make-variable-buffer-local 'org-not-done-keywords)
  2067. (defvar org-done-keywords nil)
  2068. (make-variable-buffer-local 'org-done-keywords)
  2069. (defvar org-todo-heads nil)
  2070. (make-variable-buffer-local 'org-todo-heads)
  2071. (defvar org-todo-sets nil)
  2072. (make-variable-buffer-local 'org-todo-sets)
  2073. (defvar org-todo-log-states nil)
  2074. (make-variable-buffer-local 'org-todo-log-states)
  2075. (defvar org-todo-kwd-alist nil)
  2076. (make-variable-buffer-local 'org-todo-kwd-alist)
  2077. (defvar org-todo-key-alist nil)
  2078. (make-variable-buffer-local 'org-todo-key-alist)
  2079. (defvar org-todo-key-trigger nil)
  2080. (make-variable-buffer-local 'org-todo-key-trigger)
  2081. (defcustom org-todo-interpretation 'sequence
  2082. "Controls how TODO keywords are interpreted.
  2083. This variable is in principle obsolete and is only used for
  2084. backward compatibility, if the interpretation of todo keywords is
  2085. not given already in `org-todo-keywords'. See that variable for
  2086. more information."
  2087. :group 'org-todo
  2088. :group 'org-keywords
  2089. :type '(choice (const sequence)
  2090. (const type)))
  2091. (defcustom org-use-fast-todo-selection t
  2092. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2093. This variable describes if and under what circumstances the cycling
  2094. mechanism for TODO keywords will be replaced by a single-key, direct
  2095. selection scheme.
  2096. When nil, fast selection is never used.
  2097. When the symbol `prefix', it will be used when `org-todo' is called
  2098. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2099. `C-u t' in an agenda buffer.
  2100. When t, fast selection is used by default. In this case, the prefix
  2101. argument forces cycling instead.
  2102. In all cases, the special interface is only used if access keys have
  2103. actually been assigned by the user, i.e. if keywords in the configuration
  2104. are followed by a letter in parenthesis, like TODO(t)."
  2105. :group 'org-todo
  2106. :type '(choice
  2107. (const :tag "Never" nil)
  2108. (const :tag "By default" t)
  2109. (const :tag "Only with C-u C-c C-t" prefix)))
  2110. (defcustom org-provide-todo-statistics t
  2111. "Non-nil means update todo statistics after insert and toggle.
  2112. ALL-HEADLINES means update todo statistics by including headlines
  2113. with no TODO keyword as well, counting them as not done.
  2114. A list of TODO keywords means the same, but skip keywords that are
  2115. not in this list.
  2116. When this is set, todo statistics is updated in the parent of the
  2117. current entry each time a todo state is changed."
  2118. :group 'org-todo
  2119. :type '(choice
  2120. (const :tag "Yes, only for TODO entries" t)
  2121. (const :tag "Yes, including all entries" 'all-headlines)
  2122. (repeat :tag "Yes, for TODOs in this list"
  2123. (string :tag "TODO keyword"))
  2124. (other :tag "No TODO statistics" nil)))
  2125. (defcustom org-hierarchical-todo-statistics t
  2126. "Non-nil means TODO statistics covers just direct children.
  2127. When nil, all entries in the subtree are considered.
  2128. This has only an effect if `org-provide-todo-statistics' is set.
  2129. To set this to nil for only a single subtree, use a COOKIE_DATA
  2130. property and include the word \"recursive\" into the value."
  2131. :group 'org-todo
  2132. :type 'boolean)
  2133. (defcustom org-after-todo-state-change-hook nil
  2134. "Hook which is run after the state of a TODO item was changed.
  2135. The new state (a string with a TODO keyword, or nil) is available in the
  2136. Lisp variable `org-state'."
  2137. :group 'org-todo
  2138. :type 'hook)
  2139. (defvar org-blocker-hook nil
  2140. "Hook for functions that are allowed to block a state change.
  2141. Functions in this hook should not modify the buffer.
  2142. Each function gets as its single argument a property list,
  2143. see `org-trigger-hook' for more information about this list.
  2144. If any of the functions in this hook returns nil, the state change
  2145. is blocked.")
  2146. (defvar org-trigger-hook nil
  2147. "Hook for functions that are triggered by a state change.
  2148. Each function gets as its single argument a property list with at
  2149. least the following elements:
  2150. (:type type-of-change :position pos-at-entry-start
  2151. :from old-state :to new-state)
  2152. Depending on the type, more properties may be present.
  2153. This mechanism is currently implemented for:
  2154. TODO state changes
  2155. ------------------
  2156. :type todo-state-change
  2157. :from previous state (keyword as a string), or nil, or a symbol
  2158. 'todo' or 'done', to indicate the general type of state.
  2159. :to new state, like in :from")
  2160. (defcustom org-enforce-todo-dependencies nil
  2161. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2162. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2163. be blocked if any prior sibling is not yet done.
  2164. Finally, if the parent is blocked because of ordered siblings of its own,
  2165. the child will also be blocked."
  2166. :set (lambda (var val)
  2167. (set var val)
  2168. (if val
  2169. (add-hook 'org-blocker-hook
  2170. 'org-block-todo-from-children-or-siblings-or-parent)
  2171. (remove-hook 'org-blocker-hook
  2172. 'org-block-todo-from-children-or-siblings-or-parent)))
  2173. :group 'org-todo
  2174. :type 'boolean)
  2175. (defcustom org-enforce-todo-checkbox-dependencies nil
  2176. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2177. When this is nil, checkboxes have no influence on switching TODO states.
  2178. When non-nil, you first need to check off all check boxes before the TODO
  2179. entry can be switched to DONE.
  2180. This variable needs to be set before org.el is loaded, and you need to
  2181. restart Emacs after a change to make the change effective. The only way
  2182. to change is while Emacs is running is through the customize interface."
  2183. :set (lambda (var val)
  2184. (set var val)
  2185. (if val
  2186. (add-hook 'org-blocker-hook
  2187. 'org-block-todo-from-checkboxes)
  2188. (remove-hook 'org-blocker-hook
  2189. 'org-block-todo-from-checkboxes)))
  2190. :group 'org-todo
  2191. :type 'boolean)
  2192. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2193. "Non-nil means inserting a TODO heading is treated as state change.
  2194. So when the command \\[org-insert-todo-heading] is used, state change
  2195. logging will apply if appropriate. When nil, the new TODO item will
  2196. be inserted directly, and no logging will take place."
  2197. :group 'org-todo
  2198. :type 'boolean)
  2199. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2200. "Non-nil means switching TODO states with S-cursor counts as state change.
  2201. This is the default behavior. However, setting this to nil allows a
  2202. convenient way to select a TODO state and bypass any logging associated
  2203. with that."
  2204. :group 'org-todo
  2205. :type 'boolean)
  2206. (defcustom org-todo-state-tags-triggers nil
  2207. "Tag changes that should be triggered by TODO state changes.
  2208. This is a list. Each entry is
  2209. (state-change (tag . flag) .......)
  2210. State-change can be a string with a state, and empty string to indicate the
  2211. state that has no TODO keyword, or it can be one of the symbols `todo'
  2212. or `done', meaning any not-done or done state, respectively."
  2213. :group 'org-todo
  2214. :group 'org-tags
  2215. :type '(repeat
  2216. (cons (choice :tag "When changing to"
  2217. (const :tag "Not-done state" todo)
  2218. (const :tag "Done state" done)
  2219. (string :tag "State"))
  2220. (repeat
  2221. (cons :tag "Tag action"
  2222. (string :tag "Tag")
  2223. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2224. (defcustom org-log-done nil
  2225. "Information to record when a task moves to the DONE state.
  2226. Possible values are:
  2227. nil Don't add anything, just change the keyword
  2228. time Add a time stamp to the task
  2229. note Prompt for a note and add it with template `org-log-note-headings'
  2230. This option can also be set with on a per-file-basis with
  2231. #+STARTUP: nologdone
  2232. #+STARTUP: logdone
  2233. #+STARTUP: lognotedone
  2234. You can have local logging settings for a subtree by setting the LOGGING
  2235. property to one or more of these keywords."
  2236. :group 'org-todo
  2237. :group 'org-progress
  2238. :type '(choice
  2239. (const :tag "No logging" nil)
  2240. (const :tag "Record CLOSED timestamp" time)
  2241. (const :tag "Record CLOSED timestamp with note." note)))
  2242. ;; Normalize old uses of org-log-done.
  2243. (cond
  2244. ((eq org-log-done t) (setq org-log-done 'time))
  2245. ((and (listp org-log-done) (memq 'done org-log-done))
  2246. (setq org-log-done 'note)))
  2247. (defcustom org-log-reschedule nil
  2248. "Information to record when the scheduling date of a tasks is modified.
  2249. Possible values are:
  2250. nil Don't add anything, just change the date
  2251. time Add a time stamp to the task
  2252. note Prompt for a note and add it with template `org-log-note-headings'
  2253. This option can also be set with on a per-file-basis with
  2254. #+STARTUP: nologreschedule
  2255. #+STARTUP: logreschedule
  2256. #+STARTUP: lognotereschedule"
  2257. :group 'org-todo
  2258. :group 'org-progress
  2259. :type '(choice
  2260. (const :tag "No logging" nil)
  2261. (const :tag "Record timestamp" time)
  2262. (const :tag "Record timestamp with note." note)))
  2263. (defcustom org-log-redeadline nil
  2264. "Information to record when the deadline date of a tasks is modified.
  2265. Possible values are:
  2266. nil Don't add anything, just change the date
  2267. time Add a time stamp to the task
  2268. note Prompt for a note and add it with template `org-log-note-headings'
  2269. This option can also be set with on a per-file-basis with
  2270. #+STARTUP: nologredeadline
  2271. #+STARTUP: logredeadline
  2272. #+STARTUP: lognoteredeadline
  2273. You can have local logging settings for a subtree by setting the LOGGING
  2274. property to one or more of these keywords."
  2275. :group 'org-todo
  2276. :group 'org-progress
  2277. :type '(choice
  2278. (const :tag "No logging" nil)
  2279. (const :tag "Record timestamp" time)
  2280. (const :tag "Record timestamp with note." note)))
  2281. (defcustom org-log-note-clock-out nil
  2282. "Non-nil means record a note when clocking out of an item.
  2283. This can also be configured on a per-file basis by adding one of
  2284. the following lines anywhere in the buffer:
  2285. #+STARTUP: lognoteclock-out
  2286. #+STARTUP: nolognoteclock-out"
  2287. :group 'org-todo
  2288. :group 'org-progress
  2289. :type 'boolean)
  2290. (defcustom org-log-done-with-time t
  2291. "Non-nil means the CLOSED time stamp will contain date and time.
  2292. When nil, only the date will be recorded."
  2293. :group 'org-progress
  2294. :type 'boolean)
  2295. (defcustom org-log-note-headings
  2296. '((done . "CLOSING NOTE %t")
  2297. (state . "State %-12s from %-12S %t")
  2298. (note . "Note taken on %t")
  2299. (reschedule . "Rescheduled from %S on %t")
  2300. (delschedule . "Not scheduled, was %S on %t")
  2301. (redeadline . "New deadline from %S on %t")
  2302. (deldeadline . "Removed deadline, was %S on %t")
  2303. (refile . "Refiled on %t")
  2304. (clock-out . ""))
  2305. "Headings for notes added to entries.
  2306. The value is an alist, with the car being a symbol indicating the note
  2307. context, and the cdr is the heading to be used. The heading may also be the
  2308. empty string.
  2309. %t in the heading will be replaced by a time stamp.
  2310. %T will be an active time stamp instead the default inactive one
  2311. %d will be replaced by a short-format time stamp.
  2312. %D will be replaced by an active short-format time stamp.
  2313. %s will be replaced by the new TODO state, in double quotes.
  2314. %S will be replaced by the old TODO state, in double quotes.
  2315. %u will be replaced by the user name.
  2316. %U will be replaced by the full user name.
  2317. In fact, it is not a good idea to change the `state' entry, because
  2318. agenda log mode depends on the format of these entries."
  2319. :group 'org-todo
  2320. :group 'org-progress
  2321. :type '(list :greedy t
  2322. (cons (const :tag "Heading when closing an item" done) string)
  2323. (cons (const :tag
  2324. "Heading when changing todo state (todo sequence only)"
  2325. state) string)
  2326. (cons (const :tag "Heading when just taking a note" note) string)
  2327. (cons (const :tag "Heading when clocking out" clock-out) string)
  2328. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2329. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2330. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2331. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2332. (cons (const :tag "Heading when refiling" refile) string)))
  2333. (unless (assq 'note org-log-note-headings)
  2334. (push '(note . "%t") org-log-note-headings))
  2335. (defcustom org-log-into-drawer nil
  2336. "Non-nil means insert state change notes and time stamps into a drawer.
  2337. When nil, state changes notes will be inserted after the headline and
  2338. any scheduling and clock lines, but not inside a drawer.
  2339. The value of this variable should be the name of the drawer to use.
  2340. LOGBOOK is proposed as the default drawer for this purpose, you can
  2341. also set this to a string to define the drawer of your choice.
  2342. A value of t is also allowed, representing \"LOGBOOK\".
  2343. A value of t or nil can also be set with on a per-file-basis with
  2344. #+STARTUP: logdrawer
  2345. #+STARTUP: nologdrawer
  2346. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2347. will be ignored.
  2348. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2349. a subtree."
  2350. :group 'org-todo
  2351. :group 'org-progress
  2352. :type '(choice
  2353. (const :tag "Not into a drawer" nil)
  2354. (const :tag "LOGBOOK" t)
  2355. (string :tag "Other")))
  2356. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2357. (defun org-log-into-drawer ()
  2358. "Return the value of `org-log-into-drawer', but let properties overrule.
  2359. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2360. used instead of the default value."
  2361. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2362. (cond
  2363. ((not p) org-log-into-drawer)
  2364. ((equal p "nil") nil)
  2365. ((equal p "t") "LOGBOOK")
  2366. (t p))))
  2367. (defcustom org-log-state-notes-insert-after-drawers nil
  2368. "Non-nil means insert state change notes after any drawers in entry.
  2369. Only the drawers that *immediately* follow the headline and the
  2370. deadline/scheduled line are skipped.
  2371. When nil, insert notes right after the heading and perhaps the line
  2372. with deadline/scheduling if present.
  2373. This variable will have no effect if `org-log-into-drawer' is
  2374. set."
  2375. :group 'org-todo
  2376. :group 'org-progress
  2377. :type 'boolean)
  2378. (defcustom org-log-states-order-reversed t
  2379. "Non-nil means the latest state note will be directly after heading.
  2380. When nil, the state change notes will be ordered according to time.
  2381. This option can also be set with on a per-file-basis with
  2382. #+STARTUP: logstatesreversed
  2383. #+STARTUP: nologstatesreversed"
  2384. :group 'org-todo
  2385. :group 'org-progress
  2386. :type 'boolean)
  2387. (defcustom org-todo-repeat-to-state nil
  2388. "The TODO state to which a repeater should return the repeating task.
  2389. By default this is the first task in a TODO sequence, or the previous state
  2390. in a TODO_TYP set. But you can specify another task here.
  2391. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2392. :group 'org-todo
  2393. :version "24.1"
  2394. :type '(choice (const :tag "Head of sequence" nil)
  2395. (string :tag "Specific state")))
  2396. (defcustom org-log-repeat 'time
  2397. "Non-nil means record moving through the DONE state when triggering repeat.
  2398. An auto-repeating task is immediately switched back to TODO when
  2399. marked DONE. If you are not logging state changes (by adding \"@\"
  2400. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2401. record a closing note, there will be no record of the task moving
  2402. through DONE. This variable forces taking a note anyway.
  2403. nil Don't force a record
  2404. time Record a time stamp
  2405. note Prompt for a note and add it with template `org-log-note-headings'
  2406. This option can also be set with on a per-file-basis with
  2407. #+STARTUP: nologrepeat
  2408. #+STARTUP: logrepeat
  2409. #+STARTUP: lognoterepeat
  2410. You can have local logging settings for a subtree by setting the LOGGING
  2411. property to one or more of these keywords."
  2412. :group 'org-todo
  2413. :group 'org-progress
  2414. :type '(choice
  2415. (const :tag "Don't force a record" nil)
  2416. (const :tag "Force recording the DONE state" time)
  2417. (const :tag "Force recording a note with the DONE state" note)))
  2418. (defgroup org-priorities nil
  2419. "Priorities in Org-mode."
  2420. :tag "Org Priorities"
  2421. :group 'org-todo)
  2422. (defcustom org-enable-priority-commands t
  2423. "Non-nil means priority commands are active.
  2424. When nil, these commands will be disabled, so that you never accidentally
  2425. set a priority."
  2426. :group 'org-priorities
  2427. :type 'boolean)
  2428. (defcustom org-highest-priority ?A
  2429. "The highest priority of TODO items. A character like ?A, ?B etc.
  2430. Must have a smaller ASCII number than `org-lowest-priority'."
  2431. :group 'org-priorities
  2432. :type 'character)
  2433. (defcustom org-lowest-priority ?C
  2434. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2435. Must have a larger ASCII number than `org-highest-priority'."
  2436. :group 'org-priorities
  2437. :type 'character)
  2438. (defcustom org-default-priority ?B
  2439. "The default priority of TODO items.
  2440. This is the priority an item gets if no explicit priority is given.
  2441. When starting to cycle on an empty priority the first step in the cycle
  2442. depends on `org-priority-start-cycle-with-default'. The resulting first
  2443. step priority must not exceed the range from `org-highest-priority' to
  2444. `org-lowest-priority' which means that `org-default-priority' has to be
  2445. in this range exclusive or inclusive the range boundaries. Else the
  2446. first step refuses to set the default and the second will fall back
  2447. to (depending on the command used) the highest or lowest priority."
  2448. :group 'org-priorities
  2449. :type 'character)
  2450. (defcustom org-priority-start-cycle-with-default t
  2451. "Non-nil means start with default priority when starting to cycle.
  2452. When this is nil, the first step in the cycle will be (depending on the
  2453. command used) one higher or lower than the default priority.
  2454. See also `org-default-priority'."
  2455. :group 'org-priorities
  2456. :type 'boolean)
  2457. (defcustom org-get-priority-function nil
  2458. "Function to extract the priority from a string.
  2459. The string is normally the headline. If this is nil Org computes the
  2460. priority from the priority cookie like [#A] in the headline. It returns
  2461. an integer, increasing by 1000 for each priority level.
  2462. The user can set a different function here, which should take a string
  2463. as an argument and return the numeric priority."
  2464. :group 'org-priorities
  2465. :version "24.1"
  2466. :type '(choice
  2467. (const nil)
  2468. (function)))
  2469. (defgroup org-time nil
  2470. "Options concerning time stamps and deadlines in Org-mode."
  2471. :tag "Org Time"
  2472. :group 'org)
  2473. (defcustom org-insert-labeled-timestamps-at-point nil
  2474. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2475. When nil, these labeled time stamps are forces into the second line of an
  2476. entry, just after the headline. When scheduling from the global TODO list,
  2477. the time stamp will always be forced into the second line."
  2478. :group 'org-time
  2479. :type 'boolean)
  2480. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2481. "Formats for `format-time-string' which are used for time stamps.
  2482. It is not recommended to change this constant.")
  2483. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2484. "Number of minutes to round time stamps to.
  2485. These are two values, the first applies when first creating a time stamp.
  2486. The second applies when changing it with the commands `S-up' and `S-down'.
  2487. When changing the time stamp, this means that it will change in steps
  2488. of N minutes, as given by the second value.
  2489. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2490. numbers should be factors of 60, so for example 5, 10, 15.
  2491. When this is larger than 1, you can still force an exact time stamp by using
  2492. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2493. and by using a prefix arg to `S-up/down' to specify the exact number
  2494. of minutes to shift."
  2495. :group 'org-time
  2496. :get #'(lambda (var) ; Make sure both elements are there
  2497. (if (integerp (default-value var))
  2498. (list (default-value var) 5)
  2499. (default-value var)))
  2500. :type '(list
  2501. (integer :tag "when inserting times")
  2502. (integer :tag "when modifying times")))
  2503. ;; Normalize old customizations of this variable.
  2504. (when (integerp org-time-stamp-rounding-minutes)
  2505. (setq org-time-stamp-rounding-minutes
  2506. (list org-time-stamp-rounding-minutes
  2507. org-time-stamp-rounding-minutes)))
  2508. (defcustom org-display-custom-times nil
  2509. "Non-nil means overlay custom formats over all time stamps.
  2510. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2511. To turn this on on a per-file basis, insert anywhere in the file:
  2512. #+STARTUP: customtime"
  2513. :group 'org-time
  2514. :set 'set-default
  2515. :type 'sexp)
  2516. (make-variable-buffer-local 'org-display-custom-times)
  2517. (defcustom org-time-stamp-custom-formats
  2518. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2519. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2520. These are overlaid over the default ISO format if the variable
  2521. `org-display-custom-times' is set. Time like %H:%M should be at the
  2522. end of the second format. The custom formats are also honored by export
  2523. commands, if custom time display is turned on at the time of export."
  2524. :group 'org-time
  2525. :type 'sexp)
  2526. (defun org-time-stamp-format (&optional long inactive)
  2527. "Get the right format for a time string."
  2528. (let ((f (if long (cdr org-time-stamp-formats)
  2529. (car org-time-stamp-formats))))
  2530. (if inactive
  2531. (concat "[" (substring f 1 -1) "]")
  2532. f)))
  2533. (defcustom org-time-clocksum-format
  2534. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2535. "The format string used when creating CLOCKSUM lines.
  2536. This is also used when Org mode generates a time duration.
  2537. The value can be a single format string containing two
  2538. %-sequences, which will be filled with the number of hours and
  2539. minutes in that order.
  2540. Alternatively, the value can be a plist associating any of the
  2541. keys :years, :months, :weeks, :days, :hours or :minutes with
  2542. format strings. The time duration is formatted using only the
  2543. time components that are needed and concatenating the results.
  2544. If a time unit in absent, it falls back to the next smallest
  2545. unit.
  2546. The keys :require-years, :require-months, :require-days,
  2547. :require-weeks, :require-hours, :require-minutes are also
  2548. meaningful. A non-nil value for these keys indicates that the
  2549. corresponding time component should always be included, even if
  2550. its value is 0.
  2551. For example,
  2552. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2553. :require-minutes t)
  2554. means durations longer than a day will be expressed in days,
  2555. hours and minutes, and durations less than a day will always be
  2556. expressed in hours and minutes (even for durations less than an
  2557. hour).
  2558. The value
  2559. \(:days \"%dd\" :minutes \"%dm\")
  2560. means durations longer than a day will be expressed in days and
  2561. minutes, and durations less than a day will be expressed entirely
  2562. in minutes (even for durations longer than an hour)."
  2563. :group 'org-time
  2564. :group 'org-clock
  2565. :version "24.4"
  2566. :package-version '(Org . "8.0")
  2567. :type '(choice (string :tag "Format string")
  2568. (set :tag "Plist"
  2569. (group :inline t (const :tag "Years" :years)
  2570. (string :tag "Format string"))
  2571. (group :inline t
  2572. (const :tag "Always show years" :require-years)
  2573. (const t))
  2574. (group :inline t (const :tag "Months" :months)
  2575. (string :tag "Format string"))
  2576. (group :inline t
  2577. (const :tag "Always show months" :require-months)
  2578. (const t))
  2579. (group :inline t (const :tag "Weeks" :weeks)
  2580. (string :tag "Format string"))
  2581. (group :inline t
  2582. (const :tag "Always show weeks" :require-weeks)
  2583. (const t))
  2584. (group :inline t (const :tag "Days" :days)
  2585. (string :tag "Format string"))
  2586. (group :inline t
  2587. (const :tag "Always show days" :require-days)
  2588. (const t))
  2589. (group :inline t (const :tag "Hours" :hours)
  2590. (string :tag "Format string"))
  2591. (group :inline t
  2592. (const :tag "Always show hours" :require-hours)
  2593. (const t))
  2594. (group :inline t (const :tag "Minutes" :minutes)
  2595. (string :tag "Format string"))
  2596. (group :inline t
  2597. (const :tag "Always show minutes" :require-minutes)
  2598. (const t)))))
  2599. (defcustom org-time-clocksum-use-fractional nil
  2600. "When non-nil, \\[org-clock-display] uses fractional times.
  2601. See `org-time-clocksum-format' for more on time clock formats."
  2602. :group 'org-time
  2603. :group 'org-clock
  2604. :version "24.3"
  2605. :type 'boolean)
  2606. (defcustom org-time-clocksum-use-effort-durations nil
  2607. "When non-nil, \\[org-clock-display] uses effort durations.
  2608. E.g. by default, one day is considered to be a 8 hours effort,
  2609. so a task that has been clocked for 16 hours will be displayed
  2610. as during 2 days in the clock display or in the clocktable.
  2611. See `org-effort-durations' on how to set effort durations
  2612. and `org-time-clocksum-format' for more on time clock formats."
  2613. :group 'org-time
  2614. :group 'org-clock
  2615. :version "24.4"
  2616. :package-version '(Org . "8.0")
  2617. :type 'boolean)
  2618. (defcustom org-time-clocksum-fractional-format "%.2f"
  2619. "The format string used when creating CLOCKSUM lines,
  2620. or when Org mode generates a time duration, if
  2621. `org-time-clocksum-use-fractional' is enabled.
  2622. The value can be a single format string containing one
  2623. %-sequence, which will be filled with the number of hours as
  2624. a float.
  2625. Alternatively, the value can be a plist associating any of the
  2626. keys :years, :months, :weeks, :days, :hours or :minutes with
  2627. a format string. The time duration is formatted using the
  2628. largest time unit which gives a non-zero integer part. If all
  2629. specified formats have zero integer part, the smallest time unit
  2630. is used."
  2631. :group 'org-time
  2632. :type '(choice (string :tag "Format string")
  2633. (set (group :inline t (const :tag "Years" :years)
  2634. (string :tag "Format string"))
  2635. (group :inline t (const :tag "Months" :months)
  2636. (string :tag "Format string"))
  2637. (group :inline t (const :tag "Weeks" :weeks)
  2638. (string :tag "Format string"))
  2639. (group :inline t (const :tag "Days" :days)
  2640. (string :tag "Format string"))
  2641. (group :inline t (const :tag "Hours" :hours)
  2642. (string :tag "Format string"))
  2643. (group :inline t (const :tag "Minutes" :minutes)
  2644. (string :tag "Format string")))))
  2645. (defcustom org-deadline-warning-days 14
  2646. "Number of days before expiration during which a deadline becomes active.
  2647. This variable governs the display in sparse trees and in the agenda.
  2648. When 0 or negative, it means use this number (the absolute value of it)
  2649. even if a deadline has a different individual lead time specified.
  2650. Custom commands can set this variable in the options section."
  2651. :group 'org-time
  2652. :group 'org-agenda-daily/weekly
  2653. :type 'integer)
  2654. (defcustom org-scheduled-delay-days 0
  2655. "Number of days before a scheduled item becomes active.
  2656. This variable governs the display in sparse trees and in the agenda.
  2657. The default value (i.e. 0) means: don't delay scheduled item.
  2658. When negative, it means use this number (the absolute value of it)
  2659. even if a scheduled item has a different individual delay time
  2660. specified.
  2661. Custom commands can set this variable in the options section."
  2662. :group 'org-time
  2663. :group 'org-agenda-daily/weekly
  2664. :version "24.4"
  2665. :package-version '(Org . "8.0")
  2666. :type 'integer)
  2667. (defcustom org-read-date-prefer-future t
  2668. "Non-nil means assume future for incomplete date input from user.
  2669. This affects the following situations:
  2670. 1. The user gives a month but not a year.
  2671. For example, if it is April and you enter \"feb 2\", this will be read
  2672. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2673. 2. The user gives a day, but no month.
  2674. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2675. read this as the third of *next* month. However, if you enter \"17\",
  2676. it will be considered as *this* month.
  2677. If you set this variable to the symbol `time', then also the following
  2678. will work:
  2679. 3. If the user gives a time.
  2680. If the time is before now, it will be interpreted as tomorrow.
  2681. Currently none of this works for ISO week specifications.
  2682. When this option is nil, the current day, month and year will always be
  2683. used as defaults.
  2684. See also `org-agenda-jump-prefer-future'."
  2685. :group 'org-time
  2686. :type '(choice
  2687. (const :tag "Never" nil)
  2688. (const :tag "Check month and day" t)
  2689. (const :tag "Check month, day, and time" time)))
  2690. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2691. "Should the agenda jump command prefer the future for incomplete dates?
  2692. The default is to do the same as configured in `org-read-date-prefer-future'.
  2693. But you can also set a deviating value here.
  2694. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2695. :group 'org-agenda
  2696. :group 'org-time
  2697. :version "24.1"
  2698. :type '(choice
  2699. (const :tag "Use org-read-date-prefer-future"
  2700. org-read-date-prefer-future)
  2701. (const :tag "Never" nil)
  2702. (const :tag "Always" t)))
  2703. (defcustom org-read-date-force-compatible-dates t
  2704. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2705. Depending on the system Emacs is running on, certain dates cannot
  2706. be represented with the type used internally to represent time.
  2707. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2708. correctly. Some systems allow for earlier dates, some for later,
  2709. some for both. One way to find out it to insert any date into an
  2710. Org buffer, putting the cursor on the year and hitting S-up and
  2711. S-down to test the range.
  2712. When this variable is set to t, the date/time prompt will not let
  2713. you specify dates outside the 1970-2037 range, so it is certain that
  2714. these dates will work in whatever version of Emacs you are
  2715. running, and also that you can move a file from one Emacs implementation
  2716. to another. WHenever Org is forcing the year for you, it will display
  2717. a message and beep.
  2718. When this variable is nil, Org will check if the date is
  2719. representable in the specific Emacs implementation you are using.
  2720. If not, it will force a year, usually the current year, and beep
  2721. to remind you. Currently this setting is not recommended because
  2722. the likelihood that you will open your Org files in an Emacs that
  2723. has limited date range is not negligible.
  2724. A workaround for this problem is to use diary sexp dates for time
  2725. stamps outside of this range."
  2726. :group 'org-time
  2727. :version "24.1"
  2728. :type 'boolean)
  2729. (defcustom org-read-date-display-live t
  2730. "Non-nil means display current interpretation of date prompt live.
  2731. This display will be in an overlay, in the minibuffer."
  2732. :group 'org-time
  2733. :type 'boolean)
  2734. (defcustom org-read-date-popup-calendar t
  2735. "Non-nil means pop up a calendar when prompting for a date.
  2736. In the calendar, the date can be selected with mouse-1. However, the
  2737. minibuffer will also be active, and you can simply enter the date as well.
  2738. When nil, only the minibuffer will be available."
  2739. :group 'org-time
  2740. :type 'boolean)
  2741. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2742. 'org-read-date-popup-calendar)
  2743. (make-obsolete-variable
  2744. 'org-read-date-minibuffer-setup-hook
  2745. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2746. (defcustom org-read-date-minibuffer-setup-hook nil
  2747. "Hook to be used to set up keys for the date/time interface.
  2748. Add key definitions to `minibuffer-local-map', which will be a
  2749. temporary copy.
  2750. WARNING: This option is obsolete, you should use
  2751. `org-read-date-minibuffer-local-map' to set up keys."
  2752. :group 'org-time
  2753. :type 'hook)
  2754. (defcustom org-extend-today-until 0
  2755. "The hour when your day really ends. Must be an integer.
  2756. This has influence for the following applications:
  2757. - When switching the agenda to \"today\". It it is still earlier than
  2758. the time given here, the day recognized as TODAY is actually yesterday.
  2759. - When a date is read from the user and it is still before the time given
  2760. here, the current date and time will be assumed to be yesterday, 23:59.
  2761. Also, timestamps inserted in capture templates follow this rule.
  2762. IMPORTANT: This is a feature whose implementation is and likely will
  2763. remain incomplete. Really, it is only here because past midnight seems to
  2764. be the favorite working time of John Wiegley :-)"
  2765. :group 'org-time
  2766. :type 'integer)
  2767. (defcustom org-use-effective-time nil
  2768. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2769. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2770. \"effective time\" of any timestamps between midnight and 8am will be
  2771. 23:59 of the previous day."
  2772. :group 'org-time
  2773. :version "24.1"
  2774. :type 'boolean)
  2775. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2776. "When non-nil, use the last clock out time for `org-todo'.
  2777. Note that this option has precedence over the combined use of
  2778. `org-use-effective-time' and `org-extend-today-until'."
  2779. :group 'org-time
  2780. :version "24.4"
  2781. :package-version '(Org . "8.0")
  2782. :type 'boolean)
  2783. (defcustom org-edit-timestamp-down-means-later nil
  2784. "Non-nil means S-down will increase the time in a time stamp.
  2785. When nil, S-up will increase."
  2786. :group 'org-time
  2787. :type 'boolean)
  2788. (defcustom org-calendar-follow-timestamp-change t
  2789. "Non-nil means make the calendar window follow timestamp changes.
  2790. When a timestamp is modified and the calendar window is visible, it will be
  2791. moved to the new date."
  2792. :group 'org-time
  2793. :type 'boolean)
  2794. (defgroup org-tags nil
  2795. "Options concerning tags in Org-mode."
  2796. :tag "Org Tags"
  2797. :group 'org)
  2798. (defcustom org-tag-alist nil
  2799. "List of tags allowed in Org-mode files.
  2800. When this list is nil, Org-mode will base TAG input on what is already in the
  2801. buffer.
  2802. The value of this variable is an alist, the car of each entry must be a
  2803. keyword as a string, the cdr may be a character that is used to select
  2804. that tag through the fast-tag-selection interface.
  2805. See the manual for details."
  2806. :group 'org-tags
  2807. :type '(repeat
  2808. (choice
  2809. (cons (string :tag "Tag name")
  2810. (character :tag "Access char"))
  2811. (list :tag "Start radio group"
  2812. (const :startgroup)
  2813. (option (string :tag "Group description")))
  2814. (list :tag "Group tags delimiter"
  2815. (const :grouptags))
  2816. (list :tag "End radio group"
  2817. (const :endgroup)
  2818. (option (string :tag "Group description")))
  2819. (const :tag "New line" (:newline)))))
  2820. (defcustom org-tag-persistent-alist nil
  2821. "List of tags that will always appear in all Org-mode files.
  2822. This is in addition to any in buffer settings or customizations
  2823. of `org-tag-alist'.
  2824. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2825. The value of this variable is an alist, the car of each entry must be a
  2826. keyword as a string, the cdr may be a character that is used to select
  2827. that tag through the fast-tag-selection interface.
  2828. See the manual for details.
  2829. To disable these tags on a per-file basis, insert anywhere in the file:
  2830. #+STARTUP: noptag"
  2831. :group 'org-tags
  2832. :type '(repeat
  2833. (choice
  2834. (cons (string :tag "Tag name")
  2835. (character :tag "Access char"))
  2836. (const :tag "Start radio group" (:startgroup))
  2837. (const :tag "Group tags delimiter" (:grouptags))
  2838. (const :tag "End radio group" (:endgroup))
  2839. (const :tag "New line" (:newline)))))
  2840. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2841. "If non-nil, always offer completion for all tags of all agenda files.
  2842. Instead of customizing this variable directly, you might want to
  2843. set it locally for capture buffers, because there no list of
  2844. tags in that file can be created dynamically (there are none).
  2845. (add-hook 'org-capture-mode-hook
  2846. (lambda ()
  2847. (set (make-local-variable
  2848. 'org-complete-tags-always-offer-all-agenda-tags)
  2849. t)))"
  2850. :group 'org-tags
  2851. :version "24.1"
  2852. :type 'boolean)
  2853. (defvar org-file-tags nil
  2854. "List of tags that can be inherited by all entries in the file.
  2855. The tags will be inherited if the variable `org-use-tag-inheritance'
  2856. says they should be.
  2857. This variable is populated from #+FILETAGS lines.")
  2858. (defcustom org-use-fast-tag-selection 'auto
  2859. "Non-nil means use fast tag selection scheme.
  2860. This is a special interface to select and deselect tags with single keys.
  2861. When nil, fast selection is never used.
  2862. When the symbol `auto', fast selection is used if and only if selection
  2863. characters for tags have been configured, either through the variable
  2864. `org-tag-alist' or through a #+TAGS line in the buffer.
  2865. When t, fast selection is always used and selection keys are assigned
  2866. automatically if necessary."
  2867. :group 'org-tags
  2868. :type '(choice
  2869. (const :tag "Always" t)
  2870. (const :tag "Never" nil)
  2871. (const :tag "When selection characters are configured" auto)))
  2872. (defcustom org-fast-tag-selection-single-key nil
  2873. "Non-nil means fast tag selection exits after first change.
  2874. When nil, you have to press RET to exit it.
  2875. During fast tag selection, you can toggle this flag with `C-c'.
  2876. This variable can also have the value `expert'. In this case, the window
  2877. displaying the tags menu is not even shown, until you press C-c again."
  2878. :group 'org-tags
  2879. :type '(choice
  2880. (const :tag "No" nil)
  2881. (const :tag "Yes" t)
  2882. (const :tag "Expert" expert)))
  2883. (defvar org-fast-tag-selection-include-todo nil
  2884. "Non-nil means fast tags selection interface will also offer TODO states.
  2885. This is an undocumented feature, you should not rely on it.")
  2886. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2887. "The column to which tags should be indented in a headline.
  2888. If this number is positive, it specifies the column. If it is negative,
  2889. it means that the tags should be flushright to that column. For example,
  2890. -80 works well for a normal 80 character screen.
  2891. When 0, place tags directly after headline text, with only one space in
  2892. between."
  2893. :group 'org-tags
  2894. :type 'integer)
  2895. (defcustom org-auto-align-tags t
  2896. "Non-nil keeps tags aligned when modifying headlines.
  2897. Some operations (i.e. demoting) change the length of a headline and
  2898. therefore shift the tags around. With this option turned on, after
  2899. each such operation the tags are again aligned to `org-tags-column'."
  2900. :group 'org-tags
  2901. :type 'boolean)
  2902. (defcustom org-use-tag-inheritance t
  2903. "Non-nil means tags in levels apply also for sublevels.
  2904. When nil, only the tags directly given in a specific line apply there.
  2905. This may also be a list of tags that should be inherited, or a regexp that
  2906. matches tags that should be inherited. Additional control is possible
  2907. with the variable `org-tags-exclude-from-inheritance' which gives an
  2908. explicit list of tags to be excluded from inheritance, even if the value of
  2909. `org-use-tag-inheritance' would select it for inheritance.
  2910. If this option is t, a match early-on in a tree can lead to a large
  2911. number of matches in the subtree when constructing the agenda or creating
  2912. a sparse tree. If you only want to see the first match in a tree during
  2913. a search, check out the variable `org-tags-match-list-sublevels'."
  2914. :group 'org-tags
  2915. :type '(choice
  2916. (const :tag "Not" nil)
  2917. (const :tag "Always" t)
  2918. (repeat :tag "Specific tags" (string :tag "Tag"))
  2919. (regexp :tag "Tags matched by regexp")))
  2920. (defcustom org-tags-exclude-from-inheritance nil
  2921. "List of tags that should never be inherited.
  2922. This is a way to exclude a few tags from inheritance. For way to do
  2923. the opposite, to actively allow inheritance for selected tags,
  2924. see the variable `org-use-tag-inheritance'."
  2925. :group 'org-tags
  2926. :type '(repeat (string :tag "Tag")))
  2927. (defun org-tag-inherit-p (tag)
  2928. "Check if TAG is one that should be inherited."
  2929. (cond
  2930. ((member tag org-tags-exclude-from-inheritance) nil)
  2931. ((eq org-use-tag-inheritance t) t)
  2932. ((not org-use-tag-inheritance) nil)
  2933. ((stringp org-use-tag-inheritance)
  2934. (string-match org-use-tag-inheritance tag))
  2935. ((listp org-use-tag-inheritance)
  2936. (member tag org-use-tag-inheritance))
  2937. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2938. (defcustom org-tags-match-list-sublevels t
  2939. "Non-nil means list also sublevels of headlines matching a search.
  2940. This variable applies to tags/property searches, and also to stuck
  2941. projects because this search is based on a tags match as well.
  2942. When set to the symbol `indented', sublevels are indented with
  2943. leading dots.
  2944. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2945. the sublevels of a headline matching a tag search often also match
  2946. the same search. Listing all of them can create very long lists.
  2947. Setting this variable to nil causes subtrees of a match to be skipped.
  2948. This variable is semi-obsolete and probably should always be true. It
  2949. is better to limit inheritance to certain tags using the variables
  2950. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2951. :group 'org-tags
  2952. :type '(choice
  2953. (const :tag "No, don't list them" nil)
  2954. (const :tag "Yes, do list them" t)
  2955. (const :tag "List them, indented with leading dots" indented)))
  2956. (defcustom org-tags-sort-function nil
  2957. "When set, tags are sorted using this function as a comparator."
  2958. :group 'org-tags
  2959. :type '(choice
  2960. (const :tag "No sorting" nil)
  2961. (const :tag "Alphabetical" string<)
  2962. (const :tag "Reverse alphabetical" string>)
  2963. (function :tag "Custom function" nil)))
  2964. (defvar org-tags-history nil
  2965. "History of minibuffer reads for tags.")
  2966. (defvar org-last-tags-completion-table nil
  2967. "The last used completion table for tags.")
  2968. (defvar org-after-tags-change-hook nil
  2969. "Hook that is run after the tags in a line have changed.")
  2970. (defgroup org-properties nil
  2971. "Options concerning properties in Org-mode."
  2972. :tag "Org Properties"
  2973. :group 'org)
  2974. (defcustom org-property-format "%-10s %s"
  2975. "How property key/value pairs should be formatted by `indent-line'.
  2976. When `indent-line' hits a property definition, it will format the line
  2977. according to this format, mainly to make sure that the values are
  2978. lined-up with respect to each other."
  2979. :group 'org-properties
  2980. :type 'string)
  2981. (defcustom org-properties-postprocess-alist nil
  2982. "Alist of properties and functions to adjust inserted values.
  2983. Elements of this alist must be of the form
  2984. ([string] [function])
  2985. where [string] must be a property name and [function] must be a
  2986. lambda expression: this lambda expression must take one argument,
  2987. the value to adjust, and return the new value as a string.
  2988. For example, this element will allow the property \"Remaining\"
  2989. to be updated wrt the relation between the \"Effort\" property
  2990. and the clock summary:
  2991. ((\"Remaining\" (lambda(value)
  2992. (let ((clocksum (org-clock-sum-current-item))
  2993. (effort (org-duration-string-to-minutes
  2994. (org-entry-get (point) \"Effort\"))))
  2995. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2996. :group 'org-properties
  2997. :version "24.1"
  2998. :type '(alist :key-type (string :tag "Property")
  2999. :value-type (function :tag "Function")))
  3000. (defcustom org-use-property-inheritance nil
  3001. "Non-nil means properties apply also for sublevels.
  3002. This setting is chiefly used during property searches. Turning it on can
  3003. cause significant overhead when doing a search, which is why it is not
  3004. on by default.
  3005. When nil, only the properties directly given in the current entry count.
  3006. When t, every property is inherited. The value may also be a list of
  3007. properties that should have inheritance, or a regular expression matching
  3008. properties that should be inherited.
  3009. However, note that some special properties use inheritance under special
  3010. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3011. and the properties ending in \"_ALL\" when they are used as descriptor
  3012. for valid values of a property.
  3013. Note for programmers:
  3014. When querying an entry with `org-entry-get', you can control if inheritance
  3015. should be used. By default, `org-entry-get' looks only at the local
  3016. properties. You can request inheritance by setting the inherit argument
  3017. to t (to force inheritance) or to `selective' (to respect the setting
  3018. in this variable)."
  3019. :group 'org-properties
  3020. :type '(choice
  3021. (const :tag "Not" nil)
  3022. (const :tag "Always" t)
  3023. (repeat :tag "Specific properties" (string :tag "Property"))
  3024. (regexp :tag "Properties matched by regexp")))
  3025. (defun org-property-inherit-p (property)
  3026. "Check if PROPERTY is one that should be inherited."
  3027. (cond
  3028. ((eq org-use-property-inheritance t) t)
  3029. ((not org-use-property-inheritance) nil)
  3030. ((stringp org-use-property-inheritance)
  3031. (string-match org-use-property-inheritance property))
  3032. ((listp org-use-property-inheritance)
  3033. (member property org-use-property-inheritance))
  3034. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3035. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3036. "The default column format, if no other format has been defined.
  3037. This variable can be set on the per-file basis by inserting a line
  3038. #+COLUMNS: %25ITEM ....."
  3039. :group 'org-properties
  3040. :type 'string)
  3041. (defcustom org-columns-ellipses ".."
  3042. "The ellipses to be used when a field in column view is truncated.
  3043. When this is the empty string, as many characters as possible are shown,
  3044. but then there will be no visual indication that the field has been truncated.
  3045. When this is a string of length N, the last N characters of a truncated
  3046. field are replaced by this string. If the column is narrower than the
  3047. ellipses string, only part of the ellipses string will be shown."
  3048. :group 'org-properties
  3049. :type 'string)
  3050. (defcustom org-columns-modify-value-for-display-function nil
  3051. "Function that modifies values for display in column view.
  3052. For example, it can be used to cut out a certain part from a time stamp.
  3053. The function must take 2 arguments:
  3054. column-title The title of the column (*not* the property name)
  3055. value The value that should be modified.
  3056. The function should return the value that should be displayed,
  3057. or nil if the normal value should be used."
  3058. :group 'org-properties
  3059. :type '(choice (const nil) (function)))
  3060. (defcustom org-effort-property "Effort"
  3061. "The property that is being used to keep track of effort estimates.
  3062. Effort estimates given in this property need to have the format H:MM."
  3063. :group 'org-properties
  3064. :group 'org-progress
  3065. :type '(string :tag "Property"))
  3066. (defconst org-global-properties-fixed
  3067. '(("VISIBILITY_ALL" . "folded children content all")
  3068. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3069. "List of property/value pairs that can be inherited by any entry.
  3070. These are fixed values, for the preset properties. The user variable
  3071. that can be used to add to this list is `org-global-properties'.
  3072. The entries in this list are cons cells where the car is a property
  3073. name and cdr is a string with the value. If the value represents
  3074. multiple items like an \"_ALL\" property, separate the items by
  3075. spaces.")
  3076. (defcustom org-global-properties nil
  3077. "List of property/value pairs that can be inherited by any entry.
  3078. This list will be combined with the constant `org-global-properties-fixed'.
  3079. The entries in this list are cons cells where the car is a property
  3080. name and cdr is a string with the value.
  3081. You can set buffer-local values for the same purpose in the variable
  3082. `org-file-properties' this by adding lines like
  3083. #+PROPERTY: NAME VALUE"
  3084. :group 'org-properties
  3085. :type '(repeat
  3086. (cons (string :tag "Property")
  3087. (string :tag "Value"))))
  3088. (defvar org-file-properties nil
  3089. "List of property/value pairs that can be inherited by any entry.
  3090. Valid for the current buffer.
  3091. This variable is populated from #+PROPERTY lines.")
  3092. (make-variable-buffer-local 'org-file-properties)
  3093. (defgroup org-agenda nil
  3094. "Options concerning agenda views in Org-mode."
  3095. :tag "Org Agenda"
  3096. :group 'org)
  3097. (defvar org-category nil
  3098. "Variable used by org files to set a category for agenda display.
  3099. Such files should use a file variable to set it, for example
  3100. # -*- mode: org; org-category: \"ELisp\"
  3101. or contain a special line
  3102. #+CATEGORY: ELisp
  3103. If the file does not specify a category, then file's base name
  3104. is used instead.")
  3105. (make-variable-buffer-local 'org-category)
  3106. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3107. (defcustom org-agenda-files nil
  3108. "The files to be used for agenda display.
  3109. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3110. \\[org-remove-file]. You can also use customize to edit the list.
  3111. If an entry is a directory, all files in that directory that are matched by
  3112. `org-agenda-file-regexp' will be part of the file list.
  3113. If the value of the variable is not a list but a single file name, then
  3114. the list of agenda files is actually stored and maintained in that file, one
  3115. agenda file per line. In this file paths can be given relative to
  3116. `org-directory'. Tilde expansion and environment variable substitution
  3117. are also made."
  3118. :group 'org-agenda
  3119. :type '(choice
  3120. (repeat :tag "List of files and directories" file)
  3121. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3122. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3123. "Regular expression to match files for `org-agenda-files'.
  3124. If any element in the list in that variable contains a directory instead
  3125. of a normal file, all files in that directory that are matched by this
  3126. regular expression will be included."
  3127. :group 'org-agenda
  3128. :type 'regexp)
  3129. (defcustom org-agenda-text-search-extra-files nil
  3130. "List of extra files to be searched by text search commands.
  3131. These files will be searched in addition to the agenda files by the
  3132. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3133. Note that these files will only be searched for text search commands,
  3134. not for the other agenda views like todo lists, tag searches or the weekly
  3135. agenda. This variable is intended to list notes and possibly archive files
  3136. that should also be searched by these two commands.
  3137. In fact, if the first element in the list is the symbol `agenda-archives',
  3138. then all archive files of all agenda files will be added to the search
  3139. scope."
  3140. :group 'org-agenda
  3141. :type '(set :greedy t
  3142. (const :tag "Agenda Archives" agenda-archives)
  3143. (repeat :inline t (file))))
  3144. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3145. 'org-agenda-text-search-extra-files)
  3146. (defcustom org-agenda-skip-unavailable-files nil
  3147. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3148. A nil value means to remove them, after a query, from the list."
  3149. :group 'org-agenda
  3150. :type 'boolean)
  3151. (defcustom org-calendar-to-agenda-key [?c]
  3152. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3153. The command `org-calendar-goto-agenda' will be bound to this key. The
  3154. default is the character `c' because then `c' can be used to switch back and
  3155. forth between agenda and calendar."
  3156. :group 'org-agenda
  3157. :type 'sexp)
  3158. (defcustom org-calendar-insert-diary-entry-key [?i]
  3159. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3160. This option is irrelevant until `org-agenda-diary-file' has been configured
  3161. to point to an Org-mode file. When that is the case, the command
  3162. `org-agenda-diary-entry' will be bound to the key given here, by default
  3163. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3164. if you want to continue doing this, you need to change this to a different
  3165. key."
  3166. :group 'org-agenda
  3167. :type 'sexp)
  3168. (defcustom org-agenda-diary-file 'diary-file
  3169. "File to which to add new entries with the `i' key in agenda and calendar.
  3170. When this is the symbol `diary-file', the functionality in the Emacs
  3171. calendar will be used to add entries to the `diary-file'. But when this
  3172. points to a file, `org-agenda-diary-entry' will be used instead."
  3173. :group 'org-agenda
  3174. :type '(choice
  3175. (const :tag "The standard Emacs diary file" diary-file)
  3176. (file :tag "Special Org file diary entries")))
  3177. (eval-after-load "calendar"
  3178. '(progn
  3179. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3180. 'org-calendar-goto-agenda)
  3181. (add-hook 'calendar-mode-hook
  3182. (lambda ()
  3183. (unless (eq org-agenda-diary-file 'diary-file)
  3184. (define-key calendar-mode-map
  3185. org-calendar-insert-diary-entry-key
  3186. 'org-agenda-diary-entry))))))
  3187. (defgroup org-latex nil
  3188. "Options for embedding LaTeX code into Org-mode."
  3189. :tag "Org LaTeX"
  3190. :group 'org)
  3191. (defcustom org-format-latex-options
  3192. '(:foreground default :background default :scale 1.0
  3193. :html-foreground "Black" :html-background "Transparent"
  3194. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3195. "Options for creating images from LaTeX fragments.
  3196. This is a property list with the following properties:
  3197. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3198. `default' means use the foreground of the default face.
  3199. `auto' means use the foreground from the text face.
  3200. :background the background color, or \"Transparent\".
  3201. `default' means use the background of the default face.
  3202. `auto' means use the background from the text face.
  3203. :scale a scaling factor for the size of the images, to get more pixels
  3204. :html-foreground, :html-background, :html-scale
  3205. the same numbers for HTML export.
  3206. :matchers a list indicating which matchers should be used to
  3207. find LaTeX fragments. Valid members of this list are:
  3208. \"begin\" find environments
  3209. \"$1\" find single characters surrounded by $.$
  3210. \"$\" find math expressions surrounded by $...$
  3211. \"$$\" find math expressions surrounded by $$....$$
  3212. \"\\(\" find math expressions surrounded by \\(...\\)
  3213. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3214. :group 'org-latex
  3215. :type 'plist)
  3216. (defcustom org-format-latex-signal-error t
  3217. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3218. When nil, just push out a message."
  3219. :group 'org-latex
  3220. :version "24.1"
  3221. :type 'boolean)
  3222. (defcustom org-latex-to-mathml-jar-file nil
  3223. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3224. Use this to specify additional executable file say a jar file.
  3225. When using MathToWeb as the converter, specify the full-path to
  3226. your mathtoweb.jar file."
  3227. :group 'org-latex
  3228. :version "24.1"
  3229. :type '(choice
  3230. (const :tag "None" nil)
  3231. (file :tag "JAR file" :must-match t)))
  3232. (defcustom org-latex-to-mathml-convert-command nil
  3233. "Command to convert LaTeX fragments to MathML.
  3234. Replace format-specifiers in the command as noted below and use
  3235. `shell-command' to convert LaTeX to MathML.
  3236. %j: Executable file in fully expanded form as specified by
  3237. `org-latex-to-mathml-jar-file'.
  3238. %I: Input LaTeX file in fully expanded form
  3239. %o: Output MathML file
  3240. This command is used by `org-create-math-formula'.
  3241. When using MathToWeb as the converter, set this to
  3242. \"java -jar %j -unicode -force -df %o %I\"."
  3243. :group 'org-latex
  3244. :version "24.1"
  3245. :type '(choice
  3246. (const :tag "None" nil)
  3247. (string :tag "\nShell command")))
  3248. (defcustom org-latex-create-formula-image-program 'dvipng
  3249. "Program to convert LaTeX fragments with.
  3250. dvipng Process the LaTeX fragments to dvi file, then convert
  3251. dvi files to png files using dvipng.
  3252. This will also include processing of non-math environments.
  3253. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3254. to convert pdf files to png files"
  3255. :group 'org-latex
  3256. :version "24.1"
  3257. :type '(choice
  3258. (const :tag "dvipng" dvipng)
  3259. (const :tag "imagemagick" imagemagick)))
  3260. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3261. "Path to store latex preview images.
  3262. A relative path here creates many directories relative to the
  3263. processed org files paths. An absolute path puts all preview
  3264. images at the same place."
  3265. :group 'org-latex
  3266. :version "24.3"
  3267. :type 'string)
  3268. (defun org-format-latex-mathml-available-p ()
  3269. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3270. (save-match-data
  3271. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3272. org-latex-to-mathml-convert-command)
  3273. (let ((executable (car (split-string
  3274. org-latex-to-mathml-convert-command))))
  3275. (when (executable-find executable)
  3276. (if (string-match
  3277. "%j" org-latex-to-mathml-convert-command)
  3278. (file-readable-p org-latex-to-mathml-jar-file)
  3279. t))))))
  3280. (defcustom org-format-latex-header "\\documentclass{article}
  3281. \\usepackage[usenames]{color}
  3282. \\usepackage{amsmath}
  3283. \\usepackage[mathscr]{eucal}
  3284. \\pagestyle{empty} % do not remove
  3285. \[PACKAGES]
  3286. \[DEFAULT-PACKAGES]
  3287. % The settings below are copied from fullpage.sty
  3288. \\setlength{\\textwidth}{\\paperwidth}
  3289. \\addtolength{\\textwidth}{-3cm}
  3290. \\setlength{\\oddsidemargin}{1.5cm}
  3291. \\addtolength{\\oddsidemargin}{-2.54cm}
  3292. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3293. \\setlength{\\textheight}{\\paperheight}
  3294. \\addtolength{\\textheight}{-\\headheight}
  3295. \\addtolength{\\textheight}{-\\headsep}
  3296. \\addtolength{\\textheight}{-\\footskip}
  3297. \\addtolength{\\textheight}{-3cm}
  3298. \\setlength{\\topmargin}{1.5cm}
  3299. \\addtolength{\\topmargin}{-2.54cm}"
  3300. "The document header used for processing LaTeX fragments.
  3301. It is imperative that this header make sure that no page number
  3302. appears on the page. The package defined in the variables
  3303. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3304. will either replace the placeholder \"[PACKAGES]\" in this
  3305. header, or they will be appended."
  3306. :group 'org-latex
  3307. :type 'string)
  3308. (defun org-set-packages-alist (var val)
  3309. "Set the packages alist and make sure it has 3 elements per entry."
  3310. (set var (mapcar (lambda (x)
  3311. (if (and (consp x) (= (length x) 2))
  3312. (list (car x) (nth 1 x) t)
  3313. x))
  3314. val)))
  3315. (defun org-get-packages-alist (var)
  3316. "Get the packages alist and make sure it has 3 elements per entry."
  3317. (mapcar (lambda (x)
  3318. (if (and (consp x) (= (length x) 2))
  3319. (list (car x) (nth 1 x) t)
  3320. x))
  3321. (default-value var)))
  3322. (defcustom org-latex-default-packages-alist
  3323. '(("AUTO" "inputenc" t)
  3324. ("T1" "fontenc" t)
  3325. ("" "fixltx2e" nil)
  3326. ("" "graphicx" t)
  3327. ("" "longtable" nil)
  3328. ("" "float" nil)
  3329. ("" "wrapfig" nil)
  3330. ("" "soul" t)
  3331. ("" "textcomp" t)
  3332. ("" "marvosym" t)
  3333. ("" "wasysym" t)
  3334. ("" "latexsym" t)
  3335. ("" "amssymb" t)
  3336. ("" "amstext" nil)
  3337. ("" "hyperref" nil)
  3338. "\\tolerance=1000")
  3339. "Alist of default packages to be inserted in the header.
  3340. Change this only if one of the packages here causes an
  3341. incompatibility with another package you are using.
  3342. The packages in this list are needed by one part or another of
  3343. Org mode to function properly:
  3344. - inputenc, fontenc: for basic font and character selection
  3345. - amstext: for subscript and superscript
  3346. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3347. symbols used for interpreting the entities in `org-entities'.
  3348. You can skip some of these packages if you don't use any of the
  3349. symbols in it.
  3350. - graphicx: for including images
  3351. - float, wrapfig: for figure placement
  3352. - longtable: for long tables
  3353. - hyperref: for cross references
  3354. Therefore you should not modify this variable unless you know
  3355. what you are doing. The one reason to change it anyway is that
  3356. you might be loading some other package that conflicts with one
  3357. of the default packages. Each cell is of the format
  3358. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3359. the package also needs to be included when compiling LaTeX
  3360. snippets into images for inclusion into non-LaTeX output."
  3361. :group 'org-latex
  3362. :group 'org-export-latex
  3363. :set 'org-set-packages-alist
  3364. :get 'org-get-packages-alist
  3365. :version "24.1"
  3366. :type '(repeat
  3367. (choice
  3368. (list :tag "options/package pair"
  3369. (string :tag "options")
  3370. (string :tag "package")
  3371. (boolean :tag "Snippet"))
  3372. (string :tag "A line of LaTeX"))))
  3373. (defcustom org-latex-packages-alist nil
  3374. "Alist of packages to be inserted in every LaTeX header.
  3375. These will be inserted after `org-latex-default-packages-alist'.
  3376. Each cell is of the format:
  3377. \(\"options\" \"package\" snippet-flag)
  3378. SNIPPET-FLAG, when t, indicates that this package is also needed
  3379. when turning LaTeX snippets into images for inclusion into
  3380. non-LaTeX output.
  3381. Make sure that you only list packages here which:
  3382. - you want in every file
  3383. - do not conflict with the setup in `org-format-latex-header'.
  3384. - do not conflict with the default packages in
  3385. `org-latex-default-packages-alist'."
  3386. :group 'org-latex
  3387. :group 'org-export-latex
  3388. :set 'org-set-packages-alist
  3389. :get 'org-get-packages-alist
  3390. :type '(repeat
  3391. (choice
  3392. (list :tag "options/package pair"
  3393. (string :tag "options")
  3394. (string :tag "package")
  3395. (boolean :tag "Snippet"))
  3396. (string :tag "A line of LaTeX"))))
  3397. (defgroup org-appearance nil
  3398. "Settings for Org-mode appearance."
  3399. :tag "Org Appearance"
  3400. :group 'org)
  3401. (defcustom org-level-color-stars-only nil
  3402. "Non-nil means fontify only the stars in each headline.
  3403. When nil, the entire headline is fontified.
  3404. Changing it requires restart of `font-lock-mode' to become effective
  3405. also in regions already fontified."
  3406. :group 'org-appearance
  3407. :type 'boolean)
  3408. (defcustom org-hide-leading-stars nil
  3409. "Non-nil means hide the first N-1 stars in a headline.
  3410. This works by using the face `org-hide' for these stars. This
  3411. face is white for a light background, and black for a dark
  3412. background. You may have to customize the face `org-hide' to
  3413. make this work.
  3414. Changing it requires restart of `font-lock-mode' to become effective
  3415. also in regions already fontified.
  3416. You may also set this on a per-file basis by adding one of the following
  3417. lines to the buffer:
  3418. #+STARTUP: hidestars
  3419. #+STARTUP: showstars"
  3420. :group 'org-appearance
  3421. :type 'boolean)
  3422. (defcustom org-hidden-keywords nil
  3423. "List of symbols corresponding to keywords to be hidden the org buffer.
  3424. For example, a value '(title) for this list will make the document's title
  3425. appear in the buffer without the initial #+TITLE: keyword."
  3426. :group 'org-appearance
  3427. :version "24.1"
  3428. :type '(set (const :tag "#+AUTHOR" author)
  3429. (const :tag "#+DATE" date)
  3430. (const :tag "#+EMAIL" email)
  3431. (const :tag "#+TITLE" title)))
  3432. (defcustom org-custom-properties nil
  3433. "List of properties (as strings) with a special meaning.
  3434. The default use of these custom properties is to let the user
  3435. hide them with `org-toggle-custom-properties-visibility'."
  3436. :group 'org-properties
  3437. :group 'org-appearance
  3438. :version "24.3"
  3439. :type '(repeat (string :tag "Property Name")))
  3440. (defcustom org-fontify-done-headline nil
  3441. "Non-nil means change the face of a headline if it is marked DONE.
  3442. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3443. When this is non-nil, the headline after the keyword is set to the
  3444. `org-headline-done' as an additional indication."
  3445. :group 'org-appearance
  3446. :type 'boolean)
  3447. (defcustom org-fontify-emphasized-text t
  3448. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3449. Changing this variable requires a restart of Emacs to take effect."
  3450. :group 'org-appearance
  3451. :type 'boolean)
  3452. (defcustom org-fontify-whole-heading-line nil
  3453. "Non-nil means fontify the whole line for headings.
  3454. This is useful when setting a background color for the
  3455. org-level-* faces."
  3456. :group 'org-appearance
  3457. :type 'boolean)
  3458. (defcustom org-highlight-latex-and-related nil
  3459. "Non-nil means highlight LaTeX related syntax in the buffer.
  3460. When non nil, the value should be a list containing any of the
  3461. following symbols:
  3462. `latex' Highlight LaTeX snippets and environments.
  3463. `script' Highlight subscript and superscript.
  3464. `entities' Highlight entities."
  3465. :group 'org-appearance
  3466. :version "24.4"
  3467. :package-version '(Org . "8.0")
  3468. :type '(choice
  3469. (const :tag "No highlighting" nil)
  3470. (set :greedy t :tag "Highlight"
  3471. (const :tag "LaTeX snippets and environments" latex)
  3472. (const :tag "Subscript and superscript" script)
  3473. (const :tag "Entities" entities))))
  3474. (defcustom org-hide-emphasis-markers nil
  3475. "Non-nil mean font-lock should hide the emphasis marker characters."
  3476. :group 'org-appearance
  3477. :type 'boolean)
  3478. (defcustom org-pretty-entities nil
  3479. "Non-nil means show entities as UTF8 characters.
  3480. When nil, the \\name form remains in the buffer."
  3481. :group 'org-appearance
  3482. :version "24.1"
  3483. :type 'boolean)
  3484. (defcustom org-pretty-entities-include-sub-superscripts t
  3485. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3486. :group 'org-appearance
  3487. :version "24.1"
  3488. :type 'boolean)
  3489. (defvar org-emph-re nil
  3490. "Regular expression for matching emphasis.
  3491. After a match, the match groups contain these elements:
  3492. 0 The match of the full regular expression, including the characters
  3493. before and after the proper match
  3494. 1 The character before the proper match, or empty at beginning of line
  3495. 2 The proper match, including the leading and trailing markers
  3496. 3 The leading marker like * or /, indicating the type of highlighting
  3497. 4 The text between the emphasis markers, not including the markers
  3498. 5 The character after the match, empty at the end of a line")
  3499. (defvar org-verbatim-re nil
  3500. "Regular expression for matching verbatim text.")
  3501. (defvar org-emphasis-regexp-components) ; defined just below
  3502. (defvar org-emphasis-alist) ; defined just below
  3503. (defun org-set-emph-re (var val)
  3504. "Set variable and compute the emphasis regular expression."
  3505. (set var val)
  3506. (when (and (boundp 'org-emphasis-alist)
  3507. (boundp 'org-emphasis-regexp-components)
  3508. org-emphasis-alist org-emphasis-regexp-components)
  3509. (let* ((e org-emphasis-regexp-components)
  3510. (pre (car e))
  3511. (post (nth 1 e))
  3512. (border (nth 2 e))
  3513. (body (nth 3 e))
  3514. (nl (nth 4 e))
  3515. (body1 (concat body "*?"))
  3516. (markers (mapconcat 'car org-emphasis-alist ""))
  3517. (vmarkers (mapconcat
  3518. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3519. org-emphasis-alist "")))
  3520. ;; make sure special characters appear at the right position in the class
  3521. (if (string-match "\\^" markers)
  3522. (setq markers (concat (replace-match "" t t markers) "^")))
  3523. (if (string-match "-" markers)
  3524. (setq markers (concat (replace-match "" t t markers) "-")))
  3525. (if (string-match "\\^" vmarkers)
  3526. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3527. (if (string-match "-" vmarkers)
  3528. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3529. (if (> nl 0)
  3530. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3531. (int-to-string nl) "\\}")))
  3532. ;; Make the regexp
  3533. (setq org-emph-re
  3534. (concat "\\([" pre "]\\|^\\)"
  3535. "\\("
  3536. "\\([" markers "]\\)"
  3537. "\\("
  3538. "[^" border "]\\|"
  3539. "[^" border "]"
  3540. body1
  3541. "[^" border "]"
  3542. "\\)"
  3543. "\\3\\)"
  3544. "\\([" post "]\\|$\\)"))
  3545. (setq org-verbatim-re
  3546. (concat "\\([" pre "]\\|^\\)"
  3547. "\\("
  3548. "\\([" vmarkers "]\\)"
  3549. "\\("
  3550. "[^" border "]\\|"
  3551. "[^" border "]"
  3552. body1
  3553. "[^" border "]"
  3554. "\\)"
  3555. "\\3\\)"
  3556. "\\([" post "]\\|$\\)")))))
  3557. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3558. ;; set this option proved cumbersome. See this message/thread:
  3559. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3560. (defvar org-emphasis-regexp-components
  3561. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3562. "Components used to build the regular expression for emphasis.
  3563. This is a list with five entries. Terminology: In an emphasis string
  3564. like \" *strong word* \", we call the initial space PREMATCH, the final
  3565. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3566. and \"trong wor\" is the body. The different components in this variable
  3567. specify what is allowed/forbidden in each part:
  3568. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3569. post Chars allowed as postmatch. End of line will be allowed too.
  3570. border The chars *forbidden* as border characters.
  3571. body-regexp A regexp like \".\" to match a body character. Don't use
  3572. non-shy groups here, and don't allow newline here.
  3573. newline The maximum number of newlines allowed in an emphasis exp.
  3574. You need to reload Org or to restart Emacs after customizing this.")
  3575. (defcustom org-emphasis-alist
  3576. `(("*" bold)
  3577. ("/" italic)
  3578. ("_" underline)
  3579. ("=" org-code verbatim)
  3580. ("~" org-verbatim verbatim)
  3581. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3582. "Alist of characters and faces to emphasize text.
  3583. Text starting and ending with a special character will be emphasized,
  3584. for example *bold*, _underlined_ and /italic/. This variable sets the
  3585. marker characters and the face to be used by font-lock for highlighting
  3586. in Org-mode Emacs buffers.
  3587. You need to reload Org or to restart Emacs after customizing this."
  3588. :group 'org-appearance
  3589. :set 'org-set-emph-re
  3590. :version "24.4"
  3591. :package-version '(Org . "8.0")
  3592. :type '(repeat
  3593. (list
  3594. (string :tag "Marker character")
  3595. (choice
  3596. (face :tag "Font-lock-face")
  3597. (plist :tag "Face property list"))
  3598. (option (const verbatim)))))
  3599. (defvar org-protecting-blocks
  3600. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3601. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3602. This is needed for font-lock setup.")
  3603. ;;; Miscellaneous options
  3604. (defgroup org-completion nil
  3605. "Completion in Org-mode."
  3606. :tag "Org Completion"
  3607. :group 'org)
  3608. (defcustom org-completion-use-ido nil
  3609. "Non-nil means use ido completion wherever possible.
  3610. Note that `ido-mode' must be active for this variable to be relevant.
  3611. If you decide to turn this variable on, you might well want to turn off
  3612. `org-outline-path-complete-in-steps'.
  3613. See also `org-completion-use-iswitchb'."
  3614. :group 'org-completion
  3615. :type 'boolean)
  3616. (defcustom org-completion-use-iswitchb nil
  3617. "Non-nil means use iswitchb completion wherever possible.
  3618. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3619. If you decide to turn this variable on, you might well want to turn off
  3620. `org-outline-path-complete-in-steps'.
  3621. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3622. :group 'org-completion
  3623. :type 'boolean)
  3624. (defcustom org-completion-fallback-command 'hippie-expand
  3625. "The expansion command called by \\[pcomplete] in normal context.
  3626. Normal means, no org-mode-specific context."
  3627. :group 'org-completion
  3628. :type 'function)
  3629. ;;; Functions and variables from their packages
  3630. ;; Declared here to avoid compiler warnings
  3631. ;; XEmacs only
  3632. (defvar outline-mode-menu-heading)
  3633. (defvar outline-mode-menu-show)
  3634. (defvar outline-mode-menu-hide)
  3635. (defvar zmacs-regions) ; XEmacs regions
  3636. ;; Emacs only
  3637. (defvar mark-active)
  3638. ;; Various packages
  3639. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3640. (declare-function calendar-forward-day "cal-move" (arg))
  3641. (declare-function calendar-goto-date "cal-move" (date))
  3642. (declare-function calendar-goto-today "cal-move" ())
  3643. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3644. (defvar calc-embedded-close-formula)
  3645. (defvar calc-embedded-open-formula)
  3646. (declare-function cdlatex-tab "ext:cdlatex" ())
  3647. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3648. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3649. (defvar font-lock-unfontify-region-function)
  3650. (declare-function iswitchb-read-buffer "iswitchb"
  3651. (prompt &optional default require-match start matches-set))
  3652. (defvar iswitchb-temp-buflist)
  3653. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3654. (defvar org-agenda-tags-todo-honor-ignore-options)
  3655. (declare-function org-agenda-skip "org-agenda" ())
  3656. (declare-function
  3657. org-agenda-format-item "org-agenda"
  3658. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3659. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3660. (declare-function org-agenda-change-all-lines "org-agenda"
  3661. (newhead hdmarker &optional fixface just-this))
  3662. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3663. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3664. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3665. (beg end))
  3666. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3667. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3668. "org-agenda" (&optional end))
  3669. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3670. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3671. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3672. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3673. (declare-function org-indent-mode "org-indent" (&optional arg))
  3674. (declare-function parse-time-string "parse-time" (string))
  3675. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3676. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3677. (defvar remember-data-file)
  3678. (defvar texmathp-why)
  3679. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3680. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3681. (defvar org-latex-regexps)
  3682. ;;; Autoload and prepare some org modules
  3683. ;; Some table stuff that needs to be defined here, because it is used
  3684. ;; by the functions setting up org-mode or checking for table context.
  3685. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3686. "Detect an org-type or table-type table.")
  3687. (defconst org-table-line-regexp "^[ \t]*|"
  3688. "Detect an org-type table line.")
  3689. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3690. "Detect an org-type table line.")
  3691. (defconst org-table-hline-regexp "^[ \t]*|-"
  3692. "Detect an org-type table hline.")
  3693. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3694. "Detect a table-type table hline.")
  3695. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3696. "Detect the first line outside a table when searching from within it.
  3697. This works for both table types.")
  3698. ;; Autoload the functions in org-table.el that are needed by functions here.
  3699. (eval-and-compile
  3700. (org-autoload "org-table"
  3701. '(org-table-begin org-table-blank-field org-table-end)))
  3702. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  3703. "Detect a #+TBLFM line.")
  3704. ;;;###autoload
  3705. (defun turn-on-orgtbl ()
  3706. "Unconditionally turn on `orgtbl-mode'."
  3707. (require 'org-table)
  3708. (orgtbl-mode 1))
  3709. (defun org-at-table-p (&optional table-type)
  3710. "Return t if the cursor is inside an org-type table.
  3711. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3712. (if org-enable-table-editor
  3713. (save-excursion
  3714. (beginning-of-line 1)
  3715. (looking-at (if table-type org-table-any-line-regexp
  3716. org-table-line-regexp)))
  3717. nil))
  3718. (defsubst org-table-p () (org-at-table-p))
  3719. (defun org-at-table.el-p ()
  3720. "Return t if and only if we are at a table.el table."
  3721. (and (org-at-table-p 'any)
  3722. (save-excursion
  3723. (goto-char (org-table-begin 'any))
  3724. (looking-at org-table1-hline-regexp))))
  3725. (defun org-table-recognize-table.el ()
  3726. "If there is a table.el table nearby, recognize it and move into it."
  3727. (if org-table-tab-recognizes-table.el
  3728. (if (org-at-table.el-p)
  3729. (progn
  3730. (beginning-of-line 1)
  3731. (if (looking-at org-table-dataline-regexp)
  3732. nil
  3733. (if (looking-at org-table1-hline-regexp)
  3734. (progn
  3735. (beginning-of-line 2)
  3736. (if (looking-at org-table-any-border-regexp)
  3737. (beginning-of-line -1)))))
  3738. (if (re-search-forward "|" (org-table-end t) t)
  3739. (progn
  3740. (require 'table)
  3741. (if (table--at-cell-p (point))
  3742. t
  3743. (message "recognizing table.el table...")
  3744. (table-recognize-table)
  3745. (message "recognizing table.el table...done")))
  3746. (error "This should not happen"))
  3747. t)
  3748. nil)
  3749. nil))
  3750. (defun org-at-table-hline-p ()
  3751. "Return t if the cursor is inside a hline in a table."
  3752. (if org-enable-table-editor
  3753. (save-excursion
  3754. (beginning-of-line 1)
  3755. (looking-at org-table-hline-regexp))
  3756. nil))
  3757. (defun org-table-map-tables (function &optional quietly)
  3758. "Apply FUNCTION to the start of all tables in the buffer."
  3759. (save-excursion
  3760. (save-restriction
  3761. (widen)
  3762. (goto-char (point-min))
  3763. (while (re-search-forward org-table-any-line-regexp nil t)
  3764. (unless quietly
  3765. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3766. (beginning-of-line 1)
  3767. (when (and (looking-at org-table-line-regexp)
  3768. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3769. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3770. (save-excursion (funcall function))
  3771. (or (looking-at org-table-line-regexp)
  3772. (forward-char 1)))
  3773. (re-search-forward org-table-any-border-regexp nil 1))))
  3774. (unless quietly (message "Mapping tables: done")))
  3775. ;; Declare and autoload functions from ox.el and al.
  3776. (declare-function org-export-get-environment "ox"
  3777. (&optional backend subtreep ext-plist))
  3778. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3779. ;; Declare and autoload functions from org-agenda.el
  3780. (eval-and-compile
  3781. (org-autoload "org-agenda"
  3782. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3783. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3784. (declare-function org-clock-update-mode-line "org-clock" ())
  3785. (declare-function org-resolve-clocks "org-clock"
  3786. (&optional also-non-dangling-p prompt last-valid))
  3787. (defun org-at-TBLFM-p (&optional pos)
  3788. "Return t when point (or POS) is in #+TBLFM line."
  3789. (save-excursion
  3790. (let ((pos pos)))
  3791. (goto-char (or pos (point)))
  3792. (beginning-of-line 1)
  3793. (looking-at org-TBLFM-regexp)))
  3794. (defvar org-clock-start-time)
  3795. (defvar org-clock-marker (make-marker)
  3796. "Marker recording the last clock-in.")
  3797. (defvar org-clock-hd-marker (make-marker)
  3798. "Marker recording the last clock-in, but the headline position.")
  3799. (defvar org-clock-heading ""
  3800. "The heading of the current clock entry.")
  3801. (defun org-clock-is-active ()
  3802. "Return non-nil if clock is currently running.
  3803. The return value is actually the clock marker."
  3804. (marker-buffer org-clock-marker))
  3805. (eval-and-compile
  3806. (org-autoload "org-clock" '(org-clock-remove-overlays
  3807. org-clock-update-time-maybe
  3808. org-clocktable-shift)))
  3809. (defun org-check-running-clock ()
  3810. "Check if the current buffer contains the running clock.
  3811. If yes, offer to stop it and to save the buffer with the changes."
  3812. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3813. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3814. (buffer-name))))
  3815. (org-clock-out)
  3816. (when (y-or-n-p "Save changed buffer?")
  3817. (save-buffer))))
  3818. (defun org-clocktable-try-shift (dir n)
  3819. "Check if this line starts a clock table, if yes, shift the time block."
  3820. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3821. (org-clocktable-shift dir n)))
  3822. ;;;###autoload
  3823. (defun org-clock-persistence-insinuate ()
  3824. "Set up hooks for clock persistence."
  3825. (require 'org-clock)
  3826. (add-hook 'org-mode-hook 'org-clock-load)
  3827. (add-hook 'kill-emacs-hook 'org-clock-save))
  3828. ;; Define the variable already here, to make sure we have it.
  3829. (defvar org-indent-mode nil
  3830. "Non-nil if Org-Indent mode is enabled.
  3831. Use the command `org-indent-mode' to change this variable.")
  3832. ;; Autoload archiving code
  3833. ;; The stuff that is needed for cycling and tags has to be defined here.
  3834. (defgroup org-archive nil
  3835. "Options concerning archiving in Org-mode."
  3836. :tag "Org Archive"
  3837. :group 'org-structure)
  3838. (defcustom org-archive-location "%s_archive::"
  3839. "The location where subtrees should be archived.
  3840. The value of this variable is a string, consisting of two parts,
  3841. separated by a double-colon. The first part is a filename and
  3842. the second part is a headline.
  3843. When the filename is omitted, archiving happens in the same file.
  3844. %s in the filename will be replaced by the current file
  3845. name (without the directory part). Archiving to a different file
  3846. is useful to keep archived entries from contributing to the
  3847. Org-mode Agenda.
  3848. The archived entries will be filed as subtrees of the specified
  3849. headline. When the headline is omitted, the subtrees are simply
  3850. filed away at the end of the file, as top-level entries. Also in
  3851. the heading you can use %s to represent the file name, this can be
  3852. useful when using the same archive for a number of different files.
  3853. Here are a few examples:
  3854. \"%s_archive::\"
  3855. If the current file is Projects.org, archive in file
  3856. Projects.org_archive, as top-level trees. This is the default.
  3857. \"::* Archived Tasks\"
  3858. Archive in the current file, under the top-level headline
  3859. \"* Archived Tasks\".
  3860. \"~/org/archive.org::\"
  3861. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3862. \"~/org/archive.org::* From %s\"
  3863. Archive in file ~/org/archive.org (absolute path), under headlines
  3864. \"From FILENAME\" where file name is the current file name.
  3865. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3866. The \"datetree/\" string is special, signifying to archive
  3867. items to the datetree. Items are placed in either the CLOSED
  3868. date of the item, or the current date if there is no CLOSED date.
  3869. The heading will be a subentry to the current date. There doesn't
  3870. need to be a heading, but there always needs to be a slash after
  3871. datetree. For example, to store archived items directly in the
  3872. datetree, use \"~/org/datetree.org::datetree/\".
  3873. \"basement::** Finished Tasks\"
  3874. Archive in file ./basement (relative path), as level 3 trees
  3875. below the level 2 heading \"** Finished Tasks\".
  3876. You may set this option on a per-file basis by adding to the buffer a
  3877. line like
  3878. #+ARCHIVE: basement::** Finished Tasks
  3879. You may also define it locally for a subtree by setting an ARCHIVE property
  3880. in the entry. If such a property is found in an entry, or anywhere up
  3881. the hierarchy, it will be used."
  3882. :group 'org-archive
  3883. :type 'string)
  3884. (defcustom org-archive-tag "ARCHIVE"
  3885. "The tag that marks a subtree as archived.
  3886. An archived subtree does not open during visibility cycling, and does
  3887. not contribute to the agenda listings.
  3888. After changing this, font-lock must be restarted in the relevant buffers to
  3889. get the proper fontification."
  3890. :group 'org-archive
  3891. :group 'org-keywords
  3892. :type 'string)
  3893. (defcustom org-agenda-skip-archived-trees t
  3894. "Non-nil means the agenda will skip any items located in archived trees.
  3895. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3896. variable is no longer recommended, you should leave it at the value t.
  3897. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3898. :group 'org-archive
  3899. :group 'org-agenda-skip
  3900. :type 'boolean)
  3901. (defcustom org-columns-skip-archived-trees t
  3902. "Non-nil means ignore archived trees when creating column view."
  3903. :group 'org-archive
  3904. :group 'org-properties
  3905. :type 'boolean)
  3906. (defcustom org-cycle-open-archived-trees nil
  3907. "Non-nil means `org-cycle' will open archived trees.
  3908. An archived tree is a tree marked with the tag ARCHIVE.
  3909. When nil, archived trees will stay folded. You can still open them with
  3910. normal outline commands like `show-all', but not with the cycling commands."
  3911. :group 'org-archive
  3912. :group 'org-cycle
  3913. :type 'boolean)
  3914. (defcustom org-sparse-tree-open-archived-trees nil
  3915. "Non-nil means sparse tree construction shows matches in archived trees.
  3916. When nil, matches in these trees are highlighted, but the trees are kept in
  3917. collapsed state."
  3918. :group 'org-archive
  3919. :group 'org-sparse-trees
  3920. :type 'boolean)
  3921. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3922. "The default date type when building a sparse tree.
  3923. When this is nil, a date is a scheduled or a deadline timestamp.
  3924. Otherwise, these types are allowed:
  3925. all: all timestamps
  3926. active: only active timestamps (<...>)
  3927. inactive: only inactive timestamps (<...)
  3928. scheduled: only scheduled timestamps
  3929. deadline: only deadline timestamps"
  3930. :type '(choice (const :tag "Scheduled or deadline" scheduled-or-deadline)
  3931. (const :tag "All timestamps" all)
  3932. (const :tag "Only active timestamps" active)
  3933. (const :tag "Only inactive timestamps" inactive)
  3934. (const :tag "Only scheduled timestamps" scheduled)
  3935. (const :tag "Only deadline timestamps" deadline)
  3936. (const :tag "Only closed timestamps" closed))
  3937. :version "24.3"
  3938. :group 'org-sparse-trees)
  3939. (defun org-cycle-hide-archived-subtrees (state)
  3940. "Re-hide all archived subtrees after a visibility state change."
  3941. (when (and (not org-cycle-open-archived-trees)
  3942. (not (memq state '(overview folded))))
  3943. (save-excursion
  3944. (let* ((globalp (memq state '(contents all)))
  3945. (beg (if globalp (point-min) (point)))
  3946. (end (if globalp (point-max) (org-end-of-subtree t))))
  3947. (org-hide-archived-subtrees beg end)
  3948. (goto-char beg)
  3949. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3950. (message "%s" (substitute-command-keys
  3951. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3952. (defun org-force-cycle-archived ()
  3953. "Cycle subtree even if it is archived."
  3954. (interactive)
  3955. (setq this-command 'org-cycle)
  3956. (let ((org-cycle-open-archived-trees t))
  3957. (call-interactively 'org-cycle)))
  3958. (defun org-hide-archived-subtrees (beg end)
  3959. "Re-hide all archived subtrees after a visibility state change."
  3960. (save-excursion
  3961. (let* ((re (concat ":" org-archive-tag ":")))
  3962. (goto-char beg)
  3963. (while (re-search-forward re end t)
  3964. (when (org-at-heading-p)
  3965. (org-flag-subtree t)
  3966. (org-end-of-subtree t))))))
  3967. (declare-function outline-end-of-heading "outline" ())
  3968. (declare-function outline-flag-region "outline" (from to flag))
  3969. (defun org-flag-subtree (flag)
  3970. (save-excursion
  3971. (org-back-to-heading t)
  3972. (outline-end-of-heading)
  3973. (outline-flag-region (point)
  3974. (progn (org-end-of-subtree t) (point))
  3975. flag)))
  3976. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3977. (eval-and-compile
  3978. (org-autoload "org-archive"
  3979. '(org-add-archive-files)))
  3980. ;; Autoload Column View Code
  3981. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3982. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3983. (declare-function org-columns-compute "org-colview" (property))
  3984. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3985. '(org-columns-number-to-string
  3986. org-columns-get-format-and-top-level
  3987. org-columns-compute
  3988. org-columns-remove-overlays))
  3989. ;; Autoload ID code
  3990. (declare-function org-id-store-link "org-id")
  3991. (declare-function org-id-locations-load "org-id")
  3992. (declare-function org-id-locations-save "org-id")
  3993. (defvar org-id-track-globally)
  3994. (org-autoload "org-id"
  3995. '(org-id-new
  3996. org-id-copy
  3997. org-id-get-with-outline-path-completion
  3998. org-id-get-with-outline-drilling))
  3999. ;;; Variables for pre-computed regular expressions, all buffer local
  4000. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  4001. "Matches first line of a hidden block.")
  4002. (make-variable-buffer-local 'org-drawer-regexp)
  4003. (defvar org-todo-regexp nil
  4004. "Matches any of the TODO state keywords.")
  4005. (make-variable-buffer-local 'org-todo-regexp)
  4006. (defvar org-not-done-regexp nil
  4007. "Matches any of the TODO state keywords except the last one.")
  4008. (make-variable-buffer-local 'org-not-done-regexp)
  4009. (defvar org-not-done-heading-regexp nil
  4010. "Matches a TODO headline that is not done.")
  4011. (make-variable-buffer-local 'org-not-done-regexp)
  4012. (defvar org-todo-line-regexp nil
  4013. "Matches a headline and puts TODO state into group 2 if present.")
  4014. (make-variable-buffer-local 'org-todo-line-regexp)
  4015. (defvar org-complex-heading-regexp nil
  4016. "Matches a headline and puts everything into groups:
  4017. group 1: the stars
  4018. group 2: The todo keyword, maybe
  4019. group 3: Priority cookie
  4020. group 4: True headline
  4021. group 5: Tags")
  4022. (make-variable-buffer-local 'org-complex-heading-regexp)
  4023. (defvar org-complex-heading-regexp-format nil
  4024. "Printf format to make regexp to match an exact headline.
  4025. This regexp will match the headline of any node which has the
  4026. exact headline text that is put into the format, but may have any
  4027. TODO state, priority and tags.")
  4028. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4029. (defvar org-todo-line-tags-regexp nil
  4030. "Matches a headline and puts TODO state into group 2 if present.
  4031. Also put tags into group 4 if tags are present.")
  4032. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4033. (defvar org-ds-keyword-length 12
  4034. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4035. (make-variable-buffer-local 'org-ds-keyword-length)
  4036. (defvar org-deadline-regexp nil
  4037. "Matches the DEADLINE keyword.")
  4038. (make-variable-buffer-local 'org-deadline-regexp)
  4039. (defvar org-deadline-time-regexp nil
  4040. "Matches the DEADLINE keyword together with a time stamp.")
  4041. (make-variable-buffer-local 'org-deadline-time-regexp)
  4042. (defvar org-deadline-time-hour-regexp nil
  4043. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4044. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4045. (defvar org-deadline-line-regexp nil
  4046. "Matches the DEADLINE keyword and the rest of the line.")
  4047. (make-variable-buffer-local 'org-deadline-line-regexp)
  4048. (defvar org-scheduled-regexp nil
  4049. "Matches the SCHEDULED keyword.")
  4050. (make-variable-buffer-local 'org-scheduled-regexp)
  4051. (defvar org-scheduled-time-regexp nil
  4052. "Matches the SCHEDULED keyword together with a time stamp.")
  4053. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4054. (defvar org-scheduled-time-hour-regexp nil
  4055. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4056. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4057. (defvar org-closed-time-regexp nil
  4058. "Matches the CLOSED keyword together with a time stamp.")
  4059. (make-variable-buffer-local 'org-closed-time-regexp)
  4060. (defvar org-keyword-time-regexp nil
  4061. "Matches any of the 4 keywords, together with the time stamp.")
  4062. (make-variable-buffer-local 'org-keyword-time-regexp)
  4063. (defvar org-keyword-time-not-clock-regexp nil
  4064. "Matches any of the 3 keywords, together with the time stamp.")
  4065. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4066. (defvar org-maybe-keyword-time-regexp nil
  4067. "Matches a timestamp, possibly preceded by a keyword.")
  4068. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4069. (defvar org-all-time-keywords nil
  4070. "List of time keywords.")
  4071. (make-variable-buffer-local 'org-all-time-keywords)
  4072. (defconst org-plain-time-of-day-regexp
  4073. (concat
  4074. "\\(\\<[012]?[0-9]"
  4075. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4076. "\\(--?"
  4077. "\\(\\<[012]?[0-9]"
  4078. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4079. "\\)?")
  4080. "Regular expression to match a plain time or time range.
  4081. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4082. groups carry important information:
  4083. 0 the full match
  4084. 1 the first time, range or not
  4085. 8 the second time, if it is a range.")
  4086. (defconst org-plain-time-extension-regexp
  4087. (concat
  4088. "\\(\\<[012]?[0-9]"
  4089. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4090. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4091. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4092. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4093. groups carry important information:
  4094. 0 the full match
  4095. 7 hours of duration
  4096. 9 minutes of duration")
  4097. (defconst org-stamp-time-of-day-regexp
  4098. (concat
  4099. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4100. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4101. "\\(--?"
  4102. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4103. "Regular expression to match a timestamp time or time range.
  4104. After a match, the following groups carry important information:
  4105. 0 the full match
  4106. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4107. 2 the first time, range or not
  4108. 4 the second time, if it is a range.")
  4109. (defconst org-startup-options
  4110. '(("fold" org-startup-folded t)
  4111. ("overview" org-startup-folded t)
  4112. ("nofold" org-startup-folded nil)
  4113. ("showall" org-startup-folded nil)
  4114. ("showeverything" org-startup-folded showeverything)
  4115. ("content" org-startup-folded content)
  4116. ("indent" org-startup-indented t)
  4117. ("noindent" org-startup-indented nil)
  4118. ("hidestars" org-hide-leading-stars t)
  4119. ("showstars" org-hide-leading-stars nil)
  4120. ("odd" org-odd-levels-only t)
  4121. ("oddeven" org-odd-levels-only nil)
  4122. ("align" org-startup-align-all-tables t)
  4123. ("noalign" org-startup-align-all-tables nil)
  4124. ("inlineimages" org-startup-with-inline-images t)
  4125. ("noinlineimages" org-startup-with-inline-images nil)
  4126. ("latexpreview" org-startup-with-latex-preview t)
  4127. ("nolatexpreview" org-startup-with-latex-preview nil)
  4128. ("customtime" org-display-custom-times t)
  4129. ("logdone" org-log-done time)
  4130. ("lognotedone" org-log-done note)
  4131. ("nologdone" org-log-done nil)
  4132. ("lognoteclock-out" org-log-note-clock-out t)
  4133. ("nolognoteclock-out" org-log-note-clock-out nil)
  4134. ("logrepeat" org-log-repeat state)
  4135. ("lognoterepeat" org-log-repeat note)
  4136. ("logdrawer" org-log-into-drawer t)
  4137. ("nologdrawer" org-log-into-drawer nil)
  4138. ("logstatesreversed" org-log-states-order-reversed t)
  4139. ("nologstatesreversed" org-log-states-order-reversed nil)
  4140. ("nologrepeat" org-log-repeat nil)
  4141. ("logreschedule" org-log-reschedule time)
  4142. ("lognotereschedule" org-log-reschedule note)
  4143. ("nologreschedule" org-log-reschedule nil)
  4144. ("logredeadline" org-log-redeadline time)
  4145. ("lognoteredeadline" org-log-redeadline note)
  4146. ("nologredeadline" org-log-redeadline nil)
  4147. ("logrefile" org-log-refile time)
  4148. ("lognoterefile" org-log-refile note)
  4149. ("nologrefile" org-log-refile nil)
  4150. ("fninline" org-footnote-define-inline t)
  4151. ("nofninline" org-footnote-define-inline nil)
  4152. ("fnlocal" org-footnote-section nil)
  4153. ("fnauto" org-footnote-auto-label t)
  4154. ("fnprompt" org-footnote-auto-label nil)
  4155. ("fnconfirm" org-footnote-auto-label confirm)
  4156. ("fnplain" org-footnote-auto-label plain)
  4157. ("fnadjust" org-footnote-auto-adjust t)
  4158. ("nofnadjust" org-footnote-auto-adjust nil)
  4159. ("constcgs" constants-unit-system cgs)
  4160. ("constSI" constants-unit-system SI)
  4161. ("noptag" org-tag-persistent-alist nil)
  4162. ("hideblocks" org-hide-block-startup t)
  4163. ("nohideblocks" org-hide-block-startup nil)
  4164. ("beamer" org-startup-with-beamer-mode t)
  4165. ("entitiespretty" org-pretty-entities t)
  4166. ("entitiesplain" org-pretty-entities nil))
  4167. "Variable associated with STARTUP options for org-mode.
  4168. Each element is a list of three items: the startup options (as written
  4169. in the #+STARTUP line), the corresponding variable, and the value to set
  4170. this variable to if the option is found. An optional forth element PUSH
  4171. means to push this value onto the list in the variable.")
  4172. (defun org-update-property-plist (key val props)
  4173. "Update PROPS with KEY and VAL."
  4174. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4175. (key (if appending (substring key 0 (- (length key) 1)) key))
  4176. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4177. (previous (cdr (assoc key props))))
  4178. (if appending
  4179. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4180. (cons (cons key val) remainder))))
  4181. (defconst org-block-regexp
  4182. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4183. "Regular expression for hiding blocks.")
  4184. (defconst org-heading-keyword-regexp-format
  4185. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4186. "Printf format for a regexp matching a headline with some keyword.
  4187. This regexp will match the headline of any node which has the
  4188. exact keyword that is put into the format. The keyword isn't in
  4189. any group by default, but the stars and the body are.")
  4190. (defconst org-heading-keyword-maybe-regexp-format
  4191. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4192. "Printf format for a regexp matching a headline, possibly with some keyword.
  4193. This regexp can match any headline with the specified keyword, or
  4194. without a keyword. The keyword isn't in any group by default,
  4195. but the stars and the body are.")
  4196. (defcustom org-group-tags t
  4197. "When non-nil (the default), use group tags.
  4198. This can be turned on/off through `org-toggle-tags-groups'."
  4199. :group 'org-tags
  4200. :group 'org-startup
  4201. :type 'boolean)
  4202. (defun org-toggle-tags-groups ()
  4203. "Toggle support for group tags.
  4204. Support for group tags is controlled by the option
  4205. `org-group-tags', which is non-nil by default."
  4206. (interactive)
  4207. (setq org-group-tags (not org-group-tags))
  4208. (cond ((and (derived-mode-p 'org-agenda-mode)
  4209. org-group-tags)
  4210. (org-agenda-redo))
  4211. ((derived-mode-p 'org-mode)
  4212. (let ((org-inhibit-startup t)) (org-mode))))
  4213. (message "Groups tags support has been turned %s"
  4214. (if org-group-tags "on" "off")))
  4215. (defun org-set-regexps-and-options-for-tags ()
  4216. "Precompute regular expressions used for tags in the current buffer."
  4217. (when (derived-mode-p 'org-mode)
  4218. (org-set-local 'org-file-tags nil)
  4219. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4220. (splitre "[ \t]+")
  4221. (start 0)
  4222. tags ftags key value)
  4223. (save-excursion
  4224. (save-restriction
  4225. (widen)
  4226. (goto-char (point-min))
  4227. (while (re-search-forward re nil t)
  4228. (setq key (upcase (org-match-string-no-properties 1))
  4229. value (org-match-string-no-properties 2))
  4230. (if (stringp value) (setq value (org-trim value)))
  4231. (cond
  4232. ((equal key "TAGS")
  4233. (setq tags (append tags (if tags '("\\n") nil)
  4234. (org-split-string value splitre))))
  4235. ((equal key "FILETAGS")
  4236. (when (string-match "\\S-" value)
  4237. (setq ftags
  4238. (append
  4239. ftags
  4240. (apply 'append
  4241. (mapcar (lambda (x) (org-split-string x ":"))
  4242. (org-split-string value)))))))))))
  4243. ;; Process the file tags.
  4244. (and ftags (org-set-local 'org-file-tags
  4245. (mapcar 'org-add-prop-inherited ftags)))
  4246. (org-set-local 'org-tag-groups-alist nil)
  4247. ;; Process the tags.
  4248. ;; FIXME
  4249. (when tags
  4250. (let (e tgs g)
  4251. (while (setq e (pop tags))
  4252. (cond
  4253. ((equal e "{")
  4254. (progn (push '(:startgroup) tgs)
  4255. (when (equal (nth 1 tags) ":")
  4256. (push (list (replace-regexp-in-string
  4257. "(.+)$" "" (nth 0 tags)))
  4258. org-tag-groups-alist)
  4259. (setq g 0))))
  4260. ((equal e ":") (push '(:grouptags) tgs))
  4261. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4262. ((equal e "\\n") (push '(:newline) tgs))
  4263. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4264. (push (cons (match-string 1 e)
  4265. (string-to-char (match-string 2 e))) tgs)
  4266. (if (and g (> g 0))
  4267. (setcar org-tag-groups-alist
  4268. (append (car org-tag-groups-alist)
  4269. (list (match-string 1 e)))))
  4270. (if g (setq g (1+ g))))
  4271. (t (push (list e) tgs)
  4272. (if (and g (> g 0))
  4273. (setcar org-tag-groups-alist
  4274. (append (car org-tag-groups-alist) (list e))))
  4275. (if g (setq g (1+ g))))))
  4276. (org-set-local 'org-tag-alist nil)
  4277. (while (setq e (pop tgs))
  4278. (or (and (stringp (car e))
  4279. (assoc (car e) org-tag-alist))
  4280. (push e org-tag-alist))))))))
  4281. (defun org-set-regexps-and-options ()
  4282. "Precompute regular expressions used in the current buffer."
  4283. (when (derived-mode-p 'org-mode)
  4284. (org-set-local 'org-todo-kwd-alist nil)
  4285. (org-set-local 'org-todo-key-alist nil)
  4286. (org-set-local 'org-todo-key-trigger nil)
  4287. (org-set-local 'org-todo-keywords-1 nil)
  4288. (org-set-local 'org-done-keywords nil)
  4289. (org-set-local 'org-todo-heads nil)
  4290. (org-set-local 'org-todo-sets nil)
  4291. (org-set-local 'org-todo-log-states nil)
  4292. (org-set-local 'org-file-properties nil)
  4293. (let ((re (org-make-options-regexp
  4294. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4295. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4296. "SETUPFILE" "OPTIONS")
  4297. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4298. (splitre "[ \t]+")
  4299. (scripts org-use-sub-superscripts)
  4300. kwds kws0 kwsa key log value cat arch const links hw dws
  4301. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4302. (start 0))
  4303. (save-excursion
  4304. (save-restriction
  4305. (widen)
  4306. (goto-char (point-min))
  4307. (while (or (and ext-setup-or-nil
  4308. (let (ret)
  4309. (with-temp-buffer
  4310. (insert ext-setup-or-nil)
  4311. (let ((major-mode 'org-mode))
  4312. (org-set-regexps-and-options-for-tags)
  4313. (setq ret (list org-file-tags org-tag-alist
  4314. org-tag-groups-alist))))
  4315. (setq org-file-tags (nth 0 ret)
  4316. org-tag-alist (nth 1 ret)
  4317. org-tag-groups-alist (nth 2 ret))))
  4318. (and ext-setup-or-nil
  4319. (string-match re ext-setup-or-nil start)
  4320. (setq start (match-end 0)))
  4321. (and (setq ext-setup-or-nil nil start 0)
  4322. (re-search-forward re nil t)))
  4323. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4324. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4325. (if (stringp value) (setq value (org-trim value)))
  4326. (cond
  4327. ((equal key "CATEGORY")
  4328. (setq cat value))
  4329. ((member key '("SEQ_TODO" "TODO"))
  4330. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4331. ((equal key "TYP_TODO")
  4332. (push (cons 'type (org-split-string value splitre)) kwds))
  4333. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4334. ;; general TODO-like setup
  4335. (push (cons (intern (downcase (match-string 1 key)))
  4336. (org-split-string value splitre)) kwds))
  4337. ((equal key "COLUMNS")
  4338. (org-set-local 'org-columns-default-format value))
  4339. ((equal key "LINK")
  4340. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4341. (push (cons (match-string 1 value)
  4342. (org-trim (match-string 2 value)))
  4343. links)))
  4344. ((equal key "PRIORITIES")
  4345. (setq prio (org-split-string value " +")))
  4346. ((equal key "PROPERTY")
  4347. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4348. (setq props (org-update-property-plist (match-string 1 value)
  4349. (match-string 2 value)
  4350. props))))
  4351. ((equal key "DRAWERS")
  4352. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4353. ((equal key "CONSTANTS")
  4354. (org-table-set-constants))
  4355. ((equal key "STARTUP")
  4356. (let ((opts (org-split-string value splitre))
  4357. l var val)
  4358. (while (setq l (pop opts))
  4359. (when (setq l (assoc l org-startup-options))
  4360. (setq var (nth 1 l) val (nth 2 l))
  4361. (if (not (nth 3 l))
  4362. (set (make-local-variable var) val)
  4363. (if (not (listp (symbol-value var)))
  4364. (set (make-local-variable var) nil))
  4365. (set (make-local-variable var) (symbol-value var))
  4366. (add-to-list var val))))))
  4367. ((equal key "ARCHIVE")
  4368. (setq arch value)
  4369. (remove-text-properties 0 (length arch)
  4370. '(face t fontified t) arch))
  4371. ((equal key "OPTIONS")
  4372. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4373. (setq scripts (read (match-string 2 value)))))
  4374. ((and (equal key "SETUPFILE")
  4375. ;; Prevent checking in Gnus messages
  4376. (not buffer-read-only))
  4377. (setq setup-contents (org-file-contents
  4378. (expand-file-name
  4379. (org-remove-double-quotes value))
  4380. 'noerror))
  4381. (if (not ext-setup-or-nil)
  4382. (setq ext-setup-or-nil setup-contents start 0)
  4383. (setq ext-setup-or-nil
  4384. (concat (substring ext-setup-or-nil 0 start)
  4385. "\n" setup-contents "\n"
  4386. (substring ext-setup-or-nil start)))))))
  4387. ;; search for property blocks
  4388. (goto-char (point-min))
  4389. (while (re-search-forward org-block-regexp nil t)
  4390. (when (equal "PROPERTY" (upcase (match-string 1)))
  4391. (setq value (replace-regexp-in-string
  4392. "[\n\r]" " " (match-string 4)))
  4393. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4394. (setq props (org-update-property-plist (match-string 1 value)
  4395. (match-string 2 value)
  4396. props)))))))
  4397. (org-set-local 'org-use-sub-superscripts scripts)
  4398. (when cat
  4399. (org-set-local 'org-category (intern cat))
  4400. (push (cons "CATEGORY" cat) props))
  4401. (when prio
  4402. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4403. (setq prio (mapcar 'string-to-char prio))
  4404. (org-set-local 'org-highest-priority (nth 0 prio))
  4405. (org-set-local 'org-lowest-priority (nth 1 prio))
  4406. (org-set-local 'org-default-priority (nth 2 prio)))
  4407. (and props (org-set-local 'org-file-properties (nreverse props)))
  4408. (and drawers (org-set-local 'org-drawers drawers))
  4409. (and arch (org-set-local 'org-archive-location arch))
  4410. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4411. ;; Process the TODO keywords
  4412. (unless kwds
  4413. ;; Use the global values as if they had been given locally.
  4414. (setq kwds (default-value 'org-todo-keywords))
  4415. (if (stringp (car kwds))
  4416. (setq kwds (list (cons org-todo-interpretation
  4417. (default-value 'org-todo-keywords)))))
  4418. (setq kwds (reverse kwds)))
  4419. (setq kwds (nreverse kwds))
  4420. (let (inter kws kw)
  4421. (while (setq kws (pop kwds))
  4422. (let ((kws (or
  4423. (run-hook-with-args-until-success
  4424. 'org-todo-setup-filter-hook kws)
  4425. kws)))
  4426. (setq inter (pop kws) sep (member "|" kws)
  4427. kws0 (delete "|" (copy-sequence kws))
  4428. kwsa nil
  4429. kws1 (mapcar
  4430. (lambda (x)
  4431. ;; 1 2
  4432. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4433. (progn
  4434. (setq kw (match-string 1 x)
  4435. key (and (match-end 2) (match-string 2 x))
  4436. log (org-extract-log-state-settings x))
  4437. (push (cons kw (and key (string-to-char key))) kwsa)
  4438. (and log (push log org-todo-log-states))
  4439. kw)
  4440. (error "Invalid TODO keyword %s" x)))
  4441. kws0)
  4442. kwsa (if kwsa (append '((:startgroup))
  4443. (nreverse kwsa)
  4444. '((:endgroup))))
  4445. hw (car kws1)
  4446. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4447. tail (list inter hw (car dws) (org-last dws))))
  4448. (add-to-list 'org-todo-heads hw 'append)
  4449. (push kws1 org-todo-sets)
  4450. (setq org-done-keywords (append org-done-keywords dws nil))
  4451. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4452. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4453. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4454. (setq org-todo-sets (nreverse org-todo-sets)
  4455. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4456. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4457. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4458. ;; Compute the regular expressions and other local variables.
  4459. ;; Using `org-outline-regexp-bol' would complicate them much,
  4460. ;; because of the fixed white space at the end of that string.
  4461. (if (not org-done-keywords)
  4462. (setq org-done-keywords (and org-todo-keywords-1
  4463. (list (org-last org-todo-keywords-1)))))
  4464. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4465. (length org-scheduled-string)
  4466. (length org-clock-string)
  4467. (length org-closed-string)))
  4468. org-drawer-regexp
  4469. (concat "^[ \t]*:\\("
  4470. (mapconcat 'regexp-quote org-drawers "\\|")
  4471. "\\):[ \t]*$")
  4472. org-not-done-keywords
  4473. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4474. org-todo-regexp
  4475. (concat "\\("
  4476. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4477. "\\)")
  4478. org-not-done-regexp
  4479. (concat "\\("
  4480. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4481. "\\)")
  4482. org-not-done-heading-regexp
  4483. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4484. org-todo-line-regexp
  4485. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4486. org-complex-heading-regexp
  4487. (concat "^\\(\\*+\\)"
  4488. "\\(?: +" org-todo-regexp "\\)?"
  4489. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4490. "\\(?: +\\(.*?\\)\\)??"
  4491. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4492. "[ \t]*$")
  4493. org-complex-heading-regexp-format
  4494. (concat "^\\(\\*+\\)"
  4495. "\\(?: +" org-todo-regexp "\\)?"
  4496. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4497. "\\(?: +"
  4498. ;; Stats cookies can be stuck to body.
  4499. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4500. "\\(%s\\)"
  4501. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4502. "\\)"
  4503. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4504. "[ \t]*$")
  4505. org-todo-line-tags-regexp
  4506. (concat "^\\(\\*+\\)"
  4507. "\\(?: +" org-todo-regexp "\\)?"
  4508. "\\(?: +\\(.*?\\)\\)??"
  4509. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4510. "[ \t]*$")
  4511. org-deadline-regexp (concat "\\<" org-deadline-string)
  4512. org-deadline-time-regexp
  4513. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4514. org-deadline-time-hour-regexp
  4515. (concat "\\<" org-deadline-string
  4516. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4517. org-deadline-line-regexp
  4518. (concat "\\<\\(" org-deadline-string "\\).*")
  4519. org-scheduled-regexp
  4520. (concat "\\<" org-scheduled-string)
  4521. org-scheduled-time-regexp
  4522. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4523. org-scheduled-time-hour-regexp
  4524. (concat "\\<" org-scheduled-string
  4525. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4526. org-closed-time-regexp
  4527. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4528. org-keyword-time-regexp
  4529. (concat "\\<\\(" org-scheduled-string
  4530. "\\|" org-deadline-string
  4531. "\\|" org-closed-string
  4532. "\\|" org-clock-string "\\)"
  4533. " *[[<]\\([^]>]+\\)[]>]")
  4534. org-keyword-time-not-clock-regexp
  4535. (concat "\\<\\(" org-scheduled-string
  4536. "\\|" org-deadline-string
  4537. "\\|" org-closed-string
  4538. "\\)"
  4539. " *[[<]\\([^]>]+\\)[]>]")
  4540. org-maybe-keyword-time-regexp
  4541. (concat "\\(\\<\\(" org-scheduled-string
  4542. "\\|" org-deadline-string
  4543. "\\|" org-closed-string
  4544. "\\|" org-clock-string "\\)\\)?"
  4545. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4546. org-all-time-keywords
  4547. (mapcar (lambda (w) (substring w 0 -1))
  4548. (list org-scheduled-string org-deadline-string
  4549. org-clock-string org-closed-string)))
  4550. (org-compute-latex-and-related-regexp)
  4551. (org-set-font-lock-defaults))))
  4552. (defun org-file-contents (file &optional noerror)
  4553. "Return the contents of FILE, as a string."
  4554. (if (or (not file)
  4555. (not (file-readable-p file)))
  4556. (if noerror
  4557. (message "Cannot read file \"%s\"" file)
  4558. (error "Cannot read file \"%s\"" file))
  4559. (with-temp-buffer
  4560. (insert-file-contents file)
  4561. (buffer-string))))
  4562. (defun org-extract-log-state-settings (x)
  4563. "Extract the log state setting from a TODO keyword string.
  4564. This will extract info from a string like \"WAIT(w@/!)\"."
  4565. (let (kw key log1 log2)
  4566. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4567. (setq kw (match-string 1 x)
  4568. key (and (match-end 2) (match-string 2 x))
  4569. log1 (and (match-end 3) (match-string 3 x))
  4570. log2 (and (match-end 4) (match-string 4 x)))
  4571. (and (or log1 log2)
  4572. (list kw
  4573. (and log1 (if (equal log1 "!") 'time 'note))
  4574. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4575. (defun org-remove-keyword-keys (list)
  4576. "Remove a pair of parenthesis at the end of each string in LIST."
  4577. (mapcar (lambda (x)
  4578. (if (string-match "(.*)$" x)
  4579. (substring x 0 (match-beginning 0))
  4580. x))
  4581. list))
  4582. (defun org-assign-fast-keys (alist)
  4583. "Assign fast keys to a keyword-key alist.
  4584. Respect keys that are already there."
  4585. (let (new e (alt ?0))
  4586. (while (setq e (pop alist))
  4587. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4588. (cdr e)) ;; Key already assigned.
  4589. (push e new)
  4590. (let ((clist (string-to-list (downcase (car e))))
  4591. (used (append new alist)))
  4592. (when (= (car clist) ?@)
  4593. (pop clist))
  4594. (while (and clist (rassoc (car clist) used))
  4595. (pop clist))
  4596. (unless clist
  4597. (while (rassoc alt used)
  4598. (incf alt)))
  4599. (push (cons (car e) (or (car clist) alt)) new))))
  4600. (nreverse new)))
  4601. ;;; Some variables used in various places
  4602. (defvar org-window-configuration nil
  4603. "Used in various places to store a window configuration.")
  4604. (defvar org-selected-window nil
  4605. "Used in various places to store a window configuration.")
  4606. (defvar org-finish-function nil
  4607. "Function to be called when `C-c C-c' is used.
  4608. This is for getting out of special buffers like capture.")
  4609. ;; FIXME: Occasionally check by commenting these, to make sure
  4610. ;; no other functions uses these, forgetting to let-bind them.
  4611. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4612. (defvar org-last-state)
  4613. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4614. ;; Defined somewhere in this file, but used before definition.
  4615. (defvar org-entities) ;; defined in org-entities.el
  4616. (defvar org-struct-menu)
  4617. (defvar org-org-menu)
  4618. (defvar org-tbl-menu)
  4619. ;;;; Define the Org-mode
  4620. ;; We use a before-change function to check if a table might need
  4621. ;; an update.
  4622. (defvar org-table-may-need-update t
  4623. "Indicates that a table might need an update.
  4624. This variable is set by `org-before-change-function'.
  4625. `org-table-align' sets it back to nil.")
  4626. (defun org-before-change-function (beg end)
  4627. "Every change indicates that a table might need an update."
  4628. (setq org-table-may-need-update t))
  4629. (defvar org-mode-map)
  4630. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4631. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4632. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4633. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4634. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4635. (defvar org-table-buffer-is-an nil)
  4636. (defvar bidi-paragraph-direction)
  4637. (defvar buffer-face-mode-face)
  4638. (require 'outline)
  4639. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4640. (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"))
  4641. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4642. ;; Other stuff we need.
  4643. (require 'time-date)
  4644. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4645. (require 'easymenu)
  4646. (require 'overlay)
  4647. ;; (require 'org-macs) moved higher up in the file before it is first used
  4648. (require 'org-entities)
  4649. ;; (require 'org-compat) moved higher up in the file before it is first used
  4650. (require 'org-faces)
  4651. (require 'org-list)
  4652. (require 'org-pcomplete)
  4653. (require 'org-src)
  4654. (require 'org-footnote)
  4655. (require 'org-macro)
  4656. ;; babel
  4657. (require 'ob)
  4658. ;;;###autoload
  4659. (define-derived-mode org-mode outline-mode "Org"
  4660. "Outline-based notes management and organizer, alias
  4661. \"Carsten's outline-mode for keeping track of everything.\"
  4662. Org-mode develops organizational tasks around a NOTES file which
  4663. contains information about projects as plain text. Org-mode is
  4664. implemented on top of outline-mode, which is ideal to keep the content
  4665. of large files well structured. It supports ToDo items, deadlines and
  4666. time stamps, which magically appear in the diary listing of the Emacs
  4667. calendar. Tables are easily created with a built-in table editor.
  4668. Plain text URL-like links connect to websites, emails (VM), Usenet
  4669. messages (Gnus), BBDB entries, and any files related to the project.
  4670. For printing and sharing of notes, an Org-mode file (or a part of it)
  4671. can be exported as a structured ASCII or HTML file.
  4672. The following commands are available:
  4673. \\{org-mode-map}"
  4674. ;; Get rid of Outline menus, they are not needed
  4675. ;; Need to do this here because define-derived-mode sets up
  4676. ;; the keymap so late. Still, it is a waste to call this each time
  4677. ;; we switch another buffer into org-mode.
  4678. (if (featurep 'xemacs)
  4679. (when (boundp 'outline-mode-menu-heading)
  4680. ;; Assume this is Greg's port, it uses easymenu
  4681. (easy-menu-remove outline-mode-menu-heading)
  4682. (easy-menu-remove outline-mode-menu-show)
  4683. (easy-menu-remove outline-mode-menu-hide))
  4684. (define-key org-mode-map [menu-bar headings] 'undefined)
  4685. (define-key org-mode-map [menu-bar hide] 'undefined)
  4686. (define-key org-mode-map [menu-bar show] 'undefined))
  4687. (org-load-modules-maybe)
  4688. (easy-menu-add org-org-menu)
  4689. (easy-menu-add org-tbl-menu)
  4690. (org-install-agenda-files-menu)
  4691. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4692. (add-to-invisibility-spec '(org-cwidth))
  4693. (add-to-invisibility-spec '(org-hide-block . t))
  4694. (when (featurep 'xemacs)
  4695. (org-set-local 'line-move-ignore-invisible t))
  4696. (org-set-local 'outline-regexp org-outline-regexp)
  4697. (org-set-local 'outline-level 'org-outline-level)
  4698. (setq bidi-paragraph-direction 'left-to-right)
  4699. ;; FIXME Circumvent a bug in outline.el (Emacs <24.4)
  4700. (set (make-local-variable 'paragraph-start) " \\|[ \t]*$\\|\\*+ ")
  4701. (when (and org-ellipsis
  4702. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4703. (fboundp 'make-glyph-code))
  4704. (unless org-display-table
  4705. (setq org-display-table (make-display-table)))
  4706. (set-display-table-slot
  4707. org-display-table 4
  4708. (vconcat (mapcar
  4709. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4710. org-ellipsis)))
  4711. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4712. (setq buffer-display-table org-display-table))
  4713. (org-set-regexps-and-options-for-tags)
  4714. (org-set-regexps-and-options)
  4715. (when (and org-tag-faces (not org-tags-special-faces-re))
  4716. ;; tag faces set outside customize.... force initialization.
  4717. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4718. ;; Calc embedded
  4719. (org-set-local 'calc-embedded-open-mode "# ")
  4720. ;; Modify a few syntax entries
  4721. (modify-syntax-entry ?@ "w")
  4722. (modify-syntax-entry ?\" "\"")
  4723. (if org-startup-truncated (setq truncate-lines t))
  4724. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4725. (org-set-local 'font-lock-unfontify-region-function
  4726. 'org-unfontify-region)
  4727. ;; Activate before-change-function
  4728. (org-set-local 'org-table-may-need-update t)
  4729. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4730. 'local)
  4731. ;; Check for running clock before killing a buffer
  4732. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4733. ;; Initialize macros templates.
  4734. (org-macro-initialize-templates)
  4735. ;; Initialize radio targets.
  4736. (org-update-radio-target-regexp)
  4737. ;; Indentation.
  4738. (org-set-local 'indent-line-function 'org-indent-line)
  4739. (org-set-local 'indent-region-function 'org-indent-region)
  4740. ;; Filling and auto-filling.
  4741. (org-setup-filling)
  4742. ;; Comments.
  4743. (org-setup-comments-handling)
  4744. ;; Beginning/end of defun
  4745. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4746. (org-set-local 'end-of-defun-function 'org-forward-element)
  4747. ;; Next error for sparse trees
  4748. (org-set-local 'next-error-function 'org-occur-next-match)
  4749. ;; Make sure dependence stuff works reliably, even for users who set it
  4750. ;; too late :-(
  4751. (if org-enforce-todo-dependencies
  4752. (add-hook 'org-blocker-hook
  4753. 'org-block-todo-from-children-or-siblings-or-parent)
  4754. (remove-hook 'org-blocker-hook
  4755. 'org-block-todo-from-children-or-siblings-or-parent))
  4756. (if org-enforce-todo-checkbox-dependencies
  4757. (add-hook 'org-blocker-hook
  4758. 'org-block-todo-from-checkboxes)
  4759. (remove-hook 'org-blocker-hook
  4760. 'org-block-todo-from-checkboxes))
  4761. ;; Align options lines
  4762. (org-set-local
  4763. 'align-mode-rules-list
  4764. '((org-in-buffer-settings
  4765. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4766. (modes . '(org-mode)))))
  4767. ;; Imenu
  4768. (org-set-local 'imenu-create-index-function
  4769. 'org-imenu-get-tree)
  4770. ;; Make isearch reveal context
  4771. (if (or (featurep 'xemacs)
  4772. (not (boundp 'outline-isearch-open-invisible-function)))
  4773. ;; Emacs 21 and XEmacs make use of the hook
  4774. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4775. ;; Emacs 22 deals with this through a special variable
  4776. (org-set-local 'outline-isearch-open-invisible-function
  4777. (lambda (&rest ignore) (org-show-context 'isearch)))
  4778. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4779. ;; Setup the pcomplete hooks
  4780. (set (make-local-variable 'pcomplete-command-completion-function)
  4781. 'org-pcomplete-initial)
  4782. (set (make-local-variable 'pcomplete-command-name-function)
  4783. 'org-command-at-point)
  4784. (set (make-local-variable 'pcomplete-default-completion-function)
  4785. 'ignore)
  4786. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4787. 'org-parse-arguments)
  4788. (set (make-local-variable 'pcomplete-termination-string) "")
  4789. (when (>= emacs-major-version 23)
  4790. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4791. ;; If empty file that did not turn on org-mode automatically, make it to.
  4792. (if (and org-insert-mode-line-in-empty-file
  4793. (org-called-interactively-p 'any)
  4794. (= (point-min) (point-max)))
  4795. (insert "# -*- mode: org -*-\n\n"))
  4796. (unless org-inhibit-startup
  4797. (org-unmodified
  4798. (and org-startup-with-beamer-mode (org-beamer-mode))
  4799. (when org-startup-align-all-tables
  4800. (org-table-map-tables 'org-table-align 'quietly))
  4801. (when org-startup-with-inline-images
  4802. (org-display-inline-images))
  4803. (when org-startup-with-latex-preview
  4804. (org-preview-latex-fragment))
  4805. (unless org-inhibit-startup-visibility-stuff
  4806. (org-set-startup-visibility))))
  4807. ;; Try to set org-hide correctly
  4808. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4809. ;; Update `customize-package-emacs-version-alist'
  4810. (add-to-list 'customize-package-emacs-version-alist
  4811. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4812. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4813. ("8.0" . "24.4")))
  4814. (defvar org-mode-transpose-word-syntax-table
  4815. (let ((st (make-syntax-table)))
  4816. (mapc (lambda(c) (modify-syntax-entry
  4817. (string-to-char (car c)) "w p" st))
  4818. org-emphasis-alist)
  4819. st))
  4820. (when (fboundp 'abbrev-table-put)
  4821. (abbrev-table-put org-mode-abbrev-table
  4822. :parents (list text-mode-abbrev-table)))
  4823. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4824. (defsubst org-fix-ellipsis-at-bol ()
  4825. (save-excursion (goto-char (window-start)) (recenter 0)))
  4826. (defun org-find-invisible-foreground ()
  4827. (let ((candidates (remove
  4828. "unspecified-bg"
  4829. (nconc
  4830. (list (face-background 'default)
  4831. (face-background 'org-default))
  4832. (mapcar
  4833. (lambda (alist)
  4834. (when (boundp alist)
  4835. (cdr (assoc 'background-color (symbol-value alist)))))
  4836. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4837. (list (face-foreground 'org-hide))))))
  4838. (car (remove nil candidates))))
  4839. (defun org-current-time (&optional rounding-minutes past)
  4840. "Current time, possibly rounded to ROUNDING-MINUTES.
  4841. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4842. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4843. the rounding returns a past time."
  4844. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4845. (car org-time-stamp-rounding-minutes)))
  4846. (time (decode-time)) res)
  4847. (if (< r 1)
  4848. (current-time)
  4849. (setq res
  4850. (apply 'encode-time
  4851. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4852. (nthcdr 2 time))))
  4853. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4854. (seconds-to-time (- (org-float-time res) (* r 60)))
  4855. res))))
  4856. (defun org-today ()
  4857. "Return today date, considering `org-extend-today-until'."
  4858. (time-to-days
  4859. (time-subtract (current-time)
  4860. (list 0 (* 3600 org-extend-today-until) 0))))
  4861. ;;;; Font-Lock stuff, including the activators
  4862. (defvar org-mouse-map (make-sparse-keymap))
  4863. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4864. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4865. (when org-mouse-1-follows-link
  4866. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4867. (when org-tab-follows-link
  4868. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4869. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4870. (require 'font-lock)
  4871. (defconst org-non-link-chars "]\t\n\r<>")
  4872. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4873. "shell" "elisp" "doi" "message"))
  4874. (defvar org-link-types-re nil
  4875. "Matches a link that has a url-like prefix like \"http:\"")
  4876. (defvar org-link-re-with-space nil
  4877. "Matches a link with spaces, optional angular brackets around it.")
  4878. (defvar org-link-re-with-space2 nil
  4879. "Matches a link with spaces, optional angular brackets around it.")
  4880. (defvar org-link-re-with-space3 nil
  4881. "Matches a link with spaces, only for internal part in bracket links.")
  4882. (defvar org-angle-link-re nil
  4883. "Matches link with angular brackets, spaces are allowed.")
  4884. (defvar org-plain-link-re nil
  4885. "Matches plain link, without spaces.")
  4886. (defvar org-bracket-link-regexp nil
  4887. "Matches a link in double brackets.")
  4888. (defvar org-bracket-link-analytic-regexp nil
  4889. "Regular expression used to analyze links.
  4890. Here is what the match groups contain after a match:
  4891. 1: http:
  4892. 2: http
  4893. 3: path
  4894. 4: [desc]
  4895. 5: desc")
  4896. (defvar org-bracket-link-analytic-regexp++ nil
  4897. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4898. (defvar org-any-link-re nil
  4899. "Regular expression matching any link.")
  4900. (defconst org-match-sexp-depth 3
  4901. "Number of stacked braces for sub/superscript matching.")
  4902. (defun org-create-multibrace-regexp (left right n)
  4903. "Create a regular expression which will match a balanced sexp.
  4904. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4905. as single character strings.
  4906. The regexp returned will match the entire expression including the
  4907. delimiters. It will also define a single group which contains the
  4908. match except for the outermost delimiters. The maximum depth of
  4909. stacked delimiters is N. Escaping delimiters is not possible."
  4910. (let* ((nothing (concat "[^" left right "]*?"))
  4911. (or "\\|")
  4912. (re nothing)
  4913. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4914. (while (> n 1)
  4915. (setq n (1- n)
  4916. re (concat re or next)
  4917. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4918. (concat left "\\(" re "\\)" right)))
  4919. (defvar org-match-substring-regexp
  4920. (concat
  4921. "\\(\\S-\\)\\([_^]\\)\\("
  4922. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4923. "\\|"
  4924. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4925. "\\|"
  4926. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4927. "The regular expression matching a sub- or superscript.")
  4928. (defvar org-match-substring-with-braces-regexp
  4929. (concat
  4930. "\\(\\S-\\)\\([_^]\\)\\("
  4931. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4932. "\\)")
  4933. "The regular expression matching a sub- or superscript, forcing braces.")
  4934. (defun org-make-link-regexps ()
  4935. "Update the link regular expressions.
  4936. This should be called after the variable `org-link-types' has changed."
  4937. (setq org-link-types-re
  4938. (concat
  4939. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4940. org-link-re-with-space
  4941. (concat
  4942. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4943. "\\([^" org-non-link-chars " ]"
  4944. "[^" org-non-link-chars "]*"
  4945. "[^" org-non-link-chars " ]\\)>?")
  4946. org-link-re-with-space2
  4947. (concat
  4948. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4949. "\\([^" org-non-link-chars " ]"
  4950. "[^\t\n\r]*"
  4951. "[^" org-non-link-chars " ]\\)>?")
  4952. org-link-re-with-space3
  4953. (concat
  4954. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4955. "\\([^" org-non-link-chars " ]"
  4956. "[^\t\n\r]*\\)")
  4957. org-angle-link-re
  4958. (concat
  4959. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4960. "\\([^" org-non-link-chars " ]"
  4961. "[^" org-non-link-chars "]*"
  4962. "\\)>")
  4963. org-plain-link-re
  4964. (concat
  4965. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4966. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4967. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4968. org-bracket-link-regexp
  4969. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4970. org-bracket-link-analytic-regexp
  4971. (concat
  4972. "\\[\\["
  4973. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4974. "\\([^]]+\\)"
  4975. "\\]"
  4976. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4977. "\\]")
  4978. org-bracket-link-analytic-regexp++
  4979. (concat
  4980. "\\[\\["
  4981. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4982. "\\([^]]+\\)"
  4983. "\\]"
  4984. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4985. "\\]")
  4986. org-any-link-re
  4987. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4988. org-angle-link-re "\\)\\|\\("
  4989. org-plain-link-re "\\)")))
  4990. (org-make-link-regexps)
  4991. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4992. "Regular expression for fast time stamp matching.")
  4993. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4994. "Regular expression for fast time stamp matching.")
  4995. (defconst org-ts-regexp0
  4996. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4997. "Regular expression matching time strings for analysis.
  4998. This one does not require the space after the date, so it can be used
  4999. on a string that terminates immediately after the date.")
  5000. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5001. "Regular expression matching time strings for analysis.")
  5002. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  5003. "Regular expression matching time stamps, with groups.")
  5004. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  5005. "Regular expression matching time stamps (also [..]), with groups.")
  5006. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  5007. "Regular expression matching a time stamp range.")
  5008. (defconst org-tr-regexp-both
  5009. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  5010. "Regular expression matching a time stamp range.")
  5011. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  5012. org-ts-regexp "\\)?")
  5013. "Regular expression matching a time stamp or time stamp range.")
  5014. (defconst org-tsr-regexp-both
  5015. (concat org-ts-regexp-both "\\(--?-?"
  5016. org-ts-regexp-both "\\)?")
  5017. "Regular expression matching a time stamp or time stamp range.
  5018. The time stamps may be either active or inactive.")
  5019. (defvar org-emph-face nil)
  5020. (defun org-do-emphasis-faces (limit)
  5021. "Run through the buffer and add overlays to emphasized strings."
  5022. (let (rtn a)
  5023. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5024. (if (not (= (char-after (match-beginning 3))
  5025. (char-after (match-beginning 4))))
  5026. (progn
  5027. (setq rtn t)
  5028. (setq a (assoc (match-string 3) org-emphasis-alist))
  5029. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5030. 'face
  5031. (nth 1 a))
  5032. (and (nth 4 a)
  5033. (org-remove-flyspell-overlays-in
  5034. (match-beginning 0) (match-end 0)))
  5035. (add-text-properties (match-beginning 2) (match-end 2)
  5036. '(font-lock-multiline t org-emphasis t))
  5037. (when org-hide-emphasis-markers
  5038. (add-text-properties (match-end 4) (match-beginning 5)
  5039. '(invisible org-link))
  5040. (add-text-properties (match-beginning 3) (match-end 3)
  5041. '(invisible org-link)))))
  5042. (backward-char 1))
  5043. rtn))
  5044. (defun org-emphasize (&optional char)
  5045. "Insert or change an emphasis, i.e. a font like bold or italic.
  5046. If there is an active region, change that region to a new emphasis.
  5047. If there is no region, just insert the marker characters and position
  5048. the cursor between them.
  5049. CHAR should be the marker character. If it is a space, it means to
  5050. remove the emphasis of the selected region.
  5051. If CHAR is not given (for example in an interactive call) it will be
  5052. prompted for."
  5053. (interactive)
  5054. (let ((erc org-emphasis-regexp-components)
  5055. (prompt "")
  5056. (string "") beg end move c s)
  5057. (if (org-region-active-p)
  5058. (setq beg (region-beginning) end (region-end)
  5059. string (buffer-substring beg end))
  5060. (setq move t))
  5061. (unless char
  5062. (message "Emphasis marker or tag: [%s]"
  5063. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5064. (setq char (read-char-exclusive)))
  5065. (if (equal char ?\ )
  5066. (setq s "" move nil)
  5067. (unless (assoc (char-to-string char) org-emphasis-alist)
  5068. (user-error "No such emphasis marker: \"%c\"" char))
  5069. (setq s (char-to-string char)))
  5070. (while (and (> (length string) 1)
  5071. (equal (substring string 0 1) (substring string -1))
  5072. (assoc (substring string 0 1) org-emphasis-alist))
  5073. (setq string (substring string 1 -1)))
  5074. (setq string (concat s string s))
  5075. (if beg (delete-region beg end))
  5076. (unless (or (bolp)
  5077. (string-match (concat "[" (nth 0 erc) "\n]")
  5078. (char-to-string (char-before (point)))))
  5079. (insert " "))
  5080. (unless (or (eobp)
  5081. (string-match (concat "[" (nth 1 erc) "\n]")
  5082. (char-to-string (char-after (point)))))
  5083. (insert " ") (backward-char 1))
  5084. (insert string)
  5085. (and move (backward-char 1))))
  5086. (defconst org-nonsticky-props
  5087. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5088. (defsubst org-rear-nonsticky-at (pos)
  5089. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5090. (defun org-activate-plain-links (limit)
  5091. "Run through the buffer and add overlays to links."
  5092. (let (f hl)
  5093. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5094. (not (org-in-src-block-p)))
  5095. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5096. (setq f (get-text-property (match-beginning 0) 'face))
  5097. (setq hl (org-match-string-no-properties 0))
  5098. (if (or (eq f 'org-tag)
  5099. (and (listp f) (memq 'org-tag f)))
  5100. nil
  5101. (add-text-properties (match-beginning 0) (match-end 0)
  5102. (list 'mouse-face 'highlight
  5103. 'face 'org-link
  5104. 'htmlize-link `(:uri ,hl)
  5105. 'keymap org-mouse-map))
  5106. (org-rear-nonsticky-at (match-end 0)))
  5107. t)))
  5108. (defun org-activate-code (limit)
  5109. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5110. (progn
  5111. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5112. (remove-text-properties (match-beginning 0) (match-end 0)
  5113. '(display t invisible t intangible t))
  5114. t)))
  5115. (defcustom org-src-fontify-natively nil
  5116. "When non-nil, fontify code in code blocks."
  5117. :type 'boolean
  5118. :version "24.1"
  5119. :group 'org-appearance
  5120. :group 'org-babel)
  5121. (defcustom org-allow-promoting-top-level-subtree nil
  5122. "When non-nil, allow promoting a top level subtree.
  5123. The leading star of the top level headline will be replaced
  5124. by a #."
  5125. :type 'boolean
  5126. :version "24.1"
  5127. :group 'org-appearance)
  5128. (defun org-fontify-meta-lines-and-blocks (limit)
  5129. (condition-case nil
  5130. (org-fontify-meta-lines-and-blocks-1 limit)
  5131. (error (message "org-mode fontification error"))))
  5132. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5133. "Fontify #+ lines and blocks."
  5134. (let ((case-fold-search t))
  5135. (if (re-search-forward
  5136. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5137. limit t)
  5138. (let ((beg (match-beginning 0))
  5139. (block-start (match-end 0))
  5140. (block-end nil)
  5141. (lang (match-string 7))
  5142. (beg1 (line-beginning-position 2))
  5143. (dc1 (downcase (match-string 2)))
  5144. (dc3 (downcase (match-string 3)))
  5145. end end1 quoting block-type ovl)
  5146. (cond
  5147. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5148. ;; a single line of backend-specific content
  5149. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5150. (remove-text-properties (match-beginning 0) (match-end 0)
  5151. '(display t invisible t intangible t))
  5152. (add-text-properties (match-beginning 1) (match-end 3)
  5153. '(font-lock-fontified t face org-meta-line))
  5154. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5155. '(font-lock-fontified t face org-block))
  5156. ; for backend-specific code
  5157. t)
  5158. ((and (match-end 4) (equal dc3 "+begin"))
  5159. ;; Truly a block
  5160. (setq block-type (downcase (match-string 5))
  5161. quoting (member block-type org-protecting-blocks))
  5162. (when (re-search-forward
  5163. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5164. nil t) ;; on purpose, we look further than LIMIT
  5165. (setq end (min (point-max) (match-end 0))
  5166. end1 (min (point-max) (1- (match-beginning 0))))
  5167. (setq block-end (match-beginning 0))
  5168. (when quoting
  5169. (remove-text-properties beg end
  5170. '(display t invisible t intangible t)))
  5171. (add-text-properties
  5172. beg end
  5173. '(font-lock-fontified t font-lock-multiline t))
  5174. (add-text-properties beg beg1 '(face org-meta-line))
  5175. (add-text-properties end1 (min (point-max) (1+ end))
  5176. '(face org-meta-line)) ; for end_src
  5177. (cond
  5178. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5179. (org-src-font-lock-fontify-block lang block-start block-end)
  5180. ;; remove old background overlays
  5181. (mapc (lambda (ov)
  5182. (if (eq (overlay-get ov 'face) 'org-block-background)
  5183. (delete-overlay ov)))
  5184. (overlays-at (/ (+ beg1 block-end) 2)))
  5185. ;; add a background overlay
  5186. (setq ovl (make-overlay beg1 block-end))
  5187. (overlay-put ovl 'face 'org-block-background)
  5188. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5189. (quoting
  5190. (add-text-properties beg1 (min (point-max) (1+ end1))
  5191. '(face org-block))) ; end of source block
  5192. ((not org-fontify-quote-and-verse-blocks))
  5193. ((string= block-type "quote")
  5194. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5195. ((string= block-type "verse")
  5196. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5197. (add-text-properties beg beg1 '(face org-block-begin-line))
  5198. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5199. '(face org-block-end-line))
  5200. t))
  5201. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5202. (add-text-properties
  5203. beg (match-end 3)
  5204. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5205. '(font-lock-fontified t invisible t)
  5206. '(font-lock-fontified t face org-document-info-keyword)))
  5207. (add-text-properties
  5208. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5209. (if (string-equal dc1 "+title:")
  5210. '(font-lock-fontified t face org-document-title)
  5211. '(font-lock-fontified t face org-document-info))))
  5212. ((or (equal dc1 "+results")
  5213. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5214. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5215. "+call:" "+header:" "+headers:" "+name:"))
  5216. (and (match-end 4) (equal dc3 "+attr")))
  5217. (add-text-properties
  5218. beg (match-end 0)
  5219. '(font-lock-fontified t face org-meta-line))
  5220. t)
  5221. ((member dc3 '(" " ""))
  5222. (add-text-properties
  5223. beg (match-end 0)
  5224. '(font-lock-fontified t face font-lock-comment-face)))
  5225. ((not (member (char-after beg) '(?\ ?\t)))
  5226. ;; just any other in-buffer setting, but not indented
  5227. (add-text-properties
  5228. beg (match-end 0)
  5229. '(font-lock-fontified t face org-meta-line))
  5230. t)
  5231. (t nil))))))
  5232. (defun org-activate-angle-links (limit)
  5233. "Run through the buffer and add overlays to links."
  5234. (if (and (re-search-forward org-angle-link-re limit t)
  5235. (not (org-in-src-block-p)))
  5236. (progn
  5237. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5238. (add-text-properties (match-beginning 0) (match-end 0)
  5239. (list 'mouse-face 'highlight
  5240. 'keymap org-mouse-map))
  5241. (org-rear-nonsticky-at (match-end 0))
  5242. t)))
  5243. (defun org-activate-footnote-links (limit)
  5244. "Run through the buffer and add overlays to footnotes."
  5245. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5246. (when fn
  5247. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5248. (org-remove-flyspell-overlays-in beg end)
  5249. (add-text-properties beg end
  5250. (list 'mouse-face 'highlight
  5251. 'keymap org-mouse-map
  5252. 'help-echo
  5253. (if (= (point-at-bol) beg)
  5254. "Footnote definition"
  5255. "Footnote reference")
  5256. 'font-lock-fontified t
  5257. 'font-lock-multiline t
  5258. 'face 'org-footnote))))))
  5259. (defun org-activate-bracket-links (limit)
  5260. "Run through the buffer and add overlays to bracketed links."
  5261. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5262. (not (org-in-src-block-p)))
  5263. (let* ((hl (org-match-string-no-properties 1))
  5264. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5265. (ip (org-maybe-intangible
  5266. (list 'invisible 'org-link
  5267. 'keymap org-mouse-map 'mouse-face 'highlight
  5268. 'font-lock-multiline t 'help-echo help
  5269. 'htmlize-link `(:uri ,hl))))
  5270. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5271. 'font-lock-multiline t 'help-echo help
  5272. 'htmlize-link `(:uri ,hl))))
  5273. ;; We need to remove the invisible property here. Table narrowing
  5274. ;; may have made some of this invisible.
  5275. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5276. (remove-text-properties (match-beginning 0) (match-end 0)
  5277. '(invisible nil))
  5278. (if (match-end 3)
  5279. (progn
  5280. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5281. (org-rear-nonsticky-at (match-beginning 3))
  5282. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5283. (org-rear-nonsticky-at (match-end 3))
  5284. (add-text-properties (match-end 3) (match-end 0) ip)
  5285. (org-rear-nonsticky-at (match-end 0)))
  5286. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5287. (org-rear-nonsticky-at (match-beginning 1))
  5288. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5289. (org-rear-nonsticky-at (match-end 1))
  5290. (add-text-properties (match-end 1) (match-end 0) ip)
  5291. (org-rear-nonsticky-at (match-end 0)))
  5292. t)))
  5293. (defun org-activate-dates (limit)
  5294. "Run through the buffer and add overlays to dates."
  5295. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5296. (not (equal (char-before (match-beginning 0)) 91)))
  5297. (progn
  5298. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5299. (add-text-properties (match-beginning 0) (match-end 0)
  5300. (list 'mouse-face 'highlight
  5301. 'keymap org-mouse-map))
  5302. (org-rear-nonsticky-at (match-end 0))
  5303. (when org-display-custom-times
  5304. (if (match-end 3)
  5305. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5306. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5307. t)))
  5308. (defvar org-target-link-regexp nil
  5309. "Regular expression matching radio targets in plain text.")
  5310. (make-variable-buffer-local 'org-target-link-regexp)
  5311. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5312. "Regular expression matching a link target.")
  5313. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5314. "Regular expression matching a radio target.")
  5315. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5316. "Regular expression matching any target.")
  5317. (defun org-activate-target-links (limit)
  5318. "Run through the buffer and add overlays to target matches."
  5319. (when org-target-link-regexp
  5320. (let ((case-fold-search t))
  5321. (if (re-search-forward org-target-link-regexp limit t)
  5322. (progn
  5323. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5324. (add-text-properties (match-beginning 0) (match-end 0)
  5325. (list 'mouse-face 'highlight
  5326. 'keymap org-mouse-map
  5327. 'help-echo "Radio target link"
  5328. 'org-linked-text t))
  5329. (org-rear-nonsticky-at (match-end 0))
  5330. t)))))
  5331. (defun org-update-radio-target-regexp ()
  5332. "Find all radio targets in this file and update the regular expression."
  5333. (interactive)
  5334. (when (memq 'radio org-activate-links)
  5335. (setq org-target-link-regexp
  5336. (org-make-target-link-regexp (org-all-targets 'radio)))
  5337. (org-restart-font-lock)))
  5338. (defun org-hide-wide-columns (limit)
  5339. (let (s e)
  5340. (setq s (text-property-any (point) (or limit (point-max))
  5341. 'org-cwidth t))
  5342. (when s
  5343. (setq e (next-single-property-change s 'org-cwidth))
  5344. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5345. (goto-char e)
  5346. t)))
  5347. (defvar org-latex-and-related-regexp nil
  5348. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5349. (defvar org-match-substring-regexp)
  5350. (defvar org-match-substring-with-braces-regexp)
  5351. (defun org-compute-latex-and-related-regexp ()
  5352. "Compute regular expression for LaTeX, entities and sub/superscript.
  5353. Result depends on variable `org-highlight-latex-and-related'."
  5354. (org-set-local
  5355. 'org-latex-and-related-regexp
  5356. (let* ((re-sub
  5357. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5358. ((eq org-use-sub-superscripts '{})
  5359. (list org-match-substring-with-braces-regexp))
  5360. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5361. (re-latex
  5362. (when (memq 'latex org-highlight-latex-and-related)
  5363. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5364. (delq nil
  5365. (mapcar (lambda (x)
  5366. (and (member (car x) matchers) (nth 1 x)))
  5367. org-latex-regexps)))))
  5368. (re-entities
  5369. (when (memq 'entities org-highlight-latex-and-related)
  5370. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5371. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5372. (defun org-do-latex-and-related (limit)
  5373. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5374. LIMIT bounds the search for syntax to highlight. Stop at first
  5375. highlighted object, if any. Return t if some highlighting was
  5376. done, nil otherwise."
  5377. (when (org-string-nw-p org-latex-and-related-regexp)
  5378. (catch 'found
  5379. (while (re-search-forward org-latex-and-related-regexp limit t)
  5380. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5381. 'face))
  5382. '(org-code org-verbatim underline))
  5383. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5384. '(?_ ?^))
  5385. 1
  5386. 0)))
  5387. (font-lock-prepend-text-property
  5388. (+ offset (match-beginning 0)) (match-end 0)
  5389. 'face 'org-latex-and-related)
  5390. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5391. '(font-lock-multiline t)))
  5392. (throw 'found t)))
  5393. nil)))
  5394. (defun org-restart-font-lock ()
  5395. "Restart `font-lock-mode', to force refontification."
  5396. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5397. (font-lock-mode -1)
  5398. (font-lock-mode 1)))
  5399. (defun org-all-targets (&optional radio)
  5400. "Return a list of all targets in this file.
  5401. When optional argument RADIO is non-nil, only find radio
  5402. targets."
  5403. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5404. (save-excursion
  5405. (goto-char (point-min))
  5406. (while (re-search-forward re nil t)
  5407. ;; Make sure point is really within the object.
  5408. (backward-char)
  5409. (let ((obj (org-element-context)))
  5410. (when (memq (org-element-type obj) '(radio-target target))
  5411. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5412. rtn)))
  5413. (defun org-make-target-link-regexp (targets)
  5414. "Make regular expression matching all strings in TARGETS.
  5415. The regular expression finds the targets also if there is a line break
  5416. between words."
  5417. (and targets
  5418. (concat
  5419. "\\<\\("
  5420. (mapconcat
  5421. (lambda (x)
  5422. (setq x (regexp-quote x))
  5423. (while (string-match " +" x)
  5424. (setq x (replace-match "\\s-+" t t x)))
  5425. x)
  5426. targets
  5427. "\\|")
  5428. "\\)\\>")))
  5429. (defun org-activate-tags (limit)
  5430. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5431. (progn
  5432. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5433. (add-text-properties (match-beginning 1) (match-end 1)
  5434. (list 'mouse-face 'highlight
  5435. 'keymap org-mouse-map))
  5436. (org-rear-nonsticky-at (match-end 1))
  5437. t)))
  5438. (defun org-outline-level ()
  5439. "Compute the outline level of the heading at point.
  5440. If this is called at a normal headline, the level is the number of stars.
  5441. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5442. (save-excursion
  5443. (if (not (condition-case nil
  5444. (org-back-to-heading t)
  5445. (error nil)))
  5446. 0
  5447. (looking-at org-outline-regexp)
  5448. (1- (- (match-end 0) (match-beginning 0))))))
  5449. (defvar org-font-lock-keywords nil)
  5450. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5451. "Regular expression matching a property line.")
  5452. (defvar org-font-lock-hook nil
  5453. "Functions to be called for special font lock stuff.")
  5454. (defvar org-font-lock-set-keywords-hook nil
  5455. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5456. This is called after `org-font-lock-extra-keywords' is defined, but before
  5457. it is installed to be used by font lock. This can be useful if something
  5458. needs to be inserted at a specific position in the font-lock sequence.")
  5459. (defun org-font-lock-hook (limit)
  5460. "Run `org-font-lock-hook' within LIMIT."
  5461. (run-hook-with-args 'org-font-lock-hook limit))
  5462. (defun org-set-font-lock-defaults ()
  5463. "Set font lock defaults for the current buffer."
  5464. (let* ((em org-fontify-emphasized-text)
  5465. (lk org-activate-links)
  5466. (org-font-lock-extra-keywords
  5467. (list
  5468. ;; Call the hook
  5469. '(org-font-lock-hook)
  5470. ;; Headlines
  5471. `(,(if org-fontify-whole-heading-line
  5472. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5473. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5474. (1 (org-get-level-face 1))
  5475. (2 (org-get-level-face 2))
  5476. (3 (org-get-level-face 3)))
  5477. ;; Table lines
  5478. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5479. (1 'org-table t))
  5480. ;; Table internals
  5481. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5482. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5483. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5484. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5485. ;; Drawers
  5486. (list org-drawer-regexp '(0 'org-special-keyword t))
  5487. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5488. ;; Properties
  5489. (list org-property-re
  5490. '(1 'org-special-keyword t)
  5491. '(3 'org-property-value t))
  5492. ;; Links
  5493. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5494. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5495. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5496. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5497. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5498. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5499. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5500. ;; Targets.
  5501. (list org-any-target-regexp '(0 'org-target t))
  5502. ;; Diary sexps.
  5503. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5504. ;; Macro
  5505. '("{{{.+}}}" (0 'org-macro t))
  5506. '(org-hide-wide-columns (0 nil append))
  5507. ;; TODO keyword
  5508. (list (format org-heading-keyword-regexp-format
  5509. org-todo-regexp)
  5510. '(2 (org-get-todo-face 2) t))
  5511. ;; DONE
  5512. (if org-fontify-done-headline
  5513. (list (format org-heading-keyword-regexp-format
  5514. (concat
  5515. "\\(?:"
  5516. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5517. "\\)"))
  5518. '(2 'org-headline-done t))
  5519. nil)
  5520. ;; Priorities
  5521. '(org-font-lock-add-priority-faces)
  5522. ;; Tags
  5523. '(org-font-lock-add-tag-faces)
  5524. ;; Tags groups
  5525. (if (and org-group-tags org-tag-groups-alist)
  5526. (list (concat org-outline-regexp-bol ".+\\(:"
  5527. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5528. ":\\).*$")
  5529. '(1 'org-tag-group prepend)))
  5530. ;; Special keywords
  5531. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5532. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5533. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5534. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5535. ;; Emphasis
  5536. (if em
  5537. (if (featurep 'xemacs)
  5538. '(org-do-emphasis-faces (0 nil append))
  5539. '(org-do-emphasis-faces)))
  5540. ;; Checkboxes
  5541. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5542. 1 'org-checkbox prepend)
  5543. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5544. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5545. (0 (org-get-checkbox-statistics-face) t)))
  5546. ;; Description list items
  5547. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5548. 1 'org-list-dt prepend)
  5549. ;; ARCHIVEd headings
  5550. (list (concat
  5551. org-outline-regexp-bol
  5552. "\\(.*:" org-archive-tag ":.*\\)")
  5553. '(1 'org-archived prepend))
  5554. ;; Specials
  5555. '(org-do-latex-and-related)
  5556. '(org-fontify-entities)
  5557. '(org-raise-scripts)
  5558. ;; Code
  5559. '(org-activate-code (1 'org-code t))
  5560. ;; COMMENT
  5561. (list (format org-heading-keyword-regexp-format
  5562. (concat "\\("
  5563. org-comment-string "\\|" org-quote-string
  5564. "\\)"))
  5565. '(2 'org-special-keyword t))
  5566. ;; Blocks and meta lines
  5567. '(org-fontify-meta-lines-and-blocks))))
  5568. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5569. (run-hooks 'org-font-lock-set-keywords-hook)
  5570. ;; Now set the full font-lock-keywords
  5571. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5572. (org-set-local 'font-lock-defaults
  5573. '(org-font-lock-keywords t nil nil backward-paragraph))
  5574. (kill-local-variable 'font-lock-keywords) nil))
  5575. (defun org-toggle-pretty-entities ()
  5576. "Toggle the composition display of entities as UTF8 characters."
  5577. (interactive)
  5578. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5579. (org-restart-font-lock)
  5580. (if org-pretty-entities
  5581. (message "Entities are now displayed as UTF8 characters")
  5582. (save-restriction
  5583. (widen)
  5584. (org-decompose-region (point-min) (point-max))
  5585. (message "Entities are now displayed as plain text"))))
  5586. (defvar org-custom-properties-overlays nil
  5587. "List of overlays used for custom properties.")
  5588. (make-variable-buffer-local 'org-custom-properties-overlays)
  5589. (defun org-toggle-custom-properties-visibility ()
  5590. "Display or hide properties in `org-custom-properties'."
  5591. (interactive)
  5592. (if org-custom-properties-overlays
  5593. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5594. (setq org-custom-properties-overlays nil))
  5595. (unless (not org-custom-properties)
  5596. (save-excursion
  5597. (save-restriction
  5598. (widen)
  5599. (goto-char (point-min))
  5600. (while (re-search-forward org-property-re nil t)
  5601. (mapc (lambda(p)
  5602. (when (equal p (substring (match-string 1) 1 -1))
  5603. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5604. (overlay-put o 'invisible t)
  5605. (overlay-put o 'org-custom-property t)
  5606. (push o org-custom-properties-overlays))))
  5607. org-custom-properties)))))))
  5608. (defun org-fontify-entities (limit)
  5609. "Find an entity to fontify."
  5610. (let (ee)
  5611. (when org-pretty-entities
  5612. (catch 'match
  5613. (while (re-search-forward
  5614. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5615. limit t)
  5616. (if (and (not (org-in-indented-comment-line))
  5617. (setq ee (org-entity-get (match-string 1)))
  5618. (= (length (nth 6 ee)) 1))
  5619. (let*
  5620. ((end (if (equal (match-string 2) "{}")
  5621. (match-end 2)
  5622. (match-end 1))))
  5623. (add-text-properties
  5624. (match-beginning 0) end
  5625. (list 'font-lock-fontified t))
  5626. (compose-region (match-beginning 0) end
  5627. (nth 6 ee) nil)
  5628. (backward-char 1)
  5629. (throw 'match t))))
  5630. nil))))
  5631. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5632. "Fontify string S like in Org-mode."
  5633. (with-temp-buffer
  5634. (insert s)
  5635. (let ((org-odd-levels-only odd-levels))
  5636. (org-mode)
  5637. (font-lock-fontify-buffer)
  5638. (buffer-string))))
  5639. (defvar org-m nil)
  5640. (defvar org-l nil)
  5641. (defvar org-f nil)
  5642. (defun org-get-level-face (n)
  5643. "Get the right face for match N in font-lock matching of headlines."
  5644. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5645. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5646. (if org-cycle-level-faces
  5647. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5648. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5649. (cond
  5650. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5651. ((eq n 2) org-f)
  5652. (t (if org-level-color-stars-only nil org-f))))
  5653. (defun org-get-todo-face (kwd)
  5654. "Get the right face for a TODO keyword KWD.
  5655. If KWD is a number, get the corresponding match group."
  5656. (if (numberp kwd) (setq kwd (match-string kwd)))
  5657. (or (org-face-from-face-or-color
  5658. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5659. (and (member kwd org-done-keywords) 'org-done)
  5660. 'org-todo))
  5661. (defun org-face-from-face-or-color (context inherit face-or-color)
  5662. "Create a face list that inherits INHERIT, but sets the foreground color.
  5663. When FACE-OR-COLOR is not a string, just return it."
  5664. (if (stringp face-or-color)
  5665. (list :inherit inherit
  5666. (cdr (assoc context org-faces-easy-properties))
  5667. face-or-color)
  5668. face-or-color))
  5669. (defun org-font-lock-add-tag-faces (limit)
  5670. "Add the special tag faces."
  5671. (when (and org-tag-faces org-tags-special-faces-re)
  5672. (while (re-search-forward org-tags-special-faces-re limit t)
  5673. (add-text-properties (match-beginning 1) (match-end 1)
  5674. (list 'face (org-get-tag-face 1)
  5675. 'font-lock-fontified t))
  5676. (backward-char 1))))
  5677. (defun org-font-lock-add-priority-faces (limit)
  5678. "Add the special priority faces."
  5679. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5680. (when (save-match-data (org-at-heading-p))
  5681. (add-text-properties
  5682. (match-beginning 0) (match-end 0)
  5683. (list 'face (or (org-face-from-face-or-color
  5684. 'priority 'org-priority
  5685. (cdr (assoc (char-after (match-beginning 1))
  5686. org-priority-faces)))
  5687. 'org-priority)
  5688. 'font-lock-fontified t)))))
  5689. (defun org-get-tag-face (kwd)
  5690. "Get the right face for a TODO keyword KWD.
  5691. If KWD is a number, get the corresponding match group."
  5692. (if (numberp kwd) (setq kwd (match-string kwd)))
  5693. (or (org-face-from-face-or-color
  5694. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5695. 'org-tag))
  5696. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5697. "Remove fontification and activation overlays from links."
  5698. (font-lock-default-unfontify-region beg end)
  5699. (let* ((buffer-undo-list t)
  5700. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5701. (inhibit-modification-hooks t)
  5702. deactivate-mark buffer-file-name buffer-file-truename)
  5703. (org-decompose-region beg end)
  5704. (remove-text-properties beg end
  5705. '(mouse-face t keymap t org-linked-text t
  5706. invisible t intangible t
  5707. org-no-flyspell t org-emphasis t))
  5708. (org-remove-font-lock-display-properties beg end)))
  5709. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5710. ((raise 0.3) (height 0.7))
  5711. ((raise -0.5))
  5712. ((raise 0.5)))
  5713. "Display properties for showing superscripts and subscripts.")
  5714. (defun org-remove-font-lock-display-properties (beg end)
  5715. "Remove specific display properties that have been added by font lock.
  5716. The will remove the raise properties that are used to show superscripts
  5717. and subscripts."
  5718. (let (next prop)
  5719. (while (< beg end)
  5720. (setq next (next-single-property-change beg 'display nil end)
  5721. prop (get-text-property beg 'display))
  5722. (if (member prop org-script-display)
  5723. (put-text-property beg next 'display nil))
  5724. (setq beg next))))
  5725. (defun org-raise-scripts (limit)
  5726. "Add raise properties to sub/superscripts."
  5727. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5728. (if (re-search-forward
  5729. (if (eq org-use-sub-superscripts t)
  5730. org-match-substring-regexp
  5731. org-match-substring-with-braces-regexp)
  5732. limit t)
  5733. (let* ((pos (point)) table-p comment-p
  5734. (mpos (match-beginning 3))
  5735. (emph-p (get-text-property mpos 'org-emphasis))
  5736. (link-p (get-text-property mpos 'mouse-face))
  5737. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5738. (goto-char (point-at-bol))
  5739. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5740. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5741. (goto-char pos)
  5742. ;; Handle a_b^c
  5743. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5744. (if (or comment-p emph-p link-p keyw-p)
  5745. t
  5746. (put-text-property (match-beginning 3) (match-end 0)
  5747. 'display
  5748. (if (equal (char-after (match-beginning 2)) ?^)
  5749. (nth (if table-p 3 1) org-script-display)
  5750. (nth (if table-p 2 0) org-script-display)))
  5751. (add-text-properties (match-beginning 2) (match-end 2)
  5752. (list 'invisible t
  5753. 'org-dwidth t 'org-dwidth-n 1))
  5754. (if (and (eq (char-after (match-beginning 3)) ?{)
  5755. (eq (char-before (match-end 3)) ?}))
  5756. (progn
  5757. (add-text-properties
  5758. (match-beginning 3) (1+ (match-beginning 3))
  5759. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5760. (add-text-properties
  5761. (1- (match-end 3)) (match-end 3)
  5762. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5763. t)))))
  5764. ;;;; Visibility cycling, including org-goto and indirect buffer
  5765. ;;; Cycling
  5766. (defvar org-cycle-global-status nil)
  5767. (make-variable-buffer-local 'org-cycle-global-status)
  5768. (put 'org-cycle-global-status 'org-state t)
  5769. (defvar org-cycle-subtree-status nil)
  5770. (make-variable-buffer-local 'org-cycle-subtree-status)
  5771. (put 'org-cycle-subtree-status 'org-state t)
  5772. (defvar org-inlinetask-min-level)
  5773. ;;;###autoload
  5774. (defun org-cycle (&optional arg)
  5775. "TAB-action and visibility cycling for Org-mode.
  5776. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5777. is outline visibility cycling, but it also invokes other actions
  5778. in special contexts.
  5779. - When this function is called with a prefix argument, rotate the entire
  5780. buffer through 3 states (global cycling)
  5781. 1. OVERVIEW: Show only top-level headlines.
  5782. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5783. 3. SHOW ALL: Show everything.
  5784. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5785. determined by the variable `org-startup-folded', and by any VISIBILITY
  5786. properties in the buffer.
  5787. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5788. including any drawers.
  5789. - When inside a table, re-align the table and move to the next field.
  5790. - When point is at the beginning of a headline, rotate the subtree started
  5791. by this line through 3 different states (local cycling)
  5792. 1. FOLDED: Only the main headline is shown.
  5793. 2. CHILDREN: The main headline and the direct children are shown.
  5794. From this state, you can move to one of the children
  5795. and zoom in further.
  5796. 3. SUBTREE: Show the entire subtree, including body text.
  5797. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5798. - When point is at the beginning of an empty headline and the variable
  5799. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5800. of the headline by demoting and promoting it to likely levels. This
  5801. speeds up creation document structure by pressing TAB once or several
  5802. times right after creating a new headline.
  5803. - When there is a numeric prefix, go up to a heading with level ARG, do
  5804. a `show-subtree' and return to the previous cursor position. If ARG
  5805. is negative, go up that many levels.
  5806. - When point is not at the beginning of a headline, execute the global
  5807. binding for TAB, which is re-indenting the line. See the option
  5808. `org-cycle-emulate-tab' for details.
  5809. - Special case: if point is at the beginning of the buffer and there is
  5810. no headline in line 1, this function will act as if called with prefix arg
  5811. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5812. But only if also the variable `org-cycle-global-at-bob' is t."
  5813. (interactive "P")
  5814. (org-load-modules-maybe)
  5815. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5816. (and org-cycle-level-after-item/entry-creation
  5817. (or (org-cycle-level)
  5818. (org-cycle-item-indentation))))
  5819. (let* (message-log-max ; Don't populate the *Messages* buffer
  5820. (limit-level
  5821. (or org-cycle-max-level
  5822. (and (boundp 'org-inlinetask-min-level)
  5823. org-inlinetask-min-level
  5824. (1- org-inlinetask-min-level))))
  5825. (nstars (and limit-level
  5826. (if org-odd-levels-only
  5827. (and limit-level (1- (* limit-level 2)))
  5828. limit-level)))
  5829. (org-outline-regexp
  5830. (if (not (derived-mode-p 'org-mode))
  5831. outline-regexp
  5832. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5833. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5834. (not (looking-at org-outline-regexp))))
  5835. (org-cycle-hook
  5836. (if bob-special
  5837. (delq 'org-optimize-window-after-visibility-change
  5838. (copy-sequence org-cycle-hook))
  5839. org-cycle-hook))
  5840. (pos (point)))
  5841. (if (or bob-special (equal arg '(4)))
  5842. ;; special case: use global cycling
  5843. (setq arg t))
  5844. (cond
  5845. ((equal arg '(16))
  5846. (setq last-command 'dummy)
  5847. (org-set-startup-visibility)
  5848. (message "Startup visibility, plus VISIBILITY properties"))
  5849. ((equal arg '(64))
  5850. (show-all)
  5851. (message "Entire buffer visible, including drawers"))
  5852. ;; Table: enter it or move to the next field.
  5853. ((org-at-table-p 'any)
  5854. (if (org-at-table.el-p)
  5855. (message "Use C-c ' to edit table.el tables")
  5856. (if arg (org-table-edit-field t)
  5857. (org-table-justify-field-maybe)
  5858. (call-interactively 'org-table-next-field))))
  5859. ((run-hook-with-args-until-success
  5860. 'org-tab-after-check-for-table-hook))
  5861. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5862. ((eq arg t) (org-cycle-internal-global))
  5863. ;; Drawers: delegate to `org-flag-drawer'.
  5864. ((and org-drawers org-drawer-regexp
  5865. (save-excursion
  5866. (beginning-of-line 1)
  5867. (looking-at org-drawer-regexp)))
  5868. (org-flag-drawer ; toggle block visibility
  5869. (not (get-char-property (match-end 0) 'invisible))))
  5870. ;; Show-subtree, ARG levels up from here.
  5871. ((integerp arg)
  5872. (save-excursion
  5873. (org-back-to-heading)
  5874. (outline-up-heading (if (< arg 0) (- arg)
  5875. (- (funcall outline-level) arg)))
  5876. (org-show-subtree)))
  5877. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5878. ((and (featurep 'org-inlinetask)
  5879. (org-inlinetask-at-task-p)
  5880. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5881. (org-inlinetask-toggle-visibility))
  5882. ((org-try-cdlatex-tab))
  5883. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5884. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5885. (save-excursion (beginning-of-line 1)
  5886. (looking-at org-outline-regexp)))
  5887. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5888. (org-cycle-internal-local))
  5889. ;; From there: TAB emulation and template completion.
  5890. (buffer-read-only (org-back-to-heading))
  5891. ((run-hook-with-args-until-success
  5892. 'org-tab-after-check-for-cycling-hook))
  5893. ((org-try-structure-completion))
  5894. ((run-hook-with-args-until-success
  5895. 'org-tab-before-tab-emulation-hook))
  5896. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5897. (or (not (bolp))
  5898. (not (looking-at org-outline-regexp))))
  5899. (call-interactively (global-key-binding "\t")))
  5900. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5901. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5902. (or (and (eq org-cycle-emulate-tab 'white)
  5903. (= (match-end 0) (point-at-eol)))
  5904. (and (eq org-cycle-emulate-tab 'whitestart)
  5905. (>= (match-end 0) pos))))
  5906. t
  5907. (eq org-cycle-emulate-tab t))
  5908. (call-interactively (global-key-binding "\t")))
  5909. (t (save-excursion
  5910. (org-back-to-heading)
  5911. (org-cycle)))))))
  5912. (defun org-cycle-internal-global ()
  5913. "Do the global cycling action."
  5914. ;; Hack to avoid display of messages for .org attachments in Gnus
  5915. (let (message-log-max ; Don't populate the *Messages* buffer
  5916. (ga (string-match "\\*fontification" (buffer-name))))
  5917. (cond
  5918. ((and (eq last-command this-command)
  5919. (eq org-cycle-global-status 'overview))
  5920. ;; We just created the overview - now do table of contents
  5921. ;; This can be slow in very large buffers, so indicate action
  5922. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5923. (unless ga (message "CONTENTS..."))
  5924. (org-content)
  5925. (unless ga (message "CONTENTS...done"))
  5926. (setq org-cycle-global-status 'contents)
  5927. (run-hook-with-args 'org-cycle-hook 'contents))
  5928. ((and (eq last-command this-command)
  5929. (eq org-cycle-global-status 'contents))
  5930. ;; We just showed the table of contents - now show everything
  5931. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5932. (show-all)
  5933. (unless ga (message "SHOW ALL"))
  5934. (setq org-cycle-global-status 'all)
  5935. (run-hook-with-args 'org-cycle-hook 'all))
  5936. (t
  5937. ;; Default action: go to overview
  5938. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5939. (org-overview)
  5940. (unless ga (message "OVERVIEW"))
  5941. (setq org-cycle-global-status 'overview)
  5942. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5943. (defun org-cycle-internal-local ()
  5944. "Do the local cycling action."
  5945. (let (message-log-max ; Don't populate the *Messages* buffer
  5946. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5947. ;; First, determine end of headline (EOH), end of subtree or item
  5948. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5949. (save-excursion
  5950. (if (org-at-item-p)
  5951. (progn
  5952. (beginning-of-line)
  5953. (setq struct (org-list-struct))
  5954. (setq eoh (point-at-eol))
  5955. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5956. (setq has-children (org-list-has-child-p (point) struct)))
  5957. (org-back-to-heading)
  5958. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5959. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5960. (setq has-children
  5961. (or (save-excursion
  5962. (let ((level (funcall outline-level)))
  5963. (outline-next-heading)
  5964. (and (org-at-heading-p t)
  5965. (> (funcall outline-level) level))))
  5966. (save-excursion
  5967. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5968. ;; Determine end invisible part of buffer (EOL)
  5969. (beginning-of-line 2)
  5970. ;; XEmacs doesn't have `next-single-char-property-change'
  5971. (if (featurep 'xemacs)
  5972. (while (and (not (eobp)) ;; this is like `next-line'
  5973. (get-char-property (1- (point)) 'invisible))
  5974. (beginning-of-line 2))
  5975. (while (and (not (eobp)) ;; this is like `next-line'
  5976. (get-char-property (1- (point)) 'invisible))
  5977. (goto-char (next-single-char-property-change (point) 'invisible))
  5978. (and (eolp) (beginning-of-line 2))))
  5979. (setq eol (point)))
  5980. ;; Find out what to do next and set `this-command'
  5981. (cond
  5982. ((= eos eoh)
  5983. ;; Nothing is hidden behind this heading
  5984. (unless (org-before-first-heading-p)
  5985. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5986. (message "EMPTY ENTRY")
  5987. (setq org-cycle-subtree-status nil)
  5988. (save-excursion
  5989. (goto-char eos)
  5990. (outline-next-heading)
  5991. (if (outline-invisible-p) (org-flag-heading nil))))
  5992. ((and (or (>= eol eos)
  5993. (not (string-match "\\S-" (buffer-substring eol eos))))
  5994. (or has-children
  5995. (not (setq children-skipped
  5996. org-cycle-skip-children-state-if-no-children))))
  5997. ;; Entire subtree is hidden in one line: children view
  5998. (unless (org-before-first-heading-p)
  5999. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6000. (if (org-at-item-p)
  6001. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6002. (org-show-entry)
  6003. (org-with-limited-levels (show-children))
  6004. ;; FIXME: This slows down the func way too much.
  6005. ;; How keep drawers hidden in subtree anyway?
  6006. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6007. ;; (org-cycle-hide-drawers 'subtree))
  6008. ;; Fold every list in subtree to top-level items.
  6009. (when (eq org-cycle-include-plain-lists 'integrate)
  6010. (save-excursion
  6011. (org-back-to-heading)
  6012. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6013. (beginning-of-line 1)
  6014. (let* ((struct (org-list-struct))
  6015. (prevs (org-list-prevs-alist struct))
  6016. (end (org-list-get-bottom-point struct)))
  6017. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6018. (org-list-get-all-items (point) struct prevs))
  6019. (goto-char (if (< end eos) end eos)))))))
  6020. (message "CHILDREN")
  6021. (save-excursion
  6022. (goto-char eos)
  6023. (outline-next-heading)
  6024. (if (outline-invisible-p) (org-flag-heading nil)))
  6025. (setq org-cycle-subtree-status 'children)
  6026. (unless (org-before-first-heading-p)
  6027. (run-hook-with-args 'org-cycle-hook 'children)))
  6028. ((or children-skipped
  6029. (and (eq last-command this-command)
  6030. (eq org-cycle-subtree-status 'children)))
  6031. ;; We just showed the children, or no children are there,
  6032. ;; now show everything.
  6033. (unless (org-before-first-heading-p)
  6034. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6035. (outline-flag-region eoh eos nil)
  6036. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6037. (setq org-cycle-subtree-status 'subtree)
  6038. (unless (org-before-first-heading-p)
  6039. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6040. (t
  6041. ;; Default action: hide the subtree.
  6042. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6043. (outline-flag-region eoh eos t)
  6044. (message "FOLDED")
  6045. (setq org-cycle-subtree-status 'folded)
  6046. (unless (org-before-first-heading-p)
  6047. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6048. ;;;###autoload
  6049. (defun org-global-cycle (&optional arg)
  6050. "Cycle the global visibility. For details see `org-cycle'.
  6051. With \\[universal-argument] prefix arg, switch to startup visibility.
  6052. With a numeric prefix, show all headlines up to that level."
  6053. (interactive "P")
  6054. (let ((org-cycle-include-plain-lists
  6055. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6056. (cond
  6057. ((integerp arg)
  6058. (show-all)
  6059. (hide-sublevels arg)
  6060. (setq org-cycle-global-status 'contents))
  6061. ((equal arg '(4))
  6062. (org-set-startup-visibility)
  6063. (message "Startup visibility, plus VISIBILITY properties."))
  6064. (t
  6065. (org-cycle '(4))))))
  6066. (defun org-set-startup-visibility ()
  6067. "Set the visibility required by startup options and properties."
  6068. (cond
  6069. ((eq org-startup-folded t)
  6070. (org-cycle '(4)))
  6071. ((eq org-startup-folded 'content)
  6072. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  6073. (org-cycle '(4)) (org-cycle '(4)))))
  6074. (unless (eq org-startup-folded 'showeverything)
  6075. (if org-hide-block-startup (org-hide-block-all))
  6076. (org-set-visibility-according-to-property 'no-cleanup)
  6077. (org-cycle-hide-archived-subtrees 'all)
  6078. (org-cycle-hide-drawers 'all)
  6079. (org-cycle-show-empty-lines t)))
  6080. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6081. "Switch subtree visibilities according to :VISIBILITY: property."
  6082. (interactive)
  6083. (let (org-show-entry-below state)
  6084. (save-excursion
  6085. (goto-char (point-min))
  6086. (while (re-search-forward
  6087. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6088. nil t)
  6089. (setq state (match-string 1))
  6090. (save-excursion
  6091. (org-back-to-heading t)
  6092. (hide-subtree)
  6093. (org-reveal)
  6094. (cond
  6095. ((equal state '("fold" "folded"))
  6096. (hide-subtree))
  6097. ((equal state "children")
  6098. (org-show-hidden-entry)
  6099. (show-children))
  6100. ((equal state "content")
  6101. (save-excursion
  6102. (save-restriction
  6103. (org-narrow-to-subtree)
  6104. (org-content))))
  6105. ((member state '("all" "showall"))
  6106. (show-subtree)))))
  6107. (unless no-cleanup
  6108. (org-cycle-hide-archived-subtrees 'all)
  6109. (org-cycle-hide-drawers 'all)
  6110. (org-cycle-show-empty-lines 'all)))))
  6111. ;; This function uses outline-regexp instead of the more fundamental
  6112. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6113. ;; buffers, where outline-regexp is needed.
  6114. (defun org-overview ()
  6115. "Switch to overview mode, showing only top-level headlines.
  6116. Really, this shows all headlines with level equal or greater than the level
  6117. of the first headline in the buffer. This is important, because if the
  6118. first headline is not level one, then (hide-sublevels 1) gives confusing
  6119. results."
  6120. (interactive)
  6121. (let ((l (org-current-line))
  6122. (level (save-excursion
  6123. (goto-char (point-min))
  6124. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6125. (progn
  6126. (goto-char (match-beginning 0))
  6127. (funcall outline-level))))))
  6128. (and level (hide-sublevels level))
  6129. (recenter '(4))
  6130. (org-goto-line l)))
  6131. (defun org-content (&optional arg)
  6132. "Show all headlines in the buffer, like a table of contents.
  6133. With numerical argument N, show content up to level N."
  6134. (interactive "P")
  6135. (save-excursion
  6136. ;; Visit all headings and show their offspring
  6137. (and (integerp arg) (org-overview))
  6138. (goto-char (point-max))
  6139. (catch 'exit
  6140. (while (and (progn (condition-case nil
  6141. (outline-previous-visible-heading 1)
  6142. (error (goto-char (point-min))))
  6143. t)
  6144. (looking-at org-outline-regexp))
  6145. (if (integerp arg)
  6146. (show-children (1- arg))
  6147. (show-branches))
  6148. (if (bobp) (throw 'exit nil))))))
  6149. (defun org-optimize-window-after-visibility-change (state)
  6150. "Adjust the window after a change in outline visibility.
  6151. This function is the default value of the hook `org-cycle-hook'."
  6152. (when (get-buffer-window (current-buffer))
  6153. (cond
  6154. ((eq state 'content) nil)
  6155. ((eq state 'all) nil)
  6156. ((eq state 'folded) nil)
  6157. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6158. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6159. (defun org-remove-empty-overlays-at (pos)
  6160. "Remove outline overlays that do not contain non-white stuff."
  6161. (mapc
  6162. (lambda (o)
  6163. (and (eq 'outline (overlay-get o 'invisible))
  6164. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6165. (overlay-end o))))
  6166. (delete-overlay o)))
  6167. (overlays-at pos)))
  6168. (defun org-clean-visibility-after-subtree-move ()
  6169. "Fix visibility issues after moving a subtree."
  6170. ;; First, find a reasonable region to look at:
  6171. ;; Start two siblings above, end three below
  6172. (let* ((beg (save-excursion
  6173. (and (org-get-last-sibling)
  6174. (org-get-last-sibling))
  6175. (point)))
  6176. (end (save-excursion
  6177. (and (org-get-next-sibling)
  6178. (org-get-next-sibling)
  6179. (org-get-next-sibling))
  6180. (if (org-at-heading-p)
  6181. (point-at-eol)
  6182. (point))))
  6183. (level (looking-at "\\*+"))
  6184. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6185. (save-excursion
  6186. (save-restriction
  6187. (narrow-to-region beg end)
  6188. (when re
  6189. ;; Properly fold already folded siblings
  6190. (goto-char (point-min))
  6191. (while (re-search-forward re nil t)
  6192. (if (and (not (outline-invisible-p))
  6193. (save-excursion
  6194. (goto-char (point-at-eol)) (outline-invisible-p)))
  6195. (hide-entry))))
  6196. (org-cycle-show-empty-lines 'overview)
  6197. (org-cycle-hide-drawers 'overview)))))
  6198. (defun org-cycle-show-empty-lines (state)
  6199. "Show empty lines above all visible headlines.
  6200. The region to be covered depends on STATE when called through
  6201. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6202. entire buffer covered. Note that an empty line is only shown if there
  6203. are at least `org-cycle-separator-lines' empty lines before the headline."
  6204. (when (not (= org-cycle-separator-lines 0))
  6205. (save-excursion
  6206. (let* ((n (abs org-cycle-separator-lines))
  6207. (re (cond
  6208. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6209. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6210. (t (let ((ns (number-to-string (- n 2))))
  6211. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6212. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6213. beg end b e)
  6214. (cond
  6215. ((memq state '(overview contents t))
  6216. (setq beg (point-min) end (point-max)))
  6217. ((memq state '(children folded))
  6218. (setq beg (point) end (progn (org-end-of-subtree t t)
  6219. (beginning-of-line 2)
  6220. (point)))))
  6221. (when beg
  6222. (goto-char beg)
  6223. (while (re-search-forward re end t)
  6224. (unless (get-char-property (match-end 1) 'invisible)
  6225. (setq e (match-end 1))
  6226. (if (< org-cycle-separator-lines 0)
  6227. (setq b (save-excursion
  6228. (goto-char (match-beginning 0))
  6229. (org-back-over-empty-lines)
  6230. (if (save-excursion
  6231. (goto-char (max (point-min) (1- (point))))
  6232. (org-at-heading-p))
  6233. (1- (point))
  6234. (point))))
  6235. (setq b (match-beginning 1)))
  6236. (outline-flag-region b e nil)))))))
  6237. ;; Never hide empty lines at the end of the file.
  6238. (save-excursion
  6239. (goto-char (point-max))
  6240. (outline-previous-heading)
  6241. (outline-end-of-heading)
  6242. (if (and (looking-at "[ \t\n]+")
  6243. (= (match-end 0) (point-max)))
  6244. (outline-flag-region (point) (match-end 0) nil))))
  6245. (defun org-show-empty-lines-in-parent ()
  6246. "Move to the parent and re-show empty lines before visible headlines."
  6247. (save-excursion
  6248. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6249. (org-cycle-show-empty-lines context))))
  6250. (defun org-files-list ()
  6251. "Return `org-agenda-files' list, plus all open org-mode files.
  6252. This is useful for operations that need to scan all of a user's
  6253. open and agenda-wise Org files."
  6254. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6255. (dolist (buf (buffer-list))
  6256. (with-current-buffer buf
  6257. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6258. (let ((file (expand-file-name (buffer-file-name))))
  6259. (unless (member file files)
  6260. (push file files))))))
  6261. files))
  6262. (defsubst org-entry-beginning-position ()
  6263. "Return the beginning position of the current entry."
  6264. (save-excursion (outline-back-to-heading t) (point)))
  6265. (defsubst org-entry-end-position ()
  6266. "Return the end position of the current entry."
  6267. (save-excursion (outline-next-heading) (point)))
  6268. (defun org-cycle-hide-drawers (state)
  6269. "Re-hide all drawers after a visibility state change."
  6270. (when (and (derived-mode-p 'org-mode)
  6271. (not (memq state '(overview folded contents))))
  6272. (save-excursion
  6273. (let* ((globalp (memq state '(contents all)))
  6274. (beg (if globalp (point-min) (point)))
  6275. (end (if globalp (point-max)
  6276. (if (eq state 'children)
  6277. (save-excursion (outline-next-heading) (point))
  6278. (org-end-of-subtree t)))))
  6279. (goto-char beg)
  6280. (while (re-search-forward org-drawer-regexp end t)
  6281. (org-flag-drawer t))))))
  6282. (defun org-cycle-hide-inline-tasks (state)
  6283. "Re-hide inline task when switching to 'contents visibility state."
  6284. (when (and (eq state 'contents)
  6285. (boundp 'org-inlinetask-min-level)
  6286. org-inlinetask-min-level)
  6287. (hide-sublevels (1- org-inlinetask-min-level))))
  6288. (defun org-flag-drawer (flag)
  6289. "When FLAG is non-nil, hide the drawer we are within.
  6290. Otherwise make it visible."
  6291. (save-excursion
  6292. (beginning-of-line 1)
  6293. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6294. (let ((b (match-end 0)))
  6295. (if (re-search-forward
  6296. "^[ \t]*:END:"
  6297. (save-excursion (outline-next-heading) (point)) t)
  6298. (outline-flag-region b (point-at-eol) flag)
  6299. (user-error ":END: line missing at position %s" b))))))
  6300. (defun org-subtree-end-visible-p ()
  6301. "Is the end of the current subtree visible?"
  6302. (pos-visible-in-window-p
  6303. (save-excursion (org-end-of-subtree t) (point))))
  6304. (defun org-first-headline-recenter (&optional N)
  6305. "Move cursor to the first headline and recenter the headline.
  6306. Optional argument N means put the headline into the Nth line of the window."
  6307. (goto-char (point-min))
  6308. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6309. (beginning-of-line)
  6310. (recenter (prefix-numeric-value N))))
  6311. ;;; Saving and restoring visibility
  6312. (defun org-outline-overlay-data (&optional use-markers)
  6313. "Return a list of the locations of all outline overlays.
  6314. These are overlays with the `invisible' property value `outline'.
  6315. The return value is a list of cons cells, with start and stop
  6316. positions for each overlay.
  6317. If USE-MARKERS is set, return the positions as markers."
  6318. (let (beg end)
  6319. (save-excursion
  6320. (save-restriction
  6321. (widen)
  6322. (delq nil
  6323. (mapcar (lambda (o)
  6324. (when (eq (overlay-get o 'invisible) 'outline)
  6325. (setq beg (overlay-start o)
  6326. end (overlay-end o))
  6327. (and beg end (> end beg)
  6328. (if use-markers
  6329. (cons (move-marker (make-marker) beg)
  6330. (move-marker (make-marker) end))
  6331. (cons beg end)))))
  6332. (overlays-in (point-min) (point-max))))))))
  6333. (defun org-set-outline-overlay-data (data)
  6334. "Create visibility overlays for all positions in DATA.
  6335. DATA should have been made by `org-outline-overlay-data'."
  6336. (let (o)
  6337. (save-excursion
  6338. (save-restriction
  6339. (widen)
  6340. (show-all)
  6341. (mapc (lambda (c)
  6342. (outline-flag-region (car c) (cdr c) t))
  6343. data)))))
  6344. ;;; Folding of blocks
  6345. (defvar org-hide-block-overlays nil
  6346. "Overlays hiding blocks.")
  6347. (make-variable-buffer-local 'org-hide-block-overlays)
  6348. (defun org-block-map (function &optional start end)
  6349. "Call FUNCTION at the head of all source blocks in the current buffer.
  6350. Optional arguments START and END can be used to limit the range."
  6351. (let ((start (or start (point-min)))
  6352. (end (or end (point-max))))
  6353. (save-excursion
  6354. (goto-char start)
  6355. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6356. (save-excursion
  6357. (save-match-data
  6358. (goto-char (match-beginning 0))
  6359. (funcall function)))))))
  6360. (defun org-hide-block-toggle-all ()
  6361. "Toggle the visibility of all blocks in the current buffer."
  6362. (org-block-map #'org-hide-block-toggle))
  6363. (defun org-hide-block-all ()
  6364. "Fold all blocks in the current buffer."
  6365. (interactive)
  6366. (org-show-block-all)
  6367. (org-block-map #'org-hide-block-toggle-maybe))
  6368. (defun org-show-block-all ()
  6369. "Unfold all blocks in the current buffer."
  6370. (interactive)
  6371. (mapc 'delete-overlay org-hide-block-overlays)
  6372. (setq org-hide-block-overlays nil))
  6373. (defun org-hide-block-toggle-maybe ()
  6374. "Toggle visibility of block at point."
  6375. (interactive)
  6376. (let ((case-fold-search t))
  6377. (if (save-excursion
  6378. (beginning-of-line 1)
  6379. (looking-at org-block-regexp))
  6380. (progn (org-hide-block-toggle)
  6381. t) ;; to signal that we took action
  6382. nil))) ;; to signal that we did not
  6383. (defun org-hide-block-toggle (&optional force)
  6384. "Toggle the visibility of the current block."
  6385. (interactive)
  6386. (save-excursion
  6387. (beginning-of-line)
  6388. (if (re-search-forward org-block-regexp nil t)
  6389. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6390. (end (match-end 0)) ;; end of entire body
  6391. ov)
  6392. (if (memq t (mapcar (lambda (overlay)
  6393. (eq (overlay-get overlay 'invisible)
  6394. 'org-hide-block))
  6395. (overlays-at start)))
  6396. (if (or (not force) (eq force 'off))
  6397. (mapc (lambda (ov)
  6398. (when (member ov org-hide-block-overlays)
  6399. (setq org-hide-block-overlays
  6400. (delq ov org-hide-block-overlays)))
  6401. (when (eq (overlay-get ov 'invisible)
  6402. 'org-hide-block)
  6403. (delete-overlay ov)))
  6404. (overlays-at start)))
  6405. (setq ov (make-overlay start end))
  6406. (overlay-put ov 'invisible 'org-hide-block)
  6407. ;; make the block accessible to isearch
  6408. (overlay-put
  6409. ov 'isearch-open-invisible
  6410. (lambda (ov)
  6411. (when (member ov org-hide-block-overlays)
  6412. (setq org-hide-block-overlays
  6413. (delq ov org-hide-block-overlays)))
  6414. (when (eq (overlay-get ov 'invisible)
  6415. 'org-hide-block)
  6416. (delete-overlay ov))))
  6417. (push ov org-hide-block-overlays)))
  6418. (user-error "Not looking at a source block"))))
  6419. ;; org-tab-after-check-for-cycling-hook
  6420. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6421. ;; Remove overlays when changing major mode
  6422. (add-hook 'org-mode-hook
  6423. (lambda () (org-add-hook 'change-major-mode-hook
  6424. 'org-show-block-all 'append 'local)))
  6425. ;;; Org-goto
  6426. (defvar org-goto-window-configuration nil)
  6427. (defvar org-goto-marker nil)
  6428. (defvar org-goto-map)
  6429. (defun org-goto-map ()
  6430. "Set the keymap `org-goto'."
  6431. (setq org-goto-map
  6432. (let ((map (make-sparse-keymap)))
  6433. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6434. mouse-drag-region universal-argument org-occur))
  6435. cmd)
  6436. (while (setq cmd (pop cmds))
  6437. (substitute-key-definition cmd cmd map global-map)))
  6438. (suppress-keymap map)
  6439. (org-defkey map "\C-m" 'org-goto-ret)
  6440. (org-defkey map [(return)] 'org-goto-ret)
  6441. (org-defkey map [(left)] 'org-goto-left)
  6442. (org-defkey map [(right)] 'org-goto-right)
  6443. (org-defkey map [(control ?g)] 'org-goto-quit)
  6444. (org-defkey map "\C-i" 'org-cycle)
  6445. (org-defkey map [(tab)] 'org-cycle)
  6446. (org-defkey map [(down)] 'outline-next-visible-heading)
  6447. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6448. (if org-goto-auto-isearch
  6449. (if (fboundp 'define-key-after)
  6450. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6451. nil)
  6452. (org-defkey map "q" 'org-goto-quit)
  6453. (org-defkey map "n" 'outline-next-visible-heading)
  6454. (org-defkey map "p" 'outline-previous-visible-heading)
  6455. (org-defkey map "f" 'outline-forward-same-level)
  6456. (org-defkey map "b" 'outline-backward-same-level)
  6457. (org-defkey map "u" 'outline-up-heading))
  6458. (org-defkey map "/" 'org-occur)
  6459. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6460. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6461. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6462. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6463. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6464. map)))
  6465. (defconst org-goto-help
  6466. "Browse buffer copy, to find location or copy text.%s
  6467. RET=jump to location C-g=quit and return to previous location
  6468. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6469. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6470. (defun org-goto (&optional alternative-interface)
  6471. "Look up a different location in the current file, keeping current visibility.
  6472. When you want look-up or go to a different location in a
  6473. document, the fastest way is often to fold the entire buffer and
  6474. then dive into the tree. This method has the disadvantage, that
  6475. the previous location will be folded, which may not be what you
  6476. want.
  6477. This command works around this by showing a copy of the current
  6478. buffer in an indirect buffer, in overview mode. You can dive
  6479. into the tree in that copy, use org-occur and incremental search
  6480. to find a location. When pressing RET or `Q', the command
  6481. returns to the original buffer in which the visibility is still
  6482. unchanged. After RET it will also jump to the location selected
  6483. in the indirect buffer and expose the headline hierarchy above.
  6484. With a prefix argument, use the alternative interface: e.g. if
  6485. `org-goto-interface' is 'outline use 'outline-path-completion."
  6486. (interactive "P")
  6487. (org-goto-map)
  6488. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6489. (org-refile-use-outline-path t)
  6490. (org-refile-target-verify-function nil)
  6491. (interface
  6492. (if (not alternative-interface)
  6493. org-goto-interface
  6494. (if (eq org-goto-interface 'outline)
  6495. 'outline-path-completion
  6496. 'outline)))
  6497. (org-goto-start-pos (point))
  6498. (selected-point
  6499. (if (eq interface 'outline)
  6500. (car (org-get-location (current-buffer) org-goto-help))
  6501. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6502. (org-refile-check-position pa)
  6503. (nth 3 pa)))))
  6504. (if selected-point
  6505. (progn
  6506. (org-mark-ring-push org-goto-start-pos)
  6507. (goto-char selected-point)
  6508. (if (or (outline-invisible-p) (org-invisible-p2))
  6509. (org-show-context 'org-goto)))
  6510. (message "Quit"))))
  6511. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6512. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6513. (defvar org-goto-local-auto-isearch-map) ; defined below
  6514. (defun org-get-location (buf help)
  6515. "Let the user select a location in the Org-mode buffer BUF.
  6516. This function uses a recursive edit. It returns the selected position
  6517. or nil."
  6518. (org-no-popups
  6519. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6520. (isearch-hide-immediately nil)
  6521. (isearch-search-fun-function
  6522. (lambda () 'org-goto-local-search-headings))
  6523. (org-goto-selected-point org-goto-exit-command))
  6524. (save-excursion
  6525. (save-window-excursion
  6526. (delete-other-windows)
  6527. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6528. (org-pop-to-buffer-same-window
  6529. (condition-case nil
  6530. (make-indirect-buffer (current-buffer) "*org-goto*")
  6531. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6532. (with-output-to-temp-buffer "*Org Help*"
  6533. (princ (format help (if org-goto-auto-isearch
  6534. " Just type for auto-isearch."
  6535. " n/p/f/b/u to navigate, q to quit."))))
  6536. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6537. (setq buffer-read-only nil)
  6538. (let ((org-startup-truncated t)
  6539. (org-startup-folded nil)
  6540. (org-startup-align-all-tables nil))
  6541. (org-mode)
  6542. (org-overview))
  6543. (setq buffer-read-only t)
  6544. (if (and (boundp 'org-goto-start-pos)
  6545. (integer-or-marker-p org-goto-start-pos))
  6546. (let ((org-show-hierarchy-above t)
  6547. (org-show-siblings t)
  6548. (org-show-following-heading t))
  6549. (goto-char org-goto-start-pos)
  6550. (and (outline-invisible-p) (org-show-context)))
  6551. (goto-char (point-min)))
  6552. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6553. (message "Select location and press RET")
  6554. (use-local-map org-goto-map)
  6555. (recursive-edit)))
  6556. (kill-buffer "*org-goto*")
  6557. (cons org-goto-selected-point org-goto-exit-command))))
  6558. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6559. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6560. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6561. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6562. (defun org-goto-local-search-headings (string bound noerror)
  6563. "Search and make sure that any matches are in headlines."
  6564. (catch 'return
  6565. (while (if isearch-forward
  6566. (search-forward string bound noerror)
  6567. (search-backward string bound noerror))
  6568. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6569. (and (member :headline context)
  6570. (not (member :tags context))))
  6571. (throw 'return (point))))))
  6572. (defun org-goto-local-auto-isearch ()
  6573. "Start isearch."
  6574. (interactive)
  6575. (goto-char (point-min))
  6576. (let ((keys (this-command-keys)))
  6577. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6578. (isearch-mode t)
  6579. (isearch-process-search-char (string-to-char keys)))))
  6580. (defun org-goto-ret (&optional arg)
  6581. "Finish `org-goto' by going to the new location."
  6582. (interactive "P")
  6583. (setq org-goto-selected-point (point)
  6584. org-goto-exit-command 'return)
  6585. (throw 'exit nil))
  6586. (defun org-goto-left ()
  6587. "Finish `org-goto' by going to the new location."
  6588. (interactive)
  6589. (if (org-at-heading-p)
  6590. (progn
  6591. (beginning-of-line 1)
  6592. (setq org-goto-selected-point (point)
  6593. org-goto-exit-command 'left)
  6594. (throw 'exit nil))
  6595. (user-error "Not on a heading")))
  6596. (defun org-goto-right ()
  6597. "Finish `org-goto' by going to the new location."
  6598. (interactive)
  6599. (if (org-at-heading-p)
  6600. (progn
  6601. (setq org-goto-selected-point (point)
  6602. org-goto-exit-command 'right)
  6603. (throw 'exit nil))
  6604. (user-error "Not on a heading")))
  6605. (defun org-goto-quit ()
  6606. "Finish `org-goto' without cursor motion."
  6607. (interactive)
  6608. (setq org-goto-selected-point nil)
  6609. (setq org-goto-exit-command 'quit)
  6610. (throw 'exit nil))
  6611. ;;; Indirect buffer display of subtrees
  6612. (defvar org-indirect-dedicated-frame nil
  6613. "This is the frame being used for indirect tree display.")
  6614. (defvar org-last-indirect-buffer nil)
  6615. (defun org-tree-to-indirect-buffer (&optional arg)
  6616. "Create indirect buffer and narrow it to current subtree.
  6617. With a numerical prefix ARG, go up to this level and then take that tree.
  6618. If ARG is negative, go up that many levels.
  6619. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6620. indirect buffer previously made with this command, to avoid proliferation of
  6621. indirect buffers. However, when you call the command with a \
  6622. \\[universal-argument] prefix, or
  6623. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6624. is kept so that you can work with several indirect buffers at the same time.
  6625. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6626. \\[universal-argument] prefix also
  6627. requests that a new frame be made for the new buffer, so that the dedicated
  6628. frame is not changed."
  6629. (interactive "P")
  6630. (let ((cbuf (current-buffer))
  6631. (cwin (selected-window))
  6632. (pos (point))
  6633. beg end level heading ibuf)
  6634. (save-excursion
  6635. (org-back-to-heading t)
  6636. (when (numberp arg)
  6637. (setq level (org-outline-level))
  6638. (if (< arg 0) (setq arg (+ level arg)))
  6639. (while (> (setq level (org-outline-level)) arg)
  6640. (org-up-heading-safe)))
  6641. (setq beg (point)
  6642. heading (org-get-heading))
  6643. (org-end-of-subtree t t)
  6644. (if (org-at-heading-p) (backward-char 1))
  6645. (setq end (point)))
  6646. (if (and (buffer-live-p org-last-indirect-buffer)
  6647. (not (eq org-indirect-buffer-display 'new-frame))
  6648. (not arg))
  6649. (kill-buffer org-last-indirect-buffer))
  6650. (setq ibuf (org-get-indirect-buffer cbuf)
  6651. org-last-indirect-buffer ibuf)
  6652. (cond
  6653. ((or (eq org-indirect-buffer-display 'new-frame)
  6654. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6655. (select-frame (make-frame))
  6656. (delete-other-windows)
  6657. (org-pop-to-buffer-same-window ibuf)
  6658. (org-set-frame-title heading))
  6659. ((eq org-indirect-buffer-display 'dedicated-frame)
  6660. (raise-frame
  6661. (select-frame (or (and org-indirect-dedicated-frame
  6662. (frame-live-p org-indirect-dedicated-frame)
  6663. org-indirect-dedicated-frame)
  6664. (setq org-indirect-dedicated-frame (make-frame)))))
  6665. (delete-other-windows)
  6666. (org-pop-to-buffer-same-window ibuf)
  6667. (org-set-frame-title (concat "Indirect: " heading)))
  6668. ((eq org-indirect-buffer-display 'current-window)
  6669. (org-pop-to-buffer-same-window ibuf))
  6670. ((eq org-indirect-buffer-display 'other-window)
  6671. (pop-to-buffer ibuf))
  6672. (t (error "Invalid value")))
  6673. (if (featurep 'xemacs)
  6674. (save-excursion (org-mode) (turn-on-font-lock)))
  6675. (narrow-to-region beg end)
  6676. (show-all)
  6677. (goto-char pos)
  6678. (run-hook-with-args 'org-cycle-hook 'all)
  6679. (and (window-live-p cwin) (select-window cwin))))
  6680. (defun org-get-indirect-buffer (&optional buffer)
  6681. (setq buffer (or buffer (current-buffer)))
  6682. (let ((n 1) (base (buffer-name buffer)) bname)
  6683. (while (buffer-live-p
  6684. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6685. (setq n (1+ n)))
  6686. (condition-case nil
  6687. (make-indirect-buffer buffer bname 'clone)
  6688. (error (make-indirect-buffer buffer bname)))))
  6689. (defun org-set-frame-title (title)
  6690. "Set the title of the current frame to the string TITLE."
  6691. ;; FIXME: how to name a single frame in XEmacs???
  6692. (unless (featurep 'xemacs)
  6693. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6694. ;;;; Structure editing
  6695. ;;; Inserting headlines
  6696. (defun org-previous-line-empty-p (&optional next)
  6697. "Is the previous line a blank line?
  6698. When NEXT is non-nil, check the next line instead."
  6699. (save-excursion
  6700. (and (not (bobp))
  6701. (or (beginning-of-line (if next 2 0)) t)
  6702. (save-match-data
  6703. (looking-at "[ \t]*$")))))
  6704. (defun org-insert-heading (&optional arg invisible-ok)
  6705. "Insert a new heading or item with same depth at point.
  6706. If point is in a plain list and ARG is nil, create a new list item.
  6707. With one universal prefix argument, insert a heading even in lists.
  6708. With two universal prefix arguments, insert the heading at the end
  6709. of the parent subtree.
  6710. If point is at the beginning of a headline, insert a sibling before
  6711. the current headline. If point is not at the beginning, split the line
  6712. and create a new headline with the text in the current line after point
  6713. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6714. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6715. This is important for non-interactive uses of the command."
  6716. (interactive "P")
  6717. (if (org-called-interactively-p 'any) (org-reveal))
  6718. (let ((itemp (org-in-item-p)))
  6719. (cond
  6720. ((or (= (buffer-size) 0)
  6721. (and (not (save-excursion
  6722. (and (ignore-errors (org-back-to-heading invisible-ok))
  6723. (org-at-heading-p))))
  6724. (or arg (not itemp))))
  6725. (insert
  6726. (if (org-previous-line-empty-p) "" "\n")
  6727. (if (org-in-src-block-p) ",* " "* "))
  6728. (run-hooks 'org-insert-heading-hook))
  6729. ((or arg
  6730. (and (not itemp) org-insert-heading-respect-content)
  6731. (not (org-insert-item
  6732. (save-excursion
  6733. (and itemp
  6734. (goto-char itemp)
  6735. (looking-at org-list-full-item-re)
  6736. (match-string 3))))))
  6737. (let (begn endn)
  6738. (when (org-buffer-narrowed-p)
  6739. (setq begn (point-min) endn (point-max))
  6740. (widen))
  6741. (let* ((empty-line-p nil)
  6742. (eops (equal arg '(16))) ; insert at end of parent subtree
  6743. (org-insert-heading-respect-content
  6744. (or (not (null arg)) org-insert-heading-respect-content))
  6745. (level nil)
  6746. (on-heading (org-at-heading-p))
  6747. ;; Get a level to fall back on
  6748. (fix-level
  6749. (save-excursion
  6750. (org-back-to-heading t)
  6751. (looking-at org-outline-regexp)
  6752. (make-string (1- (length (match-string 0))) ?*)))
  6753. (on-empty-line
  6754. (save-excursion (beginning-of-line 1) (looking-at "^\\s-*$")))
  6755. (head (save-excursion
  6756. (condition-case nil
  6757. (progn
  6758. (org-back-to-heading invisible-ok)
  6759. (when (and (not on-heading)
  6760. (featurep 'org-inlinetask)
  6761. (integerp org-inlinetask-min-level)
  6762. (>= (length (match-string 0))
  6763. org-inlinetask-min-level))
  6764. ;; Find a heading level before the inline task
  6765. (while (and (setq level (org-up-heading-safe))
  6766. (>= level org-inlinetask-min-level)))
  6767. (if (org-at-heading-p)
  6768. (org-back-to-heading invisible-ok)
  6769. (error "This should not happen")))
  6770. (unless (and (save-excursion
  6771. (save-match-data
  6772. (org-backward-heading-same-level 1 invisible-ok))
  6773. (= (point) (match-beginning 0)))
  6774. (not (org-previous-line-empty-p t)))
  6775. (setq empty-line-p (org-previous-line-empty-p)))
  6776. (match-string 0))
  6777. (error (or fix-level "* ")))))
  6778. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6779. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6780. pos hide-previous previous-pos)
  6781. (if ;; At the beginning of a heading, open a new line for insertion
  6782. (and (bolp) (org-at-heading-p)
  6783. (not eops)
  6784. (or (bobp)
  6785. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6786. (open-line (if blank 2 1))
  6787. (save-excursion
  6788. (setq previous-pos (point-at-bol))
  6789. (end-of-line)
  6790. (setq hide-previous (outline-invisible-p)))
  6791. (and org-insert-heading-respect-content
  6792. (save-excursion
  6793. (while (outline-invisible-p)
  6794. (org-show-subtree)
  6795. (org-up-heading-safe))))
  6796. (let ((split
  6797. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6798. (save-excursion
  6799. (let ((p (point)))
  6800. (goto-char (point-at-bol))
  6801. (and (looking-at org-complex-heading-regexp)
  6802. (match-beginning 4)
  6803. (> p (match-beginning 4)))))))
  6804. tags pos)
  6805. (cond
  6806. ;; Insert a new line, possibly at end of parent subtree
  6807. ((and (not arg) (not on-heading) (not on-empty-line)
  6808. (not (save-excursion
  6809. (beginning-of-line 1)
  6810. (or (looking-at org-list-full-item-re)
  6811. ;; Don't convert :end: lines to headline
  6812. (looking-at "^\\s-*:end:")
  6813. (looking-at "^\\s-*#\\+end_?")))))
  6814. (beginning-of-line 1))
  6815. (org-insert-heading-respect-content
  6816. (if (not eops)
  6817. (progn
  6818. (org-end-of-subtree nil t)
  6819. (and (looking-at "^\\*") (backward-char 1))
  6820. (while (and (not (bobp))
  6821. ;; Don't delete spaces in empty headlines
  6822. (not (looking-back org-outline-regexp))
  6823. (member (char-before) '(?\ ?\t ?\n)))
  6824. (backward-delete-char 1)))
  6825. (let ((p (point)))
  6826. (org-up-heading-safe)
  6827. (if (= p (point))
  6828. (goto-char (point-max))
  6829. (org-end-of-subtree nil t))))
  6830. (when (featurep 'org-inlinetask)
  6831. (while (and (not (eobp))
  6832. (looking-at "\\(\\*+\\)[ \t]+")
  6833. (>= (length (match-string 1))
  6834. org-inlinetask-min-level))
  6835. (org-end-of-subtree nil t)))
  6836. (or (bolp) (newline))
  6837. (or (org-previous-line-empty-p)
  6838. (and blank (newline)))
  6839. (if (or empty-line-p eops) (open-line 1)))
  6840. ;; Insert a headling containing text after point
  6841. ((org-at-heading-p)
  6842. (when hide-previous
  6843. (show-children)
  6844. (org-show-entry))
  6845. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6846. (setq tags (and (match-end 2) (match-string 2)))
  6847. (and (match-end 1)
  6848. (delete-region (match-beginning 1) (match-end 1)))
  6849. (setq pos (point-at-bol))
  6850. (or split (end-of-line 1))
  6851. (delete-horizontal-space)
  6852. (if (string-match "\\`\\*+\\'"
  6853. (buffer-substring (point-at-bol) (point)))
  6854. (insert " "))
  6855. (newline (if blank 2 1))
  6856. (when tags
  6857. (save-excursion
  6858. (goto-char pos)
  6859. (end-of-line 1)
  6860. (insert " " tags)
  6861. (org-set-tags nil 'align))))
  6862. (t
  6863. (or split (end-of-line 1))
  6864. (newline (cond ((and blank (not on-empty-line)) 2)
  6865. (blank 1)
  6866. (on-empty-line 0) (t 1)))))))
  6867. (insert head) (just-one-space)
  6868. (setq pos (point))
  6869. (end-of-line 1)
  6870. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6871. (when (and org-insert-heading-respect-content hide-previous)
  6872. (save-excursion
  6873. (goto-char previous-pos)
  6874. (hide-subtree)))
  6875. (when (and begn endn)
  6876. (narrow-to-region (min (point) begn) (max (point) endn)))
  6877. (run-hooks 'org-insert-heading-hook)))))))
  6878. (defun org-get-heading (&optional no-tags no-todo)
  6879. "Return the heading of the current entry, without the stars.
  6880. When NO-TAGS is non-nil, don't include tags.
  6881. When NO-TODO is non-nil, don't include TODO keywords."
  6882. (save-excursion
  6883. (org-back-to-heading t)
  6884. (cond
  6885. ((and no-tags no-todo)
  6886. (looking-at org-complex-heading-regexp)
  6887. (match-string 4))
  6888. (no-tags
  6889. (looking-at (concat org-outline-regexp
  6890. "\\(.*?\\)"
  6891. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6892. (match-string 1))
  6893. (no-todo
  6894. (looking-at org-todo-line-regexp)
  6895. (match-string 3))
  6896. (t (looking-at org-heading-regexp)
  6897. (match-string 2)))))
  6898. (defvar orgstruct-mode) ; defined below
  6899. (defun org-heading-components ()
  6900. "Return the components of the current heading.
  6901. This is a list with the following elements:
  6902. - the level as an integer
  6903. - the reduced level, different if `org-odd-levels-only' is set.
  6904. - the TODO keyword, or nil
  6905. - the priority character, like ?A, or nil if no priority is given
  6906. - the headline text itself, or the tags string if no headline text
  6907. - the tags string, or nil."
  6908. (save-excursion
  6909. (org-back-to-heading t)
  6910. (if (let (case-fold-search)
  6911. (looking-at
  6912. (if orgstruct-mode
  6913. org-heading-regexp
  6914. org-complex-heading-regexp)))
  6915. (if orgstruct-mode
  6916. (list (length (match-string 1))
  6917. (org-reduced-level (length (match-string 1)))
  6918. nil
  6919. nil
  6920. (match-string 2)
  6921. nil)
  6922. (list (length (match-string 1))
  6923. (org-reduced-level (length (match-string 1)))
  6924. (org-match-string-no-properties 2)
  6925. (and (match-end 3) (aref (match-string 3) 2))
  6926. (org-match-string-no-properties 4)
  6927. (org-match-string-no-properties 5))))))
  6928. (defun org-get-entry ()
  6929. "Get the entry text, after heading, entire subtree."
  6930. (save-excursion
  6931. (org-back-to-heading t)
  6932. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6933. (defun org-insert-heading-after-current ()
  6934. "Insert a new heading with same level as current, after current subtree."
  6935. (interactive)
  6936. (org-back-to-heading)
  6937. (org-insert-heading)
  6938. (org-move-subtree-down)
  6939. (end-of-line 1))
  6940. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6941. "Insert heading with `org-insert-heading-respect-content' set to t."
  6942. (interactive "P")
  6943. (let ((org-insert-heading-respect-content t))
  6944. (org-insert-heading arg invisible-ok)))
  6945. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6946. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6947. (interactive "P")
  6948. (let ((org-insert-heading-respect-content t))
  6949. (org-insert-todo-heading force-state t)))
  6950. (defun org-insert-todo-heading (arg &optional force-heading)
  6951. "Insert a new heading with the same level and TODO state as current heading.
  6952. If the heading has no TODO state, or if the state is DONE, use the first
  6953. state (TODO by default). Also one prefix arg, force first state. With two
  6954. prefix args, force inserting at the end of the parent subtree."
  6955. (interactive "P")
  6956. (when (or force-heading (not (org-insert-item 'checkbox)))
  6957. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6958. force-heading))
  6959. (save-excursion
  6960. (org-back-to-heading)
  6961. (outline-previous-heading)
  6962. (looking-at org-todo-line-regexp))
  6963. (let*
  6964. ((new-mark-x
  6965. (if (or arg
  6966. (not (match-beginning 2))
  6967. (member (match-string 2) org-done-keywords))
  6968. (car org-todo-keywords-1)
  6969. (match-string 2)))
  6970. (new-mark
  6971. (or
  6972. (run-hook-with-args-until-success
  6973. 'org-todo-get-default-hook new-mark-x nil)
  6974. new-mark-x)))
  6975. (beginning-of-line 1)
  6976. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6977. (if org-treat-insert-todo-heading-as-state-change
  6978. (org-todo new-mark)
  6979. (insert new-mark " "))))
  6980. (when org-provide-todo-statistics
  6981. (org-update-parent-todo-statistics))))
  6982. (defun org-insert-subheading (arg)
  6983. "Insert a new subheading and demote it.
  6984. Works for outline headings and for plain lists alike."
  6985. (interactive "P")
  6986. (org-insert-heading arg)
  6987. (cond
  6988. ((org-at-heading-p) (org-do-demote))
  6989. ((org-at-item-p) (org-indent-item))))
  6990. (defun org-insert-todo-subheading (arg)
  6991. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6992. Works for outline headings and for plain lists alike."
  6993. (interactive "P")
  6994. (org-insert-todo-heading arg)
  6995. (cond
  6996. ((org-at-heading-p) (org-do-demote))
  6997. ((org-at-item-p) (org-indent-item))))
  6998. ;;; Promotion and Demotion
  6999. (defvar org-after-demote-entry-hook nil
  7000. "Hook run after an entry has been demoted.
  7001. The cursor will be at the beginning of the entry.
  7002. When a subtree is being demoted, the hook will be called for each node.")
  7003. (defvar org-after-promote-entry-hook nil
  7004. "Hook run after an entry has been promoted.
  7005. The cursor will be at the beginning of the entry.
  7006. When a subtree is being promoted, the hook will be called for each node.")
  7007. (defun org-promote-subtree ()
  7008. "Promote the entire subtree.
  7009. See also `org-promote'."
  7010. (interactive)
  7011. (save-excursion
  7012. (org-with-limited-levels (org-map-tree 'org-promote)))
  7013. (org-fix-position-after-promote))
  7014. (defun org-demote-subtree ()
  7015. "Demote the entire subtree. See `org-demote'.
  7016. See also `org-promote'."
  7017. (interactive)
  7018. (save-excursion
  7019. (org-with-limited-levels (org-map-tree 'org-demote)))
  7020. (org-fix-position-after-promote))
  7021. (defun org-do-promote ()
  7022. "Promote the current heading higher up the tree.
  7023. If the region is active in `transient-mark-mode', promote all headings
  7024. in the region."
  7025. (interactive)
  7026. (save-excursion
  7027. (if (org-region-active-p)
  7028. (org-map-region 'org-promote (region-beginning) (region-end))
  7029. (org-promote)))
  7030. (org-fix-position-after-promote))
  7031. (defun org-do-demote ()
  7032. "Demote the current heading lower down the tree.
  7033. If the region is active in `transient-mark-mode', demote all headings
  7034. in the region."
  7035. (interactive)
  7036. (save-excursion
  7037. (if (org-region-active-p)
  7038. (org-map-region 'org-demote (region-beginning) (region-end))
  7039. (org-demote)))
  7040. (org-fix-position-after-promote))
  7041. (defun org-fix-position-after-promote ()
  7042. "Make sure that after pro/demotion cursor position is right."
  7043. (let ((pos (point)))
  7044. (when (save-excursion
  7045. (beginning-of-line 1)
  7046. (looking-at org-todo-line-regexp)
  7047. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7048. (cond ((eobp) (insert " "))
  7049. ((eolp) (insert " "))
  7050. ((equal (char-after) ?\ ) (forward-char 1))))))
  7051. (defun org-current-level ()
  7052. "Return the level of the current entry, or nil if before the first headline.
  7053. The level is the number of stars at the beginning of the headline."
  7054. (save-excursion
  7055. (org-with-limited-levels
  7056. (if (ignore-errors (org-back-to-heading t))
  7057. (funcall outline-level)))))
  7058. (defun org-get-previous-line-level ()
  7059. "Return the outline depth of the last headline before the current line.
  7060. Returns 0 for the first headline in the buffer, and nil if before the
  7061. first headline."
  7062. (let ((current-level (org-current-level))
  7063. (prev-level (when (> (line-number-at-pos) 1)
  7064. (save-excursion
  7065. (beginning-of-line 0)
  7066. (org-current-level)))))
  7067. (cond ((null current-level) nil) ; Before first headline
  7068. ((null prev-level) 0) ; At first headline
  7069. (prev-level))))
  7070. (defun org-reduced-level (l)
  7071. "Compute the effective level of a heading.
  7072. This takes into account the setting of `org-odd-levels-only'."
  7073. (cond
  7074. ((zerop l) 0)
  7075. (org-odd-levels-only (1+ (floor (/ l 2))))
  7076. (t l)))
  7077. (defun org-level-increment ()
  7078. "Return the number of stars that will be added or removed at a
  7079. time to headlines when structure editing, based on the value of
  7080. `org-odd-levels-only'."
  7081. (if org-odd-levels-only 2 1))
  7082. (defun org-get-valid-level (level &optional change)
  7083. "Rectify a level change under the influence of `org-odd-levels-only'
  7084. LEVEL is a current level, CHANGE is by how much the level should be
  7085. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7086. even level numbers will become the next higher odd number."
  7087. (if org-odd-levels-only
  7088. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7089. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7090. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7091. (max 1 (+ level (or change 0)))))
  7092. (if (boundp 'define-obsolete-function-alias)
  7093. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7094. (define-obsolete-function-alias 'org-get-legal-level
  7095. 'org-get-valid-level)
  7096. (define-obsolete-function-alias 'org-get-legal-level
  7097. 'org-get-valid-level "23.1")))
  7098. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  7099. ;; ̀org-with-limited-levels'
  7100. (defun org-promote ()
  7101. "Promote the current heading higher up the tree.
  7102. If the region is active in `transient-mark-mode', promote all headings
  7103. in the region."
  7104. (org-back-to-heading t)
  7105. (let* ((level (save-match-data (funcall outline-level)))
  7106. (after-change-functions (remove 'flyspell-after-change-function
  7107. after-change-functions))
  7108. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7109. (diff (abs (- level (length up-head) -1))))
  7110. (cond ((and (= level 1) org-called-with-limited-levels
  7111. org-allow-promoting-top-level-subtree)
  7112. (replace-match "# " nil t))
  7113. ((= level 1)
  7114. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7115. (t (replace-match up-head nil t)))
  7116. ;; Fixup tag positioning
  7117. (unless (= level 1)
  7118. (and org-auto-align-tags (org-set-tags nil t))
  7119. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7120. (run-hooks 'org-after-promote-entry-hook)))
  7121. (defun org-demote ()
  7122. "Demote the current heading lower down the tree.
  7123. If the region is active in `transient-mark-mode', demote all headings
  7124. in the region."
  7125. (org-back-to-heading t)
  7126. (let* ((level (save-match-data (funcall outline-level)))
  7127. (after-change-functions (remove 'flyspell-after-change-function
  7128. after-change-functions))
  7129. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7130. (diff (abs (- level (length down-head) -1))))
  7131. (replace-match down-head nil t)
  7132. ;; Fixup tag positioning
  7133. (and org-auto-align-tags (org-set-tags nil t))
  7134. (if org-adapt-indentation (org-fixup-indentation diff))
  7135. (run-hooks 'org-after-demote-entry-hook)))
  7136. (defun org-cycle-level ()
  7137. "Cycle the level of an empty headline through possible states.
  7138. This goes first to child, then to parent, level, then up the hierarchy.
  7139. After top level, it switches back to sibling level."
  7140. (interactive)
  7141. (let ((org-adapt-indentation nil))
  7142. (when (org-point-at-end-of-empty-headline)
  7143. (setq this-command 'org-cycle-level) ; Only needed for caching
  7144. (let ((cur-level (org-current-level))
  7145. (prev-level (org-get-previous-line-level)))
  7146. (cond
  7147. ;; If first headline in file, promote to top-level.
  7148. ((= prev-level 0)
  7149. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7150. do (org-do-promote)))
  7151. ;; If same level as prev, demote one.
  7152. ((= prev-level cur-level)
  7153. (org-do-demote))
  7154. ;; If parent is top-level, promote to top level if not already.
  7155. ((= prev-level 1)
  7156. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7157. do (org-do-promote)))
  7158. ;; If top-level, return to prev-level.
  7159. ((= cur-level 1)
  7160. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7161. do (org-do-demote)))
  7162. ;; If less than prev-level, promote one.
  7163. ((< cur-level prev-level)
  7164. (org-do-promote))
  7165. ;; If deeper than prev-level, promote until higher than
  7166. ;; prev-level.
  7167. ((> cur-level prev-level)
  7168. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7169. do (org-do-promote))))
  7170. t))))
  7171. (defun org-map-tree (fun)
  7172. "Call FUN for every heading underneath the current one."
  7173. (org-back-to-heading)
  7174. (let ((level (funcall outline-level)))
  7175. (save-excursion
  7176. (funcall fun)
  7177. (while (and (progn
  7178. (outline-next-heading)
  7179. (> (funcall outline-level) level))
  7180. (not (eobp)))
  7181. (funcall fun)))))
  7182. (defun org-map-region (fun beg end)
  7183. "Call FUN for every heading between BEG and END."
  7184. (let ((org-ignore-region t))
  7185. (save-excursion
  7186. (setq end (copy-marker end))
  7187. (goto-char beg)
  7188. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7189. (< (point) end))
  7190. (funcall fun))
  7191. (while (and (progn
  7192. (outline-next-heading)
  7193. (< (point) end))
  7194. (not (eobp)))
  7195. (funcall fun)))))
  7196. (defvar org-property-end-re) ; silence byte-compiler
  7197. (defun org-fixup-indentation (diff)
  7198. "Change the indentation in the current entry by DIFF.
  7199. However, if any line in the current entry has no indentation, or if it
  7200. would end up with no indentation after the change, nothing at all is done."
  7201. (save-excursion
  7202. (let ((end (save-excursion (outline-next-heading)
  7203. (point-marker)))
  7204. (prohibit (if (> diff 0)
  7205. "^\\S-"
  7206. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7207. col)
  7208. (unless (save-excursion (end-of-line 1)
  7209. (re-search-forward prohibit end t))
  7210. (while (and (< (point) end)
  7211. (re-search-forward "^[ \t]+" end t))
  7212. (goto-char (match-end 0))
  7213. (setq col (current-column))
  7214. (if (< diff 0) (replace-match ""))
  7215. (org-indent-to-column (+ diff col))))
  7216. (move-marker end nil))))
  7217. (defun org-convert-to-odd-levels ()
  7218. "Convert an org-mode file with all levels allowed to one with odd levels.
  7219. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7220. level 5 etc."
  7221. (interactive)
  7222. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7223. (let ((outline-level 'org-outline-level)
  7224. (org-odd-levels-only nil) n)
  7225. (save-excursion
  7226. (goto-char (point-min))
  7227. (while (re-search-forward "^\\*\\*+ " nil t)
  7228. (setq n (- (length (match-string 0)) 2))
  7229. (while (>= (setq n (1- n)) 0)
  7230. (org-demote))
  7231. (end-of-line 1))))))
  7232. (defun org-convert-to-oddeven-levels ()
  7233. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7234. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7235. file contains a section with an even level, conversion would
  7236. destroy the structure of the file. An error is signaled in this
  7237. case."
  7238. (interactive)
  7239. (goto-char (point-min))
  7240. ;; First check if there are no even levels
  7241. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7242. (org-show-context t)
  7243. (error "Not all levels are odd in this file. Conversion not possible"))
  7244. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7245. (let ((outline-regexp org-outline-regexp)
  7246. (outline-level 'org-outline-level)
  7247. (org-odd-levels-only nil) n)
  7248. (save-excursion
  7249. (goto-char (point-min))
  7250. (while (re-search-forward "^\\*\\*+ " nil t)
  7251. (setq n (/ (1- (length (match-string 0))) 2))
  7252. (while (>= (setq n (1- n)) 0)
  7253. (org-promote))
  7254. (end-of-line 1))))))
  7255. (defun org-tr-level (n)
  7256. "Make N odd if required."
  7257. (if org-odd-levels-only (1+ (/ n 2)) n))
  7258. ;;; Vertical tree motion, cutting and pasting of subtrees
  7259. (defun org-move-subtree-up (&optional arg)
  7260. "Move the current subtree up past ARG headlines of the same level."
  7261. (interactive "p")
  7262. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7263. (defun org-move-subtree-down (&optional arg)
  7264. "Move the current subtree down past ARG headlines of the same level."
  7265. (interactive "p")
  7266. (setq arg (prefix-numeric-value arg))
  7267. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7268. 'org-get-last-sibling))
  7269. (ins-point (make-marker))
  7270. (cnt (abs arg))
  7271. (col (current-column))
  7272. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7273. ;; Select the tree
  7274. (org-back-to-heading)
  7275. (setq beg0 (point))
  7276. (save-excursion
  7277. (setq ne-beg (org-back-over-empty-lines))
  7278. (setq beg (point)))
  7279. (save-match-data
  7280. (save-excursion (outline-end-of-heading)
  7281. (setq folded (outline-invisible-p)))
  7282. (outline-end-of-subtree))
  7283. (outline-next-heading)
  7284. (setq ne-end (org-back-over-empty-lines))
  7285. (setq end (point))
  7286. (goto-char beg0)
  7287. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7288. ;; include less whitespace
  7289. (save-excursion
  7290. (goto-char beg)
  7291. (forward-line (- ne-beg ne-end))
  7292. (setq beg (point))))
  7293. ;; Find insertion point, with error handling
  7294. (while (> cnt 0)
  7295. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7296. (progn (goto-char beg0)
  7297. (user-error "Cannot move past superior level or buffer limit")))
  7298. (setq cnt (1- cnt)))
  7299. (if (> arg 0)
  7300. ;; Moving forward - still need to move over subtree
  7301. (progn (org-end-of-subtree t t)
  7302. (save-excursion
  7303. (org-back-over-empty-lines)
  7304. (or (bolp) (newline)))))
  7305. (setq ne-ins (org-back-over-empty-lines))
  7306. (move-marker ins-point (point))
  7307. (setq txt (buffer-substring beg end))
  7308. (org-save-markers-in-region beg end)
  7309. (delete-region beg end)
  7310. (org-remove-empty-overlays-at beg)
  7311. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7312. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7313. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7314. (let ((bbb (point)))
  7315. (insert-before-markers txt)
  7316. (org-reinstall-markers-in-region bbb)
  7317. (move-marker ins-point bbb))
  7318. (or (bolp) (insert "\n"))
  7319. (setq ins-end (point))
  7320. (goto-char ins-point)
  7321. (org-skip-whitespace)
  7322. (when (and (< arg 0)
  7323. (org-first-sibling-p)
  7324. (> ne-ins ne-beg))
  7325. ;; Move whitespace back to beginning
  7326. (save-excursion
  7327. (goto-char ins-end)
  7328. (let ((kill-whole-line t))
  7329. (kill-line (- ne-ins ne-beg)) (point)))
  7330. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7331. (move-marker ins-point nil)
  7332. (if folded
  7333. (hide-subtree)
  7334. (org-show-entry)
  7335. (show-children)
  7336. (org-cycle-hide-drawers 'children))
  7337. (org-clean-visibility-after-subtree-move)
  7338. ;; move back to the initial column we were at
  7339. (move-to-column col)))
  7340. (defvar org-subtree-clip ""
  7341. "Clipboard for cut and paste of subtrees.
  7342. This is actually only a copy of the kill, because we use the normal kill
  7343. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7344. (defvar org-subtree-clip-folded nil
  7345. "Was the last copied subtree folded?
  7346. This is used to fold the tree back after pasting.")
  7347. (defun org-cut-subtree (&optional n)
  7348. "Cut the current subtree into the clipboard.
  7349. With prefix arg N, cut this many sequential subtrees.
  7350. This is a short-hand for marking the subtree and then cutting it."
  7351. (interactive "p")
  7352. (org-copy-subtree n 'cut))
  7353. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7354. "Cut the current subtree into the clipboard.
  7355. With prefix arg N, cut this many sequential subtrees.
  7356. This is a short-hand for marking the subtree and then copying it.
  7357. If CUT is non-nil, actually cut the subtree.
  7358. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7359. of some markers in the region, even if CUT is non-nil. This is
  7360. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7361. (interactive "p")
  7362. (let (beg end folded (beg0 (point)))
  7363. (if (org-called-interactively-p 'any)
  7364. (org-back-to-heading nil) ; take what looks like a subtree
  7365. (org-back-to-heading t)) ; take what is really there
  7366. (setq beg (point))
  7367. (skip-chars-forward " \t\r\n")
  7368. (save-match-data
  7369. (if nosubtrees
  7370. (outline-next-heading)
  7371. (save-excursion (outline-end-of-heading)
  7372. (setq folded (outline-invisible-p)))
  7373. (condition-case nil
  7374. (org-forward-heading-same-level (1- n) t)
  7375. (error nil))
  7376. (org-end-of-subtree t t)))
  7377. (setq end (point))
  7378. (goto-char beg0)
  7379. (when (> end beg)
  7380. (setq org-subtree-clip-folded folded)
  7381. (when (or cut force-store-markers)
  7382. (org-save-markers-in-region beg end))
  7383. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7384. (setq org-subtree-clip (current-kill 0))
  7385. (message "%s: Subtree(s) with %d characters"
  7386. (if cut "Cut" "Copied")
  7387. (length org-subtree-clip)))))
  7388. (defun org-paste-subtree (&optional level tree for-yank)
  7389. "Paste the clipboard as a subtree, with modification of headline level.
  7390. The entire subtree is promoted or demoted in order to match a new headline
  7391. level.
  7392. If the cursor is at the beginning of a headline, the same level as
  7393. that headline is used to paste the tree.
  7394. If not, the new level is derived from the *visible* headings
  7395. before and after the insertion point, and taken to be the inferior headline
  7396. level of the two. So if the previous visible heading is level 3 and the
  7397. next is level 4 (or vice versa), level 4 will be used for insertion.
  7398. This makes sure that the subtree remains an independent subtree and does
  7399. not swallow low level entries.
  7400. You can also force a different level, either by using a numeric prefix
  7401. argument, or by inserting the heading marker by hand. For example, if the
  7402. cursor is after \"*****\", then the tree will be shifted to level 5.
  7403. If optional TREE is given, use this text instead of the kill ring.
  7404. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7405. move back over whitespace before inserting, and move point to the end of
  7406. the inserted text when done."
  7407. (interactive "P")
  7408. (setq tree (or tree (and kill-ring (current-kill 0))))
  7409. (unless (org-kill-is-subtree-p tree)
  7410. (user-error "%s"
  7411. (substitute-command-keys
  7412. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7413. (org-with-limited-levels
  7414. (let* ((visp (not (outline-invisible-p)))
  7415. (txt tree)
  7416. (^re_ "\\(\\*+\\)[ \t]*")
  7417. (old-level (if (string-match org-outline-regexp-bol txt)
  7418. (- (match-end 0) (match-beginning 0) 1)
  7419. -1))
  7420. (force-level (cond (level (prefix-numeric-value level))
  7421. ((and (looking-at "[ \t]*$")
  7422. (string-match
  7423. "^\\*+$" (buffer-substring
  7424. (point-at-bol) (point))))
  7425. (- (match-end 1) (match-beginning 1)))
  7426. ((and (bolp)
  7427. (looking-at org-outline-regexp))
  7428. (- (match-end 0) (point) 1))))
  7429. (previous-level (save-excursion
  7430. (condition-case nil
  7431. (progn
  7432. (outline-previous-visible-heading 1)
  7433. (if (looking-at ^re_)
  7434. (- (match-end 0) (match-beginning 0) 1)
  7435. 1))
  7436. (error 1))))
  7437. (next-level (save-excursion
  7438. (condition-case nil
  7439. (progn
  7440. (or (looking-at org-outline-regexp)
  7441. (outline-next-visible-heading 1))
  7442. (if (looking-at ^re_)
  7443. (- (match-end 0) (match-beginning 0) 1)
  7444. 1))
  7445. (error 1))))
  7446. (new-level (or force-level (max previous-level next-level)))
  7447. (shift (if (or (= old-level -1)
  7448. (= new-level -1)
  7449. (= old-level new-level))
  7450. 0
  7451. (- new-level old-level)))
  7452. (delta (if (> shift 0) -1 1))
  7453. (func (if (> shift 0) 'org-demote 'org-promote))
  7454. (org-odd-levels-only nil)
  7455. beg end newend)
  7456. ;; Remove the forced level indicator
  7457. (if force-level
  7458. (delete-region (point-at-bol) (point)))
  7459. ;; Paste
  7460. (beginning-of-line (if (bolp) 1 2))
  7461. (setq beg (point))
  7462. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7463. (insert-before-markers txt)
  7464. (unless (string-match "\n\\'" txt) (insert "\n"))
  7465. (setq newend (point))
  7466. (org-reinstall-markers-in-region beg)
  7467. (setq end (point))
  7468. (goto-char beg)
  7469. (skip-chars-forward " \t\n\r")
  7470. (setq beg (point))
  7471. (if (and (outline-invisible-p) visp)
  7472. (save-excursion (outline-show-heading)))
  7473. ;; Shift if necessary
  7474. (unless (= shift 0)
  7475. (save-restriction
  7476. (narrow-to-region beg end)
  7477. (while (not (= shift 0))
  7478. (org-map-region func (point-min) (point-max))
  7479. (setq shift (+ delta shift)))
  7480. (goto-char (point-min))
  7481. (setq newend (point-max))))
  7482. (when (or (org-called-interactively-p 'interactive) for-yank)
  7483. (message "Clipboard pasted as level %d subtree" new-level))
  7484. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7485. kill-ring
  7486. (eq org-subtree-clip (current-kill 0))
  7487. org-subtree-clip-folded)
  7488. ;; The tree was folded before it was killed/copied
  7489. (hide-subtree))
  7490. (and for-yank (goto-char newend)))))
  7491. (defun org-kill-is-subtree-p (&optional txt)
  7492. "Check if the current kill is an outline subtree, or a set of trees.
  7493. Returns nil if kill does not start with a headline, or if the first
  7494. headline level is not the largest headline level in the tree.
  7495. So this will actually accept several entries of equal levels as well,
  7496. which is OK for `org-paste-subtree'.
  7497. If optional TXT is given, check this string instead of the current kill."
  7498. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7499. (re (org-get-limited-outline-regexp))
  7500. (^re (concat "^" re))
  7501. (start-level (and kill
  7502. (string-match
  7503. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7504. kill)
  7505. (- (match-end 2) (match-beginning 2) 1)))
  7506. (start (1+ (or (match-beginning 2) -1))))
  7507. (if (not start-level)
  7508. (progn
  7509. nil) ;; does not even start with a heading
  7510. (catch 'exit
  7511. (while (setq start (string-match ^re kill (1+ start)))
  7512. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7513. (throw 'exit nil)))
  7514. t))))
  7515. (defvar org-markers-to-move nil
  7516. "Markers that should be moved with a cut-and-paste operation.
  7517. Those markers are stored together with their positions relative to
  7518. the start of the region.")
  7519. (defun org-save-markers-in-region (beg end)
  7520. "Check markers in region.
  7521. If these markers are between BEG and END, record their position relative
  7522. to BEG, so that after moving the block of text, we can put the markers back
  7523. into place.
  7524. This function gets called just before an entry or tree gets cut from the
  7525. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7526. called immediately, to move the markers with the entries."
  7527. (setq org-markers-to-move nil)
  7528. (when (featurep 'org-clock)
  7529. (org-clock-save-markers-for-cut-and-paste beg end))
  7530. (when (featurep 'org-agenda)
  7531. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7532. (defun org-check-and-save-marker (marker beg end)
  7533. "Check if MARKER is between BEG and END.
  7534. If yes, remember the marker and the distance to BEG."
  7535. (when (and (marker-buffer marker)
  7536. (equal (marker-buffer marker) (current-buffer)))
  7537. (if (and (>= marker beg) (< marker end))
  7538. (push (cons marker (- marker beg)) org-markers-to-move))))
  7539. (defun org-reinstall-markers-in-region (beg)
  7540. "Move all remembered markers to their position relative to BEG."
  7541. (mapc (lambda (x)
  7542. (move-marker (car x) (+ beg (cdr x))))
  7543. org-markers-to-move)
  7544. (setq org-markers-to-move nil))
  7545. (defun org-narrow-to-subtree ()
  7546. "Narrow buffer to the current subtree."
  7547. (interactive)
  7548. (save-excursion
  7549. (save-match-data
  7550. (org-with-limited-levels
  7551. (narrow-to-region
  7552. (progn (org-back-to-heading t) (point))
  7553. (progn (org-end-of-subtree t t)
  7554. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7555. (point)))))))
  7556. (defun org-narrow-to-block ()
  7557. "Narrow buffer to the current block."
  7558. (interactive)
  7559. (let* ((case-fold-search t)
  7560. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7561. "^[ \t]*#\\+end_.*")))
  7562. (if blockp
  7563. (narrow-to-region (car blockp) (cdr blockp))
  7564. (user-error "Not in a block"))))
  7565. (eval-when-compile
  7566. (defvar org-property-drawer-re))
  7567. (defvar org-property-start-re) ;; defined below
  7568. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7569. "Clone the task (subtree) at point N times.
  7570. The clones will be inserted as siblings.
  7571. In interactive use, the user will be prompted for the number of
  7572. clones to be produced. If the entry has a timestamp, the user
  7573. will also be prompted for a time shift, which may be a repeater
  7574. as used in time stamps, for example `+3d'. To disable this,
  7575. you can call the function with a universal prefix argument.
  7576. When a valid repeater is given and the entry contains any time
  7577. stamps, the clones will become a sequence in time, with time
  7578. stamps in the subtree shifted for each clone produced. If SHIFT
  7579. is nil or the empty string, time stamps will be left alone. The
  7580. ID property of the original subtree is removed.
  7581. If the original subtree did contain time stamps with a repeater,
  7582. the following will happen:
  7583. - the repeater will be removed in each clone
  7584. - an additional clone will be produced, with the current, unshifted
  7585. date(s) in the entry.
  7586. - the original entry will be placed *after* all the clones, with
  7587. repeater intact.
  7588. - the start days in the repeater in the original entry will be shifted
  7589. to past the last clone.
  7590. In this way you can spell out a number of instances of a repeating task,
  7591. and still retain the repeater to cover future instances of the task."
  7592. (interactive "nNumber of clones to produce: ")
  7593. (let ((shift
  7594. (or shift
  7595. (if (and (not (equal current-prefix-arg '(4)))
  7596. (save-excursion
  7597. (re-search-forward org-ts-regexp-both
  7598. (save-excursion
  7599. (org-end-of-subtree t)
  7600. (point)) t)))
  7601. (read-from-minibuffer
  7602. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7603. ""))) ;; No time shift
  7604. (n-no-remove -1)
  7605. (drawer-re org-drawer-regexp)
  7606. beg end template task idprop
  7607. shift-n shift-what doshift nmin nmax)
  7608. (if (not (and (integerp n) (> n 0)))
  7609. (error "Invalid number of replications %s" n))
  7610. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7611. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7612. shift)))
  7613. (error "Invalid shift specification %s" shift))
  7614. (when doshift
  7615. (setq shift-n (string-to-number (match-string 1 shift))
  7616. shift-what (cdr (assoc (match-string 2 shift)
  7617. '(("d" . day) ("w" . week)
  7618. ("m" . month) ("y" . year))))))
  7619. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7620. (setq nmin 1 nmax n)
  7621. (org-back-to-heading t)
  7622. (setq beg (point))
  7623. (setq idprop (org-entry-get nil "ID"))
  7624. (org-end-of-subtree t t)
  7625. (or (bolp) (insert "\n"))
  7626. (setq end (point))
  7627. (setq template (buffer-substring beg end))
  7628. (when (and doshift
  7629. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7630. (delete-region beg end)
  7631. (setq end beg)
  7632. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7633. (goto-char end)
  7634. (loop for n from nmin to nmax do
  7635. ;; prepare clone
  7636. (with-temp-buffer
  7637. (insert template)
  7638. (org-mode)
  7639. (goto-char (point-min))
  7640. (org-show-subtree)
  7641. (and idprop (if org-clone-delete-id
  7642. (org-entry-delete nil "ID")
  7643. (org-id-get-create t)))
  7644. (unless (= n 0)
  7645. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7646. (kill-whole-line))
  7647. (goto-char (point-min))
  7648. (while (re-search-forward drawer-re nil t)
  7649. (mapc (lambda (d)
  7650. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7651. (goto-char (point-min))
  7652. (when doshift
  7653. (while (re-search-forward org-ts-regexp-both nil t)
  7654. (org-timestamp-change (* n shift-n) shift-what))
  7655. (unless (= n n-no-remove)
  7656. (goto-char (point-min))
  7657. (while (re-search-forward org-ts-regexp nil t)
  7658. (save-excursion
  7659. (goto-char (match-beginning 0))
  7660. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7661. (delete-region (match-beginning 1) (match-end 1)))))))
  7662. (setq task (buffer-string)))
  7663. (insert task))
  7664. (goto-char beg)))
  7665. ;;; Outline Sorting
  7666. (defun org-sort (with-case)
  7667. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7668. Optional argument WITH-CASE means sort case-sensitively."
  7669. (interactive "P")
  7670. (cond
  7671. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7672. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7673. (t
  7674. (org-call-with-arg 'org-sort-entries with-case))))
  7675. (defun org-sort-remove-invisible (s)
  7676. "Remove invisible links from string S."
  7677. (remove-text-properties 0 (length s) org-rm-props s)
  7678. (while (string-match org-bracket-link-regexp s)
  7679. (setq s (replace-match (if (match-end 2)
  7680. (match-string 3 s)
  7681. (match-string 1 s)) t t s)))
  7682. (let ((st (format " %s " s)))
  7683. (while (string-match org-emph-re st)
  7684. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7685. (setq s (substring st 1 -1)))
  7686. s)
  7687. (defvar org-priority-regexp) ; defined later in the file
  7688. (defvar org-after-sorting-entries-or-items-hook nil
  7689. "Hook that is run after a bunch of entries or items have been sorted.
  7690. When children are sorted, the cursor is in the parent line when this
  7691. hook gets called. When a region or a plain list is sorted, the cursor
  7692. will be in the first entry of the sorted region/list.")
  7693. (defun org-sort-entries
  7694. (&optional with-case sorting-type getkey-func compare-func property)
  7695. "Sort entries on a certain level of an outline tree.
  7696. If there is an active region, the entries in the region are sorted.
  7697. Else, if the cursor is before the first entry, sort the top-level items.
  7698. Else, the children of the entry at point are sorted.
  7699. Sorting can be alphabetically, numerically, by date/time as given by
  7700. a time stamp, by a property, by priority order, or by a custom function.
  7701. The command prompts for the sorting type unless it has been given to the
  7702. function through the SORTING-TYPE argument, which needs to be a character,
  7703. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7704. precise meaning of each character:
  7705. n Numerically, by converting the beginning of the entry/item to a number.
  7706. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7707. o By order of TODO keywords.
  7708. t By date/time, either the first active time stamp in the entry, or, if
  7709. none exist, by the first inactive one.
  7710. s By the scheduled date/time.
  7711. d By deadline date/time.
  7712. c By creation time, which is assumed to be the first inactive time stamp
  7713. at the beginning of a line.
  7714. p By priority according to the cookie.
  7715. r By the value of a property.
  7716. Capital letters will reverse the sort order.
  7717. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7718. called with point at the beginning of the record. It must return either
  7719. a string or a number that should serve as the sorting key for that record.
  7720. Comparing entries ignores case by default. However, with an optional argument
  7721. WITH-CASE, the sorting considers case as well.
  7722. Sorting is done against the visible part of the headlines, it ignores hidden
  7723. links."
  7724. (interactive "P")
  7725. (let ((case-func (if with-case 'identity 'downcase))
  7726. (cmstr
  7727. ;; The clock marker is lost when using `sort-subr', let's
  7728. ;; store the clocking string.
  7729. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7730. (save-excursion
  7731. (goto-char org-clock-marker)
  7732. (looking-back "^.*") (match-string-no-properties 0))))
  7733. start beg end stars re re2
  7734. txt what tmp)
  7735. ;; Find beginning and end of region to sort
  7736. (cond
  7737. ((org-region-active-p)
  7738. ;; we will sort the region
  7739. (setq end (region-end)
  7740. what "region")
  7741. (goto-char (region-beginning))
  7742. (if (not (org-at-heading-p)) (outline-next-heading))
  7743. (setq start (point)))
  7744. ((or (org-at-heading-p)
  7745. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7746. ;; we will sort the children of the current headline
  7747. (org-back-to-heading)
  7748. (setq start (point)
  7749. end (progn (org-end-of-subtree t t)
  7750. (or (bolp) (insert "\n"))
  7751. (org-back-over-empty-lines)
  7752. (point))
  7753. what "children")
  7754. (goto-char start)
  7755. (show-subtree)
  7756. (outline-next-heading))
  7757. (t
  7758. ;; we will sort the top-level entries in this file
  7759. (goto-char (point-min))
  7760. (or (org-at-heading-p) (outline-next-heading))
  7761. (setq start (point))
  7762. (goto-char (point-max))
  7763. (beginning-of-line 1)
  7764. (when (looking-at ".*?\\S-")
  7765. ;; File ends in a non-white line
  7766. (end-of-line 1)
  7767. (insert "\n"))
  7768. (setq end (point-max))
  7769. (setq what "top-level")
  7770. (goto-char start)
  7771. (show-all)))
  7772. (setq beg (point))
  7773. (if (>= beg end) (user-error "Nothing to sort"))
  7774. (looking-at "\\(\\*+\\)")
  7775. (setq stars (match-string 1)
  7776. re (concat "^" (regexp-quote stars) " +")
  7777. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7778. txt (buffer-substring beg end))
  7779. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7780. (if (and (not (equal stars "*")) (string-match re2 txt))
  7781. (user-error "Region to sort contains a level above the first entry"))
  7782. (unless sorting-type
  7783. (message
  7784. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7785. [t]ime [s]cheduled [d]eadline [c]reated
  7786. A/N/P/R/O/F/T/S/D/C means reversed:"
  7787. what)
  7788. (setq sorting-type (read-char-exclusive))
  7789. (unless getkey-func
  7790. (and (= (downcase sorting-type) ?f)
  7791. (setq getkey-func
  7792. (org-icompleting-read "Sort using function: "
  7793. obarray 'fboundp t nil nil))
  7794. (setq getkey-func (intern getkey-func))))
  7795. (and (= (downcase sorting-type) ?r)
  7796. (not property)
  7797. (setq property
  7798. (org-icompleting-read "Property: "
  7799. (mapcar 'list (org-buffer-property-keys t))
  7800. nil t))))
  7801. (message "Sorting entries...")
  7802. (save-restriction
  7803. (narrow-to-region start end)
  7804. (let ((dcst (downcase sorting-type))
  7805. (case-fold-search nil)
  7806. (now (current-time)))
  7807. (sort-subr
  7808. (/= dcst sorting-type)
  7809. ;; This function moves to the beginning character of the "record" to
  7810. ;; be sorted.
  7811. (lambda nil
  7812. (if (re-search-forward re nil t)
  7813. (goto-char (match-beginning 0))
  7814. (goto-char (point-max))))
  7815. ;; This function moves to the last character of the "record" being
  7816. ;; sorted.
  7817. (lambda nil
  7818. (save-match-data
  7819. (condition-case nil
  7820. (outline-forward-same-level 1)
  7821. (error
  7822. (goto-char (point-max))))))
  7823. ;; This function returns the value that gets sorted against.
  7824. (lambda nil
  7825. (cond
  7826. ((= dcst ?n)
  7827. (if (looking-at org-complex-heading-regexp)
  7828. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7829. nil))
  7830. ((= dcst ?a)
  7831. (if (looking-at org-complex-heading-regexp)
  7832. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7833. nil))
  7834. ((= dcst ?t)
  7835. (let ((end (save-excursion (outline-next-heading) (point))))
  7836. (if (or (re-search-forward org-ts-regexp end t)
  7837. (re-search-forward org-ts-regexp-both end t))
  7838. (org-time-string-to-seconds (match-string 0))
  7839. (org-float-time now))))
  7840. ((= dcst ?c)
  7841. (let ((end (save-excursion (outline-next-heading) (point))))
  7842. (if (re-search-forward
  7843. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7844. end t)
  7845. (org-time-string-to-seconds (match-string 0))
  7846. (org-float-time now))))
  7847. ((= dcst ?s)
  7848. (let ((end (save-excursion (outline-next-heading) (point))))
  7849. (if (re-search-forward org-scheduled-time-regexp end t)
  7850. (org-time-string-to-seconds (match-string 1))
  7851. (org-float-time now))))
  7852. ((= dcst ?d)
  7853. (let ((end (save-excursion (outline-next-heading) (point))))
  7854. (if (re-search-forward org-deadline-time-regexp end t)
  7855. (org-time-string-to-seconds (match-string 1))
  7856. (org-float-time now))))
  7857. ((= dcst ?p)
  7858. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7859. (string-to-char (match-string 2))
  7860. org-default-priority))
  7861. ((= dcst ?r)
  7862. (or (org-entry-get nil property) ""))
  7863. ((= dcst ?o)
  7864. (if (looking-at org-complex-heading-regexp)
  7865. (- 9999 (length (member (match-string 2)
  7866. org-todo-keywords-1)))))
  7867. ((= dcst ?f)
  7868. (if getkey-func
  7869. (progn
  7870. (setq tmp (funcall getkey-func))
  7871. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7872. tmp)
  7873. (error "Invalid key function `%s'" getkey-func)))
  7874. (t (error "Invalid sorting type `%c'" sorting-type))))
  7875. nil
  7876. (cond
  7877. ((= dcst ?a) 'string<)
  7878. ((= dcst ?f) compare-func)
  7879. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7880. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7881. ;; Reset the clock marker if needed
  7882. (when cmstr
  7883. (save-excursion
  7884. (goto-char start)
  7885. (search-forward cmstr nil t)
  7886. (move-marker org-clock-marker (point))))
  7887. (message "Sorting entries...done")))
  7888. (defun org-do-sort (table what &optional with-case sorting-type)
  7889. "Sort TABLE of WHAT according to SORTING-TYPE.
  7890. The user will be prompted for the SORTING-TYPE if the call to this
  7891. function does not specify it. WHAT is only for the prompt, to indicate
  7892. what is being sorted. The sorting key will be extracted from
  7893. the car of the elements of the table.
  7894. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7895. (unless sorting-type
  7896. (message
  7897. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7898. what)
  7899. (setq sorting-type (read-char-exclusive)))
  7900. (let ((dcst (downcase sorting-type))
  7901. extractfun comparefun)
  7902. ;; Define the appropriate functions
  7903. (cond
  7904. ((= dcst ?n)
  7905. (setq extractfun 'string-to-number
  7906. comparefun (if (= dcst sorting-type) '< '>)))
  7907. ((= dcst ?a)
  7908. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7909. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7910. comparefun (if (= dcst sorting-type)
  7911. 'string<
  7912. (lambda (a b) (and (not (string< a b))
  7913. (not (string= a b)))))))
  7914. ((= dcst ?t)
  7915. (setq extractfun
  7916. (lambda (x)
  7917. (if (or (string-match org-ts-regexp x)
  7918. (string-match org-ts-regexp-both x))
  7919. (org-float-time
  7920. (org-time-string-to-time (match-string 0 x)))
  7921. 0))
  7922. comparefun (if (= dcst sorting-type) '< '>)))
  7923. (t (error "Invalid sorting type `%c'" sorting-type)))
  7924. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7925. table)
  7926. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7927. ;;; The orgstruct minor mode
  7928. ;; Define a minor mode which can be used in other modes in order to
  7929. ;; integrate the org-mode structure editing commands.
  7930. ;; This is really a hack, because the org-mode structure commands use
  7931. ;; keys which normally belong to the major mode. Here is how it
  7932. ;; works: The minor mode defines all the keys necessary to operate the
  7933. ;; structure commands, but wraps the commands into a function which
  7934. ;; tests if the cursor is currently at a headline or a plain list
  7935. ;; item. If that is the case, the structure command is used,
  7936. ;; temporarily setting many Org-mode variables like regular
  7937. ;; expressions for filling etc. However, when any of those keys is
  7938. ;; used at a different location, function uses `key-binding' to look
  7939. ;; up if the key has an associated command in another currently active
  7940. ;; keymap (minor modes, major mode, global), and executes that
  7941. ;; command. There might be problems if any of the keys is otherwise
  7942. ;; used as a prefix key.
  7943. (defcustom orgstruct-heading-prefix-regexp nil
  7944. "Regexp that matches the custom prefix of Org headlines in
  7945. orgstruct(++)-mode."
  7946. :group 'org
  7947. :version "24.4"
  7948. :package-version '(Org . "8.0")
  7949. :type 'string)
  7950. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7951. (defcustom orgstruct-setup-hook nil
  7952. "Hook run after orgstruct-mode-map is filled."
  7953. :group 'org
  7954. :version "24.4"
  7955. :package-version '(Org . "8.0")
  7956. :type 'hook)
  7957. (defvar orgstruct-initialized nil)
  7958. (defvar org-local-vars nil
  7959. "List of local variables, for use by `orgstruct-mode'.")
  7960. ;;;###autoload
  7961. (define-minor-mode orgstruct-mode
  7962. "Toggle the minor mode `orgstruct-mode'.
  7963. This mode is for using Org-mode structure commands in other
  7964. modes. The following keys behave as if Org-mode were active, if
  7965. the cursor is on a headline, or on a plain list item (both as
  7966. defined by Org-mode)."
  7967. nil " OrgStruct" (make-sparse-keymap)
  7968. (funcall (if orgstruct-mode
  7969. 'add-to-invisibility-spec
  7970. 'remove-from-invisibility-spec)
  7971. '(outline . t))
  7972. (when orgstruct-mode
  7973. (org-load-modules-maybe)
  7974. (unless orgstruct-initialized
  7975. (orgstruct-setup)
  7976. (setq orgstruct-initialized t))))
  7977. ;;;###autoload
  7978. (defun turn-on-orgstruct ()
  7979. "Unconditionally turn on `orgstruct-mode'."
  7980. (orgstruct-mode 1))
  7981. (defvar org-fb-vars nil)
  7982. (make-variable-buffer-local 'org-fb-vars)
  7983. (defun orgstruct++-mode (&optional arg)
  7984. "Toggle `orgstruct-mode', the enhanced version of it.
  7985. In addition to setting orgstruct-mode, this also exports all
  7986. indentation and autofilling variables from org-mode into the
  7987. buffer. It will also recognize item context in multiline items."
  7988. (interactive "P")
  7989. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7990. (if (< arg 1)
  7991. (progn (orgstruct-mode -1)
  7992. (mapc (lambda(v)
  7993. (org-set-local (car v)
  7994. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7995. org-fb-vars))
  7996. (orgstruct-mode 1)
  7997. (setq org-fb-vars nil)
  7998. (unless org-local-vars
  7999. (setq org-local-vars (org-get-local-variables)))
  8000. (let (var val)
  8001. (mapc
  8002. (lambda (x)
  8003. (when (string-match
  8004. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8005. (symbol-name (car x)))
  8006. (setq var (car x) val (nth 1 x))
  8007. (push (list var `(quote ,(eval var))) org-fb-vars)
  8008. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8009. org-local-vars)
  8010. (org-set-local 'orgstruct-is-++ t))))
  8011. (defvar orgstruct-is-++ nil
  8012. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8013. (make-variable-buffer-local 'orgstruct-is-++)
  8014. ;;;###autoload
  8015. (defun turn-on-orgstruct++ ()
  8016. "Unconditionally turn on `orgstruct++-mode'."
  8017. (orgstruct++-mode 1))
  8018. (defun orgstruct-error ()
  8019. "Error when there is no default binding for a structure key."
  8020. (interactive)
  8021. (funcall (if (fboundp 'user-error)
  8022. 'user-error
  8023. 'error)
  8024. "This key has no function outside structure elements"))
  8025. (defun orgstruct-setup ()
  8026. "Setup orgstruct keymap."
  8027. (dolist (cell '((org-demote . t)
  8028. (org-metaleft . t)
  8029. (org-metaright . t)
  8030. (org-promote . t)
  8031. (org-shiftmetaleft . t)
  8032. (org-shiftmetaright . t)
  8033. org-backward-element
  8034. org-backward-heading-same-level
  8035. org-ctrl-c-ret
  8036. org-ctrl-c-minus
  8037. org-ctrl-c-star
  8038. org-cycle
  8039. org-forward-heading-same-level
  8040. org-insert-heading
  8041. org-insert-heading-respect-content
  8042. org-kill-note-or-show-branches
  8043. org-mark-subtree
  8044. org-meta-return
  8045. org-metadown
  8046. org-metaup
  8047. org-narrow-to-subtree
  8048. org-promote-subtree
  8049. org-reveal
  8050. org-shiftdown
  8051. org-shiftleft
  8052. org-shiftmetadown
  8053. org-shiftmetaup
  8054. org-shiftright
  8055. org-shifttab
  8056. org-shifttab
  8057. org-shiftup
  8058. org-show-subtree
  8059. org-sort
  8060. org-up-element
  8061. outline-demote
  8062. outline-next-visible-heading
  8063. outline-previous-visible-heading
  8064. outline-promote
  8065. outline-up-heading
  8066. show-children))
  8067. (let ((f (or (car-safe cell) cell))
  8068. (disable-when-heading-prefix (cdr-safe cell)))
  8069. (when (fboundp f)
  8070. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8071. (where-is-internal f outline-mode-map)))
  8072. ;; TODO use local-function-key-map
  8073. (dolist (rep '(("<tab>" . "TAB")
  8074. ("<return>" . "RET")
  8075. ("<escape>" . "ESC")
  8076. ("<delete>" . "DEL")))
  8077. (setq binding (read-kbd-macro
  8078. (let ((case-fold-search))
  8079. (replace-regexp-in-string
  8080. (regexp-quote (cdr rep))
  8081. (car rep)
  8082. (key-description binding))))))
  8083. (let ((key (lookup-key orgstruct-mode-map binding)))
  8084. (when (or (not key) (numberp key))
  8085. (condition-case nil
  8086. (org-defkey orgstruct-mode-map
  8087. binding
  8088. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8089. (error nil))))))))
  8090. (run-hooks 'orgstruct-setup-hook))
  8091. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8092. "Create a function for binding in the structure minor mode.
  8093. FUN is the command to call inside a table. KEY is the key that
  8094. should be checked in for a command to execute outside of tables.
  8095. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  8096. if `orgstruct-heading-prefix-regexp' is non-nil."
  8097. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8098. (let ((nname name)
  8099. (i 0))
  8100. (while (fboundp (intern nname))
  8101. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8102. (setq name (intern nname)))
  8103. (eval
  8104. (let ((bindings '((org-heading-regexp
  8105. (concat "^"
  8106. orgstruct-heading-prefix-regexp
  8107. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8108. (org-outline-regexp
  8109. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8110. (org-outline-regexp-bol
  8111. (concat "^" org-outline-regexp))
  8112. (outline-regexp org-outline-regexp)
  8113. (outline-heading-end-regexp "\n")
  8114. (outline-level 'org-outline-level)
  8115. (outline-heading-alist))))
  8116. `(defun ,name (arg)
  8117. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8118. "Outside of structure, run the binding of `"
  8119. (key-description key) "'."
  8120. (when disable-when-heading-prefix
  8121. (concat
  8122. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  8123. "back to the default binding due to limitations of Org's implementation of\n"
  8124. "`" (symbol-name fun) "'.")))
  8125. (interactive "p")
  8126. (let* ((disable
  8127. ,(when disable-when-heading-prefix
  8128. '(and orgstruct-heading-prefix-regexp
  8129. (not (string= orgstruct-heading-prefix-regexp "")))))
  8130. (fallback
  8131. (or disable
  8132. (not
  8133. (let* ,bindings
  8134. (org-context-p 'headline 'item
  8135. ,(when (memq fun
  8136. '(org-insert-heading
  8137. org-insert-heading-respect-content
  8138. org-meta-return))
  8139. '(when orgstruct-is-++
  8140. 'item-body))))))))
  8141. (if fallback
  8142. (let* ((orgstruct-mode)
  8143. (binding
  8144. (loop with key = ,key
  8145. for rep in
  8146. '(nil
  8147. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8148. ("<\\([^>]*\\)return>" . "\\1RET")
  8149. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8150. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8151. do
  8152. (when rep
  8153. (setq key (read-kbd-macro
  8154. (let ((case-fold-search))
  8155. (replace-regexp-in-string
  8156. (car rep)
  8157. (cdr rep)
  8158. (key-description key))))))
  8159. thereis (key-binding key))))
  8160. (if (keymapp binding)
  8161. (set-temporary-overlay-map binding)
  8162. (let ((func (or binding
  8163. (unless disable
  8164. 'orgstruct-error))))
  8165. (when func
  8166. (call-interactively func)))))
  8167. (org-run-like-in-org-mode
  8168. (lambda ()
  8169. (interactive)
  8170. (let* ,bindings
  8171. (call-interactively ',fun)))))))))
  8172. name))
  8173. (defun org-contextualize-keys (alist contexts)
  8174. "Return valid elements in ALIST depending on CONTEXTS.
  8175. `org-agenda-custom-commands' or `org-capture-templates' are the
  8176. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8177. or `org-capture-templates-contexts' are the associated contexts
  8178. definitions."
  8179. (let ((contexts
  8180. ;; normalize contexts
  8181. (mapcar
  8182. (lambda(c) (cond ((listp (cadr c))
  8183. (list (car c) (car c) (cadr c)))
  8184. ((string= "" (cadr c))
  8185. (list (car c) (car c) (caddr c)))
  8186. (t c))) contexts))
  8187. (a alist) c r s)
  8188. ;; loop over all commands or templates
  8189. (while (setq c (pop a))
  8190. (let (vrules repl)
  8191. (cond
  8192. ((not (assoc (car c) contexts))
  8193. (push c r))
  8194. ((and (assoc (car c) contexts)
  8195. (setq vrules (org-contextualize-validate-key
  8196. (car c) contexts)))
  8197. (mapc (lambda (vr)
  8198. (when (not (equal (car vr) (cadr vr)))
  8199. (setq repl vr))) vrules)
  8200. (if (not repl) (push c r)
  8201. (push (cadr repl) s)
  8202. (push
  8203. (cons (car c)
  8204. (cdr (or (assoc (cadr repl) alist)
  8205. (error "Undefined key `%s' as contextual replacement for `%s'"
  8206. (cadr repl) (car c)))))
  8207. r))))))
  8208. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8209. (delq
  8210. nil
  8211. (delete-dups
  8212. (mapcar (lambda (x)
  8213. (let ((tpl (car x)))
  8214. (when (not (delq
  8215. nil
  8216. (mapcar (lambda(y)
  8217. (equal y tpl)) s))) x)))
  8218. (reverse r))))))
  8219. (defun org-contextualize-validate-key (key contexts)
  8220. "Check CONTEXTS for agenda or capture KEY."
  8221. (let (r rr res)
  8222. (while (setq r (pop contexts))
  8223. (mapc
  8224. (lambda (rr)
  8225. (when
  8226. (and (equal key (car r))
  8227. (if (functionp rr) (funcall rr)
  8228. (or (and (eq (car rr) 'in-file)
  8229. (buffer-file-name)
  8230. (string-match (cdr rr) (buffer-file-name)))
  8231. (and (eq (car rr) 'in-mode)
  8232. (string-match (cdr rr) (symbol-name major-mode)))
  8233. (and (eq (car rr) 'in-buffer)
  8234. (string-match (cdr rr) (buffer-name)))
  8235. (when (and (eq (car rr) 'not-in-file)
  8236. (buffer-file-name))
  8237. (not (string-match (cdr rr) (buffer-file-name))))
  8238. (when (eq (car rr) 'not-in-mode)
  8239. (not (string-match (cdr rr) (symbol-name major-mode))))
  8240. (when (eq (car rr) 'not-in-buffer)
  8241. (not (string-match (cdr rr) (buffer-name)))))))
  8242. (push r res)))
  8243. (car (last r))))
  8244. (delete-dups (delq nil res))))
  8245. (defun org-context-p (&rest contexts)
  8246. "Check if local context is any of CONTEXTS.
  8247. Possible values in the list of contexts are `table', `headline', and `item'."
  8248. (let ((pos (point)))
  8249. (goto-char (point-at-bol))
  8250. (prog1 (or (and (memq 'table contexts)
  8251. (looking-at "[ \t]*|"))
  8252. (and (memq 'headline contexts)
  8253. (looking-at org-outline-regexp))
  8254. (and (memq 'item contexts)
  8255. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8256. (and (memq 'item-body contexts)
  8257. (org-in-item-p)))
  8258. (goto-char pos))))
  8259. (defun org-get-local-variables ()
  8260. "Return a list of all local variables in an Org mode buffer."
  8261. (let (varlist)
  8262. (with-current-buffer (get-buffer-create "*Org tmp*")
  8263. (erase-buffer)
  8264. (org-mode)
  8265. (setq varlist (buffer-local-variables)))
  8266. (kill-buffer "*Org tmp*")
  8267. (delq nil
  8268. (mapcar
  8269. (lambda (x)
  8270. (setq x
  8271. (if (symbolp x)
  8272. (list x)
  8273. (list (car x) (cdr x))))
  8274. (if (and (not (get (car x) 'org-state))
  8275. (string-match
  8276. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8277. (symbol-name (car x))))
  8278. x nil))
  8279. varlist))))
  8280. (defun org-clone-local-variables (from-buffer &optional regexp)
  8281. "Clone local variables from FROM-BUFFER.
  8282. Optional argument REGEXP selects variables to clone."
  8283. (mapc
  8284. (lambda (pair)
  8285. (and (symbolp (car pair))
  8286. (or (null regexp)
  8287. (string-match regexp (symbol-name (car pair))))
  8288. (set (make-local-variable (car pair))
  8289. (cdr pair))))
  8290. (buffer-local-variables from-buffer)))
  8291. ;;;###autoload
  8292. (defun org-run-like-in-org-mode (cmd)
  8293. "Run a command, pretending that the current buffer is in Org-mode.
  8294. This will temporarily bind local variables that are typically bound in
  8295. Org-mode to the values they have in Org-mode, and then interactively
  8296. call CMD."
  8297. (org-load-modules-maybe)
  8298. (unless org-local-vars
  8299. (setq org-local-vars (org-get-local-variables)))
  8300. (let (binds)
  8301. (dolist (var org-local-vars)
  8302. (when (or (not (boundp (car var)))
  8303. (eq (symbol-value (car var))
  8304. (default-value (car var))))
  8305. (push (list (car var) `(quote ,(cadr var))) binds)))
  8306. (eval `(let ,binds
  8307. (call-interactively (quote ,cmd))))))
  8308. ;;;; Archiving
  8309. (defun org-get-category (&optional pos force-refresh)
  8310. "Get the category applying to position POS."
  8311. (save-match-data
  8312. (if force-refresh (org-refresh-category-properties))
  8313. (let ((pos (or pos (point))))
  8314. (or (get-text-property pos 'org-category)
  8315. (progn (org-refresh-category-properties)
  8316. (get-text-property pos 'org-category))))))
  8317. (defun org-refresh-category-properties ()
  8318. "Refresh category text properties in the buffer."
  8319. (let ((case-fold-search t)
  8320. (inhibit-read-only t)
  8321. (def-cat (cond
  8322. ((null org-category)
  8323. (if buffer-file-name
  8324. (file-name-sans-extension
  8325. (file-name-nondirectory buffer-file-name))
  8326. "???"))
  8327. ((symbolp org-category) (symbol-name org-category))
  8328. (t org-category)))
  8329. beg end cat pos optionp)
  8330. (org-with-silent-modifications
  8331. (save-excursion
  8332. (save-restriction
  8333. (widen)
  8334. (goto-char (point-min))
  8335. (put-text-property (point) (point-max) 'org-category def-cat)
  8336. (while (re-search-forward
  8337. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8338. (setq pos (match-end 0)
  8339. optionp (equal (char-after (match-beginning 0)) ?#)
  8340. cat (org-trim (match-string 2)))
  8341. (if optionp
  8342. (setq beg (point-at-bol) end (point-max))
  8343. (org-back-to-heading t)
  8344. (setq beg (point) end (org-end-of-subtree t t)))
  8345. (put-text-property beg end 'org-category cat)
  8346. (put-text-property beg end 'org-category-position beg)
  8347. (goto-char pos)))))))
  8348. (defun org-refresh-properties (dprop tprop)
  8349. "Refresh buffer text properties.
  8350. DPROP is the drawer property and TPROP is the corresponding text
  8351. property to set."
  8352. (let ((case-fold-search t)
  8353. (inhibit-read-only t) p)
  8354. (org-with-silent-modifications
  8355. (save-excursion
  8356. (save-restriction
  8357. (widen)
  8358. (goto-char (point-min))
  8359. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8360. (setq p (org-match-string-no-properties 1))
  8361. (save-excursion
  8362. (org-back-to-heading t)
  8363. (put-text-property
  8364. (point-at-bol) (point-at-eol) tprop p))))))))
  8365. ;;;; Link Stuff
  8366. ;;; Link abbreviations
  8367. (defun org-link-expand-abbrev (link)
  8368. "Apply replacements as defined in `org-link-abbrev-alist'."
  8369. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8370. (let* ((key (match-string 1 link))
  8371. (as (or (assoc key org-link-abbrev-alist-local)
  8372. (assoc key org-link-abbrev-alist)))
  8373. (tag (and (match-end 2) (match-string 3 link)))
  8374. rpl)
  8375. (if (not as)
  8376. link
  8377. (setq rpl (cdr as))
  8378. (cond
  8379. ((symbolp rpl) (funcall rpl tag))
  8380. ((string-match "%(\\([^)]+\\))" rpl)
  8381. (replace-match
  8382. (save-match-data
  8383. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8384. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8385. ((string-match "%h" rpl)
  8386. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8387. (t (concat rpl tag)))))
  8388. link))
  8389. ;;; Storing and inserting links
  8390. (defvar org-insert-link-history nil
  8391. "Minibuffer history for links inserted with `org-insert-link'.")
  8392. (defvar org-stored-links nil
  8393. "Contains the links stored with `org-store-link'.")
  8394. (defvar org-store-link-plist nil
  8395. "Plist with info about the most recently link created with `org-store-link'.")
  8396. (defvar org-link-protocols nil
  8397. "Link protocols added to Org-mode using `org-add-link-type'.")
  8398. (defvar org-store-link-functions nil
  8399. "List of functions that are called to create and store a link.
  8400. Each function will be called in turn until one returns a non-nil
  8401. value. Each function should check if it is responsible for creating
  8402. this link (for example by looking at the major mode).
  8403. If not, it must exit and return nil.
  8404. If yes, it should return a non-nil value after a calling
  8405. `org-store-link-props' with a list of properties and values.
  8406. Special properties are:
  8407. :type The link prefix, like \"http\". This must be given.
  8408. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8409. This is obligatory as well.
  8410. :description Optional default description for the second pair
  8411. of brackets in an Org-mode link. The user can still change
  8412. this when inserting this link into an Org-mode buffer.
  8413. In addition to these, any additional properties can be specified
  8414. and then used in capture templates.")
  8415. (defun org-add-link-type (type &optional follow export)
  8416. "Add TYPE to the list of `org-link-types'.
  8417. Re-compute all regular expressions depending on `org-link-types'
  8418. FOLLOW and EXPORT are two functions.
  8419. FOLLOW should take the link path as the single argument and do whatever
  8420. is necessary to follow the link, for example find a file or display
  8421. a mail message.
  8422. EXPORT should format the link path for export to one of the export formats.
  8423. It should be a function accepting three arguments:
  8424. path the path of the link, the text after the prefix (like \"http:\")
  8425. desc the description of the link, if any, or a description added by
  8426. org-export-normalize-links if there is none
  8427. format the export format, a symbol like `html' or `latex' or `ascii'..
  8428. The function may use the FORMAT information to return different values
  8429. depending on the format. The return value will be put literally into
  8430. the exported file. If the return value is nil, this means Org should
  8431. do what it normally does with links which do not have EXPORT defined.
  8432. Org-mode has a built-in default for exporting links. If you are happy with
  8433. this default, there is no need to define an export function for the link
  8434. type. For a simple example of an export function, see `org-bbdb.el'."
  8435. (add-to-list 'org-link-types type t)
  8436. (org-make-link-regexps)
  8437. (if (assoc type org-link-protocols)
  8438. (setcdr (assoc type org-link-protocols) (list follow export))
  8439. (push (list type follow export) org-link-protocols)))
  8440. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8441. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8442. ;;;###autoload
  8443. (defun org-store-link (arg)
  8444. "\\<org-mode-map>Store an org-link to the current location.
  8445. This link is added to `org-stored-links' and can later be inserted
  8446. into an org-buffer with \\[org-insert-link].
  8447. For some link types, a prefix arg is interpreted.
  8448. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8449. For file links, arg negates `org-context-in-file-links'.
  8450. A double prefix arg force skipping storing functions that are not
  8451. part of Org's core.
  8452. A triple prefix arg force storing a link for each line in the
  8453. active region."
  8454. (interactive "P")
  8455. (org-load-modules-maybe)
  8456. (if (and (equal arg '(64)) (org-region-active-p))
  8457. (save-excursion
  8458. (let ((end (region-end)))
  8459. (goto-char (region-beginning))
  8460. (set-mark (point))
  8461. (while (< (point-at-eol) end)
  8462. (move-end-of-line 1) (activate-mark)
  8463. (let (current-prefix-arg)
  8464. (call-interactively 'org-store-link))
  8465. (move-beginning-of-line 2)
  8466. (set-mark (point)))))
  8467. (org-with-limited-levels
  8468. (setq org-store-link-plist nil)
  8469. (let (link cpltxt desc description search
  8470. txt custom-id agenda-link sfuns sfunsn)
  8471. (cond
  8472. ;; Store a link using an external link type
  8473. ((and (not (equal arg '(16)))
  8474. (setq sfuns
  8475. (delq
  8476. nil (mapcar (lambda (f)
  8477. (let (fs) (if (funcall f) (push f fs))))
  8478. org-store-link-functions))
  8479. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8480. (or (and (cdr sfuns)
  8481. (funcall (intern
  8482. (completing-read
  8483. "Which function for creating the link? "
  8484. sfunsn t (car sfunsn)))))
  8485. (funcall (caar sfuns)))
  8486. (setq link (plist-get org-store-link-plist :link)
  8487. desc (or (plist-get org-store-link-plist
  8488. :description) link))))
  8489. ;; Store a link from a source code buffer
  8490. ((org-src-edit-buffer-p)
  8491. (let (label gc)
  8492. (while (or (not label)
  8493. (save-excursion
  8494. (save-restriction
  8495. (widen)
  8496. (goto-char (point-min))
  8497. (re-search-forward
  8498. (regexp-quote (format org-coderef-label-format label))
  8499. nil t))))
  8500. (when label (message "Label exists already") (sit-for 2))
  8501. (setq label (read-string "Code line label: " label)))
  8502. (end-of-line 1)
  8503. (setq link (format org-coderef-label-format label))
  8504. (setq gc (- 79 (length link)))
  8505. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8506. (insert link)
  8507. (setq link (concat "(" label ")") desc nil)))
  8508. ;; We are in the agenda, link to referenced location
  8509. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8510. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8511. (get-text-property (point) 'org-marker))))
  8512. (when m
  8513. (org-with-point-at m
  8514. (setq agenda-link
  8515. (if (org-called-interactively-p 'any)
  8516. (call-interactively 'org-store-link)
  8517. (org-store-link nil)))))))
  8518. ((eq major-mode 'calendar-mode)
  8519. (let ((cd (calendar-cursor-to-date)))
  8520. (setq link
  8521. (format-time-string
  8522. (car org-time-stamp-formats)
  8523. (apply 'encode-time
  8524. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8525. nil nil nil))))
  8526. (org-store-link-props :type "calendar" :date cd)))
  8527. ((eq major-mode 'help-mode)
  8528. (setq link (concat "help:" (save-excursion
  8529. (goto-char (point-min))
  8530. (looking-at "^[^ ]+")
  8531. (match-string 0))))
  8532. (org-store-link-props :type "help"))
  8533. ((eq major-mode 'w3-mode)
  8534. (setq cpltxt (if (and (buffer-name)
  8535. (not (string-match "Untitled" (buffer-name))))
  8536. (buffer-name)
  8537. (url-view-url t))
  8538. link (url-view-url t))
  8539. (org-store-link-props :type "w3" :url (url-view-url t)))
  8540. ((eq major-mode 'image-mode)
  8541. (setq cpltxt (concat "file:"
  8542. (abbreviate-file-name buffer-file-name))
  8543. link cpltxt)
  8544. (org-store-link-props :type "image" :file buffer-file-name))
  8545. ;; In dired, store a link to the file of the current line
  8546. ((eq major-mode 'dired-mode)
  8547. (let ((file (dired-get-filename nil t)))
  8548. (setq file (if file
  8549. (abbreviate-file-name
  8550. (expand-file-name (dired-get-filename nil t)))
  8551. ;; otherwise, no file so use current directory.
  8552. default-directory))
  8553. (setq cpltxt (concat "file:" file)
  8554. link cpltxt)))
  8555. ((setq search (run-hook-with-args-until-success
  8556. 'org-create-file-search-functions))
  8557. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8558. "::" search))
  8559. (setq cpltxt (or description link)))
  8560. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8561. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8562. (cond
  8563. ;; Store a link using the target at point
  8564. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8565. (setq cpltxt
  8566. (concat "file:"
  8567. (abbreviate-file-name
  8568. (buffer-file-name (buffer-base-buffer)))
  8569. "::" (match-string 1))
  8570. link cpltxt))
  8571. ((and (featurep 'org-id)
  8572. (or (eq org-id-link-to-org-use-id t)
  8573. (and (org-called-interactively-p 'any)
  8574. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8575. (and (eq org-id-link-to-org-use-id
  8576. 'create-if-interactive-and-no-custom-id)
  8577. (not custom-id))))
  8578. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8579. ;; Store a link using the ID at point
  8580. (setq link (condition-case nil
  8581. (prog1 (org-id-store-link)
  8582. (setq desc (plist-get org-store-link-plist
  8583. :description)))
  8584. (error
  8585. ;; Probably before first headline, link only to file
  8586. (concat "file:"
  8587. (abbreviate-file-name
  8588. (buffer-file-name (buffer-base-buffer))))))))
  8589. (t
  8590. ;; Just link to current headline
  8591. (setq cpltxt (concat "file:"
  8592. (abbreviate-file-name
  8593. (buffer-file-name (buffer-base-buffer)))))
  8594. ;; Add a context search string
  8595. (when (org-xor org-context-in-file-links arg)
  8596. (let* ((ee (org-element-at-point))
  8597. (et (org-element-type ee))
  8598. (ev (plist-get (cadr ee) :value))
  8599. (ek (plist-get (cadr ee) :key))
  8600. (eok (and (stringp ek) (string-match "name" ek))))
  8601. (setq txt (cond
  8602. ((org-at-heading-p) nil)
  8603. ((and (eq et 'keyword) eok) ev)
  8604. ((org-region-active-p)
  8605. (buffer-substring (region-beginning) (region-end)))))
  8606. (when (or (null txt) (string-match "\\S-" txt))
  8607. (setq cpltxt
  8608. (concat cpltxt "::"
  8609. (condition-case nil
  8610. (org-make-org-heading-search-string txt)
  8611. (error "")))
  8612. desc (or (and (eq et 'keyword) eok ev)
  8613. (nth 4 (ignore-errors (org-heading-components)))
  8614. "NONE")))))
  8615. (if (string-match "::\\'" cpltxt)
  8616. (setq cpltxt (substring cpltxt 0 -2)))
  8617. (setq link cpltxt))))
  8618. ((buffer-file-name (buffer-base-buffer))
  8619. ;; Just link to this file here.
  8620. (setq cpltxt (concat "file:"
  8621. (abbreviate-file-name
  8622. (buffer-file-name (buffer-base-buffer)))))
  8623. ;; Add a context string.
  8624. (when (org-xor org-context-in-file-links arg)
  8625. (setq txt (if (org-region-active-p)
  8626. (buffer-substring (region-beginning) (region-end))
  8627. (buffer-substring (point-at-bol) (point-at-eol))))
  8628. ;; Only use search option if there is some text.
  8629. (when (string-match "\\S-" txt)
  8630. (setq cpltxt
  8631. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8632. desc "NONE")))
  8633. (setq link cpltxt))
  8634. ((org-called-interactively-p 'interactive)
  8635. (user-error "No method for storing a link from this buffer"))
  8636. (t (setq link nil)))
  8637. ;; We're done setting link and desc, clean up
  8638. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8639. (setq link (or link cpltxt)
  8640. desc (or desc cpltxt))
  8641. (cond ((equal desc "NONE") (setq desc nil))
  8642. ((string-match org-bracket-link-analytic-regexp desc)
  8643. (let ((d0 (match-string 3 desc))
  8644. (p0 (match-string 5 desc)))
  8645. (setq desc
  8646. (replace-regexp-in-string
  8647. org-bracket-link-regexp
  8648. (concat (or p0 d0)
  8649. (if (equal (length (match-string 0 desc))
  8650. (length desc)) "*" "")) desc)))))
  8651. ;; Return the link
  8652. (if (not (and (or (org-called-interactively-p 'any)
  8653. executing-kbd-macro) link))
  8654. (or agenda-link (and link (org-make-link-string link desc)))
  8655. (push (list link desc) org-stored-links)
  8656. (message "Stored: %s" (or desc link))
  8657. (when custom-id
  8658. (setq link (concat "file:" (abbreviate-file-name
  8659. (buffer-file-name)) "::#" custom-id))
  8660. (push (list link desc) org-stored-links)))))))
  8661. (defun org-store-link-props (&rest plist)
  8662. "Store link properties, extract names and addresses."
  8663. (let (x adr)
  8664. (when (setq x (plist-get plist :from))
  8665. (setq adr (mail-extract-address-components x))
  8666. (setq plist (plist-put plist :fromname (car adr)))
  8667. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8668. (when (setq x (plist-get plist :to))
  8669. (setq adr (mail-extract-address-components x))
  8670. (setq plist (plist-put plist :toname (car adr)))
  8671. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8672. (let ((from (plist-get plist :from))
  8673. (to (plist-get plist :to)))
  8674. (when (and from to org-from-is-user-regexp)
  8675. (setq plist
  8676. (plist-put plist :fromto
  8677. (if (string-match org-from-is-user-regexp from)
  8678. (concat "to %t")
  8679. (concat "from %f"))))))
  8680. (setq org-store-link-plist plist))
  8681. (defun org-add-link-props (&rest plist)
  8682. "Add these properties to the link property list."
  8683. (let (key value)
  8684. (while plist
  8685. (setq key (pop plist) value (pop plist))
  8686. (setq org-store-link-plist
  8687. (plist-put org-store-link-plist key value)))))
  8688. (defun org-email-link-description (&optional fmt)
  8689. "Return the description part of an email link.
  8690. This takes information from `org-store-link-plist' and formats it
  8691. according to FMT (default from `org-email-link-description-format')."
  8692. (setq fmt (or fmt org-email-link-description-format))
  8693. (let* ((p org-store-link-plist)
  8694. (to (plist-get p :toaddress))
  8695. (from (plist-get p :fromaddress))
  8696. (table
  8697. (list
  8698. (cons "%c" (plist-get p :fromto))
  8699. (cons "%F" (plist-get p :from))
  8700. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8701. (cons "%T" (plist-get p :to))
  8702. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8703. (cons "%s" (plist-get p :subject))
  8704. (cons "%d" (plist-get p :date))
  8705. (cons "%m" (plist-get p :message-id)))))
  8706. (when (string-match "%c" fmt)
  8707. ;; Check if the user wrote this message
  8708. (if (and org-from-is-user-regexp from to
  8709. (save-match-data (string-match org-from-is-user-regexp from)))
  8710. (setq fmt (replace-match "to %t" t t fmt))
  8711. (setq fmt (replace-match "from %f" t t fmt))))
  8712. (org-replace-escapes fmt table)))
  8713. (defun org-make-org-heading-search-string (&optional string)
  8714. "Make search string for the current headline or STRING."
  8715. (let ((s (or string
  8716. (and (derived-mode-p 'org-mode)
  8717. (save-excursion
  8718. (org-back-to-heading t)
  8719. (org-element-property :raw-value (org-element-at-point))))))
  8720. (lines org-context-in-file-links))
  8721. (or string (setq s (concat "*" s))) ; Add * for headlines
  8722. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8723. (when (and string (integerp lines) (> lines 0))
  8724. (let ((slines (org-split-string s "\n")))
  8725. (when (< lines (length slines))
  8726. (setq s (mapconcat
  8727. 'identity
  8728. (reverse (nthcdr (- (length slines) lines)
  8729. (reverse slines))) "\n")))))
  8730. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8731. (defun org-make-link-string (link &optional description)
  8732. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8733. (unless (string-match "\\S-" link)
  8734. (error "Empty link"))
  8735. (when (and description
  8736. (stringp description)
  8737. (not (string-match "\\S-" description)))
  8738. (setq description nil))
  8739. (when (stringp description)
  8740. ;; Remove brackets from the description, they are fatal.
  8741. (while (string-match "\\[" description)
  8742. (setq description (replace-match "{" t t description)))
  8743. (while (string-match "\\]" description)
  8744. (setq description (replace-match "}" t t description))))
  8745. (when (equal link description)
  8746. ;; No description needed, it is identical
  8747. (setq description nil))
  8748. (when (and (not description)
  8749. (not (string-match (org-image-file-name-regexp) link))
  8750. (not (equal link (org-link-escape link))))
  8751. (setq description (org-extract-attributes link)))
  8752. (setq link
  8753. (cond ((string-match (org-image-file-name-regexp) link) link)
  8754. ((string-match org-link-types-re link)
  8755. (concat (match-string 1 link)
  8756. (org-link-escape (substring link (match-end 1)))))
  8757. (t (org-link-escape link))))
  8758. (concat "[[" link "]"
  8759. (if description (concat "[" description "]") "")
  8760. "]"))
  8761. (defconst org-link-escape-chars
  8762. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8763. "List of characters that should be escaped in link.
  8764. This is the list that is used for internal purposes.")
  8765. (defconst org-link-escape-chars-browser
  8766. '(?\ ?\")
  8767. "List of escapes for characters that are problematic in links.
  8768. This is the list that is used before handing over to the browser.")
  8769. (defun org-link-escape (text &optional table merge)
  8770. "Return percent escaped representation of TEXT.
  8771. TEXT is a string with the text to escape.
  8772. Optional argument TABLE is a list with characters that should be
  8773. escaped. When nil, `org-link-escape-chars' is used.
  8774. If optional argument MERGE is set, merge TABLE into
  8775. `org-link-escape-chars'."
  8776. (cond
  8777. ((and table merge)
  8778. (mapc (lambda (defchr)
  8779. (unless (member defchr table)
  8780. (setq table (cons defchr table)))) org-link-escape-chars))
  8781. ((null table)
  8782. (setq table org-link-escape-chars)))
  8783. (mapconcat
  8784. (lambda (char)
  8785. (if (or (member char table)
  8786. (and (or (< char 32) (= char 37) (> char 126))
  8787. org-url-hexify-p))
  8788. (mapconcat (lambda (sequence-element)
  8789. (format "%%%.2X" sequence-element))
  8790. (or (encode-coding-char char 'utf-8)
  8791. (error "Unable to percent escape character: %s"
  8792. (char-to-string char))) "")
  8793. (char-to-string char))) text ""))
  8794. (defun org-link-unescape (str)
  8795. "Unhex hexified Unicode strings as returned from the JavaScript function
  8796. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8797. (unless (and (null str) (string= "" str))
  8798. (let ((pos 0) (case-fold-search t) unhexed)
  8799. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8800. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8801. (setq str (replace-match unhexed t t str))
  8802. (setq pos (+ pos (length unhexed))))))
  8803. str)
  8804. (defun org-link-unescape-compound (hex)
  8805. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8806. Note: this function also decodes single byte encodings like
  8807. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8808. (save-match-data
  8809. (let* ((bytes (cdr (split-string hex "%")))
  8810. (ret "")
  8811. (eat 0)
  8812. (sum 0))
  8813. (while bytes
  8814. (let* ((val (string-to-number (pop bytes) 16))
  8815. (shift-xor
  8816. (if (= 0 eat)
  8817. (cond
  8818. ((>= val 252) (cons 6 252))
  8819. ((>= val 248) (cons 5 248))
  8820. ((>= val 240) (cons 4 240))
  8821. ((>= val 224) (cons 3 224))
  8822. ((>= val 192) (cons 2 192))
  8823. (t (cons 0 0)))
  8824. (cons 6 128))))
  8825. (if (>= val 192) (setq eat (car shift-xor)))
  8826. (setq val (logxor val (cdr shift-xor)))
  8827. (setq sum (+ (lsh sum (car shift-xor)) val))
  8828. (if (> eat 0) (setq eat (- eat 1)))
  8829. (cond
  8830. ((= 0 eat) ;multi byte
  8831. (setq ret (concat ret (org-char-to-string sum)))
  8832. (setq sum 0))
  8833. ((not bytes) ; single byte(s)
  8834. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8835. )) ;; end (while bytes
  8836. ret )))
  8837. (defun org-link-unescape-single-byte-sequence (hex)
  8838. "Unhexify hex-encoded single byte character sequences."
  8839. (mapconcat (lambda (byte)
  8840. (char-to-string (string-to-number byte 16)))
  8841. (cdr (split-string hex "%")) ""))
  8842. (defun org-xor (a b)
  8843. "Exclusive or."
  8844. (if a (not b) b))
  8845. (defun org-fixup-message-id-for-http (s)
  8846. "Replace special characters in a message id, so it can be used in an http query."
  8847. (when (string-match "%" s)
  8848. (setq s (mapconcat (lambda (c)
  8849. (if (eq c ?%)
  8850. "%25"
  8851. (char-to-string c)))
  8852. s "")))
  8853. (while (string-match "<" s)
  8854. (setq s (replace-match "%3C" t t s)))
  8855. (while (string-match ">" s)
  8856. (setq s (replace-match "%3E" t t s)))
  8857. (while (string-match "@" s)
  8858. (setq s (replace-match "%40" t t s)))
  8859. s)
  8860. (defun org-link-prettify (link)
  8861. "Return a human-readable representation of LINK.
  8862. The car of LINK must be a raw link the cdr of LINK must be either
  8863. a link description or nil."
  8864. (let ((desc (or (cadr link) "<no description>")))
  8865. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8866. "<" (car link) ">")))
  8867. ;;;###autoload
  8868. (defun org-insert-link-global ()
  8869. "Insert a link like Org-mode does.
  8870. This command can be called in any mode to insert a link in Org-mode syntax."
  8871. (interactive)
  8872. (org-load-modules-maybe)
  8873. (org-run-like-in-org-mode 'org-insert-link))
  8874. (defun org-insert-all-links (&optional keep)
  8875. "Insert all links in `org-stored-links'."
  8876. (interactive "P")
  8877. (let ((links (copy-sequence org-stored-links)) l)
  8878. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8879. (insert "- ")
  8880. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8881. (insert "\n"))))
  8882. (defun org-link-fontify-links-to-this-file ()
  8883. "Fontify links to the current file in `org-stored-links'."
  8884. (let ((f (buffer-file-name)) a b)
  8885. (setq a (mapcar (lambda(l)
  8886. (let ((ll (car l)))
  8887. (when (and (string-match "^file:\\(.+\\)::" ll)
  8888. (equal f (expand-file-name (match-string 1 ll))))
  8889. ll)))
  8890. org-stored-links))
  8891. (when (featurep 'org-id)
  8892. (setq b (mapcar (lambda(l)
  8893. (let ((ll (car l)))
  8894. (when (and (string-match "^id:\\(.+\\)$" ll)
  8895. (equal f (expand-file-name
  8896. (or (org-id-find-id-file
  8897. (match-string 1 ll)) ""))))
  8898. ll)))
  8899. org-stored-links)))
  8900. (mapcar (lambda(l)
  8901. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8902. (delq nil (append a b)))))
  8903. (defvar org-link-links-in-this-file nil)
  8904. (defun org-insert-link (&optional complete-file link-location default-description)
  8905. "Insert a link. At the prompt, enter the link.
  8906. Completion can be used to insert any of the link protocol prefixes like
  8907. http or ftp in use.
  8908. The history can be used to select a link previously stored with
  8909. `org-store-link'. When the empty string is entered (i.e. if you just
  8910. press RET at the prompt), the link defaults to the most recently
  8911. stored link. As SPC triggers completion in the minibuffer, you need to
  8912. use M-SPC or C-q SPC to force the insertion of a space character.
  8913. You will also be prompted for a description, and if one is given, it will
  8914. be displayed in the buffer instead of the link.
  8915. If there is already a link at point, this command will allow you to edit link
  8916. and description parts.
  8917. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8918. be selected using completion. The path to the file will be relative to the
  8919. current directory if the file is in the current directory or a subdirectory.
  8920. Otherwise, the link will be the absolute path as completed in the minibuffer
  8921. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8922. option `org-link-file-path-type'.
  8923. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8924. the current directory or below.
  8925. With three \\[universal-argument] prefixes, negate the meaning of
  8926. `org-keep-stored-link-after-insertion'.
  8927. If `org-make-link-description-function' is non-nil, this function will be
  8928. called with the link target, and the result will be the default
  8929. link description.
  8930. If the LINK-LOCATION parameter is non-nil, this value will be
  8931. used as the link location instead of reading one interactively.
  8932. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8933. be used as the default description."
  8934. (interactive "P")
  8935. (let* ((wcf (current-window-configuration))
  8936. (origbuf (current-buffer))
  8937. (region (if (org-region-active-p)
  8938. (buffer-substring (region-beginning) (region-end))))
  8939. (remove (and region (list (region-beginning) (region-end))))
  8940. (desc region)
  8941. tmphist ; byte-compile incorrectly complains about this
  8942. (link link-location)
  8943. (abbrevs org-link-abbrev-alist-local)
  8944. entry file all-prefixes auto-desc)
  8945. (cond
  8946. (link-location) ; specified by arg, just use it.
  8947. ((org-in-regexp org-bracket-link-regexp 1)
  8948. ;; We do have a link at point, and we are going to edit it.
  8949. (setq remove (list (match-beginning 0) (match-end 0)))
  8950. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8951. (setq link (read-string "Link: "
  8952. (org-link-unescape
  8953. (org-match-string-no-properties 1)))))
  8954. ((or (org-in-regexp org-angle-link-re)
  8955. (org-in-regexp org-plain-link-re))
  8956. ;; Convert to bracket link
  8957. (setq remove (list (match-beginning 0) (match-end 0))
  8958. link (read-string "Link: "
  8959. (org-remove-angle-brackets (match-string 0)))))
  8960. ((member complete-file '((4) (16)))
  8961. ;; Completing read for file names.
  8962. (setq link (org-file-complete-link complete-file)))
  8963. (t
  8964. ;; Read link, with completion for stored links.
  8965. (org-link-fontify-links-to-this-file)
  8966. (org-switch-to-buffer-other-window "*Org Links*")
  8967. (with-current-buffer "*Org Links*"
  8968. (erase-buffer)
  8969. (insert "Insert a link.
  8970. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8971. (when org-stored-links
  8972. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8973. (insert (mapconcat 'org-link-prettify
  8974. (reverse org-stored-links) "\n")))
  8975. (goto-char (point-min)))
  8976. (let ((cw (selected-window)))
  8977. (select-window (get-buffer-window "*Org Links*" 'visible))
  8978. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8979. (unless (pos-visible-in-window-p (point-max))
  8980. (org-fit-window-to-buffer))
  8981. (and (window-live-p cw) (select-window cw)))
  8982. ;; Fake a link history, containing the stored links.
  8983. (setq tmphist (append (mapcar 'car org-stored-links)
  8984. org-insert-link-history))
  8985. (setq all-prefixes (append (mapcar 'car abbrevs)
  8986. (mapcar 'car org-link-abbrev-alist)
  8987. org-link-types))
  8988. (unwind-protect
  8989. (progn
  8990. (setq link
  8991. (org-completing-read
  8992. "Link: "
  8993. (append
  8994. (mapcar (lambda (x) (concat x ":"))
  8995. all-prefixes)
  8996. (mapcar 'car org-stored-links))
  8997. nil nil nil
  8998. 'tmphist
  8999. (caar org-stored-links)))
  9000. (if (not (string-match "\\S-" link))
  9001. (user-error "No link selected"))
  9002. (mapc (lambda(l)
  9003. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9004. org-stored-links)
  9005. (if (or (member link all-prefixes)
  9006. (and (equal ":" (substring link -1))
  9007. (member (substring link 0 -1) all-prefixes)
  9008. (setq link (substring link 0 -1))))
  9009. (setq link (with-current-buffer origbuf
  9010. (org-link-try-special-completion link)))))
  9011. (set-window-configuration wcf)
  9012. (kill-buffer "*Org Links*"))
  9013. (setq entry (assoc link org-stored-links))
  9014. (or entry (push link org-insert-link-history))
  9015. (setq desc (or desc (nth 1 entry)))))
  9016. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9017. (not org-keep-stored-link-after-insertion))
  9018. (setq org-stored-links (delq (assoc link org-stored-links)
  9019. org-stored-links)))
  9020. (if (string-match org-plain-link-re link)
  9021. ;; URL-like link, normalize the use of angular brackets.
  9022. (setq link (org-remove-angle-brackets link)))
  9023. ;; Check if we are linking to the current file with a search
  9024. ;; option If yes, simplify the link by using only the search
  9025. ;; option.
  9026. (when (and buffer-file-name
  9027. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9028. (let* ((path (match-string 1 link))
  9029. (case-fold-search nil)
  9030. (search (match-string 2 link)))
  9031. (save-match-data
  9032. (if (equal (file-truename buffer-file-name) (file-truename path))
  9033. ;; We are linking to this same file, with a search option
  9034. (setq link search)))))
  9035. ;; Check if we can/should use a relative path. If yes, simplify the link
  9036. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9037. (let* ((type (match-string 1 link))
  9038. (path (match-string 2 link))
  9039. (origpath path)
  9040. (case-fold-search nil))
  9041. (cond
  9042. ((or (eq org-link-file-path-type 'absolute)
  9043. (equal complete-file '(16)))
  9044. (setq path (abbreviate-file-name (expand-file-name path))))
  9045. ((eq org-link-file-path-type 'noabbrev)
  9046. (setq path (expand-file-name path)))
  9047. ((eq org-link-file-path-type 'relative)
  9048. (setq path (file-relative-name path)))
  9049. (t
  9050. (save-match-data
  9051. (if (string-match (concat "^" (regexp-quote
  9052. (expand-file-name
  9053. (file-name-as-directory
  9054. default-directory))))
  9055. (expand-file-name path))
  9056. ;; We are linking a file with relative path name.
  9057. (setq path (substring (expand-file-name path)
  9058. (match-end 0)))
  9059. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9060. (setq link (concat type path))
  9061. (if (equal desc origpath)
  9062. (setq desc path))))
  9063. (if org-make-link-description-function
  9064. (setq desc
  9065. (or (condition-case nil
  9066. (funcall org-make-link-description-function link desc)
  9067. (error (progn (message "Can't get link description from `%s'"
  9068. (symbol-name org-make-link-description-function))
  9069. (sit-for 2) nil)))
  9070. (read-string "Description: " default-description)))
  9071. (if default-description (setq desc default-description)
  9072. (setq desc (or (and auto-desc desc)
  9073. (read-string "Description: " desc)))))
  9074. (unless (string-match "\\S-" desc) (setq desc nil))
  9075. (if remove (apply 'delete-region remove))
  9076. (insert (org-make-link-string link desc))))
  9077. (defun org-link-try-special-completion (type)
  9078. "If there is completion support for link type TYPE, offer it."
  9079. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9080. (if (functionp fun)
  9081. (funcall fun)
  9082. (read-string "Link (no completion support): " (concat type ":")))))
  9083. (defun org-file-complete-link (&optional arg)
  9084. "Create a file link using completion."
  9085. (let (file link)
  9086. (setq file (org-iread-file-name "File: "))
  9087. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9088. (pwd1 (file-name-as-directory (abbreviate-file-name
  9089. (expand-file-name ".")))))
  9090. (cond
  9091. ((equal arg '(16))
  9092. (setq link (concat
  9093. "file:"
  9094. (abbreviate-file-name (expand-file-name file)))))
  9095. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9096. (setq link (concat "file:" (match-string 1 file))))
  9097. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9098. (expand-file-name file))
  9099. (setq link (concat
  9100. "file:" (match-string 1 (expand-file-name file)))))
  9101. (t (setq link (concat "file:" file)))))
  9102. link))
  9103. (defun org-iread-file-name (&rest args)
  9104. "Read-file-name using `ido-mode' speedup if available.
  9105. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9106. See `read-file-name' for a description of parameters."
  9107. (org-without-partial-completion
  9108. (if (and org-completion-use-ido
  9109. (fboundp 'ido-read-file-name)
  9110. (boundp 'ido-mode) ido-mode
  9111. (listp (second args)))
  9112. (let ((ido-enter-matching-directory nil))
  9113. (apply 'ido-read-file-name args))
  9114. (apply 'read-file-name args))))
  9115. (defun org-completing-read (&rest args)
  9116. "Completing-read with SPACE being a normal character."
  9117. (let ((enable-recursive-minibuffers t)
  9118. (minibuffer-local-completion-map
  9119. (copy-keymap minibuffer-local-completion-map)))
  9120. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9121. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9122. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9123. (apply 'org-icompleting-read args)))
  9124. (defun org-completing-read-no-i (&rest args)
  9125. (let (org-completion-use-ido org-completion-use-iswitchb)
  9126. (apply 'org-completing-read args)))
  9127. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9128. "Use iswitch as a completing-read replacement to choose from choices.
  9129. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9130. from."
  9131. (let* ((iswitchb-use-virtual-buffers nil)
  9132. (iswitchb-make-buflist-hook
  9133. (lambda ()
  9134. (setq iswitchb-temp-buflist choices))))
  9135. (iswitchb-read-buffer prompt)))
  9136. (defun org-icompleting-read (&rest args)
  9137. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9138. (org-without-partial-completion
  9139. (if (and org-completion-use-ido
  9140. (fboundp 'ido-completing-read)
  9141. (boundp 'ido-mode) ido-mode
  9142. (listp (second args)))
  9143. (let ((ido-enter-matching-directory nil))
  9144. (apply 'ido-completing-read (concat (car args))
  9145. (if (consp (car (nth 1 args)))
  9146. (mapcar 'car (nth 1 args))
  9147. (nth 1 args))
  9148. (cddr args)))
  9149. (if (and org-completion-use-iswitchb
  9150. (boundp 'iswitchb-mode) iswitchb-mode
  9151. (listp (second args)))
  9152. (apply 'org-iswitchb-completing-read (concat (car args))
  9153. (if (consp (car (nth 1 args)))
  9154. (mapcar 'car (nth 1 args))
  9155. (nth 1 args))
  9156. (cddr args))
  9157. (apply 'completing-read args)))))
  9158. (defun org-extract-attributes (s)
  9159. "Extract the attributes cookie from a string and set as text property."
  9160. (let (a attr (start 0) key value)
  9161. (save-match-data
  9162. (when (string-match "{{\\([^}]+\\)}}$" s)
  9163. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9164. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9165. (setq key (match-string 1 a) value (match-string 2 a)
  9166. start (match-end 0)
  9167. attr (plist-put attr (intern key) value))))
  9168. (org-add-props s nil 'org-attr attr))
  9169. s))
  9170. ;;; Opening/following a link
  9171. (defvar org-link-search-failed nil)
  9172. (defvar org-open-link-functions nil
  9173. "Hook for functions finding a plain text link.
  9174. These functions must take a single argument, the link content.
  9175. They will be called for links that look like [[link text][description]]
  9176. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9177. like a filename (e.g. \"./blue.png\").
  9178. These functions will be called *before* Org attempts to resolve the
  9179. link by doing text searches in the current buffer - so if you want a
  9180. link \"[[target]]\" to still find \"<<target>>\", your function should
  9181. handle this as a special case.
  9182. When the function does handle the link, it must return a non-nil value.
  9183. If it decides that it is not responsible for this link, it must return
  9184. nil to indicate that that Org-mode can continue with other options
  9185. like exact and fuzzy text search.")
  9186. (defun org-next-link (&optional search-backward)
  9187. "Move forward to the next link.
  9188. If the link is in hidden text, expose it."
  9189. (interactive "P")
  9190. (when (and org-link-search-failed (eq this-command last-command))
  9191. (goto-char (point-min))
  9192. (message "Link search wrapped back to beginning of buffer"))
  9193. (setq org-link-search-failed nil)
  9194. (let* ((pos (point))
  9195. (ct (org-context))
  9196. (a (assoc :link ct))
  9197. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9198. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9199. ((looking-at org-any-link-re)
  9200. ;; Don't stay stuck at link without an org-link face
  9201. (forward-char (if search-backward -1 1))))
  9202. (if (funcall srch-fun org-any-link-re nil t)
  9203. (progn
  9204. (goto-char (match-beginning 0))
  9205. (if (outline-invisible-p) (org-show-context)))
  9206. (goto-char pos)
  9207. (setq org-link-search-failed t)
  9208. (message "No further link found"))))
  9209. (defun org-previous-link ()
  9210. "Move backward to the previous link.
  9211. If the link is in hidden text, expose it."
  9212. (interactive)
  9213. (funcall 'org-next-link t))
  9214. (defun org-translate-link (s)
  9215. "Translate a link string if a translation function has been defined."
  9216. (if (and org-link-translation-function
  9217. (fboundp org-link-translation-function)
  9218. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9219. (progn
  9220. (setq s (funcall org-link-translation-function
  9221. (match-string 1 s) (match-string 2 s)))
  9222. (concat (car s) ":" (cdr s)))
  9223. s))
  9224. (defun org-translate-link-from-planner (type path)
  9225. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9226. This is still an experimental function, your mileage may vary."
  9227. (cond
  9228. ((member type '("http" "https" "news" "ftp"))
  9229. ;; standard Internet links are the same.
  9230. nil)
  9231. ((and (equal type "irc") (string-match "^//" path))
  9232. ;; Planner has two / at the beginning of an irc link, we have 1.
  9233. ;; We should have zero, actually....
  9234. (setq path (substring path 1)))
  9235. ((and (equal type "lisp") (string-match "^/" path))
  9236. ;; Planner has a slash, we do not.
  9237. (setq type "elisp" path (substring path 1)))
  9238. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9239. ;; A typical message link. Planner has the id after the final slash,
  9240. ;; we separate it with a hash mark
  9241. (setq path (concat (match-string 1 path) "#"
  9242. (org-remove-angle-brackets (match-string 2 path))))))
  9243. (cons type path))
  9244. (defun org-find-file-at-mouse (ev)
  9245. "Open file link or URL at mouse."
  9246. (interactive "e")
  9247. (mouse-set-point ev)
  9248. (org-open-at-point 'in-emacs))
  9249. (defun org-open-at-mouse (ev)
  9250. "Open file link or URL at mouse.
  9251. See the docstring of `org-open-file' for details."
  9252. (interactive "e")
  9253. (mouse-set-point ev)
  9254. (if (eq major-mode 'org-agenda-mode)
  9255. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9256. (org-open-at-point))
  9257. (defvar org-window-config-before-follow-link nil
  9258. "The window configuration before following a link.
  9259. This is saved in case the need arises to restore it.")
  9260. (defvar org-open-link-marker (make-marker)
  9261. "Marker pointing to the location where `org-open-at-point; was called.")
  9262. ;;;###autoload
  9263. (defun org-open-at-point-global ()
  9264. "Follow a link like Org-mode does.
  9265. This command can be called in any mode to follow a link that has
  9266. Org-mode syntax."
  9267. (interactive)
  9268. (org-run-like-in-org-mode 'org-open-at-point))
  9269. ;;;###autoload
  9270. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9271. "Open a link in the string S, as if it was in Org-mode."
  9272. (interactive "sLink: \nP")
  9273. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9274. (with-temp-buffer
  9275. (let ((org-inhibit-startup (not reference-buffer)))
  9276. (org-mode)
  9277. (insert s)
  9278. (goto-char (point-min))
  9279. (when reference-buffer
  9280. (setq org-link-abbrev-alist-local
  9281. (with-current-buffer reference-buffer
  9282. org-link-abbrev-alist-local)))
  9283. (org-open-at-point arg reference-buffer)))))
  9284. (defvar org-open-at-point-functions nil
  9285. "Hook that is run when following a link at point.
  9286. Functions in this hook must return t if they identify and follow
  9287. a link at point. If they don't find anything interesting at point,
  9288. they must return nil.")
  9289. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9290. (defun org-open-at-point (&optional arg reference-buffer)
  9291. "Open link at or after point.
  9292. If there is no link at point, this function will search forward up to
  9293. the end of the current line.
  9294. Normally, files will be opened by an appropriate application. If the
  9295. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9296. With a double prefix argument, try to open outside of Emacs, in the
  9297. application the system uses for this file type."
  9298. (interactive "P")
  9299. ;; if in a code block, then open the block's results
  9300. (unless (call-interactively #'org-babel-open-src-block-result)
  9301. (org-load-modules-maybe)
  9302. (move-marker org-open-link-marker (point))
  9303. (setq org-window-config-before-follow-link (current-window-configuration))
  9304. (org-remove-occur-highlights nil nil t)
  9305. (cond
  9306. ((and (org-at-heading-p)
  9307. (not (org-at-timestamp-p t))
  9308. (not (org-in-regexp
  9309. (concat org-plain-link-re "\\|"
  9310. org-bracket-link-regexp "\\|"
  9311. org-angle-link-re "\\|"
  9312. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9313. (not (get-text-property (point) 'org-linked-text)))
  9314. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9315. (lk0 (car lkall))
  9316. (lk (if (stringp lk0) (list lk0) lk0))
  9317. (lkend (cdr lkall)))
  9318. (mapcar (lambda(l)
  9319. (search-forward l nil lkend)
  9320. (goto-char (match-beginning 0))
  9321. (org-open-at-point))
  9322. lk))
  9323. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9324. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9325. ((and (org-at-timestamp-p t)
  9326. (not (org-in-regexp org-bracket-link-regexp)))
  9327. (org-follow-timestamp-link))
  9328. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9329. (not (org-in-regexp org-any-link-re)))
  9330. (org-footnote-action))
  9331. (t
  9332. (let (type path link line search (pos (point)))
  9333. (catch 'match
  9334. (save-excursion
  9335. (skip-chars-forward "^]\n\r")
  9336. (when (org-in-regexp org-bracket-link-regexp 1)
  9337. (setq link (org-extract-attributes
  9338. (org-link-unescape (org-match-string-no-properties 1))))
  9339. (while (string-match " *\n *" link)
  9340. (setq link (replace-match " " t t link)))
  9341. (setq link (org-link-expand-abbrev link))
  9342. (cond
  9343. ((or (file-name-absolute-p link)
  9344. (string-match "^\\.\\.?/" link))
  9345. (setq type "file" path link))
  9346. ((string-match org-link-re-with-space3 link)
  9347. (setq type (match-string 1 link) path (match-string 2 link)))
  9348. ((string-match "^help:+\\(.+\\)" link)
  9349. (setq type "help" path (match-string 1 link)))
  9350. (t (setq type "thisfile" path link)))
  9351. (throw 'match t)))
  9352. (when (get-text-property (point) 'org-linked-text)
  9353. (setq type "thisfile"
  9354. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9355. (1+ (point)) (point))
  9356. path (buffer-substring
  9357. (or (previous-single-property-change pos 'org-linked-text)
  9358. (point-min))
  9359. (or (next-single-property-change pos 'org-linked-text)
  9360. (point-max)))
  9361. ;; Ensure we will search for a <<<radio>>> link, not
  9362. ;; a simple reference like <<ref>>
  9363. path (concat "<" path))
  9364. (throw 'match t))
  9365. (save-excursion
  9366. (when (or (org-in-regexp org-angle-link-re)
  9367. (let ((match (org-in-regexp org-plain-link-re)))
  9368. ;; Check a plain link is not within a bracket link
  9369. (and match
  9370. (save-excursion
  9371. (progn
  9372. (goto-char (car match))
  9373. (not (org-in-regexp org-bracket-link-regexp))))))
  9374. (let ((line_ending (save-excursion (end-of-line) (point))))
  9375. ;; We are in a line before a plain or bracket link
  9376. (or (re-search-forward org-plain-link-re line_ending t)
  9377. (re-search-forward org-bracket-link-regexp line_ending t))))
  9378. (setq type (match-string 1)
  9379. path (org-link-unescape (match-string 2)))
  9380. (throw 'match t)))
  9381. (save-excursion
  9382. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9383. (setq type "tags"
  9384. path (match-string 1))
  9385. (while (string-match ":" path)
  9386. (setq path (replace-match "+" t t path)))
  9387. (throw 'match t)))
  9388. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9389. (setq type "tree-match"
  9390. path (match-string 1))
  9391. (throw 'match t)))
  9392. (unless path
  9393. (user-error "No link found"))
  9394. ;; switch back to reference buffer
  9395. ;; needed when if called in a temporary buffer through
  9396. ;; org-open-link-from-string
  9397. (with-current-buffer (or reference-buffer (current-buffer))
  9398. ;; Remove any trailing spaces in path
  9399. (if (string-match " +\\'" path)
  9400. (setq path (replace-match "" t t path)))
  9401. (if (and org-link-translation-function
  9402. (fboundp org-link-translation-function))
  9403. ;; Check if we need to translate the link
  9404. (let ((tmp (funcall org-link-translation-function type path)))
  9405. (setq type (car tmp) path (cdr tmp))))
  9406. (cond
  9407. ((assoc type org-link-protocols)
  9408. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9409. ((equal type "help")
  9410. (let ((f-or-v (intern path)))
  9411. (cond ((fboundp f-or-v)
  9412. (describe-function f-or-v))
  9413. ((boundp f-or-v)
  9414. (describe-variable f-or-v))
  9415. (t (error "Not a known function or variable")))))
  9416. ((equal type "mailto")
  9417. (let ((cmd (car org-link-mailto-program))
  9418. (args (cdr org-link-mailto-program)) args1
  9419. (address path) (subject "") a)
  9420. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9421. (setq address (match-string 1 path)
  9422. subject (org-link-escape (match-string 2 path))))
  9423. (while args
  9424. (cond
  9425. ((not (stringp (car args))) (push (pop args) args1))
  9426. (t (setq a (pop args))
  9427. (if (string-match "%a" a)
  9428. (setq a (replace-match address t t a)))
  9429. (if (string-match "%s" a)
  9430. (setq a (replace-match subject t t a)))
  9431. (push a args1))))
  9432. (apply cmd (nreverse args1))))
  9433. ((member type '("http" "https" "ftp" "news"))
  9434. (browse-url
  9435. (concat type ":"
  9436. (if (org-string-match-p
  9437. (concat "[[:nonascii:]"
  9438. org-link-escape-chars-browser "]")
  9439. path)
  9440. (org-link-escape path org-link-escape-chars-browser)
  9441. path))))
  9442. ((string= type "doi")
  9443. (browse-url
  9444. (concat org-doi-server-url
  9445. (if (org-string-match-p
  9446. (concat "[[:nonascii:]"
  9447. org-link-escape-chars-browser "]")
  9448. path)
  9449. (org-link-escape path org-link-escape-chars-browser)
  9450. path))))
  9451. ((member type '("message"))
  9452. (browse-url (concat type ":" path)))
  9453. ((string= type "tags")
  9454. (org-tags-view arg path))
  9455. ((string= type "tree-match")
  9456. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9457. ((string= type "file")
  9458. (if (string-match "::\\([0-9]+\\)\\'" path)
  9459. (setq line (string-to-number (match-string 1 path))
  9460. path (substring path 0 (match-beginning 0)))
  9461. (if (string-match "::\\(.+\\)\\'" path)
  9462. (setq search (match-string 1 path)
  9463. path (substring path 0 (match-beginning 0)))))
  9464. (if (string-match "[*?{]" (file-name-nondirectory path))
  9465. (dired path)
  9466. (org-open-file path arg line search)))
  9467. ((string= type "shell")
  9468. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9469. (cmd path))
  9470. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9471. (string-match org-confirm-shell-link-not-regexp cmd))
  9472. (not org-confirm-shell-link-function)
  9473. (funcall org-confirm-shell-link-function
  9474. (format "Execute \"%s\" in shell? "
  9475. (org-add-props cmd nil
  9476. 'face 'org-warning))))
  9477. (progn
  9478. (message "Executing %s" cmd)
  9479. (shell-command cmd buf)
  9480. (if (featurep 'midnight)
  9481. (setq clean-buffer-list-kill-buffer-names
  9482. (cons buf clean-buffer-list-kill-buffer-names))))
  9483. (error "Abort"))))
  9484. ((string= type "elisp")
  9485. (let ((cmd path))
  9486. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9487. (string-match org-confirm-elisp-link-not-regexp cmd))
  9488. (not org-confirm-elisp-link-function)
  9489. (funcall org-confirm-elisp-link-function
  9490. (format "Execute \"%s\" as elisp? "
  9491. (org-add-props cmd nil
  9492. 'face 'org-warning))))
  9493. (message "%s => %s" cmd
  9494. (if (equal (string-to-char cmd) ?\()
  9495. (eval (read cmd))
  9496. (call-interactively (read cmd))))
  9497. (error "Abort"))))
  9498. ((and (string= type "thisfile")
  9499. (run-hook-with-args-until-success
  9500. 'org-open-link-functions path)))
  9501. ((string= type "thisfile")
  9502. (if arg
  9503. (switch-to-buffer-other-window
  9504. (org-get-buffer-for-internal-link (current-buffer)))
  9505. (org-mark-ring-push))
  9506. (let ((cmd `(org-link-search
  9507. ,path
  9508. ,(cond ((equal arg '(4)) ''occur)
  9509. ((equal arg '(16)) ''org-occur))
  9510. ,pos)))
  9511. (condition-case nil (let ((org-link-search-inhibit-query t))
  9512. (eval cmd))
  9513. (error (progn (widen) (eval cmd))))))
  9514. (t (browse-url-at-point)))))))
  9515. (move-marker org-open-link-marker nil)
  9516. (run-hook-with-args 'org-follow-link-hook)))
  9517. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9518. "Offer links in the current entry and return the selected link.
  9519. If there is only one link, return it.
  9520. If NTH is an integer, return the NTH link found.
  9521. If ZERO is a string, check also this string for a link, and if
  9522. there is one, return it."
  9523. (with-current-buffer buffer
  9524. (save-excursion
  9525. (save-restriction
  9526. (widen)
  9527. (goto-char marker)
  9528. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9529. "\\(" org-angle-link-re "\\)\\|"
  9530. "\\(" org-plain-link-re "\\)"))
  9531. (cnt ?0)
  9532. (in-emacs (if (integerp nth) nil nth))
  9533. have-zero end links link c)
  9534. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9535. (push (match-string 0 zero) links)
  9536. (setq cnt (1- cnt) have-zero t))
  9537. (save-excursion
  9538. (org-back-to-heading t)
  9539. (setq end (save-excursion (outline-next-heading) (point)))
  9540. (while (re-search-forward re end t)
  9541. (push (match-string 0) links))
  9542. (setq links (org-uniquify (reverse links))))
  9543. (cond
  9544. ((null links)
  9545. (message "No links"))
  9546. ((equal (length links) 1)
  9547. (setq link (car links)))
  9548. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9549. (setq link (nth (if have-zero nth (1- nth)) links)))
  9550. (t ; we have to select a link
  9551. (save-excursion
  9552. (save-window-excursion
  9553. (delete-other-windows)
  9554. (with-output-to-temp-buffer "*Select Link*"
  9555. (mapc (lambda (l)
  9556. (if (not (string-match org-bracket-link-regexp l))
  9557. (princ (format "[%c] %s\n" (incf cnt)
  9558. (org-remove-angle-brackets l)))
  9559. (if (match-end 3)
  9560. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9561. (match-string 3 l) (match-string 1 l)))
  9562. (princ (format "[%c] %s\n" (incf cnt)
  9563. (match-string 1 l))))))
  9564. links))
  9565. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9566. (message "Select link to open, RET to open all:")
  9567. (setq c (read-char-exclusive))
  9568. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9569. (when (equal c ?q) (error "Abort"))
  9570. (if (equal c ?\C-m)
  9571. (setq link links)
  9572. (setq nth (- c ?0))
  9573. (if have-zero (setq nth (1+ nth)))
  9574. (unless (and (integerp nth) (>= (length links) nth))
  9575. (user-error "Invalid link selection"))
  9576. (setq link (nth (1- nth) links)))))
  9577. (cons link end))))))
  9578. ;; Add special file links that specify the way of opening
  9579. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9580. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9581. (defun org-open-file-with-system (path)
  9582. "Open file at PATH using the system way of opening it."
  9583. (org-open-file path 'system))
  9584. (defun org-open-file-with-emacs (path)
  9585. "Open file at PATH in Emacs."
  9586. (org-open-file path 'emacs))
  9587. ;;; File search
  9588. (defvar org-create-file-search-functions nil
  9589. "List of functions to construct the right search string for a file link.
  9590. These functions are called in turn with point at the location to
  9591. which the link should point.
  9592. A function in the hook should first test if it would like to
  9593. handle this file type, for example by checking the `major-mode'
  9594. or the file extension. If it decides not to handle this file, it
  9595. should just return nil to give other functions a chance. If it
  9596. does handle the file, it must return the search string to be used
  9597. when following the link. The search string will be part of the
  9598. file link, given after a double colon, and `org-open-at-point'
  9599. will automatically search for it. If special measures must be
  9600. taken to make the search successful, another function should be
  9601. added to the companion hook `org-execute-file-search-functions',
  9602. which see.
  9603. A function in this hook may also use `setq' to set the variable
  9604. `description' to provide a suggestion for the descriptive text to
  9605. be used for this link when it gets inserted into an Org-mode
  9606. buffer with \\[org-insert-link].")
  9607. (defvar org-execute-file-search-functions nil
  9608. "List of functions to execute a file search triggered by a link.
  9609. Functions added to this hook must accept a single argument, the
  9610. search string that was part of the file link, the part after the
  9611. double colon. The function must first check if it would like to
  9612. handle this search, for example by checking the `major-mode' or
  9613. the file extension. If it decides not to handle this search, it
  9614. should just return nil to give other functions a chance. If it
  9615. does handle the search, it must return a non-nil value to keep
  9616. other functions from trying.
  9617. Each function can access the current prefix argument through the
  9618. variable `current-prefix-arg'. Note that a single prefix is used
  9619. to force opening a link in Emacs, so it may be good to only use a
  9620. numeric or double prefix to guide the search function.
  9621. In case this is needed, a function in this hook can also restore
  9622. the window configuration before `org-open-at-point' was called using:
  9623. (set-window-configuration org-window-config-before-follow-link)")
  9624. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9625. (defun org-link-search (s &optional type avoid-pos stealth)
  9626. "Search for a link search option.
  9627. If S is surrounded by forward slashes, it is interpreted as a
  9628. regular expression. In org-mode files, this will create an `org-occur'
  9629. sparse tree. In ordinary files, `occur' will be used to list matches.
  9630. If the current buffer is in `dired-mode', grep will be used to search
  9631. in all files. If AVOID-POS is given, ignore matches near that position.
  9632. When optional argument STEALTH is non-nil, do not modify
  9633. visibility around point, thus ignoring
  9634. `org-show-hierarchy-above', `org-show-following-heading' and
  9635. `org-show-siblings' variables."
  9636. (let ((case-fold-search t)
  9637. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9638. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9639. (append '(("") (" ") ("\t") ("\n"))
  9640. org-emphasis-alist)
  9641. "\\|") "\\)"))
  9642. (pos (point))
  9643. (pre nil) (post nil)
  9644. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9645. (cond
  9646. ;; First check if there are any special search functions
  9647. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9648. ;; Now try the builtin stuff
  9649. ((and (equal (string-to-char s0) ?#)
  9650. (> (length s0) 1)
  9651. (save-excursion
  9652. (goto-char (point-min))
  9653. (and
  9654. (re-search-forward
  9655. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9656. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9657. (setq type 'dedicated
  9658. pos (match-beginning 0))))
  9659. ;; There is an exact target for this
  9660. (goto-char pos)
  9661. (org-back-to-heading t)))
  9662. ((save-excursion
  9663. (goto-char (point-min))
  9664. (and
  9665. (re-search-forward
  9666. (concat "<<" (regexp-quote s0) ">>") nil t)
  9667. (setq type 'dedicated
  9668. pos (match-beginning 0))))
  9669. ;; There is an exact target for this
  9670. (goto-char pos))
  9671. ((save-excursion
  9672. (goto-char (point-min))
  9673. (and
  9674. (re-search-forward
  9675. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9676. (setq type 'dedicated pos (match-beginning 0))))
  9677. ;; Found an element with a matching #+name affiliated keyword.
  9678. (goto-char pos))
  9679. ((and (string-match "^(\\(.*\\))$" s0)
  9680. (save-excursion
  9681. (goto-char (point-min))
  9682. (and
  9683. (re-search-forward
  9684. (concat "[^[]" (regexp-quote
  9685. (format org-coderef-label-format
  9686. (match-string 1 s0))))
  9687. nil t)
  9688. (setq type 'dedicated
  9689. pos (1+ (match-beginning 0))))))
  9690. ;; There is a coderef target for this
  9691. (goto-char pos))
  9692. ((string-match "^/\\(.*\\)/$" s)
  9693. ;; A regular expression
  9694. (cond
  9695. ((derived-mode-p 'org-mode)
  9696. (org-occur (match-string 1 s)))
  9697. (t (org-do-occur (match-string 1 s)))))
  9698. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9699. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9700. (goto-char (point-min))
  9701. (cond
  9702. ((let (case-fold-search)
  9703. (re-search-forward (format org-complex-heading-regexp-format
  9704. (regexp-quote s))
  9705. nil t))
  9706. ;; OK, found a match
  9707. (setq type 'dedicated)
  9708. (goto-char (match-beginning 0)))
  9709. ((and (not org-link-search-inhibit-query)
  9710. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9711. (y-or-n-p "No match - create this as a new heading? "))
  9712. (goto-char (point-max))
  9713. (or (bolp) (newline))
  9714. (insert "* " s "\n")
  9715. (beginning-of-line 0))
  9716. (t
  9717. (goto-char pos)
  9718. (error "No match"))))
  9719. (t
  9720. ;; A normal search string
  9721. (when (equal (string-to-char s) ?*)
  9722. ;; Anchor on headlines, post may include tags.
  9723. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9724. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9725. s (substring s 1)))
  9726. (remove-text-properties
  9727. 0 (length s)
  9728. '(face nil mouse-face nil keymap nil fontified nil) s)
  9729. ;; Make a series of regular expressions to find a match
  9730. (setq words (org-split-string s "[ \n\r\t]+")
  9731. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9732. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9733. "\\)" markers)
  9734. re2a_ (concat "\\(" (mapconcat 'downcase words
  9735. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9736. re2a (concat "[ \t\r\n]" re2a_)
  9737. re4_ (concat "\\(" (mapconcat 'downcase words
  9738. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9739. re4 (concat "[^a-zA-Z_]" re4_)
  9740. re1 (concat pre re2 post)
  9741. re3 (concat pre (if pre re4_ re4) post)
  9742. re5 (concat pre ".*" re4)
  9743. re2 (concat pre re2)
  9744. re2a (concat pre (if pre re2a_ re2a))
  9745. re4 (concat pre (if pre re4_ re4))
  9746. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9747. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9748. re5 "\\)"))
  9749. (cond
  9750. ((eq type 'org-occur) (org-occur reall))
  9751. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9752. (t (goto-char (point-min))
  9753. (setq type 'fuzzy)
  9754. (if (or (and (org-search-not-self 1 re0 nil t)
  9755. (setq type 'dedicated))
  9756. (org-search-not-self 1 re1 nil t)
  9757. (org-search-not-self 1 re2 nil t)
  9758. (org-search-not-self 1 re2a nil t)
  9759. (org-search-not-self 1 re3 nil t)
  9760. (org-search-not-self 1 re4 nil t)
  9761. (org-search-not-self 1 re5 nil t))
  9762. (goto-char (match-beginning 1))
  9763. (goto-char pos)
  9764. (error "No match"))))))
  9765. (and (derived-mode-p 'org-mode)
  9766. (not stealth)
  9767. (org-show-context 'link-search))
  9768. type))
  9769. (defun org-search-not-self (group &rest args)
  9770. "Execute `re-search-forward', but only accept matches that do not
  9771. enclose the position of `org-open-link-marker'."
  9772. (let ((m org-open-link-marker))
  9773. (catch 'exit
  9774. (while (apply 're-search-forward args)
  9775. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9776. (goto-char (match-end group))
  9777. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9778. (> (match-beginning 0) (marker-position m))
  9779. (< (match-end 0) (marker-position m)))
  9780. (save-match-data
  9781. (or (not (org-in-regexp
  9782. org-bracket-link-analytic-regexp 1))
  9783. (not (match-end 4)) ; no description
  9784. (and (<= (match-beginning 4) (point))
  9785. (>= (match-end 4) (point))))))
  9786. (throw 'exit (point))))))))
  9787. (defun org-get-buffer-for-internal-link (buffer)
  9788. "Return a buffer to be used for displaying the link target of internal links."
  9789. (cond
  9790. ((not org-display-internal-link-with-indirect-buffer)
  9791. buffer)
  9792. ((string-match "(Clone)$" (buffer-name buffer))
  9793. (message "Buffer is already a clone, not making another one")
  9794. ;; we also do not modify visibility in this case
  9795. buffer)
  9796. (t ; make a new indirect buffer for displaying the link
  9797. (let* ((bn (buffer-name buffer))
  9798. (ibn (concat bn "(Clone)"))
  9799. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9800. (with-current-buffer ib (org-overview))
  9801. ib))))
  9802. (defun org-do-occur (regexp &optional cleanup)
  9803. "Call the Emacs command `occur'.
  9804. If CLEANUP is non-nil, remove the printout of the regular expression
  9805. in the *Occur* buffer. This is useful if the regex is long and not useful
  9806. to read."
  9807. (occur regexp)
  9808. (when cleanup
  9809. (let ((cwin (selected-window)) win beg end)
  9810. (when (setq win (get-buffer-window "*Occur*"))
  9811. (select-window win))
  9812. (goto-char (point-min))
  9813. (when (re-search-forward "match[a-z]+" nil t)
  9814. (setq beg (match-end 0))
  9815. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9816. (setq end (1- (match-beginning 0)))))
  9817. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9818. (goto-char (point-min))
  9819. (select-window cwin))))
  9820. ;;; The mark ring for links jumps
  9821. (defvar org-mark-ring nil
  9822. "Mark ring for positions before jumps in Org-mode.")
  9823. (defvar org-mark-ring-last-goto nil
  9824. "Last position in the mark ring used to go back.")
  9825. ;; Fill and close the ring
  9826. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9827. (loop for i from 1 to org-mark-ring-length do
  9828. (push (make-marker) org-mark-ring))
  9829. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9830. org-mark-ring)
  9831. (defun org-mark-ring-push (&optional pos buffer)
  9832. "Put the current position or POS into the mark ring and rotate it."
  9833. (interactive)
  9834. (setq pos (or pos (point)))
  9835. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9836. (move-marker (car org-mark-ring)
  9837. (or pos (point))
  9838. (or buffer (current-buffer)))
  9839. (message "%s"
  9840. (substitute-command-keys
  9841. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9842. (defun org-mark-ring-goto (&optional n)
  9843. "Jump to the previous position in the mark ring.
  9844. With prefix arg N, jump back that many stored positions. When
  9845. called several times in succession, walk through the entire ring.
  9846. Org-mode commands jumping to a different position in the current file,
  9847. or to another Org-mode file, automatically push the old position
  9848. onto the ring."
  9849. (interactive "p")
  9850. (let (p m)
  9851. (if (eq last-command this-command)
  9852. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9853. (setq p org-mark-ring))
  9854. (setq org-mark-ring-last-goto p)
  9855. (setq m (car p))
  9856. (org-pop-to-buffer-same-window (marker-buffer m))
  9857. (goto-char m)
  9858. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9859. (defun org-remove-angle-brackets (s)
  9860. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9861. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9862. s)
  9863. (defun org-add-angle-brackets (s)
  9864. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9865. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9866. s)
  9867. (defun org-remove-double-quotes (s)
  9868. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9869. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9870. s)
  9871. ;;; Following specific links
  9872. (defun org-follow-timestamp-link ()
  9873. "Open an agenda view for the time-stamp date/range at point."
  9874. (cond
  9875. ((org-at-date-range-p t)
  9876. (let ((org-agenda-start-on-weekday)
  9877. (t1 (match-string 1))
  9878. (t2 (match-string 2)) tt1 tt2)
  9879. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9880. tt2 (time-to-days (org-time-string-to-time t2)))
  9881. (let ((org-agenda-buffer-tmp-name
  9882. (format "*Org Agenda(a:%s)"
  9883. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9884. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9885. ((org-at-timestamp-p t)
  9886. (let ((org-agenda-buffer-tmp-name
  9887. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9888. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9889. (substring (match-string 1) 0 10)))
  9890. 1)))
  9891. (t (error "This should not happen"))))
  9892. ;;; Following file links
  9893. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9894. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9895. (declare-function mailcap-mime-info
  9896. "mailcap" (string &optional request no-decode))
  9897. (defvar org-wait nil)
  9898. (defun org-open-file (path &optional in-emacs line search)
  9899. "Open the file at PATH.
  9900. First, this expands any special file name abbreviations. Then the
  9901. configuration variable `org-file-apps' is checked if it contains an
  9902. entry for this file type, and if yes, the corresponding command is launched.
  9903. If no application is found, Emacs simply visits the file.
  9904. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9905. With a double \\[universal-argument] \\[universal-argument] \
  9906. prefix arg, Org tries to avoid opening in Emacs
  9907. and to use an external application to visit the file.
  9908. Optional LINE specifies a line to go to, optional SEARCH a string
  9909. to search for. If LINE or SEARCH is given, the file will be
  9910. opened in Emacs, unless an entry from org-file-apps that makes
  9911. use of groups in a regexp matches.
  9912. If you want to change the way frames are used when following a
  9913. link, please customize `org-link-frame-setup'.
  9914. If the file does not exist, an error is thrown."
  9915. (let* ((file (if (equal path "")
  9916. buffer-file-name
  9917. (substitute-in-file-name (expand-file-name path))))
  9918. (file-apps (append org-file-apps (org-default-apps)))
  9919. (apps (org-remove-if
  9920. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9921. (apps-dlink (org-remove-if-not
  9922. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9923. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9924. (dirp (if remp nil (file-directory-p file)))
  9925. (file (if (and dirp org-open-directory-means-index-dot-org)
  9926. (concat (file-name-as-directory file) "index.org")
  9927. file))
  9928. (a-m-a-p (assq 'auto-mode apps))
  9929. (dfile (downcase file))
  9930. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9931. (link (cond ((and (eq line nil)
  9932. (eq search nil))
  9933. file)
  9934. (line
  9935. (concat file "::" (number-to-string line)))
  9936. (search
  9937. (concat file "::" search))))
  9938. (dlink (downcase link))
  9939. (old-buffer (current-buffer))
  9940. (old-pos (point))
  9941. (old-mode major-mode)
  9942. ext cmd link-match-data)
  9943. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9944. (setq ext (match-string 1 dfile))
  9945. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9946. (setq ext (match-string 1 dfile))))
  9947. (cond
  9948. ((member in-emacs '((16) system))
  9949. (setq cmd (cdr (assoc 'system apps))))
  9950. (in-emacs (setq cmd 'emacs))
  9951. (t
  9952. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9953. (and dirp (cdr (assoc 'directory apps)))
  9954. ; first, try matching against apps-dlink
  9955. ; if we get a match here, store the match data for later
  9956. (let ((match (assoc-default dlink apps-dlink
  9957. 'string-match)))
  9958. (if match
  9959. (progn (setq link-match-data (match-data))
  9960. match)
  9961. (progn (setq in-emacs (or in-emacs line search))
  9962. nil))) ; if we have no match in apps-dlink,
  9963. ; always open the file in emacs if line or search
  9964. ; is given (for backwards compatibility)
  9965. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9966. 'string-match)
  9967. (cdr (assoc ext apps))
  9968. (cdr (assoc t apps))))))
  9969. (when (eq cmd 'system)
  9970. (setq cmd (cdr (assoc 'system apps))))
  9971. (when (eq cmd 'default)
  9972. (setq cmd (cdr (assoc t apps))))
  9973. (when (eq cmd 'mailcap)
  9974. (require 'mailcap)
  9975. (mailcap-parse-mailcaps)
  9976. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9977. (command (mailcap-mime-info mime-type)))
  9978. (if (stringp command)
  9979. (setq cmd command)
  9980. (setq cmd 'emacs))))
  9981. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9982. (not (file-exists-p file))
  9983. (not org-open-non-existing-files))
  9984. (user-error "No such file: %s" file))
  9985. (cond
  9986. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9987. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9988. (while (string-match "['\"]%s['\"]" cmd)
  9989. (setq cmd (replace-match "%s" t t cmd)))
  9990. (while (string-match "%s" cmd)
  9991. (setq cmd (replace-match
  9992. (save-match-data
  9993. (shell-quote-argument
  9994. (convert-standard-filename file)))
  9995. t t cmd)))
  9996. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9997. (save-match-data
  9998. (let ((match-index 1)
  9999. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10000. (set-match-data link-match-data)
  10001. (while (<= match-index number-of-groups)
  10002. (let ((regex (concat "%" (number-to-string match-index)))
  10003. (replace-with (match-string match-index dlink)))
  10004. (while (string-match regex cmd)
  10005. (setq cmd (replace-match replace-with t t cmd))))
  10006. (setq match-index (+ match-index 1)))))
  10007. (save-window-excursion
  10008. (message "Running %s...done" cmd)
  10009. (start-process-shell-command cmd nil cmd)
  10010. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10011. ((or (stringp cmd)
  10012. (eq cmd 'emacs))
  10013. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10014. (widen)
  10015. (if line (org-goto-line line)
  10016. (if search (org-link-search search))))
  10017. ((consp cmd)
  10018. (let ((file (convert-standard-filename file)))
  10019. (save-match-data
  10020. (set-match-data link-match-data)
  10021. (eval cmd))))
  10022. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10023. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10024. (or (not (equal old-buffer (current-buffer)))
  10025. (not (equal old-pos (point))))
  10026. (org-mark-ring-push old-pos old-buffer))))
  10027. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10028. "This function returns non-nil if `entry' uses a regular
  10029. expression which should be matched against the whole link by
  10030. org-open-file.
  10031. It assumes that is the case when the entry uses a regular
  10032. expression which has at least one grouping construct and the
  10033. action is either a lisp form or a command string containing
  10034. '%1', i.e. using at least one subexpression match as a
  10035. parameter."
  10036. (let ((selector (car entry))
  10037. (action (cdr entry)))
  10038. (if (stringp selector)
  10039. (and (> (regexp-opt-depth selector) 0)
  10040. (or (and (stringp action)
  10041. (string-match "%[0-9]" action))
  10042. (consp action)))
  10043. nil)))
  10044. (defun org-default-apps ()
  10045. "Return the default applications for this operating system."
  10046. (cond
  10047. ((eq system-type 'darwin)
  10048. org-file-apps-defaults-macosx)
  10049. ((eq system-type 'windows-nt)
  10050. org-file-apps-defaults-windowsnt)
  10051. (t org-file-apps-defaults-gnu)))
  10052. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10053. "Convert extensions to regular expressions in the cars of LIST.
  10054. Also, weed out any non-string entries, because the return value is used
  10055. only for regexp matching.
  10056. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10057. point to the symbol `emacs', indicating that the file should
  10058. be opened in Emacs."
  10059. (append
  10060. (delq nil
  10061. (mapcar (lambda (x)
  10062. (if (not (stringp (car x)))
  10063. nil
  10064. (if (string-match "\\W" (car x))
  10065. x
  10066. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10067. list))
  10068. (if add-auto-mode
  10069. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10070. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10071. (defun org-file-remote-p (file)
  10072. "Test whether FILE specifies a location on a remote system.
  10073. Return non-nil if the location is indeed remote.
  10074. For example, the filename \"/user@host:/foo\" specifies a location
  10075. on the system \"/user@host:\"."
  10076. (cond ((fboundp 'file-remote-p)
  10077. (file-remote-p file))
  10078. ((fboundp 'tramp-handle-file-remote-p)
  10079. (tramp-handle-file-remote-p file))
  10080. ((and (boundp 'ange-ftp-name-format)
  10081. (string-match (car ange-ftp-name-format) file))
  10082. t)))
  10083. ;;;; Refiling
  10084. (defun org-get-org-file ()
  10085. "Read a filename, with default directory `org-directory'."
  10086. (let ((default (or org-default-notes-file remember-data-file)))
  10087. (read-file-name (format "File name [%s]: " default)
  10088. (file-name-as-directory org-directory)
  10089. default)))
  10090. (defun org-notes-order-reversed-p ()
  10091. "Check if the current file should receive notes in reversed order."
  10092. (cond
  10093. ((not org-reverse-note-order) nil)
  10094. ((eq t org-reverse-note-order) t)
  10095. ((not (listp org-reverse-note-order)) nil)
  10096. (t (catch 'exit
  10097. (let ((all org-reverse-note-order)
  10098. entry)
  10099. (while (setq entry (pop all))
  10100. (if (string-match (car entry) buffer-file-name)
  10101. (throw 'exit (cdr entry))))
  10102. nil)))))
  10103. (defvar org-refile-target-table nil
  10104. "The list of refile targets, created by `org-refile'.")
  10105. (defvar org-agenda-new-buffers nil
  10106. "Buffers created to visit agenda files.")
  10107. (defvar org-refile-cache nil
  10108. "Cache for refile targets.")
  10109. (defvar org-refile-markers nil
  10110. "All the markers used for caching refile locations.")
  10111. (defun org-refile-marker (pos)
  10112. "Get a new refile marker, but only if caching is in use."
  10113. (if (not org-refile-use-cache)
  10114. pos
  10115. (let ((m (make-marker)))
  10116. (move-marker m pos)
  10117. (push m org-refile-markers)
  10118. m)))
  10119. (defun org-refile-cache-clear ()
  10120. "Clear the refile cache and disable all the markers."
  10121. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10122. (setq org-refile-markers nil)
  10123. (setq org-refile-cache nil)
  10124. (message "Refile cache has been cleared"))
  10125. (defun org-refile-cache-check-set (set)
  10126. "Check if all the markers in the cache still have live buffers."
  10127. (let (marker)
  10128. (catch 'exit
  10129. (while (and set (setq marker (nth 3 (pop set))))
  10130. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10131. (when (and marker (null (marker-buffer marker)))
  10132. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10133. (sit-for 3)
  10134. (throw 'exit nil)))
  10135. t)))
  10136. (defun org-refile-cache-put (set &rest identifiers)
  10137. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10138. (let* ((key (sha1 (prin1-to-string identifiers)))
  10139. (entry (assoc key org-refile-cache)))
  10140. (if entry
  10141. (setcdr entry set)
  10142. (push (cons key set) org-refile-cache))))
  10143. (defun org-refile-cache-get (&rest identifiers)
  10144. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10145. (cond
  10146. ((not org-refile-cache) nil)
  10147. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10148. (t
  10149. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10150. org-refile-cache))))
  10151. (and set (org-refile-cache-check-set set) set)))))
  10152. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10153. "Produce a table with refile targets."
  10154. (let ((case-fold-search nil)
  10155. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10156. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10157. targets tgs txt re files f desc descre fast-path-p level pos0)
  10158. (message "Getting targets...")
  10159. (with-current-buffer (or default-buffer (current-buffer))
  10160. (while (setq entry (pop entries))
  10161. (setq files (car entry) desc (cdr entry))
  10162. (setq fast-path-p nil)
  10163. (cond
  10164. ((null files) (setq files (list (current-buffer))))
  10165. ((eq files 'org-agenda-files)
  10166. (setq files (org-agenda-files 'unrestricted)))
  10167. ((and (symbolp files) (fboundp files))
  10168. (setq files (funcall files)))
  10169. ((and (symbolp files) (boundp files))
  10170. (setq files (symbol-value files))))
  10171. (if (stringp files) (setq files (list files)))
  10172. (cond
  10173. ((eq (car desc) :tag)
  10174. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10175. ((eq (car desc) :todo)
  10176. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10177. ((eq (car desc) :regexp)
  10178. (setq descre (cdr desc)))
  10179. ((eq (car desc) :level)
  10180. (setq descre (concat "^\\*\\{" (number-to-string
  10181. (if org-odd-levels-only
  10182. (1- (* 2 (cdr desc)))
  10183. (cdr desc)))
  10184. "\\}[ \t]")))
  10185. ((eq (car desc) :maxlevel)
  10186. (setq fast-path-p t)
  10187. (setq descre (concat "^\\*\\{1," (number-to-string
  10188. (if org-odd-levels-only
  10189. (1- (* 2 (cdr desc)))
  10190. (cdr desc)))
  10191. "\\}[ \t]")))
  10192. (t (error "Bad refiling target description %s" desc)))
  10193. (while (setq f (pop files))
  10194. (with-current-buffer
  10195. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10196. (or
  10197. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10198. (progn
  10199. (if (bufferp f) (setq f (buffer-file-name
  10200. (buffer-base-buffer f))))
  10201. (setq f (and f (expand-file-name f)))
  10202. (if (eq org-refile-use-outline-path 'file)
  10203. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10204. (save-excursion
  10205. (save-restriction
  10206. (widen)
  10207. (goto-char (point-min))
  10208. (while (re-search-forward descre nil t)
  10209. (goto-char (setq pos0 (point-at-bol)))
  10210. (catch 'next
  10211. (when org-refile-target-verify-function
  10212. (save-match-data
  10213. (or (funcall org-refile-target-verify-function)
  10214. (throw 'next t))))
  10215. (when (and (looking-at org-complex-heading-regexp)
  10216. (not (member (match-string 4) excluded-entries))
  10217. (match-string 4))
  10218. (setq level (org-reduced-level
  10219. (- (match-end 1) (match-beginning 1)))
  10220. txt (org-link-display-format (match-string 4))
  10221. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10222. re (format org-complex-heading-regexp-format
  10223. (regexp-quote (match-string 4))))
  10224. (when org-refile-use-outline-path
  10225. (setq txt (mapconcat
  10226. 'org-protect-slash
  10227. (append
  10228. (if (eq org-refile-use-outline-path
  10229. 'file)
  10230. (list (file-name-nondirectory
  10231. (buffer-file-name
  10232. (buffer-base-buffer))))
  10233. (if (eq org-refile-use-outline-path
  10234. 'full-file-path)
  10235. (list (buffer-file-name
  10236. (buffer-base-buffer)))))
  10237. (org-get-outline-path fast-path-p
  10238. level txt)
  10239. (list txt))
  10240. "/")))
  10241. (push (list txt f re (org-refile-marker (point)))
  10242. tgs)))
  10243. (when (= (point) pos0)
  10244. ;; verification function has not moved point
  10245. (goto-char (point-at-eol))))))))
  10246. (when org-refile-use-cache
  10247. (org-refile-cache-put tgs (buffer-file-name) descre))
  10248. (setq targets (append tgs targets))))))
  10249. (message "Getting targets...done")
  10250. (nreverse targets)))
  10251. (defun org-protect-slash (s)
  10252. (while (string-match "/" s)
  10253. (setq s (replace-match "\\" t t s)))
  10254. s)
  10255. (defvar org-olpa (make-vector 20 nil))
  10256. (defun org-get-outline-path (&optional fastp level heading)
  10257. "Return the outline path to the current entry, as a list.
  10258. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10259. routine which makes outline path derivations for an entire file,
  10260. avoiding backtracing. Refile target collection makes use of that."
  10261. (if fastp
  10262. (progn
  10263. (if (> level 19)
  10264. (error "Outline path failure, more than 19 levels"))
  10265. (loop for i from level upto 19 do
  10266. (aset org-olpa i nil))
  10267. (prog1
  10268. (delq nil (append org-olpa nil))
  10269. (aset org-olpa level heading)))
  10270. (let (rtn case-fold-search)
  10271. (save-excursion
  10272. (save-restriction
  10273. (widen)
  10274. (while (org-up-heading-safe)
  10275. (when (looking-at org-complex-heading-regexp)
  10276. (push (org-trim
  10277. (replace-regexp-in-string
  10278. ;; Remove statistical/checkboxes cookies
  10279. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10280. (org-match-string-no-properties 4)))
  10281. rtn)))
  10282. rtn)))))
  10283. (defun org-format-outline-path (path &optional width prefix separator)
  10284. "Format the outline path PATH for display.
  10285. WIDTH is the maximum number of characters that is available.
  10286. PREFIX is a prefix to be included in the returned string,
  10287. such as the file name.
  10288. SEPARATOR is inserted between the different parts of the path,
  10289. the default is \"/\"."
  10290. (setq width (or width 79))
  10291. (if prefix (setq width (- width (length prefix))))
  10292. (if (not path)
  10293. (or prefix "")
  10294. (let* ((nsteps (length path))
  10295. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10296. (maxwidth (if (<= total-width width)
  10297. 10000 ;; everything fits
  10298. ;; we need to shorten the level headings
  10299. (/ (- width nsteps) nsteps)))
  10300. (org-odd-levels-only nil)
  10301. (n 0)
  10302. (total (1+ (length prefix))))
  10303. (setq maxwidth (max maxwidth 10))
  10304. (concat prefix
  10305. (if prefix (or separator "/"))
  10306. (mapconcat
  10307. (lambda (h)
  10308. (setq n (1+ n))
  10309. (if (and (= n nsteps) (< maxwidth 10000))
  10310. (setq maxwidth (- total-width total)))
  10311. (if (< (length h) maxwidth)
  10312. (progn (setq total (+ total (length h) 1)) h)
  10313. (setq h (substring h 0 (- maxwidth 2))
  10314. total (+ total maxwidth 1))
  10315. (if (string-match "[ \t]+\\'" h)
  10316. (setq h (substring h 0 (match-beginning 0))))
  10317. (setq h (concat h "..")))
  10318. (org-add-props h nil 'face
  10319. (nth (% (1- n) org-n-level-faces)
  10320. org-level-faces))
  10321. h)
  10322. path (or separator "/"))))))
  10323. (defun org-display-outline-path (&optional file current separator just-return-string)
  10324. "Display the current outline path in the echo area.
  10325. If FILE is non-nil, prepend the output with the file name.
  10326. If CURRENT is non-nil, append the current heading to the output.
  10327. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10328. the different parts of the path and defaults to \"/\".
  10329. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10330. (interactive "P")
  10331. (let* (case-fold-search
  10332. message-log-max ; Don't populate the *Messages* buffer
  10333. (bfn (buffer-file-name (buffer-base-buffer)))
  10334. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10335. res)
  10336. (if current (setq path (append path
  10337. (save-excursion
  10338. (org-back-to-heading t)
  10339. (if (looking-at org-complex-heading-regexp)
  10340. (list (match-string 4)))))))
  10341. (setq res
  10342. (org-format-outline-path
  10343. path
  10344. (1- (frame-width))
  10345. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10346. separator))
  10347. (if just-return-string
  10348. (org-no-properties res)
  10349. (message "%s" res))))
  10350. (defvar org-refile-history nil
  10351. "History for refiling operations.")
  10352. (defvar org-after-refile-insert-hook nil
  10353. "Hook run after `org-refile' has inserted its stuff at the new location.
  10354. Note that this is still *before* the stuff will be removed from
  10355. the *old* location.")
  10356. (defvar org-capture-last-stored-marker)
  10357. (defvar org-refile-keep nil
  10358. "Non-nil means `org-refile' will copy instead of refile.")
  10359. (defun org-copy ()
  10360. "Like `org-refile', but copy."
  10361. (interactive)
  10362. (let ((org-refile-keep t))
  10363. (funcall 'org-refile nil nil nil "Copy")))
  10364. (defun org-refile (&optional goto default-buffer rfloc msg)
  10365. "Move the entry or entries at point to another heading.
  10366. The list of target headings is compiled using the information in
  10367. `org-refile-targets', which see.
  10368. At the target location, the entry is filed as a subitem of the target
  10369. heading. Depending on `org-reverse-note-order', the new subitem will
  10370. either be the first or the last subitem.
  10371. If there is an active region, all entries in that region will be moved.
  10372. However, the region must fulfill the requirement that the first heading
  10373. is the first one sets the top-level of the moved text - at most siblings
  10374. below it are allowed.
  10375. With prefix arg GOTO, the command will only visit the target location
  10376. and not actually move anything.
  10377. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10378. go to the location where the last refiling operation has put the subtree.
  10379. With a prefix argument of `2', refile to the running clock.
  10380. RFLOC can be a refile location obtained in a different way.
  10381. MSG is a string to replace \"Refile\" in the default prompt with
  10382. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10383. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10384. If you are using target caching (see `org-refile-use-cache'),
  10385. you have to clear the target cache in order to find new targets.
  10386. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10387. prefix argument (`C-u C-u C-u C-c C-w')."
  10388. (interactive "P")
  10389. (if (member goto '(0 (64)))
  10390. (org-refile-cache-clear)
  10391. (let* ((actionmsg (or msg "Refile"))
  10392. (cbuf (current-buffer))
  10393. (regionp (org-region-active-p))
  10394. (region-start (and regionp (region-beginning)))
  10395. (region-end (and regionp (region-end)))
  10396. (region-length (and regionp (- region-end region-start)))
  10397. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10398. pos it nbuf file re level reversed)
  10399. (setq last-command nil)
  10400. (when regionp
  10401. (goto-char region-start)
  10402. (or (bolp) (goto-char (point-at-bol)))
  10403. (setq region-start (point))
  10404. (unless (or (org-kill-is-subtree-p
  10405. (buffer-substring region-start region-end))
  10406. (prog1 org-refile-active-region-within-subtree
  10407. (org-toggle-heading)))
  10408. (user-error "The region is not a (sequence of) subtree(s)")))
  10409. (if (equal goto '(16))
  10410. (org-refile-goto-last-stored)
  10411. (when (or
  10412. (and (equal goto 2)
  10413. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10414. (prog1
  10415. (setq it (list (or org-clock-heading "running clock")
  10416. (buffer-file-name
  10417. (marker-buffer org-clock-hd-marker))
  10418. ""
  10419. (marker-position org-clock-hd-marker)))
  10420. (setq goto nil)))
  10421. (setq it (or rfloc
  10422. (let (heading-text)
  10423. (save-excursion
  10424. (unless goto
  10425. (org-back-to-heading t)
  10426. (setq heading-text
  10427. (nth 4 (org-heading-components))))
  10428. (org-refile-get-location
  10429. (cond (goto "Goto")
  10430. (regionp (concat actionmsg " region to"))
  10431. (t (concat actionmsg " subtree \""
  10432. heading-text "\" to")))
  10433. default-buffer
  10434. (and (not (equal '(4) goto))
  10435. org-refile-allow-creating-parent-nodes)
  10436. goto))))))
  10437. (setq file (nth 1 it)
  10438. re (nth 2 it)
  10439. pos (nth 3 it))
  10440. (if (and (not goto)
  10441. pos
  10442. (equal (buffer-file-name) file)
  10443. (if regionp
  10444. (and (>= pos region-start)
  10445. (<= pos region-end))
  10446. (and (>= pos (point))
  10447. (< pos (save-excursion
  10448. (org-end-of-subtree t t))))))
  10449. (error "Cannot refile to position inside the tree or region"))
  10450. (setq nbuf (or (find-buffer-visiting file)
  10451. (find-file-noselect file)))
  10452. (if goto
  10453. (progn
  10454. (org-pop-to-buffer-same-window nbuf)
  10455. (goto-char pos)
  10456. (org-show-context 'org-goto))
  10457. (if regionp
  10458. (progn
  10459. (org-kill-new (buffer-substring region-start region-end))
  10460. (org-save-markers-in-region region-start region-end))
  10461. (org-copy-subtree 1 nil t))
  10462. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10463. (find-file-noselect file)))
  10464. (setq reversed (org-notes-order-reversed-p))
  10465. (save-excursion
  10466. (save-restriction
  10467. (widen)
  10468. (if pos
  10469. (progn
  10470. (goto-char pos)
  10471. (looking-at org-outline-regexp)
  10472. (setq level (org-get-valid-level (funcall outline-level) 1))
  10473. (goto-char
  10474. (if reversed
  10475. (or (outline-next-heading) (point-max))
  10476. (or (save-excursion (org-get-next-sibling))
  10477. (org-end-of-subtree t t)
  10478. (point-max)))))
  10479. (setq level 1)
  10480. (if (not reversed)
  10481. (goto-char (point-max))
  10482. (goto-char (point-min))
  10483. (or (outline-next-heading) (goto-char (point-max)))))
  10484. (if (not (bolp)) (newline))
  10485. (org-paste-subtree level)
  10486. (when org-log-refile
  10487. (org-add-log-setup 'refile nil nil 'findpos
  10488. org-log-refile)
  10489. (unless (eq org-log-refile 'note)
  10490. (save-excursion (org-add-log-note))))
  10491. (and org-auto-align-tags
  10492. (let ((org-loop-over-headlines-in-active-region nil))
  10493. (org-set-tags nil t)))
  10494. (with-demoted-errors
  10495. (bookmark-set "org-refile-last-stored"))
  10496. ;; If we are refiling for capture, make sure that the
  10497. ;; last-capture pointers point here
  10498. (when (org-bound-and-true-p org-refile-for-capture)
  10499. (with-demoted-errors
  10500. (bookmark-set "org-capture-last-stored-marker"))
  10501. (move-marker org-capture-last-stored-marker (point)))
  10502. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10503. (run-hooks 'org-after-refile-insert-hook))))
  10504. (unless org-refile-keep
  10505. (if regionp
  10506. (delete-region (point) (+ (point) region-length))
  10507. (org-cut-subtree)))
  10508. (when (featurep 'org-inlinetask)
  10509. (org-inlinetask-remove-END-maybe))
  10510. (setq org-markers-to-move nil)
  10511. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10512. (defun org-refile-goto-last-stored ()
  10513. "Go to the location where the last refile was stored."
  10514. (interactive)
  10515. (bookmark-jump "org-refile-last-stored")
  10516. (message "This is the location of the last refile"))
  10517. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10518. no-exclude)
  10519. "Prompt the user for a refile location, using PROMPT.
  10520. PROMPT should not be suffixed with a colon and a space, because
  10521. this function appends the default value from
  10522. `org-refile-history' automatically, if that is not empty.
  10523. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10524. this is used for the GOTO interface."
  10525. (let ((org-refile-targets org-refile-targets)
  10526. (org-refile-use-outline-path org-refile-use-outline-path)
  10527. excluded-entries)
  10528. (when (and (derived-mode-p 'org-mode)
  10529. (not org-refile-use-cache)
  10530. (not no-exclude))
  10531. (org-map-tree
  10532. (lambda()
  10533. (setq excluded-entries
  10534. (append excluded-entries (list (org-get-heading t t)))))))
  10535. (setq org-refile-target-table
  10536. (org-refile-get-targets default-buffer excluded-entries)))
  10537. (unless org-refile-target-table
  10538. (user-error "No refile targets"))
  10539. (let* ((cbuf (current-buffer))
  10540. (partial-completion-mode nil)
  10541. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10542. (cfunc (if (and org-refile-use-outline-path
  10543. org-outline-path-complete-in-steps)
  10544. 'org-olpath-completing-read
  10545. 'org-icompleting-read))
  10546. (extra (if org-refile-use-outline-path "/" ""))
  10547. (cbnex (concat (buffer-name) extra))
  10548. (filename (and cfn (expand-file-name cfn)))
  10549. (tbl (mapcar
  10550. (lambda (x)
  10551. (if (and (not (member org-refile-use-outline-path
  10552. '(file full-file-path)))
  10553. (not (equal filename (nth 1 x))))
  10554. (cons (concat (car x) extra " ("
  10555. (file-name-nondirectory (nth 1 x)) ")")
  10556. (cdr x))
  10557. (cons (concat (car x) extra) (cdr x))))
  10558. org-refile-target-table))
  10559. (completion-ignore-case t)
  10560. cdef
  10561. (prompt (concat prompt
  10562. (or (and (car org-refile-history)
  10563. (concat " (default " (car org-refile-history) ")"))
  10564. (and (assoc cbnex tbl) (setq cdef cbnex)
  10565. (concat " (default " cbnex ")"))) ": "))
  10566. pa answ parent-target child parent old-hist)
  10567. (setq old-hist org-refile-history)
  10568. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10569. nil 'org-refile-history (or cdef (car org-refile-history))))
  10570. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10571. (org-refile-check-position pa)
  10572. (if pa
  10573. (progn
  10574. (when (or (not org-refile-history)
  10575. (not (eq old-hist org-refile-history))
  10576. (not (equal (car pa) (car org-refile-history))))
  10577. (setq org-refile-history
  10578. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10579. org-refile-history
  10580. (cdr org-refile-history))))
  10581. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10582. (pop org-refile-history)))
  10583. pa)
  10584. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10585. (progn
  10586. (setq parent (match-string 1 answ)
  10587. child (match-string 2 answ))
  10588. (setq parent-target (or (assoc parent tbl)
  10589. (assoc (concat parent "/") tbl)))
  10590. (when (and parent-target
  10591. (or (eq new-nodes t)
  10592. (and (eq new-nodes 'confirm)
  10593. (y-or-n-p (format "Create new node \"%s\"? "
  10594. child)))))
  10595. (org-refile-new-child parent-target child)))
  10596. (user-error "Invalid target location")))))
  10597. (declare-function org-string-nw-p "org-macs" (s))
  10598. (defun org-refile-check-position (refile-pointer)
  10599. "Check if the refile pointer matches the headline to which it points."
  10600. (let* ((file (nth 1 refile-pointer))
  10601. (re (nth 2 refile-pointer))
  10602. (pos (nth 3 refile-pointer))
  10603. buffer)
  10604. (if (and (not (markerp pos)) (not file))
  10605. (user-error "Please save the buffer to a file before refiling")
  10606. (when (org-string-nw-p re)
  10607. (setq buffer (if (markerp pos)
  10608. (marker-buffer pos)
  10609. (or (find-buffer-visiting file)
  10610. (find-file-noselect file))))
  10611. (with-current-buffer buffer
  10612. (save-excursion
  10613. (save-restriction
  10614. (widen)
  10615. (goto-char pos)
  10616. (beginning-of-line 1)
  10617. (unless (org-looking-at-p re)
  10618. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10619. (defun org-refile-new-child (parent-target child)
  10620. "Use refile target PARENT-TARGET to add new CHILD below it."
  10621. (unless parent-target
  10622. (error "Cannot find parent for new node"))
  10623. (let ((file (nth 1 parent-target))
  10624. (pos (nth 3 parent-target))
  10625. level)
  10626. (with-current-buffer (or (find-buffer-visiting file)
  10627. (find-file-noselect file))
  10628. (save-excursion
  10629. (save-restriction
  10630. (widen)
  10631. (if pos
  10632. (goto-char pos)
  10633. (goto-char (point-max))
  10634. (if (not (bolp)) (newline)))
  10635. (when (looking-at org-outline-regexp)
  10636. (setq level (funcall outline-level))
  10637. (org-end-of-subtree t t))
  10638. (org-back-over-empty-lines)
  10639. (insert "\n" (make-string
  10640. (if pos (org-get-valid-level level 1) 1) ?*)
  10641. " " child "\n")
  10642. (beginning-of-line 0)
  10643. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10644. (defun org-olpath-completing-read (prompt collection &rest args)
  10645. "Read an outline path like a file name."
  10646. (let ((thetable collection)
  10647. (org-completion-use-ido nil) ; does not work with ido.
  10648. (org-completion-use-iswitchb nil)) ; or iswitchb
  10649. (apply
  10650. 'org-icompleting-read prompt
  10651. (lambda (string predicate &optional flag)
  10652. (let (rtn r f (l (length string)))
  10653. (cond
  10654. ((eq flag nil)
  10655. ;; try completion
  10656. (try-completion string thetable))
  10657. ((eq flag t)
  10658. ;; all-completions
  10659. (setq rtn (all-completions string thetable predicate))
  10660. (mapcar
  10661. (lambda (x)
  10662. (setq r (substring x l))
  10663. (if (string-match " ([^)]*)$" x)
  10664. (setq f (match-string 0 x))
  10665. (setq f ""))
  10666. (if (string-match "/" r)
  10667. (concat string (substring r 0 (match-end 0)) f)
  10668. x))
  10669. rtn))
  10670. ((eq flag 'lambda)
  10671. ;; exact match?
  10672. (assoc string thetable)))))
  10673. args)))
  10674. ;;;; Dynamic blocks
  10675. (defun org-find-dblock (name)
  10676. "Find the first dynamic block with name NAME in the buffer.
  10677. If not found, stay at current position and return nil."
  10678. (let ((case-fold-search t) pos)
  10679. (save-excursion
  10680. (goto-char (point-min))
  10681. (setq pos (and (re-search-forward
  10682. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10683. (match-beginning 0))))
  10684. (if pos (goto-char pos))
  10685. pos))
  10686. (defconst org-dblock-start-re
  10687. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10688. "Matches the start line of a dynamic block, with parameters.")
  10689. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10690. "Matches the end of a dynamic block.")
  10691. (defun org-create-dblock (plist)
  10692. "Create a dynamic block section, with parameters taken from PLIST.
  10693. PLIST must contain a :name entry which is used as name of the block."
  10694. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10695. (end-of-line 1)
  10696. (newline))
  10697. (let ((col (current-column))
  10698. (name (plist-get plist :name)))
  10699. (insert "#+BEGIN: " name)
  10700. (while plist
  10701. (if (eq (car plist) :name)
  10702. (setq plist (cddr plist))
  10703. (insert " " (prin1-to-string (pop plist)))))
  10704. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10705. (beginning-of-line -2)))
  10706. (defun org-prepare-dblock ()
  10707. "Prepare dynamic block for refresh.
  10708. This empties the block, puts the cursor at the insert position and returns
  10709. the property list including an extra property :name with the block name."
  10710. (unless (looking-at org-dblock-start-re)
  10711. (user-error "Not at a dynamic block"))
  10712. (let* ((begdel (1+ (match-end 0)))
  10713. (name (org-no-properties (match-string 1)))
  10714. (params (append (list :name name)
  10715. (read (concat "(" (match-string 3) ")")))))
  10716. (save-excursion
  10717. (beginning-of-line 1)
  10718. (skip-chars-forward " \t")
  10719. (setq params (plist-put params :indentation-column (current-column))))
  10720. (unless (re-search-forward org-dblock-end-re nil t)
  10721. (error "Dynamic block not terminated"))
  10722. (setq params
  10723. (append params
  10724. (list :content (buffer-substring
  10725. begdel (match-beginning 0)))))
  10726. (delete-region begdel (match-beginning 0))
  10727. (goto-char begdel)
  10728. (open-line 1)
  10729. params))
  10730. (defun org-map-dblocks (&optional command)
  10731. "Apply COMMAND to all dynamic blocks in the current buffer.
  10732. If COMMAND is not given, use `org-update-dblock'."
  10733. (let ((cmd (or command 'org-update-dblock)))
  10734. (save-excursion
  10735. (goto-char (point-min))
  10736. (while (re-search-forward org-dblock-start-re nil t)
  10737. (goto-char (match-beginning 0))
  10738. (save-excursion
  10739. (condition-case nil
  10740. (funcall cmd)
  10741. (error (message "Error during update of dynamic block"))))
  10742. (unless (re-search-forward org-dblock-end-re nil t)
  10743. (error "Dynamic block not terminated"))))))
  10744. (defun org-dblock-update (&optional arg)
  10745. "User command for updating dynamic blocks.
  10746. Update the dynamic block at point. With prefix ARG, update all dynamic
  10747. blocks in the buffer."
  10748. (interactive "P")
  10749. (if arg
  10750. (org-update-all-dblocks)
  10751. (or (looking-at org-dblock-start-re)
  10752. (org-beginning-of-dblock))
  10753. (org-update-dblock)))
  10754. (defun org-update-dblock ()
  10755. "Update the dynamic block at point.
  10756. This means to empty the block, parse for parameters and then call
  10757. the correct writing function."
  10758. (interactive)
  10759. (save-window-excursion
  10760. (let* ((pos (point))
  10761. (line (org-current-line))
  10762. (params (org-prepare-dblock))
  10763. (name (plist-get params :name))
  10764. (indent (plist-get params :indentation-column))
  10765. (cmd (intern (concat "org-dblock-write:" name))))
  10766. (message "Updating dynamic block `%s' at line %d..." name line)
  10767. (funcall cmd params)
  10768. (message "Updating dynamic block `%s' at line %d...done" name line)
  10769. (goto-char pos)
  10770. (when (and indent (> indent 0))
  10771. (setq indent (make-string indent ?\ ))
  10772. (save-excursion
  10773. (org-beginning-of-dblock)
  10774. (forward-line 1)
  10775. (while (not (looking-at org-dblock-end-re))
  10776. (insert indent)
  10777. (beginning-of-line 2))
  10778. (when (looking-at org-dblock-end-re)
  10779. (and (looking-at "[ \t]+")
  10780. (replace-match ""))
  10781. (insert indent)))))))
  10782. (defun org-beginning-of-dblock ()
  10783. "Find the beginning of the dynamic block at point.
  10784. Error if there is no such block at point."
  10785. (let ((pos (point))
  10786. beg)
  10787. (end-of-line 1)
  10788. (if (and (re-search-backward org-dblock-start-re nil t)
  10789. (setq beg (match-beginning 0))
  10790. (re-search-forward org-dblock-end-re nil t)
  10791. (> (match-end 0) pos))
  10792. (goto-char beg)
  10793. (goto-char pos)
  10794. (error "Not in a dynamic block"))))
  10795. (defun org-update-all-dblocks ()
  10796. "Update all dynamic blocks in the buffer.
  10797. This function can be used in a hook."
  10798. (interactive)
  10799. (when (derived-mode-p 'org-mode)
  10800. (org-map-dblocks 'org-update-dblock)))
  10801. ;;;; Completion
  10802. (defun org-get-export-keywords ()
  10803. "Return a list of all currently understood export keywords.
  10804. Export keywords include options, block names, attributes and
  10805. keywords relative to each registered export back-end."
  10806. (delq nil
  10807. (let (keywords)
  10808. (mapc
  10809. (lambda (back-end)
  10810. (let ((props (cdr back-end)))
  10811. ;; Back-end name (for keywords, like #+LATEX:)
  10812. (push (upcase (symbol-name (car back-end))) keywords)
  10813. ;; Back-end options.
  10814. (mapc (lambda (option) (push (cadr option) keywords))
  10815. (plist-get (cdr back-end) :options-alist))))
  10816. (org-bound-and-true-p org-export-registered-backends))
  10817. keywords)))
  10818. (defconst org-options-keywords
  10819. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10820. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10821. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10822. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10823. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10824. (defcustom org-structure-template-alist
  10825. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10826. "<src lang=\"?\">\n\n</src>")
  10827. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10828. "<example>\n?\n</example>")
  10829. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10830. "<quote>\n?\n</quote>")
  10831. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10832. "<verse>\n?\n</verse>")
  10833. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10834. "<verbatim>\n?\n</verbatim>")
  10835. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10836. "<center>\n?\n</center>")
  10837. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10838. "<literal style=\"latex\">\n?\n</literal>")
  10839. ("L" "#+LaTeX: "
  10840. "<literal style=\"latex\">?</literal>")
  10841. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10842. "<literal style=\"html\">\n?\n</literal>")
  10843. ("H" "#+HTML: "
  10844. "<literal style=\"html\">?</literal>")
  10845. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10846. ("A" "#+ASCII: ")
  10847. ("i" "#+INDEX: ?"
  10848. "#+INDEX: ?")
  10849. ("I" "#+INCLUDE: %file ?"
  10850. "<include file=%file markup=\"?\">"))
  10851. "Structure completion elements.
  10852. This is a list of abbreviation keys and values. The value gets inserted
  10853. if you type `<' followed by the key and then press the completion key,
  10854. usually `M-TAB'. %file will be replaced by a file name after prompting
  10855. for the file using completion. The cursor will be placed at the position
  10856. of the `?` in the template.
  10857. There are two templates for each key, the first uses the original Org syntax,
  10858. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10859. the default when the /org-mtags.el/ module has been loaded. See also the
  10860. variable `org-mtags-prefer-muse-templates'."
  10861. :group 'org-completion
  10862. :type '(repeat
  10863. (string :tag "Key")
  10864. (string :tag "Template")
  10865. (string :tag "Muse Template")))
  10866. (defun org-try-structure-completion ()
  10867. "Try to complete a structure template before point.
  10868. This looks for strings like \"<e\" on an otherwise empty line and
  10869. expands them."
  10870. (let ((l (buffer-substring (point-at-bol) (point)))
  10871. a)
  10872. (when (and (looking-at "[ \t]*$")
  10873. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10874. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10875. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10876. (match-beginning 1)) a)
  10877. t)))
  10878. (defun org-complete-expand-structure-template (start cell)
  10879. "Expand a structure template."
  10880. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10881. (rpl (nth (if musep 2 1) cell))
  10882. (ind ""))
  10883. (delete-region start (point))
  10884. (when (string-match "\\`#\\+" rpl)
  10885. (cond
  10886. ((bolp))
  10887. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10888. (setq ind (buffer-substring (point-at-bol) (point))))
  10889. (t (newline))))
  10890. (setq start (point))
  10891. (if (string-match "%file" rpl)
  10892. (setq rpl (replace-match
  10893. (concat
  10894. "\""
  10895. (save-match-data
  10896. (abbreviate-file-name (read-file-name "Include file: ")))
  10897. "\"")
  10898. t t rpl)))
  10899. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10900. (concat "\n" ind)))
  10901. (insert rpl)
  10902. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10903. ;;;; TODO, DEADLINE, Comments
  10904. (defun org-toggle-comment ()
  10905. "Change the COMMENT state of an entry."
  10906. (interactive)
  10907. (save-excursion
  10908. (org-back-to-heading)
  10909. (let (case-fold-search)
  10910. (cond
  10911. ((looking-at (format org-heading-keyword-regexp-format
  10912. org-comment-string))
  10913. (goto-char (match-end 1))
  10914. (looking-at (concat " +" org-comment-string))
  10915. (replace-match "" t t)
  10916. (when (eolp) (insert " ")))
  10917. ((looking-at org-outline-regexp)
  10918. (goto-char (match-end 0))
  10919. (insert org-comment-string " "))))))
  10920. (defvar org-last-todo-state-is-todo nil
  10921. "This is non-nil when the last TODO state change led to a TODO state.
  10922. If the last change removed the TODO tag or switched to DONE, then
  10923. this is nil.")
  10924. (defvar org-setting-tags nil) ; dynamically skipped
  10925. (defvar org-todo-setup-filter-hook nil
  10926. "Hook for functions that pre-filter todo specs.
  10927. Each function takes a todo spec and returns either nil or the spec
  10928. transformed into canonical form." )
  10929. (defvar org-todo-get-default-hook nil
  10930. "Hook for functions that get a default item for todo.
  10931. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10932. nil or a string to be used for the todo mark." )
  10933. (defvar org-agenda-headline-snapshot-before-repeat)
  10934. (defun org-current-effective-time ()
  10935. "Return current time adjusted for `org-extend-today-until' variable."
  10936. (let* ((ct (org-current-time))
  10937. (dct (decode-time ct))
  10938. (ct1
  10939. (cond
  10940. (org-use-last-clock-out-time-as-effective-time
  10941. (or (org-clock-get-last-clock-out-time) ct))
  10942. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10943. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10944. (t ct))))
  10945. ct1))
  10946. (defun org-todo-yesterday (&optional arg)
  10947. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10948. (interactive "P")
  10949. (if (eq major-mode 'org-agenda-mode)
  10950. (apply 'org-agenda-todo-yesterday arg)
  10951. (let* ((hour (third (decode-time
  10952. (org-current-time))))
  10953. (org-extend-today-until (1+ hour)))
  10954. (org-todo arg))))
  10955. (defvar org-block-entry-blocking ""
  10956. "First entry preventing the TODO state change.")
  10957. (defun org-todo (&optional arg)
  10958. "Change the TODO state of an item.
  10959. The state of an item is given by a keyword at the start of the heading,
  10960. like
  10961. *** TODO Write paper
  10962. *** DONE Call mom
  10963. The different keywords are specified in the variable `org-todo-keywords'.
  10964. By default the available states are \"TODO\" and \"DONE\".
  10965. So for this example: when the item starts with TODO, it is changed to DONE.
  10966. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10967. DONE are present, add TODO at the beginning of the heading.
  10968. With \\[universal-argument] prefix arg, use completion to determine the new \
  10969. state.
  10970. With numeric prefix arg, switch to that state.
  10971. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10972. keywords (nextset).
  10973. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10974. With a numeric prefix arg of 0, inhibit note taking for the change.
  10975. For calling through lisp, arg is also interpreted in the following way:
  10976. 'none -> empty state
  10977. \"\"(empty string) -> switch to empty state
  10978. 'done -> switch to DONE
  10979. 'nextset -> switch to the next set of keywords
  10980. 'previousset -> switch to the previous set of keywords
  10981. \"WAITING\" -> switch to the specified keyword, but only if it
  10982. really is a member of `org-todo-keywords'."
  10983. (interactive "P")
  10984. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10985. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10986. 'region-start-level 'region))
  10987. org-loop-over-headlines-in-active-region)
  10988. (org-map-entries
  10989. `(org-todo ,arg)
  10990. org-loop-over-headlines-in-active-region
  10991. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10992. (if (equal arg '(16)) (setq arg 'nextset))
  10993. (let ((org-blocker-hook org-blocker-hook)
  10994. commentp
  10995. case-fold-search)
  10996. (when (equal arg '(64))
  10997. (setq arg nil org-blocker-hook nil))
  10998. (when (and org-blocker-hook
  10999. (or org-inhibit-blocking
  11000. (org-entry-get nil "NOBLOCKING")))
  11001. (setq org-blocker-hook nil))
  11002. (save-excursion
  11003. (catch 'exit
  11004. (org-back-to-heading t)
  11005. (when (looking-at (concat "^\\*+ " org-comment-string))
  11006. (org-toggle-comment)
  11007. (setq commentp t))
  11008. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11009. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11010. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11011. (let* ((match-data (match-data))
  11012. (startpos (point-at-bol))
  11013. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11014. (org-log-done org-log-done)
  11015. (org-log-repeat org-log-repeat)
  11016. (org-todo-log-states org-todo-log-states)
  11017. (org-inhibit-logging
  11018. (if (equal arg 0)
  11019. (progn (setq arg nil) 'note) org-inhibit-logging))
  11020. (this (match-string 1))
  11021. (hl-pos (match-beginning 0))
  11022. (head (org-get-todo-sequence-head this))
  11023. (ass (assoc head org-todo-kwd-alist))
  11024. (interpret (nth 1 ass))
  11025. (done-word (nth 3 ass))
  11026. (final-done-word (nth 4 ass))
  11027. (org-last-state (or this ""))
  11028. (completion-ignore-case t)
  11029. (member (member this org-todo-keywords-1))
  11030. (tail (cdr member))
  11031. (org-state (cond
  11032. ((and org-todo-key-trigger
  11033. (or (and (equal arg '(4))
  11034. (eq org-use-fast-todo-selection 'prefix))
  11035. (and (not arg) org-use-fast-todo-selection
  11036. (not (eq org-use-fast-todo-selection
  11037. 'prefix)))))
  11038. ;; Use fast selection
  11039. (org-fast-todo-selection))
  11040. ((and (equal arg '(4))
  11041. (or (not org-use-fast-todo-selection)
  11042. (not org-todo-key-trigger)))
  11043. ;; Read a state with completion
  11044. (org-icompleting-read
  11045. "State: " (mapcar 'list org-todo-keywords-1)
  11046. nil t))
  11047. ((eq arg 'right)
  11048. (if this
  11049. (if tail (car tail) nil)
  11050. (car org-todo-keywords-1)))
  11051. ((eq arg 'left)
  11052. (if (equal member org-todo-keywords-1)
  11053. nil
  11054. (if this
  11055. (nth (- (length org-todo-keywords-1)
  11056. (length tail) 2)
  11057. org-todo-keywords-1)
  11058. (org-last org-todo-keywords-1))))
  11059. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11060. (setq arg nil))) ; hack to fall back to cycling
  11061. (arg
  11062. ;; user or caller requests a specific state
  11063. (cond
  11064. ((equal arg "") nil)
  11065. ((eq arg 'none) nil)
  11066. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11067. ((eq arg 'nextset)
  11068. (or (car (cdr (member head org-todo-heads)))
  11069. (car org-todo-heads)))
  11070. ((eq arg 'previousset)
  11071. (let ((org-todo-heads (reverse org-todo-heads)))
  11072. (or (car (cdr (member head org-todo-heads)))
  11073. (car org-todo-heads))))
  11074. ((car (member arg org-todo-keywords-1)))
  11075. ((stringp arg)
  11076. (user-error "State `%s' not valid in this file" arg))
  11077. ((nth (1- (prefix-numeric-value arg))
  11078. org-todo-keywords-1))))
  11079. ((null member) (or head (car org-todo-keywords-1)))
  11080. ((equal this final-done-word) nil) ;; -> make empty
  11081. ((null tail) nil) ;; -> first entry
  11082. ((memq interpret '(type priority))
  11083. (if (eq this-command last-command)
  11084. (car tail)
  11085. (if (> (length tail) 0)
  11086. (or done-word (car org-done-keywords))
  11087. nil)))
  11088. (t
  11089. (car tail))))
  11090. (org-state (or
  11091. (run-hook-with-args-until-success
  11092. 'org-todo-get-default-hook org-state org-last-state)
  11093. org-state))
  11094. (next (if org-state (concat " " org-state " ") " "))
  11095. (change-plist (list :type 'todo-state-change :from this :to org-state
  11096. :position startpos))
  11097. dolog now-done-p)
  11098. (when org-blocker-hook
  11099. (setq org-last-todo-state-is-todo
  11100. (not (member this org-done-keywords)))
  11101. (unless (save-excursion
  11102. (save-match-data
  11103. (org-with-wide-buffer
  11104. (run-hook-with-args-until-failure
  11105. 'org-blocker-hook change-plist))))
  11106. (if (org-called-interactively-p 'interactive)
  11107. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11108. this org-state org-block-entry-blocking)
  11109. ;; fail silently
  11110. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11111. this org-state org-block-entry-blocking)
  11112. (throw 'exit nil))))
  11113. (store-match-data match-data)
  11114. (replace-match next t t)
  11115. (unless (pos-visible-in-window-p hl-pos)
  11116. (message "TODO state changed to %s" (org-trim next)))
  11117. (unless head
  11118. (setq head (org-get-todo-sequence-head org-state)
  11119. ass (assoc head org-todo-kwd-alist)
  11120. interpret (nth 1 ass)
  11121. done-word (nth 3 ass)
  11122. final-done-word (nth 4 ass)))
  11123. (when (memq arg '(nextset previousset))
  11124. (message "Keyword-Set %d/%d: %s"
  11125. (- (length org-todo-sets) -1
  11126. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11127. (length org-todo-sets)
  11128. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11129. (setq org-last-todo-state-is-todo
  11130. (not (member org-state org-done-keywords)))
  11131. (setq now-done-p (and (member org-state org-done-keywords)
  11132. (not (member this org-done-keywords))))
  11133. (and logging (org-local-logging logging))
  11134. (when (and (or org-todo-log-states org-log-done)
  11135. (not (eq org-inhibit-logging t))
  11136. (not (memq arg '(nextset previousset))))
  11137. ;; we need to look at recording a time and note
  11138. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11139. (nth 2 (assoc this org-todo-log-states))))
  11140. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11141. (setq dolog 'time))
  11142. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11143. (and org-state
  11144. (member org-state org-not-done-keywords)
  11145. (not (member this org-not-done-keywords))))
  11146. ;; This is now a todo state and was not one before
  11147. ;; If there was a CLOSED time stamp, get rid of it.
  11148. (org-add-planning-info nil nil 'closed))
  11149. (when (and now-done-p org-log-done)
  11150. ;; It is now done, and it was not done before
  11151. (org-add-planning-info 'closed (org-current-effective-time))
  11152. (if (and (not dolog) (eq 'note org-log-done))
  11153. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11154. (when (and org-state dolog)
  11155. ;; This is a non-nil state, and we need to log it
  11156. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11157. ;; Fixup tag positioning
  11158. (org-todo-trigger-tag-changes org-state)
  11159. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11160. (when org-provide-todo-statistics
  11161. (org-update-parent-todo-statistics))
  11162. (run-hooks 'org-after-todo-state-change-hook)
  11163. (if (and arg (not (member org-state org-done-keywords)))
  11164. (setq head (org-get-todo-sequence-head org-state)))
  11165. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11166. ;; Do we need to trigger a repeat?
  11167. (when now-done-p
  11168. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11169. ;; This is for the agenda, take a snapshot of the headline.
  11170. (save-match-data
  11171. (setq org-agenda-headline-snapshot-before-repeat
  11172. (org-get-heading))))
  11173. (org-auto-repeat-maybe org-state))
  11174. ;; Fixup cursor location if close to the keyword
  11175. (if (and (outline-on-heading-p)
  11176. (not (bolp))
  11177. (save-excursion (beginning-of-line 1)
  11178. (looking-at org-todo-line-regexp))
  11179. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11180. (progn
  11181. (goto-char (or (match-end 2) (match-end 1)))
  11182. (and (looking-at " ") (just-one-space))))
  11183. (when org-trigger-hook
  11184. (save-excursion
  11185. (run-hook-with-args 'org-trigger-hook change-plist)))
  11186. (when commentp (org-toggle-comment))))))))
  11187. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11188. "Block turning an entry into a TODO, using the hierarchy.
  11189. This checks whether the current task should be blocked from state
  11190. changes. Such blocking occurs when:
  11191. 1. The task has children which are not all in a completed state.
  11192. 2. A task has a parent with the property :ORDERED:, and there
  11193. are siblings prior to the current task with incomplete
  11194. status.
  11195. 3. The parent of the task is blocked because it has siblings that should
  11196. be done first, or is child of a block grandparent TODO entry."
  11197. (if (not org-enforce-todo-dependencies)
  11198. t ; if locally turned off don't block
  11199. (catch 'dont-block
  11200. ;; If this is not a todo state change, or if this entry is already DONE,
  11201. ;; do not block
  11202. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11203. (member (plist-get change-plist :from)
  11204. (cons 'done org-done-keywords))
  11205. (member (plist-get change-plist :to)
  11206. (cons 'todo org-not-done-keywords))
  11207. (not (plist-get change-plist :to)))
  11208. (throw 'dont-block t))
  11209. ;; If this task has children, and any are undone, it's blocked
  11210. (save-excursion
  11211. (org-back-to-heading t)
  11212. (let ((this-level (funcall outline-level)))
  11213. (outline-next-heading)
  11214. (let ((child-level (funcall outline-level)))
  11215. (while (and (not (eobp))
  11216. (> child-level this-level))
  11217. ;; this todo has children, check whether they are all
  11218. ;; completed
  11219. (if (and (not (org-entry-is-done-p))
  11220. (org-entry-is-todo-p))
  11221. (progn (setq org-block-entry-blocking (org-get-heading))
  11222. (throw 'dont-block nil)))
  11223. (outline-next-heading)
  11224. (setq child-level (funcall outline-level))))))
  11225. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11226. ;; any previous siblings are undone, it's blocked
  11227. (save-excursion
  11228. (org-back-to-heading t)
  11229. (let* ((pos (point))
  11230. (parent-pos (and (org-up-heading-safe) (point))))
  11231. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11232. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11233. (forward-line 1)
  11234. (re-search-forward org-not-done-heading-regexp pos t))
  11235. (setq org-block-entry-blocking (match-string 0))
  11236. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11237. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11238. (while t
  11239. (goto-char parent-pos)
  11240. (if (not (looking-at org-not-done-heading-regexp))
  11241. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11242. (setq pos (point))
  11243. (setq parent-pos (and (org-up-heading-safe) (point)))
  11244. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11245. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11246. (forward-line 1)
  11247. (re-search-forward org-not-done-heading-regexp pos t)
  11248. (setq org-block-entry-blocking (org-get-heading)))
  11249. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11250. (defcustom org-track-ordered-property-with-tag nil
  11251. "Should the ORDERED property also be shown as a tag?
  11252. The ORDERED property decides if an entry should require subtasks to be
  11253. completed in sequence. Since a property is not very visible, setting
  11254. this option means that toggling the ORDERED property with the command
  11255. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11256. not relevant for the behavior, but it makes things more visible.
  11257. Note that toggling the tag with tags commands will not change the property
  11258. and therefore not influence behavior!
  11259. This can be t, meaning the tag ORDERED should be used, It can also be a
  11260. string to select a different tag for this task."
  11261. :group 'org-todo
  11262. :type '(choice
  11263. (const :tag "No tracking" nil)
  11264. (const :tag "Track with ORDERED tag" t)
  11265. (string :tag "Use other tag")))
  11266. (defun org-toggle-ordered-property ()
  11267. "Toggle the ORDERED property of the current entry.
  11268. For better visibility, you can track the value of this property with a tag.
  11269. See variable `org-track-ordered-property-with-tag'."
  11270. (interactive)
  11271. (let* ((t1 org-track-ordered-property-with-tag)
  11272. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11273. (save-excursion
  11274. (org-back-to-heading)
  11275. (if (org-entry-get nil "ORDERED")
  11276. (progn
  11277. (org-delete-property "ORDERED" "PROPERTIES")
  11278. (and tag (org-toggle-tag tag 'off))
  11279. (message "Subtasks can be completed in arbitrary order"))
  11280. (org-entry-put nil "ORDERED" "t")
  11281. (and tag (org-toggle-tag tag 'on))
  11282. (message "Subtasks must be completed in sequence")))))
  11283. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11284. (defun org-block-todo-from-checkboxes (change-plist)
  11285. "Block turning an entry into a TODO, using checkboxes.
  11286. This checks whether the current task should be blocked from state
  11287. changes because there are unchecked boxes in this entry."
  11288. (if (not org-enforce-todo-checkbox-dependencies)
  11289. t ; if locally turned off don't block
  11290. (catch 'dont-block
  11291. ;; If this is not a todo state change, or if this entry is already DONE,
  11292. ;; do not block
  11293. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11294. (member (plist-get change-plist :from)
  11295. (cons 'done org-done-keywords))
  11296. (member (plist-get change-plist :to)
  11297. (cons 'todo org-not-done-keywords))
  11298. (not (plist-get change-plist :to)))
  11299. (throw 'dont-block t))
  11300. ;; If this task has checkboxes that are not checked, it's blocked
  11301. (save-excursion
  11302. (org-back-to-heading t)
  11303. (let ((beg (point)) end)
  11304. (outline-next-heading)
  11305. (setq end (point))
  11306. (goto-char beg)
  11307. (if (org-list-search-forward
  11308. (concat (org-item-beginning-re)
  11309. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11310. "\\[[- ]\\]")
  11311. end t)
  11312. (progn
  11313. (if (boundp 'org-blocked-by-checkboxes)
  11314. (setq org-blocked-by-checkboxes t))
  11315. (throw 'dont-block nil)))))
  11316. t))) ; do not block
  11317. (defun org-entry-blocked-p ()
  11318. "Is the current entry blocked?"
  11319. (org-with-silent-modifications
  11320. (if (org-entry-get nil "NOBLOCKING")
  11321. nil ;; Never block this entry
  11322. (not (run-hook-with-args-until-failure
  11323. 'org-blocker-hook
  11324. (list :type 'todo-state-change
  11325. :position (point)
  11326. :from 'todo
  11327. :to 'done))))))
  11328. (defun org-update-statistics-cookies (all)
  11329. "Update the statistics cookie, either from TODO or from checkboxes.
  11330. This should be called with the cursor in a line with a statistics cookie."
  11331. (interactive "P")
  11332. (if all
  11333. (progn
  11334. (org-update-checkbox-count 'all)
  11335. (org-map-entries 'org-update-parent-todo-statistics))
  11336. (if (not (org-at-heading-p))
  11337. (org-update-checkbox-count)
  11338. (let ((pos (point-marker))
  11339. end l1 l2)
  11340. (ignore-errors (org-back-to-heading t))
  11341. (if (not (org-at-heading-p))
  11342. (org-update-checkbox-count)
  11343. (setq l1 (org-outline-level))
  11344. (setq end (save-excursion
  11345. (outline-next-heading)
  11346. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11347. (point)))
  11348. (if (and (save-excursion
  11349. (re-search-forward
  11350. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11351. (not (save-excursion (re-search-forward
  11352. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11353. (org-update-checkbox-count)
  11354. (if (and l2 (> l2 l1))
  11355. (progn
  11356. (goto-char end)
  11357. (org-update-parent-todo-statistics))
  11358. (goto-char pos)
  11359. (beginning-of-line 1)
  11360. (while (re-search-forward
  11361. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11362. (point-at-eol) t)
  11363. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11364. (goto-char pos)
  11365. (move-marker pos nil)))))
  11366. (defvar org-entry-property-inherited-from) ;; defined below
  11367. (defun org-update-parent-todo-statistics ()
  11368. "Update any statistics cookie in the parent of the current headline.
  11369. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11370. the entire subtree and this will travel up the hierarchy and update
  11371. statistics everywhere."
  11372. (let* ((prop (save-excursion (org-up-heading-safe)
  11373. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11374. (recursive (or (not org-hierarchical-todo-statistics)
  11375. (and prop (string-match "\\<recursive\\>" prop))))
  11376. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11377. 0))
  11378. (first t)
  11379. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11380. level ltoggle l1 new ndel
  11381. (cnt-all 0) (cnt-done 0) is-percent kwd
  11382. checkbox-beg ov ovs ove cookie-present)
  11383. (catch 'exit
  11384. (save-excursion
  11385. (beginning-of-line 1)
  11386. (setq ltoggle (funcall outline-level))
  11387. ;; Three situations are to consider:
  11388. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11389. ;; to the top-level ancestor on the headline;
  11390. ;; 2. If parent has "recursive" property, repeat up to the
  11391. ;; headline setting that property, taking inheritance into
  11392. ;; account;
  11393. ;; 3. Else, move up to direct parent and proceed only once.
  11394. (while (and (setq level (org-up-heading-safe))
  11395. (or recursive first)
  11396. (>= (point) lim))
  11397. (setq first nil cookie-present nil)
  11398. (unless (and level
  11399. (not (string-match
  11400. "\\<checkbox\\>"
  11401. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11402. "")))))
  11403. (throw 'exit nil))
  11404. (while (re-search-forward box-re (point-at-eol) t)
  11405. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11406. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11407. (save-match-data
  11408. (unless (outline-next-heading) (throw 'exit nil))
  11409. (while (and (looking-at org-complex-heading-regexp)
  11410. (> (setq l1 (length (match-string 1))) level))
  11411. (setq kwd (and (or recursive (= l1 ltoggle))
  11412. (match-string 2)))
  11413. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11414. (and (listp org-provide-todo-statistics)
  11415. (or (member kwd org-provide-todo-statistics)
  11416. (member kwd org-done-keywords))))
  11417. (setq cnt-all (1+ cnt-all))
  11418. (if (eq org-provide-todo-statistics t)
  11419. (and kwd (setq cnt-all (1+ cnt-all)))))
  11420. (and (member kwd org-done-keywords)
  11421. (setq cnt-done (1+ cnt-done)))
  11422. (outline-next-heading)))
  11423. (setq new
  11424. (if is-percent
  11425. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11426. (format "[%d/%d]" cnt-done cnt-all))
  11427. ndel (- (match-end 0) checkbox-beg))
  11428. ;; handle overlays when updating cookie from column view
  11429. (when (setq ov (car (overlays-at checkbox-beg)))
  11430. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11431. (delete-overlay ov))
  11432. (goto-char checkbox-beg)
  11433. (insert new)
  11434. (delete-region (point) (+ (point) ndel))
  11435. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11436. (when ov (move-overlay ov ovs ove)))
  11437. (when cookie-present
  11438. (run-hook-with-args 'org-after-todo-statistics-hook
  11439. cnt-done (- cnt-all cnt-done))))))
  11440. (run-hooks 'org-todo-statistics-hook)))
  11441. (defvar org-after-todo-statistics-hook nil
  11442. "Hook that is called after a TODO statistics cookie has been updated.
  11443. Each function is called with two arguments: the number of not-done entries
  11444. and the number of done entries.
  11445. For example, the following function, when added to this hook, will switch
  11446. an entry to DONE when all children are done, and back to TODO when new
  11447. entries are set to a TODO status. Note that this hook is only called
  11448. when there is a statistics cookie in the headline!
  11449. (defun org-summary-todo (n-done n-not-done)
  11450. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11451. (let (org-log-done org-log-states) ; turn off logging
  11452. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11453. ")
  11454. (defvar org-todo-statistics-hook nil
  11455. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11456. This hook runs even if there is no statistics cookie present, in which case
  11457. `org-after-todo-statistics-hook' would not run.")
  11458. (defun org-todo-trigger-tag-changes (state)
  11459. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11460. (let ((l org-todo-state-tags-triggers)
  11461. changes)
  11462. (when (or (not state) (equal state ""))
  11463. (setq changes (append changes (cdr (assoc "" l)))))
  11464. (when (and (stringp state) (> (length state) 0))
  11465. (setq changes (append changes (cdr (assoc state l)))))
  11466. (when (member state org-not-done-keywords)
  11467. (setq changes (append changes (cdr (assoc 'todo l)))))
  11468. (when (member state org-done-keywords)
  11469. (setq changes (append changes (cdr (assoc 'done l)))))
  11470. (dolist (c changes)
  11471. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11472. (defun org-local-logging (value)
  11473. "Get logging settings from a property VALUE."
  11474. (let* (words w a)
  11475. ;; directly set the variables, they are already local.
  11476. (setq org-log-done nil
  11477. org-log-repeat nil
  11478. org-todo-log-states nil)
  11479. (setq words (org-split-string value))
  11480. (while (setq w (pop words))
  11481. (cond
  11482. ((setq a (assoc w org-startup-options))
  11483. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11484. (set (nth 1 a) (nth 2 a))))
  11485. ((setq a (org-extract-log-state-settings w))
  11486. (and (member (car a) org-todo-keywords-1)
  11487. (push a org-todo-log-states)))))))
  11488. (defun org-get-todo-sequence-head (kwd)
  11489. "Return the head of the TODO sequence to which KWD belongs.
  11490. If KWD is not set, check if there is a text property remembering the
  11491. right sequence."
  11492. (let (p)
  11493. (cond
  11494. ((not kwd)
  11495. (or (get-text-property (point-at-bol) 'org-todo-head)
  11496. (progn
  11497. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11498. nil (point-at-eol)))
  11499. (get-text-property p 'org-todo-head))))
  11500. ((not (member kwd org-todo-keywords-1))
  11501. (car org-todo-keywords-1))
  11502. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11503. (defun org-fast-todo-selection ()
  11504. "Fast TODO keyword selection with single keys.
  11505. Returns the new TODO keyword, or nil if no state change should occur."
  11506. (let* ((fulltable org-todo-key-alist)
  11507. (done-keywords org-done-keywords) ;; needed for the faces.
  11508. (maxlen (apply 'max (mapcar
  11509. (lambda (x)
  11510. (if (stringp (car x)) (string-width (car x)) 0))
  11511. fulltable)))
  11512. (expert nil)
  11513. (fwidth (+ maxlen 3 1 3))
  11514. (ncol (/ (- (window-width) 4) fwidth))
  11515. tg cnt e c tbl
  11516. groups ingroup)
  11517. (save-excursion
  11518. (save-window-excursion
  11519. (if expert
  11520. (set-buffer (get-buffer-create " *Org todo*"))
  11521. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11522. (erase-buffer)
  11523. (org-set-local 'org-done-keywords done-keywords)
  11524. (setq tbl fulltable cnt 0)
  11525. (while (setq e (pop tbl))
  11526. (cond
  11527. ((equal e '(:startgroup))
  11528. (push '() groups) (setq ingroup t)
  11529. (when (not (= cnt 0))
  11530. (setq cnt 0)
  11531. (insert "\n"))
  11532. (insert "{ "))
  11533. ((equal e '(:endgroup))
  11534. (setq ingroup nil cnt 0)
  11535. (insert "}\n"))
  11536. ((equal e '(:newline))
  11537. (when (not (= cnt 0))
  11538. (setq cnt 0)
  11539. (insert "\n")
  11540. (setq e (car tbl))
  11541. (while (equal (car tbl) '(:newline))
  11542. (insert "\n")
  11543. (setq tbl (cdr tbl)))))
  11544. (t
  11545. (setq tg (car e) c (cdr e))
  11546. (if ingroup (push tg (car groups)))
  11547. (setq tg (org-add-props tg nil 'face
  11548. (org-get-todo-face tg)))
  11549. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11550. (insert "[" c "] " tg (make-string
  11551. (- fwidth 4 (length tg)) ?\ ))
  11552. (when (= (setq cnt (1+ cnt)) ncol)
  11553. (insert "\n")
  11554. (if ingroup (insert " "))
  11555. (setq cnt 0)))))
  11556. (insert "\n")
  11557. (goto-char (point-min))
  11558. (if (not expert) (org-fit-window-to-buffer))
  11559. (message "[a-z..]:Set [SPC]:clear")
  11560. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11561. (cond
  11562. ((or (= c ?\C-g)
  11563. (and (= c ?q) (not (rassoc c fulltable))))
  11564. (setq quit-flag t))
  11565. ((= c ?\ ) nil)
  11566. ((setq e (rassoc c fulltable) tg (car e))
  11567. tg)
  11568. (t (setq quit-flag t)))))))
  11569. (defun org-entry-is-todo-p ()
  11570. (member (org-get-todo-state) org-not-done-keywords))
  11571. (defun org-entry-is-done-p ()
  11572. (member (org-get-todo-state) org-done-keywords))
  11573. (defun org-get-todo-state ()
  11574. "Return the TODO keyword of the current subtree."
  11575. (save-excursion
  11576. (org-back-to-heading t)
  11577. (and (looking-at org-todo-line-regexp)
  11578. (match-end 2)
  11579. (match-string 2))))
  11580. (defun org-at-date-range-p (&optional inactive-ok)
  11581. "Is the cursor inside a date range?"
  11582. (interactive)
  11583. (save-excursion
  11584. (catch 'exit
  11585. (let ((pos (point)))
  11586. (skip-chars-backward "^[<\r\n")
  11587. (skip-chars-backward "<[")
  11588. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11589. (>= (match-end 0) pos)
  11590. (throw 'exit t))
  11591. (skip-chars-backward "^<[\r\n")
  11592. (skip-chars-backward "<[")
  11593. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11594. (>= (match-end 0) pos)
  11595. (throw 'exit t)))
  11596. nil)))
  11597. (defun org-get-repeat (&optional tagline)
  11598. "Check if there is a deadline/schedule with repeater in this entry."
  11599. (save-match-data
  11600. (save-excursion
  11601. (org-back-to-heading t)
  11602. (and (re-search-forward (if tagline
  11603. (concat tagline "\\s-*" org-repeat-re)
  11604. org-repeat-re)
  11605. (org-entry-end-position) t)
  11606. (match-string-no-properties 1)))))
  11607. (defvar org-last-changed-timestamp)
  11608. (defvar org-last-inserted-timestamp)
  11609. (defvar org-log-post-message)
  11610. (defvar org-log-note-purpose)
  11611. (defvar org-log-note-how)
  11612. (defvar org-log-note-extra)
  11613. (defun org-auto-repeat-maybe (done-word)
  11614. "Check if the current headline contains a repeated deadline/schedule.
  11615. If yes, set TODO state back to what it was and change the base date
  11616. of repeating deadline/scheduled time stamps to new date.
  11617. This function is run automatically after each state change to a DONE state."
  11618. ;; last-state is dynamically scoped into this function
  11619. (let* ((repeat (org-get-repeat))
  11620. (aa (assoc org-last-state org-todo-kwd-alist))
  11621. (interpret (nth 1 aa))
  11622. (head (nth 2 aa))
  11623. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11624. (msg "Entry repeats: ")
  11625. (org-log-done nil)
  11626. (org-todo-log-states nil)
  11627. re type n what ts time to-state)
  11628. (when repeat
  11629. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11630. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11631. org-todo-repeat-to-state))
  11632. (unless (and to-state (member to-state org-todo-keywords-1))
  11633. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11634. (org-todo to-state)
  11635. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11636. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11637. (org-time-stamp-format t t))))
  11638. (when org-log-repeat
  11639. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11640. (memq 'org-add-log-note post-command-hook))
  11641. ;; OK, we are already setup for some record
  11642. (if (eq org-log-repeat 'note)
  11643. ;; make sure we take a note, not only a time stamp
  11644. (setq org-log-note-how 'note))
  11645. ;; Set up for taking a record
  11646. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11647. org-last-state
  11648. 'findpos org-log-repeat)))
  11649. (org-back-to-heading t)
  11650. (org-add-planning-info nil nil 'closed)
  11651. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11652. org-deadline-time-regexp "\\)\\|\\("
  11653. org-ts-regexp "\\)"))
  11654. (while (re-search-forward
  11655. re (save-excursion (outline-next-heading) (point)) t)
  11656. (setq type (if (match-end 1) org-scheduled-string
  11657. (if (match-end 3) org-deadline-string "Plain:"))
  11658. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11659. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11660. (setq n (string-to-number (match-string 2 ts))
  11661. what (match-string 3 ts))
  11662. (if (equal what "w") (setq n (* n 7) what "d"))
  11663. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11664. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11665. ;; Preparation, see if we need to modify the start date for the change
  11666. (when (match-end 1)
  11667. (setq time (save-match-data (org-time-string-to-time ts)))
  11668. (cond
  11669. ((equal (match-string 1 ts) ".")
  11670. ;; Shift starting date to today
  11671. (org-timestamp-change
  11672. (- (org-today) (time-to-days time))
  11673. 'day))
  11674. ((equal (match-string 1 ts) "+")
  11675. (let ((nshiftmax 10) (nshift 0))
  11676. (while (or (= nshift 0)
  11677. (<= (time-to-days time)
  11678. (time-to-days (current-time))))
  11679. (when (= (incf nshift) nshiftmax)
  11680. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11681. (error "Abort")))
  11682. (org-timestamp-change n (cdr (assoc what whata)))
  11683. (org-at-timestamp-p t)
  11684. (setq ts (match-string 1))
  11685. (setq time (save-match-data (org-time-string-to-time ts)))))
  11686. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11687. ;; rematch, so that we have everything in place for the real shift
  11688. (org-at-timestamp-p t)
  11689. (setq ts (match-string 1))
  11690. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11691. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11692. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11693. (setq org-log-post-message msg)
  11694. (message "%s" msg))))
  11695. (defun org-show-todo-tree (arg)
  11696. "Make a compact tree which shows all headlines marked with TODO.
  11697. The tree will show the lines where the regexp matches, and all higher
  11698. headlines above the match.
  11699. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11700. With a numeric prefix N, construct a sparse tree for the Nth element
  11701. of `org-todo-keywords-1'."
  11702. (interactive "P")
  11703. (let ((case-fold-search nil)
  11704. (kwd-re
  11705. (cond ((null arg) org-not-done-regexp)
  11706. ((equal arg '(4))
  11707. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11708. (mapcar 'list org-todo-keywords-1))))
  11709. (concat "\\("
  11710. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11711. "\\)\\>")))
  11712. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11713. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11714. org-todo-keywords-1)))
  11715. (t (user-error "Invalid prefix argument: %s" arg)))))
  11716. (message "%d TODO entries found"
  11717. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11718. (defun org-deadline (arg &optional time)
  11719. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11720. With one universal prefix argument, remove any deadline from the item.
  11721. With two universal prefix arguments, prompt for a warning delay.
  11722. With argument TIME, set the deadline at the corresponding date. TIME
  11723. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11724. (interactive "P")
  11725. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11726. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11727. 'region-start-level 'region))
  11728. org-loop-over-headlines-in-active-region)
  11729. (org-map-entries
  11730. `(org-deadline ',arg ,time)
  11731. org-loop-over-headlines-in-active-region
  11732. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11733. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11734. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11735. (repeater (and old-date
  11736. (string-match
  11737. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11738. old-date)
  11739. (match-string 1 old-date))))
  11740. (cond
  11741. ((equal arg '(4))
  11742. (when (and old-date org-log-redeadline)
  11743. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11744. org-log-redeadline))
  11745. (org-remove-timestamp-with-keyword org-deadline-string)
  11746. (message "Item no longer has a deadline."))
  11747. ((equal arg '(16))
  11748. (save-excursion
  11749. (if (re-search-forward
  11750. org-deadline-time-regexp
  11751. (save-excursion (outline-next-heading) (point)) t)
  11752. (let* ((rpl0 (match-string 1))
  11753. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11754. (replace-match
  11755. (concat org-deadline-string
  11756. " <" rpl
  11757. (format " -%dd"
  11758. (abs
  11759. (- (time-to-days
  11760. (save-match-data
  11761. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11762. (time-to-days old-date-time))))
  11763. ">") t t))
  11764. (user-error "No deadline information to update"))))
  11765. (t
  11766. (org-add-planning-info 'deadline time 'closed)
  11767. (when (and old-date org-log-redeadline
  11768. (not (equal old-date
  11769. (substring org-last-inserted-timestamp 1 -1))))
  11770. (org-add-log-setup 'redeadline nil old-date 'findpos
  11771. org-log-redeadline))
  11772. (when repeater
  11773. (save-excursion
  11774. (org-back-to-heading t)
  11775. (when (re-search-forward (concat org-deadline-string " "
  11776. org-last-inserted-timestamp)
  11777. (save-excursion
  11778. (outline-next-heading) (point)) t)
  11779. (goto-char (1- (match-end 0)))
  11780. (insert " " repeater)
  11781. (setq org-last-inserted-timestamp
  11782. (concat (substring org-last-inserted-timestamp 0 -1)
  11783. " " repeater
  11784. (substring org-last-inserted-timestamp -1))))))
  11785. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11786. (defun org-schedule (arg &optional time)
  11787. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11788. With one universal prefix argument, remove any scheduling date from the item.
  11789. With two universal prefix arguments, prompt for a delay cookie.
  11790. With argument TIME, scheduled at the corresponding date. TIME can
  11791. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11792. (interactive "P")
  11793. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11794. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11795. 'region-start-level 'region))
  11796. org-loop-over-headlines-in-active-region)
  11797. (org-map-entries
  11798. `(org-schedule ',arg ,time)
  11799. org-loop-over-headlines-in-active-region
  11800. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11801. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11802. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11803. (repeater (and old-date
  11804. (string-match
  11805. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11806. old-date)
  11807. (match-string 1 old-date))))
  11808. (cond
  11809. ((equal arg '(4))
  11810. (progn
  11811. (when (and old-date org-log-reschedule)
  11812. (org-add-log-setup 'delschedule nil old-date 'findpos
  11813. org-log-reschedule))
  11814. (org-remove-timestamp-with-keyword org-scheduled-string)
  11815. (message "Item is no longer scheduled.")))
  11816. ((equal arg '(16))
  11817. (save-excursion
  11818. (if (re-search-forward
  11819. org-scheduled-time-regexp
  11820. (save-excursion (outline-next-heading) (point)) t)
  11821. (let* ((rpl0 (match-string 1))
  11822. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11823. (replace-match
  11824. (concat org-scheduled-string
  11825. " <" rpl
  11826. (format " -%dd"
  11827. (abs
  11828. (- (time-to-days
  11829. (save-match-data
  11830. (org-read-date nil t nil "Delay until" old-date-time)))
  11831. (time-to-days old-date-time))))
  11832. ">") t t))
  11833. (user-error "No scheduled information to update"))))
  11834. (t
  11835. (org-add-planning-info 'scheduled time 'closed)
  11836. (when (and old-date org-log-reschedule
  11837. (not (equal old-date
  11838. (substring org-last-inserted-timestamp 1 -1))))
  11839. (org-add-log-setup 'reschedule nil old-date 'findpos
  11840. org-log-reschedule))
  11841. (when repeater
  11842. (save-excursion
  11843. (org-back-to-heading t)
  11844. (when (re-search-forward (concat org-scheduled-string " "
  11845. org-last-inserted-timestamp)
  11846. (save-excursion
  11847. (outline-next-heading) (point)) t)
  11848. (goto-char (1- (match-end 0)))
  11849. (insert " " repeater)
  11850. (setq org-last-inserted-timestamp
  11851. (concat (substring org-last-inserted-timestamp 0 -1)
  11852. " " repeater
  11853. (substring org-last-inserted-timestamp -1))))))
  11854. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11855. (defun org-get-scheduled-time (pom &optional inherit)
  11856. "Get the scheduled time as a time tuple, of a format suitable
  11857. for calling org-schedule with, or if there is no scheduling,
  11858. returns nil."
  11859. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11860. (when time
  11861. (apply 'encode-time (org-parse-time-string time)))))
  11862. (defun org-get-deadline-time (pom &optional inherit)
  11863. "Get the deadline as a time tuple, of a format suitable for
  11864. calling org-deadline with, or if there is no scheduling, returns
  11865. nil."
  11866. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11867. (when time
  11868. (apply 'encode-time (org-parse-time-string time)))))
  11869. (defun org-remove-timestamp-with-keyword (keyword)
  11870. "Remove all time stamps with KEYWORD in the current entry."
  11871. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11872. beg)
  11873. (save-excursion
  11874. (org-back-to-heading t)
  11875. (setq beg (point))
  11876. (outline-next-heading)
  11877. (while (re-search-backward re beg t)
  11878. (replace-match "")
  11879. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11880. (equal (char-before) ?\ ))
  11881. (backward-delete-char 1)
  11882. (if (string-match "^[ \t]*$" (buffer-substring
  11883. (point-at-bol) (point-at-eol)))
  11884. (delete-region (point-at-bol)
  11885. (min (point-max) (1+ (point-at-eol))))))))))
  11886. (defun org-add-planning-info (what &optional time &rest remove)
  11887. "Insert new timestamp with keyword in the line directly after the headline.
  11888. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11889. If non is given, the user is prompted for a date.
  11890. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11891. be removed."
  11892. (interactive)
  11893. (let (org-time-was-given org-end-time-was-given ts
  11894. end default-time default-input)
  11895. (catch 'exit
  11896. (when (and (memq what '(scheduled deadline))
  11897. (or (not time)
  11898. (and (stringp time)
  11899. (string-match "^[-+]+[0-9]" time))))
  11900. ;; Try to get a default date/time from existing timestamp
  11901. (save-excursion
  11902. (org-back-to-heading t)
  11903. (setq end (save-excursion (outline-next-heading) (point)))
  11904. (when (re-search-forward (if (eq what 'scheduled)
  11905. org-scheduled-time-regexp
  11906. org-deadline-time-regexp)
  11907. end t)
  11908. (setq ts (match-string 1)
  11909. default-time
  11910. (apply 'encode-time (org-parse-time-string ts))
  11911. default-input (and ts (org-get-compact-tod ts))))))
  11912. (when what
  11913. (setq time
  11914. (if (stringp time)
  11915. ;; This is a string (relative or absolute), set proper date
  11916. (apply 'encode-time
  11917. (org-read-date-analyze
  11918. time default-time (decode-time default-time)))
  11919. ;; If necessary, get the time from the user
  11920. (or time (org-read-date nil 'to-time nil nil
  11921. default-time default-input)))))
  11922. (when (and org-insert-labeled-timestamps-at-point
  11923. (member what '(scheduled deadline)))
  11924. (insert
  11925. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11926. (org-insert-time-stamp time org-time-was-given
  11927. nil nil nil (list org-end-time-was-given))
  11928. (setq what nil))
  11929. (save-excursion
  11930. (save-restriction
  11931. (let (col list elt ts buffer-invisibility-spec)
  11932. (org-back-to-heading t)
  11933. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11934. (goto-char (match-end 1))
  11935. (setq col (current-column))
  11936. (goto-char (match-end 0))
  11937. (if (eobp) (insert "\n") (forward-char 1))
  11938. (when (and (not what)
  11939. (not (looking-at
  11940. (concat "[ \t]*"
  11941. org-keyword-time-not-clock-regexp))))
  11942. ;; Nothing to add, nothing to remove...... :-)
  11943. (throw 'exit nil))
  11944. (if (and (not (looking-at org-outline-regexp))
  11945. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11946. "[^\r\n]*"))
  11947. (not (equal (match-string 1) org-clock-string)))
  11948. (narrow-to-region (match-beginning 0) (match-end 0))
  11949. (insert-before-markers "\n")
  11950. (backward-char 1)
  11951. (narrow-to-region (point) (point))
  11952. (and org-adapt-indentation (org-indent-to-column col)))
  11953. ;; Check if we have to remove something.
  11954. (setq list (cons what remove))
  11955. (while list
  11956. (setq elt (pop list))
  11957. (when (or (and (eq elt 'scheduled)
  11958. (re-search-forward org-scheduled-time-regexp nil t))
  11959. (and (eq elt 'deadline)
  11960. (re-search-forward org-deadline-time-regexp nil t))
  11961. (and (eq elt 'closed)
  11962. (re-search-forward org-closed-time-regexp nil t)))
  11963. (replace-match "")
  11964. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11965. (and (looking-at "[ \t]+") (replace-match ""))
  11966. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11967. (when what
  11968. (insert
  11969. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11970. (cond ((eq what 'scheduled) org-scheduled-string)
  11971. ((eq what 'deadline) org-deadline-string)
  11972. ((eq what 'closed) org-closed-string))
  11973. " ")
  11974. (setq ts (org-insert-time-stamp
  11975. time
  11976. (or org-time-was-given
  11977. (and (eq what 'closed) org-log-done-with-time))
  11978. (eq what 'closed)
  11979. nil nil (list org-end-time-was-given)))
  11980. (insert
  11981. (if (not (or (bolp) (eq (char-before) ?\ )
  11982. (memq (char-after) '(32 10))
  11983. (eobp))) " " ""))
  11984. (end-of-line 1))
  11985. (goto-char (point-min))
  11986. (widen)
  11987. (if (and (looking-at "[ \t]*\n")
  11988. (equal (char-before) ?\n))
  11989. (delete-region (1- (point)) (point-at-eol)))
  11990. ts))))))
  11991. (defvar org-log-note-marker (make-marker))
  11992. (defvar org-log-note-purpose nil)
  11993. (defvar org-log-note-state nil)
  11994. (defvar org-log-note-previous-state nil)
  11995. (defvar org-log-note-how nil)
  11996. (defvar org-log-note-extra nil)
  11997. (defvar org-log-note-window-configuration nil)
  11998. (defvar org-log-note-return-to (make-marker))
  11999. (defvar org-log-note-effective-time nil
  12000. "Remembered current time so that dynamically scoped
  12001. `org-extend-today-until' affects tha timestamps in state change
  12002. log")
  12003. (defvar org-log-post-message nil
  12004. "Message to be displayed after a log note has been stored.
  12005. The auto-repeater uses this.")
  12006. (defun org-add-note ()
  12007. "Add a note to the current entry.
  12008. This is done in the same way as adding a state change note."
  12009. (interactive)
  12010. (org-add-log-setup 'note nil nil 'findpos nil))
  12011. (defvar org-property-end-re)
  12012. (defun org-add-log-setup (&optional purpose state prev-state
  12013. findpos how extra)
  12014. "Set up the post command hook to take a note.
  12015. If this is about to TODO state change, the new state is expected in STATE.
  12016. When FINDPOS is non-nil, find the correct position for the note in
  12017. the current entry. If not, assume that it can be inserted at point.
  12018. HOW is an indicator what kind of note should be created.
  12019. EXTRA is additional text that will be inserted into the notes buffer."
  12020. (let* ((org-log-into-drawer (org-log-into-drawer))
  12021. (drawer (cond ((stringp org-log-into-drawer)
  12022. org-log-into-drawer)
  12023. (org-log-into-drawer "LOGBOOK"))))
  12024. (save-restriction
  12025. (save-excursion
  12026. (when findpos
  12027. (org-back-to-heading t)
  12028. (narrow-to-region (point) (save-excursion
  12029. (outline-next-heading) (point)))
  12030. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12031. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12032. "[^\r\n]*\\)?"))
  12033. (goto-char (match-end 0))
  12034. (cond
  12035. (drawer
  12036. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12037. nil t)
  12038. (progn
  12039. (goto-char (match-end 0))
  12040. (or org-log-states-order-reversed
  12041. (and (re-search-forward org-property-end-re nil t)
  12042. (goto-char (1- (match-beginning 0))))))
  12043. (insert "\n:" drawer ":\n:END:")
  12044. (beginning-of-line 0)
  12045. (org-indent-line)
  12046. (beginning-of-line 2)
  12047. (org-indent-line)
  12048. (end-of-line 0)))
  12049. ((and org-log-state-notes-insert-after-drawers
  12050. (save-excursion
  12051. (forward-line) (looking-at org-drawer-regexp)))
  12052. (forward-line)
  12053. (while (looking-at org-drawer-regexp)
  12054. (goto-char (match-end 0))
  12055. (re-search-forward org-property-end-re (point-max) t)
  12056. (forward-line))
  12057. (forward-line -1)))
  12058. (unless org-log-states-order-reversed
  12059. (and (= (char-after) ?\n) (forward-char 1))
  12060. (org-skip-over-state-notes)
  12061. (skip-chars-backward " \t\n\r")))
  12062. (move-marker org-log-note-marker (point))
  12063. (setq org-log-note-purpose purpose
  12064. org-log-note-state state
  12065. org-log-note-previous-state prev-state
  12066. org-log-note-how how
  12067. org-log-note-extra extra
  12068. org-log-note-effective-time (org-current-effective-time))
  12069. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12070. (defun org-skip-over-state-notes ()
  12071. "Skip past the list of State notes in an entry."
  12072. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12073. (when (ignore-errors (goto-char (org-in-item-p)))
  12074. (let* ((struct (org-list-struct))
  12075. (prevs (org-list-prevs-alist struct)))
  12076. (while (looking-at "[ \t]*- State")
  12077. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12078. (org-list-get-item-end (point) struct)))))))
  12079. (defun org-add-log-note (&optional purpose)
  12080. "Pop up a window for taking a note, and add this note later at point."
  12081. (remove-hook 'post-command-hook 'org-add-log-note)
  12082. (setq org-log-note-window-configuration (current-window-configuration))
  12083. (delete-other-windows)
  12084. (move-marker org-log-note-return-to (point))
  12085. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12086. (goto-char org-log-note-marker)
  12087. (org-switch-to-buffer-other-window "*Org Note*")
  12088. (erase-buffer)
  12089. (if (memq org-log-note-how '(time state))
  12090. (let (current-prefix-arg) (org-store-log-note))
  12091. (let ((org-inhibit-startup t)) (org-mode))
  12092. (insert (format "# Insert note for %s.
  12093. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12094. (cond
  12095. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12096. ((eq org-log-note-purpose 'done) "closed todo item")
  12097. ((eq org-log-note-purpose 'state)
  12098. (format "state change from \"%s\" to \"%s\""
  12099. (or org-log-note-previous-state "")
  12100. (or org-log-note-state "")))
  12101. ((eq org-log-note-purpose 'reschedule)
  12102. "rescheduling")
  12103. ((eq org-log-note-purpose 'delschedule)
  12104. "no longer scheduled")
  12105. ((eq org-log-note-purpose 'redeadline)
  12106. "changing deadline")
  12107. ((eq org-log-note-purpose 'deldeadline)
  12108. "removing deadline")
  12109. ((eq org-log-note-purpose 'refile)
  12110. "refiling")
  12111. ((eq org-log-note-purpose 'note)
  12112. "this entry")
  12113. (t (error "This should not happen")))))
  12114. (if org-log-note-extra (insert org-log-note-extra))
  12115. (org-set-local 'org-finish-function 'org-store-log-note)
  12116. (run-hooks 'org-log-buffer-setup-hook)))
  12117. (defvar org-note-abort nil) ; dynamically scoped
  12118. (defun org-store-log-note ()
  12119. "Finish taking a log note, and insert it to where it belongs."
  12120. (let ((txt (buffer-string)))
  12121. (kill-buffer (current-buffer))
  12122. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12123. lines ind bul)
  12124. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12125. (setq txt (replace-match "" t t txt)))
  12126. (if (string-match "\\s-+\\'" txt)
  12127. (setq txt (replace-match "" t t txt)))
  12128. (setq lines (org-split-string txt "\n"))
  12129. (when (and note (string-match "\\S-" note))
  12130. (setq note
  12131. (org-replace-escapes
  12132. note
  12133. (list (cons "%u" (user-login-name))
  12134. (cons "%U" user-full-name)
  12135. (cons "%t" (format-time-string
  12136. (org-time-stamp-format 'long 'inactive)
  12137. org-log-note-effective-time))
  12138. (cons "%T" (format-time-string
  12139. (org-time-stamp-format 'long nil)
  12140. org-log-note-effective-time))
  12141. (cons "%d" (format-time-string
  12142. (org-time-stamp-format nil 'inactive)
  12143. org-log-note-effective-time))
  12144. (cons "%D" (format-time-string
  12145. (org-time-stamp-format nil nil)
  12146. org-log-note-effective-time))
  12147. (cons "%s" (if org-log-note-state
  12148. (concat "\"" org-log-note-state "\"")
  12149. ""))
  12150. (cons "%S" (if org-log-note-previous-state
  12151. (concat "\"" org-log-note-previous-state "\"")
  12152. "\"\"")))))
  12153. (if lines (setq note (concat note " \\\\")))
  12154. (push note lines))
  12155. (when (or current-prefix-arg org-note-abort)
  12156. (when org-log-into-drawer
  12157. (org-remove-empty-drawer-at
  12158. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12159. org-log-note-marker))
  12160. (setq lines nil))
  12161. (when lines
  12162. (with-current-buffer (marker-buffer org-log-note-marker)
  12163. (save-excursion
  12164. (goto-char org-log-note-marker)
  12165. (move-marker org-log-note-marker nil)
  12166. (end-of-line 1)
  12167. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12168. (setq ind (save-excursion
  12169. (if (ignore-errors (goto-char (org-in-item-p)))
  12170. (let ((struct (org-list-struct)))
  12171. (org-list-get-ind
  12172. (org-list-get-top-point struct) struct))
  12173. (skip-chars-backward " \r\t\n")
  12174. (cond
  12175. ((and (org-at-heading-p)
  12176. org-adapt-indentation)
  12177. (1+ (org-current-level)))
  12178. ((org-at-heading-p) 0)
  12179. (t (org-get-indentation))))))
  12180. (setq bul (org-list-bullet-string "-"))
  12181. (org-indent-line-to ind)
  12182. (insert bul (pop lines))
  12183. (let ((ind-body (+ (length bul) ind)))
  12184. (while lines
  12185. (insert "\n")
  12186. (org-indent-line-to ind-body)
  12187. (insert (pop lines))))
  12188. (message "Note stored")
  12189. (org-back-to-heading t)
  12190. (org-cycle-hide-drawers 'children))
  12191. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12192. ;; from within `org-add-log-note' because `buffer-undo-list'
  12193. ;; is then modified outside of `org-with-remote-undo'.
  12194. (when (eq this-command 'org-agenda-todo)
  12195. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12196. ;; Don't add undo information when called from `org-agenda-todo'
  12197. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12198. (set-window-configuration org-log-note-window-configuration)
  12199. (with-current-buffer (marker-buffer org-log-note-return-to)
  12200. (goto-char org-log-note-return-to))
  12201. (move-marker org-log-note-return-to nil)
  12202. (and org-log-post-message (message "%s" org-log-post-message))))
  12203. (defun org-remove-empty-drawer-at (drawer pos)
  12204. "Remove an empty drawer DRAWER at position POS.
  12205. POS may also be a marker."
  12206. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12207. (save-excursion
  12208. (save-restriction
  12209. (widen)
  12210. (goto-char pos)
  12211. (if (org-in-regexp
  12212. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12213. (replace-match ""))))))
  12214. (defvar org-ts-type nil)
  12215. (defun org-sparse-tree (&optional arg type)
  12216. "Create a sparse tree, prompt for the details.
  12217. This command can create sparse trees. You first need to select the type
  12218. of match used to create the tree:
  12219. t Show all TODO entries.
  12220. T Show entries with a specific TODO keyword.
  12221. m Show entries selected by a tags/property match.
  12222. p Enter a property name and its value (both with completion on existing
  12223. names/values) and show entries with that property.
  12224. r Show entries matching a regular expression (`/' can be used as well).
  12225. b Show deadlines and scheduled items before a date.
  12226. a Show deadlines and scheduled items after a date.
  12227. d Show deadlines due within `org-deadline-warning-days'.
  12228. D Show deadlines and scheduled items between a date range."
  12229. (interactive "P")
  12230. (let (ans kwd value ts-type)
  12231. (setq type (or type org-sparse-tree-default-date-type))
  12232. (setq org-ts-type type)
  12233. (message "Sparse tree: [/]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"
  12234. (cond ((eq type 'all) "all timestamps")
  12235. ((eq type 'scheduled) "only scheduled")
  12236. ((eq type 'deadline) "only deadline")
  12237. ((eq type 'active) "only active timestamps")
  12238. ((eq type 'inactive) "only inactive timestamps")
  12239. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12240. ((eq type 'closed) "with a closed time-stamp")
  12241. (t "scheduled/deadline")))
  12242. (setq ans (read-char-exclusive))
  12243. (cond
  12244. ((equal ans ?c)
  12245. (org-sparse-tree
  12246. arg (cadr (member type '(scheduled-or-deadline
  12247. all scheduled deadline active inactive closed)))))
  12248. ((equal ans ?d)
  12249. (call-interactively 'org-check-deadlines))
  12250. ((equal ans ?b)
  12251. (call-interactively 'org-check-before-date))
  12252. ((equal ans ?a)
  12253. (call-interactively 'org-check-after-date))
  12254. ((equal ans ?D)
  12255. (call-interactively 'org-check-dates-range))
  12256. ((equal ans ?t)
  12257. (call-interactively 'org-show-todo-tree))
  12258. ((equal ans ?T)
  12259. (org-show-todo-tree '(4)))
  12260. ((member ans '(?T ?m))
  12261. (call-interactively 'org-match-sparse-tree))
  12262. ((member ans '(?p ?P))
  12263. (setq kwd (org-icompleting-read "Property: "
  12264. (mapcar 'list (org-buffer-property-keys))))
  12265. (setq value (org-icompleting-read "Value: "
  12266. (mapcar 'list (org-property-values kwd))))
  12267. (unless (string-match "\\`{.*}\\'" value)
  12268. (setq value (concat "\"" value "\"")))
  12269. (org-match-sparse-tree arg (concat kwd "=" value)))
  12270. ((member ans '(?r ?R ?/))
  12271. (call-interactively 'org-occur))
  12272. (t (user-error "No such sparse tree command \"%c\"" ans)))))
  12273. (defvar org-occur-highlights nil
  12274. "List of overlays used for occur matches.")
  12275. (make-variable-buffer-local 'org-occur-highlights)
  12276. (defvar org-occur-parameters nil
  12277. "Parameters of the active org-occur calls.
  12278. This is a list, each call to org-occur pushes as cons cell,
  12279. containing the regular expression and the callback, onto the list.
  12280. The list can contain several entries if `org-occur' has been called
  12281. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12282. will only contain one set of parameters. When the highlights are
  12283. removed (for example with `C-c C-c', or with the next edit (depending
  12284. on `org-remove-highlights-with-change'), this variable is emptied
  12285. as well.")
  12286. (make-variable-buffer-local 'org-occur-parameters)
  12287. (defun org-occur (regexp &optional keep-previous callback)
  12288. "Make a compact tree which shows all matches of REGEXP.
  12289. The tree will show the lines where the regexp matches, and all higher
  12290. headlines above the match. It will also show the heading after the match,
  12291. to make sure editing the matching entry is easy.
  12292. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12293. call to `org-occur' will be kept, to allow stacking of calls to this
  12294. command.
  12295. If CALLBACK is non-nil, it is a function which is called to confirm
  12296. that the match should indeed be shown."
  12297. (interactive "sRegexp: \nP")
  12298. (when (equal regexp "")
  12299. (user-error "Regexp cannot be empty"))
  12300. (unless keep-previous
  12301. (org-remove-occur-highlights nil nil t))
  12302. (push (cons regexp callback) org-occur-parameters)
  12303. (let ((cnt 0))
  12304. (save-excursion
  12305. (goto-char (point-min))
  12306. (if (or (not keep-previous) ; do not want to keep
  12307. (not org-occur-highlights)) ; no previous matches
  12308. ;; hide everything
  12309. (org-overview))
  12310. (while (re-search-forward regexp nil t)
  12311. (when (or (not callback)
  12312. (save-match-data (funcall callback)))
  12313. (setq cnt (1+ cnt))
  12314. (when org-highlight-sparse-tree-matches
  12315. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12316. (org-show-context 'occur-tree))))
  12317. (when org-remove-highlights-with-change
  12318. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12319. nil 'local))
  12320. (unless org-sparse-tree-open-archived-trees
  12321. (org-hide-archived-subtrees (point-min) (point-max)))
  12322. (run-hooks 'org-occur-hook)
  12323. (if (org-called-interactively-p 'interactive)
  12324. (message "%d match(es) for regexp %s" cnt regexp))
  12325. cnt))
  12326. (defun org-occur-next-match (&optional n reset)
  12327. "Function for `next-error-function' to find sparse tree matches.
  12328. N is the number of matches to move, when negative move backwards.
  12329. RESET is entirely ignored - this function always goes back to the
  12330. starting point when no match is found."
  12331. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12332. (search-func (if (< n 0)
  12333. 'previous-single-char-property-change
  12334. 'next-single-char-property-change))
  12335. (n (abs n))
  12336. (pos (point))
  12337. p1)
  12338. (catch 'exit
  12339. (while (setq p1 (funcall search-func (point) 'org-type))
  12340. (when (equal p1 limit)
  12341. (goto-char pos)
  12342. (error "No more matches"))
  12343. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12344. (setq n (1- n))
  12345. (when (= n 0)
  12346. (goto-char p1)
  12347. (throw 'exit (point))))
  12348. (goto-char p1))
  12349. (goto-char p1)
  12350. (error "No more matches"))))
  12351. (defun org-show-context (&optional key)
  12352. "Make sure point and context are visible.
  12353. How much context is shown depends upon the variables
  12354. `org-show-hierarchy-above', `org-show-following-heading',
  12355. `org-show-entry-below' and `org-show-siblings'."
  12356. (let ((heading-p (org-at-heading-p t))
  12357. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12358. (following-p (org-get-alist-option org-show-following-heading key))
  12359. (entry-p (org-get-alist-option org-show-entry-below key))
  12360. (siblings-p (org-get-alist-option org-show-siblings key)))
  12361. ;; Show heading or entry text
  12362. (if (and heading-p (not entry-p))
  12363. (org-flag-heading nil) ; only show the heading
  12364. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12365. (org-show-hidden-entry))) ; show entire entry
  12366. (when following-p
  12367. ;; Show next sibling, or heading below text
  12368. (save-excursion
  12369. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12370. (org-flag-heading nil))))
  12371. (when siblings-p (org-show-siblings))
  12372. (when hierarchy-p
  12373. ;; show all higher headings, possibly with siblings
  12374. (save-excursion
  12375. (while (and (condition-case nil
  12376. (progn (org-up-heading-all 1) t)
  12377. (error nil))
  12378. (not (bobp)))
  12379. (org-flag-heading nil)
  12380. (when siblings-p (org-show-siblings)))))
  12381. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12382. (defvar org-reveal-start-hook nil
  12383. "Hook run before revealing a location.")
  12384. (defun org-reveal (&optional siblings)
  12385. "Show current entry, hierarchy above it, and the following headline.
  12386. This can be used to show a consistent set of context around locations
  12387. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12388. not t for the search context.
  12389. With optional argument SIBLINGS, on each level of the hierarchy all
  12390. siblings are shown. This repairs the tree structure to what it would
  12391. look like when opened with hierarchical calls to `org-cycle'.
  12392. With double optional argument \\[universal-argument] \\[universal-argument], \
  12393. go to the parent and show the
  12394. entire tree."
  12395. (interactive "P")
  12396. (run-hooks 'org-reveal-start-hook)
  12397. (let ((org-show-hierarchy-above t)
  12398. (org-show-following-heading t)
  12399. (org-show-siblings (if siblings t org-show-siblings)))
  12400. (org-show-context nil))
  12401. (when (equal siblings '(16))
  12402. (save-excursion
  12403. (when (org-up-heading-safe)
  12404. (org-show-subtree)
  12405. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12406. (defun org-highlight-new-match (beg end)
  12407. "Highlight from BEG to END and mark the highlight is an occur headline."
  12408. (let ((ov (make-overlay beg end)))
  12409. (overlay-put ov 'face 'secondary-selection)
  12410. (overlay-put ov 'org-type 'org-occur)
  12411. (push ov org-occur-highlights)))
  12412. (defun org-remove-occur-highlights (&optional beg end noremove)
  12413. "Remove the occur highlights from the buffer.
  12414. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12415. from the `before-change-functions' in the current buffer."
  12416. (interactive)
  12417. (unless org-inhibit-highlight-removal
  12418. (mapc 'delete-overlay org-occur-highlights)
  12419. (setq org-occur-highlights nil)
  12420. (setq org-occur-parameters nil)
  12421. (unless noremove
  12422. (remove-hook 'before-change-functions
  12423. 'org-remove-occur-highlights 'local))))
  12424. ;;;; Priorities
  12425. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12426. "Regular expression matching the priority indicator.")
  12427. (defvar org-remove-priority-next-time nil)
  12428. (defun org-priority-up ()
  12429. "Increase the priority of the current item."
  12430. (interactive)
  12431. (org-priority 'up))
  12432. (defun org-priority-down ()
  12433. "Decrease the priority of the current item."
  12434. (interactive)
  12435. (org-priority 'down))
  12436. (defun org-priority (&optional action show)
  12437. "Change the priority of an item.
  12438. ACTION can be `set', `up', `down', or a character."
  12439. (interactive "P")
  12440. (if (equal action '(4))
  12441. (org-show-priority)
  12442. (unless org-enable-priority-commands
  12443. (user-error "Priority commands are disabled"))
  12444. (setq action (or action 'set))
  12445. (let (current new news have remove)
  12446. (save-excursion
  12447. (org-back-to-heading t)
  12448. (if (looking-at org-priority-regexp)
  12449. (setq current (string-to-char (match-string 2))
  12450. have t))
  12451. (cond
  12452. ((eq action 'remove)
  12453. (setq remove t new ?\ ))
  12454. ((or (eq action 'set)
  12455. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12456. (if (not (eq action 'set))
  12457. (setq new action)
  12458. (message "Priority %c-%c, SPC to remove: "
  12459. org-highest-priority org-lowest-priority)
  12460. (save-match-data
  12461. (setq new (read-char-exclusive))))
  12462. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12463. (= (upcase org-lowest-priority) org-lowest-priority))
  12464. (setq new (upcase new)))
  12465. (cond ((equal new ?\ ) (setq remove t))
  12466. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12467. (user-error "Priority must be between `%c' and `%c'"
  12468. org-highest-priority org-lowest-priority))))
  12469. ((eq action 'up)
  12470. (setq new (if have
  12471. (1- current) ; normal cycling
  12472. ;; last priority was empty
  12473. (if (eq last-command this-command)
  12474. org-lowest-priority ; wrap around empty to lowest
  12475. ;; default
  12476. (if org-priority-start-cycle-with-default
  12477. org-default-priority
  12478. (1- org-default-priority))))))
  12479. ((eq action 'down)
  12480. (setq new (if have
  12481. (1+ current) ; normal cycling
  12482. ;; last priority was empty
  12483. (if (eq last-command this-command)
  12484. org-highest-priority ; wrap around empty to highest
  12485. ;; default
  12486. (if org-priority-start-cycle-with-default
  12487. org-default-priority
  12488. (1+ org-default-priority))))))
  12489. (t (user-error "Invalid action")))
  12490. (if (or (< (upcase new) org-highest-priority)
  12491. (> (upcase new) org-lowest-priority))
  12492. (if (and (memq action '(up down))
  12493. (not have) (not (eq last-command this-command)))
  12494. ;; `new' is from default priority
  12495. (error
  12496. "The default can not be set, see `org-default-priority' why")
  12497. ;; normal cycling: `new' is beyond highest/lowest priority
  12498. ;; and is wrapped around to the empty priority
  12499. (setq remove t)))
  12500. (setq news (format "%c" new))
  12501. (if have
  12502. (if remove
  12503. (replace-match "" t t nil 1)
  12504. (replace-match news t t nil 2))
  12505. (if remove
  12506. (user-error "No priority cookie found in line")
  12507. (let ((case-fold-search nil))
  12508. (looking-at org-todo-line-regexp))
  12509. (if (match-end 2)
  12510. (progn
  12511. (goto-char (match-end 2))
  12512. (insert " [#" news "]"))
  12513. (goto-char (match-beginning 3))
  12514. (insert "[#" news "] "))))
  12515. (org-preserve-lc (org-set-tags nil 'align)))
  12516. (if remove
  12517. (message "Priority removed")
  12518. (message "Priority of current item set to %s" news)))))
  12519. (defun org-show-priority ()
  12520. "Show the priority of the current item.
  12521. This priority is composed of the main priority given with the [#A] cookies,
  12522. and by additional input from the age of a schedules or deadline entry."
  12523. (interactive)
  12524. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12525. (org-get-at-bol 'priority)
  12526. (save-excursion
  12527. (save-match-data
  12528. (beginning-of-line)
  12529. (and (looking-at org-heading-regexp)
  12530. (org-get-priority (match-string 0))))))))
  12531. (message "Priority is %d" (if pri pri -1000))))
  12532. (defun org-get-priority (s)
  12533. "Find priority cookie and return priority."
  12534. (save-match-data
  12535. (if (functionp org-get-priority-function)
  12536. (funcall org-get-priority-function)
  12537. (if (not (string-match org-priority-regexp s))
  12538. (* 1000 (- org-lowest-priority org-default-priority))
  12539. (* 1000 (- org-lowest-priority
  12540. (string-to-char (match-string 2 s))))))))
  12541. ;;;; Tags
  12542. (defvar org-agenda-archives-mode)
  12543. (defvar org-map-continue-from nil
  12544. "Position from where mapping should continue.
  12545. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12546. (defvar org-scanner-tags nil
  12547. "The current tag list while the tags scanner is running.")
  12548. (defvar org-trust-scanner-tags nil
  12549. "Should `org-get-tags-at' use the tags for the scanner.
  12550. This is for internal dynamical scoping only.
  12551. When this is non-nil, the function `org-get-tags-at' will return the value
  12552. of `org-scanner-tags' instead of building the list by itself. This
  12553. can lead to large speed-ups when the tags scanner is used in a file with
  12554. many entries, and when the list of tags is retrieved, for example to
  12555. obtain a list of properties. Building the tags list for each entry in such
  12556. a file becomes an N^2 operation - but with this variable set, it scales
  12557. as N.")
  12558. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12559. "Sca headline tags with inheritance and produce output ACTION.
  12560. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12561. or `agenda' to produce an entry list for an agenda view. It can also be
  12562. a Lisp form or a function that should be called at each matched headline, in
  12563. this case the return value is a list of all return values from these calls.
  12564. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12565. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12566. only lines with a not-done TODO keyword are included in the output.
  12567. This should be the same variable that was scoped into
  12568. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12569. START-LEVEL can be a string with asterisks, reducing the scope to
  12570. headlines matching this string."
  12571. (require 'org-agenda)
  12572. (let* ((re (concat "^"
  12573. (if start-level
  12574. ;; Get the correct level to match
  12575. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12576. org-outline-regexp)
  12577. " *\\(\\<\\("
  12578. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12579. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12580. (props (list 'face 'default
  12581. 'done-face 'org-agenda-done
  12582. 'undone-face 'default
  12583. 'mouse-face 'highlight
  12584. 'org-not-done-regexp org-not-done-regexp
  12585. 'org-todo-regexp org-todo-regexp
  12586. 'org-complex-heading-regexp org-complex-heading-regexp
  12587. 'help-echo
  12588. (format "mouse-2 or RET jump to org file %s"
  12589. (abbreviate-file-name
  12590. (or (buffer-file-name (buffer-base-buffer))
  12591. (buffer-name (buffer-base-buffer)))))))
  12592. (case-fold-search nil)
  12593. (org-map-continue-from nil)
  12594. lspos tags tags-list
  12595. (tags-alist (list (cons 0 org-file-tags)))
  12596. (llast 0) rtn rtn1 level category i txt
  12597. todo marker entry priority)
  12598. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12599. (setq action (list 'lambda nil action)))
  12600. (save-excursion
  12601. (goto-char (point-min))
  12602. (when (eq action 'sparse-tree)
  12603. (org-overview)
  12604. (org-remove-occur-highlights))
  12605. (while (re-search-forward re nil t)
  12606. (setq org-map-continue-from nil)
  12607. (catch :skip
  12608. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12609. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12610. (goto-char (setq lspos (match-beginning 0)))
  12611. (setq level (org-reduced-level (org-outline-level))
  12612. category (org-get-category))
  12613. (setq i llast llast level)
  12614. ;; remove tag lists from same and sublevels
  12615. (while (>= i level)
  12616. (when (setq entry (assoc i tags-alist))
  12617. (setq tags-alist (delete entry tags-alist)))
  12618. (setq i (1- i)))
  12619. ;; add the next tags
  12620. (when tags
  12621. (setq tags (org-split-string tags ":")
  12622. tags-alist
  12623. (cons (cons level tags) tags-alist)))
  12624. ;; compile tags for current headline
  12625. (setq tags-list
  12626. (if org-use-tag-inheritance
  12627. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12628. tags)
  12629. org-scanner-tags tags-list)
  12630. (when org-use-tag-inheritance
  12631. (setcdr (car tags-alist)
  12632. (mapcar (lambda (x)
  12633. (setq x (copy-sequence x))
  12634. (org-add-prop-inherited x))
  12635. (cdar tags-alist))))
  12636. (when (and tags org-use-tag-inheritance
  12637. (or (not (eq t org-use-tag-inheritance))
  12638. org-tags-exclude-from-inheritance))
  12639. ;; selective inheritance, remove uninherited ones
  12640. (setcdr (car tags-alist)
  12641. (org-remove-uninherited-tags (cdar tags-alist))))
  12642. (when (and
  12643. ;; eval matcher only when the todo condition is OK
  12644. (and (or (not todo-only) (member todo org-not-done-keywords))
  12645. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12646. (eval matcher)))
  12647. ;; Call the skipper, but return t if it does not skip,
  12648. ;; so that the `and' form continues evaluating
  12649. (progn
  12650. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12651. t)
  12652. ;; Check if timestamps are deselecting this entry
  12653. (or (not todo-only)
  12654. (and (member todo org-not-done-keywords)
  12655. (or (not org-agenda-tags-todo-honor-ignore-options)
  12656. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12657. ;; select this headline
  12658. (cond
  12659. ((eq action 'sparse-tree)
  12660. (and org-highlight-sparse-tree-matches
  12661. (org-get-heading) (match-end 0)
  12662. (org-highlight-new-match
  12663. (match-beginning 1) (match-end 1)))
  12664. (org-show-context 'tags-tree))
  12665. ((eq action 'agenda)
  12666. (setq txt (org-agenda-format-item
  12667. ""
  12668. (concat
  12669. (if (eq org-tags-match-list-sublevels 'indented)
  12670. (make-string (1- level) ?.) "")
  12671. (org-get-heading))
  12672. level category
  12673. tags-list)
  12674. priority (org-get-priority txt))
  12675. (goto-char lspos)
  12676. (setq marker (org-agenda-new-marker))
  12677. (org-add-props txt props
  12678. 'org-marker marker 'org-hd-marker marker 'org-category category
  12679. 'todo-state todo
  12680. 'priority priority 'type "tagsmatch")
  12681. (push txt rtn))
  12682. ((functionp action)
  12683. (setq org-map-continue-from nil)
  12684. (save-excursion
  12685. (setq rtn1 (funcall action))
  12686. (push rtn1 rtn)))
  12687. (t (user-error "Invalid action")))
  12688. ;; if we are to skip sublevels, jump to end of subtree
  12689. (unless org-tags-match-list-sublevels
  12690. (org-end-of-subtree t)
  12691. (backward-char 1))))
  12692. ;; Get the correct position from where to continue
  12693. (if org-map-continue-from
  12694. (goto-char org-map-continue-from)
  12695. (and (= (point) lspos) (end-of-line 1)))))
  12696. (when (and (eq action 'sparse-tree)
  12697. (not org-sparse-tree-open-archived-trees))
  12698. (org-hide-archived-subtrees (point-min) (point-max)))
  12699. (nreverse rtn)))
  12700. (defun org-remove-uninherited-tags (tags)
  12701. "Remove all tags that are not inherited from the list TAGS."
  12702. (cond
  12703. ((eq org-use-tag-inheritance t)
  12704. (if org-tags-exclude-from-inheritance
  12705. (org-delete-all org-tags-exclude-from-inheritance tags)
  12706. tags))
  12707. ((not org-use-tag-inheritance) nil)
  12708. ((stringp org-use-tag-inheritance)
  12709. (delq nil (mapcar
  12710. (lambda (x)
  12711. (if (and (string-match org-use-tag-inheritance x)
  12712. (not (member x org-tags-exclude-from-inheritance)))
  12713. x nil))
  12714. tags)))
  12715. ((listp org-use-tag-inheritance)
  12716. (delq nil (mapcar
  12717. (lambda (x)
  12718. (if (member x org-use-tag-inheritance) x nil))
  12719. tags)))))
  12720. (defun org-match-sparse-tree (&optional todo-only match)
  12721. "Create a sparse tree according to tags string MATCH.
  12722. MATCH can contain positive and negative selection of tags, like
  12723. \"+WORK+URGENT-WITHBOSS\".
  12724. If optional argument TODO-ONLY is non-nil, only select lines that are
  12725. also TODO lines."
  12726. (interactive "P")
  12727. (org-agenda-prepare-buffers (list (current-buffer)))
  12728. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12729. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12730. (defvar org-cached-props nil)
  12731. (defun org-cached-entry-get (pom property)
  12732. (if (or (eq t org-use-property-inheritance)
  12733. (and (stringp org-use-property-inheritance)
  12734. (string-match org-use-property-inheritance property))
  12735. (and (listp org-use-property-inheritance)
  12736. (member property org-use-property-inheritance)))
  12737. ;; Caching is not possible, check it directly
  12738. (org-entry-get pom property 'inherit)
  12739. ;; Get all properties, so that we can do complicated checks easily
  12740. (cdr (assoc property (or org-cached-props
  12741. (setq org-cached-props
  12742. (org-entry-properties pom)))))))
  12743. (defun org-global-tags-completion-table (&optional files)
  12744. "Return the list of all tags in all agenda buffer/files.
  12745. Optional FILES argument is a list of files which can be used
  12746. instead of the agenda files."
  12747. (save-excursion
  12748. (org-uniquify
  12749. (delq nil
  12750. (apply 'append
  12751. (mapcar
  12752. (lambda (file)
  12753. (set-buffer (find-file-noselect file))
  12754. (append (org-get-buffer-tags)
  12755. (mapcar (lambda (x) (if (stringp (car-safe x))
  12756. (list (car-safe x)) nil))
  12757. org-tag-alist)))
  12758. (if (and files (car files))
  12759. files
  12760. (org-agenda-files))))))))
  12761. (defun org-make-tags-matcher (match)
  12762. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12763. The variable `todo-only' is scoped dynamically into this function.
  12764. It will be set to t if the matcher restricts matching to TODO entries,
  12765. otherwise will not be touched.
  12766. Returns a cons of the selection string MATCH and the constructed
  12767. lisp form implementing the matcher. The matcher is to be evaluated
  12768. at an Org entry, with point on the headline, and returns t if the
  12769. entry matches the selection string MATCH. The returned lisp form
  12770. references two variables with information about the entry, which
  12771. must be bound around the form's evaluation: todo, the TODO keyword
  12772. at the entry (or nil of none); and tags-list, the list of all tags
  12773. at the entry including inherited ones. Additionally, the category
  12774. of the entry (if any) must be specified as the text property
  12775. 'org-category on the headline.
  12776. See also `org-scan-tags'.
  12777. "
  12778. (declare (special todo-only))
  12779. (unless (boundp 'todo-only)
  12780. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12781. (unless match
  12782. ;; Get a new match request, with completion against the global
  12783. ;; tags table and the local tags in current buffer
  12784. (let ((org-last-tags-completion-table
  12785. (org-uniquify
  12786. (delq nil (append (org-get-buffer-tags)
  12787. (org-global-tags-completion-table))))))
  12788. (setq match (org-completing-read-no-i
  12789. "Match: " 'org-tags-completion-function nil nil nil
  12790. 'org-tags-history))))
  12791. ;; Parse the string and create a lisp form
  12792. (let ((match0 match)
  12793. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12794. minus tag mm
  12795. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12796. orterms term orlist re-p str-p level-p level-op time-p
  12797. prop-p pn pv po gv rest)
  12798. ;; Expand group tags
  12799. (setq match (org-tags-expand match))
  12800. (if (string-match "/+" match)
  12801. ;; match contains also a todo-matching request
  12802. (progn
  12803. (setq tagsmatch (substring match 0 (match-beginning 0))
  12804. todomatch (substring match (match-end 0)))
  12805. (if (string-match "^!" todomatch)
  12806. (setq todo-only t todomatch (substring todomatch 1)))
  12807. (if (string-match "^\\s-*$" todomatch)
  12808. (setq todomatch nil)))
  12809. ;; only matching tags
  12810. (setq tagsmatch match todomatch nil))
  12811. ;; Make the tags matcher
  12812. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12813. (setq tagsmatcher t)
  12814. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12815. (while (setq term (pop orterms))
  12816. (while (and (equal (substring term -1) "\\") orterms)
  12817. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12818. (while (string-match re term)
  12819. (setq rest (substring term (match-end 0))
  12820. minus (and (match-end 1)
  12821. (equal (match-string 1 term) "-"))
  12822. tag (save-match-data (replace-regexp-in-string
  12823. "\\\\-" "-"
  12824. (match-string 2 term)))
  12825. re-p (equal (string-to-char tag) ?{)
  12826. level-p (match-end 4)
  12827. prop-p (match-end 5)
  12828. mm (cond
  12829. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12830. (level-p
  12831. (setq level-op (org-op-to-function (match-string 3 term)))
  12832. `(,level-op level ,(string-to-number
  12833. (match-string 4 term))))
  12834. (prop-p
  12835. (setq pn (match-string 5 term)
  12836. po (match-string 6 term)
  12837. pv (match-string 7 term)
  12838. re-p (equal (string-to-char pv) ?{)
  12839. str-p (equal (string-to-char pv) ?\")
  12840. time-p (save-match-data
  12841. (string-match "^\"[[<].*[]>]\"$" pv))
  12842. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12843. (if time-p (setq pv (org-matcher-time pv)))
  12844. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12845. (cond
  12846. ((equal pn "CATEGORY")
  12847. (setq gv '(get-text-property (point) 'org-category)))
  12848. ((equal pn "TODO")
  12849. (setq gv 'todo))
  12850. (t
  12851. (setq gv `(org-cached-entry-get nil ,pn))))
  12852. (if re-p
  12853. (if (eq po 'org<>)
  12854. `(not (string-match ,pv (or ,gv "")))
  12855. `(string-match ,pv (or ,gv "")))
  12856. (if str-p
  12857. `(,po (or ,gv "") ,pv)
  12858. `(,po (string-to-number (or ,gv ""))
  12859. ,(string-to-number pv) ))))
  12860. (t `(member ,tag tags-list)))
  12861. mm (if minus (list 'not mm) mm)
  12862. term rest)
  12863. (push mm tagsmatcher))
  12864. (push (if (> (length tagsmatcher) 1)
  12865. (cons 'and tagsmatcher)
  12866. (car tagsmatcher))
  12867. orlist)
  12868. (setq tagsmatcher nil))
  12869. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12870. (setq tagsmatcher
  12871. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12872. ;; Make the todo matcher
  12873. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12874. (setq todomatcher t)
  12875. (setq orterms (org-split-string todomatch "|") orlist nil)
  12876. (while (setq term (pop orterms))
  12877. (while (string-match re term)
  12878. (setq minus (and (match-end 1)
  12879. (equal (match-string 1 term) "-"))
  12880. kwd (match-string 2 term)
  12881. re-p (equal (string-to-char kwd) ?{)
  12882. term (substring term (match-end 0))
  12883. mm (if re-p
  12884. `(string-match ,(substring kwd 1 -1) todo)
  12885. (list 'equal 'todo kwd))
  12886. mm (if minus (list 'not mm) mm))
  12887. (push mm todomatcher))
  12888. (push (if (> (length todomatcher) 1)
  12889. (cons 'and todomatcher)
  12890. (car todomatcher))
  12891. orlist)
  12892. (setq todomatcher nil))
  12893. (setq todomatcher (if (> (length orlist) 1)
  12894. (cons 'or orlist) (car orlist))))
  12895. ;; Return the string and lisp forms of the matcher
  12896. (setq matcher (if todomatcher
  12897. (list 'and tagsmatcher todomatcher)
  12898. tagsmatcher))
  12899. (when todo-only
  12900. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12901. matcher)))
  12902. (cons match0 matcher)))
  12903. (defun org-tags-expand (match &optional single-as-list downcased)
  12904. "Expand group tags in MATCH.
  12905. This replaces every group tag in MATCH with a regexp tag search.
  12906. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  12907. will be replaced like this:
  12908. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  12909. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  12910. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  12911. Replacing by a regexp preserves the structure of the match.
  12912. E.g., this expansion
  12913. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  12914. will match anything tagged with \"Lab\" and \"Home\", or tagged
  12915. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  12916. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  12917. assumed to be a single group tag, and the function will return
  12918. the list of tags in this group.
  12919. When DOWNCASE is non-nil, expand downcased TAGS."
  12920. (if org-group-tags
  12921. (let* ((case-fold-search t)
  12922. (stable org-mode-syntax-table)
  12923. (tal (or org-tag-groups-alist-for-agenda
  12924. org-tag-groups-alist))
  12925. (tal (if downcased
  12926. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  12927. (tml (mapcar 'car tal))
  12928. (rtnmatch match) rpl)
  12929. ;; @ and _ are allowed as word-components in tags
  12930. (modify-syntax-entry ?@ "w" stable)
  12931. (modify-syntax-entry ?_ "w" stable)
  12932. (while (and tml
  12933. (string-match
  12934. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  12935. (regexp-opt tml) "\\>\\)") rtnmatch))
  12936. (let* ((dir (match-string 1 rtnmatch))
  12937. (tag (match-string 2 rtnmatch))
  12938. (tag (if downcased (downcase tag) tag)))
  12939. (setq tml (delete tag tml))
  12940. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  12941. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  12942. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  12943. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  12944. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  12945. (if single-as-list
  12946. (or (reverse rpl) (list rtnmatch))
  12947. rtnmatch))
  12948. (if single-as-list (list (if downcased (downcase match) match))
  12949. match)))
  12950. (defun org-op-to-function (op &optional stringp)
  12951. "Turn an operator into the appropriate function."
  12952. (setq op
  12953. (cond
  12954. ((equal op "<" ) '(< string< org-time<))
  12955. ((equal op ">" ) '(> org-string> org-time>))
  12956. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12957. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12958. ((member op '("=" "==")) '(= string= org-time=))
  12959. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12960. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12961. (defun org<> (a b) (not (= a b)))
  12962. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12963. (defun org-string>= (a b) (not (string< a b)))
  12964. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12965. (defun org-string<> (a b) (not (string= a b)))
  12966. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12967. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12968. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12969. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12970. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12971. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12972. (defun org-2ft (s)
  12973. "Convert S to a floating point time.
  12974. If S is already a number, just return it. If it is a string, parse
  12975. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12976. (cond
  12977. ((numberp s) s)
  12978. ((stringp s)
  12979. (condition-case nil
  12980. (float-time (apply 'encode-time (org-parse-time-string s)))
  12981. (error 0.)))
  12982. (t 0.)))
  12983. (defun org-time-today ()
  12984. "Time in seconds today at 0:00.
  12985. Returns the float number of seconds since the beginning of the
  12986. epoch to the beginning of today (00:00)."
  12987. (float-time (apply 'encode-time
  12988. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12989. (defun org-matcher-time (s)
  12990. "Interpret a time comparison value."
  12991. (save-match-data
  12992. (cond
  12993. ((string= s "<now>") (float-time))
  12994. ((string= s "<today>") (org-time-today))
  12995. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12996. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12997. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12998. (+ (org-time-today)
  12999. (* (string-to-number (match-string 1 s))
  13000. (cdr (assoc (match-string 2 s)
  13001. '(("d" . 86400.0) ("w" . 604800.0)
  13002. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13003. (t (org-2ft s)))))
  13004. (defun org-match-any-p (re list)
  13005. "Does re match any element of list?"
  13006. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13007. (delq nil list))
  13008. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13009. (defvar org-tags-overlay (make-overlay 1 1))
  13010. (org-detach-overlay org-tags-overlay)
  13011. (defun org-get-local-tags-at (&optional pos)
  13012. "Get a list of tags defined in the current headline."
  13013. (org-get-tags-at pos 'local))
  13014. (defun org-get-local-tags ()
  13015. "Get a list of tags defined in the current headline."
  13016. (org-get-tags-at nil 'local))
  13017. (defun org-get-tags-at (&optional pos local)
  13018. "Get a list of all headline tags applicable at POS.
  13019. POS defaults to point. If tags are inherited, the list contains
  13020. the targets in the same sequence as the headlines appear, i.e.
  13021. the tags of the current headline come last.
  13022. When LOCAL is non-nil, only return tags from the current headline,
  13023. ignore inherited ones."
  13024. (interactive)
  13025. (if (and org-trust-scanner-tags
  13026. (or (not pos) (equal pos (point)))
  13027. (not local))
  13028. org-scanner-tags
  13029. (let (tags ltags lastpos parent)
  13030. (save-excursion
  13031. (save-restriction
  13032. (widen)
  13033. (goto-char (or pos (point)))
  13034. (save-match-data
  13035. (catch 'done
  13036. (condition-case nil
  13037. (progn
  13038. (org-back-to-heading t)
  13039. (while (not (equal lastpos (point)))
  13040. (setq lastpos (point))
  13041. (when (looking-at
  13042. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13043. (setq ltags (org-split-string
  13044. (org-match-string-no-properties 1) ":"))
  13045. (when parent
  13046. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13047. (setq tags (append
  13048. (if parent
  13049. (org-remove-uninherited-tags ltags)
  13050. ltags)
  13051. tags)))
  13052. (or org-use-tag-inheritance (throw 'done t))
  13053. (if local (throw 'done t))
  13054. (or (org-up-heading-safe) (error nil))
  13055. (setq parent t)))
  13056. (error nil)))))
  13057. (if local
  13058. tags
  13059. (reverse (delete-dups
  13060. (reverse (append
  13061. (org-remove-uninherited-tags
  13062. org-file-tags) tags)))))))))
  13063. (defun org-add-prop-inherited (s)
  13064. (add-text-properties 0 (length s) '(inherited t) s)
  13065. s)
  13066. (defun org-toggle-tag (tag &optional onoff)
  13067. "Toggle the tag TAG for the current line.
  13068. If ONOFF is `on' or `off', don't toggle but set to this state."
  13069. (let (res current)
  13070. (save-excursion
  13071. (org-back-to-heading t)
  13072. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13073. (point-at-eol) t)
  13074. (progn
  13075. (setq current (match-string 1))
  13076. (replace-match ""))
  13077. (setq current ""))
  13078. (setq current (nreverse (org-split-string current ":")))
  13079. (cond
  13080. ((eq onoff 'on)
  13081. (setq res t)
  13082. (or (member tag current) (push tag current)))
  13083. ((eq onoff 'off)
  13084. (or (not (member tag current)) (setq current (delete tag current))))
  13085. (t (if (member tag current)
  13086. (setq current (delete tag current))
  13087. (setq res t)
  13088. (push tag current))))
  13089. (end-of-line 1)
  13090. (if current
  13091. (progn
  13092. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13093. (org-set-tags nil t))
  13094. (delete-horizontal-space))
  13095. (run-hooks 'org-after-tags-change-hook))
  13096. res))
  13097. (defun org-align-tags-here (to-col)
  13098. ;; Assumes that this is a headline
  13099. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13100. (beginning-of-line 1)
  13101. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13102. (< pos (match-beginning 2)))
  13103. (progn
  13104. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13105. (goto-char (match-beginning 1))
  13106. (insert " ")
  13107. (delete-region (point) (1+ (match-beginning 2)))
  13108. (setq ncol (max (current-column)
  13109. (1+ col)
  13110. (if (> to-col 0)
  13111. to-col
  13112. (- (abs to-col) tags-l))))
  13113. (setq p (point))
  13114. (insert (make-string (- ncol (current-column)) ?\ ))
  13115. (setq ncol (current-column))
  13116. (when indent-tabs-mode (tabify p (point-at-eol)))
  13117. (org-move-to-column (min ncol col) t))
  13118. (goto-char pos))))
  13119. (defun org-set-tags-command (&optional arg just-align)
  13120. "Call the set-tags command for the current entry."
  13121. (interactive "P")
  13122. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13123. (org-set-tags arg just-align)
  13124. (save-excursion
  13125. (unless (and (org-region-active-p)
  13126. org-loop-over-headlines-in-active-region)
  13127. (org-back-to-heading t))
  13128. (org-set-tags arg just-align))))
  13129. (defun org-set-tags-to (data)
  13130. "Set the tags of the current entry to DATA, replacing the current tags.
  13131. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13132. If DATA is nil or the empty string, any tags will be removed."
  13133. (interactive "sTags: ")
  13134. (setq data
  13135. (cond
  13136. ((eq data nil) "")
  13137. ((equal data "") "")
  13138. ((stringp data)
  13139. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13140. ":"))
  13141. ((listp data)
  13142. (concat ":" (mapconcat 'identity data ":") ":"))))
  13143. (when data
  13144. (save-excursion
  13145. (org-back-to-heading t)
  13146. (when (looking-at org-complex-heading-regexp)
  13147. (if (match-end 5)
  13148. (progn
  13149. (goto-char (match-beginning 5))
  13150. (insert data)
  13151. (delete-region (point) (point-at-eol))
  13152. (org-set-tags nil 'align))
  13153. (goto-char (point-at-eol))
  13154. (insert " " data)
  13155. (org-set-tags nil 'align)))
  13156. (beginning-of-line 1)
  13157. (if (looking-at ".*?\\([ \t]+\\)$")
  13158. (delete-region (match-beginning 1) (match-end 1))))))
  13159. (defun org-align-all-tags ()
  13160. "Align the tags i all headings."
  13161. (interactive)
  13162. (save-excursion
  13163. (or (ignore-errors (org-back-to-heading t))
  13164. (outline-next-heading))
  13165. (if (org-at-heading-p)
  13166. (org-set-tags t)
  13167. (message "No headings"))))
  13168. (defvar org-indent-indentation-per-level)
  13169. (defun org-set-tags (&optional arg just-align)
  13170. "Set the tags for the current headline.
  13171. With prefix ARG, realign all tags in headings in the current buffer."
  13172. (interactive "P")
  13173. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13174. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13175. 'region-start-level 'region))
  13176. org-loop-over-headlines-in-active-region)
  13177. (org-map-entries
  13178. ;; We don't use ARG and JUST-ALIGN here these args are not
  13179. ;; useful when looping over headlines
  13180. `(org-set-tags)
  13181. org-loop-over-headlines-in-active-region
  13182. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13183. (let* ((re org-outline-regexp-bol)
  13184. (current (unless arg (org-get-tags-string)))
  13185. (col (current-column))
  13186. (org-setting-tags t)
  13187. table current-tags inherited-tags ; computed below when needed
  13188. tags p0 c0 c1 rpl di tc level)
  13189. (if arg
  13190. (save-excursion
  13191. (goto-char (point-min))
  13192. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13193. (while (re-search-forward re nil t)
  13194. (org-set-tags nil t)
  13195. (end-of-line 1)))
  13196. (message "All tags realigned to column %d" org-tags-column))
  13197. (if just-align
  13198. (setq tags current)
  13199. ;; Get a new set of tags from the user
  13200. (save-excursion
  13201. (setq table (append org-tag-persistent-alist
  13202. (or org-tag-alist (org-get-buffer-tags))
  13203. (and
  13204. org-complete-tags-always-offer-all-agenda-tags
  13205. (org-global-tags-completion-table
  13206. (org-agenda-files))))
  13207. org-last-tags-completion-table table
  13208. current-tags (org-split-string current ":")
  13209. inherited-tags (nreverse
  13210. (nthcdr (length current-tags)
  13211. (nreverse (org-get-tags-at))))
  13212. tags
  13213. (if (or (eq t org-use-fast-tag-selection)
  13214. (and org-use-fast-tag-selection
  13215. (delq nil (mapcar 'cdr table))))
  13216. (org-fast-tag-selection
  13217. current-tags inherited-tags table
  13218. (if org-fast-tag-selection-include-todo
  13219. org-todo-key-alist))
  13220. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13221. (org-trim
  13222. (org-icompleting-read "Tags: "
  13223. 'org-tags-completion-function
  13224. nil nil current 'org-tags-history))))))
  13225. (while (string-match "[-+&]+" tags)
  13226. ;; No boolean logic, just a list
  13227. (setq tags (replace-match ":" t t tags))))
  13228. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13229. (if org-tags-sort-function
  13230. (setq tags (mapconcat 'identity
  13231. (sort (org-split-string
  13232. tags (org-re "[^[:alnum:]_@#%]+"))
  13233. org-tags-sort-function) ":")))
  13234. (if (string-match "\\`[\t ]*\\'" tags)
  13235. (setq tags "")
  13236. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13237. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13238. ;; Insert new tags at the correct column
  13239. (beginning-of-line 1)
  13240. (setq level (or (and (looking-at org-outline-regexp)
  13241. (- (match-end 0) (point) 1))
  13242. 1))
  13243. (cond
  13244. ((and (equal current "") (equal tags "")))
  13245. ((re-search-forward
  13246. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13247. (point-at-eol) t)
  13248. (if (equal tags "")
  13249. (setq rpl "")
  13250. (goto-char (match-beginning 0))
  13251. (setq c0 (current-column)
  13252. ;; compute offset for the case of org-indent-mode active
  13253. di (if org-indent-mode
  13254. (* (1- org-indent-indentation-per-level) (1- level))
  13255. 0)
  13256. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13257. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13258. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13259. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13260. (replace-match rpl t t)
  13261. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13262. tags)
  13263. (t (error "Tags alignment failed")))
  13264. (org-move-to-column col)
  13265. (unless just-align
  13266. (run-hooks 'org-after-tags-change-hook))))))
  13267. (defun org-change-tag-in-region (beg end tag off)
  13268. "Add or remove TAG for each entry in the region.
  13269. This works in the agenda, and also in an org-mode buffer."
  13270. (interactive
  13271. (list (region-beginning) (region-end)
  13272. (let ((org-last-tags-completion-table
  13273. (if (derived-mode-p 'org-mode)
  13274. (org-uniquify
  13275. (delq nil (append (org-get-buffer-tags)
  13276. (org-global-tags-completion-table))))
  13277. (org-global-tags-completion-table))))
  13278. (org-icompleting-read
  13279. "Tag: " 'org-tags-completion-function nil nil nil
  13280. 'org-tags-history))
  13281. (progn
  13282. (message "[s]et or [r]emove? ")
  13283. (equal (read-char-exclusive) ?r))))
  13284. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13285. (let ((agendap (equal major-mode 'org-agenda-mode))
  13286. l1 l2 m buf pos newhead (cnt 0))
  13287. (goto-char end)
  13288. (setq l2 (1- (org-current-line)))
  13289. (goto-char beg)
  13290. (setq l1 (org-current-line))
  13291. (loop for l from l1 to l2 do
  13292. (org-goto-line l)
  13293. (setq m (get-text-property (point) 'org-hd-marker))
  13294. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13295. (and agendap m))
  13296. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13297. pos (if agendap m (point)))
  13298. (with-current-buffer buf
  13299. (save-excursion
  13300. (save-restriction
  13301. (goto-char pos)
  13302. (setq cnt (1+ cnt))
  13303. (org-toggle-tag tag (if off 'off 'on))
  13304. (setq newhead (org-get-heading)))))
  13305. (and agendap (org-agenda-change-all-lines newhead m))))
  13306. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13307. (defun org-tags-completion-function (string predicate &optional flag)
  13308. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13309. (confirm (lambda (x) (stringp (car x)))))
  13310. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13311. (setq s1 (match-string 1 string)
  13312. s2 (match-string 2 string))
  13313. (setq s1 "" s2 string))
  13314. (cond
  13315. ((eq flag nil)
  13316. ;; try completion
  13317. (setq rtn (try-completion s2 ctable confirm))
  13318. (if (stringp rtn)
  13319. (setq rtn
  13320. (concat s1 s2 (substring rtn (length s2))
  13321. (if (and org-add-colon-after-tag-completion
  13322. (assoc rtn ctable))
  13323. ":" ""))))
  13324. rtn)
  13325. ((eq flag t)
  13326. ;; all-completions
  13327. (all-completions s2 ctable confirm))
  13328. ((eq flag 'lambda)
  13329. ;; exact match?
  13330. (assoc s2 ctable)))))
  13331. (defun org-fast-tag-insert (kwd tags face &optional end)
  13332. "Insert KDW, and the TAGS, the latter with face FACE.
  13333. Also insert END."
  13334. (insert (format "%-12s" (concat kwd ":"))
  13335. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13336. (or end "")))
  13337. (defun org-fast-tag-show-exit (flag)
  13338. (save-excursion
  13339. (org-goto-line 3)
  13340. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13341. (replace-match ""))
  13342. (when flag
  13343. (end-of-line 1)
  13344. (org-move-to-column (- (window-width) 19) t)
  13345. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13346. (defun org-set-current-tags-overlay (current prefix)
  13347. "Add an overlay to CURRENT tag with PREFIX."
  13348. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13349. (if (featurep 'xemacs)
  13350. (org-overlay-display org-tags-overlay (concat prefix s)
  13351. 'secondary-selection)
  13352. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13353. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13354. (defvar org-last-tag-selection-key nil)
  13355. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13356. "Fast tag selection with single keys.
  13357. CURRENT is the current list of tags in the headline, INHERITED is the
  13358. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13359. possibly with grouping information. TODO-TABLE is a similar table with
  13360. TODO keywords, should these have keys assigned to them.
  13361. If the keys are nil, a-z are automatically assigned.
  13362. Returns the new tags string, or nil to not change the current settings."
  13363. (let* ((fulltable (append table todo-table))
  13364. (maxlen (apply 'max (mapcar
  13365. (lambda (x)
  13366. (if (stringp (car x)) (string-width (car x)) 0))
  13367. fulltable)))
  13368. (buf (current-buffer))
  13369. (expert (eq org-fast-tag-selection-single-key 'expert))
  13370. (buffer-tags nil)
  13371. (fwidth (+ maxlen 3 1 3))
  13372. (ncol (/ (- (window-width) 4) fwidth))
  13373. (i-face 'org-done)
  13374. (c-face 'org-todo)
  13375. tg cnt e c char c1 c2 ntable tbl rtn
  13376. ov-start ov-end ov-prefix
  13377. (exit-after-next org-fast-tag-selection-single-key)
  13378. (done-keywords org-done-keywords)
  13379. groups ingroup)
  13380. (save-excursion
  13381. (beginning-of-line 1)
  13382. (if (looking-at
  13383. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13384. (setq ov-start (match-beginning 1)
  13385. ov-end (match-end 1)
  13386. ov-prefix "")
  13387. (setq ov-start (1- (point-at-eol))
  13388. ov-end (1+ ov-start))
  13389. (skip-chars-forward "^\n\r")
  13390. (setq ov-prefix
  13391. (concat
  13392. (buffer-substring (1- (point)) (point))
  13393. (if (> (current-column) org-tags-column)
  13394. " "
  13395. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13396. (move-overlay org-tags-overlay ov-start ov-end)
  13397. (save-window-excursion
  13398. (if expert
  13399. (set-buffer (get-buffer-create " *Org tags*"))
  13400. (delete-other-windows)
  13401. (split-window-vertically)
  13402. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13403. (erase-buffer)
  13404. (org-set-local 'org-done-keywords done-keywords)
  13405. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13406. (org-fast-tag-insert "Current" current c-face "\n\n")
  13407. (org-fast-tag-show-exit exit-after-next)
  13408. (org-set-current-tags-overlay current ov-prefix)
  13409. (setq tbl fulltable char ?a cnt 0)
  13410. (while (setq e (pop tbl))
  13411. (cond
  13412. ((equal (car e) :startgroup)
  13413. (push '() groups) (setq ingroup t)
  13414. (when (not (= cnt 0))
  13415. (setq cnt 0)
  13416. (insert "\n"))
  13417. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13418. ((equal (car e) :endgroup)
  13419. (setq ingroup nil cnt 0)
  13420. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13421. ((equal e '(:newline))
  13422. (when (not (= cnt 0))
  13423. (setq cnt 0)
  13424. (insert "\n")
  13425. (setq e (car tbl))
  13426. (while (equal (car tbl) '(:newline))
  13427. (insert "\n")
  13428. (setq tbl (cdr tbl)))))
  13429. ((equal e '(:grouptags)) nil)
  13430. (t
  13431. (setq tg (copy-sequence (car e)) c2 nil)
  13432. (if (cdr e)
  13433. (setq c (cdr e))
  13434. ;; automatically assign a character.
  13435. (setq c1 (string-to-char
  13436. (downcase (substring
  13437. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13438. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13439. (while (or (rassoc char ntable) (rassoc char table))
  13440. (setq char (1+ char)))
  13441. (setq c2 c1))
  13442. (setq c (or c2 char)))
  13443. (if ingroup (push tg (car groups)))
  13444. (setq tg (org-add-props tg nil 'face
  13445. (cond
  13446. ((not (assoc tg table))
  13447. (org-get-todo-face tg))
  13448. ((member tg current) c-face)
  13449. ((member tg inherited) i-face))))
  13450. (if (equal (caar tbl) :grouptags)
  13451. (org-add-props tg nil 'face 'org-tag-group))
  13452. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13453. (insert "[" c "] " tg (make-string
  13454. (- fwidth 4 (length tg)) ?\ ))
  13455. (push (cons tg c) ntable)
  13456. (when (= (setq cnt (1+ cnt)) ncol)
  13457. (insert "\n")
  13458. (if ingroup (insert " "))
  13459. (setq cnt 0)))))
  13460. (setq ntable (nreverse ntable))
  13461. (insert "\n")
  13462. (goto-char (point-min))
  13463. (if (not expert) (org-fit-window-to-buffer))
  13464. (setq rtn
  13465. (catch 'exit
  13466. (while t
  13467. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13468. (if (not groups) "no " "")
  13469. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13470. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13471. (setq org-last-tag-selection-key c)
  13472. (cond
  13473. ((= c ?\r) (throw 'exit t))
  13474. ((= c ?!)
  13475. (setq groups (not groups))
  13476. (goto-char (point-min))
  13477. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13478. ((= c ?\C-c)
  13479. (if (not expert)
  13480. (org-fast-tag-show-exit
  13481. (setq exit-after-next (not exit-after-next)))
  13482. (setq expert nil)
  13483. (delete-other-windows)
  13484. (set-window-buffer (split-window-vertically) " *Org tags*")
  13485. (org-switch-to-buffer-other-window " *Org tags*")
  13486. (org-fit-window-to-buffer)))
  13487. ((or (= c ?\C-g)
  13488. (and (= c ?q) (not (rassoc c ntable))))
  13489. (org-detach-overlay org-tags-overlay)
  13490. (setq quit-flag t))
  13491. ((= c ?\ )
  13492. (setq current nil)
  13493. (if exit-after-next (setq exit-after-next 'now)))
  13494. ((= c ?\t)
  13495. (condition-case nil
  13496. (setq tg (org-icompleting-read
  13497. "Tag: "
  13498. (or buffer-tags
  13499. (with-current-buffer buf
  13500. (org-get-buffer-tags)))))
  13501. (quit (setq tg "")))
  13502. (when (string-match "\\S-" tg)
  13503. (add-to-list 'buffer-tags (list tg))
  13504. (if (member tg current)
  13505. (setq current (delete tg current))
  13506. (push tg current)))
  13507. (if exit-after-next (setq exit-after-next 'now)))
  13508. ((setq e (rassoc c todo-table) tg (car e))
  13509. (with-current-buffer buf
  13510. (save-excursion (org-todo tg)))
  13511. (if exit-after-next (setq exit-after-next 'now)))
  13512. ((setq e (rassoc c ntable) tg (car e))
  13513. (if (member tg current)
  13514. (setq current (delete tg current))
  13515. (loop for g in groups do
  13516. (if (member tg g)
  13517. (mapc (lambda (x)
  13518. (setq current (delete x current)))
  13519. g)))
  13520. (push tg current))
  13521. (if exit-after-next (setq exit-after-next 'now))))
  13522. ;; Create a sorted list
  13523. (setq current
  13524. (sort current
  13525. (lambda (a b)
  13526. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13527. (if (eq exit-after-next 'now) (throw 'exit t))
  13528. (goto-char (point-min))
  13529. (beginning-of-line 2)
  13530. (delete-region (point) (point-at-eol))
  13531. (org-fast-tag-insert "Current" current c-face)
  13532. (org-set-current-tags-overlay current ov-prefix)
  13533. (while (re-search-forward
  13534. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13535. (setq tg (match-string 1))
  13536. (add-text-properties
  13537. (match-beginning 1) (match-end 1)
  13538. (list 'face
  13539. (cond
  13540. ((member tg current) c-face)
  13541. ((member tg inherited) i-face)
  13542. (t (get-text-property (match-beginning 1) 'face))))))
  13543. (goto-char (point-min)))))
  13544. (org-detach-overlay org-tags-overlay)
  13545. (if rtn
  13546. (mapconcat 'identity current ":")
  13547. nil))))
  13548. (defun org-get-tags-string ()
  13549. "Get the TAGS string in the current headline."
  13550. (unless (org-at-heading-p t)
  13551. (user-error "Not on a heading"))
  13552. (save-excursion
  13553. (beginning-of-line 1)
  13554. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13555. (org-match-string-no-properties 1)
  13556. "")))
  13557. (defun org-get-tags ()
  13558. "Get the list of tags specified in the current headline."
  13559. (org-split-string (org-get-tags-string) ":"))
  13560. (defun org-get-buffer-tags ()
  13561. "Get a table of all tags used in the buffer, for completion."
  13562. (let (tags)
  13563. (save-excursion
  13564. (goto-char (point-min))
  13565. (while (re-search-forward
  13566. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13567. (when (equal (char-after (point-at-bol 0)) ?*)
  13568. (mapc (lambda (x) (add-to-list 'tags x))
  13569. (org-split-string (org-match-string-no-properties 1) ":")))))
  13570. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13571. (mapcar 'list tags)))
  13572. ;;;; The mapping API
  13573. (defun org-map-entries (func &optional match scope &rest skip)
  13574. "Call FUNC at each headline selected by MATCH in SCOPE.
  13575. FUNC is a function or a lisp form. The function will be called without
  13576. arguments, with the cursor positioned at the beginning of the headline.
  13577. The return values of all calls to the function will be collected and
  13578. returned as a list.
  13579. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13580. does not need to preserve point. After evaluation, the cursor will be
  13581. moved to the end of the line (presumably of the headline of the
  13582. processed entry) and search continues from there. Under some
  13583. circumstances, this may not produce the wanted results. For example,
  13584. if you have removed (e.g. archived) the current (sub)tree it could
  13585. mean that the next entry will be skipped entirely. In such cases, you
  13586. can specify the position from where search should continue by making
  13587. FUNC set the variable `org-map-continue-from' to the desired buffer
  13588. position.
  13589. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13590. Only headlines that are matched by this query will be considered during
  13591. the iteration. When MATCH is nil or t, all headlines will be
  13592. visited by the iteration.
  13593. SCOPE determines the scope of this command. It can be any of:
  13594. nil The current buffer, respecting the restriction if any
  13595. tree The subtree started with the entry at point
  13596. region The entries within the active region, if any
  13597. region-start-level
  13598. The entries within the active region, but only those at
  13599. the same level than the first one.
  13600. file The current buffer, without restriction
  13601. file-with-archives
  13602. The current buffer, and any archives associated with it
  13603. agenda All agenda files
  13604. agenda-with-archives
  13605. All agenda files with any archive files associated with them
  13606. \(file1 file2 ...)
  13607. If this is a list, all files in the list will be scanned
  13608. The remaining args are treated as settings for the skipping facilities of
  13609. the scanner. The following items can be given here:
  13610. archive skip trees with the archive tag
  13611. comment skip trees with the COMMENT keyword
  13612. function or Emacs Lisp form:
  13613. will be used as value for `org-agenda-skip-function', so
  13614. whenever the function returns a position, FUNC will not be
  13615. called for that entry and search will continue from the
  13616. position returned
  13617. If your function needs to retrieve the tags including inherited tags
  13618. at the *current* entry, you can use the value of the variable
  13619. `org-scanner-tags' which will be much faster than getting the value
  13620. with `org-get-tags-at'. If your function gets properties with
  13621. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13622. to t around the call to `org-entry-properties' to get the same speedup.
  13623. Note that if your function moves around to retrieve tags and properties at
  13624. a *different* entry, you cannot use these techniques."
  13625. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13626. (not (org-region-active-p)))
  13627. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13628. (org-agenda-skip-archived-trees (memq 'archive skip))
  13629. (org-agenda-skip-comment-trees (memq 'comment skip))
  13630. (org-agenda-skip-function
  13631. (car (org-delete-all '(comment archive) skip)))
  13632. (org-tags-match-list-sublevels t)
  13633. (start-level (eq scope 'region-start-level))
  13634. matcher file res
  13635. org-todo-keywords-for-agenda
  13636. org-done-keywords-for-agenda
  13637. org-todo-keyword-alist-for-agenda
  13638. org-drawers-for-agenda
  13639. org-tag-alist-for-agenda
  13640. todo-only)
  13641. (cond
  13642. ((eq match t) (setq matcher t))
  13643. ((eq match nil) (setq matcher t))
  13644. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13645. (save-window-excursion
  13646. (save-restriction
  13647. (cond ((eq scope 'tree)
  13648. (org-back-to-heading t)
  13649. (org-narrow-to-subtree)
  13650. (setq scope nil))
  13651. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13652. (org-region-active-p))
  13653. ;; If needed, set start-level to a string like "2"
  13654. (when start-level
  13655. (save-excursion
  13656. (goto-char (region-beginning))
  13657. (unless (org-at-heading-p) (outline-next-heading))
  13658. (setq start-level (org-current-level))))
  13659. (narrow-to-region (region-beginning)
  13660. (save-excursion
  13661. (goto-char (region-end))
  13662. (unless (and (bolp) (org-at-heading-p))
  13663. (outline-next-heading))
  13664. (point)))
  13665. (setq scope nil)))
  13666. (if (not scope)
  13667. (progn
  13668. (org-agenda-prepare-buffers
  13669. (list (buffer-file-name (current-buffer))))
  13670. (setq res (org-scan-tags func matcher todo-only start-level)))
  13671. ;; Get the right scope
  13672. (cond
  13673. ((and scope (listp scope) (symbolp (car scope)))
  13674. (setq scope (eval scope)))
  13675. ((eq scope 'agenda)
  13676. (setq scope (org-agenda-files t)))
  13677. ((eq scope 'agenda-with-archives)
  13678. (setq scope (org-agenda-files t))
  13679. (setq scope (org-add-archive-files scope)))
  13680. ((eq scope 'file)
  13681. (setq scope (list (buffer-file-name))))
  13682. ((eq scope 'file-with-archives)
  13683. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13684. (org-agenda-prepare-buffers scope)
  13685. (while (setq file (pop scope))
  13686. (with-current-buffer (org-find-base-buffer-visiting file)
  13687. (save-excursion
  13688. (save-restriction
  13689. (widen)
  13690. (goto-char (point-min))
  13691. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13692. res)))
  13693. ;;;; Properties
  13694. ;;; Setting and retrieving properties
  13695. (defconst org-special-properties
  13696. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13697. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13698. "The special properties valid in Org-mode.
  13699. These are properties that are not defined in the property drawer,
  13700. but in some other way.")
  13701. (defconst org-default-properties
  13702. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13703. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13704. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13705. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13706. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13707. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13708. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13709. "Some properties that are used by Org-mode for various purposes.
  13710. Being in this list makes sure that they are offered for completion.")
  13711. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13712. "Regular expression matching the first line of a property drawer.")
  13713. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13714. "Regular expression matching the last line of a property drawer.")
  13715. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13716. "Regular expression matching the first line of a property drawer.")
  13717. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13718. "Regular expression matching the first line of a property drawer.")
  13719. (defconst org-property-drawer-re
  13720. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13721. org-property-end-re "\\)\n?")
  13722. "Matches an entire property drawer.")
  13723. (defconst org-clock-drawer-re
  13724. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13725. org-property-end-re "\\)\n?")
  13726. "Matches an entire clock drawer.")
  13727. (defsubst org-re-property (property)
  13728. "Return a regexp matching a PROPERTY line.
  13729. Match group 1 will be set to the value."
  13730. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13731. (defsubst org-re-property-keyword (property)
  13732. "Return a regexp matching a PROPERTY line, possibly with no
  13733. value for the property."
  13734. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13735. (defun org-property-action ()
  13736. "Do an action on properties."
  13737. (interactive)
  13738. (let (c)
  13739. (org-at-property-p)
  13740. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13741. (setq c (read-char-exclusive))
  13742. (cond
  13743. ((equal c ?s)
  13744. (call-interactively 'org-set-property))
  13745. ((equal c ?d)
  13746. (call-interactively 'org-delete-property))
  13747. ((equal c ?D)
  13748. (call-interactively 'org-delete-property-globally))
  13749. ((equal c ?c)
  13750. (call-interactively 'org-compute-property-at-point))
  13751. (t (user-error "No such property action %c" c)))))
  13752. (defun org-inc-effort ()
  13753. "Increment the value of the effort property in the current entry."
  13754. (interactive)
  13755. (org-set-effort nil t))
  13756. (defvar org-clock-effort) ;; Defined in org-clock.el
  13757. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13758. (defun org-set-effort (&optional value increment)
  13759. "Set the effort property of the current entry.
  13760. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13761. 10th allowed value.
  13762. When INCREMENT is non-nil, set the property to the next allowed value."
  13763. (interactive "P")
  13764. (if (equal value 0) (setq value 10))
  13765. (let* ((completion-ignore-case t)
  13766. (prop org-effort-property)
  13767. (cur (org-entry-get nil prop))
  13768. (allowed (org-property-get-allowed-values nil prop 'table))
  13769. (existing (mapcar 'list (org-property-values prop)))
  13770. (heading (nth 4 (org-heading-components)))
  13771. rpl
  13772. (val (cond
  13773. ((stringp value) value)
  13774. ((and allowed (integerp value))
  13775. (or (car (nth (1- value) allowed))
  13776. (car (org-last allowed))))
  13777. ((and allowed increment)
  13778. (or (caadr (member (list cur) allowed))
  13779. (user-error "Allowed effort values are not set")))
  13780. (allowed
  13781. (message "Select 1-9,0, [RET%s]: %s"
  13782. (if cur (concat "=" cur) "")
  13783. (mapconcat 'car allowed " "))
  13784. (setq rpl (read-char-exclusive))
  13785. (if (equal rpl ?\r)
  13786. cur
  13787. (setq rpl (- rpl ?0))
  13788. (if (equal rpl 0) (setq rpl 10))
  13789. (if (and (> rpl 0) (<= rpl (length allowed)))
  13790. (car (nth (1- rpl) allowed))
  13791. (org-completing-read "Effort: " allowed nil))))
  13792. (t
  13793. (let (org-completion-use-ido org-completion-use-iswitchb)
  13794. (org-completing-read
  13795. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13796. (concat "[" cur "]") "")
  13797. ": ")
  13798. existing nil nil "" nil cur))))))
  13799. (unless (equal (org-entry-get nil prop) val)
  13800. (org-entry-put nil prop val))
  13801. (save-excursion
  13802. (org-back-to-heading t)
  13803. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13804. (when (string= heading org-clock-current-task)
  13805. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13806. (org-clock-update-mode-line))
  13807. (message "%s is now %s" prop val)))
  13808. (defun org-at-property-p ()
  13809. "Is cursor inside a property drawer?"
  13810. (save-excursion
  13811. (beginning-of-line 1)
  13812. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13813. (save-match-data ;; Used by calling procedures
  13814. (let ((p (point))
  13815. (range (unless (org-before-first-heading-p)
  13816. (org-get-property-block))))
  13817. (and range (<= (car range) p) (< p (cdr range))))))))
  13818. (defun org-get-property-block (&optional beg end force)
  13819. "Return the (beg . end) range of the body of the property drawer.
  13820. BEG and END are the beginning and end of the current subtree, or of
  13821. the part before the first headline. If they are not given, they will
  13822. be found. If the drawer does not exist and FORCE is non-nil, create
  13823. the drawer."
  13824. (catch 'exit
  13825. (save-excursion
  13826. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13827. (progn (org-back-to-heading t) (point))))
  13828. (end (or end (and (not (outline-next-heading)) (point-max))
  13829. (point))))
  13830. (goto-char beg)
  13831. (if (re-search-forward org-property-start-re end t)
  13832. (setq beg (1+ (match-end 0)))
  13833. (if force
  13834. (save-excursion
  13835. (org-insert-property-drawer)
  13836. (setq end (progn (outline-next-heading) (point))))
  13837. (throw 'exit nil))
  13838. (goto-char beg)
  13839. (if (re-search-forward org-property-start-re end t)
  13840. (setq beg (1+ (match-end 0)))))
  13841. (if (re-search-forward org-property-end-re end t)
  13842. (setq end (match-beginning 0))
  13843. (or force (throw 'exit nil))
  13844. (goto-char beg)
  13845. (setq end beg)
  13846. (org-indent-line)
  13847. (insert ":END:\n"))
  13848. (cons beg end)))))
  13849. (defun org-entry-properties (&optional pom which specific)
  13850. "Get all properties of the entry at point-or-marker POM.
  13851. This includes the TODO keyword, the tags, time strings for deadline,
  13852. scheduled, and clocking, and any additional properties defined in the
  13853. entry. The return value is an alist, keys may occur multiple times
  13854. if the property key was used several times.
  13855. POM may also be nil, in which case the current entry is used.
  13856. If WHICH is nil or `all', get all properties. If WHICH is
  13857. `special' or `standard', only get that subclass. If WHICH
  13858. is a string only get exactly this property. SPECIFIC can be a string, the
  13859. specific property we are interested in. Specifying it can speed
  13860. things up because then unnecessary parsing is avoided."
  13861. (setq which (or which 'all))
  13862. (org-with-point-at pom
  13863. (let ((clockstr (substring org-clock-string 0 -1))
  13864. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13865. (case-fold-search nil)
  13866. beg end range props sum-props key key1 value string clocksum clocksumt)
  13867. (save-excursion
  13868. (when (condition-case nil
  13869. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13870. (error nil))
  13871. (setq beg (point))
  13872. (setq sum-props (get-text-property (point) 'org-summaries))
  13873. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13874. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13875. (outline-next-heading)
  13876. (setq end (point))
  13877. (when (memq which '(all special))
  13878. ;; Get the special properties, like TODO and tags
  13879. (goto-char beg)
  13880. (when (and (or (not specific) (string= specific "TODO"))
  13881. (looking-at org-todo-line-regexp) (match-end 2))
  13882. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13883. (when (and (or (not specific) (string= specific "PRIORITY"))
  13884. (looking-at org-priority-regexp))
  13885. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13886. (when (or (not specific) (string= specific "FILE"))
  13887. (push (cons "FILE" buffer-file-name) props))
  13888. (when (and (or (not specific) (string= specific "TAGS"))
  13889. (setq value (org-get-tags-string))
  13890. (string-match "\\S-" value))
  13891. (push (cons "TAGS" value) props))
  13892. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13893. (setq value (org-get-tags-at)))
  13894. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13895. ":"))
  13896. props))
  13897. (when (or (not specific) (string= specific "BLOCKED"))
  13898. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13899. (when (or (not specific)
  13900. (member specific
  13901. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13902. "TIMESTAMP" "TIMESTAMP_IA")))
  13903. (catch 'match
  13904. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13905. (setq key (if (match-end 1)
  13906. (substring (org-match-string-no-properties 1)
  13907. 0 -1))
  13908. string (if (equal key clockstr)
  13909. (org-trim
  13910. (buffer-substring-no-properties
  13911. (match-beginning 3) (goto-char
  13912. (point-at-eol))))
  13913. (substring (org-match-string-no-properties 3)
  13914. 1 -1)))
  13915. ;; Get the correct property name from the key. This is
  13916. ;; necessary if the user has configured time keywords.
  13917. (setq key1 (concat key ":"))
  13918. (cond
  13919. ((not key)
  13920. (setq key
  13921. (if (= (char-after (match-beginning 3)) ?\[)
  13922. "TIMESTAMP_IA" "TIMESTAMP")))
  13923. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13924. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13925. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13926. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13927. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13928. (progn
  13929. (push (cons key string) props)
  13930. ;; no need to search further if match is found
  13931. (throw 'match t))
  13932. (when (or (equal key "CLOCK") (not (assoc key props)))
  13933. (push (cons key string) props)))))))
  13934. (when (memq which '(all standard))
  13935. ;; Get the standard properties, like :PROP: ...
  13936. (setq range (org-get-property-block beg end))
  13937. (when range
  13938. (goto-char (car range))
  13939. (while (re-search-forward
  13940. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13941. (cdr range) t)
  13942. (setq key (org-match-string-no-properties 1)
  13943. value (org-trim (or (org-match-string-no-properties 2) "")))
  13944. (unless (member key excluded)
  13945. (push (cons key (or value "")) props)))))
  13946. (if clocksum
  13947. (push (cons "CLOCKSUM"
  13948. (org-columns-number-to-string (/ (float clocksum) 60.)
  13949. 'add_times))
  13950. props))
  13951. (if clocksumt
  13952. (push (cons "CLOCKSUM_T"
  13953. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13954. 'add_times))
  13955. props))
  13956. (unless (assoc "CATEGORY" props)
  13957. (push (cons "CATEGORY" (org-get-category)) props))
  13958. (append sum-props (nreverse props)))))))
  13959. (defun org-entry-get (pom property &optional inherit literal-nil)
  13960. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13961. If INHERIT is non-nil and the entry does not have the property,
  13962. then also check higher levels of the hierarchy.
  13963. If INHERIT is the symbol `selective', use inheritance only if the setting
  13964. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13965. If the property is present but empty, the return value is the empty string.
  13966. If the property is not present at all, nil is returned.
  13967. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13968. do not interpret it as the list atom nil. This is used for inheritance
  13969. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13970. (org-with-point-at pom
  13971. (if (and inherit (if (eq inherit 'selective)
  13972. (org-property-inherit-p property)
  13973. t))
  13974. (org-entry-get-with-inheritance property literal-nil)
  13975. (if (member property org-special-properties)
  13976. ;; We need a special property. Use `org-entry-properties' to
  13977. ;; retrieve it, but specify the wanted property
  13978. (cdr (assoc property (org-entry-properties nil 'special property)))
  13979. (let ((range (org-get-property-block)))
  13980. (when (and range (not (eq (car range) (cdr range))))
  13981. (let* ((props (list (or (assoc property org-file-properties)
  13982. (assoc property org-global-properties)
  13983. (assoc property org-global-properties-fixed))))
  13984. (ap (lambda (key)
  13985. (when (re-search-forward
  13986. (org-re-property key) (cdr range) t)
  13987. (setq props
  13988. (org-update-property-plist
  13989. key
  13990. (if (match-end 1)
  13991. (org-match-string-no-properties 1) "")
  13992. props)))))
  13993. val)
  13994. (goto-char (car range))
  13995. (funcall ap property)
  13996. (goto-char (car range))
  13997. (while (funcall ap (concat property "+")))
  13998. (setq val (cdr (assoc property props)))
  13999. (when val (if literal-nil val (org-not-nil val))))))))))
  14000. (defun org-property-or-variable-value (var &optional inherit)
  14001. "Check if there is a property fixing the value of VAR.
  14002. If yes, return this value. If not, return the current value of the variable."
  14003. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14004. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14005. (read prop)
  14006. (symbol-value var))))
  14007. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  14008. "Delete the property PROPERTY from entry at point-or-marker POM.
  14009. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14010. an empty drawer to delete."
  14011. (org-with-point-at pom
  14012. (if (member property org-special-properties)
  14013. nil ; cannot delete these properties.
  14014. (let ((range (org-get-property-block)))
  14015. (if (and range
  14016. (goto-char (car range))
  14017. (re-search-forward
  14018. (org-re-property property)
  14019. (cdr range) t))
  14020. (progn
  14021. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14022. (and delete-empty-drawer
  14023. (org-remove-empty-drawer-at
  14024. delete-empty-drawer (car range)))
  14025. t)
  14026. nil)))))
  14027. ;; Multi-values properties are properties that contain multiple values
  14028. ;; These values are assumed to be single words, separated by whitespace.
  14029. (defun org-entry-add-to-multivalued-property (pom property value)
  14030. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14031. (let* ((old (org-entry-get pom property))
  14032. (values (and old (org-split-string old "[ \t]"))))
  14033. (setq value (org-entry-protect-space value))
  14034. (unless (member value values)
  14035. (setq values (append values (list value)))
  14036. (org-entry-put pom property
  14037. (mapconcat 'identity values " ")))))
  14038. (defun org-entry-remove-from-multivalued-property (pom property value)
  14039. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14040. (let* ((old (org-entry-get pom property))
  14041. (values (and old (org-split-string old "[ \t]"))))
  14042. (setq value (org-entry-protect-space value))
  14043. (when (member value values)
  14044. (setq values (delete value values))
  14045. (org-entry-put pom property
  14046. (mapconcat 'identity values " ")))))
  14047. (defun org-entry-member-in-multivalued-property (pom property value)
  14048. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14049. (let* ((old (org-entry-get pom property))
  14050. (values (and old (org-split-string old "[ \t]"))))
  14051. (setq value (org-entry-protect-space value))
  14052. (member value values)))
  14053. (defun org-entry-get-multivalued-property (pom property)
  14054. "Return a list of values in a multivalued property."
  14055. (let* ((value (org-entry-get pom property))
  14056. (values (and value (org-split-string value "[ \t]"))))
  14057. (mapcar 'org-entry-restore-space values)))
  14058. (defun org-entry-put-multivalued-property (pom property &rest values)
  14059. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14060. VALUES should be a list of strings. Spaces will be protected."
  14061. (org-entry-put pom property
  14062. (mapconcat 'org-entry-protect-space values " "))
  14063. (let* ((value (org-entry-get pom property))
  14064. (values (and value (org-split-string value "[ \t]"))))
  14065. (mapcar 'org-entry-restore-space values)))
  14066. (defun org-entry-protect-space (s)
  14067. "Protect spaces and newline in string S."
  14068. (while (string-match " " s)
  14069. (setq s (replace-match "%20" t t s)))
  14070. (while (string-match "\n" s)
  14071. (setq s (replace-match "%0A" t t s)))
  14072. s)
  14073. (defun org-entry-restore-space (s)
  14074. "Restore spaces and newline in string S."
  14075. (while (string-match "%20" s)
  14076. (setq s (replace-match " " t t s)))
  14077. (while (string-match "%0A" s)
  14078. (setq s (replace-match "\n" t t s)))
  14079. s)
  14080. (defvar org-entry-property-inherited-from (make-marker)
  14081. "Marker pointing to the entry from where a property was inherited.
  14082. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14083. location of the entry where the inheritance search matched. If there was
  14084. no match, the marker will point nowhere.
  14085. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14086. is set.")
  14087. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14088. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14089. The search will stop at the first ancestor which has the property defined.
  14090. If the value found is \"nil\", return nil to show that the property
  14091. should be considered as undefined (this is the meaning of nil here).
  14092. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14093. (move-marker org-entry-property-inherited-from nil)
  14094. (let (tmp)
  14095. (save-excursion
  14096. (save-restriction
  14097. (widen)
  14098. (catch 'ex
  14099. (while t
  14100. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  14101. (or (ignore-errors (org-back-to-heading t))
  14102. (goto-char (point-min)))
  14103. (move-marker org-entry-property-inherited-from (point))
  14104. (throw 'ex tmp))
  14105. (or (ignore-errors (org-up-heading-safe))
  14106. (throw 'ex nil))))))
  14107. (setq tmp (or tmp
  14108. (cdr (assoc property org-file-properties))
  14109. (cdr (assoc property org-global-properties))
  14110. (cdr (assoc property org-global-properties-fixed))))
  14111. (if literal-nil tmp (org-not-nil tmp))))
  14112. (defvar org-property-changed-functions nil
  14113. "Hook called when the value of a property has changed.
  14114. Each hook function should accept two arguments, the name of the property
  14115. and the new value.")
  14116. (defun org-entry-put (pom property value)
  14117. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14118. (org-with-point-at pom
  14119. (org-back-to-heading t)
  14120. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14121. range)
  14122. (cond
  14123. ((equal property "TODO")
  14124. (when (and (stringp value) (string-match "\\S-" value)
  14125. (not (member value org-todo-keywords-1)))
  14126. (user-error "\"%s\" is not a valid TODO state" value))
  14127. (if (or (not value)
  14128. (not (string-match "\\S-" value)))
  14129. (setq value 'none))
  14130. (org-todo value)
  14131. (org-set-tags nil 'align))
  14132. ((equal property "PRIORITY")
  14133. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14134. (string-to-char value) ?\ ))
  14135. (org-set-tags nil 'align))
  14136. ((equal property "CLOCKSUM")
  14137. (if (not (re-search-forward
  14138. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14139. (error "Cannot find a clock log")
  14140. (goto-char (- (match-end 1) 2))
  14141. (cond
  14142. ((eq value 'earlier) (org-timestamp-down))
  14143. ((eq value 'later) (org-timestamp-up)))
  14144. (org-clock-sum-current-item)))
  14145. ((equal property "SCHEDULED")
  14146. (if (re-search-forward org-scheduled-time-regexp end t)
  14147. (cond
  14148. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14149. ((eq value 'later) (org-timestamp-change 1 'day))
  14150. (t (call-interactively 'org-schedule)))
  14151. (call-interactively 'org-schedule)))
  14152. ((equal property "DEADLINE")
  14153. (if (re-search-forward org-deadline-time-regexp end t)
  14154. (cond
  14155. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14156. ((eq value 'later) (org-timestamp-change 1 'day))
  14157. (t (call-interactively 'org-deadline)))
  14158. (call-interactively 'org-deadline)))
  14159. ((member property org-special-properties)
  14160. (error "The %s property can not yet be set with `org-entry-put'"
  14161. property))
  14162. (t ; a non-special property
  14163. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14164. (setq range (org-get-property-block beg end 'force))
  14165. (goto-char (car range))
  14166. (if (re-search-forward
  14167. (org-re-property-keyword property) (cdr range) t)
  14168. (progn
  14169. (delete-region (match-beginning 0) (match-end 0))
  14170. (goto-char (match-beginning 0)))
  14171. (goto-char (cdr range))
  14172. (insert "\n")
  14173. (backward-char 1)
  14174. (org-indent-line))
  14175. (insert ":" property ":")
  14176. (and value (insert " " value))
  14177. (org-indent-line)))))
  14178. (run-hook-with-args 'org-property-changed-functions property value)))
  14179. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14180. "Get all property keys in the current buffer.
  14181. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14182. like tags and TODO state.
  14183. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14184. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14185. and others.
  14186. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14187. formats in the current buffer."
  14188. (let (rtn range cfmt s p)
  14189. (save-excursion
  14190. (save-restriction
  14191. (widen)
  14192. (goto-char (point-min))
  14193. (while (re-search-forward org-property-start-re nil t)
  14194. (setq range (org-get-property-block))
  14195. (goto-char (car range))
  14196. (while (re-search-forward
  14197. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14198. (cdr range) t)
  14199. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14200. (outline-next-heading))))
  14201. (when include-specials
  14202. (setq rtn (append org-special-properties rtn)))
  14203. (when include-defaults
  14204. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14205. (add-to-list 'rtn org-effort-property))
  14206. (when include-columns
  14207. (save-excursion
  14208. (save-restriction
  14209. (widen)
  14210. (goto-char (point-min))
  14211. (while (re-search-forward
  14212. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14213. nil t)
  14214. (setq cfmt (match-string 2) s 0)
  14215. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14216. cfmt s)
  14217. (setq s (match-end 0)
  14218. p (match-string 1 cfmt))
  14219. (unless (or (equal p "ITEM")
  14220. (member p org-special-properties))
  14221. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14222. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14223. (defun org-property-values (key)
  14224. "Return a list of all values of property KEY in the current buffer."
  14225. (save-excursion
  14226. (save-restriction
  14227. (widen)
  14228. (goto-char (point-min))
  14229. (let ((re (org-re-property key))
  14230. values)
  14231. (while (re-search-forward re nil t)
  14232. (add-to-list 'values (org-trim (match-string 1))))
  14233. (delete "" values)))))
  14234. (defun org-insert-property-drawer ()
  14235. "Insert a property drawer into the current entry."
  14236. (org-back-to-heading t)
  14237. (looking-at org-outline-regexp)
  14238. (let ((indent (if org-adapt-indentation
  14239. (- (match-end 0) (match-beginning 0))
  14240. 0))
  14241. (beg (point))
  14242. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14243. end hiddenp)
  14244. (outline-next-heading)
  14245. (setq end (point))
  14246. (goto-char beg)
  14247. (while (re-search-forward re end t))
  14248. (setq hiddenp (outline-invisible-p))
  14249. (end-of-line 1)
  14250. (and (equal (char-after) ?\n) (forward-char 1))
  14251. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14252. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14253. ;; just skip this line
  14254. (beginning-of-line 2)
  14255. ;; Drawer start, find the end
  14256. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14257. (beginning-of-line 1)))
  14258. (org-skip-over-state-notes)
  14259. (skip-chars-backward " \t\n\r")
  14260. (if (eq (char-before) ?*) (forward-char 1))
  14261. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14262. (beginning-of-line 0)
  14263. (org-indent-to-column indent)
  14264. (beginning-of-line 2)
  14265. (org-indent-to-column indent)
  14266. (beginning-of-line 0)
  14267. (if hiddenp
  14268. (save-excursion
  14269. (org-back-to-heading t)
  14270. (hide-entry))
  14271. (org-flag-drawer t))))
  14272. (defun org-insert-drawer (&optional arg drawer)
  14273. "Insert a drawer at point.
  14274. Optional argument DRAWER, when non-nil, is a string representing
  14275. drawer's name. Otherwise, the user is prompted for a name.
  14276. If a region is active, insert the drawer around that region
  14277. instead.
  14278. Point is left between drawer's boundaries."
  14279. (interactive "P")
  14280. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14281. "LOGBOOK"))
  14282. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14283. ;; internally by Org. They are meant to be inserted
  14284. ;; automatically.
  14285. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14286. ;; Remove system drawers from list. Note: For some reason,
  14287. ;; `org-completing-read' ignores the predicate while
  14288. ;; `completing-read' handles it fine.
  14289. (drawer (if arg "PROPERTIES"
  14290. (or drawer
  14291. (completing-read
  14292. "Drawer: " org-drawers
  14293. (lambda (d) (not (member d system-drawers))))))))
  14294. (cond
  14295. ;; With C-u, fall back on `org-insert-property-drawer'
  14296. (arg (org-insert-property-drawer))
  14297. ;; With an active region, insert a drawer at point.
  14298. ((not (org-region-active-p))
  14299. (progn
  14300. (unless (bolp) (insert "\n"))
  14301. (insert (format ":%s:\n\n:END:\n" drawer))
  14302. (forward-line -2)))
  14303. ;; Otherwise, insert the drawer at point
  14304. (t
  14305. (let ((rbeg (region-beginning))
  14306. (rend (copy-marker (region-end))))
  14307. (unwind-protect
  14308. (progn
  14309. (goto-char rbeg)
  14310. (beginning-of-line)
  14311. (when (save-excursion
  14312. (re-search-forward org-outline-regexp-bol rend t))
  14313. (user-error "Drawers cannot contain headlines"))
  14314. ;; Position point at the beginning of the first
  14315. ;; non-blank line in region. Insert drawer's opening
  14316. ;; there, then indent it.
  14317. (org-skip-whitespace)
  14318. (beginning-of-line)
  14319. (insert ":" drawer ":\n")
  14320. (forward-line -1)
  14321. (indent-for-tab-command)
  14322. ;; Move point to the beginning of the first blank line
  14323. ;; after the last non-blank line in region. Insert
  14324. ;; drawer's closing, then indent it.
  14325. (goto-char rend)
  14326. (skip-chars-backward " \r\t\n")
  14327. (insert "\n:END:")
  14328. (deactivate-mark t)
  14329. (indent-for-tab-command)
  14330. (unless (eolp) (insert "\n")))
  14331. ;; Clear marker, whatever the outcome of insertion is.
  14332. (set-marker rend nil)))))))
  14333. (defvar org-property-set-functions-alist nil
  14334. "Property set function alist.
  14335. Each entry should have the following format:
  14336. (PROPERTY . READ-FUNCTION)
  14337. The read function will be called with the same argument as
  14338. `org-completing-read'.")
  14339. (defun org-set-property-function (property)
  14340. "Get the function that should be used to set PROPERTY.
  14341. This is computed according to `org-property-set-functions-alist'."
  14342. (or (cdr (assoc property org-property-set-functions-alist))
  14343. 'org-completing-read))
  14344. (defun org-read-property-value (property)
  14345. "Read PROPERTY value from user."
  14346. (let* ((completion-ignore-case t)
  14347. (allowed (org-property-get-allowed-values nil property 'table))
  14348. (cur (org-entry-get nil property))
  14349. (prompt (concat property " value"
  14350. (if (and cur (string-match "\\S-" cur))
  14351. (concat " [" cur "]") "") ": "))
  14352. (set-function (org-set-property-function property))
  14353. (val (if allowed
  14354. (funcall set-function prompt allowed nil
  14355. (not (get-text-property 0 'org-unrestricted
  14356. (caar allowed))))
  14357. (let (org-completion-use-ido org-completion-use-iswitchb)
  14358. (funcall set-function prompt
  14359. (mapcar 'list (org-property-values property))
  14360. nil nil "" nil cur)))))
  14361. (if (equal val "")
  14362. cur
  14363. val)))
  14364. (defvar org-last-set-property nil)
  14365. (defvar org-last-set-property-value nil)
  14366. (defun org-read-property-name ()
  14367. "Read a property name."
  14368. (let* ((completion-ignore-case t)
  14369. (keys (org-buffer-property-keys nil t t))
  14370. (default-prop (or (save-excursion
  14371. (save-match-data
  14372. (beginning-of-line)
  14373. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14374. (null (string= (match-string 1) "END"))
  14375. (match-string 1))))
  14376. org-last-set-property))
  14377. (property (org-icompleting-read
  14378. (concat "Property"
  14379. (if default-prop (concat " [" default-prop "]") "")
  14380. ": ")
  14381. (mapcar 'list keys)
  14382. nil nil nil nil
  14383. default-prop)))
  14384. (if (member property keys)
  14385. property
  14386. (or (cdr (assoc (downcase property)
  14387. (mapcar (lambda (x) (cons (downcase x) x))
  14388. keys)))
  14389. property))))
  14390. (defun org-set-property-and-value (use-last)
  14391. "Allow to set [PROPERTY]: [value] direction from prompt.
  14392. When use-default, don't even ask, just use the last
  14393. \"[PROPERTY]: [value]\" string from the history."
  14394. (interactive "P")
  14395. (let* ((completion-ignore-case t)
  14396. (pv (or (and use-last org-last-set-property-value)
  14397. (org-completing-read
  14398. "Enter a \"[Property]: [value]\" pair: "
  14399. nil nil nil nil nil
  14400. org-last-set-property-value)))
  14401. prop val)
  14402. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14403. (setq prop (match-string 1 pv)
  14404. val (match-string 2 pv))
  14405. (org-set-property prop val))))
  14406. (defun org-set-property (property value)
  14407. "In the current entry, set PROPERTY to VALUE.
  14408. When called interactively, this will prompt for a property name, offering
  14409. completion on existing and default properties. And then it will prompt
  14410. for a value, offering completion either on allowed values (via an inherited
  14411. xxx_ALL property) or on existing values in other instances of this property
  14412. in the current file."
  14413. (interactive (list nil nil))
  14414. (let* ((property (or property (org-read-property-name)))
  14415. (value (or value (org-read-property-value property)))
  14416. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14417. (setq org-last-set-property property)
  14418. (setq org-last-set-property-value (concat property ": " value))
  14419. ;; Possibly postprocess the inserted value:
  14420. (when fn (setq value (funcall fn value)))
  14421. (unless (equal (org-entry-get nil property) value)
  14422. (org-entry-put nil property value))))
  14423. (defun org-delete-property (property &optional delete-empty-drawer)
  14424. "In the current entry, delete PROPERTY.
  14425. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14426. an empty drawer to delete."
  14427. (interactive
  14428. (let* ((completion-ignore-case t)
  14429. (prop (org-icompleting-read "Property: "
  14430. (org-entry-properties nil 'standard))))
  14431. (list prop)))
  14432. (message "Property %s %s" property
  14433. (if (org-entry-delete nil property delete-empty-drawer)
  14434. "deleted"
  14435. "was not present in the entry")))
  14436. (defun org-delete-property-globally (property)
  14437. "Remove PROPERTY globally, from all entries."
  14438. (interactive
  14439. (let* ((completion-ignore-case t)
  14440. (prop (org-icompleting-read
  14441. "Globally remove property: "
  14442. (mapcar 'list (org-buffer-property-keys)))))
  14443. (list prop)))
  14444. (save-excursion
  14445. (save-restriction
  14446. (widen)
  14447. (goto-char (point-min))
  14448. (let ((cnt 0))
  14449. (while (re-search-forward
  14450. (org-re-property property)
  14451. nil t)
  14452. (setq cnt (1+ cnt))
  14453. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14454. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14455. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14456. (defun org-compute-property-at-point ()
  14457. "Compute the property at point.
  14458. This looks for an enclosing column format, extracts the operator and
  14459. then applies it to the property in the column format's scope."
  14460. (interactive)
  14461. (unless (org-at-property-p)
  14462. (user-error "Not at a property"))
  14463. (let ((prop (org-match-string-no-properties 2)))
  14464. (org-columns-get-format-and-top-level)
  14465. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14466. (user-error "No operator defined for property %s" prop))
  14467. (org-columns-compute prop)))
  14468. (defvar org-property-allowed-value-functions nil
  14469. "Hook for functions supplying allowed values for a specific property.
  14470. The functions must take a single argument, the name of the property, and
  14471. return a flat list of allowed values. If \":ETC\" is one of
  14472. the values, this means that these values are intended as defaults for
  14473. completion, but that other values should be allowed too.
  14474. The functions must return nil if they are not responsible for this
  14475. property.")
  14476. (defun org-property-get-allowed-values (pom property &optional table)
  14477. "Get allowed values for the property PROPERTY.
  14478. When TABLE is non-nil, return an alist that can directly be used for
  14479. completion."
  14480. (let (vals)
  14481. (cond
  14482. ((equal property "TODO")
  14483. (setq vals (org-with-point-at pom
  14484. (append org-todo-keywords-1 '("")))))
  14485. ((equal property "PRIORITY")
  14486. (let ((n org-lowest-priority))
  14487. (while (>= n org-highest-priority)
  14488. (push (char-to-string n) vals)
  14489. (setq n (1- n)))))
  14490. ((member property org-special-properties))
  14491. ((setq vals (run-hook-with-args-until-success
  14492. 'org-property-allowed-value-functions property)))
  14493. (t
  14494. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14495. (when (and vals (string-match "\\S-" vals))
  14496. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14497. (setq vals (mapcar (lambda (x)
  14498. (cond ((stringp x) x)
  14499. ((numberp x) (number-to-string x))
  14500. ((symbolp x) (symbol-name x))
  14501. (t "???")))
  14502. vals)))))
  14503. (when (member ":ETC" vals)
  14504. (setq vals (remove ":ETC" vals))
  14505. (org-add-props (car vals) '(org-unrestricted t)))
  14506. (if table (mapcar 'list vals) vals)))
  14507. (defun org-property-previous-allowed-value (&optional previous)
  14508. "Switch to the next allowed value for this property."
  14509. (interactive)
  14510. (org-property-next-allowed-value t))
  14511. (defun org-property-next-allowed-value (&optional previous)
  14512. "Switch to the next allowed value for this property."
  14513. (interactive)
  14514. (unless (org-at-property-p)
  14515. (user-error "Not at a property"))
  14516. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14517. (key (match-string 2))
  14518. (value (match-string 3))
  14519. (allowed (or (org-property-get-allowed-values (point) key)
  14520. (and (member value '("[ ]" "[-]" "[X]"))
  14521. '("[ ]" "[X]"))))
  14522. (heading (save-match-data (nth 4 (org-heading-components))))
  14523. nval)
  14524. (unless allowed
  14525. (user-error "Allowed values for this property have not been defined"))
  14526. (if previous (setq allowed (reverse allowed)))
  14527. (if (member value allowed)
  14528. (setq nval (car (cdr (member value allowed)))))
  14529. (setq nval (or nval (car allowed)))
  14530. (if (equal nval value)
  14531. (user-error "Only one allowed value for this property"))
  14532. (org-at-property-p)
  14533. (replace-match (concat " :" key ": " nval) t t)
  14534. (org-indent-line)
  14535. (beginning-of-line 1)
  14536. (skip-chars-forward " \t")
  14537. (when (equal prop org-effort-property)
  14538. (save-excursion
  14539. (org-back-to-heading t)
  14540. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14541. (when (string= org-clock-current-task heading)
  14542. (setq org-clock-effort nval)
  14543. (org-clock-update-mode-line)))
  14544. (run-hook-with-args 'org-property-changed-functions key nval)))
  14545. (defun org-find-olp (path &optional this-buffer)
  14546. "Return a marker pointing to the entry at outline path OLP.
  14547. If anything goes wrong, throw an error.
  14548. You can wrap this call to catch the error like this:
  14549. (condition-case msg
  14550. (org-mobile-locate-entry (match-string 4))
  14551. (error (nth 1 msg)))
  14552. The return value will then be either a string with the error message,
  14553. or a marker if everything is OK.
  14554. If THIS-BUFFER is set, the outline path does not contain a file,
  14555. only headings."
  14556. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14557. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14558. (level 1)
  14559. (lmin 1)
  14560. (lmax 1)
  14561. limit re end found pos heading cnt flevel)
  14562. (unless buffer (error "File not found :%s" file))
  14563. (with-current-buffer buffer
  14564. (save-excursion
  14565. (save-restriction
  14566. (widen)
  14567. (setq limit (point-max))
  14568. (goto-char (point-min))
  14569. (while (setq heading (pop path))
  14570. (setq re (format org-complex-heading-regexp-format
  14571. (regexp-quote heading)))
  14572. (setq cnt 0 pos (point))
  14573. (while (re-search-forward re end t)
  14574. (setq level (- (match-end 1) (match-beginning 1)))
  14575. (if (and (>= level lmin) (<= level lmax))
  14576. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14577. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14578. lmax heading))
  14579. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14580. lmax heading))
  14581. (goto-char found)
  14582. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14583. (setq end (save-excursion (org-end-of-subtree t t))))
  14584. (when (org-at-heading-p)
  14585. (point-marker)))))))
  14586. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14587. "Find node HEADING in BUFFER.
  14588. Return a marker to the heading if it was found, or nil if not.
  14589. If POS-ONLY is set, return just the position instead of a marker.
  14590. The heading text must match exact, but it may have a TODO keyword,
  14591. a priority cookie and tags in the standard locations."
  14592. (with-current-buffer (or buffer (current-buffer))
  14593. (save-excursion
  14594. (save-restriction
  14595. (widen)
  14596. (goto-char (point-min))
  14597. (let (case-fold-search)
  14598. (if (re-search-forward
  14599. (format org-complex-heading-regexp-format
  14600. (regexp-quote heading)) nil t)
  14601. (if pos-only
  14602. (match-beginning 0)
  14603. (move-marker (make-marker) (match-beginning 0)))))))))
  14604. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14605. "Find Org node headline HEADING in all .org files in directory DIR.
  14606. When the target headline is found, return a marker to this location."
  14607. (let ((files (directory-files (or dir default-directory)
  14608. nil "\\`[^.#].*\\.org\\'"))
  14609. file visiting m buffer)
  14610. (catch 'found
  14611. (while (setq file (pop files))
  14612. (message "trying %s" file)
  14613. (setq visiting (org-find-base-buffer-visiting file))
  14614. (setq buffer (or visiting (find-file-noselect file)))
  14615. (setq m (org-find-exact-headline-in-buffer
  14616. heading buffer))
  14617. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14618. (and m (throw 'found m))))))
  14619. (defun org-find-entry-with-id (ident)
  14620. "Locate the entry that contains the ID property with exact value IDENT.
  14621. IDENT can be a string, a symbol or a number, this function will search for
  14622. the string representation of it.
  14623. Return the position where this entry starts, or nil if there is no such entry."
  14624. (interactive "sID: ")
  14625. (let ((id (cond
  14626. ((stringp ident) ident)
  14627. ((symbol-name ident) (symbol-name ident))
  14628. ((numberp ident) (number-to-string ident))
  14629. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14630. (case-fold-search nil))
  14631. (save-excursion
  14632. (save-restriction
  14633. (widen)
  14634. (goto-char (point-min))
  14635. (when (re-search-forward
  14636. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14637. nil t)
  14638. (org-back-to-heading t)
  14639. (point))))))
  14640. ;;;; Timestamps
  14641. (defvar org-last-changed-timestamp nil)
  14642. (defvar org-last-inserted-timestamp nil
  14643. "The last time stamp inserted with `org-insert-time-stamp'.")
  14644. (defvar org-time-was-given) ; dynamically scoped parameter
  14645. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14646. (defvar org-ts-what) ; dynamically scoped parameter
  14647. (defun org-time-stamp (arg &optional inactive)
  14648. "Prompt for a date/time and insert a time stamp.
  14649. If the user specifies a time like HH:MM or if this command is
  14650. called with at least one prefix argument, the time stamp contains
  14651. the date and the time. Otherwise, only the date is be included.
  14652. All parts of a date not specified by the user is filled in from
  14653. the current date/time. So if you just press return without
  14654. typing anything, the time stamp will represent the current
  14655. date/time.
  14656. If there is already a timestamp at the cursor, it will be
  14657. modified.
  14658. With two universal prefix arguments, insert an active timestamp
  14659. with the current time without prompting the user."
  14660. (interactive "P")
  14661. (let* ((ts nil)
  14662. (default-time
  14663. ;; Default time is either today, or, when entering a range,
  14664. ;; the range start.
  14665. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14666. (save-excursion
  14667. (re-search-backward
  14668. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14669. (- (point) 20) t)))
  14670. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14671. (current-time)))
  14672. (default-input (and ts (org-get-compact-tod ts)))
  14673. (repeater (save-excursion
  14674. (save-match-data
  14675. (beginning-of-line)
  14676. (when (re-search-forward
  14677. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14678. (save-excursion (progn (end-of-line) (point))) t)
  14679. (match-string 0)))))
  14680. org-time-was-given org-end-time-was-given time)
  14681. (cond
  14682. ((and (org-at-timestamp-p t)
  14683. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14684. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14685. (insert "--")
  14686. (setq time (let ((this-command this-command))
  14687. (org-read-date arg 'totime nil nil
  14688. default-time default-input inactive)))
  14689. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14690. ((org-at-timestamp-p t)
  14691. (setq time (let ((this-command this-command))
  14692. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14693. (when (org-at-timestamp-p t) ; just to get the match data
  14694. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14695. (replace-match "")
  14696. (setq org-last-changed-timestamp
  14697. (org-insert-time-stamp
  14698. time (or org-time-was-given arg)
  14699. inactive nil nil (list org-end-time-was-given)))
  14700. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14701. (setq org-last-changed-timestamp
  14702. (concat (substring org-last-inserted-timestamp 0 -1)
  14703. " " repeater ">"))))
  14704. (message "Timestamp updated"))
  14705. ((equal arg '(16))
  14706. (org-insert-time-stamp (current-time) t))
  14707. (t
  14708. (setq time (let ((this-command this-command))
  14709. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14710. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14711. nil nil (list org-end-time-was-given))))))
  14712. ;; FIXME: can we use this for something else, like computing time differences?
  14713. (defun org-get-compact-tod (s)
  14714. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14715. (let* ((t1 (match-string 1 s))
  14716. (h1 (string-to-number (match-string 2 s)))
  14717. (m1 (string-to-number (match-string 3 s)))
  14718. (t2 (and (match-end 4) (match-string 5 s)))
  14719. (h2 (and t2 (string-to-number (match-string 6 s))))
  14720. (m2 (and t2 (string-to-number (match-string 7 s))))
  14721. dh dm)
  14722. (if (not t2)
  14723. t1
  14724. (setq dh (- h2 h1) dm (- m2 m1))
  14725. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14726. (concat t1 "+" (number-to-string dh)
  14727. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14728. (defun org-time-stamp-inactive (&optional arg)
  14729. "Insert an inactive time stamp.
  14730. An inactive time stamp is enclosed in square brackets instead of angle
  14731. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14732. does not link to the calendar and cannot be changed with the S-cursor keys.
  14733. So these are more for recording a certain time/date."
  14734. (interactive "P")
  14735. (org-time-stamp arg 'inactive))
  14736. (defvar org-date-ovl (make-overlay 1 1))
  14737. (overlay-put org-date-ovl 'face 'org-date-selected)
  14738. (org-detach-overlay org-date-ovl)
  14739. (defvar org-ans1) ; dynamically scoped parameter
  14740. (defvar org-ans2) ; dynamically scoped parameter
  14741. (defvar org-plain-time-of-day-regexp) ; defined below
  14742. (defvar org-overriding-default-time nil) ; dynamically scoped
  14743. (defvar org-read-date-overlay nil)
  14744. (defvar org-dcst nil) ; dynamically scoped
  14745. (defvar org-read-date-history nil)
  14746. (defvar org-read-date-final-answer nil)
  14747. (defvar org-read-date-analyze-futurep nil)
  14748. (defvar org-read-date-analyze-forced-year nil)
  14749. (defvar org-read-date-inactive)
  14750. (defvar org-read-date-minibuffer-local-map
  14751. (let ((map (make-sparse-keymap)))
  14752. (set-keymap-parent map minibuffer-local-map)
  14753. (org-defkey map (kbd ".")
  14754. (lambda () (interactive)
  14755. ;; Are we at the beginning of the prompt?
  14756. (if (looking-back "^[^:]+: ")
  14757. (org-eval-in-calendar '(calendar-goto-today))
  14758. (insert "."))))
  14759. (org-defkey map (kbd "C-.")
  14760. (lambda () (interactive)
  14761. (org-eval-in-calendar '(calendar-goto-today))))
  14762. (org-defkey map [(meta shift left)]
  14763. (lambda () (interactive)
  14764. (org-eval-in-calendar '(calendar-backward-month 1))))
  14765. (org-defkey map [(meta shift right)]
  14766. (lambda () (interactive)
  14767. (org-eval-in-calendar '(calendar-forward-month 1))))
  14768. (org-defkey map [(meta shift up)]
  14769. (lambda () (interactive)
  14770. (org-eval-in-calendar '(calendar-backward-year 1))))
  14771. (org-defkey map [(meta shift down)]
  14772. (lambda () (interactive)
  14773. (org-eval-in-calendar '(calendar-forward-year 1))))
  14774. (org-defkey map [?\e (shift left)]
  14775. (lambda () (interactive)
  14776. (org-eval-in-calendar '(calendar-backward-month 1))))
  14777. (org-defkey map [?\e (shift right)]
  14778. (lambda () (interactive)
  14779. (org-eval-in-calendar '(calendar-forward-month 1))))
  14780. (org-defkey map [?\e (shift up)]
  14781. (lambda () (interactive)
  14782. (org-eval-in-calendar '(calendar-backward-year 1))))
  14783. (org-defkey map [?\e (shift down)]
  14784. (lambda () (interactive)
  14785. (org-eval-in-calendar '(calendar-forward-year 1))))
  14786. (org-defkey map [(shift up)]
  14787. (lambda () (interactive)
  14788. (org-eval-in-calendar '(calendar-backward-week 1))))
  14789. (org-defkey map [(shift down)]
  14790. (lambda () (interactive)
  14791. (org-eval-in-calendar '(calendar-forward-week 1))))
  14792. (org-defkey map [(shift left)]
  14793. (lambda () (interactive)
  14794. (org-eval-in-calendar '(calendar-backward-day 1))))
  14795. (org-defkey map [(shift right)]
  14796. (lambda () (interactive)
  14797. (org-eval-in-calendar '(calendar-forward-day 1))))
  14798. (org-defkey map "!"
  14799. (lambda () (interactive)
  14800. (org-eval-in-calendar '(diary-view-entries))
  14801. (message "")))
  14802. (org-defkey map ">"
  14803. (lambda () (interactive)
  14804. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14805. (org-defkey map "<"
  14806. (lambda () (interactive)
  14807. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14808. (org-defkey map "\C-v"
  14809. (lambda () (interactive)
  14810. (org-eval-in-calendar
  14811. '(calendar-scroll-left-three-months 1))))
  14812. (org-defkey map "\M-v"
  14813. (lambda () (interactive)
  14814. (org-eval-in-calendar
  14815. '(calendar-scroll-right-three-months 1))))
  14816. map)
  14817. "Keymap for minibuffer commands when using `org-read-date'.")
  14818. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14819. default-time default-input inactive)
  14820. "Read a date, possibly a time, and make things smooth for the user.
  14821. The prompt will suggest to enter an ISO date, but you can also enter anything
  14822. which will at least partially be understood by `parse-time-string'.
  14823. Unrecognized parts of the date will default to the current day, month, year,
  14824. hour and minute. If this command is called to replace a timestamp at point,
  14825. or to enter the second timestamp of a range, the default time is taken
  14826. from the existing stamp. Furthermore, the command prefers the future,
  14827. so if you are giving a date where the year is not given, and the day-month
  14828. combination is already past in the current year, it will assume you
  14829. mean next year. For details, see the manual. A few examples:
  14830. 3-2-5 --> 2003-02-05
  14831. feb 15 --> currentyear-02-15
  14832. 2/15 --> currentyear-02-15
  14833. sep 12 9 --> 2009-09-12
  14834. 12:45 --> today 12:45
  14835. 22 sept 0:34 --> currentyear-09-22 0:34
  14836. 12 --> currentyear-currentmonth-12
  14837. Fri --> nearest Friday after today
  14838. -Tue --> last Tuesday
  14839. etc.
  14840. Furthermore you can specify a relative date by giving, as the *first* thing
  14841. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14842. change in days weeks, months, years.
  14843. With a single plus or minus, the date is relative to today. With a double
  14844. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14845. +4d --> four days from today
  14846. +4 --> same as above
  14847. +2w --> two weeks from today
  14848. ++5 --> five days from default date
  14849. The function understands only English month and weekday abbreviations.
  14850. While prompting, a calendar is popped up - you can also select the
  14851. date with the mouse (button 1). The calendar shows a period of three
  14852. months. To scroll it to other months, use the keys `>' and `<'.
  14853. If you don't like the calendar, turn it off with
  14854. \(setq org-read-date-popup-calendar nil)
  14855. With optional argument TO-TIME, the date will immediately be converted
  14856. to an internal time.
  14857. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14858. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14859. still enter a time, and this function will inform the calling routine
  14860. about this change. The calling routine may then choose to change the
  14861. format used to insert the time stamp into the buffer to include the time.
  14862. With optional argument FROM-STRING, read from this string instead from
  14863. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14864. the time/date that is used for everything that is not specified by the
  14865. user."
  14866. (require 'parse-time)
  14867. (let* ((org-time-stamp-rounding-minutes
  14868. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14869. (org-dcst org-display-custom-times)
  14870. (ct (org-current-time))
  14871. (org-def (or org-overriding-default-time default-time ct))
  14872. (org-defdecode (decode-time org-def))
  14873. (dummy (progn
  14874. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14875. (setcar (nthcdr 2 org-defdecode) -1)
  14876. (setcar (nthcdr 1 org-defdecode) 59)
  14877. (setq org-def (apply 'encode-time org-defdecode)
  14878. org-defdecode (decode-time org-def)))))
  14879. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14880. (calendar-frame-setup nil)
  14881. (calendar-setup nil)
  14882. (calendar-move-hook nil)
  14883. (calendar-view-diary-initially-flag nil)
  14884. (calendar-view-holidays-initially-flag nil)
  14885. (timestr (format-time-string
  14886. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14887. (prompt (concat (if prompt (concat prompt " ") "")
  14888. (format "Date+time [%s]: " timestr)))
  14889. ans (org-ans0 "") org-ans1 org-ans2 final)
  14890. (cond
  14891. (from-string (setq ans from-string))
  14892. (org-read-date-popup-calendar
  14893. (save-excursion
  14894. (save-window-excursion
  14895. (calendar)
  14896. (org-eval-in-calendar '(setq cursor-type nil) t)
  14897. (unwind-protect
  14898. (progn
  14899. (calendar-forward-day (- (time-to-days org-def)
  14900. (calendar-absolute-from-gregorian
  14901. (calendar-current-date))))
  14902. (org-eval-in-calendar nil t)
  14903. (let* ((old-map (current-local-map))
  14904. (map (copy-keymap calendar-mode-map))
  14905. (minibuffer-local-map
  14906. (copy-keymap org-read-date-minibuffer-local-map)))
  14907. (org-defkey map (kbd "RET") 'org-calendar-select)
  14908. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14909. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14910. (unwind-protect
  14911. (progn
  14912. (use-local-map map)
  14913. (setq org-read-date-inactive inactive)
  14914. (add-hook 'post-command-hook 'org-read-date-display)
  14915. (setq org-ans0 (read-string prompt default-input
  14916. 'org-read-date-history nil))
  14917. ;; org-ans0: from prompt
  14918. ;; org-ans1: from mouse click
  14919. ;; org-ans2: from calendar motion
  14920. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14921. (remove-hook 'post-command-hook 'org-read-date-display)
  14922. (use-local-map old-map)
  14923. (when org-read-date-overlay
  14924. (delete-overlay org-read-date-overlay)
  14925. (setq org-read-date-overlay nil)))))
  14926. (bury-buffer "*Calendar*")))))
  14927. (t ; Naked prompt only
  14928. (unwind-protect
  14929. (setq ans (read-string prompt default-input
  14930. 'org-read-date-history timestr))
  14931. (when org-read-date-overlay
  14932. (delete-overlay org-read-date-overlay)
  14933. (setq org-read-date-overlay nil)))))
  14934. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14935. (when org-read-date-analyze-forced-year
  14936. (message "Year was forced into %s"
  14937. (if org-read-date-force-compatible-dates
  14938. "compatible range (1970-2037)"
  14939. "range representable on this machine"))
  14940. (ding))
  14941. ;; One round trip to get rid of 34th of August and stuff like that....
  14942. (setq final (decode-time (apply 'encode-time final)))
  14943. (setq org-read-date-final-answer ans)
  14944. (if to-time
  14945. (apply 'encode-time final)
  14946. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14947. (format "%04d-%02d-%02d %02d:%02d"
  14948. (nth 5 final) (nth 4 final) (nth 3 final)
  14949. (nth 2 final) (nth 1 final))
  14950. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14951. (defvar org-def)
  14952. (defvar org-defdecode)
  14953. (defvar org-with-time)
  14954. (defun org-read-date-display ()
  14955. "Display the current date prompt interpretation in the minibuffer."
  14956. (when org-read-date-display-live
  14957. (when org-read-date-overlay
  14958. (delete-overlay org-read-date-overlay))
  14959. (when (minibufferp (current-buffer))
  14960. (save-excursion
  14961. (end-of-line 1)
  14962. (while (not (equal (buffer-substring
  14963. (max (point-min) (- (point) 4)) (point))
  14964. " "))
  14965. (insert " ")))
  14966. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14967. " " (or org-ans1 org-ans2)))
  14968. (org-end-time-was-given nil)
  14969. (f (org-read-date-analyze ans org-def org-defdecode))
  14970. (fmts (if org-dcst
  14971. org-time-stamp-custom-formats
  14972. org-time-stamp-formats))
  14973. (fmt (if (or org-with-time
  14974. (and (boundp 'org-time-was-given) org-time-was-given))
  14975. (cdr fmts)
  14976. (car fmts)))
  14977. (txt (format-time-string fmt (apply 'encode-time f)))
  14978. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14979. (txt (concat "=> " txt)))
  14980. (when (and org-end-time-was-given
  14981. (string-match org-plain-time-of-day-regexp txt))
  14982. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14983. org-end-time-was-given
  14984. (substring txt (match-end 0)))))
  14985. (when org-read-date-analyze-futurep
  14986. (setq txt (concat txt " (=>F)")))
  14987. (setq org-read-date-overlay
  14988. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14989. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14990. (defun org-read-date-analyze (ans org-def org-defdecode)
  14991. "Analyze the combined answer of the date prompt."
  14992. ;; FIXME: cleanup and comment
  14993. (let ((nowdecode (decode-time (current-time)))
  14994. delta deltan deltaw deltadef year month day
  14995. hour minute second wday pm h2 m2 tl wday1
  14996. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14997. (setq org-read-date-analyze-futurep nil
  14998. org-read-date-analyze-forced-year nil)
  14999. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15000. (setq ans "+0"))
  15001. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15002. (setq ans (replace-match "" t t ans)
  15003. deltan (car delta)
  15004. deltaw (nth 1 delta)
  15005. deltadef (nth 2 delta)))
  15006. ;; Check if there is an iso week date in there. If yes, store the
  15007. ;; info and postpone interpreting it until the rest of the parsing
  15008. ;; is done.
  15009. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15010. (setq iso-year (if (match-end 1)
  15011. (org-small-year-to-year
  15012. (string-to-number (match-string 1 ans))))
  15013. iso-weekday (if (match-end 3)
  15014. (string-to-number (match-string 3 ans)))
  15015. iso-week (string-to-number (match-string 2 ans)))
  15016. (setq ans (replace-match "" t t ans)))
  15017. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15018. (when (string-match
  15019. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15020. (setq year (if (match-end 2)
  15021. (string-to-number (match-string 2 ans))
  15022. (progn (setq kill-year t)
  15023. (string-to-number (format-time-string "%Y"))))
  15024. month (string-to-number (match-string 3 ans))
  15025. day (string-to-number (match-string 4 ans)))
  15026. (if (< year 100) (setq year (+ 2000 year)))
  15027. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15028. t nil ans)))
  15029. ;; Help matching dotted european dates
  15030. (when (string-match
  15031. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15032. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15033. (setq kill-year t)
  15034. (string-to-number (format-time-string "%Y")))
  15035. day (string-to-number (match-string 1 ans))
  15036. month (string-to-number (match-string 2 ans))
  15037. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15038. t nil ans)))
  15039. ;; Help matching american dates, like 5/30 or 5/30/7
  15040. (when (string-match
  15041. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15042. (setq year (if (match-end 4)
  15043. (string-to-number (match-string 4 ans))
  15044. (progn (setq kill-year t)
  15045. (string-to-number (format-time-string "%Y"))))
  15046. month (string-to-number (match-string 1 ans))
  15047. day (string-to-number (match-string 2 ans)))
  15048. (if (< year 100) (setq year (+ 2000 year)))
  15049. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15050. t nil ans)))
  15051. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15052. ;; If there is a time with am/pm, and *no* time without it, we convert
  15053. ;; so that matching will be successful.
  15054. (loop for i from 1 to 2 do ; twice, for end time as well
  15055. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15056. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15057. (setq hour (string-to-number (match-string 1 ans))
  15058. minute (if (match-end 3)
  15059. (string-to-number (match-string 3 ans))
  15060. 0)
  15061. pm (equal ?p
  15062. (string-to-char (downcase (match-string 4 ans)))))
  15063. (if (and (= hour 12) (not pm))
  15064. (setq hour 0)
  15065. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15066. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15067. t t ans))))
  15068. ;; Check if a time range is given as a duration
  15069. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15070. (setq hour (string-to-number (match-string 1 ans))
  15071. h2 (+ hour (string-to-number (match-string 3 ans)))
  15072. minute (string-to-number (match-string 2 ans))
  15073. m2 (+ minute (if (match-end 5) (string-to-number
  15074. (match-string 5 ans))0)))
  15075. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15076. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15077. t t ans)))
  15078. ;; Check if there is a time range
  15079. (when (boundp 'org-end-time-was-given)
  15080. (setq org-time-was-given nil)
  15081. (when (and (string-match org-plain-time-of-day-regexp ans)
  15082. (match-end 8))
  15083. (setq org-end-time-was-given (match-string 8 ans))
  15084. (setq ans (concat (substring ans 0 (match-beginning 7))
  15085. (substring ans (match-end 7))))))
  15086. (setq tl (parse-time-string ans)
  15087. day (or (nth 3 tl) (nth 3 org-defdecode))
  15088. month (or (nth 4 tl)
  15089. (if (and org-read-date-prefer-future
  15090. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15091. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15092. (nth 4 org-defdecode)))
  15093. year (or (and (not kill-year) (nth 5 tl))
  15094. (if (and org-read-date-prefer-future
  15095. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15096. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15097. (nth 5 org-defdecode)))
  15098. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15099. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15100. second (or (nth 0 tl) 0)
  15101. wday (nth 6 tl))
  15102. (when (and (eq org-read-date-prefer-future 'time)
  15103. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15104. (equal day (nth 3 nowdecode))
  15105. (equal month (nth 4 nowdecode))
  15106. (equal year (nth 5 nowdecode))
  15107. (nth 2 tl)
  15108. (or (< (nth 2 tl) (nth 2 nowdecode))
  15109. (and (= (nth 2 tl) (nth 2 nowdecode))
  15110. (nth 1 tl)
  15111. (< (nth 1 tl) (nth 1 nowdecode)))))
  15112. (setq day (1+ day)
  15113. futurep t))
  15114. ;; Special date definitions below
  15115. (cond
  15116. (iso-week
  15117. ;; There was an iso week
  15118. (require 'cal-iso)
  15119. (setq futurep nil)
  15120. (setq year (or iso-year year)
  15121. day (or iso-weekday wday 1)
  15122. wday nil ; to make sure that the trigger below does not match
  15123. iso-date (calendar-gregorian-from-absolute
  15124. (calendar-absolute-from-iso
  15125. (list iso-week day year))))
  15126. ; FIXME: Should we also push ISO weeks into the future?
  15127. ; (when (and org-read-date-prefer-future
  15128. ; (not iso-year)
  15129. ; (< (calendar-absolute-from-gregorian iso-date)
  15130. ; (time-to-days (current-time))))
  15131. ; (setq year (1+ year)
  15132. ; iso-date (calendar-gregorian-from-absolute
  15133. ; (calendar-absolute-from-iso
  15134. ; (list iso-week day year)))))
  15135. (setq month (car iso-date)
  15136. year (nth 2 iso-date)
  15137. day (nth 1 iso-date)))
  15138. (deltan
  15139. (setq futurep nil)
  15140. (unless deltadef
  15141. (let ((now (decode-time (current-time))))
  15142. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15143. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15144. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15145. ((equal deltaw "m") (setq month (+ month deltan)))
  15146. ((equal deltaw "y") (setq year (+ year deltan)))))
  15147. ((and wday (not (nth 3 tl)))
  15148. ;; Weekday was given, but no day, so pick that day in the week
  15149. ;; on or after the derived date.
  15150. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15151. (unless (equal wday wday1)
  15152. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15153. (if (and (boundp 'org-time-was-given)
  15154. (nth 2 tl))
  15155. (setq org-time-was-given t))
  15156. (if (< year 100) (setq year (+ 2000 year)))
  15157. ;; Check of the date is representable
  15158. (if org-read-date-force-compatible-dates
  15159. (progn
  15160. (if (< year 1970)
  15161. (setq year 1970 org-read-date-analyze-forced-year t))
  15162. (if (> year 2037)
  15163. (setq year 2037 org-read-date-analyze-forced-year t)))
  15164. (condition-case nil
  15165. (ignore (encode-time second minute hour day month year))
  15166. (error
  15167. (setq year (nth 5 org-defdecode))
  15168. (setq org-read-date-analyze-forced-year t))))
  15169. (setq org-read-date-analyze-futurep futurep)
  15170. (list second minute hour day month year)))
  15171. (defvar parse-time-weekdays)
  15172. (defun org-read-date-get-relative (s today default)
  15173. "Check string S for special relative date string.
  15174. TODAY and DEFAULT are internal times, for today and for a default.
  15175. Return shift list (N what def-flag)
  15176. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15177. N is the number of WHATs to shift.
  15178. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15179. the DEFAULT date rather than TODAY."
  15180. (require 'parse-time)
  15181. (when (and
  15182. (string-match
  15183. (concat
  15184. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15185. "\\([0-9]+\\)?"
  15186. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15187. "\\([ \t]\\|$\\)") s)
  15188. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15189. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15190. (string-to-char (substring (match-string 1 s) -1))
  15191. ?+))
  15192. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15193. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15194. (what (if (match-end 3) (match-string 3 s) "d"))
  15195. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15196. (date (if rel default today))
  15197. (wday (nth 6 (decode-time date)))
  15198. delta)
  15199. (if wday1
  15200. (progn
  15201. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15202. (if (= delta 0) (setq delta 7))
  15203. (if (= dir ?-)
  15204. (progn
  15205. (setq delta (- delta 7))
  15206. (if (= delta 0) (setq delta -7))))
  15207. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15208. (list delta "d" rel))
  15209. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15210. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15211. "Turn a user-specified date into the internal representation.
  15212. The internal representation needed by the calendar is (month day year).
  15213. This is a wrapper to handle the brain-dead convention in calendar that
  15214. user function argument order change dependent on argument order."
  15215. (if (boundp 'calendar-date-style)
  15216. (cond
  15217. ((eq calendar-date-style 'american)
  15218. (list arg1 arg2 arg3))
  15219. ((eq calendar-date-style 'european)
  15220. (list arg2 arg1 arg3))
  15221. ((eq calendar-date-style 'iso)
  15222. (list arg2 arg3 arg1)))
  15223. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15224. (if (org-bound-and-true-p european-calendar-style)
  15225. (list arg2 arg1 arg3)
  15226. (list arg1 arg2 arg3)))))
  15227. (defun org-eval-in-calendar (form &optional keepdate)
  15228. "Eval FORM in the calendar window and return to current window.
  15229. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15230. otherwise stick to the current value of `org-ans2'."
  15231. (let ((sf (selected-frame))
  15232. (sw (selected-window)))
  15233. (select-window (get-buffer-window "*Calendar*" t))
  15234. (eval form)
  15235. (when (and (not keepdate) (calendar-cursor-to-date))
  15236. (let* ((date (calendar-cursor-to-date))
  15237. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15238. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15239. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15240. (select-window sw)
  15241. (org-select-frame-set-input-focus sf)))
  15242. (defun org-calendar-select ()
  15243. "Return to `org-read-date' with the date currently selected.
  15244. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15245. (interactive)
  15246. (when (calendar-cursor-to-date)
  15247. (let* ((date (calendar-cursor-to-date))
  15248. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15249. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15250. (if (active-minibuffer-window) (exit-minibuffer))))
  15251. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15252. "Insert a date stamp for the date given by the internal TIME.
  15253. WITH-HM means use the stamp format that includes the time of the day.
  15254. INACTIVE means use square brackets instead of angular ones, so that the
  15255. stamp will not contribute to the agenda.
  15256. PRE and POST are optional strings to be inserted before and after the
  15257. stamp.
  15258. The command returns the inserted time stamp."
  15259. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15260. stamp)
  15261. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15262. (insert-before-markers (or pre ""))
  15263. (when (listp extra)
  15264. (setq extra (car extra))
  15265. (if (and (stringp extra)
  15266. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15267. (setq extra (format "-%02d:%02d"
  15268. (string-to-number (match-string 1 extra))
  15269. (string-to-number (match-string 2 extra))))
  15270. (setq extra nil)))
  15271. (when extra
  15272. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15273. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15274. (insert-before-markers (or post ""))
  15275. (setq org-last-inserted-timestamp stamp)))
  15276. (defun org-toggle-time-stamp-overlays ()
  15277. "Toggle the use of custom time stamp formats."
  15278. (interactive)
  15279. (setq org-display-custom-times (not org-display-custom-times))
  15280. (unless org-display-custom-times
  15281. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15282. (while (setq p (next-single-property-change p 'display))
  15283. (if (and (get-text-property p 'display)
  15284. (eq (get-text-property p 'face) 'org-date))
  15285. (remove-text-properties
  15286. p (setq p (next-single-property-change p 'display))
  15287. '(display t))))
  15288. (set-buffer-modified-p bmp)))
  15289. (if (featurep 'xemacs)
  15290. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15291. (org-restart-font-lock)
  15292. (setq org-table-may-need-update t)
  15293. (if org-display-custom-times
  15294. (message "Time stamps are overlaid with custom format")
  15295. (message "Time stamp overlays removed")))
  15296. (defun org-display-custom-time (beg end)
  15297. "Overlay modified time stamp format over timestamp between BEG and END."
  15298. (let* ((ts (buffer-substring beg end))
  15299. t1 w1 with-hm tf time str w2 (off 0))
  15300. (save-match-data
  15301. (setq t1 (org-parse-time-string ts t))
  15302. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15303. (setq off (- (match-end 0) (match-beginning 0)))))
  15304. (setq end (- end off))
  15305. (setq w1 (- end beg)
  15306. with-hm (and (nth 1 t1) (nth 2 t1))
  15307. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15308. time (org-fix-decoded-time t1)
  15309. str (org-add-props
  15310. (format-time-string
  15311. (substring tf 1 -1) (apply 'encode-time time))
  15312. nil 'mouse-face 'highlight)
  15313. w2 (length str))
  15314. (if (not (= w2 w1))
  15315. (add-text-properties (1+ beg) (+ 2 beg)
  15316. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15317. (if (featurep 'xemacs)
  15318. (progn
  15319. (put-text-property beg end 'invisible t)
  15320. (put-text-property beg end 'end-glyph (make-glyph str)))
  15321. (put-text-property beg end 'display str))))
  15322. (defun org-translate-time (string)
  15323. "Translate all timestamps in STRING to custom format.
  15324. But do this only if the variable `org-display-custom-times' is set."
  15325. (when org-display-custom-times
  15326. (save-match-data
  15327. (let* ((start 0)
  15328. (re org-ts-regexp-both)
  15329. t1 with-hm inactive tf time str beg end)
  15330. (while (setq start (string-match re string start))
  15331. (setq beg (match-beginning 0)
  15332. end (match-end 0)
  15333. t1 (save-match-data
  15334. (org-parse-time-string (substring string beg end) t))
  15335. with-hm (and (nth 1 t1) (nth 2 t1))
  15336. inactive (equal (substring string beg (1+ beg)) "[")
  15337. tf (funcall (if with-hm 'cdr 'car)
  15338. org-time-stamp-custom-formats)
  15339. time (org-fix-decoded-time t1)
  15340. str (format-time-string
  15341. (concat
  15342. (if inactive "[" "<") (substring tf 1 -1)
  15343. (if inactive "]" ">"))
  15344. (apply 'encode-time time))
  15345. string (replace-match str t t string)
  15346. start (+ start (length str)))))))
  15347. string)
  15348. (defun org-fix-decoded-time (time)
  15349. "Set 0 instead of nil for the first 6 elements of time.
  15350. Don't touch the rest."
  15351. (let ((n 0))
  15352. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15353. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15354. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15355. "Difference between TIMESTAMP-STRING and now in days.
  15356. If SECONDS is non-nil, return the difference in seconds."
  15357. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15358. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15359. (funcall fdiff (current-time)))))
  15360. (defun org-deadline-close (timestamp-string &optional ndays)
  15361. "Is the time in TIMESTAMP-STRING close to the current date?"
  15362. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15363. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15364. (not (org-entry-is-done-p))))
  15365. (defun org-get-wdays (ts &optional delay zero-delay)
  15366. "Get the deadline lead time appropriate for timestring TS.
  15367. When DELAY is non-nil, get the delay time for scheduled items
  15368. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15369. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15370. don't try to find the delay cookie in the scheduled timestamp."
  15371. (let ((tv (if delay org-scheduled-delay-days
  15372. org-deadline-warning-days)))
  15373. (cond
  15374. ((or (and delay (< tv 0))
  15375. (and delay zero-delay (<= tv 0))
  15376. (and (not delay) (<= tv 0)))
  15377. ;; Enforce this value no matter what
  15378. (- tv))
  15379. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15380. ;; lead time is specified.
  15381. (floor (* (string-to-number (match-string 1 ts))
  15382. (cdr (assoc (match-string 2 ts)
  15383. '(("d" . 1) ("w" . 7)
  15384. ("m" . 30.4) ("y" . 365.25)
  15385. ("h" . 0.041667)))))))
  15386. ;; go for the default.
  15387. (t tv))))
  15388. (defun org-calendar-select-mouse (ev)
  15389. "Return to `org-read-date' with the date currently selected.
  15390. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15391. (interactive "e")
  15392. (mouse-set-point ev)
  15393. (when (calendar-cursor-to-date)
  15394. (let* ((date (calendar-cursor-to-date))
  15395. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15396. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15397. (if (active-minibuffer-window) (exit-minibuffer))))
  15398. (defun org-check-deadlines (ndays)
  15399. "Check if there are any deadlines due or past due.
  15400. A deadline is considered due if it happens within `org-deadline-warning-days'
  15401. days from today's date. If the deadline appears in an entry marked DONE,
  15402. it is not shown. The prefix arg NDAYS can be used to test that many
  15403. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15404. (interactive "P")
  15405. (let* ((org-warn-days
  15406. (cond
  15407. ((equal ndays '(4)) 100000)
  15408. (ndays (prefix-numeric-value ndays))
  15409. (t (abs org-deadline-warning-days))))
  15410. (case-fold-search nil)
  15411. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15412. (callback
  15413. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15414. (message "%d deadlines past-due or due within %d days"
  15415. (org-occur regexp nil callback)
  15416. org-warn-days)))
  15417. (defsubst org-re-timestamp (type)
  15418. "Return a regexp for timestamp TYPE.
  15419. Allowed values for TYPE are:
  15420. all: all timestamps
  15421. active: only active timestamps (<...>)
  15422. inactive: only inactive timestamps ([...])
  15423. scheduled: only scheduled timestamps
  15424. deadline: only deadline timestamps
  15425. closed: only closed time-stamps
  15426. When TYPE is nil, fall back on returning a regexp that matches
  15427. both scheduled and deadline timestamps."
  15428. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15429. ((eq type 'active) org-ts-regexp)
  15430. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15431. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15432. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15433. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15434. ((eq type 'scheduled-or-deadline)
  15435. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15436. (defun org-check-before-date (date)
  15437. "Check if there are deadlines or scheduled entries before DATE."
  15438. (interactive (list (org-read-date)))
  15439. (let ((case-fold-search nil)
  15440. (regexp (org-re-timestamp org-ts-type))
  15441. (callback
  15442. (lambda () (time-less-p
  15443. (org-time-string-to-time (match-string 1))
  15444. (org-time-string-to-time date)))))
  15445. (message "%d entries before %s"
  15446. (org-occur regexp nil callback) date)))
  15447. (defun org-check-after-date (date)
  15448. "Check if there are deadlines or scheduled entries after DATE."
  15449. (interactive (list (org-read-date)))
  15450. (let ((case-fold-search nil)
  15451. (regexp (org-re-timestamp org-ts-type))
  15452. (callback
  15453. (lambda () (not
  15454. (time-less-p
  15455. (org-time-string-to-time (match-string 1))
  15456. (org-time-string-to-time date))))))
  15457. (message "%d entries after %s"
  15458. (org-occur regexp nil callback) date)))
  15459. (defun org-check-dates-range (start-date end-date)
  15460. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15461. (interactive (list (org-read-date nil nil nil "Range starts")
  15462. (org-read-date nil nil nil "Range end")))
  15463. (let ((case-fold-search nil)
  15464. (regexp (org-re-timestamp org-ts-type))
  15465. (callback
  15466. (lambda ()
  15467. (let ((match (match-string 1)))
  15468. (and
  15469. (not (time-less-p
  15470. (org-time-string-to-time match)
  15471. (org-time-string-to-time start-date)))
  15472. (time-less-p
  15473. (org-time-string-to-time match)
  15474. (org-time-string-to-time end-date)))))))
  15475. (message "%d entries between %s and %s"
  15476. (org-occur regexp nil callback) start-date end-date)))
  15477. (defun org-evaluate-time-range (&optional to-buffer)
  15478. "Evaluate a time range by computing the difference between start and end.
  15479. Normally the result is just printed in the echo area, but with prefix arg
  15480. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15481. If the time range is actually in a table, the result is inserted into the
  15482. next column.
  15483. For time difference computation, a year is assumed to be exactly 365
  15484. days in order to avoid rounding problems."
  15485. (interactive "P")
  15486. (or
  15487. (org-clock-update-time-maybe)
  15488. (save-excursion
  15489. (unless (org-at-date-range-p t)
  15490. (goto-char (point-at-bol))
  15491. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15492. (if (not (org-at-date-range-p t))
  15493. (user-error "Not at a time-stamp range, and none found in current line")))
  15494. (let* ((ts1 (match-string 1))
  15495. (ts2 (match-string 2))
  15496. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15497. (match-end (match-end 0))
  15498. (time1 (org-time-string-to-time ts1))
  15499. (time2 (org-time-string-to-time ts2))
  15500. (t1 (org-float-time time1))
  15501. (t2 (org-float-time time2))
  15502. (diff (abs (- t2 t1)))
  15503. (negative (< (- t2 t1) 0))
  15504. ;; (ys (floor (* 365 24 60 60)))
  15505. (ds (* 24 60 60))
  15506. (hs (* 60 60))
  15507. (fy "%dy %dd %02d:%02d")
  15508. (fy1 "%dy %dd")
  15509. (fd "%dd %02d:%02d")
  15510. (fd1 "%dd")
  15511. (fh "%02d:%02d")
  15512. y d h m align)
  15513. (if havetime
  15514. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15515. y 0
  15516. d (floor (/ diff ds)) diff (mod diff ds)
  15517. h (floor (/ diff hs)) diff (mod diff hs)
  15518. m (floor (/ diff 60)))
  15519. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15520. y 0
  15521. d (floor (+ (/ diff ds) 0.5))
  15522. h 0 m 0))
  15523. (if (not to-buffer)
  15524. (message "%s" (org-make-tdiff-string y d h m))
  15525. (if (org-at-table-p)
  15526. (progn
  15527. (goto-char match-end)
  15528. (setq align t)
  15529. (and (looking-at " *|") (goto-char (match-end 0))))
  15530. (goto-char match-end))
  15531. (if (looking-at
  15532. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15533. (replace-match ""))
  15534. (if negative (insert " -"))
  15535. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15536. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15537. (insert " " (format fh h m))))
  15538. (if align (org-table-align))
  15539. (message "Time difference inserted")))))
  15540. (defun org-make-tdiff-string (y d h m)
  15541. (let ((fmt "")
  15542. (l nil))
  15543. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15544. l (push y l)))
  15545. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15546. l (push d l)))
  15547. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15548. l (push h l)))
  15549. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15550. l (push m l)))
  15551. (apply 'format fmt (nreverse l))))
  15552. (defun org-time-string-to-time (s &optional buffer pos)
  15553. "Convert a timestamp string into internal time."
  15554. (condition-case errdata
  15555. (apply 'encode-time (org-parse-time-string s))
  15556. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15557. s (if (not (and buffer pos))
  15558. ""
  15559. (format " at %d in buffer `%s'" pos buffer))
  15560. (cdr errdata)))))
  15561. (defun org-time-string-to-seconds (s)
  15562. "Convert a timestamp string to a number of seconds."
  15563. (org-float-time (org-time-string-to-time s)))
  15564. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15565. "Convert a time stamp to an absolute day number.
  15566. If there is a specifier for a cyclic time stamp, get the closest
  15567. date to DAYNR.
  15568. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15569. The variable `date' is bound by the calendar when this is called."
  15570. (cond
  15571. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15572. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15573. daynr
  15574. (+ daynr 1000)))
  15575. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15576. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15577. (time-to-days (current-time))) (match-string 0 s)
  15578. prefer show-all))
  15579. (t (time-to-days
  15580. (condition-case errdata
  15581. (apply 'encode-time (org-parse-time-string s))
  15582. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15583. s (if (not (and buffer pos))
  15584. ""
  15585. (format " at %d in buffer `%s'" pos buffer))
  15586. (cdr errdata))))))))
  15587. (defun org-days-to-iso-week (days)
  15588. "Return the iso week number."
  15589. (require 'cal-iso)
  15590. (car (calendar-iso-from-absolute days)))
  15591. (defun org-small-year-to-year (year)
  15592. "Convert 2-digit years into 4-digit years.
  15593. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15594. The year 2000 cannot be abbreviated. Any year larger than 99
  15595. is returned unchanged."
  15596. (if (< year 38)
  15597. (setq year (+ 2000 year))
  15598. (if (< year 100)
  15599. (setq year (+ 1900 year))))
  15600. year)
  15601. (defun org-time-from-absolute (d)
  15602. "Return the time corresponding to date D.
  15603. D may be an absolute day number, or a calendar-type list (month day year)."
  15604. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15605. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15606. (defun org-calendar-holiday ()
  15607. "List of holidays, for Diary display in Org-mode."
  15608. (require 'holidays)
  15609. (let ((hl (funcall
  15610. (if (fboundp 'calendar-check-holidays)
  15611. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15612. (if hl (mapconcat 'identity hl "; "))))
  15613. (defun org-diary-sexp-entry (sexp entry date)
  15614. "Process a SEXP diary ENTRY for DATE."
  15615. (require 'diary-lib)
  15616. (let ((result (if calendar-debug-sexp
  15617. (let ((stack-trace-on-error t))
  15618. (eval (car (read-from-string sexp))))
  15619. (condition-case nil
  15620. (eval (car (read-from-string sexp)))
  15621. (error
  15622. (beep)
  15623. (message "Bad sexp at line %d in %s: %s"
  15624. (org-current-line)
  15625. (buffer-file-name) sexp)
  15626. (sleep-for 2))))))
  15627. (cond ((stringp result) (split-string result "; "))
  15628. ((and (consp result)
  15629. (not (consp (cdr result)))
  15630. (stringp (cdr result))) (cdr result))
  15631. ((and (consp result)
  15632. (stringp (car result))) result)
  15633. (result entry))))
  15634. (defun org-diary-to-ical-string (frombuf)
  15635. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15636. This uses the icalendar.el library."
  15637. (let* ((tmpdir (if (featurep 'xemacs)
  15638. (temp-directory)
  15639. temporary-file-directory))
  15640. (tmpfile (make-temp-name
  15641. (expand-file-name "orgics" tmpdir)))
  15642. buf rtn b e)
  15643. (with-current-buffer frombuf
  15644. (icalendar-export-region (point-min) (point-max) tmpfile)
  15645. (setq buf (find-buffer-visiting tmpfile))
  15646. (set-buffer buf)
  15647. (goto-char (point-min))
  15648. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15649. (setq b (match-beginning 0)))
  15650. (goto-char (point-max))
  15651. (if (re-search-backward "^END:VEVENT" nil t)
  15652. (setq e (match-end 0)))
  15653. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15654. (kill-buffer buf)
  15655. (delete-file tmpfile)
  15656. rtn))
  15657. (defun org-closest-date (start current change prefer show-all)
  15658. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15659. When PREFER is `past', return a date that is either CURRENT or past.
  15660. When PREFER is `future', return a date that is either CURRENT or future.
  15661. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15662. ;; Make the proper lists from the dates
  15663. (catch 'exit
  15664. (let ((a1 '(("h" . hour)
  15665. ("d" . day)
  15666. ("w" . week)
  15667. ("m" . month)
  15668. ("y" . year)))
  15669. (shour (nth 2 (org-parse-time-string start)))
  15670. dn dw sday cday n1 n2 n0
  15671. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15672. (setq start (org-date-to-gregorian start)
  15673. current (org-date-to-gregorian
  15674. (if show-all
  15675. current
  15676. (time-to-days (current-time))))
  15677. sday (calendar-absolute-from-gregorian start)
  15678. cday (calendar-absolute-from-gregorian current))
  15679. (if (<= cday sday) (throw 'exit sday))
  15680. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15681. (setq dn (string-to-number (match-string 1 change))
  15682. dw (cdr (assoc (match-string 2 change) a1)))
  15683. (user-error "Invalid change specifier: %s" change))
  15684. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15685. (cond
  15686. ((eq dw 'hour)
  15687. (let ((missing-hours
  15688. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15689. dn)))
  15690. (setq n1 (if (zerop missing-hours) cday
  15691. (- cday (1+ (floor (/ missing-hours 24)))))
  15692. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15693. ((eq dw 'day)
  15694. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15695. n2 (+ n1 dn)))
  15696. ((eq dw 'year)
  15697. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15698. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15699. (setq date1 (list m d y1)
  15700. n1 (calendar-absolute-from-gregorian date1)
  15701. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15702. n2 (calendar-absolute-from-gregorian date2)))
  15703. ((eq dw 'month)
  15704. ;; approx number of month between the two dates
  15705. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15706. ;; How often does dn fit in there?
  15707. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15708. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15709. m (+ m nm)
  15710. ny (floor (/ m 12))
  15711. y (+ y ny)
  15712. m (- m (* ny 12)))
  15713. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15714. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15715. (setq m2 (+ m dn) y2 y)
  15716. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15717. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15718. (while (<= n2 cday)
  15719. (setq n1 n2 m m2 y y2)
  15720. (setq m2 (+ m dn) y2 y)
  15721. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15722. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15723. ;; Make sure n1 is the earlier date
  15724. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15725. (if show-all
  15726. (cond
  15727. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15728. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15729. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15730. (cond
  15731. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15732. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15733. (t (if (= cday n1) n1 n2)))))))
  15734. (defun org-date-to-gregorian (date)
  15735. "Turn any specification of DATE into a Gregorian date for the calendar."
  15736. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15737. ((and (listp date) (= (length date) 3)) date)
  15738. ((stringp date)
  15739. (setq date (org-parse-time-string date))
  15740. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15741. ((listp date)
  15742. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15743. (defun org-parse-time-string (s &optional nodefault)
  15744. "Parse the standard Org-mode time string.
  15745. This should be a lot faster than the normal `parse-time-string'.
  15746. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15747. hour and minute fields will be nil if not given."
  15748. (cond ((string-match org-ts-regexp0 s)
  15749. (list 0
  15750. (if (or (match-beginning 8) (not nodefault))
  15751. (string-to-number (or (match-string 8 s) "0")))
  15752. (if (or (match-beginning 7) (not nodefault))
  15753. (string-to-number (or (match-string 7 s) "0")))
  15754. (string-to-number (match-string 4 s))
  15755. (string-to-number (match-string 3 s))
  15756. (string-to-number (match-string 2 s))
  15757. nil nil nil))
  15758. ((string-match "^<[^>]+>$" s)
  15759. (decode-time (seconds-to-time (org-matcher-time s))))
  15760. (t (error "Not a standard Org-mode time string: %s" s))))
  15761. (defun org-timestamp-up (&optional arg)
  15762. "Increase the date item at the cursor by one.
  15763. If the cursor is on the year, change the year. If it is on the month,
  15764. the day or the time, change that.
  15765. With prefix ARG, change by that many units."
  15766. (interactive "p")
  15767. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15768. (defun org-timestamp-down (&optional arg)
  15769. "Decrease the date item at the cursor by one.
  15770. If the cursor is on the year, change the year. If it is on the month,
  15771. the day or the time, change that.
  15772. With prefix ARG, change by that many units."
  15773. (interactive "p")
  15774. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15775. (defun org-timestamp-up-day (&optional arg)
  15776. "Increase the date in the time stamp by one day.
  15777. With prefix ARG, change that many days."
  15778. (interactive "p")
  15779. (if (and (not (org-at-timestamp-p t))
  15780. (org-at-heading-p))
  15781. (org-todo 'up)
  15782. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15783. (defun org-timestamp-down-day (&optional arg)
  15784. "Decrease the date in the time stamp by one day.
  15785. With prefix ARG, change that many days."
  15786. (interactive "p")
  15787. (if (and (not (org-at-timestamp-p t))
  15788. (org-at-heading-p))
  15789. (org-todo 'down)
  15790. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15791. (defun org-at-timestamp-p (&optional inactive-ok)
  15792. "Determine if the cursor is in or at a timestamp."
  15793. (interactive)
  15794. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15795. (pos (point))
  15796. (ans (or (looking-at tsr)
  15797. (save-excursion
  15798. (skip-chars-backward "^[<\n\r\t")
  15799. (if (> (point) (point-min)) (backward-char 1))
  15800. (and (looking-at tsr)
  15801. (> (- (match-end 0) pos) -1))))))
  15802. (and ans
  15803. (boundp 'org-ts-what)
  15804. (setq org-ts-what
  15805. (cond
  15806. ((= pos (match-beginning 0)) 'bracket)
  15807. ;; Point is considered to be "on the bracket" whether
  15808. ;; it's really on it or right after it.
  15809. ((= pos (1- (match-end 0))) 'bracket)
  15810. ((= pos (match-end 0)) 'after)
  15811. ((org-pos-in-match-range pos 2) 'year)
  15812. ((org-pos-in-match-range pos 3) 'month)
  15813. ((org-pos-in-match-range pos 7) 'hour)
  15814. ((org-pos-in-match-range pos 8) 'minute)
  15815. ((or (org-pos-in-match-range pos 4)
  15816. (org-pos-in-match-range pos 5)) 'day)
  15817. ((and (> pos (or (match-end 8) (match-end 5)))
  15818. (< pos (match-end 0)))
  15819. (- pos (or (match-end 8) (match-end 5))))
  15820. (t 'day))))
  15821. ans))
  15822. (defun org-toggle-timestamp-type ()
  15823. "Toggle the type (<active> or [inactive]) of a time stamp."
  15824. (interactive)
  15825. (when (org-at-timestamp-p t)
  15826. (let ((beg (match-beginning 0)) (end (match-end 0))
  15827. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15828. (save-excursion
  15829. (goto-char beg)
  15830. (while (re-search-forward "[][<>]" end t)
  15831. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15832. t t)))
  15833. (message "Timestamp is now %sactive"
  15834. (if (equal (char-after beg) ?<) "" "in")))))
  15835. (defun org-at-clock-log-p nil
  15836. "Is the cursor on the clock log line?"
  15837. (save-excursion
  15838. (move-beginning-of-line 1)
  15839. (looking-at "^[ \t]*CLOCK:")))
  15840. (defvar org-clock-history) ; defined in org-clock.el
  15841. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15842. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15843. "Change the date in the time stamp at point.
  15844. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15845. `year', `minute', `second'. If WHAT is not given, the cursor position
  15846. in the timestamp determines what will be changed.
  15847. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15848. (let ((origin (point)) origin-cat
  15849. with-hm inactive
  15850. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15851. org-ts-what
  15852. extra rem
  15853. ts time time0 fixnext clrgx)
  15854. (if (not (org-at-timestamp-p t))
  15855. (user-error "Not at a timestamp"))
  15856. (if (and (not what) (eq org-ts-what 'bracket))
  15857. (org-toggle-timestamp-type)
  15858. ;; Point isn't on brackets. Remember the part of the time-stamp
  15859. ;; the point was in. Indeed, size of time-stamps may change,
  15860. ;; but point must be kept in the same category nonetheless.
  15861. (setq origin-cat org-ts-what)
  15862. (if (and (not what) (not (eq org-ts-what 'day))
  15863. org-display-custom-times
  15864. (get-text-property (point) 'display)
  15865. (not (get-text-property (1- (point)) 'display)))
  15866. (setq org-ts-what 'day))
  15867. (setq org-ts-what (or what org-ts-what)
  15868. inactive (= (char-after (match-beginning 0)) ?\[)
  15869. ts (match-string 0))
  15870. (replace-match "")
  15871. (when (string-match
  15872. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15873. ts)
  15874. (setq extra (match-string 1 ts))
  15875. (if suppress-tmp-delay
  15876. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15877. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15878. (setq with-hm t))
  15879. (setq time0 (org-parse-time-string ts))
  15880. (when (and updown
  15881. (eq org-ts-what 'minute)
  15882. (not current-prefix-arg))
  15883. ;; This looks like s-up and s-down. Change by one rounding step.
  15884. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15885. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15886. (setcar (cdr time0) (+ (nth 1 time0)
  15887. (if (> n 0) (- rem) (- dm rem))))))
  15888. (setq time
  15889. (encode-time (or (car time0) 0)
  15890. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15891. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15892. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15893. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15894. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15895. (nthcdr 6 time0)))
  15896. (when (and (member org-ts-what '(hour minute))
  15897. extra
  15898. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15899. (setq extra (org-modify-ts-extra
  15900. extra
  15901. (if (eq org-ts-what 'hour) 2 5)
  15902. n dm)))
  15903. (when (integerp org-ts-what)
  15904. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15905. (if (eq what 'calendar)
  15906. (let ((cal-date (org-get-date-from-calendar)))
  15907. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15908. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15909. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15910. (setcar time0 (or (car time0) 0))
  15911. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15912. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15913. (setq time (apply 'encode-time time0))))
  15914. ;; Insert the new time-stamp, and ensure point stays in the same
  15915. ;; category as before (i.e. not after the last position in that
  15916. ;; category).
  15917. (let ((pos (point)))
  15918. ;; Stay before inserted string. `save-excursion' is of no use.
  15919. (setq org-last-changed-timestamp
  15920. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15921. (goto-char pos))
  15922. (save-match-data
  15923. (looking-at org-ts-regexp3)
  15924. (goto-char (cond
  15925. ;; `day' category ends before `hour' if any, or at
  15926. ;; the end of the day name.
  15927. ((eq origin-cat 'day)
  15928. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15929. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15930. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15931. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15932. ;; `year' and `month' have both fixed size: point
  15933. ;; couldn't have moved into another part.
  15934. (t origin))))
  15935. ;; Update clock if on a CLOCK line.
  15936. (org-clock-update-time-maybe)
  15937. ;; Maybe adjust the closest clock in `org-clock-history'
  15938. (when org-clock-adjust-closest
  15939. (if (not (and (org-at-clock-log-p)
  15940. (< 1 (length (delq nil (mapcar 'marker-position
  15941. org-clock-history))))))
  15942. (message "No clock to adjust")
  15943. (cond ((save-excursion ; fix previous clock?
  15944. (re-search-backward org-ts-regexp0 nil t)
  15945. (org-looking-back (concat org-clock-string " \\[")))
  15946. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15947. ((save-excursion ; fix next clock?
  15948. (re-search-backward org-ts-regexp0 nil t)
  15949. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15950. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15951. (save-window-excursion
  15952. ;; Find closest clock to point, adjust the previous/next one in history
  15953. (let* ((p (save-excursion (org-back-to-heading t)))
  15954. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15955. (clfixnth
  15956. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15957. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15958. (if (not clfixpos)
  15959. (message "No clock to adjust")
  15960. (save-excursion
  15961. (org-goto-marker-or-bmk clfixpos)
  15962. (org-show-subtree)
  15963. (when (re-search-forward clrgx nil t)
  15964. (goto-char (match-beginning 1))
  15965. (let (org-clock-adjust-closest)
  15966. (org-timestamp-change n org-ts-what updown))
  15967. (message "Clock adjusted in %s for heading: %s"
  15968. (file-name-nondirectory (buffer-file-name))
  15969. (org-get-heading t t)))))))))
  15970. ;; Try to recenter the calendar window, if any.
  15971. (if (and org-calendar-follow-timestamp-change
  15972. (get-buffer-window "*Calendar*" t)
  15973. (memq org-ts-what '(day month year)))
  15974. (org-recenter-calendar (time-to-days time))))))
  15975. (defun org-modify-ts-extra (s pos n dm)
  15976. "Change the different parts of the lead-time and repeat fields in timestamp."
  15977. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15978. ng h m new rem)
  15979. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15980. (cond
  15981. ((or (org-pos-in-match-range pos 2)
  15982. (org-pos-in-match-range pos 3))
  15983. (setq m (string-to-number (match-string 3 s))
  15984. h (string-to-number (match-string 2 s)))
  15985. (if (org-pos-in-match-range pos 2)
  15986. (setq h (+ h n))
  15987. (setq n (* dm (org-no-warnings (signum n))))
  15988. (when (not (= 0 (setq rem (% m dm))))
  15989. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15990. (setq m (+ m n)))
  15991. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15992. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15993. (setq h (min 24 (max 0 h)))
  15994. (setq ng 1 new (format "-%02d:%02d" h m)))
  15995. ((org-pos-in-match-range pos 6)
  15996. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15997. ((org-pos-in-match-range pos 5)
  15998. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15999. ((org-pos-in-match-range pos 9)
  16000. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16001. ((org-pos-in-match-range pos 8)
  16002. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16003. (when ng
  16004. (setq s (concat
  16005. (substring s 0 (match-beginning ng))
  16006. new
  16007. (substring s (match-end ng))))))
  16008. s))
  16009. (defun org-recenter-calendar (date)
  16010. "If the calendar is visible, recenter it to DATE."
  16011. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16012. (when cwin
  16013. (let ((calendar-move-hook nil))
  16014. (with-selected-window cwin
  16015. (calendar-goto-date (if (listp date) date
  16016. (calendar-gregorian-from-absolute date))))))))
  16017. (defun org-goto-calendar (&optional arg)
  16018. "Go to the Emacs calendar at the current date.
  16019. If there is a time stamp in the current line, go to that date.
  16020. A prefix ARG can be used to force the current date."
  16021. (interactive "P")
  16022. (let ((tsr org-ts-regexp) diff
  16023. (calendar-move-hook nil)
  16024. (calendar-view-holidays-initially-flag nil)
  16025. (calendar-view-diary-initially-flag nil))
  16026. (if (or (org-at-timestamp-p)
  16027. (save-excursion
  16028. (beginning-of-line 1)
  16029. (looking-at (concat ".*" tsr))))
  16030. (let ((d1 (time-to-days (current-time)))
  16031. (d2 (time-to-days
  16032. (org-time-string-to-time (match-string 1)))))
  16033. (setq diff (- d2 d1))))
  16034. (calendar)
  16035. (calendar-goto-today)
  16036. (if (and diff (not arg)) (calendar-forward-day diff))))
  16037. (defun org-get-date-from-calendar ()
  16038. "Return a list (month day year) of date at point in calendar."
  16039. (with-current-buffer "*Calendar*"
  16040. (save-match-data
  16041. (calendar-cursor-to-date))))
  16042. (defun org-date-from-calendar ()
  16043. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16044. If there is already a time stamp at the cursor position, update it."
  16045. (interactive)
  16046. (if (org-at-timestamp-p t)
  16047. (org-timestamp-change 0 'calendar)
  16048. (let ((cal-date (org-get-date-from-calendar)))
  16049. (org-insert-time-stamp
  16050. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16051. (defcustom org-effort-durations
  16052. `(("h" . 60)
  16053. ("d" . ,(* 60 8))
  16054. ("w" . ,(* 60 8 5))
  16055. ("m" . ,(* 60 8 5 4))
  16056. ("y" . ,(* 60 8 5 40)))
  16057. "Conversion factor to minutes for an effort modifier.
  16058. Each entry has the form (MODIFIER . MINUTES).
  16059. In an effort string, a number followed by MODIFIER is multiplied
  16060. by the specified number of MINUTES to obtain an effort in
  16061. minutes.
  16062. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16063. effort string \"2hours\" is equivalent to 120 minutes."
  16064. :group 'org-agenda
  16065. :version "24.1"
  16066. :type '(alist :key-type (string :tag "Modifier")
  16067. :value-type (number :tag "Minutes")))
  16068. (defun org-minutes-to-clocksum-string (m)
  16069. "Format number of minutes as a clocksum string.
  16070. The format is determined by `org-time-clocksum-format',
  16071. `org-time-clocksum-use-fractional' and
  16072. `org-time-clocksum-fractional-format' and
  16073. `org-time-clocksum-use-effort-durations'."
  16074. (let ((clocksum "")
  16075. (m (round m)) ; Don't allow fractions of minutes
  16076. h d w mo y fmt n)
  16077. (setq h (if org-time-clocksum-use-effort-durations
  16078. (cdr (assoc "h" org-effort-durations)) 60)
  16079. d (if org-time-clocksum-use-effort-durations
  16080. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16081. w (if org-time-clocksum-use-effort-durations
  16082. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16083. mo (if org-time-clocksum-use-effort-durations
  16084. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16085. y (if org-time-clocksum-use-effort-durations
  16086. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16087. ;; fractional format
  16088. (if org-time-clocksum-use-fractional
  16089. (cond
  16090. ;; single format string
  16091. ((stringp org-time-clocksum-fractional-format)
  16092. (format org-time-clocksum-fractional-format (/ m (float h))))
  16093. ;; choice of fractional formats for different time units
  16094. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16095. (> (/ (truncate m) (* y d h)) 0))
  16096. (format fmt (/ m (* y d (float h)))))
  16097. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16098. (> (/ (truncate m) (* mo d h)) 0))
  16099. (format fmt (/ m (* mo d (float h)))))
  16100. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16101. (> (/ (truncate m) (* w d h)) 0))
  16102. (format fmt (/ m (* w d (float h)))))
  16103. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16104. (> (/ (truncate m) (* d h)) 0))
  16105. (format fmt (/ m (* d (float h)))))
  16106. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16107. (> (/ (truncate m) h) 0))
  16108. (format fmt (/ m (float h))))
  16109. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16110. (format fmt m))
  16111. ;; fall back to smallest time unit with a format
  16112. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16113. (format fmt (/ m (float h))))
  16114. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16115. (format fmt (/ m (* d (float h)))))
  16116. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16117. (format fmt (/ m (* w d (float h)))))
  16118. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16119. (format fmt (/ m (* mo d (float h)))))
  16120. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16121. (format fmt (/ m (* y d (float h))))))
  16122. ;; standard (non-fractional) format, with single format string
  16123. (if (stringp org-time-clocksum-format)
  16124. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16125. ;; separate formats components
  16126. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16127. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16128. (plist-get org-time-clocksum-format :require-years))
  16129. (setq clocksum (concat clocksum (format fmt n))
  16130. m (- m (* n y d h))))
  16131. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16132. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16133. (plist-get org-time-clocksum-format :require-months))
  16134. (setq clocksum (concat clocksum (format fmt n))
  16135. m (- m (* n mo d h))))
  16136. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16137. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16138. (plist-get org-time-clocksum-format :require-weeks))
  16139. (setq clocksum (concat clocksum (format fmt n))
  16140. m (- m (* n w d h))))
  16141. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16142. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16143. (plist-get org-time-clocksum-format :require-days))
  16144. (setq clocksum (concat clocksum (format fmt n))
  16145. m (- m (* n d h))))
  16146. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16147. (or (> (setq n (/ (truncate m) h)) 0)
  16148. (plist-get org-time-clocksum-format :require-hours))
  16149. (setq clocksum (concat clocksum (format fmt n))
  16150. m (- m (* n h))))
  16151. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16152. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16153. (setq clocksum (concat clocksum (format fmt m))))
  16154. ;; return formatted time duration
  16155. clocksum))))
  16156. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16157. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16158. "Org mode version 8.0")
  16159. (defun org-hours-to-clocksum-string (n)
  16160. (org-minutes-to-clocksum-string (* n 60)))
  16161. (defun org-hh:mm-string-to-minutes (s)
  16162. "Convert a string H:MM to a number of minutes.
  16163. If the string is just a number, interpret it as minutes.
  16164. In fact, the first hh:mm or number in the string will be taken,
  16165. there can be extra stuff in the string.
  16166. If no number is found, the return value is 0."
  16167. (cond
  16168. ((integerp s) s)
  16169. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16170. (+ (* (string-to-number (match-string 1 s)) 60)
  16171. (string-to-number (match-string 2 s))))
  16172. ((string-match "\\([0-9]+\\)" s)
  16173. (string-to-number (match-string 1 s)))
  16174. (t 0)))
  16175. (defcustom org-image-actual-width t
  16176. "Should we use the actual width of images when inlining them?
  16177. When set to `t', always use the image width.
  16178. When set to a number, use imagemagick (when available) to set
  16179. the image's width to this value.
  16180. When set to a number in a list, try to get the width from any
  16181. #+ATTR.* keyword if it matches a width specification like
  16182. #+ATTR_HTML: :width 300px
  16183. and fall back on that number if none is found.
  16184. When set to nil, try to get the width from an #+ATTR.* keyword
  16185. and fall back on the original width if none is found.
  16186. This requires Emacs >= 24.1, build with imagemagick support."
  16187. :group 'org-appearance
  16188. :version "24.4"
  16189. :package-version '(Org . "8.0")
  16190. :type '(choice
  16191. (const :tag "Use the image width" t)
  16192. (integer :tag "Use a number of pixels")
  16193. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16194. (const :tag "Use #+ATTR* or don't resize" nil)))
  16195. (defcustom org-agenda-inhibit-startup nil
  16196. "Inhibit startup when preparing agenda buffers.
  16197. When this variable is `t' (the default), the initialization of
  16198. the Org agenda buffers is inhibited: e.g. the visibility state
  16199. is not set, the tables are not re-aligned, etc."
  16200. :type 'boolean
  16201. :version "24.3"
  16202. :group 'org-agenda)
  16203. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16204. "Convert a duration string S to minutes.
  16205. A bare number is interpreted as minutes, modifiers can be set by
  16206. customizing `org-effort-durations' (which see).
  16207. Entries containing a colon are interpreted as H:MM by
  16208. `org-hh:mm-string-to-minutes'."
  16209. (let ((result 0)
  16210. (re (concat "\\([0-9.]+\\) *\\("
  16211. (regexp-opt (mapcar 'car org-effort-durations))
  16212. "\\)")))
  16213. (while (string-match re s)
  16214. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16215. (string-to-number (match-string 1 s))))
  16216. (setq s (replace-match "" nil t s)))
  16217. (setq result (floor result))
  16218. (incf result (org-hh:mm-string-to-minutes s))
  16219. (if output-to-string (number-to-string result) result)))
  16220. ;;;; Files
  16221. (defun org-save-all-org-buffers ()
  16222. "Save all Org-mode buffers without user confirmation."
  16223. (interactive)
  16224. (message "Saving all Org-mode buffers...")
  16225. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16226. (when (featurep 'org-id) (org-id-locations-save))
  16227. (message "Saving all Org-mode buffers... done"))
  16228. (defun org-revert-all-org-buffers ()
  16229. "Revert all Org-mode buffers.
  16230. Prompt for confirmation when there are unsaved changes.
  16231. Be sure you know what you are doing before letting this function
  16232. overwrite your changes.
  16233. This function is useful in a setup where one tracks org files
  16234. with a version control system, to revert on one machine after pulling
  16235. changes from another. I believe the procedure must be like this:
  16236. 1. M-x org-save-all-org-buffers
  16237. 2. Pull changes from the other machine, resolve conflicts
  16238. 3. M-x org-revert-all-org-buffers"
  16239. (interactive)
  16240. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16241. (user-error "Abort"))
  16242. (save-excursion
  16243. (save-window-excursion
  16244. (mapc
  16245. (lambda (b)
  16246. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16247. (with-current-buffer b buffer-file-name))
  16248. (org-pop-to-buffer-same-window b)
  16249. (revert-buffer t 'no-confirm)))
  16250. (buffer-list))
  16251. (when (and (featurep 'org-id) org-id-track-globally)
  16252. (org-id-locations-load)))))
  16253. ;;;; Agenda files
  16254. ;;;###autoload
  16255. (defun org-switchb (&optional arg)
  16256. "Switch between Org buffers.
  16257. With one prefix argument, restrict available buffers to files.
  16258. With two prefix arguments, restrict available buffers to agenda files.
  16259. Defaults to `iswitchb' for buffer name completion.
  16260. Set `org-completion-use-ido' to make it use ido instead."
  16261. (interactive "P")
  16262. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16263. ((equal arg '(16)) (org-buffer-list 'agenda))
  16264. (t (org-buffer-list))))
  16265. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16266. (org-completion-use-ido org-completion-use-ido))
  16267. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16268. (setq org-completion-use-iswitchb t))
  16269. (org-pop-to-buffer-same-window
  16270. (org-icompleting-read "Org buffer: "
  16271. (mapcar 'list (mapcar 'buffer-name blist))
  16272. nil t))))
  16273. ;;; Define some older names previously used for this functionality
  16274. ;;;###autoload
  16275. (defalias 'org-ido-switchb 'org-switchb)
  16276. ;;;###autoload
  16277. (defalias 'org-iswitchb 'org-switchb)
  16278. (defun org-buffer-list (&optional predicate exclude-tmp)
  16279. "Return a list of Org buffers.
  16280. PREDICATE can be `export', `files' or `agenda'.
  16281. export restrict the list to Export buffers.
  16282. files restrict the list to buffers visiting Org files.
  16283. agenda restrict the list to buffers visiting agenda files.
  16284. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16285. (let* ((bfn nil)
  16286. (agenda-files (and (eq predicate 'agenda)
  16287. (mapcar 'file-truename (org-agenda-files t))))
  16288. (filter
  16289. (cond
  16290. ((eq predicate 'files)
  16291. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16292. ((eq predicate 'export)
  16293. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16294. ((eq predicate 'agenda)
  16295. (lambda (b)
  16296. (with-current-buffer b
  16297. (and (derived-mode-p 'org-mode)
  16298. (setq bfn (buffer-file-name b))
  16299. (member (file-truename bfn) agenda-files)))))
  16300. (t (lambda (b) (with-current-buffer b
  16301. (or (derived-mode-p 'org-mode)
  16302. (string-match "\*Org .*Export"
  16303. (buffer-name b)))))))))
  16304. (delq nil
  16305. (mapcar
  16306. (lambda(b)
  16307. (if (and (funcall filter b)
  16308. (or (not exclude-tmp)
  16309. (not (string-match "tmp" (buffer-name b)))))
  16310. b
  16311. nil))
  16312. (buffer-list)))))
  16313. (defun org-agenda-files (&optional unrestricted archives)
  16314. "Get the list of agenda files.
  16315. Optional UNRESTRICTED means return the full list even if a restriction
  16316. is currently in place.
  16317. When ARCHIVES is t, include all archive files that are really being
  16318. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16319. `org-agenda-archives-mode' is t."
  16320. (let ((files
  16321. (cond
  16322. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16323. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16324. ((listp org-agenda-files) org-agenda-files)
  16325. (t (error "Invalid value of `org-agenda-files'")))))
  16326. (setq files (apply 'append
  16327. (mapcar (lambda (f)
  16328. (if (file-directory-p f)
  16329. (directory-files
  16330. f t org-agenda-file-regexp)
  16331. (list f)))
  16332. files)))
  16333. (when org-agenda-skip-unavailable-files
  16334. (setq files (delq nil
  16335. (mapcar (function
  16336. (lambda (file)
  16337. (and (file-readable-p file) file)))
  16338. files))))
  16339. (when (or (eq archives t)
  16340. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16341. (setq files (org-add-archive-files files)))
  16342. files))
  16343. (defun org-agenda-file-p (&optional file)
  16344. "Return non-nil, if FILE is an agenda file.
  16345. If FILE is omitted, use the file associated with the current
  16346. buffer."
  16347. (member (or file (buffer-file-name))
  16348. (org-agenda-files t)))
  16349. (defun org-edit-agenda-file-list ()
  16350. "Edit the list of agenda files.
  16351. Depending on setup, this either uses customize to edit the variable
  16352. `org-agenda-files', or it visits the file that is holding the list. In the
  16353. latter case, the buffer is set up in a way that saving it automatically kills
  16354. the buffer and restores the previous window configuration."
  16355. (interactive)
  16356. (if (stringp org-agenda-files)
  16357. (let ((cw (current-window-configuration)))
  16358. (find-file org-agenda-files)
  16359. (org-set-local 'org-window-configuration cw)
  16360. (org-add-hook 'after-save-hook
  16361. (lambda ()
  16362. (set-window-configuration
  16363. (prog1 org-window-configuration
  16364. (kill-buffer (current-buffer))))
  16365. (org-install-agenda-files-menu)
  16366. (message "New agenda file list installed"))
  16367. nil 'local)
  16368. (message "%s" (substitute-command-keys
  16369. "Edit list and finish with \\[save-buffer]")))
  16370. (customize-variable 'org-agenda-files)))
  16371. (defun org-store-new-agenda-file-list (list)
  16372. "Set new value for the agenda file list and save it correctly."
  16373. (if (stringp org-agenda-files)
  16374. (let ((fe (org-read-agenda-file-list t)) b u)
  16375. (while (setq b (find-buffer-visiting org-agenda-files))
  16376. (kill-buffer b))
  16377. (with-temp-file org-agenda-files
  16378. (insert
  16379. (mapconcat
  16380. (lambda (f) ;; Keep un-expanded entries.
  16381. (if (setq u (assoc f fe))
  16382. (cdr u)
  16383. f))
  16384. list "\n")
  16385. "\n")))
  16386. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16387. (org-insert-mode-line-in-empty-file nil))
  16388. (setq org-agenda-files list)
  16389. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16390. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16391. "Read the list of agenda files from a file.
  16392. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16393. filenames, used by `org-store-new-agenda-file-list' to write back
  16394. un-expanded file names."
  16395. (when (file-directory-p org-agenda-files)
  16396. (error "`org-agenda-files' cannot be a single directory"))
  16397. (when (stringp org-agenda-files)
  16398. (with-temp-buffer
  16399. (insert-file-contents org-agenda-files)
  16400. (mapcar
  16401. (lambda (f)
  16402. (let ((e (expand-file-name (substitute-in-file-name f)
  16403. org-directory)))
  16404. (if pair-with-expansion
  16405. (cons e f)
  16406. e)))
  16407. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16408. ;;;###autoload
  16409. (defun org-cycle-agenda-files ()
  16410. "Cycle through the files in `org-agenda-files'.
  16411. If the current buffer visits an agenda file, find the next one in the list.
  16412. If the current buffer does not, find the first agenda file."
  16413. (interactive)
  16414. (let* ((fs (org-agenda-files t))
  16415. (files (append fs (list (car fs))))
  16416. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16417. file)
  16418. (unless files (user-error "No agenda files"))
  16419. (catch 'exit
  16420. (while (setq file (pop files))
  16421. (if (equal (file-truename file) tcf)
  16422. (when (car files)
  16423. (find-file (car files))
  16424. (throw 'exit t))))
  16425. (find-file (car fs)))
  16426. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16427. (defun org-agenda-file-to-front (&optional to-end)
  16428. "Move/add the current file to the top of the agenda file list.
  16429. If the file is not present in the list, it is added to the front. If it is
  16430. present, it is moved there. With optional argument TO-END, add/move to the
  16431. end of the list."
  16432. (interactive "P")
  16433. (let ((org-agenda-skip-unavailable-files nil)
  16434. (file-alist (mapcar (lambda (x)
  16435. (cons (file-truename x) x))
  16436. (org-agenda-files t)))
  16437. (ctf (file-truename
  16438. (or buffer-file-name
  16439. (user-error "Please save the current buffer to a file"))))
  16440. x had)
  16441. (setq x (assoc ctf file-alist) had x)
  16442. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16443. (if to-end
  16444. (setq file-alist (append (delq x file-alist) (list x)))
  16445. (setq file-alist (cons x (delq x file-alist))))
  16446. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16447. (org-install-agenda-files-menu)
  16448. (message "File %s to %s of agenda file list"
  16449. (if had "moved" "added") (if to-end "end" "front"))))
  16450. (defun org-remove-file (&optional file)
  16451. "Remove current file from the list of files in variable `org-agenda-files'.
  16452. These are the files which are being checked for agenda entries.
  16453. Optional argument FILE means use this file instead of the current."
  16454. (interactive)
  16455. (let* ((org-agenda-skip-unavailable-files nil)
  16456. (file (or file buffer-file-name
  16457. (user-error "Current buffer does not visit a file")))
  16458. (true-file (file-truename file))
  16459. (afile (abbreviate-file-name file))
  16460. (files (delq nil (mapcar
  16461. (lambda (x)
  16462. (if (equal true-file
  16463. (file-truename x))
  16464. nil x))
  16465. (org-agenda-files t)))))
  16466. (if (not (= (length files) (length (org-agenda-files t))))
  16467. (progn
  16468. (org-store-new-agenda-file-list files)
  16469. (org-install-agenda-files-menu)
  16470. (message "Removed file: %s" afile))
  16471. (message "File was not in list: %s (not removed)" afile))))
  16472. (defun org-file-menu-entry (file)
  16473. (vector file (list 'find-file file) t))
  16474. (defun org-check-agenda-file (file)
  16475. "Make sure FILE exists. If not, ask user what to do."
  16476. (when (not (file-exists-p file))
  16477. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16478. (abbreviate-file-name file))
  16479. (let ((r (downcase (read-char-exclusive))))
  16480. (cond
  16481. ((equal r ?r)
  16482. (org-remove-file file)
  16483. (throw 'nextfile t))
  16484. (t (error "Abort"))))))
  16485. (defun org-get-agenda-file-buffer (file)
  16486. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16487. it to the list of buffers which might be released later."
  16488. (let ((buf (org-find-base-buffer-visiting file)))
  16489. (if buf
  16490. buf ; just return it
  16491. ;; Make a new buffer and remember it
  16492. (setq buf (find-file-noselect file))
  16493. (if buf (push buf org-agenda-new-buffers))
  16494. buf)))
  16495. (defun org-release-buffers (blist)
  16496. "Release all buffers in list, asking the user for confirmation when needed.
  16497. When a buffer is unmodified, it is just killed. When modified, it is saved
  16498. \(if the user agrees) and then killed."
  16499. (let (buf file)
  16500. (while (setq buf (pop blist))
  16501. (setq file (buffer-file-name buf))
  16502. (when (and (buffer-modified-p buf)
  16503. file
  16504. (y-or-n-p (format "Save file %s? " file)))
  16505. (with-current-buffer buf (save-buffer)))
  16506. (kill-buffer buf))))
  16507. (defun org-agenda-prepare-buffers (files)
  16508. "Create buffers for all agenda files, protect archived trees and comments."
  16509. (interactive)
  16510. (let ((pa '(:org-archived t))
  16511. (pc '(:org-comment t))
  16512. (pall '(:org-archived t :org-comment t))
  16513. (inhibit-read-only t)
  16514. (org-inhibit-startup org-agenda-inhibit-startup)
  16515. (rea (concat ":" org-archive-tag ":"))
  16516. file re)
  16517. (setq org-tag-alist-for-agenda nil
  16518. org-tag-groups-alist-for-agenda nil)
  16519. (save-excursion
  16520. (save-restriction
  16521. (while (setq file (pop files))
  16522. (catch 'nextfile
  16523. (if (bufferp file)
  16524. (set-buffer file)
  16525. (org-check-agenda-file file)
  16526. (set-buffer (org-get-agenda-file-buffer file)))
  16527. (widen)
  16528. (org-set-regexps-and-options-for-tags)
  16529. (org-refresh-category-properties)
  16530. (org-refresh-properties org-effort-property 'org-effort)
  16531. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16532. (setq org-todo-keywords-for-agenda
  16533. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16534. (setq org-done-keywords-for-agenda
  16535. (append org-done-keywords-for-agenda org-done-keywords))
  16536. (setq org-todo-keyword-alist-for-agenda
  16537. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16538. (setq org-drawers-for-agenda
  16539. (append org-drawers-for-agenda org-drawers))
  16540. (setq org-tag-alist-for-agenda
  16541. (org-uniquify
  16542. (append org-tag-alist-for-agenda
  16543. org-tag-alist
  16544. org-tag-persistent-alist)))
  16545. (if org-group-tags
  16546. (setq org-tag-groups-alist-for-agenda
  16547. (org-uniquify-alist
  16548. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16549. (org-with-silent-modifications
  16550. (save-excursion
  16551. (remove-text-properties (point-min) (point-max) pall)
  16552. (when org-agenda-skip-archived-trees
  16553. (goto-char (point-min))
  16554. (while (re-search-forward rea nil t)
  16555. (if (org-at-heading-p t)
  16556. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16557. (goto-char (point-min))
  16558. (setq re (format org-heading-keyword-regexp-format
  16559. org-comment-string))
  16560. (while (re-search-forward re nil t)
  16561. (add-text-properties
  16562. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16563. (setq org-todo-keywords-for-agenda
  16564. (org-uniquify org-todo-keywords-for-agenda))
  16565. (setq org-todo-keyword-alist-for-agenda
  16566. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16567. ;;;; CDLaTeX minor mode
  16568. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16569. "Keymap for the minor `org-cdlatex-mode'.")
  16570. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16571. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16572. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16573. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16574. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16575. (defvar org-cdlatex-texmathp-advice-is-done nil
  16576. "Flag remembering if we have applied the advice to texmathp already.")
  16577. (define-minor-mode org-cdlatex-mode
  16578. "Toggle the minor `org-cdlatex-mode'.
  16579. This mode supports entering LaTeX environment and math in LaTeX fragments
  16580. in Org-mode.
  16581. \\{org-cdlatex-mode-map}"
  16582. nil " OCDL" nil
  16583. (when org-cdlatex-mode
  16584. (require 'cdlatex)
  16585. (run-hooks 'cdlatex-mode-hook)
  16586. (cdlatex-compute-tables))
  16587. (unless org-cdlatex-texmathp-advice-is-done
  16588. (setq org-cdlatex-texmathp-advice-is-done t)
  16589. (defadvice texmathp (around org-math-always-on activate)
  16590. "Always return t in org-mode buffers.
  16591. This is because we want to insert math symbols without dollars even outside
  16592. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16593. an embedded LaTeX fragment, let texmathp do its job.
  16594. \\[org-cdlatex-mode-map]"
  16595. (interactive)
  16596. (let (p)
  16597. (cond
  16598. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16599. ((eq this-command 'cdlatex-math-symbol)
  16600. (setq ad-return-value t
  16601. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16602. (t
  16603. (let ((p (org-inside-LaTeX-fragment-p)))
  16604. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16605. (setq ad-return-value t
  16606. texmathp-why '("Org-mode embedded math" . 0))
  16607. (if p ad-do-it)))))))))
  16608. (defun turn-on-org-cdlatex ()
  16609. "Unconditionally turn on `org-cdlatex-mode'."
  16610. (org-cdlatex-mode 1))
  16611. (defun org-try-cdlatex-tab ()
  16612. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16613. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16614. - inside a LaTeX fragment, or
  16615. - after the first word in a line, where an abbreviation expansion could
  16616. insert a LaTeX environment."
  16617. (when org-cdlatex-mode
  16618. (cond
  16619. ;; Before any word on the line: No expansion possible.
  16620. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16621. ;; Just after first word on the line: Expand it. Make sure it
  16622. ;; cannot happen on headlines, though.
  16623. ((save-excursion
  16624. (skip-chars-backward "a-zA-Z0-9*")
  16625. (skip-chars-backward " \t")
  16626. (and (bolp) (not (org-at-heading-p))))
  16627. (cdlatex-tab) t)
  16628. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16629. (defun org-cdlatex-underscore-caret (&optional arg)
  16630. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16631. Revert to the normal definition outside of these fragments."
  16632. (interactive "P")
  16633. (if (org-inside-LaTeX-fragment-p)
  16634. (call-interactively 'cdlatex-sub-superscript)
  16635. (let (org-cdlatex-mode)
  16636. (call-interactively (key-binding (vector last-input-event))))))
  16637. (defun org-cdlatex-math-modify (&optional arg)
  16638. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16639. Revert to the normal definition outside of these fragments."
  16640. (interactive "P")
  16641. (if (org-inside-LaTeX-fragment-p)
  16642. (call-interactively 'cdlatex-math-modify)
  16643. (let (org-cdlatex-mode)
  16644. (call-interactively (key-binding (vector last-input-event))))))
  16645. ;;;; LaTeX fragments
  16646. (defvar org-latex-regexps
  16647. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16648. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16649. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16650. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16651. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16652. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16653. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16654. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16655. "Regular expressions for matching embedded LaTeX.")
  16656. (defun org-inside-LaTeX-fragment-p ()
  16657. "Test if point is inside a LaTeX fragment.
  16658. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16659. sequence appearing also before point.
  16660. Even though the matchers for math are configurable, this function assumes
  16661. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16662. delimiters are skipped when they have been removed by customization.
  16663. The return value is nil, or a cons cell with the delimiter and the
  16664. position of this delimiter.
  16665. This function does a reasonably good job, but can locally be fooled by
  16666. for example currency specifications. For example it will assume being in
  16667. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16668. fragments that are properly closed, but during editing, we have to live
  16669. with the uncertainty caused by missing closing delimiters. This function
  16670. looks only before point, not after."
  16671. (catch 'exit
  16672. (let ((pos (point))
  16673. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16674. (lim (progn
  16675. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16676. (point)))
  16677. dd-on str (start 0) m re)
  16678. (goto-char pos)
  16679. (when dodollar
  16680. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16681. re (nth 1 (assoc "$" org-latex-regexps)))
  16682. (while (string-match re str start)
  16683. (cond
  16684. ((= (match-end 0) (length str))
  16685. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16686. ((= (match-end 0) (- (length str) 5))
  16687. (throw 'exit nil))
  16688. (t (setq start (match-end 0))))))
  16689. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16690. (goto-char pos)
  16691. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16692. (and (match-beginning 2) (throw 'exit nil))
  16693. ;; count $$
  16694. (while (re-search-backward "\\$\\$" lim t)
  16695. (setq dd-on (not dd-on)))
  16696. (goto-char pos)
  16697. (if dd-on (cons "$$" m))))))
  16698. (defun org-inside-latex-macro-p ()
  16699. "Is point inside a LaTeX macro or its arguments?"
  16700. (save-match-data
  16701. (org-in-regexp
  16702. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16703. (defvar org-latex-fragment-image-overlays nil
  16704. "List of overlays carrying the images of latex fragments.")
  16705. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16706. (defun org-remove-latex-fragment-image-overlays ()
  16707. "Remove all overlays with LaTeX fragment images in current buffer."
  16708. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16709. (setq org-latex-fragment-image-overlays nil))
  16710. (defun org-preview-latex-fragment (&optional subtree)
  16711. "Preview the LaTeX fragment at point, or all locally or globally.
  16712. If the cursor is in a LaTeX fragment, create the image and overlay
  16713. it over the source code. If there is no fragment at point, display
  16714. all fragments in the current text, from one headline to the next. With
  16715. prefix SUBTREE, display all fragments in the current subtree. With a
  16716. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16717. the cursor is before the first headline,
  16718. display all fragments in the buffer.
  16719. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16720. (interactive "P")
  16721. (unless buffer-file-name
  16722. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  16723. (when (display-graphic-p)
  16724. (org-remove-latex-fragment-image-overlays)
  16725. (save-excursion
  16726. (save-restriction
  16727. (let (beg end at msg)
  16728. (cond
  16729. ((or (equal subtree '(16))
  16730. (not (save-excursion
  16731. (re-search-backward org-outline-regexp-bol nil t))))
  16732. (setq beg (point-min) end (point-max)
  16733. msg "Creating images for buffer...%s"))
  16734. ((equal subtree '(4))
  16735. (org-back-to-heading)
  16736. (setq beg (point) end (org-end-of-subtree t)
  16737. msg "Creating images for subtree...%s"))
  16738. (t
  16739. (if (setq at (org-inside-LaTeX-fragment-p))
  16740. (goto-char (max (point-min) (- (cdr at) 2)))
  16741. (org-back-to-heading))
  16742. (setq beg (point) end (progn (outline-next-heading) (point))
  16743. msg (if at "Creating image...%s"
  16744. "Creating images for entry...%s"))))
  16745. (message msg "")
  16746. (narrow-to-region beg end)
  16747. (goto-char beg)
  16748. (org-format-latex
  16749. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16750. (file-name-nondirectory
  16751. buffer-file-name)))
  16752. default-directory 'overlays msg at 'forbuffer
  16753. org-latex-create-formula-image-program)
  16754. (message msg "done. Use `C-c C-c' to remove images."))))))
  16755. (defun org-format-latex (prefix &optional dir overlays msg at
  16756. forbuffer processing-type)
  16757. "Replace LaTeX fragments with links to an image, and produce images.
  16758. Some of the options can be changed using the variable
  16759. `org-format-latex-options'."
  16760. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16761. (let* ((prefixnodir (file-name-nondirectory prefix))
  16762. (absprefix (expand-file-name prefix dir))
  16763. (todir (file-name-directory absprefix))
  16764. (opt org-format-latex-options)
  16765. (optnew org-format-latex-options)
  16766. (matchers (plist-get opt :matchers))
  16767. (re-list org-latex-regexps)
  16768. (cnt 0) txt hash link beg end re e checkdir
  16769. string
  16770. m n block-type block linkfile movefile ov)
  16771. ;; Check the different regular expressions
  16772. (while (setq e (pop re-list))
  16773. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16774. block (if block-type "\n\n" ""))
  16775. (when (member m matchers)
  16776. (goto-char (point-min))
  16777. (while (re-search-forward re nil t)
  16778. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16779. (or (not overlays)
  16780. (not (eq (get-char-property (match-beginning n)
  16781. 'org-overlay-type)
  16782. 'org-latex-overlay))))
  16783. (cond
  16784. ((eq processing-type 'verbatim))
  16785. ((eq processing-type 'mathjax)
  16786. ;; Prepare for MathJax processing.
  16787. (setq string (match-string n))
  16788. (when (member m '("$" "$1"))
  16789. (save-excursion
  16790. (delete-region (match-beginning n) (match-end n))
  16791. (goto-char (match-beginning n))
  16792. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16793. ((or (eq processing-type 'dvipng)
  16794. (eq processing-type 'imagemagick))
  16795. ;; Process to an image.
  16796. (setq txt (match-string n)
  16797. beg (match-beginning n) end (match-end n)
  16798. cnt (1+ cnt))
  16799. (let ((face (face-at-point))
  16800. (fg (plist-get opt :foreground))
  16801. (bg (plist-get opt :background))
  16802. ;; Ensure full list is printed.
  16803. print-length print-level)
  16804. (when forbuffer
  16805. ;; Get the colors from the face at point.
  16806. (goto-char beg)
  16807. (when (eq fg 'auto)
  16808. (setq fg (face-attribute face :foreground nil 'default)))
  16809. (when (eq bg 'auto)
  16810. (setq bg (face-attribute face :background nil 'default)))
  16811. (setq optnew (copy-sequence opt))
  16812. (plist-put optnew :foreground fg)
  16813. (plist-put optnew :background bg))
  16814. (setq hash (sha1 (prin1-to-string
  16815. (list org-format-latex-header
  16816. org-latex-default-packages-alist
  16817. org-latex-packages-alist
  16818. org-format-latex-options
  16819. forbuffer txt fg bg)))
  16820. linkfile (format "%s_%s.png" prefix hash)
  16821. movefile (format "%s_%s.png" absprefix hash)))
  16822. (setq link (concat block "[[file:" linkfile "]]" block))
  16823. (if msg (message msg cnt))
  16824. (goto-char beg)
  16825. (unless checkdir ; Ensure the directory exists.
  16826. (setq checkdir t)
  16827. (or (file-directory-p todir) (make-directory todir t)))
  16828. (unless (file-exists-p movefile)
  16829. (org-create-formula-image
  16830. txt movefile optnew forbuffer processing-type))
  16831. (if overlays
  16832. (progn
  16833. (mapc (lambda (o)
  16834. (if (eq (overlay-get o 'org-overlay-type)
  16835. 'org-latex-overlay)
  16836. (delete-overlay o)))
  16837. (overlays-in beg end))
  16838. (setq ov (make-overlay beg end))
  16839. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16840. (if (featurep 'xemacs)
  16841. (progn
  16842. (overlay-put ov 'invisible t)
  16843. (overlay-put
  16844. ov 'end-glyph
  16845. (make-glyph (vector 'png :file movefile))))
  16846. (overlay-put
  16847. ov 'display
  16848. (list 'image :type 'png :file movefile :ascent 'center)))
  16849. (push ov org-latex-fragment-image-overlays)
  16850. (goto-char end))
  16851. (delete-region beg end)
  16852. (insert (org-add-props link
  16853. (list 'org-latex-src
  16854. (replace-regexp-in-string
  16855. "\"" "" txt)
  16856. 'org-latex-src-embed-type
  16857. (if block-type 'paragraph 'character))))))
  16858. ((eq processing-type 'mathml)
  16859. ;; Process to MathML
  16860. (unless (save-match-data (org-format-latex-mathml-available-p))
  16861. (user-error "LaTeX to MathML converter not configured"))
  16862. (setq txt (match-string n)
  16863. beg (match-beginning n) end (match-end n)
  16864. cnt (1+ cnt))
  16865. (if msg (message msg cnt))
  16866. (goto-char beg)
  16867. (delete-region beg end)
  16868. (insert (org-format-latex-as-mathml
  16869. txt block-type prefix dir)))
  16870. (t
  16871. (error "Unknown conversion type %s for LaTeX fragments"
  16872. processing-type)))))))))
  16873. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16874. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16875. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16876. sucessful, return the portion between \"<math...> </math>\"
  16877. elements otherwise return nil. When MATHML-FILE is specified,
  16878. write the results in to that file. When invoked as an
  16879. interactive command, prompt for LATEX-FRAG, with initial value
  16880. set to the current active region and echo the results for user
  16881. inspection."
  16882. (interactive (list (let ((frag (when (org-region-active-p)
  16883. (buffer-substring-no-properties
  16884. (region-beginning) (region-end)))))
  16885. (read-string "LaTeX Fragment: " frag nil frag))))
  16886. (unless latex-frag (error "Invalid LaTeX fragment"))
  16887. (let* ((tmp-in-file (file-relative-name
  16888. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16889. (ignore (write-region latex-frag nil tmp-in-file))
  16890. (tmp-out-file (file-relative-name
  16891. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16892. (cmd (format-spec
  16893. org-latex-to-mathml-convert-command
  16894. `((?j . ,(shell-quote-argument
  16895. (expand-file-name org-latex-to-mathml-jar-file)))
  16896. (?I . ,(shell-quote-argument tmp-in-file))
  16897. (?o . ,(shell-quote-argument tmp-out-file)))))
  16898. mathml shell-command-output)
  16899. (when (org-called-interactively-p 'any)
  16900. (unless (org-format-latex-mathml-available-p)
  16901. (user-error "LaTeX to MathML converter not configured")))
  16902. (message "Running %s" cmd)
  16903. (setq shell-command-output (shell-command-to-string cmd))
  16904. (setq mathml
  16905. (when (file-readable-p tmp-out-file)
  16906. (with-current-buffer (find-file-noselect tmp-out-file t)
  16907. (goto-char (point-min))
  16908. (when (re-search-forward
  16909. (concat
  16910. (regexp-quote
  16911. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16912. "\\(.\\|\n\\)*"
  16913. (regexp-quote "</math>")) nil t)
  16914. (prog1 (match-string 0) (kill-buffer))))))
  16915. (cond
  16916. (mathml
  16917. (setq mathml
  16918. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16919. (when mathml-file
  16920. (write-region mathml nil mathml-file))
  16921. (when (org-called-interactively-p 'any)
  16922. (message mathml)))
  16923. ((message "LaTeX to MathML conversion failed")
  16924. (message shell-command-output)))
  16925. (delete-file tmp-in-file)
  16926. (when (file-exists-p tmp-out-file)
  16927. (delete-file tmp-out-file))
  16928. mathml))
  16929. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16930. prefix &optional dir)
  16931. "Use `org-create-math-formula' but check local cache first."
  16932. (let* ((absprefix (expand-file-name prefix dir))
  16933. (print-length nil) (print-level nil)
  16934. (formula-id (concat
  16935. "formula-"
  16936. (sha1
  16937. (prin1-to-string
  16938. (list latex-frag
  16939. org-latex-to-mathml-convert-command)))))
  16940. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16941. (formula-cache-dir (file-name-directory formula-cache)))
  16942. (unless (file-directory-p formula-cache-dir)
  16943. (make-directory formula-cache-dir t))
  16944. (unless (file-exists-p formula-cache)
  16945. (org-create-math-formula latex-frag formula-cache))
  16946. (if (file-exists-p formula-cache)
  16947. ;; Successful conversion. Return the link to MathML file.
  16948. (org-add-props
  16949. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16950. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16951. 'org-latex-src-embed-type (if latex-frag-type
  16952. 'paragraph 'character)))
  16953. ;; Failed conversion. Return the LaTeX fragment verbatim
  16954. latex-frag)))
  16955. (defun org-create-formula-image (string tofile options buffer &optional type)
  16956. "Create an image from LaTeX source using dvipng or convert.
  16957. This function calls either `org-create-formula-image-with-dvipng'
  16958. or `org-create-formula-image-with-imagemagick' depending on the
  16959. value of `org-latex-create-formula-image-program' or on the value
  16960. of the optional TYPE variable.
  16961. Note: ultimately these two function should be combined as they
  16962. share a good deal of logic."
  16963. (org-check-external-command
  16964. "latex" "needed to convert LaTeX fragments to images")
  16965. (funcall
  16966. (case (or type org-latex-create-formula-image-program)
  16967. ('dvipng
  16968. (org-check-external-command
  16969. "dvipng" "needed to convert LaTeX fragments to images")
  16970. #'org-create-formula-image-with-dvipng)
  16971. ('imagemagick
  16972. (org-check-external-command
  16973. "convert" "you need to install imagemagick")
  16974. #'org-create-formula-image-with-imagemagick)
  16975. (t (error
  16976. "Invalid value of `org-latex-create-formula-image-program'")))
  16977. string tofile options buffer))
  16978. (declare-function org-export--get-global-options "ox" (&optional backend))
  16979. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16980. (defun org-create-formula--latex-header ()
  16981. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  16982. (org-latex-guess-inputenc
  16983. (org-splice-latex-header
  16984. org-format-latex-header
  16985. org-latex-default-packages-alist
  16986. org-latex-packages-alist t
  16987. (plist-get
  16988. (org-combine-plists
  16989. (org-export--get-global-options 'latex)
  16990. (org-export--get-inbuffer-options 'latex))
  16991. :latex-header))))
  16992. ;; This function borrows from Ganesh Swami's latex2png.el
  16993. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16994. "This calls dvipng."
  16995. (require 'ox-latex)
  16996. (let* ((tmpdir (if (featurep 'xemacs)
  16997. (temp-directory)
  16998. temporary-file-directory))
  16999. (texfilebase (make-temp-name
  17000. (expand-file-name "orgtex" tmpdir)))
  17001. (texfile (concat texfilebase ".tex"))
  17002. (dvifile (concat texfilebase ".dvi"))
  17003. (pngfile (concat texfilebase ".png"))
  17004. (fnh (if (featurep 'xemacs)
  17005. (font-height (face-font 'default))
  17006. (face-attribute 'default :height nil)))
  17007. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17008. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17009. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17010. "Black"))
  17011. (bg (or (plist-get options (if buffer :background :html-background))
  17012. "Transparent")))
  17013. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17014. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17015. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17016. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17017. (let ((latex-header (org-create-formula--latex-header)))
  17018. (with-temp-file texfile
  17019. (insert latex-header)
  17020. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17021. (let ((dir default-directory))
  17022. (condition-case nil
  17023. (progn
  17024. (cd tmpdir)
  17025. (call-process "latex" nil nil nil texfile))
  17026. (error nil))
  17027. (cd dir))
  17028. (if (not (file-exists-p dvifile))
  17029. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17030. (condition-case nil
  17031. (if (featurep 'xemacs)
  17032. (call-process "dvipng" nil nil nil
  17033. "-fg" fg "-bg" bg
  17034. "-T" "tight"
  17035. "-o" pngfile
  17036. dvifile)
  17037. (call-process "dvipng" nil nil nil
  17038. "-fg" fg "-bg" bg
  17039. "-D" dpi
  17040. ;;"-x" scale "-y" scale
  17041. "-T" "tight"
  17042. "-o" pngfile
  17043. dvifile))
  17044. (error nil))
  17045. (if (not (file-exists-p pngfile))
  17046. (if org-format-latex-signal-error
  17047. (error "Failed to create png file from %s" texfile)
  17048. (message "Failed to create png file from %s" texfile)
  17049. nil)
  17050. ;; Use the requested file name and clean up
  17051. (copy-file pngfile tofile 'replace)
  17052. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17053. (if (file-exists-p (concat texfilebase e))
  17054. (delete-file (concat texfilebase e))))
  17055. pngfile))))
  17056. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17057. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17058. "This calls convert, which is included into imagemagick."
  17059. (require 'ox-latex)
  17060. (let* ((tmpdir (if (featurep 'xemacs)
  17061. (temp-directory)
  17062. temporary-file-directory))
  17063. (texfilebase (make-temp-name
  17064. (expand-file-name "orgtex" tmpdir)))
  17065. (texfile (concat texfilebase ".tex"))
  17066. (pdffile (concat texfilebase ".pdf"))
  17067. (pngfile (concat texfilebase ".png"))
  17068. (fnh (if (featurep 'xemacs)
  17069. (font-height (face-font 'default))
  17070. (face-attribute 'default :height nil)))
  17071. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17072. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17073. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17074. "black"))
  17075. (bg (or (plist-get options (if buffer :background :html-background))
  17076. "white")))
  17077. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17078. (setq fg (org-latex-color-format fg)))
  17079. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17080. (setq bg (org-latex-color-format
  17081. (if (string= bg "Transparent") "white" bg))))
  17082. (let ((latex-header (org-create-formula--latex-header)))
  17083. (with-temp-file texfile
  17084. (insert latex-header)
  17085. (insert "\n\\begin{document}\n"
  17086. "\\definecolor{fg}{rgb}{" fg "}\n"
  17087. "\\definecolor{bg}{rgb}{" bg "}\n"
  17088. "\n\\pagecolor{bg}\n"
  17089. "\n{\\color{fg}\n"
  17090. string
  17091. "\n}\n"
  17092. "\n\\end{document}\n")))
  17093. (org-latex-compile texfile t)
  17094. (if (not (file-exists-p pdffile))
  17095. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17096. (condition-case nil
  17097. (if (featurep 'xemacs)
  17098. (call-process "convert" nil nil nil
  17099. "-density" "96"
  17100. "-trim"
  17101. "-antialias"
  17102. pdffile
  17103. "-quality" "100"
  17104. ;; "-sharpen" "0x1.0"
  17105. pngfile)
  17106. (call-process "convert" nil nil nil
  17107. "-density" dpi
  17108. "-trim"
  17109. "-antialias"
  17110. pdffile
  17111. "-quality" "100"
  17112. ;; "-sharpen" "0x1.0"
  17113. pngfile))
  17114. (error nil))
  17115. (if (not (file-exists-p pngfile))
  17116. (if org-format-latex-signal-error
  17117. (error "Failed to create png file from %s" texfile)
  17118. (message "Failed to create png file from %s" texfile)
  17119. nil)
  17120. ;; Use the requested file name and clean up
  17121. (copy-file pngfile tofile 'replace)
  17122. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17123. (if (file-exists-p (concat texfilebase e))
  17124. (delete-file (concat texfilebase e))))
  17125. pngfile))))
  17126. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17127. "Fill a LaTeX header template TPL.
  17128. In the template, the following place holders will be recognized:
  17129. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17130. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17131. [PACKAGES] \\usepackage statements for PKG
  17132. [NO-PACKAGES] do not include PKG
  17133. [EXTRA] the string EXTRA
  17134. [NO-EXTRA] do not include EXTRA
  17135. For backward compatibility, if both the positive and the negative place
  17136. holder is missing, the positive one (without the \"NO-\") will be
  17137. assumed to be present at the end of the template.
  17138. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17139. EXTRA is a string.
  17140. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17141. (let (rpl (end ""))
  17142. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17143. (setq rpl (if (or (match-end 1) (not def-pkg))
  17144. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17145. tpl (replace-match rpl t t tpl))
  17146. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17147. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17148. (setq rpl (if (or (match-end 1) (not pkg))
  17149. "" (org-latex-packages-to-string pkg snippets-p t))
  17150. tpl (replace-match rpl t t tpl))
  17151. (if pkg (setq end
  17152. (concat end "\n"
  17153. (org-latex-packages-to-string pkg snippets-p)))))
  17154. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17155. (setq rpl (if (or (match-end 1) (not extra))
  17156. "" (concat extra "\n"))
  17157. tpl (replace-match rpl t t tpl))
  17158. (if (and extra (string-match "\\S-" extra))
  17159. (setq end (concat end "\n" extra))))
  17160. (if (string-match "\\S-" end)
  17161. (concat tpl "\n" end)
  17162. tpl)))
  17163. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17164. "Turn an alist of packages into a string with the \\usepackage macros."
  17165. (setq pkg (mapconcat (lambda(p)
  17166. (cond
  17167. ((stringp p) p)
  17168. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17169. (format "%% Package %s omitted" (cadr p)))
  17170. ((equal "" (car p))
  17171. (format "\\usepackage{%s}" (cadr p)))
  17172. (t
  17173. (format "\\usepackage[%s]{%s}"
  17174. (car p) (cadr p)))))
  17175. pkg
  17176. "\n"))
  17177. (if newline (concat pkg "\n") pkg))
  17178. (defun org-dvipng-color (attr)
  17179. "Return a RGB color specification for dvipng."
  17180. (apply 'format "rgb %s %s %s"
  17181. (mapcar 'org-normalize-color
  17182. (if (featurep 'xemacs)
  17183. (color-rgb-components
  17184. (face-property 'default
  17185. (cond ((eq attr :foreground) 'foreground)
  17186. ((eq attr :background) 'background))))
  17187. (color-values (face-attribute 'default attr nil))))))
  17188. (defun org-dvipng-color-format (color-name)
  17189. "Convert COLOR-NAME to a RGB color value for dvipng."
  17190. (apply 'format "rgb %s %s %s"
  17191. (mapcar 'org-normalize-color
  17192. (color-values color-name))))
  17193. (defun org-latex-color (attr)
  17194. "Return a RGB color for the LaTeX color package."
  17195. (apply 'format "%s,%s,%s"
  17196. (mapcar 'org-normalize-color
  17197. (if (featurep 'xemacs)
  17198. (color-rgb-components
  17199. (face-property 'default
  17200. (cond ((eq attr :foreground) 'foreground)
  17201. ((eq attr :background) 'background))))
  17202. (color-values (face-attribute 'default attr nil))))))
  17203. (defun org-latex-color-format (color-name)
  17204. "Convert COLOR-NAME to a RGB color value."
  17205. (apply 'format "%s,%s,%s"
  17206. (mapcar 'org-normalize-color
  17207. (color-values color-name))))
  17208. (defun org-normalize-color (value)
  17209. "Return string to be used as color value for an RGB component."
  17210. (format "%g" (/ value 65535.0)))
  17211. ;; Image display
  17212. (defvar org-inline-image-overlays nil)
  17213. (make-variable-buffer-local 'org-inline-image-overlays)
  17214. (defun org-toggle-inline-images (&optional include-linked)
  17215. "Toggle the display of inline images.
  17216. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17217. (interactive "P")
  17218. (if org-inline-image-overlays
  17219. (progn
  17220. (org-remove-inline-images)
  17221. (message "Inline image display turned off"))
  17222. (org-display-inline-images include-linked)
  17223. (if (and (org-called-interactively-p)
  17224. org-inline-image-overlays)
  17225. (message "%d images displayed inline"
  17226. (length org-inline-image-overlays))
  17227. (message "No images to display inline"))))
  17228. (defun org-redisplay-inline-images ()
  17229. "Refresh the display of inline images."
  17230. (interactive)
  17231. (if (not org-inline-image-overlays)
  17232. (org-toggle-inline-images)
  17233. (org-toggle-inline-images)
  17234. (org-toggle-inline-images)))
  17235. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17236. "Display inline images.
  17237. Normally only links without a description part are inlined, because this
  17238. is how it will work for export. When INCLUDE-LINKED is set, also links
  17239. with a description part will be inlined. This can be nice for a quick
  17240. look at those images, but it does not reflect what exported files will look
  17241. like.
  17242. When REFRESH is set, refresh existing images between BEG and END.
  17243. This will create new image displays only if necessary.
  17244. BEG and END default to the buffer boundaries."
  17245. (interactive "P")
  17246. (when (display-graphic-p)
  17247. (unless refresh
  17248. (org-remove-inline-images)
  17249. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17250. (save-excursion
  17251. (save-restriction
  17252. (widen)
  17253. (setq beg (or beg (point-min)) end (or end (point-max)))
  17254. (goto-char beg)
  17255. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17256. (substring (org-image-file-name-regexp) 0 -2)
  17257. "\\)\\]" (if include-linked "" "\\]")))
  17258. (case-fold-search t)
  17259. old file ov img type attrwidth width)
  17260. (while (re-search-forward re end t)
  17261. (setq old (get-char-property-and-overlay (match-beginning 1)
  17262. 'org-image-overlay)
  17263. file (expand-file-name
  17264. (concat (or (match-string 3) "") (match-string 4))))
  17265. (when (image-type-available-p 'imagemagick)
  17266. (setq attrwidth (if (or (listp org-image-actual-width)
  17267. (null org-image-actual-width))
  17268. (save-excursion
  17269. (save-match-data
  17270. (when (re-search-backward
  17271. "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
  17272. (save-excursion
  17273. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17274. (string-to-number (match-string 1))))))
  17275. width (cond ((eq org-image-actual-width t) nil)
  17276. ((null org-image-actual-width) attrwidth)
  17277. ((numberp org-image-actual-width)
  17278. org-image-actual-width)
  17279. ((listp org-image-actual-width)
  17280. (or attrwidth (car org-image-actual-width))))
  17281. type (if width 'imagemagick)))
  17282. (when (file-exists-p file)
  17283. (if (and (car-safe old) refresh)
  17284. (image-refresh (overlay-get (cdr old) 'display))
  17285. (setq img (save-match-data (create-image file type nil :width width)))
  17286. (when img
  17287. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17288. (overlay-put ov 'display img)
  17289. (overlay-put ov 'face 'default)
  17290. (overlay-put ov 'org-image-overlay t)
  17291. (overlay-put ov 'modification-hooks
  17292. (list 'org-display-inline-remove-overlay))
  17293. (push ov org-inline-image-overlays))))))))))
  17294. (define-obsolete-function-alias
  17295. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17296. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17297. "Remove inline-display overlay if a corresponding region is modified."
  17298. (let ((inhibit-modification-hooks t))
  17299. (when (and ov after)
  17300. (delete ov org-inline-image-overlays)
  17301. (delete-overlay ov))))
  17302. (defun org-remove-inline-images ()
  17303. "Remove inline display of images."
  17304. (interactive)
  17305. (mapc 'delete-overlay org-inline-image-overlays)
  17306. (setq org-inline-image-overlays nil))
  17307. ;;;; Key bindings
  17308. ;; Outline functions from `outline-mode-prefix-map'
  17309. ;; that can be remapped in Org:
  17310. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17311. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17312. (define-key org-mode-map [remap outline-forward-same-level]
  17313. 'org-forward-heading-same-level)
  17314. (define-key org-mode-map [remap outline-backward-same-level]
  17315. 'org-backward-heading-same-level)
  17316. (define-key org-mode-map [remap show-branches]
  17317. 'org-kill-note-or-show-branches)
  17318. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17319. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17320. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17321. ;; Outline functions from `outline-mode-prefix-map' that can not
  17322. ;; be remapped in Org:
  17323. ;;
  17324. ;; - the column "key binding" shows whether the Outline function is still
  17325. ;; available in Org mode on the same key that it has been bound to in
  17326. ;; Outline mode:
  17327. ;; - "overridden": key used for a different functionality in Org mode
  17328. ;; - else: key still bound to the same Outline function in Org mode
  17329. ;;
  17330. ;; | Outline function | key binding | Org replacement |
  17331. ;; |------------------------------------+-------------+-----------------------|
  17332. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17333. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17334. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17335. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17336. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17337. ;; | `show-entry' | overridden | no replacement |
  17338. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17339. ;; | `show-branches' | `C-c C-k' | still same function |
  17340. ;; | `show-subtree' | overridden | visibility cycling |
  17341. ;; | `show-all' | overridden | no replacement |
  17342. ;; | `hide-subtree' | overridden | visibility cycling |
  17343. ;; | `hide-body' | overridden | no replacement |
  17344. ;; | `hide-entry' | overridden | visibility cycling |
  17345. ;; | `hide-leaves' | overridden | no replacement |
  17346. ;; | `hide-sublevels' | overridden | no replacement |
  17347. ;; | `hide-other' | overridden | no replacement |
  17348. ;; Make `C-c C-x' a prefix key
  17349. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17350. ;; TAB key with modifiers
  17351. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17352. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17353. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17354. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17355. ;; The following line is necessary under Suse GNU/Linux
  17356. (unless (featurep 'xemacs)
  17357. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17358. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17359. (define-key org-mode-map [backtab] 'org-shifttab)
  17360. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17361. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17362. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17363. ;; Cursor keys with modifiers
  17364. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17365. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17366. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17367. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17368. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17369. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17370. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17371. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17372. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17373. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17374. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17375. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17376. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17377. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17378. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17379. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17380. ;; Babel keys
  17381. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17382. (mapc (lambda (pair)
  17383. (define-key org-babel-map (car pair) (cdr pair)))
  17384. org-babel-key-bindings)
  17385. ;;; Extra keys for tty access.
  17386. ;; We only set them when really needed because otherwise the
  17387. ;; menus don't show the simple keys
  17388. (when (or org-use-extra-keys
  17389. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17390. (not window-system))
  17391. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17392. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17393. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17394. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17395. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17396. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17397. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17398. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17399. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17400. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17401. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17402. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17403. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17404. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17405. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17406. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17407. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17408. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17409. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17410. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17411. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17412. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17413. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17414. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17415. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17416. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17417. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17418. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17419. ;; All the other keys
  17420. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17421. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17422. (if (boundp 'narrow-map)
  17423. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17424. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17425. (if (boundp 'narrow-map)
  17426. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17427. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17428. (if (boundp 'narrow-map)
  17429. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17430. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17431. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17432. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17433. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17434. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17435. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17436. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17437. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17438. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17439. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17440. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17441. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17442. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17443. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17444. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17445. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17446. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17447. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17448. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17449. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17450. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17451. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17452. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17453. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17454. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17455. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17456. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17457. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17458. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17459. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17460. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17461. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17462. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17463. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17464. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17465. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17466. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17467. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17468. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17469. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17470. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17471. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17472. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17473. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17474. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17475. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17476. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17477. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17478. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17479. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17480. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17481. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17482. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17483. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17484. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17485. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17486. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17487. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17488. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17489. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17490. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17491. (org-defkey org-mode-map "\C-m" 'org-return)
  17492. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17493. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17494. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17495. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17496. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17497. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17498. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17499. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17500. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17501. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17502. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17503. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17504. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17505. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17506. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17507. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17508. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17509. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17510. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17511. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17512. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17513. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17514. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17515. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17516. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17517. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17518. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17519. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17520. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17521. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17522. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17523. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17524. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17525. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17526. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17527. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17528. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17529. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17530. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17531. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17532. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17533. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17534. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17535. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17536. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17537. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17538. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17539. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17540. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17541. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17542. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17543. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17544. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17545. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17546. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17547. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17548. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17549. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17550. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17551. (when (featurep 'xemacs)
  17552. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17553. (defconst org-speed-commands-default
  17554. '(
  17555. ("Outline Navigation")
  17556. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17557. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17558. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17559. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17560. ("F" . org-next-block)
  17561. ("B" . org-previous-block)
  17562. ("u" . (org-speed-move-safe 'outline-up-heading))
  17563. ("j" . org-goto)
  17564. ("g" . (org-refile t))
  17565. ("Outline Visibility")
  17566. ("c" . org-cycle)
  17567. ("C" . org-shifttab)
  17568. (" " . org-display-outline-path)
  17569. ("s" . org-narrow-to-subtree)
  17570. ("=" . org-columns)
  17571. ("Outline Structure Editing")
  17572. ("U" . org-shiftmetaup)
  17573. ("D" . org-shiftmetadown)
  17574. ("r" . org-metaright)
  17575. ("l" . org-metaleft)
  17576. ("R" . org-shiftmetaright)
  17577. ("L" . org-shiftmetaleft)
  17578. ("i" . (progn (forward-char 1) (call-interactively
  17579. 'org-insert-heading-respect-content)))
  17580. ("^" . org-sort)
  17581. ("w" . org-refile)
  17582. ("a" . org-archive-subtree-default-with-confirmation)
  17583. ("@" . org-mark-subtree)
  17584. ("#" . org-toggle-comment)
  17585. ("Clock Commands")
  17586. ("I" . org-clock-in)
  17587. ("O" . org-clock-out)
  17588. ("Meta Data Editing")
  17589. ("t" . org-todo)
  17590. ("," . (org-priority))
  17591. ("0" . (org-priority ?\ ))
  17592. ("1" . (org-priority ?A))
  17593. ("2" . (org-priority ?B))
  17594. ("3" . (org-priority ?C))
  17595. (":" . org-set-tags-command)
  17596. ("e" . org-set-effort)
  17597. ("E" . org-inc-effort)
  17598. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17599. (org-entry-put (point) "APPT_WARNTIME" m)))
  17600. ("Agenda Views etc")
  17601. ("v" . org-agenda)
  17602. ("/" . org-sparse-tree)
  17603. ("Misc")
  17604. ("o" . org-open-at-point)
  17605. ("?" . org-speed-command-help)
  17606. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17607. (">" . (org-agenda-remove-restriction-lock))
  17608. )
  17609. "The default speed commands.")
  17610. (defun org-print-speed-command (e)
  17611. (if (> (length (car e)) 1)
  17612. (progn
  17613. (princ "\n")
  17614. (princ (car e))
  17615. (princ "\n")
  17616. (princ (make-string (length (car e)) ?-))
  17617. (princ "\n"))
  17618. (princ (car e))
  17619. (princ " ")
  17620. (if (symbolp (cdr e))
  17621. (princ (symbol-name (cdr e)))
  17622. (prin1 (cdr e)))
  17623. (princ "\n")))
  17624. (defun org-speed-command-help ()
  17625. "Show the available speed commands."
  17626. (interactive)
  17627. (if (not org-use-speed-commands)
  17628. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  17629. (with-output-to-temp-buffer "*Help*"
  17630. (princ "User-defined Speed commands\n===========================\n")
  17631. (mapc 'org-print-speed-command org-speed-commands-user)
  17632. (princ "\n")
  17633. (princ "Built-in Speed commands\n=======================\n")
  17634. (mapc 'org-print-speed-command org-speed-commands-default))
  17635. (with-current-buffer "*Help*"
  17636. (setq truncate-lines t))))
  17637. (defun org-speed-move-safe (cmd)
  17638. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17639. If not, return to the original position and throw an error."
  17640. (interactive)
  17641. (let ((pos (point)))
  17642. (call-interactively cmd)
  17643. (unless (and (bolp) (org-at-heading-p))
  17644. (goto-char pos)
  17645. (error "Boundary reached while executing %s" cmd))))
  17646. (defvar org-self-insert-command-undo-counter 0)
  17647. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17648. (defvar org-speed-command nil)
  17649. (define-obsolete-function-alias
  17650. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17651. (defun org-speed-command-activate (keys)
  17652. "Hook for activating single-letter speed commands.
  17653. `org-speed-commands-default' specifies a minimal command set.
  17654. Use `org-speed-commands-user' for further customization."
  17655. (when (or (and (bolp) (looking-at org-outline-regexp))
  17656. (and (functionp org-use-speed-commands)
  17657. (funcall org-use-speed-commands)))
  17658. (cdr (assoc keys (append org-speed-commands-user
  17659. org-speed-commands-default)))))
  17660. (define-obsolete-function-alias
  17661. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17662. (defun org-babel-speed-command-activate (keys)
  17663. "Hook for activating single-letter code block commands."
  17664. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17665. (cdr (assoc keys org-babel-key-bindings))))
  17666. (defcustom org-speed-command-hook
  17667. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17668. "Hook for activating speed commands at strategic locations.
  17669. Hook functions are called in sequence until a valid handler is
  17670. found.
  17671. Each hook takes a single argument, a user-pressed command key
  17672. which is also a `self-insert-command' from the global map.
  17673. Within the hook, examine the cursor position and the command key
  17674. and return nil or a valid handler as appropriate. Handler could
  17675. be one of an interactive command, a function, or a form.
  17676. Set `org-use-speed-commands' to non-nil value to enable this
  17677. hook. The default setting is `org-speed-command-activate'."
  17678. :group 'org-structure
  17679. :version "24.1"
  17680. :type 'hook)
  17681. (defun org-self-insert-command (N)
  17682. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17683. If the cursor is in a table looking at whitespace, the whitespace is
  17684. overwritten, and the table is not marked as requiring realignment."
  17685. (interactive "p")
  17686. (org-check-before-invisible-edit 'insert)
  17687. (cond
  17688. ((and org-use-speed-commands
  17689. (setq org-speed-command
  17690. (run-hook-with-args-until-success
  17691. 'org-speed-command-hook (this-command-keys))))
  17692. (cond
  17693. ((commandp org-speed-command)
  17694. (setq this-command org-speed-command)
  17695. (call-interactively org-speed-command))
  17696. ((functionp org-speed-command)
  17697. (funcall org-speed-command))
  17698. ((and org-speed-command (listp org-speed-command))
  17699. (eval org-speed-command))
  17700. (t (let (org-use-speed-commands)
  17701. (call-interactively 'org-self-insert-command)))))
  17702. ((and
  17703. (org-table-p)
  17704. (progn
  17705. ;; check if we blank the field, and if that triggers align
  17706. (and (featurep 'org-table) org-table-auto-blank-field
  17707. (member last-command
  17708. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17709. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17710. ;; got extra space, this field does not determine column width
  17711. (let (org-table-may-need-update) (org-table-blank-field))
  17712. ;; no extra space, this field may determine column width
  17713. (org-table-blank-field)))
  17714. t)
  17715. (eq N 1)
  17716. (looking-at "[^|\n]* |"))
  17717. (let (org-table-may-need-update)
  17718. (goto-char (1- (match-end 0)))
  17719. (backward-delete-char 1)
  17720. (goto-char (match-beginning 0))
  17721. (self-insert-command N)))
  17722. (t
  17723. (setq org-table-may-need-update t)
  17724. (self-insert-command N)
  17725. (org-fix-tags-on-the-fly)
  17726. (if org-self-insert-cluster-for-undo
  17727. (if (not (eq last-command 'org-self-insert-command))
  17728. (setq org-self-insert-command-undo-counter 1)
  17729. (if (>= org-self-insert-command-undo-counter 20)
  17730. (setq org-self-insert-command-undo-counter 1)
  17731. (and (> org-self-insert-command-undo-counter 0)
  17732. buffer-undo-list (listp buffer-undo-list)
  17733. (not (cadr buffer-undo-list)) ; remove nil entry
  17734. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17735. (setq org-self-insert-command-undo-counter
  17736. (1+ org-self-insert-command-undo-counter))))))))
  17737. (defun org-check-before-invisible-edit (kind)
  17738. "Check is editing if kind KIND would be dangerous with invisible text around.
  17739. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17740. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17741. (if (and org-catch-invisible-edits
  17742. (or (not (boundp 'visible-mode)) (not visible-mode))
  17743. (or (get-char-property (point) 'invisible)
  17744. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17745. ;; OK, we need to take a closer look
  17746. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17747. (invisible-before-point (if (bobp) nil (get-char-property
  17748. (1- (point)) 'invisible)))
  17749. (border-and-ok-direction
  17750. (or
  17751. ;; Check if we are acting predictably before invisible text
  17752. (and invisible-at-point (not invisible-before-point)
  17753. (memq kind '(insert delete-backward)))
  17754. ;; Check if we are acting predictably after invisible text
  17755. ;; This works not well, and I have turned it off. It seems
  17756. ;; better to always show and stop after invisible text.
  17757. ;; (and (not invisible-at-point) invisible-before-point
  17758. ;; (memq kind '(insert delete)))
  17759. )))
  17760. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17761. (memq invisible-before-point '(outline org-hide-block t)))
  17762. (if (eq org-catch-invisible-edits 'error)
  17763. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  17764. (if (and org-custom-properties-overlays
  17765. (y-or-n-p "Display invisible properties in this buffer? "))
  17766. (org-toggle-custom-properties-visibility)
  17767. ;; Make the area visible
  17768. (save-excursion
  17769. (if invisible-before-point
  17770. (goto-char (previous-single-char-property-change
  17771. (point) 'invisible)))
  17772. (org-cycle))
  17773. (cond
  17774. ((eq org-catch-invisible-edits 'show)
  17775. ;; That's it, we do the edit after showing
  17776. (message
  17777. "Unfolding invisible region around point before editing")
  17778. (sit-for 1))
  17779. ((and (eq org-catch-invisible-edits 'smart)
  17780. border-and-ok-direction)
  17781. (message "Unfolding invisible region around point before editing"))
  17782. (t
  17783. ;; Don't do the edit, make the user repeat it in full visibility
  17784. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17785. (defun org-fix-tags-on-the-fly ()
  17786. (when (and (equal (char-after (point-at-bol)) ?*)
  17787. (org-at-heading-p))
  17788. (org-align-tags-here org-tags-column)))
  17789. (defun org-delete-backward-char (N)
  17790. "Like `delete-backward-char', insert whitespace at field end in tables.
  17791. When deleting backwards, in tables this function will insert whitespace in
  17792. front of the next \"|\" separator, to keep the table aligned. The table will
  17793. still be marked for re-alignment if the field did fill the entire column,
  17794. because, in this case the deletion might narrow the column."
  17795. (interactive "p")
  17796. (save-match-data
  17797. (org-check-before-invisible-edit 'delete-backward)
  17798. (if (and (org-table-p)
  17799. (eq N 1)
  17800. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17801. (looking-at ".*?|"))
  17802. (let ((pos (point))
  17803. (noalign (looking-at "[^|\n\r]* |"))
  17804. (c org-table-may-need-update))
  17805. (backward-delete-char N)
  17806. (if (not overwrite-mode)
  17807. (progn
  17808. (skip-chars-forward "^|")
  17809. (insert " ")
  17810. (goto-char (1- pos))))
  17811. ;; noalign: if there were two spaces at the end, this field
  17812. ;; does not determine the width of the column.
  17813. (if noalign (setq org-table-may-need-update c)))
  17814. (backward-delete-char N)
  17815. (org-fix-tags-on-the-fly))))
  17816. (defun org-delete-char (N)
  17817. "Like `delete-char', but insert whitespace at field end in tables.
  17818. When deleting characters, in tables this function will insert whitespace in
  17819. front of the next \"|\" separator, to keep the table aligned. The table will
  17820. still be marked for re-alignment if the field did fill the entire column,
  17821. because, in this case the deletion might narrow the column."
  17822. (interactive "p")
  17823. (save-match-data
  17824. (org-check-before-invisible-edit 'delete)
  17825. (if (and (org-table-p)
  17826. (not (bolp))
  17827. (not (= (char-after) ?|))
  17828. (eq N 1))
  17829. (if (looking-at ".*?|")
  17830. (let ((pos (point))
  17831. (noalign (looking-at "[^|\n\r]* |"))
  17832. (c org-table-may-need-update))
  17833. (replace-match
  17834. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17835. (goto-char pos)
  17836. ;; noalign: if there were two spaces at the end, this field
  17837. ;; does not determine the width of the column.
  17838. (if noalign (setq org-table-may-need-update c)))
  17839. (delete-char N))
  17840. (delete-char N)
  17841. (org-fix-tags-on-the-fly))))
  17842. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17843. (put 'org-self-insert-command 'delete-selection t)
  17844. (put 'orgtbl-self-insert-command 'delete-selection t)
  17845. (put 'org-delete-char 'delete-selection 'supersede)
  17846. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17847. (put 'org-yank 'delete-selection 'yank)
  17848. ;; Make `flyspell-mode' delay after some commands
  17849. (put 'org-self-insert-command 'flyspell-delayed t)
  17850. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17851. (put 'org-delete-char 'flyspell-delayed t)
  17852. (put 'org-delete-backward-char 'flyspell-delayed t)
  17853. ;; Make pabbrev-mode expand after org-mode commands
  17854. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17855. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17856. ;; How to do this: Measure non-white length of current string
  17857. ;; If equal to column width, we should realign.
  17858. (defun org-remap (map &rest commands)
  17859. "In MAP, remap the functions given in COMMANDS.
  17860. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17861. (let (new old)
  17862. (while commands
  17863. (setq old (pop commands) new (pop commands))
  17864. (if (fboundp 'command-remapping)
  17865. (org-defkey map (vector 'remap old) new)
  17866. (substitute-key-definition old new map global-map)))))
  17867. (defun org-transpose-words ()
  17868. "Transpose words for Org.
  17869. This uses the `org-mode-transpose-word-syntax-table' syntax
  17870. table, which interprets characters in `org-emphasis-alist' as
  17871. word constituants."
  17872. (interactive)
  17873. (with-syntax-table org-mode-transpose-word-syntax-table
  17874. (call-interactively 'transpose-words)))
  17875. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17876. (when (eq org-enable-table-editor 'optimized)
  17877. ;; If the user wants maximum table support, we need to hijack
  17878. ;; some standard editing functions
  17879. (org-remap org-mode-map
  17880. 'self-insert-command 'org-self-insert-command
  17881. 'delete-char 'org-delete-char
  17882. 'delete-backward-char 'org-delete-backward-char)
  17883. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17884. (defvar org-ctrl-c-ctrl-c-hook nil
  17885. "Hook for functions attaching themselves to `C-c C-c'.
  17886. This can be used to add additional functionality to the C-c C-c
  17887. key which executes context-dependent commands. This hook is run
  17888. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17889. run after the last test.
  17890. Each function will be called with no arguments. The function
  17891. must check if the context is appropriate for it to act. If yes,
  17892. it should do its thing and then return a non-nil value. If the
  17893. context is wrong, just do nothing and return nil.")
  17894. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17895. "Hook for functions attaching themselves to `C-c C-c'.
  17896. This can be used to add additional functionality to the C-c C-c
  17897. key which executes context-dependent commands. This hook is run
  17898. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17899. before the first test.
  17900. Each function will be called with no arguments. The function
  17901. must check if the context is appropriate for it to act. If yes,
  17902. it should do its thing and then return a non-nil value. If the
  17903. context is wrong, just do nothing and return nil.")
  17904. (defvar org-tab-first-hook nil
  17905. "Hook for functions to attach themselves to TAB.
  17906. See `org-ctrl-c-ctrl-c-hook' for more information.
  17907. This hook runs as the first action when TAB is pressed, even before
  17908. `org-cycle' messes around with the `outline-regexp' to cater for
  17909. inline tasks and plain list item folding.
  17910. If any function in this hook returns t, any other actions that
  17911. would have been caused by TAB (such as table field motion or visibility
  17912. cycling) will not occur.")
  17913. (defvar org-tab-after-check-for-table-hook nil
  17914. "Hook for functions to attach themselves to TAB.
  17915. See `org-ctrl-c-ctrl-c-hook' for more information.
  17916. This hook runs after it has been established that the cursor is not in a
  17917. table, but before checking if the cursor is in a headline or if global cycling
  17918. should be done.
  17919. If any function in this hook returns t, not other actions like visibility
  17920. cycling will be done.")
  17921. (defvar org-tab-after-check-for-cycling-hook nil
  17922. "Hook for functions to attach themselves to TAB.
  17923. See `org-ctrl-c-ctrl-c-hook' for more information.
  17924. This hook runs after it has been established that not table field motion and
  17925. not visibility should be done because of current context. This is probably
  17926. the place where a package like yasnippets can hook in.")
  17927. (defvar org-tab-before-tab-emulation-hook nil
  17928. "Hook for functions to attach themselves to TAB.
  17929. See `org-ctrl-c-ctrl-c-hook' for more information.
  17930. This hook runs after every other options for TAB have been exhausted, but
  17931. before indentation and \t insertion takes place.")
  17932. (defvar org-metaleft-hook nil
  17933. "Hook for functions attaching themselves to `M-left'.
  17934. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17935. (defvar org-metaright-hook nil
  17936. "Hook for functions attaching themselves to `M-right'.
  17937. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17938. (defvar org-metaup-hook nil
  17939. "Hook for functions attaching themselves to `M-up'.
  17940. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17941. (defvar org-metadown-hook nil
  17942. "Hook for functions attaching themselves to `M-down'.
  17943. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17944. (defvar org-shiftmetaleft-hook nil
  17945. "Hook for functions attaching themselves to `M-S-left'.
  17946. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17947. (defvar org-shiftmetaright-hook nil
  17948. "Hook for functions attaching themselves to `M-S-right'.
  17949. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17950. (defvar org-shiftmetaup-hook nil
  17951. "Hook for functions attaching themselves to `M-S-up'.
  17952. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17953. (defvar org-shiftmetadown-hook nil
  17954. "Hook for functions attaching themselves to `M-S-down'.
  17955. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17956. (defvar org-metareturn-hook nil
  17957. "Hook for functions attaching themselves to `M-RET'.
  17958. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17959. (defvar org-shiftup-hook nil
  17960. "Hook for functions attaching themselves to `S-up'.
  17961. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17962. (defvar org-shiftup-final-hook nil
  17963. "Hook for functions attaching themselves to `S-up'.
  17964. This one runs after all other options except shift-select have been excluded.
  17965. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17966. (defvar org-shiftdown-hook nil
  17967. "Hook for functions attaching themselves to `S-down'.
  17968. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17969. (defvar org-shiftdown-final-hook nil
  17970. "Hook for functions attaching themselves to `S-down'.
  17971. This one runs after all other options except shift-select have been excluded.
  17972. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17973. (defvar org-shiftleft-hook nil
  17974. "Hook for functions attaching themselves to `S-left'.
  17975. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17976. (defvar org-shiftleft-final-hook nil
  17977. "Hook for functions attaching themselves to `S-left'.
  17978. This one runs after all other options except shift-select have been excluded.
  17979. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17980. (defvar org-shiftright-hook nil
  17981. "Hook for functions attaching themselves to `S-right'.
  17982. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17983. (defvar org-shiftright-final-hook nil
  17984. "Hook for functions attaching themselves to `S-right'.
  17985. This one runs after all other options except shift-select have been excluded.
  17986. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17987. (defun org-modifier-cursor-error ()
  17988. "Throw an error, a modified cursor command was applied in wrong context."
  17989. (user-error "This command is active in special context like tables, headlines or items"))
  17990. (defun org-shiftselect-error ()
  17991. "Throw an error because Shift-Cursor command was applied in wrong context."
  17992. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17993. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17994. (user-error "This command works only in special context like headlines or timestamps")))
  17995. (defun org-call-for-shift-select (cmd)
  17996. (let ((this-command-keys-shift-translated t))
  17997. (call-interactively cmd)))
  17998. (defun org-shifttab (&optional arg)
  17999. "Global visibility cycling or move to previous table field.
  18000. Call `org-table-previous-field' within a table.
  18001. When ARG is nil, cycle globally through visibility states.
  18002. When ARG is a numeric prefix, show contents of this level."
  18003. (interactive "P")
  18004. (cond
  18005. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18006. ((integerp arg)
  18007. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18008. (message "Content view to level: %d" arg)
  18009. (org-content (prefix-numeric-value arg2))
  18010. (org-cycle-show-empty-lines t)
  18011. (setq org-cycle-global-status 'overview)))
  18012. (t (call-interactively 'org-global-cycle))))
  18013. (defun org-shiftmetaleft ()
  18014. "Promote subtree or delete table column.
  18015. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18016. `org-table-delete-column', depending on context. See the
  18017. individual commands for more information."
  18018. (interactive)
  18019. (cond
  18020. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18021. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18022. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18023. ((if (not (org-region-active-p)) (org-at-item-p)
  18024. (save-excursion (goto-char (region-beginning))
  18025. (org-at-item-p)))
  18026. (call-interactively 'org-outdent-item-tree))
  18027. (t (org-modifier-cursor-error))))
  18028. (defun org-shiftmetaright ()
  18029. "Demote subtree or insert table column.
  18030. Calls `org-demote-subtree', `org-indent-item-tree', or
  18031. `org-table-insert-column', depending on context. See the
  18032. individual commands for more information."
  18033. (interactive)
  18034. (cond
  18035. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18036. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18037. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18038. ((if (not (org-region-active-p)) (org-at-item-p)
  18039. (save-excursion (goto-char (region-beginning))
  18040. (org-at-item-p)))
  18041. (call-interactively 'org-indent-item-tree))
  18042. (t (org-modifier-cursor-error))))
  18043. (defun org-shiftmetaup (&optional arg)
  18044. "Move subtree up or kill table row.
  18045. Calls `org-move-subtree-up' or `org-table-kill-row' or
  18046. `org-move-item-up' or `org-timestamp-up', depending on context.
  18047. See the individual commands for more information."
  18048. (interactive "P")
  18049. (cond
  18050. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18051. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18052. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18053. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18054. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18055. (call-interactively 'org-timestamp-up)))
  18056. (t (call-interactively 'org-drag-line-backward))))
  18057. (defun org-shiftmetadown (&optional arg)
  18058. "Move subtree down or insert table row.
  18059. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18060. `org-move-item-down' or `org-timestamp-up', depending on context.
  18061. See the individual commands for more information."
  18062. (interactive "P")
  18063. (cond
  18064. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18065. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18066. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18067. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18068. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18069. (call-interactively 'org-timestamp-down)))
  18070. (t (call-interactively 'org-drag-line-forward))))
  18071. (defsubst org-hidden-tree-error ()
  18072. (user-error
  18073. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18074. (defun org-metaleft (&optional arg)
  18075. "Promote heading or move table column to left.
  18076. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18077. With no specific context, calls the Emacs default `backward-word'.
  18078. See the individual commands for more information."
  18079. (interactive "P")
  18080. (cond
  18081. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18082. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18083. ((org-with-limited-levels
  18084. (or (org-at-heading-p)
  18085. (and (org-region-active-p)
  18086. (save-excursion
  18087. (goto-char (region-beginning))
  18088. (org-at-heading-p)))))
  18089. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18090. (call-interactively 'org-do-promote))
  18091. ;; At an inline task.
  18092. ((org-at-heading-p)
  18093. (call-interactively 'org-inlinetask-promote))
  18094. ((or (org-at-item-p)
  18095. (and (org-region-active-p)
  18096. (save-excursion
  18097. (goto-char (region-beginning))
  18098. (org-at-item-p))))
  18099. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18100. (call-interactively 'org-outdent-item))
  18101. (t (call-interactively 'backward-word))))
  18102. (defun org-metaright (&optional arg)
  18103. "Demote a subtree, a list item or move table column to right.
  18104. In front of a drawer or a block keyword, indent it correctly.
  18105. With no specific context, calls the Emacs default `forward-word'.
  18106. See the individual commands for more information."
  18107. (interactive "P")
  18108. (cond
  18109. ((run-hook-with-args-until-success 'org-metaright-hook))
  18110. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18111. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18112. ((org-at-block-p) (call-interactively 'org-indent-block))
  18113. ((org-with-limited-levels
  18114. (or (org-at-heading-p)
  18115. (and (org-region-active-p)
  18116. (save-excursion
  18117. (goto-char (region-beginning))
  18118. (org-at-heading-p)))))
  18119. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18120. (call-interactively 'org-do-demote))
  18121. ;; At an inline task.
  18122. ((org-at-heading-p)
  18123. (call-interactively 'org-inlinetask-demote))
  18124. ((or (org-at-item-p)
  18125. (and (org-region-active-p)
  18126. (save-excursion
  18127. (goto-char (region-beginning))
  18128. (org-at-item-p))))
  18129. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18130. (call-interactively 'org-indent-item))
  18131. (t (call-interactively 'forward-word))))
  18132. (defun org-check-for-hidden (what)
  18133. "Check if there are hidden headlines/items in the current visual line.
  18134. WHAT can be either `headlines' or `items'. If the current line is
  18135. an outline or item heading and it has a folded subtree below it,
  18136. this function returns t, nil otherwise."
  18137. (let ((re (cond
  18138. ((eq what 'headlines) org-outline-regexp-bol)
  18139. ((eq what 'items) (org-item-beginning-re))
  18140. (t (error "This should not happen"))))
  18141. beg end)
  18142. (save-excursion
  18143. (catch 'exit
  18144. (unless (org-region-active-p)
  18145. (setq beg (point-at-bol))
  18146. (beginning-of-line 2)
  18147. (while (and (not (eobp)) ;; this is like `next-line'
  18148. (get-char-property (1- (point)) 'invisible))
  18149. (beginning-of-line 2))
  18150. (setq end (point))
  18151. (goto-char beg)
  18152. (goto-char (point-at-eol))
  18153. (setq end (max end (point)))
  18154. (while (re-search-forward re end t)
  18155. (if (get-char-property (match-beginning 0) 'invisible)
  18156. (throw 'exit t))))
  18157. nil))))
  18158. (defun org-metaup (&optional arg)
  18159. "Move subtree up or move table row up.
  18160. Calls `org-move-subtree-up' or `org-table-move-row' or
  18161. `org-move-item-up', depending on context. See the individual commands
  18162. for more information."
  18163. (interactive "P")
  18164. (cond
  18165. ((run-hook-with-args-until-success 'org-metaup-hook))
  18166. ((org-region-active-p)
  18167. (let* ((a (min (region-beginning) (region-end)))
  18168. (b (1- (max (region-beginning) (region-end))))
  18169. (c (save-excursion (goto-char a)
  18170. (move-beginning-of-line 0)))
  18171. (d (save-excursion (goto-char a)
  18172. (move-end-of-line 0) (point))))
  18173. (transpose-regions a b c d)
  18174. (goto-char c)))
  18175. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18176. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18177. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18178. (t (org-drag-element-backward))))
  18179. (defun org-metadown (&optional arg)
  18180. "Move subtree down or move table row down.
  18181. Calls `org-move-subtree-down' or `org-table-move-row' or
  18182. `org-move-item-down', depending on context. See the individual
  18183. commands for more information."
  18184. (interactive "P")
  18185. (cond
  18186. ((run-hook-with-args-until-success 'org-metadown-hook))
  18187. ((org-region-active-p)
  18188. (let* ((a (min (region-beginning) (region-end)))
  18189. (b (max (region-beginning) (region-end)))
  18190. (c (save-excursion (goto-char b)
  18191. (move-beginning-of-line 1)))
  18192. (d (save-excursion (goto-char b)
  18193. (move-end-of-line 1) (1+ (point)))))
  18194. (transpose-regions a b c d)
  18195. (goto-char d)))
  18196. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18197. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18198. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18199. (t (org-drag-element-forward))))
  18200. (defun org-shiftup (&optional arg)
  18201. "Increase item in timestamp or increase priority of current headline.
  18202. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18203. depending on context. See the individual commands for more information."
  18204. (interactive "P")
  18205. (cond
  18206. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18207. ((and org-support-shift-select (org-region-active-p))
  18208. (org-call-for-shift-select 'previous-line))
  18209. ((org-at-timestamp-p t)
  18210. (call-interactively (if org-edit-timestamp-down-means-later
  18211. 'org-timestamp-down 'org-timestamp-up)))
  18212. ((and (not (eq org-support-shift-select 'always))
  18213. org-enable-priority-commands
  18214. (org-at-heading-p))
  18215. (call-interactively 'org-priority-up))
  18216. ((and (not org-support-shift-select) (org-at-item-p))
  18217. (call-interactively 'org-previous-item))
  18218. ((org-clocktable-try-shift 'up arg))
  18219. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18220. (org-support-shift-select
  18221. (org-call-for-shift-select 'previous-line))
  18222. (t (org-shiftselect-error))))
  18223. (defun org-shiftdown (&optional arg)
  18224. "Decrease item in timestamp or decrease priority of current headline.
  18225. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18226. depending on context. See the individual commands for more information."
  18227. (interactive "P")
  18228. (cond
  18229. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18230. ((and org-support-shift-select (org-region-active-p))
  18231. (org-call-for-shift-select 'next-line))
  18232. ((org-at-timestamp-p t)
  18233. (call-interactively (if org-edit-timestamp-down-means-later
  18234. 'org-timestamp-up 'org-timestamp-down)))
  18235. ((and (not (eq org-support-shift-select 'always))
  18236. org-enable-priority-commands
  18237. (org-at-heading-p))
  18238. (call-interactively 'org-priority-down))
  18239. ((and (not org-support-shift-select) (org-at-item-p))
  18240. (call-interactively 'org-next-item))
  18241. ((org-clocktable-try-shift 'down arg))
  18242. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18243. (org-support-shift-select
  18244. (org-call-for-shift-select 'next-line))
  18245. (t (org-shiftselect-error))))
  18246. (defun org-shiftright (&optional arg)
  18247. "Cycle the thing at point or in the current line, depending on context.
  18248. Depending on context, this does one of the following:
  18249. - switch a timestamp at point one day into the future
  18250. - on a headline, switch to the next TODO keyword.
  18251. - on an item, switch entire list to the next bullet type
  18252. - on a property line, switch to the next allowed value
  18253. - on a clocktable definition line, move time block into the future"
  18254. (interactive "P")
  18255. (cond
  18256. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18257. ((and org-support-shift-select (org-region-active-p))
  18258. (org-call-for-shift-select 'forward-char))
  18259. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18260. ((and (not (eq org-support-shift-select 'always))
  18261. (org-at-heading-p))
  18262. (let ((org-inhibit-logging
  18263. (not org-treat-S-cursor-todo-selection-as-state-change))
  18264. (org-inhibit-blocking
  18265. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18266. (org-call-with-arg 'org-todo 'right)))
  18267. ((or (and org-support-shift-select
  18268. (not (eq org-support-shift-select 'always))
  18269. (org-at-item-bullet-p))
  18270. (and (not org-support-shift-select) (org-at-item-p)))
  18271. (org-call-with-arg 'org-cycle-list-bullet nil))
  18272. ((and (not (eq org-support-shift-select 'always))
  18273. (org-at-property-p))
  18274. (call-interactively 'org-property-next-allowed-value))
  18275. ((org-clocktable-try-shift 'right arg))
  18276. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18277. (org-support-shift-select
  18278. (org-call-for-shift-select 'forward-char))
  18279. (t (org-shiftselect-error))))
  18280. (defun org-shiftleft (&optional arg)
  18281. "Cycle the thing at point or in the current line, depending on context.
  18282. Depending on context, this does one of the following:
  18283. - switch a timestamp at point one day into the past
  18284. - on a headline, switch to the previous TODO keyword.
  18285. - on an item, switch entire list to the previous bullet type
  18286. - on a property line, switch to the previous allowed value
  18287. - on a clocktable definition line, move time block into the past"
  18288. (interactive "P")
  18289. (cond
  18290. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18291. ((and org-support-shift-select (org-region-active-p))
  18292. (org-call-for-shift-select 'backward-char))
  18293. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18294. ((and (not (eq org-support-shift-select 'always))
  18295. (org-at-heading-p))
  18296. (let ((org-inhibit-logging
  18297. (not org-treat-S-cursor-todo-selection-as-state-change))
  18298. (org-inhibit-blocking
  18299. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18300. (org-call-with-arg 'org-todo 'left)))
  18301. ((or (and org-support-shift-select
  18302. (not (eq org-support-shift-select 'always))
  18303. (org-at-item-bullet-p))
  18304. (and (not org-support-shift-select) (org-at-item-p)))
  18305. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18306. ((and (not (eq org-support-shift-select 'always))
  18307. (org-at-property-p))
  18308. (call-interactively 'org-property-previous-allowed-value))
  18309. ((org-clocktable-try-shift 'left arg))
  18310. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18311. (org-support-shift-select
  18312. (org-call-for-shift-select 'backward-char))
  18313. (t (org-shiftselect-error))))
  18314. (defun org-shiftcontrolright ()
  18315. "Switch to next TODO set."
  18316. (interactive)
  18317. (cond
  18318. ((and org-support-shift-select (org-region-active-p))
  18319. (org-call-for-shift-select 'forward-word))
  18320. ((and (not (eq org-support-shift-select 'always))
  18321. (org-at-heading-p))
  18322. (org-call-with-arg 'org-todo 'nextset))
  18323. (org-support-shift-select
  18324. (org-call-for-shift-select 'forward-word))
  18325. (t (org-shiftselect-error))))
  18326. (defun org-shiftcontrolleft ()
  18327. "Switch to previous TODO set."
  18328. (interactive)
  18329. (cond
  18330. ((and org-support-shift-select (org-region-active-p))
  18331. (org-call-for-shift-select 'backward-word))
  18332. ((and (not (eq org-support-shift-select 'always))
  18333. (org-at-heading-p))
  18334. (org-call-with-arg 'org-todo 'previousset))
  18335. (org-support-shift-select
  18336. (org-call-for-shift-select 'backward-word))
  18337. (t (org-shiftselect-error))))
  18338. (defun org-shiftcontrolup (&optional n)
  18339. "Change timestamps synchronously up in CLOCK log lines.
  18340. Optional argument N tells to change by that many units."
  18341. (interactive "P")
  18342. (cond ((and (not org-support-shift-select)
  18343. (org-at-clock-log-p)
  18344. (org-at-timestamp-p t))
  18345. (org-clock-timestamps-up n))
  18346. (t (org-shiftselect-error))))
  18347. (defun org-shiftcontroldown (&optional n)
  18348. "Change timestamps synchronously down in CLOCK log lines.
  18349. Optional argument N tells to change by that many units."
  18350. (interactive "P")
  18351. (cond ((and (not org-support-shift-select)
  18352. (org-at-clock-log-p)
  18353. (org-at-timestamp-p t))
  18354. (org-clock-timestamps-down n))
  18355. (t (org-shiftselect-error))))
  18356. (defun org-ctrl-c-ret ()
  18357. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18358. (interactive)
  18359. (cond
  18360. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18361. (t (call-interactively 'org-insert-heading))))
  18362. (defun org-find-visible ()
  18363. (let ((s (point)))
  18364. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18365. (get-char-property s 'invisible)))
  18366. s))
  18367. (defun org-find-invisible ()
  18368. (let ((s (point)))
  18369. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18370. (not (get-char-property s 'invisible))))
  18371. s))
  18372. (defun org-copy-visible (beg end)
  18373. "Copy the visible parts of the region."
  18374. (interactive "r")
  18375. (let (snippets s)
  18376. (save-excursion
  18377. (save-restriction
  18378. (narrow-to-region beg end)
  18379. (setq s (goto-char (point-min)))
  18380. (while (not (= (point) (point-max)))
  18381. (goto-char (org-find-invisible))
  18382. (push (buffer-substring s (point)) snippets)
  18383. (setq s (goto-char (org-find-visible))))))
  18384. (kill-new (apply 'concat (nreverse snippets)))))
  18385. (defun org-copy-special ()
  18386. "Copy region in table or copy current subtree.
  18387. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18388. See the individual commands for more information."
  18389. (interactive)
  18390. (call-interactively
  18391. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18392. (defun org-cut-special ()
  18393. "Cut region in table or cut current subtree.
  18394. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18395. See the individual commands for more information."
  18396. (interactive)
  18397. (call-interactively
  18398. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18399. (defun org-paste-special (arg)
  18400. "Paste rectangular region into table, or past subtree relative to level.
  18401. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18402. See the individual commands for more information."
  18403. (interactive "P")
  18404. (if (org-at-table-p)
  18405. (org-table-paste-rectangle)
  18406. (org-paste-subtree arg)))
  18407. (defsubst org-in-fixed-width-region-p ()
  18408. "Is point in a fixed-width region?"
  18409. (save-match-data
  18410. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18411. (defun org-edit-special (&optional arg)
  18412. "Call a special editor for the element at point.
  18413. When at a table, call the formula editor with `org-table-edit-formulas'.
  18414. When in a source code block, call `org-edit-src-code'.
  18415. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18416. When at an #+INCLUDE keyword, visit the included file.
  18417. On a link, call `ffap' to visit the link at point.
  18418. Otherwise, return a user error."
  18419. (interactive "P")
  18420. (let ((element (org-element-at-point)))
  18421. (assert (not buffer-read-only) nil
  18422. "Buffer is read-only: %s" (buffer-name))
  18423. (case (org-element-type element)
  18424. (src-block
  18425. (if (not arg) (org-edit-src-code)
  18426. (let* ((info (org-babel-get-src-block-info))
  18427. (lang (nth 0 info))
  18428. (params (nth 2 info))
  18429. (session (cdr (assq :session params))))
  18430. (if (not session) (org-edit-src-code)
  18431. ;; At a src-block with a session and function called with
  18432. ;; an ARG: switch to the buffer related to the inferior
  18433. ;; process.
  18434. (switch-to-buffer
  18435. (funcall (intern (concat "org-babel-prep-session:" lang))
  18436. session params))))))
  18437. (keyword
  18438. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18439. (find-file
  18440. (org-remove-double-quotes
  18441. (car (org-split-string (org-element-property :value element)))))
  18442. (user-error "No special environment to edit here")))
  18443. (table
  18444. (if (eq (org-element-property :type element) 'table.el)
  18445. (org-edit-src-code)
  18446. (call-interactively 'org-table-edit-formulas)))
  18447. ;; Only Org tables contain `table-row' type elements.
  18448. (table-row (call-interactively 'org-table-edit-formulas))
  18449. ((example-block export-block) (org-edit-src-code))
  18450. (fixed-width (org-edit-fixed-width-region))
  18451. (otherwise
  18452. ;; No notable element at point. Though, we may be at a link,
  18453. ;; which is an object. Thus, scan deeper.
  18454. (if (eq (org-element-type (org-element-context element)) 'link)
  18455. (call-interactively 'ffap)
  18456. (user-error "No special environment to edit here"))))))
  18457. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18458. (defun org-ctrl-c-ctrl-c (&optional arg)
  18459. "Set tags in headline, or update according to changed information at point.
  18460. This command does many different things, depending on context:
  18461. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18462. this is what we do.
  18463. - If the cursor is on a statistics cookie, update it.
  18464. - If the cursor is in a headline, prompt for tags and insert them
  18465. into the current line, aligned to `org-tags-column'. When called
  18466. with prefix arg, realign all tags in the current buffer.
  18467. - If the cursor is in one of the special #+KEYWORD lines, this
  18468. triggers scanning the buffer for these lines and updating the
  18469. information.
  18470. - If the cursor is inside a table, realign the table. This command
  18471. works even if the automatic table editor has been turned off.
  18472. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18473. the entire table.
  18474. - If the cursor is at a footnote reference or definition, jump to
  18475. the corresponding definition or references, respectively.
  18476. - If the cursor is a the beginning of a dynamic block, update it.
  18477. - If the current buffer is a capture buffer, close note and file it.
  18478. - If the cursor is on a <<<target>>>, update radio targets and
  18479. corresponding links in this buffer.
  18480. - If the cursor is on a numbered item in a plain list, renumber the
  18481. ordered list.
  18482. - If the cursor is on a checkbox, toggle it.
  18483. - If the cursor is on a code block, evaluate it. The variable
  18484. `org-confirm-babel-evaluate' can be used to control prompting
  18485. before code block evaluation, by default every code block
  18486. evaluation requires confirmation. Code block evaluation can be
  18487. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18488. (interactive "P")
  18489. (cond
  18490. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18491. org-occur-highlights
  18492. org-latex-fragment-image-overlays)
  18493. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18494. (org-remove-occur-highlights)
  18495. (org-remove-latex-fragment-image-overlays)
  18496. (message "Temporary highlights/overlays removed from current buffer"))
  18497. ((and (local-variable-p 'org-finish-function (current-buffer))
  18498. (fboundp org-finish-function))
  18499. (funcall org-finish-function))
  18500. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18501. (t
  18502. (let* ((context (org-element-context)) (type (org-element-type context)))
  18503. ;; Test if point is within a blank line.
  18504. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18505. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18506. (user-error "C-c C-c can do nothing useful at this location"))
  18507. ;; For convenience: at the first line of a paragraph on the
  18508. ;; same line as an item, apply function on that item instead.
  18509. (when (eq type 'paragraph)
  18510. (let ((parent (org-element-property :parent context)))
  18511. (when (and (eq (org-element-type parent) 'item)
  18512. (= (point-at-bol) (org-element-property :begin parent)))
  18513. (setq context parent type 'item))))
  18514. ;; Act according to type of element or object at point.
  18515. (case type
  18516. (clock (org-clock-update-time-maybe))
  18517. (dynamic-block
  18518. (save-excursion
  18519. (goto-char (org-element-property :post-affiliated context))
  18520. (org-update-dblock)))
  18521. (footnote-definition
  18522. (goto-char (org-element-property :post-affiliated context))
  18523. (call-interactively 'org-footnote-action))
  18524. (footnote-reference (call-interactively 'org-footnote-action))
  18525. ((headline inlinetask)
  18526. (save-excursion (goto-char (org-element-property :begin context))
  18527. (call-interactively 'org-set-tags)))
  18528. (item
  18529. ;; At an item: a double C-u set checkbox to "[-]"
  18530. ;; unconditionally, whereas a single one will toggle its
  18531. ;; presence. Without an universal argument, if the item
  18532. ;; has a checkbox, toggle it. Otherwise repair the list.
  18533. (let* ((box (org-element-property :checkbox context))
  18534. (struct (org-element-property :structure context))
  18535. (old-struct (copy-tree struct))
  18536. (parents (org-list-parents-alist struct))
  18537. (prevs (org-list-prevs-alist struct))
  18538. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18539. (org-list-set-checkbox
  18540. (org-element-property :begin context) struct
  18541. (cond ((equal arg '(16)) "[-]")
  18542. ((and (not box) (equal arg '(4))) "[ ]")
  18543. ((or (not box) (equal arg '(4))) nil)
  18544. ((eq box 'on) "[ ]")
  18545. (t "[X]")))
  18546. ;; Mimic `org-list-write-struct' but with grabbing
  18547. ;; a return value from `org-list-struct-fix-box'.
  18548. (org-list-struct-fix-ind struct parents 2)
  18549. (org-list-struct-fix-item-end struct)
  18550. (org-list-struct-fix-bul struct prevs)
  18551. (org-list-struct-fix-ind struct parents)
  18552. (let ((block-item
  18553. (org-list-struct-fix-box struct parents prevs orderedp)))
  18554. (if (and box (equal struct old-struct))
  18555. (if (equal arg '(16))
  18556. (message "Checkboxes already reset")
  18557. (user-error "Cannot toggle this checkbox: %s"
  18558. (if (eq box 'on)
  18559. "all subitems checked"
  18560. "unchecked subitems")))
  18561. (org-list-struct-apply-struct struct old-struct)
  18562. (org-update-checkbox-count-maybe))
  18563. (when block-item
  18564. (message "Checkboxes were removed due to empty box at line %d"
  18565. (org-current-line block-item))))))
  18566. (keyword
  18567. (let ((org-inhibit-startup-visibility-stuff t)
  18568. (org-startup-align-all-tables nil))
  18569. (when (boundp 'org-table-coordinate-overlays)
  18570. (mapc 'delete-overlay org-table-coordinate-overlays)
  18571. (setq org-table-coordinate-overlays nil))
  18572. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18573. (message "Local setup has been refreshed"))
  18574. (plain-list
  18575. ;; At a plain list, with a double C-u argument, set
  18576. ;; checkboxes of each item to "[-]", whereas a single one
  18577. ;; will toggle their presence according to the state of the
  18578. ;; first item in the list. Without an argument, repair the
  18579. ;; list.
  18580. (let* ((begin (org-element-property :contents-begin context))
  18581. (beginm (move-marker (make-marker) begin))
  18582. (struct (org-element-property :structure context))
  18583. (old-struct (copy-tree struct))
  18584. (first-box (save-excursion
  18585. (goto-char begin)
  18586. (looking-at org-list-full-item-re)
  18587. (match-string-no-properties 3)))
  18588. (new-box (cond ((equal arg '(16)) "[-]")
  18589. ((equal arg '(4)) (unless first-box "[ ]"))
  18590. ((equal first-box "[X]") "[ ]")
  18591. (t "[X]"))))
  18592. (cond
  18593. (arg
  18594. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18595. (org-list-get-all-items
  18596. begin struct (org-list-prevs-alist struct))))
  18597. ((and first-box (eq (point) begin))
  18598. ;; For convenience, when point is at bol on the first
  18599. ;; item of the list and no argument is provided, simply
  18600. ;; toggle checkbox of that item, if any.
  18601. (org-list-set-checkbox begin struct new-box)))
  18602. (org-list-write-struct
  18603. struct (org-list-parents-alist struct) old-struct)
  18604. (org-update-checkbox-count-maybe)
  18605. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18606. ((property-drawer node-property)
  18607. (call-interactively 'org-property-action))
  18608. ((radio-target target)
  18609. (call-interactively 'org-update-radio-target-regexp))
  18610. (statistics-cookie
  18611. (call-interactively 'org-update-statistics-cookies))
  18612. ((table table-cell table-row)
  18613. ;; At a table, recalculate every field and align it. Also
  18614. ;; send the table if necessary. If the table has
  18615. ;; a `table.el' type, just give up. At a table row or
  18616. ;; cell, maybe recalculate line but always align table.
  18617. (if (eq (org-element-property :type context) 'table.el)
  18618. (message "Use C-c ' to edit table.el tables")
  18619. (let ((org-enable-table-editor t))
  18620. (if (or (eq type 'table)
  18621. ;; Check if point is at a TBLFM line.
  18622. (and (eq type 'table-row)
  18623. (= (point) (org-element-property :end context))))
  18624. (save-excursion
  18625. (if (org-at-TBLFM-p) (org-calc-current-TBLFM)
  18626. (goto-char (org-element-property :contents-begin context))
  18627. (org-call-with-arg 'org-table-recalculate (or arg t))
  18628. (orgtbl-send-table 'maybe)))
  18629. (org-table-maybe-eval-formula)
  18630. (cond (arg (call-interactively 'org-table-recalculate))
  18631. ((org-table-maybe-recalculate-line))
  18632. (t (org-table-align)))))))
  18633. (timestamp (org-timestamp-change 0 'day))
  18634. (otherwise
  18635. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18636. (user-error
  18637. "C-c C-c can do nothing useful at this location")))))))))
  18638. (defun org-mode-restart ()
  18639. "Restart Org-mode, to scan again for special lines.
  18640. Also updates the keyword regular expressions."
  18641. (interactive)
  18642. (org-mode)
  18643. (message "Org-mode restarted"))
  18644. (defun org-kill-note-or-show-branches ()
  18645. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18646. (interactive)
  18647. (if (not org-finish-function)
  18648. (progn
  18649. (hide-subtree)
  18650. (call-interactively 'show-branches))
  18651. (let ((org-note-abort t))
  18652. (funcall org-finish-function))))
  18653. (defun org-open-line (n)
  18654. "Insert a new row in tables, call `open-line' elsewhere."
  18655. (interactive "*p")
  18656. (if (org-at-table-p)
  18657. (org-table-insert-row)
  18658. (open-line n)))
  18659. (defun org-return (&optional indent)
  18660. "Goto next table row or insert a newline.
  18661. Calls `org-table-next-row' or `newline', depending on context.
  18662. See the individual commands for more information."
  18663. (interactive)
  18664. (let (org-ts-what)
  18665. (cond
  18666. ((or (bobp) (org-in-src-block-p))
  18667. (if indent (newline-and-indent) (newline)))
  18668. ((org-at-table-p)
  18669. (org-table-justify-field-maybe)
  18670. (call-interactively 'org-table-next-row))
  18671. ;; when `newline-and-indent' is called within a list, make sure
  18672. ;; text moved stays inside the item.
  18673. ((and (org-in-item-p) indent)
  18674. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18675. (progn
  18676. (save-match-data (newline))
  18677. (org-indent-line-to (length (match-string 0))))
  18678. (let ((ind (org-get-indentation)))
  18679. (newline)
  18680. (if (org-looking-back org-list-end-re)
  18681. (org-indent-line)
  18682. (org-indent-line-to ind)))))
  18683. ((and org-return-follows-link
  18684. (org-at-timestamp-p t)
  18685. (not (eq org-ts-what 'after)))
  18686. (org-follow-timestamp-link))
  18687. ((and org-return-follows-link
  18688. (let ((tprop (get-text-property (point) 'face)))
  18689. (or (eq tprop 'org-link)
  18690. (and (listp tprop) (memq 'org-link tprop)))))
  18691. (call-interactively 'org-open-at-point))
  18692. ((and (org-at-heading-p)
  18693. (looking-at
  18694. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18695. (org-show-entry)
  18696. (end-of-line 1)
  18697. (newline))
  18698. (t (if indent (newline-and-indent) (newline))))))
  18699. (defun org-return-indent ()
  18700. "Goto next table row or insert a newline and indent.
  18701. Calls `org-table-next-row' or `newline-and-indent', depending on
  18702. context. See the individual commands for more information."
  18703. (interactive)
  18704. (org-return t))
  18705. (defun org-ctrl-c-star ()
  18706. "Compute table, or change heading status of lines.
  18707. Calls `org-table-recalculate' or `org-toggle-heading',
  18708. depending on context."
  18709. (interactive)
  18710. (cond
  18711. ((org-at-table-p)
  18712. (call-interactively 'org-table-recalculate))
  18713. (t
  18714. ;; Convert all lines in region to list items
  18715. (call-interactively 'org-toggle-heading))))
  18716. (defun org-ctrl-c-minus ()
  18717. "Insert separator line in table or modify bullet status of line.
  18718. Also turns a plain line or a region of lines into list items.
  18719. Calls `org-table-insert-hline', `org-toggle-item', or
  18720. `org-cycle-list-bullet', depending on context."
  18721. (interactive)
  18722. (cond
  18723. ((org-at-table-p)
  18724. (call-interactively 'org-table-insert-hline))
  18725. ((org-region-active-p)
  18726. (call-interactively 'org-toggle-item))
  18727. ((org-in-item-p)
  18728. (call-interactively 'org-cycle-list-bullet))
  18729. (t
  18730. (call-interactively 'org-toggle-item))))
  18731. (defun org-toggle-item (arg)
  18732. "Convert headings or normal lines to items, items to normal lines.
  18733. If there is no active region, only the current line is considered.
  18734. If the first non blank line in the region is a headline, convert
  18735. all headlines to items, shifting text accordingly.
  18736. If it is an item, convert all items to normal lines.
  18737. If it is normal text, change region into a list of items.
  18738. With a prefix argument ARG, change the region in a single item."
  18739. (interactive "P")
  18740. (let ((shift-text
  18741. (function
  18742. ;; Shift text in current section to IND, from point to END.
  18743. ;; The function leaves point to END line.
  18744. (lambda (ind end)
  18745. (let ((min-i 1000) (end (copy-marker end)))
  18746. ;; First determine the minimum indentation (MIN-I) of
  18747. ;; the text.
  18748. (save-excursion
  18749. (catch 'exit
  18750. (while (< (point) end)
  18751. (let ((i (org-get-indentation)))
  18752. (cond
  18753. ;; Skip blank lines and inline tasks.
  18754. ((looking-at "^[ \t]*$"))
  18755. ((looking-at org-outline-regexp-bol))
  18756. ;; We can't find less than 0 indentation.
  18757. ((zerop i) (throw 'exit (setq min-i 0)))
  18758. ((< i min-i) (setq min-i i))))
  18759. (forward-line))))
  18760. ;; Then indent each line so that a line indented to
  18761. ;; MIN-I becomes indented to IND. Ignore blank lines
  18762. ;; and inline tasks in the process.
  18763. (let ((delta (- ind min-i)))
  18764. (while (< (point) end)
  18765. (unless (or (looking-at "^[ \t]*$")
  18766. (looking-at org-outline-regexp-bol))
  18767. (org-indent-line-to (+ (org-get-indentation) delta)))
  18768. (forward-line)))))))
  18769. (skip-blanks
  18770. (function
  18771. ;; Return beginning of first non-blank line, starting from
  18772. ;; line at POS.
  18773. (lambda (pos)
  18774. (save-excursion
  18775. (goto-char pos)
  18776. (skip-chars-forward " \r\t\n")
  18777. (point-at-bol)))))
  18778. beg end)
  18779. ;; Determine boundaries of changes.
  18780. (if (org-region-active-p)
  18781. (setq beg (funcall skip-blanks (region-beginning))
  18782. end (copy-marker (region-end)))
  18783. (setq beg (funcall skip-blanks (point-at-bol))
  18784. end (copy-marker (point-at-eol))))
  18785. ;; Depending on the starting line, choose an action on the text
  18786. ;; between BEG and END.
  18787. (org-with-limited-levels
  18788. (save-excursion
  18789. (goto-char beg)
  18790. (cond
  18791. ;; Case 1. Start at an item: de-itemize. Note that it only
  18792. ;; happens when a region is active: `org-ctrl-c-minus'
  18793. ;; would call `org-cycle-list-bullet' otherwise.
  18794. ((org-at-item-p)
  18795. (while (< (point) end)
  18796. (when (org-at-item-p)
  18797. (skip-chars-forward " \t")
  18798. (delete-region (point) (match-end 0)))
  18799. (forward-line)))
  18800. ;; Case 2. Start at an heading: convert to items.
  18801. ((org-at-heading-p)
  18802. (let* ((bul (org-list-bullet-string "-"))
  18803. (bul-len (length bul))
  18804. ;; Indentation of the first heading. It should be
  18805. ;; relative to the indentation of its parent, if any.
  18806. (start-ind (save-excursion
  18807. (cond
  18808. ((not org-adapt-indentation) 0)
  18809. ((not (outline-previous-heading)) 0)
  18810. (t (length (match-string 0))))))
  18811. ;; Level of first heading. Further headings will be
  18812. ;; compared to it to determine hierarchy in the list.
  18813. (ref-level (org-reduced-level (org-outline-level))))
  18814. (while (< (point) end)
  18815. (let* ((level (org-reduced-level (org-outline-level)))
  18816. (delta (max 0 (- level ref-level))))
  18817. ;; If current headline is less indented than the first
  18818. ;; one, set it as reference, in order to preserve
  18819. ;; subtrees.
  18820. (when (< level ref-level) (setq ref-level level))
  18821. (replace-match bul t t)
  18822. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18823. ;; Ensure all text down to END (or SECTION-END) belongs
  18824. ;; to the newly created item.
  18825. (let ((section-end (save-excursion
  18826. (or (outline-next-heading) (point)))))
  18827. (forward-line)
  18828. (funcall shift-text
  18829. (+ start-ind (* (1+ delta) bul-len))
  18830. (min end section-end)))))))
  18831. ;; Case 3. Normal line with ARG: make the first line of region
  18832. ;; an item, and shift indentation of others lines to
  18833. ;; set them as item's body.
  18834. (arg (let* ((bul (org-list-bullet-string "-"))
  18835. (bul-len (length bul))
  18836. (ref-ind (org-get-indentation)))
  18837. (skip-chars-forward " \t")
  18838. (insert bul)
  18839. (forward-line)
  18840. (while (< (point) end)
  18841. ;; Ensure that lines less indented than first one
  18842. ;; still get included in item body.
  18843. (funcall shift-text
  18844. (+ ref-ind bul-len)
  18845. (min end (save-excursion (or (outline-next-heading)
  18846. (point)))))
  18847. (forward-line))))
  18848. ;; Case 4. Normal line without ARG: turn each non-item line
  18849. ;; into an item.
  18850. (t
  18851. (while (< (point) end)
  18852. (unless (or (org-at-heading-p) (org-at-item-p))
  18853. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18854. (replace-match
  18855. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18856. (forward-line))))))))
  18857. (defun org-toggle-heading (&optional nstars)
  18858. "Convert headings to normal text, or items or text to headings.
  18859. If there is no active region, only convert the current line.
  18860. With a \\[universal-argument] prefix, convert the whole list at
  18861. point into heading.
  18862. In a region:
  18863. - If the first non blank line is a headline, remove the stars
  18864. from all headlines in the region.
  18865. - If it is a normal line, turn each and every normal line (i.e.,
  18866. not an heading or an item) in the region into headings. If you
  18867. want to convert only the first line of this region, use one
  18868. universal prefix argument.
  18869. - If it is a plain list item, turn all plain list items into headings.
  18870. When converting a line into a heading, the number of stars is chosen
  18871. such that the lines become children of the current entry. However,
  18872. when a numeric prefix argument is given, its value determines the
  18873. number of stars to add."
  18874. (interactive "P")
  18875. (let ((skip-blanks
  18876. (function
  18877. ;; Return beginning of first non-blank line, starting from
  18878. ;; line at POS.
  18879. (lambda (pos)
  18880. (save-excursion
  18881. (goto-char pos)
  18882. (while (org-at-comment-p) (forward-line))
  18883. (skip-chars-forward " \r\t\n")
  18884. (point-at-bol)))))
  18885. beg end toggled)
  18886. ;; Determine boundaries of changes. If a universal prefix has
  18887. ;; been given, put the list in a region. If region ends at a bol,
  18888. ;; do not consider the last line to be in the region.
  18889. (when (and current-prefix-arg (org-at-item-p))
  18890. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18891. (org-mark-element))
  18892. (if (org-region-active-p)
  18893. (setq beg (funcall skip-blanks (region-beginning))
  18894. end (copy-marker (save-excursion
  18895. (goto-char (region-end))
  18896. (if (bolp) (point) (point-at-eol)))))
  18897. (setq beg (funcall skip-blanks (point-at-bol))
  18898. end (copy-marker (point-at-eol))))
  18899. ;; Ensure inline tasks don't count as headings.
  18900. (org-with-limited-levels
  18901. (save-excursion
  18902. (goto-char beg)
  18903. (cond
  18904. ;; Case 1. Started at an heading: de-star headings.
  18905. ((org-at-heading-p)
  18906. (while (< (point) end)
  18907. (when (org-at-heading-p t)
  18908. (looking-at org-outline-regexp) (replace-match "")
  18909. (setq toggled t))
  18910. (forward-line)))
  18911. ;; Case 2. Started at an item: change items into headlines.
  18912. ;; One star will be added by `org-list-to-subtree'.
  18913. ((org-at-item-p)
  18914. (let* ((stars (make-string
  18915. ;; subtract the star that will be added again by
  18916. ;; `org-list-to-subtree'
  18917. (if (numberp nstars) (1- nstars)
  18918. (or (org-current-level) 0))
  18919. ?*))
  18920. (add-stars
  18921. (cond (nstars "") ; stars from prefix only
  18922. ((equal stars "") "") ; before first heading
  18923. (org-odd-levels-only "*") ; inside heading, odd
  18924. (t "")))) ; inside heading, oddeven
  18925. (while (< (point) end)
  18926. (when (org-at-item-p)
  18927. ;; Pay attention to cases when region ends before list.
  18928. (let* ((struct (org-list-struct))
  18929. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18930. (save-restriction
  18931. (narrow-to-region (point) list-end)
  18932. (insert
  18933. (org-list-to-subtree
  18934. (org-list-parse-list t)
  18935. '(:istart (concat stars add-stars (funcall get-stars depth))
  18936. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18937. (setq toggled t))
  18938. (forward-line))))
  18939. ;; Case 3. Started at normal text: make every line an heading,
  18940. ;; skipping headlines and items.
  18941. (t (let* ((stars
  18942. (make-string
  18943. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  18944. (add-stars
  18945. (cond (nstars "") ; stars from prefix only
  18946. ((equal stars "") "*") ; before first heading
  18947. (org-odd-levels-only "**") ; inside heading, odd
  18948. (t "*"))) ; inside heading, oddeven
  18949. (rpl (concat stars add-stars " "))
  18950. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  18951. (while (< (point) (if (equal nstars '(4)) lend end))
  18952. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18953. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18954. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18955. (forward-line)))))))
  18956. (unless toggled (message "Cannot toggle heading from here"))))
  18957. (defun org-meta-return (&optional arg)
  18958. "Insert a new heading or wrap a region in a table.
  18959. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18960. See the individual commands for more information."
  18961. (interactive "P")
  18962. (org-check-before-invisible-edit 'insert)
  18963. (cond
  18964. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18965. ((or (org-at-drawer-p) (org-in-drawer-p) (org-at-property-p))
  18966. (newline-and-indent))
  18967. ((org-at-table-p)
  18968. (call-interactively 'org-table-wrap-region))
  18969. (t (call-interactively 'org-insert-heading))))
  18970. ;;; Menu entries
  18971. (defsubst org-in-subtree-not-table-p ()
  18972. "Are we in a subtree and not in a table?"
  18973. (and (not (org-before-first-heading-p))
  18974. (not (org-at-table-p))))
  18975. ;; Define the Org-mode menus
  18976. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18977. '("Tbl"
  18978. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18979. ["Next Field" org-cycle (org-at-table-p)]
  18980. ["Previous Field" org-shifttab (org-at-table-p)]
  18981. ["Next Row" org-return (org-at-table-p)]
  18982. "--"
  18983. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18984. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18985. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18986. "--"
  18987. ("Column"
  18988. ["Move Column Left" org-metaleft (org-at-table-p)]
  18989. ["Move Column Right" org-metaright (org-at-table-p)]
  18990. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18991. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18992. ("Row"
  18993. ["Move Row Up" org-metaup (org-at-table-p)]
  18994. ["Move Row Down" org-metadown (org-at-table-p)]
  18995. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18996. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18997. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18998. "--"
  18999. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19000. ("Rectangle"
  19001. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19002. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19003. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19004. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19005. "--"
  19006. ("Calculate"
  19007. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19008. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19009. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19010. "--"
  19011. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19012. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19013. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19014. "--"
  19015. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19016. "--"
  19017. ["Sum Column/Rectangle" org-table-sum
  19018. (or (org-at-table-p) (org-region-active-p))]
  19019. ["Which Column?" org-table-current-column (org-at-table-p)])
  19020. ["Debug Formulas"
  19021. org-table-toggle-formula-debugger
  19022. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19023. ["Show Col/Row Numbers"
  19024. org-table-toggle-coordinate-overlays
  19025. :style toggle
  19026. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19027. "--"
  19028. ["Create" org-table-create (and (not (org-at-table-p))
  19029. org-enable-table-editor)]
  19030. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19031. ["Import from File" org-table-import (not (org-at-table-p))]
  19032. ["Export to File" org-table-export (org-at-table-p)]
  19033. "--"
  19034. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  19035. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19036. '("Org"
  19037. ("Show/Hide"
  19038. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19039. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19040. ["Sparse Tree..." org-sparse-tree t]
  19041. ["Reveal Context" org-reveal t]
  19042. ["Show All" show-all t]
  19043. "--"
  19044. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19045. "--"
  19046. ["New Heading" org-insert-heading t]
  19047. ("Navigate Headings"
  19048. ["Up" outline-up-heading t]
  19049. ["Next" outline-next-visible-heading t]
  19050. ["Previous" outline-previous-visible-heading t]
  19051. ["Next Same Level" outline-forward-same-level t]
  19052. ["Previous Same Level" outline-backward-same-level t]
  19053. "--"
  19054. ["Jump" org-goto t])
  19055. ("Edit Structure"
  19056. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19057. "--"
  19058. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19059. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19060. "--"
  19061. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19062. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19063. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19064. "--"
  19065. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19066. "--"
  19067. ["Copy visible text" org-copy-visible t]
  19068. "--"
  19069. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19070. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19071. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19072. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19073. "--"
  19074. ["Sort Region/Children" org-sort t]
  19075. "--"
  19076. ["Convert to odd levels" org-convert-to-odd-levels t]
  19077. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19078. ("Editing"
  19079. ["Emphasis..." org-emphasize t]
  19080. ["Edit Source Example" org-edit-special t]
  19081. "--"
  19082. ["Footnote new/jump" org-footnote-action t]
  19083. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19084. ("Archive"
  19085. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19086. "--"
  19087. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19088. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19089. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19090. )
  19091. "--"
  19092. ("Hyperlinks"
  19093. ["Store Link (Global)" org-store-link t]
  19094. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19095. ["Insert Link" org-insert-link t]
  19096. ["Follow Link" org-open-at-point t]
  19097. "--"
  19098. ["Next link" org-next-link t]
  19099. ["Previous link" org-previous-link t]
  19100. "--"
  19101. ["Descriptive Links"
  19102. org-toggle-link-display
  19103. :style radio
  19104. :selected org-descriptive-links
  19105. ]
  19106. ["Literal Links"
  19107. org-toggle-link-display
  19108. :style radio
  19109. :selected (not org-descriptive-links)])
  19110. "--"
  19111. ("TODO Lists"
  19112. ["TODO/DONE/-" org-todo t]
  19113. ("Select keyword"
  19114. ["Next keyword" org-shiftright (org-at-heading-p)]
  19115. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19116. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19117. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19118. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19119. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19120. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19121. "--"
  19122. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19123. :selected org-enforce-todo-dependencies :style toggle :active t]
  19124. "Settings for tree at point"
  19125. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19126. :selected (org-entry-get nil "ORDERED")
  19127. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19128. ["Do Children parallel" org-toggle-ordered-property :style radio
  19129. :selected (not (org-entry-get nil "ORDERED"))
  19130. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19131. "--"
  19132. ["Set Priority" org-priority t]
  19133. ["Priority Up" org-shiftup t]
  19134. ["Priority Down" org-shiftdown t]
  19135. "--"
  19136. ["Get news from all feeds" org-feed-update-all t]
  19137. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19138. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19139. ("TAGS and Properties"
  19140. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19141. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19142. "--"
  19143. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19144. ["Column view of properties" org-columns t]
  19145. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19146. ("Dates and Scheduling"
  19147. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19148. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19149. ("Change Date"
  19150. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19151. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19152. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19153. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19154. ["Compute Time Range" org-evaluate-time-range t]
  19155. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19156. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19157. "--"
  19158. ["Custom time format" org-toggle-time-stamp-overlays
  19159. :style radio :selected org-display-custom-times]
  19160. "--"
  19161. ["Goto Calendar" org-goto-calendar t]
  19162. ["Date from Calendar" org-date-from-calendar t]
  19163. "--"
  19164. ["Start/Restart Timer" org-timer-start t]
  19165. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19166. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19167. ["Insert Timer String" org-timer t]
  19168. ["Insert Timer Item" org-timer-item t])
  19169. ("Logging work"
  19170. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19171. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19172. ["Clock out" org-clock-out t]
  19173. ["Clock cancel" org-clock-cancel t]
  19174. "--"
  19175. ["Mark as default task" org-clock-mark-default-task t]
  19176. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19177. ["Goto running clock" org-clock-goto t]
  19178. "--"
  19179. ["Display times" org-clock-display t]
  19180. ["Create clock table" org-clock-report t]
  19181. "--"
  19182. ["Record DONE time"
  19183. (progn (setq org-log-done (not org-log-done))
  19184. (message "Switching to %s will %s record a timestamp"
  19185. (car org-done-keywords)
  19186. (if org-log-done "automatically" "not")))
  19187. :style toggle :selected org-log-done])
  19188. "--"
  19189. ["Agenda Command..." org-agenda t]
  19190. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19191. ("File List for Agenda")
  19192. ("Special views current file"
  19193. ["TODO Tree" org-show-todo-tree t]
  19194. ["Check Deadlines" org-check-deadlines t]
  19195. ["Timeline" org-timeline t]
  19196. ["Tags/Property tree" org-match-sparse-tree t])
  19197. "--"
  19198. ["Export/Publish..." org-export-dispatch t]
  19199. ("LaTeX"
  19200. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19201. :selected org-cdlatex-mode]
  19202. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19203. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19204. ["Modify math symbol" org-cdlatex-math-modify
  19205. (org-inside-LaTeX-fragment-p)]
  19206. ["Insert citation" org-reftex-citation t]
  19207. "--"
  19208. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19209. "--"
  19210. ("MobileOrg"
  19211. ["Push Files and Views" org-mobile-push t]
  19212. ["Get Captured and Flagged" org-mobile-pull t]
  19213. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19214. "--"
  19215. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19216. "--"
  19217. ("Documentation"
  19218. ["Show Version" org-version t]
  19219. ["Info Documentation" org-info t])
  19220. ("Customize"
  19221. ["Browse Org Group" org-customize t]
  19222. "--"
  19223. ["Expand This Menu" org-create-customize-menu
  19224. (fboundp 'customize-menu-create)])
  19225. ["Send bug report" org-submit-bug-report t]
  19226. "--"
  19227. ("Refresh/Reload"
  19228. ["Refresh setup current buffer" org-mode-restart t]
  19229. ["Reload Org (after update)" org-reload t]
  19230. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19231. ))
  19232. (defun org-info (&optional node)
  19233. "Read documentation for Org-mode in the info system.
  19234. With optional NODE, go directly to that node."
  19235. (interactive)
  19236. (info (format "(org)%s" (or node ""))))
  19237. ;;;###autoload
  19238. (defun org-submit-bug-report ()
  19239. "Submit a bug report on Org-mode via mail.
  19240. Don't hesitate to report any problems or inaccurate documentation.
  19241. If you don't have setup sending mail from (X)Emacs, please copy the
  19242. output buffer into your mail program, as it gives us important
  19243. information about your Org-mode version and configuration."
  19244. (interactive)
  19245. (require 'reporter)
  19246. (org-load-modules-maybe)
  19247. (org-require-autoloaded-modules)
  19248. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19249. (reporter-submit-bug-report
  19250. "emacs-orgmode@gnu.org"
  19251. (org-version nil 'full)
  19252. (let (list)
  19253. (save-window-excursion
  19254. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19255. (delete-other-windows)
  19256. (erase-buffer)
  19257. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19258. We would like to add your full Org-mode and Outline configuration to the
  19259. bug report. This greatly simplifies the work of the maintainer and
  19260. other experts on the mailing list.
  19261. HOWEVER, some variables you have customized may contain private
  19262. information. The names of customers, colleagues, or friends, might
  19263. appear in the form of file names, tags, todo states, or search strings.
  19264. If you answer yes to the prompt, you might want to check and remove
  19265. such private information before sending the email.")
  19266. (add-text-properties (point-min) (point-max) '(face org-warning))
  19267. (when (yes-or-no-p "Include your Org-mode configuration ")
  19268. (mapatoms
  19269. (lambda (v)
  19270. (and (boundp v)
  19271. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19272. (or (and (symbol-value v)
  19273. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19274. (and
  19275. (get v 'custom-type) (get v 'standard-value)
  19276. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19277. (push v list)))))
  19278. (kill-buffer (get-buffer "*Warn about privacy*"))
  19279. list))
  19280. nil nil
  19281. "Remember to cover the basics, that is, what you expected to happen and
  19282. what in fact did happen. You don't know how to make a good report? See
  19283. http://orgmode.org/manual/Feedback.html#Feedback
  19284. Your bug report will be posted to the Org-mode mailing list.
  19285. ------------------------------------------------------------------------")
  19286. (save-excursion
  19287. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19288. (replace-match "\\1Bug: \\3 [\\2]")))))
  19289. (defun org-install-agenda-files-menu ()
  19290. (let ((bl (buffer-list)))
  19291. (save-excursion
  19292. (while bl
  19293. (set-buffer (pop bl))
  19294. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19295. (when (derived-mode-p 'org-mode)
  19296. (easy-menu-change
  19297. '("Org") "File List for Agenda"
  19298. (append
  19299. (list
  19300. ["Edit File List" (org-edit-agenda-file-list) t]
  19301. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19302. ["Remove Current File from List" org-remove-file t]
  19303. ["Cycle through agenda files" org-cycle-agenda-files t]
  19304. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19305. "--")
  19306. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19307. ;;;; Documentation
  19308. (defun org-require-autoloaded-modules ()
  19309. (interactive)
  19310. (mapc 'require
  19311. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19312. org-remember org-table org-timer)))
  19313. ;;;###autoload
  19314. (defun org-reload (&optional uncompiled)
  19315. "Reload all org lisp files.
  19316. With prefix arg UNCOMPILED, load the uncompiled versions."
  19317. (interactive "P")
  19318. (require 'loadhist)
  19319. (let* ((org-dir (org-find-library-dir "org"))
  19320. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19321. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19322. (remove-re (mapconcat 'identity
  19323. (mapcar (lambda (f) (concat "^" f "$"))
  19324. (list (if (featurep 'xemacs)
  19325. "org-colview"
  19326. "org-colview-xemacs")
  19327. "org" "org-loaddefs" "org-version"))
  19328. "\\|"))
  19329. (feats (delete-dups
  19330. (mapcar 'file-name-sans-extension
  19331. (mapcar 'file-name-nondirectory
  19332. (delq nil
  19333. (mapcar 'feature-file
  19334. features))))))
  19335. (lfeat (append
  19336. (sort
  19337. (setq feats
  19338. (delq nil (mapcar
  19339. (lambda (f)
  19340. (if (and (string-match feature-re f)
  19341. (not (string-match remove-re f)))
  19342. f nil))
  19343. feats)))
  19344. 'string-lessp)
  19345. (list "org-version" "org")))
  19346. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19347. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19348. load-uncore load-misses)
  19349. (setq load-misses
  19350. (delq 't
  19351. (mapcar (lambda (f)
  19352. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19353. (and (string= org-dir contrib-dir)
  19354. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19355. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19356. (add-to-list 'load-uncore f 'append)
  19357. 't)
  19358. f))
  19359. lfeat)))
  19360. (if load-uncore
  19361. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19362. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19363. (if load-misses
  19364. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19365. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19366. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19367. ;;;###autoload
  19368. (defun org-customize ()
  19369. "Call the customize function with org as argument."
  19370. (interactive)
  19371. (org-load-modules-maybe)
  19372. (org-require-autoloaded-modules)
  19373. (customize-browse 'org))
  19374. (defun org-create-customize-menu ()
  19375. "Create a full customization menu for Org-mode, insert it into the menu."
  19376. (interactive)
  19377. (org-load-modules-maybe)
  19378. (org-require-autoloaded-modules)
  19379. (if (fboundp 'customize-menu-create)
  19380. (progn
  19381. (easy-menu-change
  19382. '("Org") "Customize"
  19383. `(["Browse Org group" org-customize t]
  19384. "--"
  19385. ,(customize-menu-create 'org)
  19386. ["Set" Custom-set t]
  19387. ["Save" Custom-save t]
  19388. ["Reset to Current" Custom-reset-current t]
  19389. ["Reset to Saved" Custom-reset-saved t]
  19390. ["Reset to Standard Settings" Custom-reset-standard t]))
  19391. (message "\"Org\"-menu now contains full customization menu"))
  19392. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19393. ;;;; Miscellaneous stuff
  19394. ;;; Generally useful functions
  19395. (defun org-get-at-bol (property)
  19396. "Get text property PROPERTY at beginning of line."
  19397. (get-text-property (point-at-bol) property))
  19398. (defun org-find-text-property-in-string (prop s)
  19399. "Return the first non-nil value of property PROP in string S."
  19400. (or (get-text-property 0 prop s)
  19401. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19402. prop s)))
  19403. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19404. "Display the given MESSAGE as a warning."
  19405. (if (fboundp 'display-warning)
  19406. (display-warning 'org message
  19407. (if (featurep 'xemacs) 'warning :warning))
  19408. (let ((buf (get-buffer-create "*Org warnings*")))
  19409. (with-current-buffer buf
  19410. (goto-char (point-max))
  19411. (insert "Warning (Org): " message)
  19412. (unless (bolp)
  19413. (newline)))
  19414. (display-buffer buf)
  19415. (sit-for 0))))
  19416. (defun org-eval (form)
  19417. "Eval FORM and return result."
  19418. (condition-case error
  19419. (eval form)
  19420. (error (format "%%![Error: %s]" error))))
  19421. (defun org-in-clocktable-p ()
  19422. "Check if the cursor is in a clocktable."
  19423. (let ((pos (point)) start)
  19424. (save-excursion
  19425. (end-of-line 1)
  19426. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19427. (setq start (match-beginning 0))
  19428. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19429. (>= (match-end 0) pos)
  19430. start))))
  19431. (defun org-in-commented-line ()
  19432. "Is point in a line starting with `#'?"
  19433. (equal (char-after (point-at-bol)) ?#))
  19434. (defun org-in-indented-comment-line ()
  19435. "Is point in a line starting with `#' after some white space?"
  19436. (save-excursion
  19437. (save-match-data
  19438. (goto-char (point-at-bol))
  19439. (looking-at "[ \t]*#"))))
  19440. (defun org-in-verbatim-emphasis ()
  19441. (save-match-data
  19442. (and (org-in-regexp org-emph-re 2)
  19443. (>= (point) (match-beginning 3))
  19444. (<= (point) (match-end 4))
  19445. (member (match-string 3) '("=" "~")))))
  19446. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19447. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19448. (if (and marker (marker-buffer marker)
  19449. (buffer-live-p (marker-buffer marker)))
  19450. (progn
  19451. (org-pop-to-buffer-same-window (marker-buffer marker))
  19452. (if (or (> marker (point-max)) (< marker (point-min)))
  19453. (widen))
  19454. (goto-char marker)
  19455. (org-show-context 'org-goto))
  19456. (if bookmark
  19457. (bookmark-jump bookmark)
  19458. (error "Cannot find location"))))
  19459. (defun org-quote-csv-field (s)
  19460. "Quote field for inclusion in CSV material."
  19461. (if (string-match "[\",]" s)
  19462. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19463. s))
  19464. (defun org-force-self-insert (N)
  19465. "Needed to enforce self-insert under remapping."
  19466. (interactive "p")
  19467. (self-insert-command N))
  19468. (defun org-string-width (s)
  19469. "Compute width of string, ignoring invisible characters.
  19470. This ignores character with invisibility property `org-link', and also
  19471. characters with property `org-cwidth', because these will become invisible
  19472. upon the next fontification round."
  19473. (let (b l)
  19474. (when (or (eq t buffer-invisibility-spec)
  19475. (assq 'org-link buffer-invisibility-spec))
  19476. (while (setq b (text-property-any 0 (length s)
  19477. 'invisible 'org-link s))
  19478. (setq s (concat (substring s 0 b)
  19479. (substring s (or (next-single-property-change
  19480. b 'invisible s) (length s)))))))
  19481. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19482. (setq s (concat (substring s 0 b)
  19483. (substring s (or (next-single-property-change
  19484. b 'org-cwidth s) (length s))))))
  19485. (setq l (string-width s) b -1)
  19486. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19487. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19488. l))
  19489. (defun org-shorten-string (s maxlength)
  19490. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19491. If the string is shorter or has length MAXLENGTH, just return the
  19492. original string. If it is longer, the functions finds a space in the
  19493. string, breaks this string off at that locations and adds three dots
  19494. as ellipsis. Including the ellipsis, the string will not be longer
  19495. than MAXLENGTH. If finding a good breaking point in the string does
  19496. not work, the string is just chopped off in the middle of a word
  19497. if necessary."
  19498. (if (<= (length s) maxlength)
  19499. s
  19500. (let* ((n (max (- maxlength 4) 1))
  19501. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19502. (if (string-match re s)
  19503. (concat (match-string 1 s) "...")
  19504. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19505. (defun org-get-indentation (&optional line)
  19506. "Get the indentation of the current line, interpreting tabs.
  19507. When LINE is given, assume it represents a line and compute its indentation."
  19508. (if line
  19509. (if (string-match "^ *" (org-remove-tabs line))
  19510. (match-end 0))
  19511. (save-excursion
  19512. (beginning-of-line 1)
  19513. (skip-chars-forward " \t")
  19514. (current-column))))
  19515. (defun org-get-string-indentation (s)
  19516. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19517. (let ((n -1) (i 0) (w tab-width) c)
  19518. (catch 'exit
  19519. (while (< (setq n (1+ n)) (length s))
  19520. (setq c (aref s n))
  19521. (cond ((= c ?\ ) (setq i (1+ i)))
  19522. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19523. (t (throw 'exit t)))))
  19524. i))
  19525. (defun org-remove-tabs (s &optional width)
  19526. "Replace tabulators in S with spaces.
  19527. Assumes that s is a single line, starting in column 0."
  19528. (setq width (or width tab-width))
  19529. (while (string-match "\t" s)
  19530. (setq s (replace-match
  19531. (make-string
  19532. (- (* width (/ (+ (match-beginning 0) width) width))
  19533. (match-beginning 0)) ?\ )
  19534. t t s)))
  19535. s)
  19536. (defun org-fix-indentation (line ind)
  19537. "Fix indentation in LINE.
  19538. IND is a cons cell with target and minimum indentation.
  19539. If the current indentation in LINE is smaller than the minimum,
  19540. leave it alone. If it is larger than ind, set it to the target."
  19541. (let* ((l (org-remove-tabs line))
  19542. (i (org-get-indentation l))
  19543. (i1 (car ind)) (i2 (cdr ind)))
  19544. (if (>= i i2) (setq l (substring line i2)))
  19545. (if (> i1 0)
  19546. (concat (make-string i1 ?\ ) l)
  19547. l)))
  19548. (defun org-remove-indentation (code &optional n)
  19549. "Remove the maximum common indentation from the lines in CODE.
  19550. N may optionally be the number of spaces to remove."
  19551. (with-temp-buffer
  19552. (insert code)
  19553. (org-do-remove-indentation n)
  19554. (buffer-string)))
  19555. (defun org-do-remove-indentation (&optional n)
  19556. "Remove the maximum common indentation from the buffer."
  19557. (untabify (point-min) (point-max))
  19558. (let ((min 10000) re)
  19559. (if n
  19560. (setq min n)
  19561. (goto-char (point-min))
  19562. (while (re-search-forward "^ *[^ \n]" nil t)
  19563. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19564. (unless (or (= min 0) (= min 10000))
  19565. (setq re (format "^ \\{%d\\}" min))
  19566. (goto-char (point-min))
  19567. (while (re-search-forward re nil t)
  19568. (replace-match "")
  19569. (end-of-line 1))
  19570. min)))
  19571. (defun org-fill-template (template alist)
  19572. "Find each %key of ALIST in TEMPLATE and replace it."
  19573. (let ((case-fold-search nil)
  19574. entry key value)
  19575. (setq alist (sort (copy-sequence alist)
  19576. (lambda (a b) (< (length (car a)) (length (car b))))))
  19577. (while (setq entry (pop alist))
  19578. (setq template
  19579. (replace-regexp-in-string
  19580. (concat "%" (regexp-quote (car entry)))
  19581. (or (cdr entry) "") template t t)))
  19582. template))
  19583. (defun org-base-buffer (buffer)
  19584. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19585. (if (not buffer)
  19586. buffer
  19587. (or (buffer-base-buffer buffer)
  19588. buffer)))
  19589. (defun org-trim (s)
  19590. "Remove whitespace at beginning and end of string."
  19591. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19592. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19593. s)
  19594. (defun org-wrap (string &optional width lines)
  19595. "Wrap string to either a number of lines, or a width in characters.
  19596. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19597. that costs. If there is a word longer than WIDTH, the text is actually
  19598. wrapped to the length of that word.
  19599. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19600. many lines, whatever width that takes.
  19601. The return value is a list of lines, without newlines at the end."
  19602. (let* ((words (org-split-string string "[ \t\n]+"))
  19603. (maxword (apply 'max (mapcar 'org-string-width words)))
  19604. w ll)
  19605. (cond (width
  19606. (org-do-wrap words (max maxword width)))
  19607. (lines
  19608. (setq w maxword)
  19609. (setq ll (org-do-wrap words maxword))
  19610. (if (<= (length ll) lines)
  19611. ll
  19612. (setq ll words)
  19613. (while (> (length ll) lines)
  19614. (setq w (1+ w))
  19615. (setq ll (org-do-wrap words w)))
  19616. ll))
  19617. (t (error "Cannot wrap this")))))
  19618. (defun org-do-wrap (words width)
  19619. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19620. (let (lines line)
  19621. (while words
  19622. (setq line (pop words))
  19623. (while (and words (< (+ (length line) (length (car words))) width))
  19624. (setq line (concat line " " (pop words))))
  19625. (setq lines (push line lines)))
  19626. (nreverse lines)))
  19627. (defun org-split-string (string &optional separators)
  19628. "Splits STRING into substrings at SEPARATORS.
  19629. No empty strings are returned if there are matches at the beginning
  19630. and end of string."
  19631. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19632. (start 0)
  19633. notfirst
  19634. (list nil))
  19635. (while (and (string-match rexp string
  19636. (if (and notfirst
  19637. (= start (match-beginning 0))
  19638. (< start (length string)))
  19639. (1+ start) start))
  19640. (< (match-beginning 0) (length string)))
  19641. (setq notfirst t)
  19642. (or (eq (match-beginning 0) 0)
  19643. (and (eq (match-beginning 0) (match-end 0))
  19644. (eq (match-beginning 0) start))
  19645. (setq list
  19646. (cons (substring string start (match-beginning 0))
  19647. list)))
  19648. (setq start (match-end 0)))
  19649. (or (eq start (length string))
  19650. (setq list
  19651. (cons (substring string start)
  19652. list)))
  19653. (nreverse list)))
  19654. (defun org-quote-vert (s)
  19655. "Replace \"|\" with \"\\vert\"."
  19656. (while (string-match "|" s)
  19657. (setq s (replace-match "\\vert" t t s)))
  19658. s)
  19659. (defun org-uuidgen-p (s)
  19660. "Is S an ID created by UUIDGEN?"
  19661. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19662. (defun org-in-src-block-p (&optional inside)
  19663. "Whether point is in a code source block.
  19664. When INSIDE is non-nil, don't consider we are within a src block
  19665. when point is at #+BEGIN_SRC or #+END_SRC."
  19666. (let ((case-fold-search t) ov)
  19667. (or (and (setq ov (overlays-at (point)))
  19668. (memq 'org-block-background
  19669. (overlay-properties (car ov))))
  19670. (and (not inside)
  19671. (save-match-data
  19672. (save-excursion
  19673. (beginning-of-line)
  19674. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19675. (defun org-context ()
  19676. "Return a list of contexts of the current cursor position.
  19677. If several contexts apply, all are returned.
  19678. Each context entry is a list with a symbol naming the context, and
  19679. two positions indicating start and end of the context. Possible
  19680. contexts are:
  19681. :headline anywhere in a headline
  19682. :headline-stars on the leading stars in a headline
  19683. :todo-keyword on a TODO keyword (including DONE) in a headline
  19684. :tags on the TAGS in a headline
  19685. :priority on the priority cookie in a headline
  19686. :item on the first line of a plain list item
  19687. :item-bullet on the bullet/number of a plain list item
  19688. :checkbox on the checkbox in a plain list item
  19689. :table in an org-mode table
  19690. :table-special on a special filed in a table
  19691. :table-table in a table.el table
  19692. :clocktable in a clocktable
  19693. :src-block in a source block
  19694. :link on a hyperlink
  19695. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19696. :target on a <<target>>
  19697. :radio-target on a <<<radio-target>>>
  19698. :latex-fragment on a LaTeX fragment
  19699. :latex-preview on a LaTeX fragment with overlaid preview image
  19700. This function expects the position to be visible because it uses font-lock
  19701. faces as a help to recognize the following contexts: :table-special, :link,
  19702. and :keyword."
  19703. (let* ((f (get-text-property (point) 'face))
  19704. (faces (if (listp f) f (list f)))
  19705. (case-fold-search t)
  19706. (p (point)) clist o)
  19707. ;; First the large context
  19708. (cond
  19709. ((org-at-heading-p t)
  19710. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19711. (when (progn
  19712. (beginning-of-line 1)
  19713. (looking-at org-todo-line-tags-regexp))
  19714. (push (org-point-in-group p 1 :headline-stars) clist)
  19715. (push (org-point-in-group p 2 :todo-keyword) clist)
  19716. (push (org-point-in-group p 4 :tags) clist))
  19717. (goto-char p)
  19718. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19719. (if (looking-at "\\[#[A-Z0-9]\\]")
  19720. (push (org-point-in-group p 0 :priority) clist)))
  19721. ((org-at-item-p)
  19722. (push (org-point-in-group p 2 :item-bullet) clist)
  19723. (push (list :item (point-at-bol)
  19724. (save-excursion (org-end-of-item) (point)))
  19725. clist)
  19726. (and (org-at-item-checkbox-p)
  19727. (push (org-point-in-group p 0 :checkbox) clist)))
  19728. ((org-at-table-p)
  19729. (push (list :table (org-table-begin) (org-table-end)) clist)
  19730. (if (memq 'org-formula faces)
  19731. (push (list :table-special
  19732. (previous-single-property-change p 'face)
  19733. (next-single-property-change p 'face)) clist)))
  19734. ((org-at-table-p 'any)
  19735. (push (list :table-table) clist)))
  19736. (goto-char p)
  19737. (let ((case-fold-search t))
  19738. ;; New the "medium" contexts: clocktables, source blocks
  19739. (cond ((org-in-clocktable-p)
  19740. (push (list :clocktable
  19741. (and (or (looking-at "#\\+BEGIN: clocktable")
  19742. (search-backward "#+BEGIN: clocktable" nil t))
  19743. (match-beginning 0))
  19744. (and (re-search-forward "#\\+END:?" nil t)
  19745. (match-end 0))) clist))
  19746. ((org-in-src-block-p)
  19747. (push (list :src-block
  19748. (and (or (looking-at "#\\+BEGIN_SRC")
  19749. (search-backward "#+BEGIN_SRC" nil t))
  19750. (match-beginning 0))
  19751. (and (search-forward "#+END_SRC" nil t)
  19752. (match-beginning 0))) clist))))
  19753. (goto-char p)
  19754. ;; Now the small context
  19755. (cond
  19756. ((org-at-timestamp-p)
  19757. (push (org-point-in-group p 0 :timestamp) clist))
  19758. ((memq 'org-link faces)
  19759. (push (list :link
  19760. (previous-single-property-change p 'face)
  19761. (next-single-property-change p 'face)) clist))
  19762. ((memq 'org-special-keyword faces)
  19763. (push (list :keyword
  19764. (previous-single-property-change p 'face)
  19765. (next-single-property-change p 'face)) clist))
  19766. ((org-at-target-p)
  19767. (push (org-point-in-group p 0 :target) clist)
  19768. (goto-char (1- (match-beginning 0)))
  19769. (if (looking-at org-radio-target-regexp)
  19770. (push (org-point-in-group p 0 :radio-target) clist))
  19771. (goto-char p))
  19772. ((setq o (car (delq nil
  19773. (mapcar
  19774. (lambda (x)
  19775. (if (memq x org-latex-fragment-image-overlays) x))
  19776. (overlays-at (point))))))
  19777. (push (list :latex-fragment
  19778. (overlay-start o) (overlay-end o)) clist)
  19779. (push (list :latex-preview
  19780. (overlay-start o) (overlay-end o)) clist))
  19781. ((org-inside-LaTeX-fragment-p)
  19782. ;; FIXME: positions wrong.
  19783. (push (list :latex-fragment (point) (point)) clist)))
  19784. (setq clist (nreverse (delq nil clist)))
  19785. clist))
  19786. ;; FIXME: Compare with at-regexp-p Do we need both?
  19787. (defun org-in-regexp (re &optional nlines visually)
  19788. "Check if point is inside a match of regexp.
  19789. Normally only the current line is checked, but you can include NLINES extra
  19790. lines both before and after point into the search.
  19791. If VISUALLY is set, require that the cursor is not after the match but
  19792. really on, so that the block visually is on the match."
  19793. (catch 'exit
  19794. (let ((pos (point))
  19795. (eol (point-at-eol (+ 1 (or nlines 0))))
  19796. (inc (if visually 1 0)))
  19797. (save-excursion
  19798. (beginning-of-line (- 1 (or nlines 0)))
  19799. (while (re-search-forward re eol t)
  19800. (if (and (<= (match-beginning 0) pos)
  19801. (>= (+ inc (match-end 0)) pos))
  19802. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19803. (defun org-at-regexp-p (regexp)
  19804. "Is point inside a match of REGEXP in the current line?"
  19805. (catch 'exit
  19806. (save-excursion
  19807. (let ((pos (point)) (end (point-at-eol)))
  19808. (beginning-of-line 1)
  19809. (while (re-search-forward regexp end t)
  19810. (if (and (<= (match-beginning 0) pos)
  19811. (>= (match-end 0) pos))
  19812. (throw 'exit t)))
  19813. nil))))
  19814. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19815. "Non-nil when point is between matches of START-RE and END-RE.
  19816. Also return a non-nil value when point is on one of the matches.
  19817. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19818. buffer positions. Default values are the positions of headlines
  19819. surrounding the point.
  19820. The functions returns a cons cell whose car (resp. cdr) is the
  19821. position before START-RE (resp. after END-RE)."
  19822. (save-match-data
  19823. (let ((pos (point))
  19824. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19825. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19826. beg end)
  19827. (save-excursion
  19828. ;; Point is on a block when on START-RE or if START-RE can be
  19829. ;; found before it...
  19830. (and (or (org-at-regexp-p start-re)
  19831. (re-search-backward start-re limit-up t))
  19832. (setq beg (match-beginning 0))
  19833. ;; ... and END-RE after it...
  19834. (goto-char (match-end 0))
  19835. (re-search-forward end-re limit-down t)
  19836. (> (setq end (match-end 0)) pos)
  19837. ;; ... without another START-RE in-between.
  19838. (goto-char (match-beginning 0))
  19839. (not (re-search-backward start-re (1+ beg) t))
  19840. ;; Return value.
  19841. (cons beg end))))))
  19842. (defun org-in-block-p (names)
  19843. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19844. NAMES is a list of strings containing names of blocks.
  19845. Return first block name matched, or nil. Beware that in case of
  19846. nested blocks, the returned name may not belong to the closest
  19847. block from point."
  19848. (save-match-data
  19849. (catch 'exit
  19850. (let ((case-fold-search t)
  19851. (lim-up (save-excursion (outline-previous-heading)))
  19852. (lim-down (save-excursion (outline-next-heading))))
  19853. (mapc (lambda (name)
  19854. (let ((n (regexp-quote name)))
  19855. (when (org-between-regexps-p
  19856. (concat "^[ \t]*#\\+begin_" n)
  19857. (concat "^[ \t]*#\\+end_" n)
  19858. lim-up lim-down)
  19859. (throw 'exit n))))
  19860. names))
  19861. nil)))
  19862. (defun org-in-drawer-p ()
  19863. "Is point within a drawer?"
  19864. (save-match-data
  19865. (let ((case-fold-search t)
  19866. (lim-up (save-excursion (outline-previous-heading)))
  19867. (lim-down (save-excursion (outline-next-heading))))
  19868. (org-between-regexps-p
  19869. (concat "^[ \t]*:" (regexp-opt org-drawers) ":")
  19870. "^[ \t]*:end:.*$"
  19871. lim-up lim-down))))
  19872. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19873. "Call `multi-occur' with buffers for all agenda files."
  19874. (interactive "sOrg-files matching: \np")
  19875. (let* ((files (org-agenda-files))
  19876. (tnames (mapcar 'file-truename files))
  19877. (extra org-agenda-text-search-extra-files)
  19878. f)
  19879. (when (eq (car extra) 'agenda-archives)
  19880. (setq extra (cdr extra))
  19881. (setq files (org-add-archive-files files)))
  19882. (while (setq f (pop extra))
  19883. (unless (member (file-truename f) tnames)
  19884. (add-to-list 'files f 'append)
  19885. (add-to-list 'tnames (file-truename f) 'append)))
  19886. (multi-occur
  19887. (mapcar (lambda (x)
  19888. (with-current-buffer
  19889. (or (get-file-buffer x) (find-file-noselect x))
  19890. (widen)
  19891. (current-buffer)))
  19892. files)
  19893. regexp)))
  19894. (if (boundp 'occur-mode-find-occurrence-hook)
  19895. ;; Emacs 23
  19896. (add-hook 'occur-mode-find-occurrence-hook
  19897. (lambda ()
  19898. (when (derived-mode-p 'org-mode)
  19899. (org-reveal))))
  19900. ;; Emacs 22
  19901. (defadvice occur-mode-goto-occurrence
  19902. (after org-occur-reveal activate)
  19903. (and (derived-mode-p 'org-mode) (org-reveal)))
  19904. (defadvice occur-mode-goto-occurrence-other-window
  19905. (after org-occur-reveal activate)
  19906. (and (derived-mode-p 'org-mode) (org-reveal)))
  19907. (defadvice occur-mode-display-occurrence
  19908. (after org-occur-reveal activate)
  19909. (when (derived-mode-p 'org-mode)
  19910. (let ((pos (occur-mode-find-occurrence)))
  19911. (with-current-buffer (marker-buffer pos)
  19912. (save-excursion
  19913. (goto-char pos)
  19914. (org-reveal)))))))
  19915. (defun org-occur-link-in-agenda-files ()
  19916. "Create a link and search for it in the agendas.
  19917. The link is not stored in `org-stored-links', it is just created
  19918. for the search purpose."
  19919. (interactive)
  19920. (let ((link (condition-case nil
  19921. (org-store-link nil)
  19922. (error "Unable to create a link to here"))))
  19923. (org-occur-in-agenda-files (regexp-quote link))))
  19924. (defun org-reverse-string (string)
  19925. "Return the reverse of STRING."
  19926. (apply 'string (reverse (string-to-list string))))
  19927. (defsubst org-uniquify (list)
  19928. "Non-destructively remove duplicate elements from LIST."
  19929. (let ((res (copy-sequence list))) (delete-dups res)))
  19930. (defun org-uniquify-alist (alist)
  19931. "Merge elements of ALIST with the same key.
  19932. For example, in this alist:
  19933. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  19934. => '((a 1 3) (b 2))
  19935. merge (a 1) and (a 3) into (a 1 3).
  19936. The function returns the new ALIST."
  19937. (let (rtn)
  19938. (mapc
  19939. (lambda (e)
  19940. (let (n)
  19941. (if (not (assoc (car e) rtn))
  19942. (push e rtn)
  19943. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  19944. (setq rtn (assq-delete-all (car e) rtn))
  19945. (push n rtn))))
  19946. alist)
  19947. rtn))
  19948. (defun org-delete-all (elts list)
  19949. "Remove all elements in ELTS from LIST."
  19950. (while elts
  19951. (setq list (delete (pop elts) list)))
  19952. list)
  19953. (defun org-count (cl-item cl-seq)
  19954. "Count the number of occurrences of ITEM in SEQ.
  19955. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19956. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19957. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19958. (while (< cl-start cl-end)
  19959. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19960. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19961. (setq cl-start (1+ cl-start)))
  19962. cl-count))
  19963. (defun org-remove-if (predicate seq)
  19964. "Remove everything from SEQ that fulfills PREDICATE."
  19965. (let (res e)
  19966. (while seq
  19967. (setq e (pop seq))
  19968. (if (not (funcall predicate e)) (push e res)))
  19969. (nreverse res)))
  19970. (defun org-remove-if-not (predicate seq)
  19971. "Remove everything from SEQ that does not fulfill PREDICATE."
  19972. (let (res e)
  19973. (while seq
  19974. (setq e (pop seq))
  19975. (if (funcall predicate e) (push e res)))
  19976. (nreverse res)))
  19977. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19978. "Reduce two-argument FUNCTION across SEQ.
  19979. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19980. \":initial-value\" removed."
  19981. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19982. (cadr (memq :initial-value cl-keys)))
  19983. (cl-seq (pop cl-seq))
  19984. (t (funcall cl-func)))))
  19985. (while cl-seq
  19986. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19987. cl-accum))
  19988. (defun org-back-over-empty-lines ()
  19989. "Move backwards over whitespace, to the beginning of the first empty line.
  19990. Returns the number of empty lines passed."
  19991. (let ((pos (point)))
  19992. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19993. (skip-chars-backward " \t\n\r")
  19994. (unless (eobp)
  19995. (forward-line -1)))
  19996. (beginning-of-line 2)
  19997. (goto-char (min (point) pos))
  19998. (count-lines (point) pos)))
  19999. (defun org-skip-whitespace ()
  20000. (skip-chars-forward " \t\n\r"))
  20001. (defun org-point-in-group (point group &optional context)
  20002. "Check if POINT is in match-group GROUP.
  20003. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20004. match. If the match group does not exist or point is not inside it,
  20005. return nil."
  20006. (and (match-beginning group)
  20007. (>= point (match-beginning group))
  20008. (<= point (match-end group))
  20009. (if context
  20010. (list context (match-beginning group) (match-end group))
  20011. t)))
  20012. (defun org-switch-to-buffer-other-window (&rest args)
  20013. "Switch to buffer in a second window on the current frame.
  20014. In particular, do not allow pop-up frames.
  20015. Returns the newly created buffer."
  20016. (org-no-popups
  20017. (apply 'switch-to-buffer-other-window args)))
  20018. (defun org-combine-plists (&rest plists)
  20019. "Create a single property list from all plists in PLISTS.
  20020. The process starts by copying the first list, and then setting properties
  20021. from the other lists. Settings in the last list are the most significant
  20022. ones and overrule settings in the other lists."
  20023. (let ((rtn (copy-sequence (pop plists)))
  20024. p v ls)
  20025. (while plists
  20026. (setq ls (pop plists))
  20027. (while ls
  20028. (setq p (pop ls) v (pop ls))
  20029. (setq rtn (plist-put rtn p v))))
  20030. rtn))
  20031. (defun org-replace-escapes (string table)
  20032. "Replace %-escapes in STRING with values in TABLE.
  20033. TABLE is an association list with keys like \"%a\" and string values.
  20034. The sequences in STRING may contain normal field width and padding information,
  20035. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20036. so values can contain further %-escapes if they are define later in TABLE."
  20037. (let ((tbl (copy-alist table))
  20038. (case-fold-search nil)
  20039. (pchg 0)
  20040. e re rpl)
  20041. (while (setq e (pop tbl))
  20042. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20043. (when (and (cdr e) (string-match re (cdr e)))
  20044. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20045. (safe "SREF"))
  20046. (add-text-properties 0 3 (list 'sref sref) safe)
  20047. (setcdr e (replace-match safe t t (cdr e)))))
  20048. (while (string-match re string)
  20049. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20050. (cdr e)))
  20051. (setq string (replace-match rpl t t string))))
  20052. (while (setq pchg (next-property-change pchg string))
  20053. (let ((sref (get-text-property pchg 'sref string)))
  20054. (when (and sref (string-match "SREF" string pchg))
  20055. (setq string (replace-match sref t t string)))))
  20056. string))
  20057. (defun org-sublist (list start end)
  20058. "Return a section of LIST, from START to END.
  20059. Counting starts at 1."
  20060. (let (rtn (c start))
  20061. (setq list (nthcdr (1- start) list))
  20062. (while (and list (<= c end))
  20063. (push (pop list) rtn)
  20064. (setq c (1+ c)))
  20065. (nreverse rtn)))
  20066. (defun org-find-base-buffer-visiting (file)
  20067. "Like `find-buffer-visiting' but always return the base buffer and
  20068. not an indirect buffer."
  20069. (let ((buf (or (get-file-buffer file)
  20070. (find-buffer-visiting file))))
  20071. (if buf
  20072. (or (buffer-base-buffer buf) buf)
  20073. nil)))
  20074. (defun org-image-file-name-regexp (&optional extensions)
  20075. "Return regexp matching the file names of images.
  20076. If EXTENSIONS is given, only match these."
  20077. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20078. (image-file-name-regexp)
  20079. (let ((image-file-name-extensions
  20080. (or extensions
  20081. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20082. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20083. (concat "\\."
  20084. (regexp-opt (nconc (mapcar 'upcase
  20085. image-file-name-extensions)
  20086. image-file-name-extensions)
  20087. t)
  20088. "\\'"))))
  20089. (defun org-file-image-p (file &optional extensions)
  20090. "Return non-nil if FILE is an image."
  20091. (save-match-data
  20092. (string-match (org-image-file-name-regexp extensions) file)))
  20093. (defun org-get-cursor-date (&optional with-time)
  20094. "Return the date at cursor in as a time.
  20095. This works in the calendar and in the agenda, anywhere else it just
  20096. returns the current time.
  20097. If WITH-TIME is non-nil, returns the time of the event at point (in
  20098. the agenda) or the current time of the day."
  20099. (let (date day defd tp tm hod mod)
  20100. (when with-time
  20101. (setq tp (get-text-property (point) 'time))
  20102. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20103. (setq hod (string-to-number (match-string 1 tp))
  20104. mod (string-to-number (match-string 2 tp))))
  20105. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20106. mod (nth 1 (decode-time (current-time))))))
  20107. (cond
  20108. ((eq major-mode 'calendar-mode)
  20109. (setq date (calendar-cursor-to-date)
  20110. defd (encode-time 0 (or mod 0) (or hod 0)
  20111. (nth 1 date) (nth 0 date) (nth 2 date))))
  20112. ((eq major-mode 'org-agenda-mode)
  20113. (setq day (get-text-property (point) 'day))
  20114. (if day
  20115. (setq date (calendar-gregorian-from-absolute day)
  20116. defd (encode-time 0 (or mod 0) (or hod 0)
  20117. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20118. (or defd (current-time))))
  20119. (defun org-mark-subtree (&optional up)
  20120. "Mark the current subtree.
  20121. This puts point at the start of the current subtree, and mark at
  20122. the end. If a numeric prefix UP is given, move up into the
  20123. hierarchy of headlines by UP levels before marking the subtree."
  20124. (interactive "P")
  20125. (org-with-limited-levels
  20126. (cond ((org-at-heading-p) (beginning-of-line))
  20127. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20128. (t (outline-previous-visible-heading 1))))
  20129. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20130. (if (org-called-interactively-p 'any)
  20131. (call-interactively 'org-mark-element)
  20132. (org-mark-element)))
  20133. ;;; Indentation
  20134. (defun org-indent-line ()
  20135. "Indent line depending on context."
  20136. (interactive)
  20137. (let* ((pos (point))
  20138. (itemp (org-at-item-p))
  20139. (case-fold-search t)
  20140. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20141. (inline-task-p (and (featurep 'org-inlinetask)
  20142. (org-inlinetask-in-task-p)))
  20143. (inline-re (and inline-task-p
  20144. (org-inlinetask-outline-regexp)))
  20145. column)
  20146. (if (and orgstruct-is-++ (eq pos (point)))
  20147. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20148. (indent-according-to-mode))
  20149. (beginning-of-line 1)
  20150. (cond
  20151. ;; Headings
  20152. ((looking-at org-outline-regexp) (setq column 0))
  20153. ;; Footnote definition
  20154. ((looking-at org-footnote-definition-re) (setq column 0))
  20155. ;; Literal examples
  20156. ((looking-at "[ \t]*:\\( \\|$\\)")
  20157. (setq column (org-get-indentation))) ; do nothing
  20158. ;; Lists
  20159. ((ignore-errors (goto-char (org-in-item-p)))
  20160. (setq column (if itemp
  20161. (org-get-indentation)
  20162. (org-list-item-body-column (point))))
  20163. (goto-char pos))
  20164. ;; Drawers
  20165. ((and (looking-at "[ \t]*:END:")
  20166. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20167. (save-excursion
  20168. (goto-char (1- (match-beginning 1)))
  20169. (setq column (current-column))))
  20170. ;; Special blocks
  20171. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20172. (save-excursion
  20173. (re-search-backward
  20174. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20175. (setq column (org-get-indentation (match-string 0))))
  20176. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20177. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20178. (save-excursion
  20179. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20180. (setq column
  20181. (cond ((equal (downcase (match-string 1)) "src")
  20182. ;; src blocks: let `org-edit-src-exit' handle them
  20183. (org-get-indentation))
  20184. ((equal (downcase (match-string 1)) "example")
  20185. (max (org-get-indentation)
  20186. (org-get-indentation (match-string 0))))
  20187. (t
  20188. (org-get-indentation (match-string 0))))))
  20189. ;; This line has nothing special, look at the previous relevant
  20190. ;; line to compute indentation
  20191. (t
  20192. (beginning-of-line 0)
  20193. (while (and (not (bobp))
  20194. (not (looking-at org-table-line-regexp))
  20195. (not (looking-at org-drawer-regexp))
  20196. ;; When point started in an inline task, do not move
  20197. ;; above task starting line.
  20198. (not (and inline-task-p (looking-at inline-re)))
  20199. ;; Skip drawers, blocks, empty lines, verbatim,
  20200. ;; comments, tables, footnotes definitions, lists,
  20201. ;; inline tasks.
  20202. (or (and (looking-at "[ \t]*:END:")
  20203. (re-search-backward org-drawer-regexp nil t))
  20204. (and (looking-at "[ \t]*#\\+end_")
  20205. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20206. (looking-at "[ \t]*[\n:#|]")
  20207. (looking-at org-footnote-definition-re)
  20208. (and (not inline-task-p)
  20209. (featurep 'org-inlinetask)
  20210. (org-inlinetask-in-task-p)
  20211. (or (org-inlinetask-goto-beginning) t))))
  20212. (beginning-of-line 0))
  20213. (cond
  20214. ;; There was a list item above.
  20215. ((save-excursion
  20216. (and (ignore-errors (goto-char (org-in-item-p)))
  20217. (goto-char
  20218. (org-list-get-top-point (org-list-struct)))))
  20219. (looking-at org-list-full-item-re)
  20220. (setq column (length (match-string 0))))
  20221. ;; There was an heading above.
  20222. ((looking-at "\\*+[ \t]+")
  20223. (if (not org-adapt-indentation)
  20224. (setq column 0)
  20225. (goto-char (match-end 0))
  20226. (setq column (current-column))))
  20227. ;; A drawer had started and is unfinished
  20228. ((looking-at org-drawer-regexp)
  20229. (goto-char (1- (match-beginning 1)))
  20230. (setq column (current-column)))
  20231. ;; Else, nothing noticeable found: get indentation and go on.
  20232. (t (setq column (org-get-indentation))))))
  20233. ;; Now apply indentation and move cursor accordingly
  20234. (goto-char pos)
  20235. (if (<= (current-column) (current-indentation))
  20236. (org-indent-line-to column)
  20237. (save-excursion (org-indent-line-to column)))
  20238. ;; Special polishing for properties, see `org-property-format'
  20239. (setq column (current-column))
  20240. (beginning-of-line 1)
  20241. (if (looking-at
  20242. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  20243. (replace-match (concat (match-string 1)
  20244. (format org-property-format
  20245. (match-string 2) (match-string 3)))
  20246. t t))
  20247. (org-move-to-column column))))
  20248. (defun org-indent-drawer ()
  20249. "Indent the drawer at point."
  20250. (interactive)
  20251. (let ((p (point))
  20252. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20253. (match-end 0)))
  20254. (folded
  20255. (save-excursion
  20256. (end-of-line)
  20257. (when (overlays-at (point))
  20258. (member 'invisible (overlay-properties
  20259. (car (overlays-at (point)))))))))
  20260. (when folded (org-cycle))
  20261. (indent-for-tab-command)
  20262. (while (and (move-beginning-of-line 2) (< (point) e))
  20263. (indent-for-tab-command))
  20264. (goto-char p)
  20265. (when folded (org-cycle)))
  20266. (message "Drawer at point indented"))
  20267. (defun org-indent-block ()
  20268. "Indent the block at point."
  20269. (interactive)
  20270. (let ((p (point))
  20271. (case-fold-search t)
  20272. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20273. (match-end 0)))
  20274. (folded
  20275. (save-excursion
  20276. (end-of-line)
  20277. (when (overlays-at (point))
  20278. (member 'invisible (overlay-properties
  20279. (car (overlays-at (point)))))))))
  20280. (when folded (org-cycle))
  20281. (indent-for-tab-command)
  20282. (while (and (move-beginning-of-line 2) (< (point) e))
  20283. (indent-for-tab-command))
  20284. (goto-char p)
  20285. (when folded (org-cycle)))
  20286. (message "Block at point indented"))
  20287. (defun org-indent-region (start end)
  20288. "Indent region."
  20289. (interactive "r")
  20290. (save-excursion
  20291. (let ((line-end (org-current-line end)))
  20292. (goto-char start)
  20293. (while (< (org-current-line) line-end)
  20294. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  20295. (t (call-interactively 'org-indent-line)))
  20296. (move-beginning-of-line 2)))))
  20297. ;;; Filling
  20298. ;; We use our own fill-paragraph and auto-fill functions.
  20299. ;; `org-fill-paragraph' relies on adaptive filling and context
  20300. ;; checking. Appropriate `fill-prefix' is computed with
  20301. ;; `org-adaptive-fill-function'.
  20302. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20303. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20304. ;; `org-adaptive-fill-function' value. Internally,
  20305. ;; `org-comment-line-break-function' breaks the line.
  20306. ;; `org-setup-filling' installs filling and auto-filling related
  20307. ;; variables during `org-mode' initialization.
  20308. (defun org-setup-filling ()
  20309. (interactive)
  20310. ;; Prevent auto-fill from inserting unwanted new items.
  20311. (when (boundp 'fill-nobreak-predicate)
  20312. (org-set-local
  20313. 'fill-nobreak-predicate
  20314. (org-uniquify
  20315. (append fill-nobreak-predicate
  20316. '(org-fill-paragraph-separate-nobreak-p
  20317. org-fill-line-break-nobreak-p
  20318. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20319. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20320. (org-set-local 'auto-fill-inhibit-regexp nil)
  20321. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20322. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20323. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20324. (defvar org-element-paragraph-separate) ; org-element.el
  20325. (defun org-fill-paragraph-separate-nobreak-p ()
  20326. "Non-nil when a line break at point would insert a new item."
  20327. (looking-at (substring org-element-paragraph-separate 1)))
  20328. (defun org-fill-line-break-nobreak-p ()
  20329. "Non-nil when a line break at point would create an Org line break."
  20330. (save-excursion
  20331. (skip-chars-backward "[ \t]")
  20332. (skip-chars-backward "\\\\")
  20333. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20334. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20335. "Non-nil when a line break at point would insert a new item."
  20336. (and (org-at-timestamp-p t)
  20337. (not (looking-at org-ts-regexp-both))))
  20338. (declare-function message-in-body-p "message" ())
  20339. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20340. (defun org-adaptive-fill-function ()
  20341. "Compute a fill prefix for the current line.
  20342. Return fill prefix, as a string, or nil if current line isn't
  20343. meant to be filled."
  20344. (let (prefix)
  20345. (catch 'exit
  20346. (when (derived-mode-p 'message-mode)
  20347. (save-excursion
  20348. (beginning-of-line)
  20349. (cond ((or (not (message-in-body-p))
  20350. (looking-at orgtbl-line-start-regexp))
  20351. (throw 'exit nil))
  20352. ((looking-at message-cite-prefix-regexp)
  20353. (throw 'exit (match-string-no-properties 0)))
  20354. ((looking-at org-outline-regexp)
  20355. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20356. (org-with-wide-buffer
  20357. (let* ((p (line-beginning-position))
  20358. (element (save-excursion
  20359. (beginning-of-line)
  20360. (or (ignore-errors (org-element-at-point))
  20361. (user-error "An element cannot be parsed line %d"
  20362. (line-number-at-pos (point))))))
  20363. (type (org-element-type element))
  20364. (post-affiliated (org-element-property :post-affiliated element)))
  20365. (unless (and post-affiliated (< p post-affiliated))
  20366. (case type
  20367. (comment (looking-at "[ \t]*# ?") (match-string 0))
  20368. (footnote-definition "")
  20369. ((item plain-list)
  20370. (make-string (org-list-item-body-column
  20371. (or post-affiliated
  20372. (org-element-property :begin element)))
  20373. ? ))
  20374. (paragraph
  20375. ;; Fill prefix is usually the same as the current line,
  20376. ;; except if the paragraph is at the beginning of an item.
  20377. (let ((parent (org-element-property :parent element)))
  20378. (cond ((eq (org-element-type parent) 'item)
  20379. (make-string (org-list-item-body-column
  20380. (org-element-property :begin parent))
  20381. ? ))
  20382. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20383. (match-string 0))
  20384. (t ""))))
  20385. (comment-block
  20386. ;; Only fill contents if P is within block boundaries.
  20387. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20388. (forward-line)
  20389. (point)))
  20390. (cend (save-excursion
  20391. (goto-char (org-element-property :end element))
  20392. (skip-chars-backward " \r\t\n")
  20393. (line-beginning-position))))
  20394. (when (and (>= p cbeg) (< p cend))
  20395. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20396. (match-string 0)
  20397. "")))))))))))
  20398. (declare-function message-goto-body "message" ())
  20399. (defvar message-cite-prefix-regexp) ; From message.el
  20400. (defun org-fill-paragraph (&optional justify)
  20401. "Fill element at point, when applicable.
  20402. This function only applies to comment blocks, comments, example
  20403. blocks and paragraphs. Also, as a special case, re-align table
  20404. when point is at one.
  20405. If JUSTIFY is non-nil (interactively, with prefix argument),
  20406. justify as well. If `sentence-end-double-space' is non-nil, then
  20407. period followed by one space does not end a sentence, so don't
  20408. break a line there. The variable `fill-column' controls the
  20409. width for filling.
  20410. For convenience, when point is at a plain list, an item or
  20411. a footnote definition, try to fill the first paragraph within."
  20412. (interactive)
  20413. (if (and (derived-mode-p 'message-mode)
  20414. (or (not (message-in-body-p))
  20415. (save-excursion (move-beginning-of-line 1)
  20416. (looking-at message-cite-prefix-regexp))))
  20417. ;; First ensure filling is correct in message-mode.
  20418. (let ((fill-paragraph-function
  20419. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20420. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20421. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20422. (paragraph-separate
  20423. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20424. (fill-paragraph nil))
  20425. (with-syntax-table org-mode-transpose-word-syntax-table
  20426. ;; Move to end of line in order to get the first paragraph
  20427. ;; within a plain list or a footnote definition.
  20428. (let ((element (save-excursion
  20429. (end-of-line)
  20430. (or (ignore-errors (org-element-at-point))
  20431. (user-error "An element cannot be parsed line %d"
  20432. (line-number-at-pos (point)))))))
  20433. ;; First check if point is in a blank line at the beginning of
  20434. ;; the buffer. In that case, ignore filling.
  20435. (case (org-element-type element)
  20436. ;; Use major mode filling function is src blocks.
  20437. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20438. ;; Align Org tables, leave table.el tables as-is.
  20439. (table-row (org-table-align) t)
  20440. (table
  20441. (when (eq (org-element-property :type element) 'org)
  20442. (save-excursion
  20443. (goto-char (org-element-property :post-affiliated element))
  20444. (org-table-align)))
  20445. t)
  20446. (paragraph
  20447. ;; Paragraphs may contain `line-break' type objects.
  20448. (let ((beg (max (point-min)
  20449. (org-element-property :contents-begin element)))
  20450. (end (min (point-max)
  20451. (org-element-property :contents-end element))))
  20452. ;; Do nothing if point is at an affiliated keyword.
  20453. (if (< (line-end-position) beg) t
  20454. (when (derived-mode-p 'message-mode)
  20455. ;; In `message-mode', do not fill following citation
  20456. ;; in current paragraph nor text before message body.
  20457. (let ((body-start (save-excursion (message-goto-body))))
  20458. (when body-start (setq beg (max body-start beg))))
  20459. (when (save-excursion
  20460. (re-search-forward
  20461. (concat "^" message-cite-prefix-regexp) end t))
  20462. (setq end (match-beginning 0))))
  20463. ;; Fill paragraph, taking line breaks into account.
  20464. ;; For that, slice the paragraph using line breaks as
  20465. ;; separators, and fill the parts in reverse order to
  20466. ;; avoid messing with markers.
  20467. (save-excursion
  20468. (goto-char end)
  20469. (mapc
  20470. (lambda (pos)
  20471. (fill-region-as-paragraph pos (point) justify)
  20472. (goto-char pos))
  20473. ;; Find the list of ending positions for line breaks
  20474. ;; in the current paragraph. Add paragraph
  20475. ;; beginning to include first slice.
  20476. (nreverse
  20477. (cons beg
  20478. (org-element-map
  20479. (org-element--parse-objects
  20480. beg end nil (org-element-restriction 'paragraph))
  20481. 'line-break
  20482. (lambda (lb) (org-element-property :end lb)))))))
  20483. t)))
  20484. ;; Contents of `comment-block' type elements should be
  20485. ;; filled as plain text, but only if point is within block
  20486. ;; markers.
  20487. (comment-block
  20488. (let* ((case-fold-search t)
  20489. (beg (save-excursion
  20490. (goto-char (org-element-property :begin element))
  20491. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20492. (forward-line)
  20493. (point)))
  20494. (end (save-excursion
  20495. (goto-char (org-element-property :end element))
  20496. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20497. (line-beginning-position))))
  20498. (when (and (>= (point) beg) (< (point) end))
  20499. (fill-region-as-paragraph
  20500. (save-excursion
  20501. (end-of-line)
  20502. (re-search-backward "^[ \t]*$" beg 'move)
  20503. (line-beginning-position))
  20504. (save-excursion
  20505. (beginning-of-line)
  20506. (re-search-forward "^[ \t]*$" end 'move)
  20507. (line-beginning-position))
  20508. justify)))
  20509. t)
  20510. ;; Fill comments.
  20511. (comment (fill-comment-paragraph justify))
  20512. ;; Ignore every other element.
  20513. (otherwise t))))))
  20514. (defun org-auto-fill-function ()
  20515. "Auto-fill function."
  20516. ;; Check if auto-filling is meaningful.
  20517. (let ((fc (current-fill-column)))
  20518. (when (and fc (> (current-column) fc))
  20519. (let* ((fill-prefix (org-adaptive-fill-function))
  20520. ;; Enforce empty fill prefix, if required. Otherwise, it
  20521. ;; will be computed again.
  20522. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20523. (when fill-prefix (do-auto-fill))))))
  20524. (defun org-comment-line-break-function (&optional soft)
  20525. "Break line at point and indent, continuing comment if within one.
  20526. The inserted newline is marked hard if variable
  20527. `use-hard-newlines' is true, unless optional argument SOFT is
  20528. non-nil."
  20529. (if soft (insert-and-inherit ?\n) (newline 1))
  20530. (save-excursion (forward-char -1) (delete-horizontal-space))
  20531. (delete-horizontal-space)
  20532. (indent-to-left-margin)
  20533. (insert-before-markers-and-inherit fill-prefix))
  20534. ;;; Comments
  20535. ;; Org comments syntax is quite complex. It requires the entire line
  20536. ;; to be just a comment. Also, even with the right syntax at the
  20537. ;; beginning of line, some some elements (i.e. verse-block or
  20538. ;; example-block) don't accept comments. Usual Emacs comment commands
  20539. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20540. ;; Org still relies on `comment-dwim', but cannot trust
  20541. ;; `comment-only-p'. So, `comment-region-function' and
  20542. ;; `uncomment-region-function' both point
  20543. ;; to`org-comment-or-uncomment-region'. Eventually,
  20544. ;; `org-insert-comment' takes care of insertion of comments at the
  20545. ;; beginning of line.
  20546. ;; `org-setup-comments-handling' install comments related variables
  20547. ;; during `org-mode' initialization.
  20548. (defun org-setup-comments-handling ()
  20549. (interactive)
  20550. (org-set-local 'comment-use-syntax nil)
  20551. (org-set-local 'comment-start "# ")
  20552. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20553. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20554. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20555. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20556. (defun org-insert-comment ()
  20557. "Insert an empty comment above current line.
  20558. If the line is empty, insert comment at its beginning."
  20559. (beginning-of-line)
  20560. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20561. (org-indent-line)
  20562. (insert "# "))
  20563. (defvar comment-empty-lines) ; From newcomment.el.
  20564. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20565. "Comment or uncomment each non-blank line in the region.
  20566. Uncomment each non-blank line between BEG and END if it only
  20567. contains commented lines. Otherwise, comment them."
  20568. (save-restriction
  20569. ;; Restrict region
  20570. (narrow-to-region (save-excursion (goto-char beg)
  20571. (skip-chars-forward " \r\t\n" end)
  20572. (line-beginning-position))
  20573. (save-excursion (goto-char end)
  20574. (skip-chars-backward " \r\t\n" beg)
  20575. (line-end-position)))
  20576. (let ((uncommentp
  20577. ;; UNCOMMENTP is non-nil when every non blank line between
  20578. ;; BEG and END is a comment.
  20579. (save-excursion
  20580. (goto-char (point-min))
  20581. (while (and (not (eobp))
  20582. (let ((element (org-element-at-point)))
  20583. (and (eq (org-element-type element) 'comment)
  20584. (goto-char (min (point-max)
  20585. (org-element-property
  20586. :end element)))))))
  20587. (eobp))))
  20588. (if uncommentp
  20589. ;; Only blank lines and comments in region: uncomment it.
  20590. (save-excursion
  20591. (goto-char (point-min))
  20592. (while (not (eobp))
  20593. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20594. (replace-match "" nil nil nil 1))
  20595. (forward-line)))
  20596. ;; Comment each line in region.
  20597. (let ((min-indent (point-max)))
  20598. ;; First find the minimum indentation across all lines.
  20599. (save-excursion
  20600. (goto-char (point-min))
  20601. (while (and (not (eobp)) (not (zerop min-indent)))
  20602. (unless (looking-at "[ \t]*$")
  20603. (setq min-indent (min min-indent (current-indentation))))
  20604. (forward-line)))
  20605. ;; Then loop over all lines.
  20606. (save-excursion
  20607. (goto-char (point-min))
  20608. (while (not (eobp))
  20609. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20610. ;; Don't get fooled by invisible text (e.g. link path)
  20611. ;; when moving to column MIN-INDENT.
  20612. (let ((buffer-invisibility-spec nil))
  20613. (org-move-to-column min-indent t))
  20614. (insert comment-start))
  20615. (forward-line))))))))
  20616. ;;; Planning
  20617. ;; This section contains tools to operate on timestamp objects, as
  20618. ;; returned by, e.g. `org-element-context'.
  20619. (defun org-timestamp-has-time-p (timestamp)
  20620. "Non-nil when TIMESTAMP has a time specified."
  20621. (org-element-property :hour-start timestamp))
  20622. (defun org-timestamp-format (timestamp format &optional end utc)
  20623. "Format a TIMESTAMP element into a string.
  20624. FORMAT is a format specifier to be passed to
  20625. `format-time-string'.
  20626. When optional argument END is non-nil, use end of date-range or
  20627. time-range, if possible.
  20628. When optional argument UTC is non-nil, time will be expressed as
  20629. Universal Time."
  20630. (format-time-string
  20631. format
  20632. (apply 'encode-time
  20633. (cons 0
  20634. (mapcar
  20635. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20636. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20637. '(:minute-start :hour-start :day-start :month-start
  20638. :year-start)))))
  20639. utc))
  20640. (defun org-timestamp-split-range (timestamp &optional end)
  20641. "Extract a timestamp object from a date or time range.
  20642. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20643. the end of the range. Otherwise, extract its start.
  20644. Return a new timestamp object sharing the same parent as
  20645. TIMESTAMP."
  20646. (let ((type (org-element-property :type timestamp)))
  20647. (if (memq type '(active inactive diary)) timestamp
  20648. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20649. ;; Set new type.
  20650. (org-element-put-property
  20651. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20652. ;; Copy start properties over end properties if END is
  20653. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20654. (let ((p-alist '((:minute-start . :minute-end)
  20655. (:hour-start . :hour-end)
  20656. (:day-start . :day-end)
  20657. (:month-start . :month-end)
  20658. (:year-start . :year-end))))
  20659. (dolist (p-cell p-alist)
  20660. (org-element-put-property
  20661. split-ts
  20662. (funcall (if end 'car 'cdr) p-cell)
  20663. (org-element-property
  20664. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20665. ;; Eventually refresh `:raw-value'.
  20666. (org-element-put-property split-ts :raw-value nil)
  20667. (org-element-put-property
  20668. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20669. (defun org-timestamp-translate (timestamp &optional boundary)
  20670. "Apply `org-translate-time' on a TIMESTAMP object.
  20671. When optional argument BOUNDARY is non-nil, it is either the
  20672. symbol `start' or `end'. In this case, only translate the
  20673. starting or ending part of TIMESTAMP if it is a date or time
  20674. range. Otherwise, translate both parts."
  20675. (if (and (not boundary)
  20676. (memq (org-element-property :type timestamp)
  20677. '(active-range inactive-range)))
  20678. (concat
  20679. (org-translate-time
  20680. (org-element-property :raw-value
  20681. (org-timestamp-split-range timestamp)))
  20682. "--"
  20683. (org-translate-time
  20684. (org-element-property :raw-value
  20685. (org-timestamp-split-range timestamp t))))
  20686. (org-translate-time
  20687. (org-element-property
  20688. :raw-value
  20689. (if (not boundary) timestamp
  20690. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20691. ;;; Other stuff.
  20692. (defun org-toggle-fixed-width-section (arg)
  20693. "Toggle the fixed-width export.
  20694. If there is no active region, the QUOTE keyword at the current headline is
  20695. inserted or removed. When present, it causes the text between this headline
  20696. and the next to be exported as fixed-width text, and unmodified.
  20697. If there is an active region, this command adds or removes a colon as the
  20698. first character of this line. If the first character of a line is a colon,
  20699. this line is also exported in fixed-width font."
  20700. (interactive "P")
  20701. (let* ((cc 0)
  20702. (regionp (org-region-active-p))
  20703. (beg (if regionp (region-beginning) (point)))
  20704. (end (if regionp (region-end)))
  20705. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20706. (case-fold-search nil)
  20707. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20708. off)
  20709. (if regionp
  20710. (save-excursion
  20711. (goto-char beg)
  20712. (setq cc (current-column))
  20713. (beginning-of-line 1)
  20714. (setq off (looking-at re))
  20715. (while (> nlines 0)
  20716. (setq nlines (1- nlines))
  20717. (beginning-of-line 1)
  20718. (cond
  20719. (arg
  20720. (org-move-to-column cc t)
  20721. (insert ": \n")
  20722. (forward-line -1))
  20723. ((and off (looking-at re))
  20724. (replace-match "" t t nil 1))
  20725. ((not off) (org-move-to-column cc t) (insert ": ")))
  20726. (forward-line 1)))
  20727. (save-excursion
  20728. (org-back-to-heading)
  20729. (cond
  20730. ((looking-at (format org-heading-keyword-regexp-format
  20731. org-quote-string))
  20732. (goto-char (match-end 1))
  20733. (looking-at (concat " +" org-quote-string))
  20734. (replace-match "" t t)
  20735. (when (eolp) (insert " ")))
  20736. ((looking-at org-outline-regexp)
  20737. (goto-char (match-end 0))
  20738. (insert org-quote-string " ")))))))
  20739. (defun org-reftex-citation ()
  20740. "Use reftex-citation to insert a citation into the buffer.
  20741. This looks for a line like
  20742. #+BIBLIOGRAPHY: foo plain option:-d
  20743. and derives from it that foo.bib is the bibliography file relevant
  20744. for this document. It then installs the necessary environment for RefTeX
  20745. to work in this buffer and calls `reftex-citation' to insert a citation
  20746. into the buffer.
  20747. Export of such citations to both LaTeX and HTML is handled by the contributed
  20748. package org-exp-bibtex by Taru Karttunen."
  20749. (interactive)
  20750. (let ((reftex-docstruct-symbol 'rds)
  20751. (reftex-cite-format "\\cite{%l}")
  20752. rds bib)
  20753. (save-excursion
  20754. (save-restriction
  20755. (widen)
  20756. (let ((case-fold-search t)
  20757. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20758. (if (not (save-excursion
  20759. (or (re-search-forward re nil t)
  20760. (re-search-backward re nil t))))
  20761. (error "No bibliography defined in file")
  20762. (setq bib (concat (match-string 1) ".bib")
  20763. rds (list (list 'bib bib)))))))
  20764. (call-interactively 'reftex-citation)))
  20765. ;;;; Functions extending outline functionality
  20766. (defun org-beginning-of-line (&optional arg)
  20767. "Go to the beginning of the current line. If that is invisible, continue
  20768. to a visible line beginning. This makes the function of C-a more intuitive.
  20769. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20770. first attempt, and only move to after the tags when the cursor is already
  20771. beyond the end of the headline."
  20772. (interactive "P")
  20773. (let ((pos (point))
  20774. (special (if (consp org-special-ctrl-a/e)
  20775. (car org-special-ctrl-a/e)
  20776. org-special-ctrl-a/e))
  20777. refpos)
  20778. (if (org-bound-and-true-p visual-line-mode)
  20779. (beginning-of-visual-line 1)
  20780. (beginning-of-line 1))
  20781. (if (and arg (fboundp 'move-beginning-of-line))
  20782. (call-interactively 'move-beginning-of-line)
  20783. (if (bobp)
  20784. nil
  20785. (backward-char 1)
  20786. (if (org-truely-invisible-p)
  20787. (while (and (not (bobp)) (org-truely-invisible-p))
  20788. (backward-char 1)
  20789. (beginning-of-line 1))
  20790. (forward-char 1))))
  20791. (when special
  20792. (cond
  20793. ((and (looking-at org-complex-heading-regexp)
  20794. (= (char-after (match-end 1)) ?\ ))
  20795. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20796. (point-at-eol)))
  20797. (goto-char
  20798. (if (eq special t)
  20799. (cond ((> pos refpos) refpos)
  20800. ((= pos (point)) refpos)
  20801. (t (point)))
  20802. (cond ((> pos (point)) (point))
  20803. ((not (eq last-command this-command)) (point))
  20804. (t refpos)))))
  20805. ((org-at-item-p)
  20806. ;; Being at an item and not looking at an the item means point
  20807. ;; was previously moved to beginning of a visual line, which
  20808. ;; doesn't contain the item. Therefore, do nothing special,
  20809. ;; just stay here.
  20810. (when (looking-at org-list-full-item-re)
  20811. ;; Set special position at first white space character after
  20812. ;; bullet, and check-box, if any.
  20813. (let ((after-bullet
  20814. (let ((box (match-end 3)))
  20815. (if (not box) (match-end 1)
  20816. (let ((after (char-after box)))
  20817. (if (and after (= after ? )) (1+ box) box))))))
  20818. ;; Special case: Move point to special position when
  20819. ;; currently after it or at beginning of line.
  20820. (if (eq special t)
  20821. (when (or (> pos after-bullet) (= (point) pos))
  20822. (goto-char after-bullet))
  20823. ;; Reversed case: Move point to special position when
  20824. ;; point was already at beginning of line and command is
  20825. ;; repeated.
  20826. (when (and (= (point) pos) (eq last-command this-command))
  20827. (goto-char after-bullet))))))))
  20828. (org-no-warnings
  20829. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  20830. (setq disable-point-adjustment
  20831. (or (not (invisible-p (point)))
  20832. (not (invisible-p (max (point-min) (1- (point))))))))
  20833. (defun org-end-of-line (&optional arg)
  20834. "Go to the end of the line.
  20835. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20836. tags on the first attempt, and only move to after the tags when
  20837. the cursor is already beyond the end of the headline."
  20838. (interactive "P")
  20839. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20840. org-special-ctrl-a/e))
  20841. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20842. 'end-of-visual-line)
  20843. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20844. (t 'end-of-line))))
  20845. (if (or (not special) arg) (call-interactively move-fun)
  20846. (let* ((element (save-excursion (beginning-of-line)
  20847. (org-element-at-point)))
  20848. (type (org-element-type element)))
  20849. (cond
  20850. ((memq type '(headline inlinetask))
  20851. (let ((pos (point)))
  20852. (beginning-of-line 1)
  20853. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20854. (if (eq special t)
  20855. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20856. (goto-char (match-beginning 1))
  20857. (goto-char (match-end 0)))
  20858. (if (or (< pos (match-end 0))
  20859. (not (eq this-command last-command)))
  20860. (goto-char (match-end 0))
  20861. (goto-char (match-beginning 1))))
  20862. (call-interactively move-fun))))
  20863. ((org-element-property :hiddenp element)
  20864. ;; If element is hidden, `move-end-of-line' would put point
  20865. ;; after it. Use `end-of-line' to stay on current line.
  20866. (call-interactively 'end-of-line))
  20867. (t (call-interactively move-fun)))))
  20868. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  20869. (setq disable-point-adjustment
  20870. (or (not (invisible-p (point)))
  20871. (not (invisible-p (max (point-min) (1- (point))))))))
  20872. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20873. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20874. (defun org-backward-sentence (&optional arg)
  20875. "Go to beginning of sentence, or beginning of table field.
  20876. This will call `backward-sentence' or `org-table-beginning-of-field',
  20877. depending on context."
  20878. (interactive "P")
  20879. (cond
  20880. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20881. (t (call-interactively 'backward-sentence))))
  20882. (defun org-forward-sentence (&optional arg)
  20883. "Go to end of sentence, or end of table field.
  20884. This will call `forward-sentence' or `org-table-end-of-field',
  20885. depending on context."
  20886. (interactive "P")
  20887. (cond
  20888. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20889. (t (call-interactively 'forward-sentence))))
  20890. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20891. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20892. (defun org-kill-line (&optional arg)
  20893. "Kill line, to tags or end of line."
  20894. (interactive "P")
  20895. (cond
  20896. ((or (not org-special-ctrl-k)
  20897. (bolp)
  20898. (not (org-at-heading-p)))
  20899. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20900. org-ctrl-k-protect-subtree)
  20901. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20902. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20903. (user-error "C-k aborted as it would kill a hidden subtree")))
  20904. (call-interactively
  20905. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20906. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20907. (kill-region (point) (match-beginning 1))
  20908. (org-set-tags nil t))
  20909. (t (kill-region (point) (point-at-eol)))))
  20910. (define-key org-mode-map "\C-k" 'org-kill-line)
  20911. (defun org-yank (&optional arg)
  20912. "Yank. If the kill is a subtree, treat it specially.
  20913. This command will look at the current kill and check if is a single
  20914. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20915. cursor is at the beginning of a line or after the stars of a currently
  20916. empty headline, then the yank is handled specially. How exactly depends
  20917. on the value of the following variables, both set by default.
  20918. org-yank-folded-subtrees
  20919. When set, the subtree(s) will be folded after insertion, but only
  20920. if doing so would now swallow text after the yanked text.
  20921. org-yank-adjusted-subtrees
  20922. When set, the subtree will be promoted or demoted in order to
  20923. fit into the local outline tree structure, which means that the level
  20924. will be adjusted so that it becomes the smaller one of the two
  20925. *visible* surrounding headings.
  20926. Any prefix to this command will cause `yank' to be called directly with
  20927. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20928. will just
  20929. plainly yank the text as it is.
  20930. \[1] The test checks if the first non-white line is a heading
  20931. and if there are no other headings with fewer stars."
  20932. (interactive "P")
  20933. (org-yank-generic 'yank arg))
  20934. (defun org-yank-generic (command arg)
  20935. "Perform some yank-like command.
  20936. This function implements the behavior described in the `org-yank'
  20937. documentation. However, it has been generalized to work for any
  20938. interactive command with similar behavior."
  20939. ;; pretend to be command COMMAND
  20940. (setq this-command command)
  20941. (if arg
  20942. (call-interactively command)
  20943. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20944. (and (org-kill-is-subtree-p)
  20945. (or (bolp)
  20946. (and (looking-at "[ \t]*$")
  20947. (string-match
  20948. "\\`\\*+\\'"
  20949. (buffer-substring (point-at-bol) (point)))))))
  20950. swallowp)
  20951. (cond
  20952. ((and subtreep org-yank-folded-subtrees)
  20953. (let ((beg (point))
  20954. end)
  20955. (if (and subtreep org-yank-adjusted-subtrees)
  20956. (org-paste-subtree nil nil 'for-yank)
  20957. (call-interactively command))
  20958. (setq end (point))
  20959. (goto-char beg)
  20960. (when (and (bolp) subtreep
  20961. (not (setq swallowp
  20962. (org-yank-folding-would-swallow-text beg end))))
  20963. (org-with-limited-levels
  20964. (or (looking-at org-outline-regexp)
  20965. (re-search-forward org-outline-regexp-bol end t))
  20966. (while (and (< (point) end) (looking-at org-outline-regexp))
  20967. (hide-subtree)
  20968. (org-cycle-show-empty-lines 'folded)
  20969. (condition-case nil
  20970. (outline-forward-same-level 1)
  20971. (error (goto-char end))))))
  20972. (when swallowp
  20973. (message
  20974. "Inserted text not folded because that would swallow text"))
  20975. (goto-char end)
  20976. (skip-chars-forward " \t\n\r")
  20977. (beginning-of-line 1)
  20978. (push-mark beg 'nomsg)))
  20979. ((and subtreep org-yank-adjusted-subtrees)
  20980. (let ((beg (point-at-bol)))
  20981. (org-paste-subtree nil nil 'for-yank)
  20982. (push-mark beg 'nomsg)))
  20983. (t
  20984. (call-interactively command))))))
  20985. (defun org-yank-folding-would-swallow-text (beg end)
  20986. "Would hide-subtree at BEG swallow any text after END?"
  20987. (let (level)
  20988. (org-with-limited-levels
  20989. (save-excursion
  20990. (goto-char beg)
  20991. (when (or (looking-at org-outline-regexp)
  20992. (re-search-forward org-outline-regexp-bol end t))
  20993. (setq level (org-outline-level)))
  20994. (goto-char end)
  20995. (skip-chars-forward " \t\r\n\v\f")
  20996. (if (or (eobp)
  20997. (and (bolp) (looking-at org-outline-regexp)
  20998. (<= (org-outline-level) level)))
  20999. nil ; Nothing would be swallowed
  21000. t))))) ; something would swallow
  21001. (define-key org-mode-map "\C-y" 'org-yank)
  21002. (defun org-truely-invisible-p ()
  21003. "Check if point is at a character currently not visible.
  21004. This version does not only check the character property, but also
  21005. `visible-mode'."
  21006. ;; Early versions of noutline don't have `outline-invisible-p'.
  21007. (if (org-bound-and-true-p visible-mode)
  21008. nil
  21009. (outline-invisible-p)))
  21010. (defun org-invisible-p2 ()
  21011. "Check if point is at a character currently not visible."
  21012. (save-excursion
  21013. (if (and (eolp) (not (bobp))) (backward-char 1))
  21014. ;; Early versions of noutline don't have `outline-invisible-p'.
  21015. (outline-invisible-p)))
  21016. (defun org-back-to-heading (&optional invisible-ok)
  21017. "Call `outline-back-to-heading', but provide a better error message."
  21018. (condition-case nil
  21019. (outline-back-to-heading invisible-ok)
  21020. (error (error "Before first headline at position %d in buffer %s"
  21021. (point) (current-buffer)))))
  21022. (defun org-before-first-heading-p ()
  21023. "Before first heading?"
  21024. (save-excursion
  21025. (end-of-line)
  21026. (null (re-search-backward org-outline-regexp-bol nil t))))
  21027. (defun org-at-heading-p (&optional ignored)
  21028. (outline-on-heading-p t))
  21029. ;; Compatibility alias with Org versions < 7.8.03
  21030. (defalias 'org-on-heading-p 'org-at-heading-p)
  21031. (defun org-at-comment-p nil
  21032. "Is cursor in a line starting with a # character?"
  21033. (save-excursion
  21034. (beginning-of-line)
  21035. (looking-at "^#")))
  21036. (defun org-at-drawer-p nil
  21037. "Is cursor at a drawer keyword?"
  21038. (save-excursion
  21039. (move-beginning-of-line 1)
  21040. (looking-at org-drawer-regexp)))
  21041. (defun org-at-block-p nil
  21042. "Is cursor at a block keyword?"
  21043. (save-excursion
  21044. (move-beginning-of-line 1)
  21045. (looking-at org-block-regexp)))
  21046. (defun org-point-at-end-of-empty-headline ()
  21047. "If point is at the end of an empty headline, return t, else nil.
  21048. If the heading only contains a TODO keyword, it is still still considered
  21049. empty."
  21050. (and (looking-at "[ \t]*$")
  21051. (when org-todo-line-regexp
  21052. (save-excursion
  21053. (beginning-of-line 1)
  21054. (let ((case-fold-search nil))
  21055. (looking-at org-todo-line-regexp)
  21056. (string= (match-string 3) ""))))))
  21057. (defun org-at-heading-or-item-p ()
  21058. (or (org-at-heading-p) (org-at-item-p)))
  21059. (defun org-at-target-p ()
  21060. (or (org-in-regexp org-radio-target-regexp)
  21061. (org-in-regexp org-target-regexp)))
  21062. ;; Compatibility alias with Org versions < 7.8.03
  21063. (defalias 'org-on-target-p 'org-at-target-p)
  21064. (defun org-up-heading-all (arg)
  21065. "Move to the heading line of which the present line is a subheading.
  21066. This function considers both visible and invisible heading lines.
  21067. With argument, move up ARG levels."
  21068. (if (fboundp 'outline-up-heading-all)
  21069. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21070. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21071. (defun org-up-heading-safe ()
  21072. "Move to the heading line of which the present line is a subheading.
  21073. This version will not throw an error. It will return the level of the
  21074. headline found, or nil if no higher level is found.
  21075. Also, this function will be a lot faster than `outline-up-heading',
  21076. because it relies on stars being the outline starters. This can really
  21077. make a significant difference in outlines with very many siblings."
  21078. (let (start-level re)
  21079. (org-back-to-heading t)
  21080. (setq start-level (funcall outline-level))
  21081. (if (equal start-level 1)
  21082. nil
  21083. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21084. (if (re-search-backward re nil t)
  21085. (funcall outline-level)))))
  21086. (defun org-first-sibling-p ()
  21087. "Is this heading the first child of its parents?"
  21088. (interactive)
  21089. (let ((re org-outline-regexp-bol)
  21090. level l)
  21091. (unless (org-at-heading-p t)
  21092. (user-error "Not at a heading"))
  21093. (setq level (funcall outline-level))
  21094. (save-excursion
  21095. (if (not (re-search-backward re nil t))
  21096. t
  21097. (setq l (funcall outline-level))
  21098. (< l level)))))
  21099. (defun org-goto-sibling (&optional previous)
  21100. "Goto the next sibling, even if it is invisible.
  21101. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21102. when a sibling was found. When none is found, return nil and don't
  21103. move point."
  21104. (let ((fun (if previous 're-search-backward 're-search-forward))
  21105. (pos (point))
  21106. (re org-outline-regexp-bol)
  21107. level l)
  21108. (when (condition-case nil (org-back-to-heading t) (error nil))
  21109. (setq level (funcall outline-level))
  21110. (catch 'exit
  21111. (or previous (forward-char 1))
  21112. (while (funcall fun re nil t)
  21113. (setq l (funcall outline-level))
  21114. (when (< l level) (goto-char pos) (throw 'exit nil))
  21115. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21116. (goto-char pos)
  21117. nil))))
  21118. (defun org-show-siblings ()
  21119. "Show all siblings of the current headline."
  21120. (save-excursion
  21121. (while (org-goto-sibling) (org-flag-heading nil)))
  21122. (save-excursion
  21123. (while (org-goto-sibling 'previous)
  21124. (org-flag-heading nil))))
  21125. (defun org-goto-first-child ()
  21126. "Goto the first child, even if it is invisible.
  21127. Return t when a child was found. Otherwise don't move point and
  21128. return nil."
  21129. (let (level (pos (point)) (re org-outline-regexp-bol))
  21130. (when (condition-case nil (org-back-to-heading t) (error nil))
  21131. (setq level (outline-level))
  21132. (forward-char 1)
  21133. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21134. (progn (goto-char (match-beginning 0)) t)
  21135. (goto-char pos) nil))))
  21136. (defun org-show-hidden-entry ()
  21137. "Show an entry where even the heading is hidden."
  21138. (save-excursion
  21139. (org-show-entry)))
  21140. (defun org-flag-heading (flag &optional entry)
  21141. "Flag the current heading. FLAG non-nil means make invisible.
  21142. When ENTRY is non-nil, show the entire entry."
  21143. (save-excursion
  21144. (org-back-to-heading t)
  21145. ;; Check if we should show the entire entry
  21146. (if entry
  21147. (progn
  21148. (org-show-entry)
  21149. (save-excursion
  21150. (and (outline-next-heading)
  21151. (org-flag-heading nil))))
  21152. (outline-flag-region (max (point-min) (1- (point)))
  21153. (save-excursion (outline-end-of-heading) (point))
  21154. flag))))
  21155. (defun org-get-next-sibling ()
  21156. "Move to next heading of the same level, and return point.
  21157. If there is no such heading, return nil.
  21158. This is like outline-next-sibling, but invisible headings are ok."
  21159. (let ((level (funcall outline-level)))
  21160. (outline-next-heading)
  21161. (while (and (not (eobp)) (> (funcall outline-level) level))
  21162. (outline-next-heading))
  21163. (if (or (eobp) (< (funcall outline-level) level))
  21164. nil
  21165. (point))))
  21166. (defun org-get-last-sibling ()
  21167. "Move to previous heading of the same level, and return point.
  21168. If there is no such heading, return nil."
  21169. (let ((opoint (point))
  21170. (level (funcall outline-level)))
  21171. (outline-previous-heading)
  21172. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21173. (while (and (> (funcall outline-level) level)
  21174. (not (bobp)))
  21175. (outline-previous-heading))
  21176. (if (< (funcall outline-level) level)
  21177. nil
  21178. (point)))))
  21179. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21180. "Goto to the end of a subtree."
  21181. ;; This contains an exact copy of the original function, but it uses
  21182. ;; `org-back-to-heading', to make it work also in invisible
  21183. ;; trees. And is uses an invisible-ok argument.
  21184. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21185. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21186. ;; with many children and grandchildren etc, this can be much faster
  21187. ;; than the outline version.
  21188. (org-back-to-heading invisible-ok)
  21189. (let ((first t)
  21190. (level (funcall outline-level)))
  21191. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21192. ;; A true heading (not a plain list item), in Org-mode
  21193. ;; This means we can easily find the end by looking
  21194. ;; only for the right number of stars. Using a regexp to do
  21195. ;; this is so much faster than using a Lisp loop.
  21196. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21197. (forward-char 1)
  21198. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21199. ;; something else, do it the slow way
  21200. (while (and (not (eobp))
  21201. (or first (> (funcall outline-level) level)))
  21202. (setq first nil)
  21203. (outline-next-heading)))
  21204. (unless to-heading
  21205. (if (memq (preceding-char) '(?\n ?\^M))
  21206. (progn
  21207. ;; Go to end of line before heading
  21208. (forward-char -1)
  21209. (if (memq (preceding-char) '(?\n ?\^M))
  21210. ;; leave blank line before heading
  21211. (forward-char -1))))))
  21212. (point))
  21213. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  21214. "Use Org version in org-mode, for dramatic speed-up."
  21215. (if (derived-mode-p 'org-mode)
  21216. (progn
  21217. (org-end-of-subtree nil t)
  21218. (unless (eobp) (backward-char 1)))
  21219. ad-do-it))
  21220. (defun org-end-of-meta-data-and-drawers ()
  21221. "Jump to the first text after meta data and drawers in the current entry.
  21222. This will move over empty lines, lines with planning time stamps,
  21223. clocking lines, and drawers."
  21224. (org-back-to-heading t)
  21225. (let ((end (save-excursion (outline-next-heading) (point)))
  21226. (re (concat "\\(" org-drawer-regexp "\\)"
  21227. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21228. (forward-line 1)
  21229. (while (re-search-forward re end t)
  21230. (if (not (match-end 1))
  21231. ;; empty or planning line
  21232. (forward-line 1)
  21233. ;; a drawer, find the end
  21234. (re-search-forward "^[ \t]*:END:" end 'move)
  21235. (forward-line 1)))
  21236. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21237. (point)))
  21238. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21239. "Move forward to the ARG'th subheading at same level as this one.
  21240. Stop at the first and last subheadings of a superior heading.
  21241. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21242. non-nil it will also look at invisible ones."
  21243. (interactive "p")
  21244. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21245. (if (and arg (< arg 0))
  21246. (goto-char (point-min))
  21247. (outline-next-heading))
  21248. (org-at-heading-p)
  21249. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21250. (f (if (and arg (< arg 0))
  21251. 're-search-backward
  21252. 're-search-forward))
  21253. (count (if arg (abs arg) 1))
  21254. (result (point)))
  21255. (while (and (prog1 (> count 0)
  21256. (forward-char (if (and arg (< arg 0)) -1 1)))
  21257. (funcall f org-outline-regexp-bol nil 'move))
  21258. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21259. (cond ((< l level) (setq count 0))
  21260. ((and (= l level)
  21261. (or invisible-ok
  21262. (progn
  21263. (goto-char (line-beginning-position))
  21264. (not (outline-invisible-p)))))
  21265. (setq count (1- count))
  21266. (when (eq l level)
  21267. (setq result (point)))))))
  21268. (goto-char result))
  21269. (beginning-of-line 1)))
  21270. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21271. "Move backward to the ARG'th subheading at same level as this one.
  21272. Stop at the first and last subheadings of a superior heading."
  21273. (interactive "p")
  21274. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21275. (defun org-next-block (arg &optional backward block-regexp)
  21276. "Jump to the next block.
  21277. With a prefix argument ARG, jump forward ARG many source blocks.
  21278. When BACKWARD is non-nil, jump to the previous block.
  21279. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21280. (interactive "p")
  21281. (let ((re (or block-regexp org-block-regexp))
  21282. (re-search-fn (or (and backward 're-search-backward)
  21283. 're-search-forward)))
  21284. (if (looking-at re) (forward-char 1))
  21285. (condition-case nil
  21286. (funcall re-search-fn re nil nil arg)
  21287. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21288. (goto-char (match-beginning 0)) (org-show-context)))
  21289. (defun org-previous-block (arg &optional block-regexp)
  21290. "Jump to the previous block.
  21291. With a prefix argument ARG, jump backward ARG many source blocks.
  21292. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21293. (interactive "p")
  21294. (org-next-block arg t block-regexp))
  21295. (defun org-forward-element ()
  21296. "Move forward by one element.
  21297. Move to the next element at the same level, when possible."
  21298. (interactive)
  21299. (cond ((eobp) (user-error "Cannot move further down"))
  21300. ((org-with-limited-levels (org-at-heading-p))
  21301. (let ((origin (point)))
  21302. (goto-char (org-end-of-subtree nil t))
  21303. (unless (org-with-limited-levels (org-at-heading-p))
  21304. (goto-char origin)
  21305. (user-error "Cannot move further down"))))
  21306. (t
  21307. (let* ((elem (org-element-at-point))
  21308. (end (org-element-property :end elem))
  21309. (parent (org-element-property :parent elem)))
  21310. (if (and parent (= (org-element-property :contents-end parent) end))
  21311. (goto-char (org-element-property :end parent))
  21312. (goto-char end))))))
  21313. (defun org-backward-element ()
  21314. "Move backward by one element.
  21315. Move to the previous element at the same level, when possible."
  21316. (interactive)
  21317. (cond ((bobp) (user-error "Cannot move further up"))
  21318. ((org-with-limited-levels (org-at-heading-p))
  21319. ;; At a headline, move to the previous one, if any, or stay
  21320. ;; here.
  21321. (let ((origin (point)))
  21322. (org-with-limited-levels (org-backward-heading-same-level 1))
  21323. ;; When current headline has no sibling above, move to its
  21324. ;; parent.
  21325. (when (= (point) origin)
  21326. (or (org-with-limited-levels (org-up-heading-safe))
  21327. (progn (goto-char origin)
  21328. (user-error "Cannot move further up"))))))
  21329. (t
  21330. (let* ((trail (org-element-at-point 'keep-trail))
  21331. (elem (car trail))
  21332. (prev-elem (nth 1 trail))
  21333. (beg (org-element-property :begin elem)))
  21334. (cond
  21335. ;; Move to beginning of current element if point isn't
  21336. ;; there already.
  21337. ((/= (point) beg) (goto-char beg))
  21338. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21339. ((org-before-first-heading-p) (goto-char (point-min)))
  21340. (t (org-back-to-heading)))))))
  21341. (defun org-up-element ()
  21342. "Move to upper element."
  21343. (interactive)
  21344. (if (org-with-limited-levels (org-at-heading-p))
  21345. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  21346. (let* ((elem (org-element-at-point))
  21347. (parent (org-element-property :parent elem)))
  21348. (if parent (goto-char (org-element-property :begin parent))
  21349. (if (org-with-limited-levels (org-before-first-heading-p))
  21350. (user-error "No surrounding element")
  21351. (org-with-limited-levels (org-back-to-heading)))))))
  21352. (defvar org-element-greater-elements)
  21353. (defun org-down-element ()
  21354. "Move to inner element."
  21355. (interactive)
  21356. (let ((element (org-element-at-point)))
  21357. (cond
  21358. ((memq (org-element-type element) '(plain-list table))
  21359. (goto-char (org-element-property :contents-begin element))
  21360. (forward-char))
  21361. ((memq (org-element-type element) org-element-greater-elements)
  21362. ;; If contents are hidden, first disclose them.
  21363. (when (org-element-property :hiddenp element) (org-cycle))
  21364. (goto-char (or (org-element-property :contents-begin element)
  21365. (user-error "No content for this element"))))
  21366. (t (user-error "No inner element")))))
  21367. (defun org-drag-element-backward ()
  21368. "Move backward element at point."
  21369. (interactive)
  21370. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21371. (let* ((trail (org-element-at-point 'keep-trail))
  21372. (elem (car trail))
  21373. (prev-elem (nth 1 trail)))
  21374. ;; Error out if no previous element or previous element is
  21375. ;; a parent of the current one.
  21376. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21377. (user-error "Cannot drag element backward")
  21378. (let ((pos (point)))
  21379. (org-element-swap-A-B prev-elem elem)
  21380. (goto-char (+ (org-element-property :begin prev-elem)
  21381. (- pos (org-element-property :begin elem)))))))))
  21382. (defun org-drag-element-forward ()
  21383. "Move forward element at point."
  21384. (interactive)
  21385. (let* ((pos (point))
  21386. (elem (org-element-at-point)))
  21387. (when (= (point-max) (org-element-property :end elem))
  21388. (user-error "Cannot drag element forward"))
  21389. (goto-char (org-element-property :end elem))
  21390. (let ((next-elem (org-element-at-point)))
  21391. (when (or (org-element-nested-p elem next-elem)
  21392. (and (eq (org-element-type next-elem) 'headline)
  21393. (not (eq (org-element-type elem) 'headline))))
  21394. (goto-char pos)
  21395. (user-error "Cannot drag element forward"))
  21396. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21397. ;; body's length (without final blanks) and by the length of
  21398. ;; blanks between ELEM and NEXT-ELEM.
  21399. (let ((size-next (- (save-excursion
  21400. (goto-char (org-element-property :end next-elem))
  21401. (skip-chars-backward " \r\t\n")
  21402. (forward-line)
  21403. ;; Small correction if buffer doesn't end
  21404. ;; with a newline character.
  21405. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21406. (org-element-property :begin next-elem)))
  21407. (size-blank (- (org-element-property :end elem)
  21408. (save-excursion
  21409. (goto-char (org-element-property :end elem))
  21410. (skip-chars-backward " \r\t\n")
  21411. (forward-line)
  21412. (point)))))
  21413. (org-element-swap-A-B elem next-elem)
  21414. (goto-char (+ pos size-next size-blank))))))
  21415. (defun org-drag-line-forward (arg)
  21416. "Drag the line at point ARG lines forward."
  21417. (interactive "p")
  21418. (dotimes (n (abs arg))
  21419. (let ((c (current-column)))
  21420. (if (< 0 arg)
  21421. (progn
  21422. (beginning-of-line 2)
  21423. (transpose-lines 1)
  21424. (beginning-of-line 0))
  21425. (transpose-lines 1)
  21426. (beginning-of-line -1))
  21427. (org-move-to-column c))))
  21428. (defun org-drag-line-backward (arg)
  21429. "Drag the line at point ARG lines backward."
  21430. (interactive "p")
  21431. (org-drag-line-forward (- arg)))
  21432. (defun org-mark-element ()
  21433. "Put point at beginning of this element, mark at end.
  21434. Interactively, if this command is repeated or (in Transient Mark
  21435. mode) if the mark is active, it marks the next element after the
  21436. ones already marked."
  21437. (interactive)
  21438. (let (deactivate-mark)
  21439. (if (and (org-called-interactively-p 'any)
  21440. (or (and (eq last-command this-command) (mark t))
  21441. (and transient-mark-mode mark-active)))
  21442. (set-mark
  21443. (save-excursion
  21444. (goto-char (mark))
  21445. (goto-char (org-element-property :end (org-element-at-point)))))
  21446. (let ((element (org-element-at-point)))
  21447. (end-of-line)
  21448. (push-mark (org-element-property :end element) t t)
  21449. (goto-char (org-element-property :begin element))))))
  21450. (defun org-narrow-to-element ()
  21451. "Narrow buffer to current element."
  21452. (interactive)
  21453. (let ((elem (org-element-at-point)))
  21454. (cond
  21455. ((eq (car elem) 'headline)
  21456. (narrow-to-region
  21457. (org-element-property :begin elem)
  21458. (org-element-property :end elem)))
  21459. ((memq (car elem) org-element-greater-elements)
  21460. (narrow-to-region
  21461. (org-element-property :contents-begin elem)
  21462. (org-element-property :contents-end elem)))
  21463. (t
  21464. (narrow-to-region
  21465. (org-element-property :begin elem)
  21466. (org-element-property :end elem))))))
  21467. (defun org-transpose-element ()
  21468. "Transpose current and previous elements, keeping blank lines between.
  21469. Point is moved after both elements."
  21470. (interactive)
  21471. (org-skip-whitespace)
  21472. (let ((end (org-element-property :end (org-element-at-point))))
  21473. (org-drag-element-backward)
  21474. (goto-char end)))
  21475. (defun org-unindent-buffer ()
  21476. "Un-indent the visible part of the buffer.
  21477. Relative indentation (between items, inside blocks, etc.) isn't
  21478. modified."
  21479. (interactive)
  21480. (unless (eq major-mode 'org-mode)
  21481. (user-error "Cannot un-indent a buffer not in Org mode"))
  21482. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21483. unindent-tree ; For byte-compiler.
  21484. (unindent-tree
  21485. (function
  21486. (lambda (contents)
  21487. (mapc
  21488. (lambda (element)
  21489. (if (memq (org-element-type element) '(headline section))
  21490. (funcall unindent-tree (org-element-contents element))
  21491. (save-excursion
  21492. (save-restriction
  21493. (narrow-to-region
  21494. (org-element-property :begin element)
  21495. (org-element-property :end element))
  21496. (org-do-remove-indentation)))))
  21497. (reverse contents))))))
  21498. (funcall unindent-tree (org-element-contents parse-tree))))
  21499. (defun org-show-subtree ()
  21500. "Show everything after this heading at deeper levels."
  21501. (interactive)
  21502. (outline-flag-region
  21503. (point)
  21504. (save-excursion
  21505. (org-end-of-subtree t t))
  21506. nil))
  21507. (defun org-show-entry ()
  21508. "Show the body directly following this heading.
  21509. Show the heading too, if it is currently invisible."
  21510. (interactive)
  21511. (save-excursion
  21512. (condition-case nil
  21513. (progn
  21514. (org-back-to-heading t)
  21515. (outline-flag-region
  21516. (max (point-min) (1- (point)))
  21517. (save-excursion
  21518. (if (re-search-forward
  21519. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21520. (match-beginning 1)
  21521. (point-max)))
  21522. nil)
  21523. (org-cycle-hide-drawers 'children))
  21524. (error nil))))
  21525. (defun org-make-options-regexp (kwds &optional extra)
  21526. "Make a regular expression for keyword lines."
  21527. (concat
  21528. "^#\\+\\("
  21529. (mapconcat 'regexp-quote kwds "\\|")
  21530. (if extra (concat "\\|" extra))
  21531. "\\):[ \t]*\\(.*\\)"))
  21532. ;; Make isearch reveal the necessary context
  21533. (defun org-isearch-end ()
  21534. "Reveal context after isearch exits."
  21535. (when isearch-success ; only if search was successful
  21536. (if (featurep 'xemacs)
  21537. ;; Under XEmacs, the hook is run in the correct place,
  21538. ;; we directly show the context.
  21539. (org-show-context 'isearch)
  21540. ;; In Emacs the hook runs *before* restoring the overlays.
  21541. ;; So we have to use a one-time post-command-hook to do this.
  21542. ;; (Emacs 22 has a special variable, see function `org-mode')
  21543. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21544. isearch-mode-end-hook-quit)
  21545. ;; Only when the isearch was not quitted.
  21546. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21547. 'append 'local)))
  21548. (org-fix-ellipsis-at-bol)))
  21549. (defun org-isearch-post-command ()
  21550. "Remove self from hook, and show context."
  21551. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21552. (org-show-context 'isearch))
  21553. ;;;; Integration with and fixes for other packages
  21554. ;;; Imenu support
  21555. (defvar org-imenu-markers nil
  21556. "All markers currently used by Imenu.")
  21557. (make-variable-buffer-local 'org-imenu-markers)
  21558. (defun org-imenu-new-marker (&optional pos)
  21559. "Return a new marker for use by Imenu, and remember the marker."
  21560. (let ((m (make-marker)))
  21561. (move-marker m (or pos (point)))
  21562. (push m org-imenu-markers)
  21563. m))
  21564. (defun org-imenu-get-tree ()
  21565. "Produce the index for Imenu."
  21566. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21567. (setq org-imenu-markers nil)
  21568. (let* ((n org-imenu-depth)
  21569. (re (concat "^" (org-get-limited-outline-regexp)))
  21570. (subs (make-vector (1+ n) nil))
  21571. (last-level 0)
  21572. m level head0 head)
  21573. (save-excursion
  21574. (save-restriction
  21575. (widen)
  21576. (goto-char (point-max))
  21577. (while (re-search-backward re nil t)
  21578. (setq level (org-reduced-level (funcall outline-level)))
  21579. (when (and (<= level n)
  21580. (looking-at org-complex-heading-regexp)
  21581. (setq head0 (org-match-string-no-properties 4)))
  21582. (setq head (org-link-display-format head0)
  21583. m (org-imenu-new-marker))
  21584. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21585. (if (>= level last-level)
  21586. (push (cons head m) (aref subs level))
  21587. (push (cons head (aref subs (1+ level))) (aref subs level))
  21588. (loop for i from (1+ level) to n do (aset subs i nil)))
  21589. (setq last-level level)))))
  21590. (aref subs 1)))
  21591. (eval-after-load "imenu"
  21592. '(progn
  21593. (add-hook 'imenu-after-jump-hook
  21594. (lambda ()
  21595. (if (derived-mode-p 'org-mode)
  21596. (org-show-context 'org-goto))))))
  21597. (defun org-link-display-format (link)
  21598. "Replace a link with its the description.
  21599. If there is no description, use the link target."
  21600. (save-match-data
  21601. (if (string-match org-bracket-link-analytic-regexp link)
  21602. (replace-match (if (match-end 5)
  21603. (match-string 5 link)
  21604. (concat (match-string 1 link)
  21605. (match-string 3 link)))
  21606. nil t link)
  21607. link)))
  21608. (defun org-toggle-link-display ()
  21609. "Toggle the literal or descriptive display of links."
  21610. (interactive)
  21611. (if org-descriptive-links
  21612. (progn (org-remove-from-invisibility-spec '(org-link))
  21613. (org-restart-font-lock)
  21614. (setq org-descriptive-links nil))
  21615. (progn (add-to-invisibility-spec '(org-link))
  21616. (org-restart-font-lock)
  21617. (setq org-descriptive-links t))))
  21618. ;; Speedbar support
  21619. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21620. "Overlay marking the agenda restriction line in speedbar.")
  21621. (overlay-put org-speedbar-restriction-lock-overlay
  21622. 'face 'org-agenda-restriction-lock)
  21623. (overlay-put org-speedbar-restriction-lock-overlay
  21624. 'help-echo "Agendas are currently limited to this item.")
  21625. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21626. (defun org-speedbar-set-agenda-restriction ()
  21627. "Restrict future agenda commands to the location at point in speedbar.
  21628. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21629. (interactive)
  21630. (require 'org-agenda)
  21631. (let (p m tp np dir txt)
  21632. (cond
  21633. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21634. 'org-imenu t))
  21635. (setq m (get-text-property p 'org-imenu-marker))
  21636. (with-current-buffer (marker-buffer m)
  21637. (goto-char m)
  21638. (org-agenda-set-restriction-lock 'subtree)))
  21639. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21640. 'speedbar-function 'speedbar-find-file))
  21641. (setq tp (previous-single-property-change
  21642. (1+ p) 'speedbar-function)
  21643. np (next-single-property-change
  21644. tp 'speedbar-function)
  21645. dir (speedbar-line-directory)
  21646. txt (buffer-substring-no-properties (or tp (point-min))
  21647. (or np (point-max))))
  21648. (with-current-buffer (find-file-noselect
  21649. (let ((default-directory dir))
  21650. (expand-file-name txt)))
  21651. (unless (derived-mode-p 'org-mode)
  21652. (user-error "Cannot restrict to non-Org-mode file"))
  21653. (org-agenda-set-restriction-lock 'file)))
  21654. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  21655. (move-overlay org-speedbar-restriction-lock-overlay
  21656. (point-at-bol) (point-at-eol))
  21657. (setq current-prefix-arg nil)
  21658. (org-agenda-maybe-redo)))
  21659. (eval-after-load "speedbar"
  21660. '(progn
  21661. (speedbar-add-supported-extension ".org")
  21662. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21663. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21664. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21665. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21666. (add-hook 'speedbar-visiting-tag-hook
  21667. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21668. ;;; Fixes and Hacks for problems with other packages
  21669. ;; Make flyspell not check words in links, to not mess up our keymap
  21670. (defvar org-element-affiliated-keywords) ; From org-element.el
  21671. (defvar org-element-block-name-alist) ; From org-element.el
  21672. (defun org-mode-flyspell-verify ()
  21673. "Don't let flyspell put overlays at active buttons, or on
  21674. {todo,all-time,additional-option-like}-keywords."
  21675. (require 'org-element) ; For `org-element-affiliated-keywords'
  21676. (let ((pos (max (1- (point)) (point-min)))
  21677. (word (thing-at-point 'word)))
  21678. (and (not (get-text-property pos 'keymap))
  21679. (not (get-text-property pos 'org-no-flyspell))
  21680. (not (member word org-todo-keywords-1))
  21681. (not (member word org-all-time-keywords))
  21682. (not (member word org-options-keywords))
  21683. (not (member word (mapcar 'car org-startup-options)))
  21684. (not (member-ignore-case word org-element-affiliated-keywords))
  21685. (not (member-ignore-case word (org-get-export-keywords)))
  21686. (not (member-ignore-case
  21687. word (mapcar 'car org-element-block-name-alist)))
  21688. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21689. (defun org-remove-flyspell-overlays-in (beg end)
  21690. "Remove flyspell overlays in region."
  21691. (and (org-bound-and-true-p flyspell-mode)
  21692. (fboundp 'flyspell-delete-region-overlays)
  21693. (flyspell-delete-region-overlays beg end))
  21694. (add-text-properties beg end '(org-no-flyspell t)))
  21695. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21696. (eval-after-load "bookmark"
  21697. '(if (boundp 'bookmark-after-jump-hook)
  21698. ;; We can use the hook
  21699. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21700. ;; Hook not available, use advice
  21701. (defadvice bookmark-jump (after org-make-visible activate)
  21702. "Make the position visible."
  21703. (org-bookmark-jump-unhide))))
  21704. ;; Make sure saveplace shows the location if it was hidden
  21705. (eval-after-load "saveplace"
  21706. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21707. "Make the position visible."
  21708. (org-bookmark-jump-unhide)))
  21709. ;; Make sure ecb shows the location if it was hidden
  21710. (eval-after-load "ecb"
  21711. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21712. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21713. (if (derived-mode-p 'org-mode)
  21714. (org-show-context))))
  21715. (defun org-bookmark-jump-unhide ()
  21716. "Unhide the current position, to show the bookmark location."
  21717. (and (derived-mode-p 'org-mode)
  21718. (or (outline-invisible-p)
  21719. (save-excursion (goto-char (max (point-min) (1- (point))))
  21720. (outline-invisible-p)))
  21721. (org-show-context 'bookmark-jump)))
  21722. ;; Make session.el ignore our circular variable
  21723. (eval-after-load "session"
  21724. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21725. ;;;; Finish up
  21726. (provide 'org)
  21727. (run-hooks 'org-load-hook)
  21728. ;;; org.el ends here