org.el 933 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2014 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-bound 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-clocktable-shift "org-clock" (dir n))
  105. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  106. (declare-function org-clock-update-time-maybe "org-clock" ())
  107. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  108. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  109. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  110. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  111. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  112. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body))
  113. (declare-function orgtbl-mode "org-table" (&optional arg))
  114. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  115. (declare-function org-beamer-mode "ox-beamer" ())
  116. (declare-function org-table-blank-field "org-table" ())
  117. (declare-function org-table-edit-field "org-table" (arg))
  118. (declare-function org-table-insert-row "org-table" (&optional arg))
  119. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  120. (declare-function org-table-set-constants "org-table" ())
  121. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  122. (declare-function org-id-get-create "org-id" (&optional force))
  123. (declare-function org-add-archive-files "org-archive" (files))
  124. (declare-function org-id-find-id-file "org-id" (id))
  125. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  126. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  127. (declare-function org-agenda-redo "org-agenda" (&optional all))
  128. (declare-function org-table-align "org-table" ())
  129. (declare-function org-table-begin "org-table" (&optional table-type))
  130. (declare-function org-table-end "org-table" (&optional table-type))
  131. (declare-function org-table-paste-rectangle "org-table" ())
  132. (declare-function org-table-maybe-eval-formula "org-table" ())
  133. (declare-function org-table-maybe-recalculate-line "org-table" ())
  134. (declare-function org-element--parse-objects "org-element"
  135. (beg end acc restriction))
  136. (declare-function org-element-at-point "org-element" ())
  137. (declare-function org-element-cache-reset "org-element" (&optional all))
  138. (declare-function org-element-contents "org-element" (element))
  139. (declare-function org-element-context "org-element" (&optional element))
  140. (declare-function org-element-interpret-data "org-element"
  141. (data &optional parent))
  142. (declare-function org-element-map "org-element"
  143. (data types fun &optional info first-match no-recursion))
  144. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  145. (declare-function org-element-parse-buffer "org-element"
  146. (&optional granularity visible-only))
  147. (declare-function org-element-property "org-element" (property element))
  148. (declare-function org-element-put-property "org-element"
  149. (element property value))
  150. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  151. (declare-function org-element--parse-objects "org-element"
  152. (beg end acc restriction))
  153. (declare-function org-element-parse-buffer "org-element"
  154. (&optional granularity visible-only))
  155. (declare-function org-element-restriction "org-element" (element))
  156. (declare-function org-element-type "org-element" (element))
  157. (defsubst org-uniquify (list)
  158. "Non-destructively remove duplicate elements from LIST."
  159. (let ((res (copy-sequence list))) (delete-dups res)))
  160. ;; load languages based on value of `org-babel-load-languages'
  161. (defvar org-babel-load-languages)
  162. ;;;###autoload
  163. (defun org-babel-do-load-languages (sym value)
  164. "Load the languages defined in `org-babel-load-languages'."
  165. (set-default sym value)
  166. (mapc (lambda (pair)
  167. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  168. (if active
  169. (progn
  170. (require (intern (concat "ob-" lang))))
  171. (progn
  172. (funcall 'fmakunbound
  173. (intern (concat "org-babel-execute:" lang)))
  174. (funcall 'fmakunbound
  175. (intern (concat "org-babel-expand-body:" lang)))))))
  176. org-babel-load-languages))
  177. ;;;###autoload
  178. (defun org-babel-load-file (file &optional compile)
  179. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  180. This function exports the source code using `org-babel-tangle'
  181. and then loads the resulting file using `load-file'. With prefix
  182. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  183. file to byte-code before it is loaded."
  184. (interactive "fFile to load: \nP")
  185. (let* ((age (lambda (file)
  186. (float-time
  187. (time-subtract (current-time)
  188. (nth 5 (or (file-attributes (file-truename file))
  189. (file-attributes file)))))))
  190. (base-name (file-name-sans-extension file))
  191. (exported-file (concat base-name ".el")))
  192. ;; tangle if the org-mode file is newer than the elisp file
  193. (unless (and (file-exists-p exported-file)
  194. (> (funcall age file) (funcall age exported-file)))
  195. (setq exported-file
  196. (car (org-babel-tangle-file file exported-file "emacs-lisp"))))
  197. (message "%s %s"
  198. (if compile
  199. (progn (byte-compile-file exported-file 'load)
  200. "Compiled and loaded")
  201. (progn (load-file exported-file) "Loaded"))
  202. exported-file)))
  203. (defcustom org-babel-load-languages '((emacs-lisp . t))
  204. "Languages which can be evaluated in Org-mode buffers.
  205. This list can be used to load support for any of the languages
  206. below, note that each language will depend on a different set of
  207. system executables and/or Emacs modes. When a language is
  208. \"loaded\", then code blocks in that language can be evaluated
  209. with `org-babel-execute-src-block' bound by default to C-c
  210. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  211. be set to remove code block evaluation from the C-c C-c
  212. keybinding. By default only Emacs Lisp (which has no
  213. requirements) is loaded."
  214. :group 'org-babel
  215. :set 'org-babel-do-load-languages
  216. :version "24.1"
  217. :type '(alist :tag "Babel Languages"
  218. :key-type
  219. (choice
  220. (const :tag "Awk" awk)
  221. (const :tag "C" C)
  222. (const :tag "R" R)
  223. (const :tag "Asymptote" asymptote)
  224. (const :tag "Calc" calc)
  225. (const :tag "Clojure" clojure)
  226. (const :tag "CSS" css)
  227. (const :tag "Ditaa" ditaa)
  228. (const :tag "Dot" dot)
  229. (const :tag "Emacs Lisp" emacs-lisp)
  230. (const :tag "Fortran" fortran)
  231. (const :tag "Gnuplot" gnuplot)
  232. (const :tag "Haskell" haskell)
  233. (const :tag "IO" io)
  234. (const :tag "J" J)
  235. (const :tag "Java" java)
  236. (const :tag "Javascript" js)
  237. (const :tag "LaTeX" latex)
  238. (const :tag "Ledger" ledger)
  239. (const :tag "Lilypond" lilypond)
  240. (const :tag "Lisp" lisp)
  241. (const :tag "Makefile" makefile)
  242. (const :tag "Maxima" maxima)
  243. (const :tag "Matlab" matlab)
  244. (const :tag "Mscgen" mscgen)
  245. (const :tag "Ocaml" ocaml)
  246. (const :tag "Octave" octave)
  247. (const :tag "Org" org)
  248. (const :tag "Perl" perl)
  249. (const :tag "Pico Lisp" picolisp)
  250. (const :tag "PlantUML" plantuml)
  251. (const :tag "Python" python)
  252. (const :tag "Ruby" ruby)
  253. (const :tag "Sass" sass)
  254. (const :tag "Scala" scala)
  255. (const :tag "Scheme" scheme)
  256. (const :tag "Screen" screen)
  257. (const :tag "Shell Script" shell)
  258. (const :tag "Shen" shen)
  259. (const :tag "Sql" sql)
  260. (const :tag "Sqlite" sqlite)
  261. (const :tag "ebnf2ps" ebnf2ps))
  262. :value-type (boolean :tag "Activate" :value t)))
  263. ;;;; Customization variables
  264. (defcustom org-clone-delete-id nil
  265. "Remove ID property of clones of a subtree.
  266. When non-nil, clones of a subtree don't inherit the ID property.
  267. Otherwise they inherit the ID property with a new unique
  268. identifier."
  269. :type 'boolean
  270. :version "24.1"
  271. :group 'org-id)
  272. ;;; Version
  273. (org-check-version)
  274. ;;;###autoload
  275. (defun org-version (&optional here full message)
  276. "Show the org-mode version.
  277. Interactively, or when MESSAGE is non-nil, show it in echo area.
  278. With prefix argument, or when HERE is non-nil, insert it at point.
  279. In non-interactive uses, a reduced version string is output unless
  280. FULL is given."
  281. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  282. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  283. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  284. (load-suffixes (list ".el"))
  285. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  286. (org-trash (or
  287. (and (fboundp 'org-release) (fboundp 'org-git-version))
  288. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  289. (load-suffixes save-load-suffixes)
  290. (org-version (org-release))
  291. (git-version (org-git-version))
  292. (version (format "Org-mode version %s (%s @ %s)"
  293. org-version
  294. git-version
  295. (if org-install-dir
  296. (if (string= org-dir org-install-dir)
  297. org-install-dir
  298. (concat "mixed installation! " org-install-dir " and " org-dir))
  299. "org-loaddefs.el can not be found!")))
  300. (version1 (if full version org-version)))
  301. (when here (insert version1))
  302. (when message (message "%s" version1))
  303. version1))
  304. (defconst org-version (org-version))
  305. ;;; Syntax Constants
  306. ;;;; Block
  307. (defconst org-block-regexp
  308. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  309. "Regular expression for hiding blocks.")
  310. (defconst org-dblock-start-re
  311. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  312. "Matches the start line of a dynamic block, with parameters.")
  313. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  314. "Matches the end of a dynamic block.")
  315. ;;;; Clock and Planning
  316. (defconst org-clock-string "CLOCK:"
  317. "String used as prefix for timestamps clocking work hours on an item.")
  318. (defconst org-closed-string "CLOSED:"
  319. "String used as the prefix for timestamps logging closing a TODO entry.")
  320. (defconst org-deadline-string "DEADLINE:"
  321. "String to mark deadline entries.
  322. A deadline is this string, followed by a time stamp. Should be a word,
  323. terminated by a colon. You can insert a schedule keyword and
  324. a timestamp with \\[org-deadline].")
  325. (defconst org-scheduled-string "SCHEDULED:"
  326. "String to mark scheduled TODO entries.
  327. A schedule is this string, followed by a time stamp. Should be a word,
  328. terminated by a colon. You can insert a schedule keyword and
  329. a timestamp with \\[org-schedule].")
  330. (defconst org-planning-or-clock-line-re
  331. (concat "^[ \t]*"
  332. (regexp-opt
  333. (list org-clock-string org-closed-string org-deadline-string
  334. org-scheduled-string)
  335. t))
  336. "Matches a line with planning or clock info.
  337. Matched keyword is in group 1.")
  338. ;;;; Drawer
  339. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  340. "Matches first or last line of a hidden block.
  341. Group 1 contains drawer's name or \"END\".")
  342. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  343. "Regular expression matching the first line of a property drawer.")
  344. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  345. "Regular expression matching the last line of a property drawer.")
  346. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  347. "Regular expression matching the first line of a clock drawer.")
  348. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  349. "Regular expression matching the last line of a clock drawer.")
  350. (defconst org-property-drawer-re
  351. (concat "\\(" org-property-start-re "\\)[^\000]*?\\("
  352. org-property-end-re "\\)\n?")
  353. "Matches an entire property drawer.")
  354. (defconst org-clock-drawer-re
  355. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  356. org-clock-drawer-end-re "\\)\n?")
  357. "Matches an entire clock drawer.")
  358. ;;;; Headline
  359. (defconst org-heading-keyword-regexp-format
  360. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  361. "Printf format for a regexp matching a headline with some keyword.
  362. This regexp will match the headline of any node which has the
  363. exact keyword that is put into the format. The keyword isn't in
  364. any group by default, but the stars and the body are.")
  365. (defconst org-heading-keyword-maybe-regexp-format
  366. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  367. "Printf format for a regexp matching a headline, possibly with some keyword.
  368. This regexp can match any headline with the specified keyword, or
  369. without a keyword. The keyword isn't in any group by default,
  370. but the stars and the body are.")
  371. (defconst org-archive-tag "ARCHIVE"
  372. "The tag that marks a subtree as archived.
  373. An archived subtree does not open during visibility cycling, and does
  374. not contribute to the agenda listings.")
  375. (defconst org-comment-string "COMMENT"
  376. "Entries starting with this keyword will never be exported.
  377. An entry can be toggled between COMMENT and normal with
  378. \\[org-toggle-comment].")
  379. ;;;; LaTeX Environments and Fragments
  380. (defconst org-latex-regexps
  381. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  382. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  383. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  384. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  385. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  386. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  387. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  388. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  389. "Regular expressions for matching embedded LaTeX.")
  390. ;;;; Node Property
  391. (defconst org-effort-property "Effort"
  392. "The property that is being used to keep track of effort estimates.
  393. Effort estimates given in this property need to have the format H:MM.")
  394. ;;;; Table
  395. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  396. "Detect an org-type or table-type table.")
  397. (defconst org-table-line-regexp "^[ \t]*|"
  398. "Detect an org-type table line.")
  399. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  400. "Detect an org-type table line.")
  401. (defconst org-table-hline-regexp "^[ \t]*|-"
  402. "Detect an org-type table hline.")
  403. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  404. "Detect a table-type table hline.")
  405. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  406. "Detect the first line outside a table when searching from within it.
  407. This works for both table types.")
  408. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  409. "Detect a #+TBLFM line.")
  410. ;;;; Timestamp
  411. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  412. "Regular expression for fast time stamp matching.")
  413. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  414. "Regular expression for fast time stamp matching.")
  415. (defconst org-ts-regexp0
  416. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  417. "Regular expression matching time strings for analysis.
  418. This one does not require the space after the date, so it can be used
  419. on a string that terminates immediately after the date.")
  420. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  421. "Regular expression matching time strings for analysis.")
  422. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  423. "Regular expression matching time stamps, with groups.")
  424. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  425. "Regular expression matching time stamps (also [..]), with groups.")
  426. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  427. "Regular expression matching a time stamp range.")
  428. (defconst org-tr-regexp-both
  429. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  430. "Regular expression matching a time stamp range.")
  431. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  432. org-ts-regexp "\\)?")
  433. "Regular expression matching a time stamp or time stamp range.")
  434. (defconst org-tsr-regexp-both
  435. (concat org-ts-regexp-both "\\(--?-?"
  436. org-ts-regexp-both "\\)?")
  437. "Regular expression matching a time stamp or time stamp range.
  438. The time stamps may be either active or inactive.")
  439. (defconst org-repeat-re
  440. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  441. "Regular expression for specifying repeated events.
  442. After a match, group 1 contains the repeat expression.")
  443. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  444. "Formats for `format-time-string' which are used for time stamps.")
  445. ;;; The custom variables
  446. (defgroup org nil
  447. "Outline-based notes management and organizer."
  448. :tag "Org"
  449. :group 'outlines
  450. :group 'calendar)
  451. (defcustom org-mode-hook nil
  452. "Mode hook for Org-mode, run after the mode was turned on."
  453. :group 'org
  454. :type 'hook)
  455. (defcustom org-load-hook nil
  456. "Hook that is run after org.el has been loaded."
  457. :group 'org
  458. :type 'hook)
  459. (defcustom org-log-buffer-setup-hook nil
  460. "Hook that is run after an Org log buffer is created."
  461. :group 'org
  462. :version "24.1"
  463. :type 'hook)
  464. (defvar org-modules) ; defined below
  465. (defvar org-modules-loaded nil
  466. "Have the modules been loaded already?")
  467. (defun org-load-modules-maybe (&optional force)
  468. "Load all extensions listed in `org-modules'."
  469. (when (or force (not org-modules-loaded))
  470. (mapc (lambda (ext)
  471. (condition-case nil (require ext)
  472. (error (message "Problems while trying to load feature `%s'" ext))))
  473. org-modules)
  474. (setq org-modules-loaded t)))
  475. (defun org-set-modules (var value)
  476. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  477. (set var value)
  478. (when (featurep 'org)
  479. (org-load-modules-maybe 'force)
  480. (org-element-cache-reset 'all)))
  481. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  482. "Modules that should always be loaded together with org.el.
  483. If a description starts with <C>, the file is not part of Emacs
  484. and loading it will require that you have downloaded and properly
  485. installed the Org mode distribution.
  486. You can also use this system to load external packages (i.e. neither Org
  487. core modules, nor modules from the CONTRIB directory). Just add symbols
  488. to the end of the list. If the package is called org-xyz.el, then you need
  489. to add the symbol `xyz', and the package must have a call to:
  490. \(provide 'org-xyz)
  491. For export specific modules, see also `org-export-backends'."
  492. :group 'org
  493. :set 'org-set-modules
  494. :version "24.4"
  495. :package-version '(Org . "8.0")
  496. :type
  497. '(set :greedy t
  498. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  499. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  500. (const :tag " crypt: Encryption of subtrees" org-crypt)
  501. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  502. (const :tag " docview: Links to doc-view buffers" org-docview)
  503. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  504. (const :tag " habit: Track your consistency with habits" org-habit)
  505. (const :tag " id: Global IDs for identifying entries" org-id)
  506. (const :tag " info: Links to Info nodes" org-info)
  507. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  508. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  509. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  510. (const :tag " mouse: Additional mouse support" org-mouse)
  511. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  512. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  513. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  514. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  515. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  516. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  517. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  518. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  519. (const :tag "C collector: Collect properties into tables" org-collector)
  520. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  521. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  522. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  523. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  524. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  525. (const :tag "C eval: Include command output as text" org-eval)
  526. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  527. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  528. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  529. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  530. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  531. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  532. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  533. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  534. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  535. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  536. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  537. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  538. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  539. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  540. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  541. (const :tag "C registry: A registry for Org-mode links" org-registry)
  542. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  543. (const :tag "C secretary: Team management with org-mode" org-secretary)
  544. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  545. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  546. (const :tag "C track: Keep up with Org-mode development" org-track)
  547. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  548. (const :tag "C vm: Links to VM folders/messages" org-vm)
  549. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  550. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  551. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  552. (defvar org-export--registered-backends) ; From ox.el.
  553. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  554. (declare-function org-export-backend-name "ox" (backend))
  555. (defcustom org-export-backends '(ascii html icalendar latex)
  556. "List of export back-ends that should be always available.
  557. If a description starts with <C>, the file is not part of Emacs
  558. and loading it will require that you have downloaded and properly
  559. installed the Org mode distribution.
  560. Unlike to `org-modules', libraries in this list will not be
  561. loaded along with Org, but only once the export framework is
  562. needed.
  563. This variable needs to be set before org.el is loaded. If you
  564. need to make a change while Emacs is running, use the customize
  565. interface or run the following code, where VAL stands for the new
  566. value of the variable, after updating it:
  567. \(progn
  568. \(setq org-export--registered-backends
  569. \(org-remove-if-not
  570. \(lambda (backend)
  571. \(let ((name (org-export-backend-name backend)))
  572. \(or (memq name val)
  573. \(catch 'parentp
  574. \(dolist (b val)
  575. \(and (org-export-derived-backend-p b name)
  576. \(throw 'parentp t)))))))
  577. org-export--registered-backends))
  578. \(let ((new-list (mapcar 'org-export-backend-name
  579. org-export--registered-backends)))
  580. \(dolist (backend val)
  581. \(cond
  582. \((not (load (format \"ox-%s\" backend) t t))
  583. \(message \"Problems while trying to load export back-end `%s'\"
  584. backend))
  585. \((not (memq backend new-list)) (push backend new-list))))
  586. \(set-default 'org-export-backends new-list)))
  587. Adding a back-end to this list will also pull the back-end it
  588. depends on, if any."
  589. :group 'org
  590. :group 'org-export
  591. :version "24.4"
  592. :package-version '(Org . "8.0")
  593. :initialize 'custom-initialize-set
  594. :set (lambda (var val)
  595. (if (not (featurep 'ox)) (set-default var val)
  596. ;; Any back-end not required anymore (not present in VAL and not
  597. ;; a parent of any back-end in the new value) is removed from the
  598. ;; list of registered back-ends.
  599. (setq org-export--registered-backends
  600. (org-remove-if-not
  601. (lambda (backend)
  602. (let ((name (org-export-backend-name backend)))
  603. (or (memq name val)
  604. (catch 'parentp
  605. (dolist (b val)
  606. (and (org-export-derived-backend-p b name)
  607. (throw 'parentp t)))))))
  608. org-export--registered-backends))
  609. ;; Now build NEW-LIST of both new back-ends and required
  610. ;; parents.
  611. (let ((new-list (mapcar 'org-export-backend-name
  612. org-export--registered-backends)))
  613. (dolist (backend val)
  614. (cond
  615. ((not (load (format "ox-%s" backend) t t))
  616. (message "Problems while trying to load export back-end `%s'"
  617. backend))
  618. ((not (memq backend new-list)) (push backend new-list))))
  619. ;; Set VAR to that list with fixed dependencies.
  620. (set-default var new-list))))
  621. :type '(set :greedy t
  622. (const :tag " ascii Export buffer to ASCII format" ascii)
  623. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  624. (const :tag " html Export buffer to HTML format" html)
  625. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  626. (const :tag " latex Export buffer to LaTeX format" latex)
  627. (const :tag " man Export buffer to MAN format" man)
  628. (const :tag " md Export buffer to Markdown format" md)
  629. (const :tag " odt Export buffer to ODT format" odt)
  630. (const :tag " org Export buffer to Org format" org)
  631. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  632. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  633. (const :tag "C deck Export buffer to deck.js presentations" deck)
  634. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  635. (const :tag "C groff Export buffer to Groff format" groff)
  636. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  637. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  638. (const :tag "C s5 Export buffer to s5 presentations" s5)
  639. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  640. (eval-after-load 'ox
  641. '(mapc
  642. (lambda (backend)
  643. (condition-case nil (require (intern (format "ox-%s" backend)))
  644. (error (message "Problems while trying to load export back-end `%s'"
  645. backend))))
  646. org-export-backends))
  647. (defcustom org-support-shift-select nil
  648. "Non-nil means make shift-cursor commands select text when possible.
  649. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  650. start selecting a region, or enlarge regions started in this way.
  651. In Org-mode, in special contexts, these same keys are used for
  652. other purposes, important enough to compete with shift selection.
  653. Org tries to balance these needs by supporting `shift-select-mode'
  654. outside these special contexts, under control of this variable.
  655. The default of this variable is nil, to avoid confusing behavior. Shifted
  656. cursor keys will then execute Org commands in the following contexts:
  657. - on a headline, changing TODO state (left/right) and priority (up/down)
  658. - on a time stamp, changing the time
  659. - in a plain list item, changing the bullet type
  660. - in a property definition line, switching between allowed values
  661. - in the BEGIN line of a clock table (changing the time block).
  662. Outside these contexts, the commands will throw an error.
  663. When this variable is t and the cursor is not in a special
  664. context, Org-mode will support shift-selection for making and
  665. enlarging regions. To make this more effective, the bullet
  666. cycling will no longer happen anywhere in an item line, but only
  667. if the cursor is exactly on the bullet.
  668. If you set this variable to the symbol `always', then the keys
  669. will not be special in headlines, property lines, and item lines,
  670. to make shift selection work there as well. If this is what you
  671. want, you can use the following alternative commands: `C-c C-t'
  672. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  673. can be used to switch TODO sets, `C-c -' to cycle item bullet
  674. types, and properties can be edited by hand or in column view.
  675. However, when the cursor is on a timestamp, shift-cursor commands
  676. will still edit the time stamp - this is just too good to give up.
  677. XEmacs user should have this variable set to nil, because
  678. `shift-select-mode' is in Emacs 23 or later only."
  679. :group 'org
  680. :type '(choice
  681. (const :tag "Never" nil)
  682. (const :tag "When outside special context" t)
  683. (const :tag "Everywhere except timestamps" always)))
  684. (defcustom org-loop-over-headlines-in-active-region nil
  685. "Shall some commands act upon headlines in the active region?
  686. When set to `t', some commands will be performed in all headlines
  687. within the active region.
  688. When set to `start-level', some commands will be performed in all
  689. headlines within the active region, provided that these headlines
  690. are of the same level than the first one.
  691. When set to a string, those commands will be performed on the
  692. matching headlines within the active region. Such string must be
  693. a tags/property/todo match as it is used in the agenda tags view.
  694. The list of commands is: `org-schedule', `org-deadline',
  695. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  696. `org-archive-to-archive-sibling'. The archiving commands skip
  697. already archived entries."
  698. :type '(choice (const :tag "Don't loop" nil)
  699. (const :tag "All headlines in active region" t)
  700. (const :tag "In active region, headlines at the same level than the first one" start-level)
  701. (string :tag "Tags/Property/Todo matcher"))
  702. :version "24.1"
  703. :group 'org-todo
  704. :group 'org-archive)
  705. (defgroup org-startup nil
  706. "Options concerning startup of Org-mode."
  707. :tag "Org Startup"
  708. :group 'org)
  709. (defcustom org-startup-folded t
  710. "Non-nil means entering Org-mode will switch to OVERVIEW.
  711. This can also be configured on a per-file basis by adding one of
  712. the following lines anywhere in the buffer:
  713. #+STARTUP: fold (or `overview', this is equivalent)
  714. #+STARTUP: nofold (or `showall', this is equivalent)
  715. #+STARTUP: content
  716. #+STARTUP: showeverything
  717. By default, this option is ignored when Org opens agenda files
  718. for the first time. If you want the agenda to honor the startup
  719. option, set `org-agenda-inhibit-startup' to nil."
  720. :group 'org-startup
  721. :type '(choice
  722. (const :tag "nofold: show all" nil)
  723. (const :tag "fold: overview" t)
  724. (const :tag "content: all headlines" content)
  725. (const :tag "show everything, even drawers" showeverything)))
  726. (defcustom org-startup-truncated t
  727. "Non-nil means entering Org-mode will set `truncate-lines'.
  728. This is useful since some lines containing links can be very long and
  729. uninteresting. Also tables look terrible when wrapped."
  730. :group 'org-startup
  731. :type 'boolean)
  732. (defcustom org-startup-indented nil
  733. "Non-nil means turn on `org-indent-mode' on startup.
  734. This can also be configured on a per-file basis by adding one of
  735. the following lines anywhere in the buffer:
  736. #+STARTUP: indent
  737. #+STARTUP: noindent"
  738. :group 'org-structure
  739. :type '(choice
  740. (const :tag "Not" nil)
  741. (const :tag "Globally (slow on startup in large files)" t)))
  742. (defcustom org-use-sub-superscripts t
  743. "Non-nil means interpret \"_\" and \"^\" for display.
  744. If you want to control how Org exports those characters, see
  745. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  746. used to be an alias for `org-export-with-sub-superscripts' in
  747. Org <8.0, it is not anymore.
  748. When this option is turned on, you can use TeX-like syntax for
  749. sub- and superscripts within the buffer. Several characters after
  750. \"_\" or \"^\" will be considered as a single item - so grouping
  751. with {} is normally not needed. For example, the following things
  752. will be parsed as single sub- or superscripts:
  753. 10^24 or 10^tau several digits will be considered 1 item.
  754. 10^-12 or 10^-tau a leading sign with digits or a word
  755. x^2-y^3 will be read as x^2 - y^3, because items are
  756. terminated by almost any nonword/nondigit char.
  757. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  758. Still, ambiguity is possible. So when in doubt, use {} to enclose
  759. the sub/superscript. If you set this variable to the symbol `{}',
  760. the braces are *required* in order to trigger interpretations as
  761. sub/superscript. This can be helpful in documents that need \"_\"
  762. frequently in plain text."
  763. :group 'org-startup
  764. :version "24.4"
  765. :package-version '(Org . "8.0")
  766. :type '(choice
  767. (const :tag "Always interpret" t)
  768. (const :tag "Only with braces" {})
  769. (const :tag "Never interpret" nil)))
  770. (defcustom org-startup-with-beamer-mode nil
  771. "Non-nil means turn on `org-beamer-mode' on startup.
  772. This can also be configured on a per-file basis by adding one of
  773. the following lines anywhere in the buffer:
  774. #+STARTUP: beamer"
  775. :group 'org-startup
  776. :version "24.1"
  777. :type 'boolean)
  778. (defcustom org-startup-align-all-tables nil
  779. "Non-nil means align all tables when visiting a file.
  780. This is useful when the column width in tables is forced with <N> cookies
  781. in table fields. Such tables will look correct only after the first re-align.
  782. This can also be configured on a per-file basis by adding one of
  783. the following lines anywhere in the buffer:
  784. #+STARTUP: align
  785. #+STARTUP: noalign"
  786. :group 'org-startup
  787. :type 'boolean)
  788. (defcustom org-startup-with-inline-images nil
  789. "Non-nil means show inline images when loading a new Org file.
  790. This can also be configured on a per-file basis by adding one of
  791. the following lines anywhere in the buffer:
  792. #+STARTUP: inlineimages
  793. #+STARTUP: noinlineimages"
  794. :group 'org-startup
  795. :version "24.1"
  796. :type 'boolean)
  797. (defcustom org-startup-with-latex-preview nil
  798. "Non-nil means preview LaTeX fragments when loading a new Org file.
  799. This can also be configured on a per-file basis by adding one of
  800. the following lines anywhere in the buffer:
  801. #+STARTUP: latexpreview
  802. #+STARTUP: nolatexpreview"
  803. :group 'org-startup
  804. :version "24.4"
  805. :package-version '(Org . "8.0")
  806. :type 'boolean)
  807. (defcustom org-insert-mode-line-in-empty-file nil
  808. "Non-nil means insert the first line setting Org-mode in empty files.
  809. When the function `org-mode' is called interactively in an empty file, this
  810. normally means that the file name does not automatically trigger Org-mode.
  811. To ensure that the file will always be in Org-mode in the future, a
  812. line enforcing Org-mode will be inserted into the buffer, if this option
  813. has been set."
  814. :group 'org-startup
  815. :type 'boolean)
  816. (defcustom org-replace-disputed-keys nil
  817. "Non-nil means use alternative key bindings for some keys.
  818. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  819. These keys are also used by other packages like shift-selection-mode'
  820. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  821. If you want to use Org-mode together with one of these other modes,
  822. or more generally if you would like to move some Org-mode commands to
  823. other keys, set this variable and configure the keys with the variable
  824. `org-disputed-keys'.
  825. This option is only relevant at load-time of Org-mode, and must be set
  826. *before* org.el is loaded. Changing it requires a restart of Emacs to
  827. become effective."
  828. :group 'org-startup
  829. :type 'boolean)
  830. (defcustom org-use-extra-keys nil
  831. "Non-nil means use extra key sequence definitions for certain commands.
  832. This happens automatically if you run XEmacs or if `window-system'
  833. is nil. This variable lets you do the same manually. You must
  834. set it before loading org.
  835. Example: on Carbon Emacs 22 running graphically, with an external
  836. keyboard on a Powerbook, the default way of setting M-left might
  837. not work for either Alt or ESC. Setting this variable will make
  838. it work for ESC."
  839. :group 'org-startup
  840. :type 'boolean)
  841. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  842. (defcustom org-disputed-keys
  843. '(([(shift up)] . [(meta p)])
  844. ([(shift down)] . [(meta n)])
  845. ([(shift left)] . [(meta -)])
  846. ([(shift right)] . [(meta +)])
  847. ([(control shift right)] . [(meta shift +)])
  848. ([(control shift left)] . [(meta shift -)]))
  849. "Keys for which Org-mode and other modes compete.
  850. This is an alist, cars are the default keys, second element specifies
  851. the alternative to use when `org-replace-disputed-keys' is t.
  852. Keys can be specified in any syntax supported by `define-key'.
  853. The value of this option takes effect only at Org-mode's startup,
  854. therefore you'll have to restart Emacs to apply it after changing."
  855. :group 'org-startup
  856. :type 'alist)
  857. (defun org-key (key)
  858. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  859. Or return the original if not disputed.
  860. Also apply the translations defined in `org-xemacs-key-equivalents'."
  861. (when org-replace-disputed-keys
  862. (let* ((nkey (key-description key))
  863. (x (org-find-if (lambda (x)
  864. (equal (key-description (car x)) nkey))
  865. org-disputed-keys)))
  866. (setq key (if x (cdr x) key))))
  867. (when (featurep 'xemacs)
  868. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  869. key)
  870. (defun org-find-if (predicate seq)
  871. (catch 'exit
  872. (while seq
  873. (if (funcall predicate (car seq))
  874. (throw 'exit (car seq))
  875. (pop seq)))))
  876. (defun org-defkey (keymap key def)
  877. "Define a key, possibly translated, as returned by `org-key'."
  878. (define-key keymap (org-key key) def))
  879. (defcustom org-ellipsis nil
  880. "The ellipsis to use in the Org-mode outline.
  881. When nil, just use the standard three dots.
  882. When a string, use that string instead.
  883. When a face, use the standard 3 dots, but with the specified face.
  884. The change affects only Org-mode (which will then use its own display table).
  885. Changing this requires executing `M-x org-mode RET' in a buffer to become
  886. effective."
  887. :group 'org-startup
  888. :type '(choice (const :tag "Default" nil)
  889. (face :tag "Face" :value org-warning)
  890. (string :tag "String" :value "...#")))
  891. (defvar org-display-table nil
  892. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  893. (defgroup org-keywords nil
  894. "Keywords in Org-mode."
  895. :tag "Org Keywords"
  896. :group 'org)
  897. (defcustom org-closed-keep-when-no-todo nil
  898. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  899. :group 'org-todo
  900. :group 'org-keywords
  901. :version "24.4"
  902. :package-version '(Org . "8.0")
  903. :type 'boolean)
  904. (defgroup org-structure nil
  905. "Options concerning the general structure of Org-mode files."
  906. :tag "Org Structure"
  907. :group 'org)
  908. (defgroup org-reveal-location nil
  909. "Options about how to make context of a location visible."
  910. :tag "Org Reveal Location"
  911. :group 'org-structure)
  912. (defconst org-context-choice
  913. '(choice
  914. (const :tag "Always" t)
  915. (const :tag "Never" nil)
  916. (repeat :greedy t :tag "Individual contexts"
  917. (cons
  918. (choice :tag "Context"
  919. (const agenda)
  920. (const org-goto)
  921. (const occur-tree)
  922. (const tags-tree)
  923. (const link-search)
  924. (const mark-goto)
  925. (const bookmark-jump)
  926. (const isearch)
  927. (const default))
  928. (boolean))))
  929. "Contexts for the reveal options.")
  930. (defcustom org-show-hierarchy-above '((default . t))
  931. "Non-nil means show full hierarchy when revealing a location.
  932. Org-mode often shows locations in an org-mode file which might have
  933. been invisible before. When this is set, the hierarchy of headings
  934. above the exposed location is shown.
  935. Turning this off for example for sparse trees makes them very compact.
  936. Instead of t, this can also be an alist specifying this option for different
  937. contexts. Valid contexts are
  938. agenda when exposing an entry from the agenda
  939. org-goto when using the command `org-goto' on key C-c C-j
  940. occur-tree when using the command `org-occur' on key C-c /
  941. tags-tree when constructing a sparse tree based on tags matches
  942. link-search when exposing search matches associated with a link
  943. mark-goto when exposing the jump goal of a mark
  944. bookmark-jump when exposing a bookmark location
  945. isearch when exiting from an incremental search
  946. default default for all contexts not set explicitly"
  947. :group 'org-reveal-location
  948. :type org-context-choice)
  949. (defcustom org-show-following-heading '((default . nil))
  950. "Non-nil means show following heading when revealing a location.
  951. Org-mode often shows locations in an org-mode file which might have
  952. been invisible before. When this is set, the heading following the
  953. match is shown.
  954. Turning this off for example for sparse trees makes them very compact,
  955. but makes it harder to edit the location of the match. In such a case,
  956. use the command \\[org-reveal] to show more context.
  957. Instead of t, this can also be an alist specifying this option for different
  958. contexts. See `org-show-hierarchy-above' for valid contexts."
  959. :group 'org-reveal-location
  960. :type org-context-choice)
  961. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  962. "Non-nil means show all sibling heading when revealing a location.
  963. Org-mode often shows locations in an org-mode file which might have
  964. been invisible before. When this is set, the sibling of the current entry
  965. heading are all made visible. If `org-show-hierarchy-above' is t,
  966. the same happens on each level of the hierarchy above the current entry.
  967. By default this is on for the isearch context, off for all other contexts.
  968. Turning this off for example for sparse trees makes them very compact,
  969. but makes it harder to edit the location of the match. In such a case,
  970. use the command \\[org-reveal] to show more context.
  971. Instead of t, this can also be an alist specifying this option for different
  972. contexts. See `org-show-hierarchy-above' for valid contexts."
  973. :group 'org-reveal-location
  974. :type org-context-choice
  975. :version "24.4"
  976. :package-version '(Org . "8.0"))
  977. (defcustom org-show-entry-below '((default . nil))
  978. "Non-nil means show the entry below a headline when revealing a location.
  979. Org-mode often shows locations in an org-mode file which might have
  980. been invisible before. When this is set, the text below the headline that is
  981. exposed is also shown.
  982. By default this is off for all contexts.
  983. Instead of t, this can also be an alist specifying this option for different
  984. contexts. See `org-show-hierarchy-above' for valid contexts."
  985. :group 'org-reveal-location
  986. :type org-context-choice)
  987. (defcustom org-indirect-buffer-display 'other-window
  988. "How should indirect tree buffers be displayed?
  989. This applies to indirect buffers created with the commands
  990. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  991. Valid values are:
  992. current-window Display in the current window
  993. other-window Just display in another window.
  994. dedicated-frame Create one new frame, and re-use it each time.
  995. new-frame Make a new frame each time. Note that in this case
  996. previously-made indirect buffers are kept, and you need to
  997. kill these buffers yourself."
  998. :group 'org-structure
  999. :group 'org-agenda-windows
  1000. :type '(choice
  1001. (const :tag "In current window" current-window)
  1002. (const :tag "In current frame, other window" other-window)
  1003. (const :tag "Each time a new frame" new-frame)
  1004. (const :tag "One dedicated frame" dedicated-frame)))
  1005. (defcustom org-use-speed-commands nil
  1006. "Non-nil means activate single letter commands at beginning of a headline.
  1007. This may also be a function to test for appropriate locations where speed
  1008. commands should be active.
  1009. For example, to activate speed commands when the point is on any
  1010. star at the beginning of the headline, you can do this:
  1011. (setq org-use-speed-commands
  1012. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1013. :group 'org-structure
  1014. :type '(choice
  1015. (const :tag "Never" nil)
  1016. (const :tag "At beginning of headline stars" t)
  1017. (function)))
  1018. (defcustom org-speed-commands-user nil
  1019. "Alist of additional speed commands.
  1020. This list will be checked before `org-speed-commands-default'
  1021. when the variable `org-use-speed-commands' is non-nil
  1022. and when the cursor is at the beginning of a headline.
  1023. The car if each entry is a string with a single letter, which must
  1024. be assigned to `self-insert-command' in the global map.
  1025. The cdr is either a command to be called interactively, a function
  1026. to be called, or a form to be evaluated.
  1027. An entry that is just a list with a single string will be interpreted
  1028. as a descriptive headline that will be added when listing the speed
  1029. commands in the Help buffer using the `?' speed command."
  1030. :group 'org-structure
  1031. :type '(repeat :value ("k" . ignore)
  1032. (choice :value ("k" . ignore)
  1033. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1034. (cons :tag "Letter and Command"
  1035. (string :tag "Command letter")
  1036. (choice
  1037. (function)
  1038. (sexp))))))
  1039. (defcustom org-bookmark-names-plist
  1040. '(:last-capture "org-capture-last-stored"
  1041. :last-refile "org-refile-last-stored"
  1042. :last-capture-marker "org-capture-last-stored-marker")
  1043. "Names for bookmarks automatically set by some Org commands.
  1044. This can provide strings as names for a number of bookmarks Org sets
  1045. automatically. The following keys are currently implemented:
  1046. :last-capture
  1047. :last-capture-marker
  1048. :last-refile
  1049. When a key does not show up in the property list, the corresponding bookmark
  1050. is not set."
  1051. :group 'org-structure
  1052. :type 'plist)
  1053. (defgroup org-cycle nil
  1054. "Options concerning visibility cycling in Org-mode."
  1055. :tag "Org Cycle"
  1056. :group 'org-structure)
  1057. (defcustom org-cycle-skip-children-state-if-no-children t
  1058. "Non-nil means skip CHILDREN state in entries that don't have any."
  1059. :group 'org-cycle
  1060. :type 'boolean)
  1061. (defcustom org-cycle-max-level nil
  1062. "Maximum level which should still be subject to visibility cycling.
  1063. Levels higher than this will, for cycling, be treated as text, not a headline.
  1064. When `org-odd-levels-only' is set, a value of N in this variable actually
  1065. means 2N-1 stars as the limiting headline.
  1066. When nil, cycle all levels.
  1067. Note that the limiting level of cycling is also influenced by
  1068. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1069. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1070. than its value."
  1071. :group 'org-cycle
  1072. :type '(choice
  1073. (const :tag "No limit" nil)
  1074. (integer :tag "Maximum level")))
  1075. (defcustom org-hide-block-startup nil
  1076. "Non-nil means entering Org-mode will fold all blocks.
  1077. This can also be set in on a per-file basis with
  1078. #+STARTUP: hideblocks
  1079. #+STARTUP: showblocks"
  1080. :group 'org-startup
  1081. :group 'org-cycle
  1082. :type 'boolean)
  1083. (defcustom org-cycle-global-at-bob nil
  1084. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1085. This makes it possible to do global cycling without having to use S-TAB or
  1086. \\[universal-argument] TAB. For this special case to work, the first line
  1087. of the buffer must not be a headline -- it may be empty or some other text.
  1088. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1089. sure the cursor stays at the beginning of the buffer. When this option is
  1090. nil, don't do anything special at the beginning of the buffer."
  1091. :group 'org-cycle
  1092. :type 'boolean)
  1093. (defcustom org-cycle-level-after-item/entry-creation t
  1094. "Non-nil means cycle entry level or item indentation in new empty entries.
  1095. When the cursor is at the end of an empty headline, i.e., with only stars
  1096. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1097. and then all possible ancestor states, before returning to the original state.
  1098. This makes data entry extremely fast: M-RET to create a new headline,
  1099. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1100. When the cursor is at the end of an empty plain list item, one TAB will
  1101. make it a subitem, two or more tabs will back up to make this an item
  1102. higher up in the item hierarchy."
  1103. :group 'org-cycle
  1104. :type 'boolean)
  1105. (defcustom org-cycle-emulate-tab t
  1106. "Where should `org-cycle' emulate TAB.
  1107. nil Never
  1108. white Only in completely white lines
  1109. whitestart Only at the beginning of lines, before the first non-white char
  1110. t Everywhere except in headlines
  1111. exc-hl-bol Everywhere except at the start of a headline
  1112. If TAB is used in a place where it does not emulate TAB, the current subtree
  1113. visibility is cycled."
  1114. :group 'org-cycle
  1115. :type '(choice (const :tag "Never" nil)
  1116. (const :tag "Only in completely white lines" white)
  1117. (const :tag "Before first char in a line" whitestart)
  1118. (const :tag "Everywhere except in headlines" t)
  1119. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1120. (defcustom org-cycle-separator-lines 2
  1121. "Number of empty lines needed to keep an empty line between collapsed trees.
  1122. If you leave an empty line between the end of a subtree and the following
  1123. headline, this empty line is hidden when the subtree is folded.
  1124. Org-mode will leave (exactly) one empty line visible if the number of
  1125. empty lines is equal or larger to the number given in this variable.
  1126. So the default 2 means at least 2 empty lines after the end of a subtree
  1127. are needed to produce free space between a collapsed subtree and the
  1128. following headline.
  1129. If the number is negative, and the number of empty lines is at least -N,
  1130. all empty lines are shown.
  1131. Special case: when 0, never leave empty lines in collapsed view."
  1132. :group 'org-cycle
  1133. :type 'integer)
  1134. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1135. (defcustom org-pre-cycle-hook nil
  1136. "Hook that is run before visibility cycling is happening.
  1137. The function(s) in this hook must accept a single argument which indicates
  1138. the new state that will be set right after running this hook. The
  1139. argument is a symbol. Before a global state change, it can have the values
  1140. `overview', `content', or `all'. Before a local state change, it can have
  1141. the values `folded', `children', or `subtree'."
  1142. :group 'org-cycle
  1143. :type 'hook)
  1144. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1145. org-cycle-hide-drawers
  1146. org-cycle-hide-inline-tasks
  1147. org-cycle-show-empty-lines
  1148. org-optimize-window-after-visibility-change)
  1149. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1150. The function(s) in this hook must accept a single argument which indicates
  1151. the new state that was set by the most recent `org-cycle' command. The
  1152. argument is a symbol. After a global state change, it can have the values
  1153. `overview', `contents', or `all'. After a local state change, it can have
  1154. the values `folded', `children', or `subtree'."
  1155. :group 'org-cycle
  1156. :type 'hook)
  1157. (defgroup org-edit-structure nil
  1158. "Options concerning structure editing in Org-mode."
  1159. :tag "Org Edit Structure"
  1160. :group 'org-structure)
  1161. (defcustom org-odd-levels-only nil
  1162. "Non-nil means skip even levels and only use odd levels for the outline.
  1163. This has the effect that two stars are being added/taken away in
  1164. promotion/demotion commands. It also influences how levels are
  1165. handled by the exporters.
  1166. Changing it requires restart of `font-lock-mode' to become effective
  1167. for fontification also in regions already fontified.
  1168. You may also set this on a per-file basis by adding one of the following
  1169. lines to the buffer:
  1170. #+STARTUP: odd
  1171. #+STARTUP: oddeven"
  1172. :group 'org-edit-structure
  1173. :group 'org-appearance
  1174. :type 'boolean)
  1175. (defcustom org-adapt-indentation t
  1176. "Non-nil means adapt indentation to outline node level.
  1177. When this variable is set, Org assumes that you write outlines by
  1178. indenting text in each node to align with the headline (after the stars).
  1179. The following issues are influenced by this variable:
  1180. - When this is set and the *entire* text in an entry is indented, the
  1181. indentation is increased by one space in a demotion command, and
  1182. decreased by one in a promotion command. If any line in the entry
  1183. body starts with text at column 0, indentation is not changed at all.
  1184. - Property drawers and planning information is inserted indented when
  1185. this variable s set. When nil, they will not be indented.
  1186. - TAB indents a line relative to context. The lines below a headline
  1187. will be indented when this variable is set.
  1188. Note that this is all about true indentation, by adding and removing
  1189. space characters. See also `org-indent.el' which does level-dependent
  1190. indentation in a virtual way, i.e. at display time in Emacs."
  1191. :group 'org-edit-structure
  1192. :type 'boolean)
  1193. (defcustom org-special-ctrl-a/e nil
  1194. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1195. When t, `C-a' will bring back the cursor to the beginning of the
  1196. headline text, i.e. after the stars and after a possible TODO
  1197. keyword. In an item, this will be the position after bullet and
  1198. check-box, if any. When the cursor is already at that position,
  1199. another `C-a' will bring it to the beginning of the line.
  1200. `C-e' will jump to the end of the headline, ignoring the presence
  1201. of tags in the headline. A second `C-e' will then jump to the
  1202. true end of the line, after any tags. This also means that, when
  1203. this variable is non-nil, `C-e' also will never jump beyond the
  1204. end of the heading of a folded section, i.e. not after the
  1205. ellipses.
  1206. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1207. normally, going to the true line boundary first. Only a directly
  1208. following, identical keypress will bring the cursor to the
  1209. special positions.
  1210. This may also be a cons cell where the behavior for `C-a' and
  1211. `C-e' is set separately."
  1212. :group 'org-edit-structure
  1213. :type '(choice
  1214. (const :tag "off" nil)
  1215. (const :tag "on: after stars/bullet and before tags first" t)
  1216. (const :tag "reversed: true line boundary first" reversed)
  1217. (cons :tag "Set C-a and C-e separately"
  1218. (choice :tag "Special C-a"
  1219. (const :tag "off" nil)
  1220. (const :tag "on: after stars/bullet first" t)
  1221. (const :tag "reversed: before stars/bullet first" reversed))
  1222. (choice :tag "Special C-e"
  1223. (const :tag "off" nil)
  1224. (const :tag "on: before tags first" t)
  1225. (const :tag "reversed: after tags first" reversed)))))
  1226. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1227. (defcustom org-special-ctrl-k nil
  1228. "Non-nil means `C-k' will behave specially in headlines.
  1229. When nil, `C-k' will call the default `kill-line' command.
  1230. When t, the following will happen while the cursor is in the headline:
  1231. - When the cursor is at the beginning of a headline, kill the entire
  1232. line and possible the folded subtree below the line.
  1233. - When in the middle of the headline text, kill the headline up to the tags.
  1234. - When after the headline text, kill the tags."
  1235. :group 'org-edit-structure
  1236. :type 'boolean)
  1237. (defcustom org-ctrl-k-protect-subtree nil
  1238. "Non-nil means, do not delete a hidden subtree with C-k.
  1239. When set to the symbol `error', simply throw an error when C-k is
  1240. used to kill (part-of) a headline that has hidden text behind it.
  1241. Any other non-nil value will result in a query to the user, if it is
  1242. OK to kill that hidden subtree. When nil, kill without remorse."
  1243. :group 'org-edit-structure
  1244. :version "24.1"
  1245. :type '(choice
  1246. (const :tag "Do not protect hidden subtrees" nil)
  1247. (const :tag "Protect hidden subtrees with a security query" t)
  1248. (const :tag "Never kill a hidden subtree with C-k" error)))
  1249. (defcustom org-special-ctrl-o t
  1250. "Non-nil means, make `C-o' insert a row in tables."
  1251. :group 'org-edit-structure
  1252. :type 'boolean)
  1253. (defcustom org-catch-invisible-edits nil
  1254. "Check if in invisible region before inserting or deleting a character.
  1255. Valid values are:
  1256. nil Do not check, so just do invisible edits.
  1257. error Throw an error and do nothing.
  1258. show Make point visible, and do the requested edit.
  1259. show-and-error Make point visible, then throw an error and abort the edit.
  1260. smart Make point visible, and do insertion/deletion if it is
  1261. adjacent to visible text and the change feels predictable.
  1262. Never delete a previously invisible character or add in the
  1263. middle or right after an invisible region. Basically, this
  1264. allows insertion and backward-delete right before ellipses.
  1265. FIXME: maybe in this case we should not even show?"
  1266. :group 'org-edit-structure
  1267. :version "24.1"
  1268. :type '(choice
  1269. (const :tag "Do not check" nil)
  1270. (const :tag "Throw error when trying to edit" error)
  1271. (const :tag "Unhide, but do not do the edit" show-and-error)
  1272. (const :tag "Show invisible part and do the edit" show)
  1273. (const :tag "Be smart and do the right thing" smart)))
  1274. (defcustom org-yank-folded-subtrees t
  1275. "Non-nil means when yanking subtrees, fold them.
  1276. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1277. it starts with a heading and all other headings in it are either children
  1278. or siblings, then fold all the subtrees. However, do this only if no
  1279. text after the yank would be swallowed into a folded tree by this action."
  1280. :group 'org-edit-structure
  1281. :type 'boolean)
  1282. (defcustom org-yank-adjusted-subtrees nil
  1283. "Non-nil means when yanking subtrees, adjust the level.
  1284. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1285. this function for details."
  1286. :group 'org-edit-structure
  1287. :type 'boolean)
  1288. (defcustom org-M-RET-may-split-line '((default . t))
  1289. "Non-nil means M-RET will split the line at the cursor position.
  1290. When nil, it will go to the end of the line before making a
  1291. new line.
  1292. You may also set this option in a different way for different
  1293. contexts. Valid contexts are:
  1294. headline when creating a new headline
  1295. item when creating a new item
  1296. table in a table field
  1297. default the value to be used for all contexts not explicitly
  1298. customized"
  1299. :group 'org-structure
  1300. :group 'org-table
  1301. :type '(choice
  1302. (const :tag "Always" t)
  1303. (const :tag "Never" nil)
  1304. (repeat :greedy t :tag "Individual contexts"
  1305. (cons
  1306. (choice :tag "Context"
  1307. (const headline)
  1308. (const item)
  1309. (const table)
  1310. (const default))
  1311. (boolean)))))
  1312. (defcustom org-insert-heading-respect-content nil
  1313. "Non-nil means insert new headings after the current subtree.
  1314. When nil, the new heading is created directly after the current line.
  1315. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1316. this variable on for the duration of the command."
  1317. :group 'org-structure
  1318. :type 'boolean)
  1319. (defcustom org-blank-before-new-entry '((heading . auto)
  1320. (plain-list-item . auto))
  1321. "Should `org-insert-heading' leave a blank line before new heading/item?
  1322. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1323. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1324. which case Org will look at the surrounding headings/items and try to
  1325. make an intelligent decision whether to insert a blank line or not.
  1326. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1327. the setting here is ignored and no empty line is inserted to avoid breaking
  1328. the list structure."
  1329. :group 'org-edit-structure
  1330. :type '(list
  1331. (cons (const heading)
  1332. (choice (const :tag "Never" nil)
  1333. (const :tag "Always" t)
  1334. (const :tag "Auto" auto)))
  1335. (cons (const plain-list-item)
  1336. (choice (const :tag "Never" nil)
  1337. (const :tag "Always" t)
  1338. (const :tag "Auto" auto)))))
  1339. (defcustom org-insert-heading-hook nil
  1340. "Hook being run after inserting a new heading."
  1341. :group 'org-edit-structure
  1342. :type 'hook)
  1343. (defcustom org-enable-fixed-width-editor t
  1344. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1345. This currently only means they are never auto-wrapped.
  1346. When nil, such lines will be treated like ordinary lines."
  1347. :group 'org-edit-structure
  1348. :type 'boolean)
  1349. (defcustom org-goto-auto-isearch t
  1350. "Non-nil means typing characters in `org-goto' starts incremental search.
  1351. When nil, you can use these keybindings to navigate the buffer:
  1352. q Quit the org-goto interface
  1353. n Go to the next visible heading
  1354. p Go to the previous visible heading
  1355. f Go one heading forward on same level
  1356. b Go one heading backward on same level
  1357. u Go one heading up"
  1358. :group 'org-edit-structure
  1359. :type 'boolean)
  1360. (defgroup org-sparse-trees nil
  1361. "Options concerning sparse trees in Org-mode."
  1362. :tag "Org Sparse Trees"
  1363. :group 'org-structure)
  1364. (defcustom org-highlight-sparse-tree-matches t
  1365. "Non-nil means highlight all matches that define a sparse tree.
  1366. The highlights will automatically disappear the next time the buffer is
  1367. changed by an edit command."
  1368. :group 'org-sparse-trees
  1369. :type 'boolean)
  1370. (defcustom org-remove-highlights-with-change t
  1371. "Non-nil means any change to the buffer will remove temporary highlights.
  1372. Such highlights are created by `org-occur' and `org-clock-display'.
  1373. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1374. The highlights created by `org-preview-latex-fragment' always need
  1375. `C-c C-c' to be removed."
  1376. :group 'org-sparse-trees
  1377. :group 'org-time
  1378. :type 'boolean)
  1379. (defcustom org-occur-hook '(org-first-headline-recenter)
  1380. "Hook that is run after `org-occur' has constructed a sparse tree.
  1381. This can be used to recenter the window to show as much of the structure
  1382. as possible."
  1383. :group 'org-sparse-trees
  1384. :type 'hook)
  1385. (defgroup org-imenu-and-speedbar nil
  1386. "Options concerning imenu and speedbar in Org-mode."
  1387. :tag "Org Imenu and Speedbar"
  1388. :group 'org-structure)
  1389. (defcustom org-imenu-depth 2
  1390. "The maximum level for Imenu access to Org-mode headlines.
  1391. This also applied for speedbar access."
  1392. :group 'org-imenu-and-speedbar
  1393. :type 'integer)
  1394. (defgroup org-table nil
  1395. "Options concerning tables in Org-mode."
  1396. :tag "Org Table"
  1397. :group 'org)
  1398. (defcustom org-enable-table-editor 'optimized
  1399. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1400. When nil, such lines will be treated like ordinary lines.
  1401. When equal to the symbol `optimized', the table editor will be optimized to
  1402. do the following:
  1403. - Automatic overwrite mode in front of whitespace in table fields.
  1404. This makes the structure of the table stay in tact as long as the edited
  1405. field does not exceed the column width.
  1406. - Minimize the number of realigns. Normally, the table is aligned each time
  1407. TAB or RET are pressed to move to another field. With optimization this
  1408. happens only if changes to a field might have changed the column width.
  1409. Optimization requires replacing the functions `self-insert-command',
  1410. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1411. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1412. very good at guessing when a re-align will be necessary, but you can always
  1413. force one with \\[org-ctrl-c-ctrl-c].
  1414. If you would like to use the optimized version in Org-mode, but the
  1415. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1416. This variable can be used to turn on and off the table editor during a session,
  1417. but in order to toggle optimization, a restart is required.
  1418. See also the variable `org-table-auto-blank-field'."
  1419. :group 'org-table
  1420. :type '(choice
  1421. (const :tag "off" nil)
  1422. (const :tag "on" t)
  1423. (const :tag "on, optimized" optimized)))
  1424. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1425. (version<= emacs-version "24.1"))
  1426. "Non-nil means cluster self-insert commands for undo when possible.
  1427. If this is set, then, like in the Emacs command loop, 20 consecutive
  1428. characters will be undone together.
  1429. This is configurable, because there is some impact on typing performance."
  1430. :group 'org-table
  1431. :type 'boolean)
  1432. (defcustom org-table-tab-recognizes-table.el t
  1433. "Non-nil means TAB will automatically notice a table.el table.
  1434. When it sees such a table, it moves point into it and - if necessary -
  1435. calls `table-recognize-table'."
  1436. :group 'org-table-editing
  1437. :type 'boolean)
  1438. (defgroup org-link nil
  1439. "Options concerning links in Org-mode."
  1440. :tag "Org Link"
  1441. :group 'org)
  1442. (defvar org-link-abbrev-alist-local nil
  1443. "Buffer-local version of `org-link-abbrev-alist', which see.
  1444. The value of this is taken from the #+LINK lines.")
  1445. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1446. (defcustom org-link-abbrev-alist nil
  1447. "Alist of link abbreviations.
  1448. The car of each element is a string, to be replaced at the start of a link.
  1449. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1450. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1451. [[linkkey:tag][description]]
  1452. The 'linkkey' must be a word word, starting with a letter, followed
  1453. by letters, numbers, '-' or '_'.
  1454. If REPLACE is a string, the tag will simply be appended to create the link.
  1455. If the string contains \"%s\", the tag will be inserted there. If the string
  1456. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1457. at that point (see the function `url-hexify-string'). If the string contains
  1458. the specifier \"%(my-function)\", then the custom function `my-function' will
  1459. be invoked: this function takes the tag as its only argument and must return
  1460. a string.
  1461. REPLACE may also be a function that will be called with the tag as the
  1462. only argument to create the link, which should be returned as a string.
  1463. See the manual for examples."
  1464. :group 'org-link
  1465. :type '(repeat
  1466. (cons
  1467. (string :tag "Protocol")
  1468. (choice
  1469. (string :tag "Format")
  1470. (function)))))
  1471. (defcustom org-descriptive-links t
  1472. "Non-nil means Org will display descriptive links.
  1473. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1474. \"Org Website\", hiding the link itself and just displaying its
  1475. description. When set to `nil', Org will display the full links
  1476. literally.
  1477. You can interactively set the value of this variable by calling
  1478. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1479. :group 'org-link
  1480. :type 'boolean)
  1481. (defcustom org-link-file-path-type 'adaptive
  1482. "How the path name in file links should be stored.
  1483. Valid values are:
  1484. relative Relative to the current directory, i.e. the directory of the file
  1485. into which the link is being inserted.
  1486. absolute Absolute path, if possible with ~ for home directory.
  1487. noabbrev Absolute path, no abbreviation of home directory.
  1488. adaptive Use relative path for files in the current directory and sub-
  1489. directories of it. For other files, use an absolute path."
  1490. :group 'org-link
  1491. :type '(choice
  1492. (const relative)
  1493. (const absolute)
  1494. (const noabbrev)
  1495. (const adaptive)))
  1496. (defvaralias 'org-activate-links 'org-highlight-links)
  1497. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1498. "Types of links that should be highlighted in Org-mode files.
  1499. This is a list of symbols, each one of them leading to the
  1500. highlighting of a certain link type.
  1501. You can still open links that are not highlighted.
  1502. In principle, it does not hurt to turn on highlighting for all
  1503. link types. There may be a small gain when turning off unused
  1504. link types. The types are:
  1505. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1506. angle Links in angular brackets that may contain whitespace like
  1507. <bbdb:Carsten Dominik>.
  1508. plain Plain links in normal text, no whitespace, like http://google.com.
  1509. radio Text that is matched by a radio target, see manual for details.
  1510. tag Tag settings in a headline (link to tag search).
  1511. date Time stamps (link to calendar).
  1512. footnote Footnote labels.
  1513. If you set this variable during an Emacs session, use `org-mode-restart'
  1514. in the Org buffer so that the change takes effect."
  1515. :group 'org-link
  1516. :group 'org-appearance
  1517. :type '(set :greedy t
  1518. (const :tag "Double bracket links" bracket)
  1519. (const :tag "Angular bracket links" angle)
  1520. (const :tag "Plain text links" plain)
  1521. (const :tag "Radio target matches" radio)
  1522. (const :tag "Tags" tag)
  1523. (const :tag "Timestamps" date)
  1524. (const :tag "Footnotes" footnote)))
  1525. (defcustom org-make-link-description-function nil
  1526. "Function to use for generating link descriptions from links.
  1527. When nil, the link location will be used. This function must take
  1528. two parameters: the first one is the link, the second one is the
  1529. description generated by `org-insert-link'. The function should
  1530. return the description to use."
  1531. :group 'org-link
  1532. :type '(choice (const nil) (function)))
  1533. (defgroup org-link-store nil
  1534. "Options concerning storing links in Org-mode."
  1535. :tag "Org Store Link"
  1536. :group 'org-link)
  1537. (defcustom org-url-hexify-p t
  1538. "When non-nil, hexify URL when creating a link."
  1539. :type 'boolean
  1540. :version "24.3"
  1541. :group 'org-link-store)
  1542. (defcustom org-email-link-description-format "Email %c: %.30s"
  1543. "Format of the description part of a link to an email or usenet message.
  1544. The following %-escapes will be replaced by corresponding information:
  1545. %F full \"From\" field
  1546. %f name, taken from \"From\" field, address if no name
  1547. %T full \"To\" field
  1548. %t first name in \"To\" field, address if no name
  1549. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1550. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1551. %s subject
  1552. %d date
  1553. %m message-id.
  1554. You may use normal field width specification between the % and the letter.
  1555. This is for example useful to limit the length of the subject.
  1556. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1557. :group 'org-link-store
  1558. :type 'string)
  1559. (defcustom org-from-is-user-regexp
  1560. (let (r1 r2)
  1561. (when (and user-mail-address (not (string= user-mail-address "")))
  1562. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1563. (when (and user-full-name (not (string= user-full-name "")))
  1564. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1565. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1566. "Regexp matched against the \"From:\" header of an email or usenet message.
  1567. It should match if the message is from the user him/herself."
  1568. :group 'org-link-store
  1569. :type 'regexp)
  1570. (defcustom org-context-in-file-links t
  1571. "Non-nil means file links from `org-store-link' contain context.
  1572. A search string will be added to the file name with :: as separator and
  1573. used to find the context when the link is activated by the command
  1574. `org-open-at-point'. When this option is t, the entire active region
  1575. will be placed in the search string of the file link. If set to a
  1576. positive integer, only the first n lines of context will be stored.
  1577. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1578. negates this setting for the duration of the command."
  1579. :group 'org-link-store
  1580. :type '(choice boolean integer))
  1581. (defcustom org-keep-stored-link-after-insertion nil
  1582. "Non-nil means keep link in list for entire session.
  1583. The command `org-store-link' adds a link pointing to the current
  1584. location to an internal list. These links accumulate during a session.
  1585. The command `org-insert-link' can be used to insert links into any
  1586. Org-mode file (offering completion for all stored links). When this
  1587. option is nil, every link which has been inserted once using \\[org-insert-link]
  1588. will be removed from the list, to make completing the unused links
  1589. more efficient."
  1590. :group 'org-link-store
  1591. :type 'boolean)
  1592. (defgroup org-link-follow nil
  1593. "Options concerning following links in Org-mode."
  1594. :tag "Org Follow Link"
  1595. :group 'org-link)
  1596. (defcustom org-link-translation-function nil
  1597. "Function to translate links with different syntax to Org syntax.
  1598. This can be used to translate links created for example by the Planner
  1599. or emacs-wiki packages to Org syntax.
  1600. The function must accept two parameters, a TYPE containing the link
  1601. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1602. which is everything after the link protocol. It should return a cons
  1603. with possibly modified values of type and path.
  1604. Org contains a function for this, so if you set this variable to
  1605. `org-translate-link-from-planner', you should be able follow many
  1606. links created by planner."
  1607. :group 'org-link-follow
  1608. :type '(choice (const nil) (function)))
  1609. (defcustom org-follow-link-hook nil
  1610. "Hook that is run after a link has been followed."
  1611. :group 'org-link-follow
  1612. :type 'hook)
  1613. (defcustom org-tab-follows-link nil
  1614. "Non-nil means on links TAB will follow the link.
  1615. Needs to be set before org.el is loaded.
  1616. This really should not be used, it does not make sense, and the
  1617. implementation is bad."
  1618. :group 'org-link-follow
  1619. :type 'boolean)
  1620. (defcustom org-return-follows-link nil
  1621. "Non-nil means on links RET will follow the link.
  1622. In tables, the special behavior of RET has precedence."
  1623. :group 'org-link-follow
  1624. :type 'boolean)
  1625. (defcustom org-mouse-1-follows-link
  1626. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1627. "Non-nil means mouse-1 on a link will follow the link.
  1628. A longer mouse click will still set point. Does not work on XEmacs.
  1629. Needs to be set before org.el is loaded."
  1630. :group 'org-link-follow
  1631. :version "24.4"
  1632. :package-version '(Org . "8.3")
  1633. :type '(choice
  1634. (const :tag "A double click follows the link" double)
  1635. (const :tag "Unconditionally follow the link with mouse-1" t)
  1636. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1637. (defcustom org-mark-ring-length 4
  1638. "Number of different positions to be recorded in the ring.
  1639. Changing this requires a restart of Emacs to work correctly."
  1640. :group 'org-link-follow
  1641. :type 'integer)
  1642. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1643. "Non-nil means internal links in Org files must exactly match a headline.
  1644. When nil, the link search tries to match a phrase with all words
  1645. in the search text."
  1646. :group 'org-link-follow
  1647. :version "24.1"
  1648. :type '(choice
  1649. (const :tag "Use fuzzy text search" nil)
  1650. (const :tag "Match only exact headline" t)
  1651. (const :tag "Match exact headline or query to create it"
  1652. query-to-create)))
  1653. (defcustom org-link-frame-setup
  1654. '((vm . vm-visit-folder-other-frame)
  1655. (vm-imap . vm-visit-imap-folder-other-frame)
  1656. (gnus . org-gnus-no-new-news)
  1657. (file . find-file-other-window)
  1658. (wl . wl-other-frame))
  1659. "Setup the frame configuration for following links.
  1660. When following a link with Emacs, it may often be useful to display
  1661. this link in another window or frame. This variable can be used to
  1662. set this up for the different types of links.
  1663. For VM, use any of
  1664. `vm-visit-folder'
  1665. `vm-visit-folder-other-window'
  1666. `vm-visit-folder-other-frame'
  1667. For Gnus, use any of
  1668. `gnus'
  1669. `gnus-other-frame'
  1670. `org-gnus-no-new-news'
  1671. For FILE, use any of
  1672. `find-file'
  1673. `find-file-other-window'
  1674. `find-file-other-frame'
  1675. For Wanderlust use any of
  1676. `wl'
  1677. `wl-other-frame'
  1678. For the calendar, use the variable `calendar-setup'.
  1679. For BBDB, it is currently only possible to display the matches in
  1680. another window."
  1681. :group 'org-link-follow
  1682. :type '(list
  1683. (cons (const vm)
  1684. (choice
  1685. (const vm-visit-folder)
  1686. (const vm-visit-folder-other-window)
  1687. (const vm-visit-folder-other-frame)))
  1688. (cons (const vm-imap)
  1689. (choice
  1690. (const vm-visit-imap-folder)
  1691. (const vm-visit-imap-folder-other-window)
  1692. (const vm-visit-imap-folder-other-frame)))
  1693. (cons (const gnus)
  1694. (choice
  1695. (const gnus)
  1696. (const gnus-other-frame)
  1697. (const org-gnus-no-new-news)))
  1698. (cons (const file)
  1699. (choice
  1700. (const find-file)
  1701. (const find-file-other-window)
  1702. (const find-file-other-frame)))
  1703. (cons (const wl)
  1704. (choice
  1705. (const wl)
  1706. (const wl-other-frame)))))
  1707. (defcustom org-display-internal-link-with-indirect-buffer nil
  1708. "Non-nil means use indirect buffer to display infile links.
  1709. Activating internal links (from one location in a file to another location
  1710. in the same file) normally just jumps to the location. When the link is
  1711. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1712. is displayed in
  1713. another window. When this option is set, the other window actually displays
  1714. an indirect buffer clone of the current buffer, to avoid any visibility
  1715. changes to the current buffer."
  1716. :group 'org-link-follow
  1717. :type 'boolean)
  1718. (defcustom org-open-non-existing-files nil
  1719. "Non-nil means `org-open-file' will open non-existing files.
  1720. When nil, an error will be generated.
  1721. This variable applies only to external applications because they
  1722. might choke on non-existing files. If the link is to a file that
  1723. will be opened in Emacs, the variable is ignored."
  1724. :group 'org-link-follow
  1725. :type 'boolean)
  1726. (defcustom org-open-directory-means-index-dot-org nil
  1727. "Non-nil means a link to a directory really means to index.org.
  1728. When nil, following a directory link will run dired or open a finder/explorer
  1729. window on that directory."
  1730. :group 'org-link-follow
  1731. :type 'boolean)
  1732. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1733. "Non-nil means ask for confirmation before executing shell links.
  1734. Shell links can be dangerous: just think about a link
  1735. [[shell:rm -rf ~/*][Google Search]]
  1736. This link would show up in your Org-mode document as \"Google Search\",
  1737. but really it would remove your entire home directory.
  1738. Therefore we advise against setting this variable to nil.
  1739. Just change it to `y-or-n-p' if you want to confirm with a
  1740. single keystroke rather than having to type \"yes\"."
  1741. :group 'org-link-follow
  1742. :type '(choice
  1743. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1744. (const :tag "with y-or-n (faster)" y-or-n-p)
  1745. (const :tag "no confirmation (dangerous)" nil)))
  1746. (put 'org-confirm-shell-link-function
  1747. 'safe-local-variable
  1748. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1749. (defcustom org-confirm-shell-link-not-regexp ""
  1750. "A regexp to skip confirmation for shell links."
  1751. :group 'org-link-follow
  1752. :version "24.1"
  1753. :type 'regexp)
  1754. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1755. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1756. Elisp links can be dangerous: just think about a link
  1757. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1758. This link would show up in your Org-mode document as \"Google Search\",
  1759. but really it would remove your entire home directory.
  1760. Therefore we advise against setting this variable to nil.
  1761. Just change it to `y-or-n-p' if you want to confirm with a
  1762. single keystroke rather than having to type \"yes\"."
  1763. :group 'org-link-follow
  1764. :type '(choice
  1765. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1766. (const :tag "with y-or-n (faster)" y-or-n-p)
  1767. (const :tag "no confirmation (dangerous)" nil)))
  1768. (put 'org-confirm-shell-link-function
  1769. 'safe-local-variable
  1770. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1771. (defcustom org-confirm-elisp-link-not-regexp ""
  1772. "A regexp to skip confirmation for Elisp links."
  1773. :group 'org-link-follow
  1774. :version "24.1"
  1775. :type 'regexp)
  1776. (defconst org-file-apps-defaults-gnu
  1777. '((remote . emacs)
  1778. (system . mailcap)
  1779. (t . mailcap))
  1780. "Default file applications on a UNIX or GNU/Linux system.
  1781. See `org-file-apps'.")
  1782. (defconst org-file-apps-defaults-macosx
  1783. '((remote . emacs)
  1784. (t . "open %s")
  1785. (system . "open %s")
  1786. ("ps.gz" . "gv %s")
  1787. ("eps.gz" . "gv %s")
  1788. ("dvi" . "xdvi %s")
  1789. ("fig" . "xfig %s"))
  1790. "Default file applications on a MacOS X system.
  1791. The system \"open\" is known as a default, but we use X11 applications
  1792. for some files for which the OS does not have a good default.
  1793. See `org-file-apps'.")
  1794. (defconst org-file-apps-defaults-windowsnt
  1795. (list
  1796. '(remote . emacs)
  1797. (cons t
  1798. (list (if (featurep 'xemacs)
  1799. 'mswindows-shell-execute
  1800. 'w32-shell-execute)
  1801. "open" 'file))
  1802. (cons 'system
  1803. (list (if (featurep 'xemacs)
  1804. 'mswindows-shell-execute
  1805. 'w32-shell-execute)
  1806. "open" 'file)))
  1807. "Default file applications on a Windows NT system.
  1808. The system \"open\" is used for most files.
  1809. See `org-file-apps'.")
  1810. (defcustom org-file-apps
  1811. '((auto-mode . emacs)
  1812. ("\\.mm\\'" . default)
  1813. ("\\.x?html?\\'" . default)
  1814. ("\\.pdf\\'" . default))
  1815. "External applications for opening `file:path' items in a document.
  1816. Org-mode uses system defaults for different file types, but
  1817. you can use this variable to set the application for a given file
  1818. extension. The entries in this list are cons cells where the car identifies
  1819. files and the cdr the corresponding command. Possible values for the
  1820. file identifier are
  1821. \"string\" A string as a file identifier can be interpreted in different
  1822. ways, depending on its contents:
  1823. - Alphanumeric characters only:
  1824. Match links with this file extension.
  1825. Example: (\"pdf\" . \"evince %s\")
  1826. to open PDFs with evince.
  1827. - Regular expression: Match links where the
  1828. filename matches the regexp. If you want to
  1829. use groups here, use shy groups.
  1830. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1831. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1832. to open *.html and *.xhtml with firefox.
  1833. - Regular expression which contains (non-shy) groups:
  1834. Match links where the whole link, including \"::\", and
  1835. anything after that, matches the regexp.
  1836. In a custom command string, %1, %2, etc. are replaced with
  1837. the parts of the link that were matched by the groups.
  1838. For backwards compatibility, if a command string is given
  1839. that does not use any of the group matches, this case is
  1840. handled identically to the second one (i.e. match against
  1841. file name only).
  1842. In a custom lisp form, you can access the group matches with
  1843. (match-string n link).
  1844. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1845. to open [[file:document.pdf::5]] with evince at page 5.
  1846. `directory' Matches a directory
  1847. `remote' Matches a remote file, accessible through tramp or efs.
  1848. Remote files most likely should be visited through Emacs
  1849. because external applications cannot handle such paths.
  1850. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1851. so all files Emacs knows how to handle. Using this with
  1852. command `emacs' will open most files in Emacs. Beware that this
  1853. will also open html files inside Emacs, unless you add
  1854. (\"html\" . default) to the list as well.
  1855. t Default for files not matched by any of the other options.
  1856. `system' The system command to open files, like `open' on Windows
  1857. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1858. that will be selected if you call `C-c C-o' with a double
  1859. \\[universal-argument] \\[universal-argument] prefix.
  1860. Possible values for the command are:
  1861. `emacs' The file will be visited by the current Emacs process.
  1862. `default' Use the default application for this file type, which is the
  1863. association for t in the list, most likely in the system-specific
  1864. part.
  1865. This can be used to overrule an unwanted setting in the
  1866. system-specific variable.
  1867. `system' Use the system command for opening files, like \"open\".
  1868. This command is specified by the entry whose car is `system'.
  1869. Most likely, the system-specific version of this variable
  1870. does define this command, but you can overrule/replace it
  1871. here.
  1872. string A command to be executed by a shell; %s will be replaced
  1873. by the path to the file.
  1874. sexp A Lisp form which will be evaluated. The file path will
  1875. be available in the Lisp variable `file'.
  1876. For more examples, see the system specific constants
  1877. `org-file-apps-defaults-macosx'
  1878. `org-file-apps-defaults-windowsnt'
  1879. `org-file-apps-defaults-gnu'."
  1880. :group 'org-link-follow
  1881. :type '(repeat
  1882. (cons (choice :value ""
  1883. (string :tag "Extension")
  1884. (const :tag "System command to open files" system)
  1885. (const :tag "Default for unrecognized files" t)
  1886. (const :tag "Remote file" remote)
  1887. (const :tag "Links to a directory" directory)
  1888. (const :tag "Any files that have Emacs modes"
  1889. auto-mode))
  1890. (choice :value ""
  1891. (const :tag "Visit with Emacs" emacs)
  1892. (const :tag "Use default" default)
  1893. (const :tag "Use the system command" system)
  1894. (string :tag "Command")
  1895. (sexp :tag "Lisp form")))))
  1896. (defcustom org-doi-server-url "http://dx.doi.org/"
  1897. "The URL of the DOI server."
  1898. :type 'string
  1899. :version "24.3"
  1900. :group 'org-link-follow)
  1901. (defgroup org-refile nil
  1902. "Options concerning refiling entries in Org-mode."
  1903. :tag "Org Refile"
  1904. :group 'org)
  1905. (defcustom org-directory "~/org"
  1906. "Directory with org files.
  1907. This is just a default location to look for Org files. There is no need
  1908. at all to put your files into this directory. It is only used in the
  1909. following situations:
  1910. 1. When a capture template specifies a target file that is not an
  1911. absolute path. The path will then be interpreted relative to
  1912. `org-directory'
  1913. 2. When a capture note is filed away in an interactive way (when exiting the
  1914. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1915. with `org-directory' as the default path."
  1916. :group 'org-refile
  1917. :group 'org-capture
  1918. :type 'directory)
  1919. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1920. "Default target for storing notes.
  1921. Used as a fall back file for org-capture.el, for templates that
  1922. do not specify a target file."
  1923. :group 'org-refile
  1924. :group 'org-capture
  1925. :type '(choice
  1926. (const :tag "Default from remember-data-file" nil)
  1927. file))
  1928. (defcustom org-goto-interface 'outline
  1929. "The default interface to be used for `org-goto'.
  1930. Allowed values are:
  1931. outline The interface shows an outline of the relevant file
  1932. and the correct heading is found by moving through
  1933. the outline or by searching with incremental search.
  1934. outline-path-completion Headlines in the current buffer are offered via
  1935. completion. This is the interface also used by
  1936. the refile command."
  1937. :group 'org-refile
  1938. :type '(choice
  1939. (const :tag "Outline" outline)
  1940. (const :tag "Outline-path-completion" outline-path-completion)))
  1941. (defcustom org-goto-max-level 5
  1942. "Maximum target level when running `org-goto' with refile interface."
  1943. :group 'org-refile
  1944. :type 'integer)
  1945. (defcustom org-reverse-note-order nil
  1946. "Non-nil means store new notes at the beginning of a file or entry.
  1947. When nil, new notes will be filed to the end of a file or entry.
  1948. This can also be a list with cons cells of regular expressions that
  1949. are matched against file names, and values."
  1950. :group 'org-capture
  1951. :group 'org-refile
  1952. :type '(choice
  1953. (const :tag "Reverse always" t)
  1954. (const :tag "Reverse never" nil)
  1955. (repeat :tag "By file name regexp"
  1956. (cons regexp boolean))))
  1957. (defcustom org-log-refile nil
  1958. "Information to record when a task is refiled.
  1959. Possible values are:
  1960. nil Don't add anything
  1961. time Add a time stamp to the task
  1962. note Prompt for a note and add it with template `org-log-note-headings'
  1963. This option can also be set with on a per-file-basis with
  1964. #+STARTUP: nologrefile
  1965. #+STARTUP: logrefile
  1966. #+STARTUP: lognoterefile
  1967. You can have local logging settings for a subtree by setting the LOGGING
  1968. property to one or more of these keywords.
  1969. When bulk-refiling from the agenda, the value `note' is forbidden and
  1970. will temporarily be changed to `time'."
  1971. :group 'org-refile
  1972. :group 'org-progress
  1973. :version "24.1"
  1974. :type '(choice
  1975. (const :tag "No logging" nil)
  1976. (const :tag "Record timestamp" time)
  1977. (const :tag "Record timestamp with note." note)))
  1978. (defcustom org-refile-targets nil
  1979. "Targets for refiling entries with \\[org-refile].
  1980. This is a list of cons cells. Each cell contains:
  1981. - a specification of the files to be considered, either a list of files,
  1982. or a symbol whose function or variable value will be used to retrieve
  1983. a file name or a list of file names. If you use `org-agenda-files' for
  1984. that, all agenda files will be scanned for targets. Nil means consider
  1985. headings in the current buffer.
  1986. - A specification of how to find candidate refile targets. This may be
  1987. any of:
  1988. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1989. This tag has to be present in all target headlines, inheritance will
  1990. not be considered.
  1991. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1992. todo keyword.
  1993. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1994. headlines that are refiling targets.
  1995. - a cons cell (:level . N). Any headline of level N is considered a target.
  1996. Note that, when `org-odd-levels-only' is set, level corresponds to
  1997. order in hierarchy, not to the number of stars.
  1998. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1999. Note that, when `org-odd-levels-only' is set, level corresponds to
  2000. order in hierarchy, not to the number of stars.
  2001. Each element of this list generates a set of possible targets.
  2002. The union of these sets is presented (with completion) to
  2003. the user by `org-refile'.
  2004. You can set the variable `org-refile-target-verify-function' to a function
  2005. to verify each headline found by the simple criteria above.
  2006. When this variable is nil, all top-level headlines in the current buffer
  2007. are used, equivalent to the value `((nil . (:level . 1))'."
  2008. :group 'org-refile
  2009. :type '(repeat
  2010. (cons
  2011. (choice :value org-agenda-files
  2012. (const :tag "All agenda files" org-agenda-files)
  2013. (const :tag "Current buffer" nil)
  2014. (function) (variable) (file))
  2015. (choice :tag "Identify target headline by"
  2016. (cons :tag "Specific tag" (const :value :tag) (string))
  2017. (cons :tag "TODO keyword" (const :value :todo) (string))
  2018. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2019. (cons :tag "Level number" (const :value :level) (integer))
  2020. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2021. (defcustom org-refile-target-verify-function nil
  2022. "Function to verify if the headline at point should be a refile target.
  2023. The function will be called without arguments, with point at the
  2024. beginning of the headline. It should return t and leave point
  2025. where it is if the headline is a valid target for refiling.
  2026. If the target should not be selected, the function must return nil.
  2027. In addition to this, it may move point to a place from where the search
  2028. should be continued. For example, the function may decide that the entire
  2029. subtree of the current entry should be excluded and move point to the end
  2030. of the subtree."
  2031. :group 'org-refile
  2032. :type '(choice
  2033. (const nil)
  2034. (function)))
  2035. (defcustom org-refile-use-cache nil
  2036. "Non-nil means cache refile targets to speed up the process.
  2037. The cache for a particular file will be updated automatically when
  2038. the buffer has been killed, or when any of the marker used for flagging
  2039. refile targets no longer points at a live buffer.
  2040. If you have added new entries to a buffer that might themselves be targets,
  2041. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  2042. find that easier, `C-u C-u C-u C-c C-w'."
  2043. :group 'org-refile
  2044. :version "24.1"
  2045. :type 'boolean)
  2046. (defcustom org-refile-use-outline-path nil
  2047. "Non-nil means provide refile targets as paths.
  2048. So a level 3 headline will be available as level1/level2/level3.
  2049. When the value is `file', also include the file name (without directory)
  2050. into the path. In this case, you can also stop the completion after
  2051. the file name, to get entries inserted as top level in the file.
  2052. When `full-file-path', include the full file path."
  2053. :group 'org-refile
  2054. :type '(choice
  2055. (const :tag "Not" nil)
  2056. (const :tag "Yes" t)
  2057. (const :tag "Start with file name" file)
  2058. (const :tag "Start with full file path" full-file-path)))
  2059. (defcustom org-outline-path-complete-in-steps t
  2060. "Non-nil means complete the outline path in hierarchical steps.
  2061. When Org-mode uses the refile interface to select an outline path
  2062. \(see variable `org-refile-use-outline-path'), the completion of
  2063. the path can be done is a single go, or if can be done in steps down
  2064. the headline hierarchy. Going in steps is probably the best if you
  2065. do not use a special completion package like `ido' or `icicles'.
  2066. However, when using these packages, going in one step can be very
  2067. fast, while still showing the whole path to the entry."
  2068. :group 'org-refile
  2069. :type 'boolean)
  2070. (defcustom org-refile-allow-creating-parent-nodes nil
  2071. "Non-nil means allow to create new nodes as refile targets.
  2072. New nodes are then created by adding \"/new node name\" to the completion
  2073. of an existing node. When the value of this variable is `confirm',
  2074. new node creation must be confirmed by the user (recommended).
  2075. When nil, the completion must match an existing entry.
  2076. Note that, if the new heading is not seen by the criteria
  2077. listed in `org-refile-targets', multiple instances of the same
  2078. heading would be created by trying again to file under the new
  2079. heading."
  2080. :group 'org-refile
  2081. :type '(choice
  2082. (const :tag "Never" nil)
  2083. (const :tag "Always" t)
  2084. (const :tag "Prompt for confirmation" confirm)))
  2085. (defcustom org-refile-active-region-within-subtree nil
  2086. "Non-nil means also refile active region within a subtree.
  2087. By default `org-refile' doesn't allow refiling regions if they
  2088. don't contain a set of subtrees, but it might be convenient to
  2089. do so sometimes: in that case, the first line of the region is
  2090. converted to a headline before refiling."
  2091. :group 'org-refile
  2092. :version "24.1"
  2093. :type 'boolean)
  2094. (defgroup org-todo nil
  2095. "Options concerning TODO items in Org-mode."
  2096. :tag "Org TODO"
  2097. :group 'org)
  2098. (defgroup org-progress nil
  2099. "Options concerning Progress logging in Org-mode."
  2100. :tag "Org Progress"
  2101. :group 'org-time)
  2102. (defvar org-todo-interpretation-widgets
  2103. '((:tag "Sequence (cycling hits every state)" sequence)
  2104. (:tag "Type (cycling directly to DONE)" type))
  2105. "The available interpretation symbols for customizing `org-todo-keywords'.
  2106. Interested libraries should add to this list.")
  2107. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2108. "List of TODO entry keyword sequences and their interpretation.
  2109. \\<org-mode-map>This is a list of sequences.
  2110. Each sequence starts with a symbol, either `sequence' or `type',
  2111. indicating if the keywords should be interpreted as a sequence of
  2112. action steps, or as different types of TODO items. The first
  2113. keywords are states requiring action - these states will select a headline
  2114. for inclusion into the global TODO list Org-mode produces. If one of
  2115. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2116. signify that no further action is necessary. If \"|\" is not found,
  2117. the last keyword is treated as the only DONE state of the sequence.
  2118. The command \\[org-todo] cycles an entry through these states, and one
  2119. additional state where no keyword is present. For details about this
  2120. cycling, see the manual.
  2121. TODO keywords and interpretation can also be set on a per-file basis with
  2122. the special #+SEQ_TODO and #+TYP_TODO lines.
  2123. Each keyword can optionally specify a character for fast state selection
  2124. \(in combination with the variable `org-use-fast-todo-selection')
  2125. and specifiers for state change logging, using the same syntax that
  2126. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2127. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2128. indicates to record a time stamp each time this state is selected.
  2129. Each keyword may also specify if a timestamp or a note should be
  2130. recorded when entering or leaving the state, by adding additional
  2131. characters in the parenthesis after the keyword. This looks like this:
  2132. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2133. record only the time of the state change. With X and Y being either
  2134. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2135. Y when leaving the state if and only if the *target* state does not
  2136. define X. You may omit any of the fast-selection key or X or /Y,
  2137. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2138. For backward compatibility, this variable may also be just a list
  2139. of keywords. In this case the interpretation (sequence or type) will be
  2140. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2141. :group 'org-todo
  2142. :group 'org-keywords
  2143. :type '(choice
  2144. (repeat :tag "Old syntax, just keywords"
  2145. (string :tag "Keyword"))
  2146. (repeat :tag "New syntax"
  2147. (cons
  2148. (choice
  2149. :tag "Interpretation"
  2150. ;;Quick and dirty way to see
  2151. ;;`org-todo-interpretations'. This takes the
  2152. ;;place of item arguments
  2153. :convert-widget
  2154. (lambda (widget)
  2155. (widget-put widget
  2156. :args (mapcar
  2157. #'(lambda (x)
  2158. (widget-convert
  2159. (cons 'const x)))
  2160. org-todo-interpretation-widgets))
  2161. widget))
  2162. (repeat
  2163. (string :tag "Keyword"))))))
  2164. (defvar org-todo-keywords-1 nil
  2165. "All TODO and DONE keywords active in a buffer.")
  2166. (make-variable-buffer-local 'org-todo-keywords-1)
  2167. (defvar org-todo-keywords-for-agenda nil)
  2168. (defvar org-done-keywords-for-agenda nil)
  2169. (defvar org-todo-keyword-alist-for-agenda nil)
  2170. (defvar org-tag-alist-for-agenda nil
  2171. "Alist of all tags from all agenda files.")
  2172. (defvar org-tag-groups-alist-for-agenda nil
  2173. "Alist of all groups tags from all current agenda files.")
  2174. (defvar org-tag-groups-alist nil)
  2175. (make-variable-buffer-local 'org-tag-groups-alist)
  2176. (defvar org-agenda-contributing-files nil)
  2177. (defvar org-not-done-keywords nil)
  2178. (make-variable-buffer-local 'org-not-done-keywords)
  2179. (defvar org-done-keywords nil)
  2180. (make-variable-buffer-local 'org-done-keywords)
  2181. (defvar org-todo-heads nil)
  2182. (make-variable-buffer-local 'org-todo-heads)
  2183. (defvar org-todo-sets nil)
  2184. (make-variable-buffer-local 'org-todo-sets)
  2185. (defvar org-todo-log-states nil)
  2186. (make-variable-buffer-local 'org-todo-log-states)
  2187. (defvar org-todo-kwd-alist nil)
  2188. (make-variable-buffer-local 'org-todo-kwd-alist)
  2189. (defvar org-todo-key-alist nil)
  2190. (make-variable-buffer-local 'org-todo-key-alist)
  2191. (defvar org-todo-key-trigger nil)
  2192. (make-variable-buffer-local 'org-todo-key-trigger)
  2193. (defcustom org-todo-interpretation 'sequence
  2194. "Controls how TODO keywords are interpreted.
  2195. This variable is in principle obsolete and is only used for
  2196. backward compatibility, if the interpretation of todo keywords is
  2197. not given already in `org-todo-keywords'. See that variable for
  2198. more information."
  2199. :group 'org-todo
  2200. :group 'org-keywords
  2201. :type '(choice (const sequence)
  2202. (const type)))
  2203. (defcustom org-use-fast-todo-selection t
  2204. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2205. This variable describes if and under what circumstances the cycling
  2206. mechanism for TODO keywords will be replaced by a single-key, direct
  2207. selection scheme.
  2208. When nil, fast selection is never used.
  2209. When the symbol `prefix', it will be used when `org-todo' is called
  2210. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2211. `C-u t' in an agenda buffer.
  2212. When t, fast selection is used by default. In this case, the prefix
  2213. argument forces cycling instead.
  2214. In all cases, the special interface is only used if access keys have
  2215. actually been assigned by the user, i.e. if keywords in the configuration
  2216. are followed by a letter in parenthesis, like TODO(t)."
  2217. :group 'org-todo
  2218. :type '(choice
  2219. (const :tag "Never" nil)
  2220. (const :tag "By default" t)
  2221. (const :tag "Only with C-u C-c C-t" prefix)))
  2222. (defcustom org-provide-todo-statistics t
  2223. "Non-nil means update todo statistics after insert and toggle.
  2224. ALL-HEADLINES means update todo statistics by including headlines
  2225. with no TODO keyword as well, counting them as not done.
  2226. A list of TODO keywords means the same, but skip keywords that are
  2227. not in this list.
  2228. When set to a list of two lists, the first list contains keywords
  2229. to consider as TODO keywords, the second list contains keywords
  2230. to consider as DONE keywords.
  2231. When this is set, todo statistics is updated in the parent of the
  2232. current entry each time a todo state is changed."
  2233. :group 'org-todo
  2234. :type '(choice
  2235. (const :tag "Yes, only for TODO entries" t)
  2236. (const :tag "Yes, including all entries" all-headlines)
  2237. (repeat :tag "Yes, for TODOs in this list"
  2238. (string :tag "TODO keyword"))
  2239. (list :tag "Yes, for TODOs and DONEs in these lists"
  2240. (repeat (string :tag "TODO keyword"))
  2241. (repeat (string :tag "DONE keyword")))
  2242. (other :tag "No TODO statistics" nil)))
  2243. (defcustom org-hierarchical-todo-statistics t
  2244. "Non-nil means TODO statistics covers just direct children.
  2245. When nil, all entries in the subtree are considered.
  2246. This has only an effect if `org-provide-todo-statistics' is set.
  2247. To set this to nil for only a single subtree, use a COOKIE_DATA
  2248. property and include the word \"recursive\" into the value."
  2249. :group 'org-todo
  2250. :type 'boolean)
  2251. (defcustom org-after-todo-state-change-hook nil
  2252. "Hook which is run after the state of a TODO item was changed.
  2253. The new state (a string with a TODO keyword, or nil) is available in the
  2254. Lisp variable `org-state'."
  2255. :group 'org-todo
  2256. :type 'hook)
  2257. (defvar org-blocker-hook nil
  2258. "Hook for functions that are allowed to block a state change.
  2259. Functions in this hook should not modify the buffer.
  2260. Each function gets as its single argument a property list,
  2261. see `org-trigger-hook' for more information about this list.
  2262. If any of the functions in this hook returns nil, the state change
  2263. is blocked.")
  2264. (defvar org-trigger-hook nil
  2265. "Hook for functions that are triggered by a state change.
  2266. Each function gets as its single argument a property list with at
  2267. least the following elements:
  2268. (:type type-of-change :position pos-at-entry-start
  2269. :from old-state :to new-state)
  2270. Depending on the type, more properties may be present.
  2271. This mechanism is currently implemented for:
  2272. TODO state changes
  2273. ------------------
  2274. :type todo-state-change
  2275. :from previous state (keyword as a string), or nil, or a symbol
  2276. 'todo' or 'done', to indicate the general type of state.
  2277. :to new state, like in :from")
  2278. (defcustom org-enforce-todo-dependencies nil
  2279. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2280. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2281. be blocked if any prior sibling is not yet done.
  2282. Finally, if the parent is blocked because of ordered siblings of its own,
  2283. the child will also be blocked."
  2284. :set (lambda (var val)
  2285. (set var val)
  2286. (if val
  2287. (add-hook 'org-blocker-hook
  2288. 'org-block-todo-from-children-or-siblings-or-parent)
  2289. (remove-hook 'org-blocker-hook
  2290. 'org-block-todo-from-children-or-siblings-or-parent)))
  2291. :group 'org-todo
  2292. :type 'boolean)
  2293. (defcustom org-enforce-todo-checkbox-dependencies nil
  2294. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2295. When this is nil, checkboxes have no influence on switching TODO states.
  2296. When non-nil, you first need to check off all check boxes before the TODO
  2297. entry can be switched to DONE.
  2298. This variable needs to be set before org.el is loaded, and you need to
  2299. restart Emacs after a change to make the change effective. The only way
  2300. to change is while Emacs is running is through the customize interface."
  2301. :set (lambda (var val)
  2302. (set var val)
  2303. (if val
  2304. (add-hook 'org-blocker-hook
  2305. 'org-block-todo-from-checkboxes)
  2306. (remove-hook 'org-blocker-hook
  2307. 'org-block-todo-from-checkboxes)))
  2308. :group 'org-todo
  2309. :type 'boolean)
  2310. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2311. "Non-nil means inserting a TODO heading is treated as state change.
  2312. So when the command \\[org-insert-todo-heading] is used, state change
  2313. logging will apply if appropriate. When nil, the new TODO item will
  2314. be inserted directly, and no logging will take place."
  2315. :group 'org-todo
  2316. :type 'boolean)
  2317. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2318. "Non-nil means switching TODO states with S-cursor counts as state change.
  2319. This is the default behavior. However, setting this to nil allows a
  2320. convenient way to select a TODO state and bypass any logging associated
  2321. with that."
  2322. :group 'org-todo
  2323. :type 'boolean)
  2324. (defcustom org-todo-state-tags-triggers nil
  2325. "Tag changes that should be triggered by TODO state changes.
  2326. This is a list. Each entry is
  2327. (state-change (tag . flag) .......)
  2328. State-change can be a string with a state, and empty string to indicate the
  2329. state that has no TODO keyword, or it can be one of the symbols `todo'
  2330. or `done', meaning any not-done or done state, respectively."
  2331. :group 'org-todo
  2332. :group 'org-tags
  2333. :type '(repeat
  2334. (cons (choice :tag "When changing to"
  2335. (const :tag "Not-done state" todo)
  2336. (const :tag "Done state" done)
  2337. (string :tag "State"))
  2338. (repeat
  2339. (cons :tag "Tag action"
  2340. (string :tag "Tag")
  2341. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2342. (defcustom org-log-done nil
  2343. "Information to record when a task moves to the DONE state.
  2344. Possible values are:
  2345. nil Don't add anything, just change the keyword
  2346. time Add a time stamp to the task
  2347. note Prompt for a note and add it with template `org-log-note-headings'
  2348. This option can also be set with on a per-file-basis with
  2349. #+STARTUP: nologdone
  2350. #+STARTUP: logdone
  2351. #+STARTUP: lognotedone
  2352. You can have local logging settings for a subtree by setting the LOGGING
  2353. property to one or more of these keywords."
  2354. :group 'org-todo
  2355. :group 'org-progress
  2356. :type '(choice
  2357. (const :tag "No logging" nil)
  2358. (const :tag "Record CLOSED timestamp" time)
  2359. (const :tag "Record CLOSED timestamp with note." note)))
  2360. ;; Normalize old uses of org-log-done.
  2361. (cond
  2362. ((eq org-log-done t) (setq org-log-done 'time))
  2363. ((and (listp org-log-done) (memq 'done org-log-done))
  2364. (setq org-log-done 'note)))
  2365. (defcustom org-log-reschedule nil
  2366. "Information to record when the scheduling date of a tasks is modified.
  2367. Possible values are:
  2368. nil Don't add anything, just change the date
  2369. time Add a time stamp to the task
  2370. note Prompt for a note and add it with template `org-log-note-headings'
  2371. This option can also be set with on a per-file-basis with
  2372. #+STARTUP: nologreschedule
  2373. #+STARTUP: logreschedule
  2374. #+STARTUP: lognotereschedule"
  2375. :group 'org-todo
  2376. :group 'org-progress
  2377. :type '(choice
  2378. (const :tag "No logging" nil)
  2379. (const :tag "Record timestamp" time)
  2380. (const :tag "Record timestamp with note." note)))
  2381. (defcustom org-log-redeadline nil
  2382. "Information to record when the deadline date of a tasks is modified.
  2383. Possible values are:
  2384. nil Don't add anything, just change the date
  2385. time Add a time stamp to the task
  2386. note Prompt for a note and add it with template `org-log-note-headings'
  2387. This option can also be set with on a per-file-basis with
  2388. #+STARTUP: nologredeadline
  2389. #+STARTUP: logredeadline
  2390. #+STARTUP: lognoteredeadline
  2391. You can have local logging settings for a subtree by setting the LOGGING
  2392. property to one or more of these keywords."
  2393. :group 'org-todo
  2394. :group 'org-progress
  2395. :type '(choice
  2396. (const :tag "No logging" nil)
  2397. (const :tag "Record timestamp" time)
  2398. (const :tag "Record timestamp with note." note)))
  2399. (defcustom org-log-note-clock-out nil
  2400. "Non-nil means record a note when clocking out of an item.
  2401. This can also be configured on a per-file basis by adding one of
  2402. the following lines anywhere in the buffer:
  2403. #+STARTUP: lognoteclock-out
  2404. #+STARTUP: nolognoteclock-out"
  2405. :group 'org-todo
  2406. :group 'org-progress
  2407. :type 'boolean)
  2408. (defcustom org-log-done-with-time t
  2409. "Non-nil means the CLOSED time stamp will contain date and time.
  2410. When nil, only the date will be recorded."
  2411. :group 'org-progress
  2412. :type 'boolean)
  2413. (defcustom org-log-note-headings
  2414. '((done . "CLOSING NOTE %t")
  2415. (state . "State %-12s from %-12S %t")
  2416. (note . "Note taken on %t")
  2417. (reschedule . "Rescheduled from %S on %t")
  2418. (delschedule . "Not scheduled, was %S on %t")
  2419. (redeadline . "New deadline from %S on %t")
  2420. (deldeadline . "Removed deadline, was %S on %t")
  2421. (refile . "Refiled on %t")
  2422. (clock-out . ""))
  2423. "Headings for notes added to entries.
  2424. The value is an alist, with the car being a symbol indicating the note
  2425. context, and the cdr is the heading to be used. The heading may also be the
  2426. empty string.
  2427. %t in the heading will be replaced by a time stamp.
  2428. %T will be an active time stamp instead the default inactive one
  2429. %d will be replaced by a short-format time stamp.
  2430. %D will be replaced by an active short-format time stamp.
  2431. %s will be replaced by the new TODO state, in double quotes.
  2432. %S will be replaced by the old TODO state, in double quotes.
  2433. %u will be replaced by the user name.
  2434. %U will be replaced by the full user name.
  2435. In fact, it is not a good idea to change the `state' entry, because
  2436. agenda log mode depends on the format of these entries."
  2437. :group 'org-todo
  2438. :group 'org-progress
  2439. :type '(list :greedy t
  2440. (cons (const :tag "Heading when closing an item" done) string)
  2441. (cons (const :tag
  2442. "Heading when changing todo state (todo sequence only)"
  2443. state) string)
  2444. (cons (const :tag "Heading when just taking a note" note) string)
  2445. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2446. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2447. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2448. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2449. (cons (const :tag "Heading when refiling" refile) string)
  2450. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2451. (unless (assq 'note org-log-note-headings)
  2452. (push '(note . "%t") org-log-note-headings))
  2453. (defcustom org-log-into-drawer nil
  2454. "Non-nil means insert state change notes and time stamps into a drawer.
  2455. When nil, state changes notes will be inserted after the headline and
  2456. any scheduling and clock lines, but not inside a drawer.
  2457. The value of this variable should be the name of the drawer to use.
  2458. LOGBOOK is proposed as the default drawer for this purpose, you can
  2459. also set this to a string to define the drawer of your choice.
  2460. A value of t is also allowed, representing \"LOGBOOK\".
  2461. A value of t or nil can also be set with on a per-file-basis with
  2462. #+STARTUP: logdrawer
  2463. #+STARTUP: nologdrawer
  2464. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2465. will be ignored.
  2466. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2467. a subtree."
  2468. :group 'org-todo
  2469. :group 'org-progress
  2470. :type '(choice
  2471. (const :tag "Not into a drawer" nil)
  2472. (const :tag "LOGBOOK" t)
  2473. (string :tag "Other")))
  2474. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2475. (defun org-log-into-drawer ()
  2476. "Return the value of `org-log-into-drawer', but let properties overrule.
  2477. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2478. used instead of the default value."
  2479. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2480. (cond
  2481. ((not p) org-log-into-drawer)
  2482. ((equal p "nil") nil)
  2483. ((equal p "t") "LOGBOOK")
  2484. (t p))))
  2485. (defcustom org-log-state-notes-insert-after-drawers nil
  2486. "Non-nil means insert state change notes after any drawers in entry.
  2487. Only the drawers that *immediately* follow the headline and the
  2488. deadline/scheduled line are skipped.
  2489. When nil, insert notes right after the heading and perhaps the line
  2490. with deadline/scheduling if present.
  2491. This variable will have no effect if `org-log-into-drawer' is
  2492. set."
  2493. :group 'org-todo
  2494. :group 'org-progress
  2495. :type 'boolean)
  2496. (defcustom org-log-states-order-reversed t
  2497. "Non-nil means the latest state note will be directly after heading.
  2498. When nil, the state change notes will be ordered according to time.
  2499. This option can also be set with on a per-file-basis with
  2500. #+STARTUP: logstatesreversed
  2501. #+STARTUP: nologstatesreversed"
  2502. :group 'org-todo
  2503. :group 'org-progress
  2504. :type 'boolean)
  2505. (defcustom org-todo-repeat-to-state nil
  2506. "The TODO state to which a repeater should return the repeating task.
  2507. By default this is the first task in a TODO sequence, or the previous state
  2508. in a TODO_TYP set. But you can specify another task here.
  2509. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2510. :group 'org-todo
  2511. :version "24.1"
  2512. :type '(choice (const :tag "Head of sequence" nil)
  2513. (string :tag "Specific state")))
  2514. (defcustom org-log-repeat 'time
  2515. "Non-nil means record moving through the DONE state when triggering repeat.
  2516. An auto-repeating task is immediately switched back to TODO when
  2517. marked DONE. If you are not logging state changes (by adding \"@\"
  2518. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2519. record a closing note, there will be no record of the task moving
  2520. through DONE. This variable forces taking a note anyway.
  2521. nil Don't force a record
  2522. time Record a time stamp
  2523. note Prompt for a note and add it with template `org-log-note-headings'
  2524. This option can also be set with on a per-file-basis with
  2525. #+STARTUP: nologrepeat
  2526. #+STARTUP: logrepeat
  2527. #+STARTUP: lognoterepeat
  2528. You can have local logging settings for a subtree by setting the LOGGING
  2529. property to one or more of these keywords."
  2530. :group 'org-todo
  2531. :group 'org-progress
  2532. :type '(choice
  2533. (const :tag "Don't force a record" nil)
  2534. (const :tag "Force recording the DONE state" time)
  2535. (const :tag "Force recording a note with the DONE state" note)))
  2536. (defgroup org-priorities nil
  2537. "Priorities in Org-mode."
  2538. :tag "Org Priorities"
  2539. :group 'org-todo)
  2540. (defcustom org-enable-priority-commands t
  2541. "Non-nil means priority commands are active.
  2542. When nil, these commands will be disabled, so that you never accidentally
  2543. set a priority."
  2544. :group 'org-priorities
  2545. :type 'boolean)
  2546. (defcustom org-highest-priority ?A
  2547. "The highest priority of TODO items. A character like ?A, ?B etc.
  2548. Must have a smaller ASCII number than `org-lowest-priority'."
  2549. :group 'org-priorities
  2550. :type 'character)
  2551. (defcustom org-lowest-priority ?C
  2552. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2553. Must have a larger ASCII number than `org-highest-priority'."
  2554. :group 'org-priorities
  2555. :type 'character)
  2556. (defcustom org-default-priority ?B
  2557. "The default priority of TODO items.
  2558. This is the priority an item gets if no explicit priority is given.
  2559. When starting to cycle on an empty priority the first step in the cycle
  2560. depends on `org-priority-start-cycle-with-default'. The resulting first
  2561. step priority must not exceed the range from `org-highest-priority' to
  2562. `org-lowest-priority' which means that `org-default-priority' has to be
  2563. in this range exclusive or inclusive the range boundaries. Else the
  2564. first step refuses to set the default and the second will fall back
  2565. to (depending on the command used) the highest or lowest priority."
  2566. :group 'org-priorities
  2567. :type 'character)
  2568. (defcustom org-priority-start-cycle-with-default t
  2569. "Non-nil means start with default priority when starting to cycle.
  2570. When this is nil, the first step in the cycle will be (depending on the
  2571. command used) one higher or lower than the default priority.
  2572. See also `org-default-priority'."
  2573. :group 'org-priorities
  2574. :type 'boolean)
  2575. (defcustom org-get-priority-function nil
  2576. "Function to extract the priority from a string.
  2577. The string is normally the headline. If this is nil Org computes the
  2578. priority from the priority cookie like [#A] in the headline. It returns
  2579. an integer, increasing by 1000 for each priority level.
  2580. The user can set a different function here, which should take a string
  2581. as an argument and return the numeric priority."
  2582. :group 'org-priorities
  2583. :version "24.1"
  2584. :type '(choice
  2585. (const nil)
  2586. (function)))
  2587. (defgroup org-time nil
  2588. "Options concerning time stamps and deadlines in Org-mode."
  2589. :tag "Org Time"
  2590. :group 'org)
  2591. (defcustom org-insert-labeled-timestamps-at-point nil
  2592. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2593. When nil, these labeled time stamps are forces into the second line of an
  2594. entry, just after the headline. When scheduling from the global TODO list,
  2595. the time stamp will always be forced into the second line."
  2596. :group 'org-time
  2597. :type 'boolean)
  2598. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2599. "Number of minutes to round time stamps to.
  2600. These are two values, the first applies when first creating a time stamp.
  2601. The second applies when changing it with the commands `S-up' and `S-down'.
  2602. When changing the time stamp, this means that it will change in steps
  2603. of N minutes, as given by the second value.
  2604. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2605. numbers should be factors of 60, so for example 5, 10, 15.
  2606. When this is larger than 1, you can still force an exact time stamp by using
  2607. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2608. and by using a prefix arg to `S-up/down' to specify the exact number
  2609. of minutes to shift."
  2610. :group 'org-time
  2611. :get #'(lambda (var) ; Make sure both elements are there
  2612. (if (integerp (default-value var))
  2613. (list (default-value var) 5)
  2614. (default-value var)))
  2615. :type '(list
  2616. (integer :tag "when inserting times")
  2617. (integer :tag "when modifying times")))
  2618. ;; Normalize old customizations of this variable.
  2619. (when (integerp org-time-stamp-rounding-minutes)
  2620. (setq org-time-stamp-rounding-minutes
  2621. (list org-time-stamp-rounding-minutes
  2622. org-time-stamp-rounding-minutes)))
  2623. (defcustom org-display-custom-times nil
  2624. "Non-nil means overlay custom formats over all time stamps.
  2625. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2626. To turn this on on a per-file basis, insert anywhere in the file:
  2627. #+STARTUP: customtime"
  2628. :group 'org-time
  2629. :set 'set-default
  2630. :type 'sexp)
  2631. (make-variable-buffer-local 'org-display-custom-times)
  2632. (defcustom org-time-stamp-custom-formats
  2633. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2634. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2635. These are overlaid over the default ISO format if the variable
  2636. `org-display-custom-times' is set. Time like %H:%M should be at the
  2637. end of the second format. The custom formats are also honored by export
  2638. commands, if custom time display is turned on at the time of export."
  2639. :group 'org-time
  2640. :type 'sexp)
  2641. (defun org-time-stamp-format (&optional long inactive)
  2642. "Get the right format for a time string."
  2643. (let ((f (if long (cdr org-time-stamp-formats)
  2644. (car org-time-stamp-formats))))
  2645. (if inactive
  2646. (concat "[" (substring f 1 -1) "]")
  2647. f)))
  2648. (defcustom org-time-clocksum-format
  2649. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2650. "The format string used when creating CLOCKSUM lines.
  2651. This is also used when Org mode generates a time duration.
  2652. The value can be a single format string containing two
  2653. %-sequences, which will be filled with the number of hours and
  2654. minutes in that order.
  2655. Alternatively, the value can be a plist associating any of the
  2656. keys :years, :months, :weeks, :days, :hours or :minutes with
  2657. format strings. The time duration is formatted using only the
  2658. time components that are needed and concatenating the results.
  2659. If a time unit in absent, it falls back to the next smallest
  2660. unit.
  2661. The keys :require-years, :require-months, :require-days,
  2662. :require-weeks, :require-hours, :require-minutes are also
  2663. meaningful. A non-nil value for these keys indicates that the
  2664. corresponding time component should always be included, even if
  2665. its value is 0.
  2666. For example,
  2667. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2668. :require-minutes t)
  2669. means durations longer than a day will be expressed in days,
  2670. hours and minutes, and durations less than a day will always be
  2671. expressed in hours and minutes (even for durations less than an
  2672. hour).
  2673. The value
  2674. \(:days \"%dd\" :minutes \"%dm\")
  2675. means durations longer than a day will be expressed in days and
  2676. minutes, and durations less than a day will be expressed entirely
  2677. in minutes (even for durations longer than an hour)."
  2678. :group 'org-time
  2679. :group 'org-clock
  2680. :version "24.4"
  2681. :package-version '(Org . "8.0")
  2682. :type '(choice (string :tag "Format string")
  2683. (set :tag "Plist"
  2684. (group :inline t (const :tag "Years" :years)
  2685. (string :tag "Format string"))
  2686. (group :inline t
  2687. (const :tag "Always show years" :require-years)
  2688. (const t))
  2689. (group :inline t (const :tag "Months" :months)
  2690. (string :tag "Format string"))
  2691. (group :inline t
  2692. (const :tag "Always show months" :require-months)
  2693. (const t))
  2694. (group :inline t (const :tag "Weeks" :weeks)
  2695. (string :tag "Format string"))
  2696. (group :inline t
  2697. (const :tag "Always show weeks" :require-weeks)
  2698. (const t))
  2699. (group :inline t (const :tag "Days" :days)
  2700. (string :tag "Format string"))
  2701. (group :inline t
  2702. (const :tag "Always show days" :require-days)
  2703. (const t))
  2704. (group :inline t (const :tag "Hours" :hours)
  2705. (string :tag "Format string"))
  2706. (group :inline t
  2707. (const :tag "Always show hours" :require-hours)
  2708. (const t))
  2709. (group :inline t (const :tag "Minutes" :minutes)
  2710. (string :tag "Format string"))
  2711. (group :inline t
  2712. (const :tag "Always show minutes" :require-minutes)
  2713. (const t)))))
  2714. (defcustom org-time-clocksum-use-fractional nil
  2715. "When non-nil, \\[org-clock-display] uses fractional times.
  2716. See `org-time-clocksum-format' for more on time clock formats."
  2717. :group 'org-time
  2718. :group 'org-clock
  2719. :version "24.3"
  2720. :type 'boolean)
  2721. (defcustom org-time-clocksum-use-effort-durations nil
  2722. "When non-nil, \\[org-clock-display] uses effort durations.
  2723. E.g. by default, one day is considered to be a 8 hours effort,
  2724. so a task that has been clocked for 16 hours will be displayed
  2725. as during 2 days in the clock display or in the clocktable.
  2726. See `org-effort-durations' on how to set effort durations
  2727. and `org-time-clocksum-format' for more on time clock formats."
  2728. :group 'org-time
  2729. :group 'org-clock
  2730. :version "24.4"
  2731. :package-version '(Org . "8.0")
  2732. :type 'boolean)
  2733. (defcustom org-time-clocksum-fractional-format "%.2f"
  2734. "The format string used when creating CLOCKSUM lines,
  2735. or when Org mode generates a time duration, if
  2736. `org-time-clocksum-use-fractional' is enabled.
  2737. The value can be a single format string containing one
  2738. %-sequence, which will be filled with the number of hours as
  2739. a float.
  2740. Alternatively, the value can be a plist associating any of the
  2741. keys :years, :months, :weeks, :days, :hours or :minutes with
  2742. a format string. The time duration is formatted using the
  2743. largest time unit which gives a non-zero integer part. If all
  2744. specified formats have zero integer part, the smallest time unit
  2745. is used."
  2746. :group 'org-time
  2747. :type '(choice (string :tag "Format string")
  2748. (set (group :inline t (const :tag "Years" :years)
  2749. (string :tag "Format string"))
  2750. (group :inline t (const :tag "Months" :months)
  2751. (string :tag "Format string"))
  2752. (group :inline t (const :tag "Weeks" :weeks)
  2753. (string :tag "Format string"))
  2754. (group :inline t (const :tag "Days" :days)
  2755. (string :tag "Format string"))
  2756. (group :inline t (const :tag "Hours" :hours)
  2757. (string :tag "Format string"))
  2758. (group :inline t (const :tag "Minutes" :minutes)
  2759. (string :tag "Format string")))))
  2760. (defcustom org-deadline-warning-days 14
  2761. "Number of days before expiration during which a deadline becomes active.
  2762. This variable governs the display in sparse trees and in the agenda.
  2763. When 0 or negative, it means use this number (the absolute value of it)
  2764. even if a deadline has a different individual lead time specified.
  2765. Custom commands can set this variable in the options section."
  2766. :group 'org-time
  2767. :group 'org-agenda-daily/weekly
  2768. :type 'integer)
  2769. (defcustom org-scheduled-delay-days 0
  2770. "Number of days before a scheduled item becomes active.
  2771. This variable governs the display in sparse trees and in the agenda.
  2772. The default value (i.e. 0) means: don't delay scheduled item.
  2773. When negative, it means use this number (the absolute value of it)
  2774. even if a scheduled item has a different individual delay time
  2775. specified.
  2776. Custom commands can set this variable in the options section."
  2777. :group 'org-time
  2778. :group 'org-agenda-daily/weekly
  2779. :version "24.4"
  2780. :package-version '(Org . "8.0")
  2781. :type 'integer)
  2782. (defcustom org-read-date-prefer-future t
  2783. "Non-nil means assume future for incomplete date input from user.
  2784. This affects the following situations:
  2785. 1. The user gives a month but not a year.
  2786. For example, if it is April and you enter \"feb 2\", this will be read
  2787. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2788. 2. The user gives a day, but no month.
  2789. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2790. read this as the third of *next* month. However, if you enter \"17\",
  2791. it will be considered as *this* month.
  2792. If you set this variable to the symbol `time', then also the following
  2793. will work:
  2794. 3. If the user gives a time.
  2795. If the time is before now, it will be interpreted as tomorrow.
  2796. Currently none of this works for ISO week specifications.
  2797. When this option is nil, the current day, month and year will always be
  2798. used as defaults.
  2799. See also `org-agenda-jump-prefer-future'."
  2800. :group 'org-time
  2801. :type '(choice
  2802. (const :tag "Never" nil)
  2803. (const :tag "Check month and day" t)
  2804. (const :tag "Check month, day, and time" time)))
  2805. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2806. "Should the agenda jump command prefer the future for incomplete dates?
  2807. The default is to do the same as configured in `org-read-date-prefer-future'.
  2808. But you can also set a deviating value here.
  2809. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2810. :group 'org-agenda
  2811. :group 'org-time
  2812. :version "24.1"
  2813. :type '(choice
  2814. (const :tag "Use org-read-date-prefer-future"
  2815. org-read-date-prefer-future)
  2816. (const :tag "Never" nil)
  2817. (const :tag "Always" t)))
  2818. (defcustom org-read-date-force-compatible-dates t
  2819. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2820. Depending on the system Emacs is running on, certain dates cannot
  2821. be represented with the type used internally to represent time.
  2822. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2823. correctly. Some systems allow for earlier dates, some for later,
  2824. some for both. One way to find out it to insert any date into an
  2825. Org buffer, putting the cursor on the year and hitting S-up and
  2826. S-down to test the range.
  2827. When this variable is set to t, the date/time prompt will not let
  2828. you specify dates outside the 1970-2037 range, so it is certain that
  2829. these dates will work in whatever version of Emacs you are
  2830. running, and also that you can move a file from one Emacs implementation
  2831. to another. WHenever Org is forcing the year for you, it will display
  2832. a message and beep.
  2833. When this variable is nil, Org will check if the date is
  2834. representable in the specific Emacs implementation you are using.
  2835. If not, it will force a year, usually the current year, and beep
  2836. to remind you. Currently this setting is not recommended because
  2837. the likelihood that you will open your Org files in an Emacs that
  2838. has limited date range is not negligible.
  2839. A workaround for this problem is to use diary sexp dates for time
  2840. stamps outside of this range."
  2841. :group 'org-time
  2842. :version "24.1"
  2843. :type 'boolean)
  2844. (defcustom org-read-date-display-live t
  2845. "Non-nil means display current interpretation of date prompt live.
  2846. This display will be in an overlay, in the minibuffer."
  2847. :group 'org-time
  2848. :type 'boolean)
  2849. (defcustom org-read-date-popup-calendar t
  2850. "Non-nil means pop up a calendar when prompting for a date.
  2851. In the calendar, the date can be selected with mouse-1. However, the
  2852. minibuffer will also be active, and you can simply enter the date as well.
  2853. When nil, only the minibuffer will be available."
  2854. :group 'org-time
  2855. :type 'boolean)
  2856. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2857. 'org-read-date-popup-calendar)
  2858. (make-obsolete-variable
  2859. 'org-read-date-minibuffer-setup-hook
  2860. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2861. (defcustom org-read-date-minibuffer-setup-hook nil
  2862. "Hook to be used to set up keys for the date/time interface.
  2863. Add key definitions to `minibuffer-local-map', which will be a
  2864. temporary copy.
  2865. WARNING: This option is obsolete, you should use
  2866. `org-read-date-minibuffer-local-map' to set up keys."
  2867. :group 'org-time
  2868. :type 'hook)
  2869. (defcustom org-extend-today-until 0
  2870. "The hour when your day really ends. Must be an integer.
  2871. This has influence for the following applications:
  2872. - When switching the agenda to \"today\". It it is still earlier than
  2873. the time given here, the day recognized as TODAY is actually yesterday.
  2874. - When a date is read from the user and it is still before the time given
  2875. here, the current date and time will be assumed to be yesterday, 23:59.
  2876. Also, timestamps inserted in capture templates follow this rule.
  2877. IMPORTANT: This is a feature whose implementation is and likely will
  2878. remain incomplete. Really, it is only here because past midnight seems to
  2879. be the favorite working time of John Wiegley :-)"
  2880. :group 'org-time
  2881. :type 'integer)
  2882. (defcustom org-use-effective-time nil
  2883. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2884. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2885. \"effective time\" of any timestamps between midnight and 8am will be
  2886. 23:59 of the previous day."
  2887. :group 'org-time
  2888. :version "24.1"
  2889. :type 'boolean)
  2890. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2891. "When non-nil, use the last clock out time for `org-todo'.
  2892. Note that this option has precedence over the combined use of
  2893. `org-use-effective-time' and `org-extend-today-until'."
  2894. :group 'org-time
  2895. :version "24.4"
  2896. :package-version '(Org . "8.0")
  2897. :type 'boolean)
  2898. (defcustom org-edit-timestamp-down-means-later nil
  2899. "Non-nil means S-down will increase the time in a time stamp.
  2900. When nil, S-up will increase."
  2901. :group 'org-time
  2902. :type 'boolean)
  2903. (defcustom org-calendar-follow-timestamp-change t
  2904. "Non-nil means make the calendar window follow timestamp changes.
  2905. When a timestamp is modified and the calendar window is visible, it will be
  2906. moved to the new date."
  2907. :group 'org-time
  2908. :type 'boolean)
  2909. (defgroup org-tags nil
  2910. "Options concerning tags in Org-mode."
  2911. :tag "Org Tags"
  2912. :group 'org)
  2913. (defcustom org-tag-alist nil
  2914. "List of tags allowed in Org-mode files.
  2915. When this list is nil, Org-mode will base TAG input on what is already in the
  2916. buffer.
  2917. The value of this variable is an alist, the car of each entry must be a
  2918. keyword as a string, the cdr may be a character that is used to select
  2919. that tag through the fast-tag-selection interface.
  2920. See the manual for details."
  2921. :group 'org-tags
  2922. :type '(repeat
  2923. (choice
  2924. (cons (string :tag "Tag name")
  2925. (character :tag "Access char"))
  2926. (list :tag "Start radio group"
  2927. (const :startgroup)
  2928. (option (string :tag "Group description")))
  2929. (list :tag "Group tags delimiter"
  2930. (const :grouptags))
  2931. (list :tag "End radio group"
  2932. (const :endgroup)
  2933. (option (string :tag "Group description")))
  2934. (const :tag "New line" (:newline)))))
  2935. (defcustom org-tag-persistent-alist nil
  2936. "List of tags that will always appear in all Org-mode files.
  2937. This is in addition to any in buffer settings or customizations
  2938. of `org-tag-alist'.
  2939. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2940. The value of this variable is an alist, the car of each entry must be a
  2941. keyword as a string, the cdr may be a character that is used to select
  2942. that tag through the fast-tag-selection interface.
  2943. See the manual for details.
  2944. To disable these tags on a per-file basis, insert anywhere in the file:
  2945. #+STARTUP: noptag"
  2946. :group 'org-tags
  2947. :type '(repeat
  2948. (choice
  2949. (cons (string :tag "Tag name")
  2950. (character :tag "Access char"))
  2951. (const :tag "Start radio group" (:startgroup))
  2952. (const :tag "Group tags delimiter" (:grouptags))
  2953. (const :tag "End radio group" (:endgroup))
  2954. (const :tag "New line" (:newline)))))
  2955. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2956. "If non-nil, always offer completion for all tags of all agenda files.
  2957. Instead of customizing this variable directly, you might want to
  2958. set it locally for capture buffers, because there no list of
  2959. tags in that file can be created dynamically (there are none).
  2960. (add-hook 'org-capture-mode-hook
  2961. (lambda ()
  2962. (set (make-local-variable
  2963. 'org-complete-tags-always-offer-all-agenda-tags)
  2964. t)))"
  2965. :group 'org-tags
  2966. :version "24.1"
  2967. :type 'boolean)
  2968. (defvar org-file-tags nil
  2969. "List of tags that can be inherited by all entries in the file.
  2970. The tags will be inherited if the variable `org-use-tag-inheritance'
  2971. says they should be.
  2972. This variable is populated from #+FILETAGS lines.")
  2973. (defcustom org-use-fast-tag-selection 'auto
  2974. "Non-nil means use fast tag selection scheme.
  2975. This is a special interface to select and deselect tags with single keys.
  2976. When nil, fast selection is never used.
  2977. When the symbol `auto', fast selection is used if and only if selection
  2978. characters for tags have been configured, either through the variable
  2979. `org-tag-alist' or through a #+TAGS line in the buffer.
  2980. When t, fast selection is always used and selection keys are assigned
  2981. automatically if necessary."
  2982. :group 'org-tags
  2983. :type '(choice
  2984. (const :tag "Always" t)
  2985. (const :tag "Never" nil)
  2986. (const :tag "When selection characters are configured" auto)))
  2987. (defcustom org-fast-tag-selection-single-key nil
  2988. "Non-nil means fast tag selection exits after first change.
  2989. When nil, you have to press RET to exit it.
  2990. During fast tag selection, you can toggle this flag with `C-c'.
  2991. This variable can also have the value `expert'. In this case, the window
  2992. displaying the tags menu is not even shown, until you press C-c again."
  2993. :group 'org-tags
  2994. :type '(choice
  2995. (const :tag "No" nil)
  2996. (const :tag "Yes" t)
  2997. (const :tag "Expert" expert)))
  2998. (defvar org-fast-tag-selection-include-todo nil
  2999. "Non-nil means fast tags selection interface will also offer TODO states.
  3000. This is an undocumented feature, you should not rely on it.")
  3001. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3002. "The column to which tags should be indented in a headline.
  3003. If this number is positive, it specifies the column. If it is negative,
  3004. it means that the tags should be flushright to that column. For example,
  3005. -80 works well for a normal 80 character screen.
  3006. When 0, place tags directly after headline text, with only one space in
  3007. between."
  3008. :group 'org-tags
  3009. :type 'integer)
  3010. (defcustom org-auto-align-tags t
  3011. "Non-nil keeps tags aligned when modifying headlines.
  3012. Some operations (i.e. demoting) change the length of a headline and
  3013. therefore shift the tags around. With this option turned on, after
  3014. each such operation the tags are again aligned to `org-tags-column'."
  3015. :group 'org-tags
  3016. :type 'boolean)
  3017. (defcustom org-use-tag-inheritance t
  3018. "Non-nil means tags in levels apply also for sublevels.
  3019. When nil, only the tags directly given in a specific line apply there.
  3020. This may also be a list of tags that should be inherited, or a regexp that
  3021. matches tags that should be inherited. Additional control is possible
  3022. with the variable `org-tags-exclude-from-inheritance' which gives an
  3023. explicit list of tags to be excluded from inheritance, even if the value of
  3024. `org-use-tag-inheritance' would select it for inheritance.
  3025. If this option is t, a match early-on in a tree can lead to a large
  3026. number of matches in the subtree when constructing the agenda or creating
  3027. a sparse tree. If you only want to see the first match in a tree during
  3028. a search, check out the variable `org-tags-match-list-sublevels'."
  3029. :group 'org-tags
  3030. :type '(choice
  3031. (const :tag "Not" nil)
  3032. (const :tag "Always" t)
  3033. (repeat :tag "Specific tags" (string :tag "Tag"))
  3034. (regexp :tag "Tags matched by regexp")))
  3035. (defcustom org-tags-exclude-from-inheritance nil
  3036. "List of tags that should never be inherited.
  3037. This is a way to exclude a few tags from inheritance. For way to do
  3038. the opposite, to actively allow inheritance for selected tags,
  3039. see the variable `org-use-tag-inheritance'."
  3040. :group 'org-tags
  3041. :type '(repeat (string :tag "Tag")))
  3042. (defun org-tag-inherit-p (tag)
  3043. "Check if TAG is one that should be inherited."
  3044. (cond
  3045. ((member tag org-tags-exclude-from-inheritance) nil)
  3046. ((eq org-use-tag-inheritance t) t)
  3047. ((not org-use-tag-inheritance) nil)
  3048. ((stringp org-use-tag-inheritance)
  3049. (string-match org-use-tag-inheritance tag))
  3050. ((listp org-use-tag-inheritance)
  3051. (member tag org-use-tag-inheritance))
  3052. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3053. (defcustom org-tags-match-list-sublevels t
  3054. "Non-nil means list also sublevels of headlines matching a search.
  3055. This variable applies to tags/property searches, and also to stuck
  3056. projects because this search is based on a tags match as well.
  3057. When set to the symbol `indented', sublevels are indented with
  3058. leading dots.
  3059. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3060. the sublevels of a headline matching a tag search often also match
  3061. the same search. Listing all of them can create very long lists.
  3062. Setting this variable to nil causes subtrees of a match to be skipped.
  3063. This variable is semi-obsolete and probably should always be true. It
  3064. is better to limit inheritance to certain tags using the variables
  3065. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3066. :group 'org-tags
  3067. :type '(choice
  3068. (const :tag "No, don't list them" nil)
  3069. (const :tag "Yes, do list them" t)
  3070. (const :tag "List them, indented with leading dots" indented)))
  3071. (defcustom org-tags-sort-function nil
  3072. "When set, tags are sorted using this function as a comparator."
  3073. :group 'org-tags
  3074. :type '(choice
  3075. (const :tag "No sorting" nil)
  3076. (const :tag "Alphabetical" string<)
  3077. (const :tag "Reverse alphabetical" string>)
  3078. (function :tag "Custom function" nil)))
  3079. (defvar org-tags-history nil
  3080. "History of minibuffer reads for tags.")
  3081. (defvar org-last-tags-completion-table nil
  3082. "The last used completion table for tags.")
  3083. (defvar org-after-tags-change-hook nil
  3084. "Hook that is run after the tags in a line have changed.")
  3085. (defgroup org-properties nil
  3086. "Options concerning properties in Org-mode."
  3087. :tag "Org Properties"
  3088. :group 'org)
  3089. (defcustom org-property-format "%-10s %s"
  3090. "How property key/value pairs should be formatted by `indent-line'.
  3091. When `indent-line' hits a property definition, it will format the line
  3092. according to this format, mainly to make sure that the values are
  3093. lined-up with respect to each other."
  3094. :group 'org-properties
  3095. :type 'string)
  3096. (defcustom org-properties-postprocess-alist nil
  3097. "Alist of properties and functions to adjust inserted values.
  3098. Elements of this alist must be of the form
  3099. ([string] [function])
  3100. where [string] must be a property name and [function] must be a
  3101. lambda expression: this lambda expression must take one argument,
  3102. the value to adjust, and return the new value as a string.
  3103. For example, this element will allow the property \"Remaining\"
  3104. to be updated wrt the relation between the \"Effort\" property
  3105. and the clock summary:
  3106. ((\"Remaining\" (lambda(value)
  3107. (let ((clocksum (org-clock-sum-current-item))
  3108. (effort (org-duration-string-to-minutes
  3109. (org-entry-get (point) \"Effort\"))))
  3110. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3111. :group 'org-properties
  3112. :version "24.1"
  3113. :type '(alist :key-type (string :tag "Property")
  3114. :value-type (function :tag "Function")))
  3115. (defcustom org-use-property-inheritance nil
  3116. "Non-nil means properties apply also for sublevels.
  3117. This setting is chiefly used during property searches. Turning it on can
  3118. cause significant overhead when doing a search, which is why it is not
  3119. on by default.
  3120. When nil, only the properties directly given in the current entry count.
  3121. When t, every property is inherited. The value may also be a list of
  3122. properties that should have inheritance, or a regular expression matching
  3123. properties that should be inherited.
  3124. However, note that some special properties use inheritance under special
  3125. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3126. and the properties ending in \"_ALL\" when they are used as descriptor
  3127. for valid values of a property.
  3128. Note for programmers:
  3129. When querying an entry with `org-entry-get', you can control if inheritance
  3130. should be used. By default, `org-entry-get' looks only at the local
  3131. properties. You can request inheritance by setting the inherit argument
  3132. to t (to force inheritance) or to `selective' (to respect the setting
  3133. in this variable)."
  3134. :group 'org-properties
  3135. :type '(choice
  3136. (const :tag "Not" nil)
  3137. (const :tag "Always" t)
  3138. (repeat :tag "Specific properties" (string :tag "Property"))
  3139. (regexp :tag "Properties matched by regexp")))
  3140. (defun org-property-inherit-p (property)
  3141. "Check if PROPERTY is one that should be inherited."
  3142. (cond
  3143. ((eq org-use-property-inheritance t) t)
  3144. ((not org-use-property-inheritance) nil)
  3145. ((stringp org-use-property-inheritance)
  3146. (string-match org-use-property-inheritance property))
  3147. ((listp org-use-property-inheritance)
  3148. (member property org-use-property-inheritance))
  3149. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3150. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3151. "The default column format, if no other format has been defined.
  3152. This variable can be set on the per-file basis by inserting a line
  3153. #+COLUMNS: %25ITEM ....."
  3154. :group 'org-properties
  3155. :type 'string)
  3156. (defcustom org-columns-ellipses ".."
  3157. "The ellipses to be used when a field in column view is truncated.
  3158. When this is the empty string, as many characters as possible are shown,
  3159. but then there will be no visual indication that the field has been truncated.
  3160. When this is a string of length N, the last N characters of a truncated
  3161. field are replaced by this string. If the column is narrower than the
  3162. ellipses string, only part of the ellipses string will be shown."
  3163. :group 'org-properties
  3164. :type 'string)
  3165. (defcustom org-columns-modify-value-for-display-function nil
  3166. "Function that modifies values for display in column view.
  3167. For example, it can be used to cut out a certain part from a time stamp.
  3168. The function must take 2 arguments:
  3169. column-title The title of the column (*not* the property name)
  3170. value The value that should be modified.
  3171. The function should return the value that should be displayed,
  3172. or nil if the normal value should be used."
  3173. :group 'org-properties
  3174. :type '(choice (const nil) (function)))
  3175. (defconst org-global-properties-fixed
  3176. '(("VISIBILITY_ALL" . "folded children content all")
  3177. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3178. "List of property/value pairs that can be inherited by any entry.
  3179. These are fixed values, for the preset properties. The user variable
  3180. that can be used to add to this list is `org-global-properties'.
  3181. The entries in this list are cons cells where the car is a property
  3182. name and cdr is a string with the value. If the value represents
  3183. multiple items like an \"_ALL\" property, separate the items by
  3184. spaces.")
  3185. (defcustom org-global-properties nil
  3186. "List of property/value pairs that can be inherited by any entry.
  3187. This list will be combined with the constant `org-global-properties-fixed'.
  3188. The entries in this list are cons cells where the car is a property
  3189. name and cdr is a string with the value.
  3190. You can set buffer-local values for the same purpose in the variable
  3191. `org-file-properties' this by adding lines like
  3192. #+PROPERTY: NAME VALUE"
  3193. :group 'org-properties
  3194. :type '(repeat
  3195. (cons (string :tag "Property")
  3196. (string :tag "Value"))))
  3197. (defvar org-file-properties nil
  3198. "List of property/value pairs that can be inherited by any entry.
  3199. Valid for the current buffer.
  3200. This variable is populated from #+PROPERTY lines.")
  3201. (make-variable-buffer-local 'org-file-properties)
  3202. (defgroup org-agenda nil
  3203. "Options concerning agenda views in Org-mode."
  3204. :tag "Org Agenda"
  3205. :group 'org)
  3206. (defvar org-category nil
  3207. "Variable used by org files to set a category for agenda display.
  3208. Such files should use a file variable to set it, for example
  3209. # -*- mode: org; org-category: \"ELisp\"
  3210. or contain a special line
  3211. #+CATEGORY: ELisp
  3212. If the file does not specify a category, then file's base name
  3213. is used instead.")
  3214. (make-variable-buffer-local 'org-category)
  3215. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3216. (defcustom org-agenda-files nil
  3217. "The files to be used for agenda display.
  3218. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3219. \\[org-remove-file]. You can also use customize to edit the list.
  3220. If an entry is a directory, all files in that directory that are matched by
  3221. `org-agenda-file-regexp' will be part of the file list.
  3222. If the value of the variable is not a list but a single file name, then
  3223. the list of agenda files is actually stored and maintained in that file, one
  3224. agenda file per line. In this file paths can be given relative to
  3225. `org-directory'. Tilde expansion and environment variable substitution
  3226. are also made."
  3227. :group 'org-agenda
  3228. :type '(choice
  3229. (repeat :tag "List of files and directories" file)
  3230. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3231. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3232. "Regular expression to match files for `org-agenda-files'.
  3233. If any element in the list in that variable contains a directory instead
  3234. of a normal file, all files in that directory that are matched by this
  3235. regular expression will be included."
  3236. :group 'org-agenda
  3237. :type 'regexp)
  3238. (defcustom org-agenda-text-search-extra-files nil
  3239. "List of extra files to be searched by text search commands.
  3240. These files will be searched in addition to the agenda files by the
  3241. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3242. Note that these files will only be searched for text search commands,
  3243. not for the other agenda views like todo lists, tag searches or the weekly
  3244. agenda. This variable is intended to list notes and possibly archive files
  3245. that should also be searched by these two commands.
  3246. In fact, if the first element in the list is the symbol `agenda-archives',
  3247. then all archive files of all agenda files will be added to the search
  3248. scope."
  3249. :group 'org-agenda
  3250. :type '(set :greedy t
  3251. (const :tag "Agenda Archives" agenda-archives)
  3252. (repeat :inline t (file))))
  3253. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3254. 'org-agenda-text-search-extra-files)
  3255. (defcustom org-agenda-skip-unavailable-files nil
  3256. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3257. A nil value means to remove them, after a query, from the list."
  3258. :group 'org-agenda
  3259. :type 'boolean)
  3260. (defcustom org-calendar-to-agenda-key [?c]
  3261. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3262. The command `org-calendar-goto-agenda' will be bound to this key. The
  3263. default is the character `c' because then `c' can be used to switch back and
  3264. forth between agenda and calendar."
  3265. :group 'org-agenda
  3266. :type 'sexp)
  3267. (defcustom org-calendar-insert-diary-entry-key [?i]
  3268. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3269. This option is irrelevant until `org-agenda-diary-file' has been configured
  3270. to point to an Org-mode file. When that is the case, the command
  3271. `org-agenda-diary-entry' will be bound to the key given here, by default
  3272. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3273. if you want to continue doing this, you need to change this to a different
  3274. key."
  3275. :group 'org-agenda
  3276. :type 'sexp)
  3277. (defcustom org-agenda-diary-file 'diary-file
  3278. "File to which to add new entries with the `i' key in agenda and calendar.
  3279. When this is the symbol `diary-file', the functionality in the Emacs
  3280. calendar will be used to add entries to the `diary-file'. But when this
  3281. points to a file, `org-agenda-diary-entry' will be used instead."
  3282. :group 'org-agenda
  3283. :type '(choice
  3284. (const :tag "The standard Emacs diary file" diary-file)
  3285. (file :tag "Special Org file diary entries")))
  3286. (eval-after-load "calendar"
  3287. '(progn
  3288. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3289. 'org-calendar-goto-agenda)
  3290. (add-hook 'calendar-mode-hook
  3291. (lambda ()
  3292. (unless (eq org-agenda-diary-file 'diary-file)
  3293. (define-key calendar-mode-map
  3294. org-calendar-insert-diary-entry-key
  3295. 'org-agenda-diary-entry))))))
  3296. (defgroup org-latex nil
  3297. "Options for embedding LaTeX code into Org-mode."
  3298. :tag "Org LaTeX"
  3299. :group 'org)
  3300. (defcustom org-format-latex-options
  3301. '(:foreground default :background default :scale 1.0
  3302. :html-foreground "Black" :html-background "Transparent"
  3303. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3304. "Options for creating images from LaTeX fragments.
  3305. This is a property list with the following properties:
  3306. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3307. `default' means use the foreground of the default face.
  3308. `auto' means use the foreground from the text face.
  3309. :background the background color, or \"Transparent\".
  3310. `default' means use the background of the default face.
  3311. `auto' means use the background from the text face.
  3312. :scale a scaling factor for the size of the images, to get more pixels
  3313. :html-foreground, :html-background, :html-scale
  3314. the same numbers for HTML export.
  3315. :matchers a list indicating which matchers should be used to
  3316. find LaTeX fragments. Valid members of this list are:
  3317. \"begin\" find environments
  3318. \"$1\" find single characters surrounded by $.$
  3319. \"$\" find math expressions surrounded by $...$
  3320. \"$$\" find math expressions surrounded by $$....$$
  3321. \"\\(\" find math expressions surrounded by \\(...\\)
  3322. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3323. :group 'org-latex
  3324. :type 'plist)
  3325. (defcustom org-format-latex-signal-error t
  3326. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3327. When nil, just push out a message."
  3328. :group 'org-latex
  3329. :version "24.1"
  3330. :type 'boolean)
  3331. (defcustom org-latex-to-mathml-jar-file nil
  3332. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3333. Use this to specify additional executable file say a jar file.
  3334. When using MathToWeb as the converter, specify the full-path to
  3335. your mathtoweb.jar file."
  3336. :group 'org-latex
  3337. :version "24.1"
  3338. :type '(choice
  3339. (const :tag "None" nil)
  3340. (file :tag "JAR file" :must-match t)))
  3341. (defcustom org-latex-to-mathml-convert-command nil
  3342. "Command to convert LaTeX fragments to MathML.
  3343. Replace format-specifiers in the command as noted below and use
  3344. `shell-command' to convert LaTeX to MathML.
  3345. %j: Executable file in fully expanded form as specified by
  3346. `org-latex-to-mathml-jar-file'.
  3347. %I: Input LaTeX file in fully expanded form
  3348. %o: Output MathML file
  3349. This command is used by `org-create-math-formula'.
  3350. When using MathToWeb as the converter, set this to
  3351. \"java -jar %j -unicode -force -df %o %I\"."
  3352. :group 'org-latex
  3353. :version "24.1"
  3354. :type '(choice
  3355. (const :tag "None" nil)
  3356. (string :tag "\nShell command")))
  3357. (defcustom org-latex-create-formula-image-program 'dvipng
  3358. "Program to convert LaTeX fragments with.
  3359. dvipng Process the LaTeX fragments to dvi file, then convert
  3360. dvi files to png files using dvipng.
  3361. This will also include processing of non-math environments.
  3362. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3363. to convert pdf files to png files"
  3364. :group 'org-latex
  3365. :version "24.1"
  3366. :type '(choice
  3367. (const :tag "dvipng" dvipng)
  3368. (const :tag "imagemagick" imagemagick)))
  3369. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3370. "Path to store latex preview images.
  3371. A relative path here creates many directories relative to the
  3372. processed org files paths. An absolute path puts all preview
  3373. images at the same place."
  3374. :group 'org-latex
  3375. :version "24.3"
  3376. :type 'string)
  3377. (defun org-format-latex-mathml-available-p ()
  3378. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3379. (save-match-data
  3380. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3381. org-latex-to-mathml-convert-command)
  3382. (let ((executable (car (split-string
  3383. org-latex-to-mathml-convert-command))))
  3384. (when (executable-find executable)
  3385. (if (string-match
  3386. "%j" org-latex-to-mathml-convert-command)
  3387. (file-readable-p org-latex-to-mathml-jar-file)
  3388. t))))))
  3389. (defcustom org-format-latex-header "\\documentclass{article}
  3390. \\usepackage[usenames]{color}
  3391. \[PACKAGES]
  3392. \[DEFAULT-PACKAGES]
  3393. \\pagestyle{empty} % do not remove
  3394. % The settings below are copied from fullpage.sty
  3395. \\setlength{\\textwidth}{\\paperwidth}
  3396. \\addtolength{\\textwidth}{-3cm}
  3397. \\setlength{\\oddsidemargin}{1.5cm}
  3398. \\addtolength{\\oddsidemargin}{-2.54cm}
  3399. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3400. \\setlength{\\textheight}{\\paperheight}
  3401. \\addtolength{\\textheight}{-\\headheight}
  3402. \\addtolength{\\textheight}{-\\headsep}
  3403. \\addtolength{\\textheight}{-\\footskip}
  3404. \\addtolength{\\textheight}{-3cm}
  3405. \\setlength{\\topmargin}{1.5cm}
  3406. \\addtolength{\\topmargin}{-2.54cm}"
  3407. "The document header used for processing LaTeX fragments.
  3408. It is imperative that this header make sure that no page number
  3409. appears on the page. The package defined in the variables
  3410. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3411. will either replace the placeholder \"[PACKAGES]\" in this
  3412. header, or they will be appended."
  3413. :group 'org-latex
  3414. :type 'string)
  3415. (defun org-set-packages-alist (var val)
  3416. "Set the packages alist and make sure it has 3 elements per entry."
  3417. (set var (mapcar (lambda (x)
  3418. (if (and (consp x) (= (length x) 2))
  3419. (list (car x) (nth 1 x) t)
  3420. x))
  3421. val)))
  3422. (defun org-get-packages-alist (var)
  3423. "Get the packages alist and make sure it has 3 elements per entry."
  3424. (mapcar (lambda (x)
  3425. (if (and (consp x) (= (length x) 2))
  3426. (list (car x) (nth 1 x) t)
  3427. x))
  3428. (default-value var)))
  3429. (defcustom org-latex-default-packages-alist
  3430. '(("AUTO" "inputenc" t)
  3431. ("T1" "fontenc" t)
  3432. ("" "fixltx2e" nil)
  3433. ("" "graphicx" t)
  3434. ("" "longtable" nil)
  3435. ("" "float" nil)
  3436. ("" "wrapfig" nil)
  3437. ("" "rotating" nil)
  3438. ("normalem" "ulem" t)
  3439. ("" "amsmath" t)
  3440. ("" "textcomp" t)
  3441. ("" "marvosym" t)
  3442. ("" "wasysym" t)
  3443. ("" "amssymb" t)
  3444. ("" "hyperref" nil)
  3445. "\\tolerance=1000")
  3446. "Alist of default packages to be inserted in the header.
  3447. Change this only if one of the packages here causes an
  3448. incompatibility with another package you are using.
  3449. The packages in this list are needed by one part or another of
  3450. Org mode to function properly:
  3451. - inputenc, fontenc: for basic font and character selection
  3452. - fixltx2e: Important patches of LaTeX itself
  3453. - graphicx: for including images
  3454. - longtable: For multipage tables
  3455. - float, wrapfig: for figure placement
  3456. - rotating: for sideways figures and tables
  3457. - ulem: for underline and strike-through
  3458. - amsmath: for subscript and superscript and math environments
  3459. - textcomp, marvosymb, wasysym, amssymb: for various symbols used
  3460. for interpreting the entities in `org-entities'. You can skip
  3461. some of these packages if you don't use any of their symbols.
  3462. - hyperref: for cross references
  3463. Therefore you should not modify this variable unless you know
  3464. what you are doing. The one reason to change it anyway is that
  3465. you might be loading some other package that conflicts with one
  3466. of the default packages. Each element is either a cell or
  3467. a string.
  3468. A cell is of the format:
  3469. \( \"options\" \"package\" SNIPPET-FLAG).
  3470. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3471. when compiling LaTeX snippets into images for inclusion into
  3472. non-LaTeX output.
  3473. A string will be inserted as-is in the header of the document."
  3474. :group 'org-latex
  3475. :group 'org-export-latex
  3476. :set 'org-set-packages-alist
  3477. :get 'org-get-packages-alist
  3478. :version "24.1"
  3479. :type '(repeat
  3480. (choice
  3481. (list :tag "options/package pair"
  3482. (string :tag "options")
  3483. (string :tag "package")
  3484. (boolean :tag "Snippet"))
  3485. (string :tag "A line of LaTeX"))))
  3486. (defcustom org-latex-packages-alist nil
  3487. "Alist of packages to be inserted in every LaTeX header.
  3488. These will be inserted after `org-latex-default-packages-alist'.
  3489. Each element is either a cell or a string.
  3490. A cell is of the format:
  3491. \(\"options\" \"package\" SNIPPET-FLAG)
  3492. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3493. needed when turning LaTeX snippets into images for inclusion into
  3494. non-LaTeX output.
  3495. A string will be inserted as-is in the header of the document.
  3496. Make sure that you only list packages here which:
  3497. - you want in every file;
  3498. - do not conflict with the setup in `org-format-latex-header';
  3499. - do not conflict with the default packages in
  3500. `org-latex-default-packages-alist'."
  3501. :group 'org-latex
  3502. :group 'org-export-latex
  3503. :set 'org-set-packages-alist
  3504. :get 'org-get-packages-alist
  3505. :type '(repeat
  3506. (choice
  3507. (list :tag "options/package pair"
  3508. (string :tag "options")
  3509. (string :tag "package")
  3510. (boolean :tag "Snippet"))
  3511. (string :tag "A line of LaTeX"))))
  3512. (defgroup org-appearance nil
  3513. "Settings for Org-mode appearance."
  3514. :tag "Org Appearance"
  3515. :group 'org)
  3516. (defcustom org-level-color-stars-only nil
  3517. "Non-nil means fontify only the stars in each headline.
  3518. When nil, the entire headline is fontified.
  3519. Changing it requires restart of `font-lock-mode' to become effective
  3520. also in regions already fontified."
  3521. :group 'org-appearance
  3522. :type 'boolean)
  3523. (defcustom org-hide-leading-stars nil
  3524. "Non-nil means hide the first N-1 stars in a headline.
  3525. This works by using the face `org-hide' for these stars. This
  3526. face is white for a light background, and black for a dark
  3527. background. You may have to customize the face `org-hide' to
  3528. make this work.
  3529. Changing it requires restart of `font-lock-mode' to become effective
  3530. also in regions already fontified.
  3531. You may also set this on a per-file basis by adding one of the following
  3532. lines to the buffer:
  3533. #+STARTUP: hidestars
  3534. #+STARTUP: showstars"
  3535. :group 'org-appearance
  3536. :type 'boolean)
  3537. (defcustom org-hidden-keywords nil
  3538. "List of symbols corresponding to keywords to be hidden the org buffer.
  3539. For example, a value '(title) for this list will make the document's title
  3540. appear in the buffer without the initial #+TITLE: keyword."
  3541. :group 'org-appearance
  3542. :version "24.1"
  3543. :type '(set (const :tag "#+AUTHOR" author)
  3544. (const :tag "#+DATE" date)
  3545. (const :tag "#+EMAIL" email)
  3546. (const :tag "#+TITLE" title)))
  3547. (defcustom org-custom-properties nil
  3548. "List of properties (as strings) with a special meaning.
  3549. The default use of these custom properties is to let the user
  3550. hide them with `org-toggle-custom-properties-visibility'."
  3551. :group 'org-properties
  3552. :group 'org-appearance
  3553. :version "24.3"
  3554. :type '(repeat (string :tag "Property Name")))
  3555. (defcustom org-fontify-done-headline nil
  3556. "Non-nil means change the face of a headline if it is marked DONE.
  3557. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3558. When this is non-nil, the headline after the keyword is set to the
  3559. `org-headline-done' as an additional indication."
  3560. :group 'org-appearance
  3561. :type 'boolean)
  3562. (defcustom org-fontify-emphasized-text t
  3563. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3564. Changing this variable requires a restart of Emacs to take effect."
  3565. :group 'org-appearance
  3566. :type 'boolean)
  3567. (defcustom org-fontify-whole-heading-line nil
  3568. "Non-nil means fontify the whole line for headings.
  3569. This is useful when setting a background color for the
  3570. org-level-* faces."
  3571. :group 'org-appearance
  3572. :type 'boolean)
  3573. (defcustom org-highlight-latex-and-related nil
  3574. "Non-nil means highlight LaTeX related syntax in the buffer.
  3575. When non nil, the value should be a list containing any of the
  3576. following symbols:
  3577. `latex' Highlight LaTeX snippets and environments.
  3578. `script' Highlight subscript and superscript.
  3579. `entities' Highlight entities."
  3580. :group 'org-appearance
  3581. :version "24.4"
  3582. :package-version '(Org . "8.0")
  3583. :type '(choice
  3584. (const :tag "No highlighting" nil)
  3585. (set :greedy t :tag "Highlight"
  3586. (const :tag "LaTeX snippets and environments" latex)
  3587. (const :tag "Subscript and superscript" script)
  3588. (const :tag "Entities" entities))))
  3589. (defcustom org-hide-emphasis-markers nil
  3590. "Non-nil mean font-lock should hide the emphasis marker characters."
  3591. :group 'org-appearance
  3592. :type 'boolean)
  3593. (defcustom org-hide-macro-markers nil
  3594. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3595. :group 'org-appearance
  3596. :type 'boolean)
  3597. (defcustom org-pretty-entities nil
  3598. "Non-nil means show entities as UTF8 characters.
  3599. When nil, the \\name form remains in the buffer."
  3600. :group 'org-appearance
  3601. :version "24.1"
  3602. :type 'boolean)
  3603. (defcustom org-pretty-entities-include-sub-superscripts t
  3604. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3605. :group 'org-appearance
  3606. :version "24.1"
  3607. :type 'boolean)
  3608. (defvar org-emph-re nil
  3609. "Regular expression for matching emphasis.
  3610. After a match, the match groups contain these elements:
  3611. 0 The match of the full regular expression, including the characters
  3612. before and after the proper match
  3613. 1 The character before the proper match, or empty at beginning of line
  3614. 2 The proper match, including the leading and trailing markers
  3615. 3 The leading marker like * or /, indicating the type of highlighting
  3616. 4 The text between the emphasis markers, not including the markers
  3617. 5 The character after the match, empty at the end of a line")
  3618. (defvar org-verbatim-re nil
  3619. "Regular expression for matching verbatim text.")
  3620. (defvar org-emphasis-regexp-components) ; defined just below
  3621. (defvar org-emphasis-alist) ; defined just below
  3622. (defun org-set-emph-re (var val)
  3623. "Set variable and compute the emphasis regular expression."
  3624. (set var val)
  3625. (when (and (boundp 'org-emphasis-alist)
  3626. (boundp 'org-emphasis-regexp-components)
  3627. org-emphasis-alist org-emphasis-regexp-components)
  3628. (let* ((e org-emphasis-regexp-components)
  3629. (pre (car e))
  3630. (post (nth 1 e))
  3631. (border (nth 2 e))
  3632. (body (nth 3 e))
  3633. (nl (nth 4 e))
  3634. (body1 (concat body "*?"))
  3635. (markers (mapconcat 'car org-emphasis-alist ""))
  3636. (vmarkers (mapconcat
  3637. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3638. org-emphasis-alist "")))
  3639. ;; make sure special characters appear at the right position in the class
  3640. (if (string-match "\\^" markers)
  3641. (setq markers (concat (replace-match "" t t markers) "^")))
  3642. (if (string-match "-" markers)
  3643. (setq markers (concat (replace-match "" t t markers) "-")))
  3644. (if (string-match "\\^" vmarkers)
  3645. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3646. (if (string-match "-" vmarkers)
  3647. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3648. (if (> nl 0)
  3649. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3650. (int-to-string nl) "\\}")))
  3651. ;; Make the regexp
  3652. (setq org-emph-re
  3653. (concat "\\([" pre "]\\|^\\)"
  3654. "\\("
  3655. "\\([" markers "]\\)"
  3656. "\\("
  3657. "[^" border "]\\|"
  3658. "[^" border "]"
  3659. body1
  3660. "[^" border "]"
  3661. "\\)"
  3662. "\\3\\)"
  3663. "\\([" post "]\\|$\\)"))
  3664. (setq org-verbatim-re
  3665. (concat "\\([" pre "]\\|^\\)"
  3666. "\\("
  3667. "\\([" vmarkers "]\\)"
  3668. "\\("
  3669. "[^" border "]\\|"
  3670. "[^" border "]"
  3671. body1
  3672. "[^" border "]"
  3673. "\\)"
  3674. "\\3\\)"
  3675. "\\([" post "]\\|$\\)")))))
  3676. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3677. ;; set this option proved cumbersome. See this message/thread:
  3678. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3679. (defvar org-emphasis-regexp-components
  3680. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3681. "Components used to build the regular expression for emphasis.
  3682. This is a list with five entries. Terminology: In an emphasis string
  3683. like \" *strong word* \", we call the initial space PREMATCH, the final
  3684. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3685. and \"trong wor\" is the body. The different components in this variable
  3686. specify what is allowed/forbidden in each part:
  3687. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3688. post Chars allowed as postmatch. End of line will be allowed too.
  3689. border The chars *forbidden* as border characters.
  3690. body-regexp A regexp like \".\" to match a body character. Don't use
  3691. non-shy groups here, and don't allow newline here.
  3692. newline The maximum number of newlines allowed in an emphasis exp.
  3693. You need to reload Org or to restart Emacs after customizing this.")
  3694. (defcustom org-emphasis-alist
  3695. `(("*" bold)
  3696. ("/" italic)
  3697. ("_" underline)
  3698. ("=" org-verbatim verbatim)
  3699. ("~" org-code verbatim)
  3700. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3701. "Alist of characters and faces to emphasize text.
  3702. Text starting and ending with a special character will be emphasized,
  3703. for example *bold*, _underlined_ and /italic/. This variable sets the
  3704. marker characters and the face to be used by font-lock for highlighting
  3705. in Org-mode Emacs buffers.
  3706. You need to reload Org or to restart Emacs after customizing this."
  3707. :group 'org-appearance
  3708. :set 'org-set-emph-re
  3709. :version "24.4"
  3710. :package-version '(Org . "8.0")
  3711. :type '(repeat
  3712. (list
  3713. (string :tag "Marker character")
  3714. (choice
  3715. (face :tag "Font-lock-face")
  3716. (plist :tag "Face property list"))
  3717. (option (const verbatim)))))
  3718. (defvar org-protecting-blocks
  3719. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3720. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3721. This is needed for font-lock setup.")
  3722. ;;; Miscellaneous options
  3723. (defgroup org-completion nil
  3724. "Completion in Org-mode."
  3725. :tag "Org Completion"
  3726. :group 'org)
  3727. (defcustom org-completion-use-ido nil
  3728. "Non-nil means use ido completion wherever possible.
  3729. Note that `ido-mode' must be active for this variable to be relevant.
  3730. If you decide to turn this variable on, you might well want to turn off
  3731. `org-outline-path-complete-in-steps'.
  3732. See also `org-completion-use-iswitchb'."
  3733. :group 'org-completion
  3734. :type 'boolean)
  3735. (defcustom org-completion-use-iswitchb nil
  3736. "Non-nil means use iswitchb completion wherever possible.
  3737. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3738. If you decide to turn this variable on, you might well want to turn off
  3739. `org-outline-path-complete-in-steps'.
  3740. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3741. :group 'org-completion
  3742. :type 'boolean)
  3743. (defcustom org-completion-fallback-command 'hippie-expand
  3744. "The expansion command called by \\[pcomplete] in normal context.
  3745. Normal means, no org-mode-specific context."
  3746. :group 'org-completion
  3747. :type 'function)
  3748. ;;; Functions and variables from their packages
  3749. ;; Declared here to avoid compiler warnings
  3750. ;; XEmacs only
  3751. (defvar outline-mode-menu-heading)
  3752. (defvar outline-mode-menu-show)
  3753. (defvar outline-mode-menu-hide)
  3754. (defvar zmacs-regions) ; XEmacs regions
  3755. ;; Emacs only
  3756. (defvar mark-active)
  3757. ;; Various packages
  3758. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3759. (declare-function calendar-forward-day "cal-move" (arg))
  3760. (declare-function calendar-goto-date "cal-move" (date))
  3761. (declare-function calendar-goto-today "cal-move" ())
  3762. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3763. (defvar calc-embedded-close-formula)
  3764. (defvar calc-embedded-open-formula)
  3765. (declare-function cdlatex-tab "ext:cdlatex" ())
  3766. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3767. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3768. (defvar font-lock-unfontify-region-function)
  3769. (declare-function iswitchb-read-buffer "iswitchb"
  3770. (prompt &optional default require-match start matches-set))
  3771. (defvar iswitchb-temp-buflist)
  3772. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3773. (defvar org-agenda-tags-todo-honor-ignore-options)
  3774. (declare-function org-agenda-skip "org-agenda" ())
  3775. (declare-function
  3776. org-agenda-format-item "org-agenda"
  3777. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3778. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3779. (declare-function org-agenda-change-all-lines "org-agenda"
  3780. (newhead hdmarker &optional fixface just-this))
  3781. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3782. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3783. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3784. (beg end))
  3785. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3786. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3787. "org-agenda" (&optional end))
  3788. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3789. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3790. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3791. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3792. (declare-function org-indent-mode "org-indent" (&optional arg))
  3793. (declare-function parse-time-string "parse-time" (string))
  3794. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3795. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3796. (defvar remember-data-file)
  3797. (defvar texmathp-why)
  3798. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3799. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3800. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3801. ;;;###autoload
  3802. (defun turn-on-orgtbl ()
  3803. "Unconditionally turn on `orgtbl-mode'."
  3804. (require 'org-table)
  3805. (orgtbl-mode 1))
  3806. (defun org-at-table-p (&optional table-type)
  3807. "Return t if the cursor is inside an org-type table.
  3808. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3809. (if org-enable-table-editor
  3810. (save-excursion
  3811. (beginning-of-line 1)
  3812. (looking-at (if table-type org-table-any-line-regexp
  3813. org-table-line-regexp)))
  3814. nil))
  3815. (defsubst org-table-p () (org-at-table-p))
  3816. (defun org-at-table.el-p ()
  3817. "Return t if and only if we are at a table.el table."
  3818. (and (org-at-table-p 'any)
  3819. (save-excursion
  3820. (goto-char (org-table-begin 'any))
  3821. (looking-at org-table1-hline-regexp))))
  3822. (defun org-table-recognize-table.el ()
  3823. "If there is a table.el table nearby, recognize it and move into it."
  3824. (if org-table-tab-recognizes-table.el
  3825. (if (org-at-table.el-p)
  3826. (progn
  3827. (beginning-of-line 1)
  3828. (if (looking-at org-table-dataline-regexp)
  3829. nil
  3830. (if (looking-at org-table1-hline-regexp)
  3831. (progn
  3832. (beginning-of-line 2)
  3833. (if (looking-at org-table-any-border-regexp)
  3834. (beginning-of-line -1)))))
  3835. (if (re-search-forward "|" (org-table-end t) t)
  3836. (progn
  3837. (require 'table)
  3838. (if (table--at-cell-p (point))
  3839. t
  3840. (message "recognizing table.el table...")
  3841. (table-recognize-table)
  3842. (message "recognizing table.el table...done")))
  3843. (error "This should not happen"))
  3844. t)
  3845. nil)
  3846. nil))
  3847. (defun org-at-table-hline-p ()
  3848. "Return t if the cursor is inside a hline in a table."
  3849. (if org-enable-table-editor
  3850. (save-excursion
  3851. (beginning-of-line 1)
  3852. (looking-at org-table-hline-regexp))
  3853. nil))
  3854. (defun org-table-map-tables (function &optional quietly)
  3855. "Apply FUNCTION to the start of all tables in the buffer."
  3856. (save-excursion
  3857. (save-restriction
  3858. (widen)
  3859. (goto-char (point-min))
  3860. (while (re-search-forward org-table-any-line-regexp nil t)
  3861. (unless quietly
  3862. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3863. (beginning-of-line 1)
  3864. (when (and (looking-at org-table-line-regexp)
  3865. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3866. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3867. (save-excursion (funcall function))
  3868. (or (looking-at org-table-line-regexp)
  3869. (forward-char 1)))
  3870. (re-search-forward org-table-any-border-regexp nil 1))))
  3871. (unless quietly (message "Mapping tables: done")))
  3872. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3873. (declare-function org-clock-update-mode-line "org-clock" ())
  3874. (declare-function org-resolve-clocks "org-clock"
  3875. (&optional also-non-dangling-p prompt last-valid))
  3876. (defun org-at-TBLFM-p (&optional pos)
  3877. "Return t when point (or POS) is in #+TBLFM line."
  3878. (save-excursion
  3879. (let ((pos pos)))
  3880. (goto-char (or pos (point)))
  3881. (beginning-of-line 1)
  3882. (looking-at org-TBLFM-regexp)))
  3883. (defvar org-clock-start-time)
  3884. (defvar org-clock-marker (make-marker)
  3885. "Marker recording the last clock-in.")
  3886. (defvar org-clock-hd-marker (make-marker)
  3887. "Marker recording the last clock-in, but the headline position.")
  3888. (defvar org-clock-heading ""
  3889. "The heading of the current clock entry.")
  3890. (defun org-clock-is-active ()
  3891. "Return the buffer where the clock is currently running.
  3892. Return nil if no clock is running."
  3893. (marker-buffer org-clock-marker))
  3894. (defun org-check-running-clock ()
  3895. "Check if the current buffer contains the running clock.
  3896. If yes, offer to stop it and to save the buffer with the changes."
  3897. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3898. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3899. (buffer-name))))
  3900. (org-clock-out)
  3901. (when (y-or-n-p "Save changed buffer?")
  3902. (save-buffer))))
  3903. (defun org-clocktable-try-shift (dir n)
  3904. "Check if this line starts a clock table, if yes, shift the time block."
  3905. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3906. (org-clocktable-shift dir n)))
  3907. ;;;###autoload
  3908. (defun org-clock-persistence-insinuate ()
  3909. "Set up hooks for clock persistence."
  3910. (require 'org-clock)
  3911. (add-hook 'org-mode-hook 'org-clock-load)
  3912. (add-hook 'kill-emacs-hook 'org-clock-save))
  3913. (defgroup org-archive nil
  3914. "Options concerning archiving in Org-mode."
  3915. :tag "Org Archive"
  3916. :group 'org-structure)
  3917. (defcustom org-archive-location "%s_archive::"
  3918. "The location where subtrees should be archived.
  3919. The value of this variable is a string, consisting of two parts,
  3920. separated by a double-colon. The first part is a filename and
  3921. the second part is a headline.
  3922. When the filename is omitted, archiving happens in the same file.
  3923. %s in the filename will be replaced by the current file
  3924. name (without the directory part). Archiving to a different file
  3925. is useful to keep archived entries from contributing to the
  3926. Org-mode Agenda.
  3927. The archived entries will be filed as subtrees of the specified
  3928. headline. When the headline is omitted, the subtrees are simply
  3929. filed away at the end of the file, as top-level entries. Also in
  3930. the heading you can use %s to represent the file name, this can be
  3931. useful when using the same archive for a number of different files.
  3932. Here are a few examples:
  3933. \"%s_archive::\"
  3934. If the current file is Projects.org, archive in file
  3935. Projects.org_archive, as top-level trees. This is the default.
  3936. \"::* Archived Tasks\"
  3937. Archive in the current file, under the top-level headline
  3938. \"* Archived Tasks\".
  3939. \"~/org/archive.org::\"
  3940. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3941. \"~/org/archive.org::* From %s\"
  3942. Archive in file ~/org/archive.org (absolute path), under headlines
  3943. \"From FILENAME\" where file name is the current file name.
  3944. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3945. The \"datetree/\" string is special, signifying to archive
  3946. items to the datetree. Items are placed in either the CLOSED
  3947. date of the item, or the current date if there is no CLOSED date.
  3948. The heading will be a subentry to the current date. There doesn't
  3949. need to be a heading, but there always needs to be a slash after
  3950. datetree. For example, to store archived items directly in the
  3951. datetree, use \"~/org/datetree.org::datetree/\".
  3952. \"basement::** Finished Tasks\"
  3953. Archive in file ./basement (relative path), as level 3 trees
  3954. below the level 2 heading \"** Finished Tasks\".
  3955. You may set this option on a per-file basis by adding to the buffer a
  3956. line like
  3957. #+ARCHIVE: basement::** Finished Tasks
  3958. You may also define it locally for a subtree by setting an ARCHIVE property
  3959. in the entry. If such a property is found in an entry, or anywhere up
  3960. the hierarchy, it will be used."
  3961. :group 'org-archive
  3962. :type 'string)
  3963. (defcustom org-agenda-skip-archived-trees t
  3964. "Non-nil means the agenda will skip any items located in archived trees.
  3965. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3966. variable is no longer recommended, you should leave it at the value t.
  3967. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3968. :group 'org-archive
  3969. :group 'org-agenda-skip
  3970. :type 'boolean)
  3971. (defcustom org-columns-skip-archived-trees t
  3972. "Non-nil means ignore archived trees when creating column view."
  3973. :group 'org-archive
  3974. :group 'org-properties
  3975. :type 'boolean)
  3976. (defcustom org-cycle-open-archived-trees nil
  3977. "Non-nil means `org-cycle' will open archived trees.
  3978. An archived tree is a tree marked with the tag ARCHIVE.
  3979. When nil, archived trees will stay folded. You can still open them with
  3980. normal outline commands like `show-all', but not with the cycling commands."
  3981. :group 'org-archive
  3982. :group 'org-cycle
  3983. :type 'boolean)
  3984. (defcustom org-sparse-tree-open-archived-trees nil
  3985. "Non-nil means sparse tree construction shows matches in archived trees.
  3986. When nil, matches in these trees are highlighted, but the trees are kept in
  3987. collapsed state."
  3988. :group 'org-archive
  3989. :group 'org-sparse-trees
  3990. :type 'boolean)
  3991. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3992. "The default date type when building a sparse tree.
  3993. When this is nil, a date is a scheduled or a deadline timestamp.
  3994. Otherwise, these types are allowed:
  3995. all: all timestamps
  3996. active: only active timestamps (<...>)
  3997. inactive: only inactive timestamps (<...)
  3998. scheduled: only scheduled timestamps
  3999. deadline: only deadline timestamps"
  4000. :type '(choice (const :tag "Scheduled or deadline" scheduled-or-deadline)
  4001. (const :tag "All timestamps" all)
  4002. (const :tag "Only active timestamps" active)
  4003. (const :tag "Only inactive timestamps" inactive)
  4004. (const :tag "Only scheduled timestamps" scheduled)
  4005. (const :tag "Only deadline timestamps" deadline)
  4006. (const :tag "Only closed timestamps" closed))
  4007. :version "24.3"
  4008. :group 'org-sparse-trees)
  4009. (defun org-cycle-hide-archived-subtrees (state)
  4010. "Re-hide all archived subtrees after a visibility state change."
  4011. (when (and (not org-cycle-open-archived-trees)
  4012. (not (memq state '(overview folded))))
  4013. (save-excursion
  4014. (let* ((globalp (memq state '(contents all)))
  4015. (beg (if globalp (point-min) (point)))
  4016. (end (if globalp (point-max) (org-end-of-subtree t))))
  4017. (org-hide-archived-subtrees beg end)
  4018. (goto-char beg)
  4019. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4020. (message "%s" (substitute-command-keys
  4021. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4022. (defun org-force-cycle-archived ()
  4023. "Cycle subtree even if it is archived."
  4024. (interactive)
  4025. (setq this-command 'org-cycle)
  4026. (let ((org-cycle-open-archived-trees t))
  4027. (call-interactively 'org-cycle)))
  4028. (defun org-hide-archived-subtrees (beg end)
  4029. "Re-hide all archived subtrees after a visibility state change."
  4030. (save-excursion
  4031. (let* ((re (concat ":" org-archive-tag ":")))
  4032. (goto-char beg)
  4033. (while (re-search-forward re end t)
  4034. (when (org-at-heading-p)
  4035. (org-flag-subtree t)
  4036. (org-end-of-subtree t))))))
  4037. (declare-function outline-end-of-heading "outline" ())
  4038. (declare-function outline-flag-region "outline" (from to flag))
  4039. (defun org-flag-subtree (flag)
  4040. (save-excursion
  4041. (org-back-to-heading t)
  4042. (outline-end-of-heading)
  4043. (outline-flag-region (point)
  4044. (progn (org-end-of-subtree t) (point))
  4045. flag)))
  4046. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4047. ;; Declare Column View Code
  4048. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  4049. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4050. (declare-function org-columns-compute "org-colview" (property))
  4051. ;; Declare ID code
  4052. (declare-function org-id-store-link "org-id")
  4053. (declare-function org-id-locations-load "org-id")
  4054. (declare-function org-id-locations-save "org-id")
  4055. (defvar org-id-track-globally)
  4056. ;;; Variables for pre-computed regular expressions, all buffer local
  4057. (defvar org-todo-regexp nil
  4058. "Matches any of the TODO state keywords.")
  4059. (make-variable-buffer-local 'org-todo-regexp)
  4060. (defvar org-not-done-regexp nil
  4061. "Matches any of the TODO state keywords except the last one.")
  4062. (make-variable-buffer-local 'org-not-done-regexp)
  4063. (defvar org-not-done-heading-regexp nil
  4064. "Matches a TODO headline that is not done.")
  4065. (make-variable-buffer-local 'org-not-done-regexp)
  4066. (defvar org-todo-line-regexp nil
  4067. "Matches a headline and puts TODO state into group 2 if present.")
  4068. (make-variable-buffer-local 'org-todo-line-regexp)
  4069. (defvar org-complex-heading-regexp nil
  4070. "Matches a headline and puts everything into groups:
  4071. group 1: the stars
  4072. group 2: The todo keyword, maybe
  4073. group 3: Priority cookie
  4074. group 4: True headline
  4075. group 5: Tags")
  4076. (make-variable-buffer-local 'org-complex-heading-regexp)
  4077. (defvar org-complex-heading-regexp-format nil
  4078. "Printf format to make regexp to match an exact headline.
  4079. This regexp will match the headline of any node which has the
  4080. exact headline text that is put into the format, but may have any
  4081. TODO state, priority and tags.")
  4082. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4083. (defvar org-todo-line-tags-regexp nil
  4084. "Matches a headline and puts TODO state into group 2 if present.
  4085. Also put tags into group 4 if tags are present.")
  4086. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4087. (defvar org-ds-keyword-length 12
  4088. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4089. (make-variable-buffer-local 'org-ds-keyword-length)
  4090. (defvar org-deadline-regexp nil
  4091. "Matches the DEADLINE keyword.")
  4092. (make-variable-buffer-local 'org-deadline-regexp)
  4093. (defvar org-deadline-time-regexp nil
  4094. "Matches the DEADLINE keyword together with a time stamp.")
  4095. (make-variable-buffer-local 'org-deadline-time-regexp)
  4096. (defvar org-deadline-time-hour-regexp nil
  4097. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4098. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4099. (defvar org-deadline-line-regexp nil
  4100. "Matches the DEADLINE keyword and the rest of the line.")
  4101. (make-variable-buffer-local 'org-deadline-line-regexp)
  4102. (defvar org-scheduled-regexp nil
  4103. "Matches the SCHEDULED keyword.")
  4104. (make-variable-buffer-local 'org-scheduled-regexp)
  4105. (defvar org-scheduled-time-regexp nil
  4106. "Matches the SCHEDULED keyword together with a time stamp.")
  4107. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4108. (defvar org-scheduled-time-hour-regexp nil
  4109. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4110. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4111. (defvar org-closed-time-regexp nil
  4112. "Matches the CLOSED keyword together with a time stamp.")
  4113. (make-variable-buffer-local 'org-closed-time-regexp)
  4114. (defvar org-keyword-time-regexp nil
  4115. "Matches any of the 4 keywords, together with the time stamp.")
  4116. (make-variable-buffer-local 'org-keyword-time-regexp)
  4117. (defvar org-keyword-time-not-clock-regexp nil
  4118. "Matches any of the 3 keywords, together with the time stamp.")
  4119. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4120. (defvar org-maybe-keyword-time-regexp nil
  4121. "Matches a timestamp, possibly preceded by a keyword.")
  4122. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4123. (defvar org-all-time-keywords nil
  4124. "List of time keywords.")
  4125. (make-variable-buffer-local 'org-all-time-keywords)
  4126. (defconst org-plain-time-of-day-regexp
  4127. (concat
  4128. "\\(\\<[012]?[0-9]"
  4129. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4130. "\\(--?"
  4131. "\\(\\<[012]?[0-9]"
  4132. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4133. "\\)?")
  4134. "Regular expression to match a plain time or time range.
  4135. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4136. groups carry important information:
  4137. 0 the full match
  4138. 1 the first time, range or not
  4139. 8 the second time, if it is a range.")
  4140. (defconst org-plain-time-extension-regexp
  4141. (concat
  4142. "\\(\\<[012]?[0-9]"
  4143. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4144. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4145. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4146. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4147. groups carry important information:
  4148. 0 the full match
  4149. 7 hours of duration
  4150. 9 minutes of duration")
  4151. (defconst org-stamp-time-of-day-regexp
  4152. (concat
  4153. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4154. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4155. "\\(--?"
  4156. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4157. "Regular expression to match a timestamp time or time range.
  4158. After a match, the following groups carry important information:
  4159. 0 the full match
  4160. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4161. 2 the first time, range or not
  4162. 4 the second time, if it is a range.")
  4163. (defconst org-startup-options
  4164. '(("fold" org-startup-folded t)
  4165. ("overview" org-startup-folded t)
  4166. ("nofold" org-startup-folded nil)
  4167. ("showall" org-startup-folded nil)
  4168. ("showeverything" org-startup-folded showeverything)
  4169. ("content" org-startup-folded content)
  4170. ("indent" org-startup-indented t)
  4171. ("noindent" org-startup-indented nil)
  4172. ("hidestars" org-hide-leading-stars t)
  4173. ("showstars" org-hide-leading-stars nil)
  4174. ("odd" org-odd-levels-only t)
  4175. ("oddeven" org-odd-levels-only nil)
  4176. ("align" org-startup-align-all-tables t)
  4177. ("noalign" org-startup-align-all-tables nil)
  4178. ("inlineimages" org-startup-with-inline-images t)
  4179. ("noinlineimages" org-startup-with-inline-images nil)
  4180. ("latexpreview" org-startup-with-latex-preview t)
  4181. ("nolatexpreview" org-startup-with-latex-preview nil)
  4182. ("customtime" org-display-custom-times t)
  4183. ("logdone" org-log-done time)
  4184. ("lognotedone" org-log-done note)
  4185. ("nologdone" org-log-done nil)
  4186. ("lognoteclock-out" org-log-note-clock-out t)
  4187. ("nolognoteclock-out" org-log-note-clock-out nil)
  4188. ("logrepeat" org-log-repeat state)
  4189. ("lognoterepeat" org-log-repeat note)
  4190. ("logdrawer" org-log-into-drawer t)
  4191. ("nologdrawer" org-log-into-drawer nil)
  4192. ("logstatesreversed" org-log-states-order-reversed t)
  4193. ("nologstatesreversed" org-log-states-order-reversed nil)
  4194. ("nologrepeat" org-log-repeat nil)
  4195. ("logreschedule" org-log-reschedule time)
  4196. ("lognotereschedule" org-log-reschedule note)
  4197. ("nologreschedule" org-log-reschedule nil)
  4198. ("logredeadline" org-log-redeadline time)
  4199. ("lognoteredeadline" org-log-redeadline note)
  4200. ("nologredeadline" org-log-redeadline nil)
  4201. ("logrefile" org-log-refile time)
  4202. ("lognoterefile" org-log-refile note)
  4203. ("nologrefile" org-log-refile nil)
  4204. ("fninline" org-footnote-define-inline t)
  4205. ("nofninline" org-footnote-define-inline nil)
  4206. ("fnlocal" org-footnote-section nil)
  4207. ("fnauto" org-footnote-auto-label t)
  4208. ("fnprompt" org-footnote-auto-label nil)
  4209. ("fnconfirm" org-footnote-auto-label confirm)
  4210. ("fnplain" org-footnote-auto-label plain)
  4211. ("fnadjust" org-footnote-auto-adjust t)
  4212. ("nofnadjust" org-footnote-auto-adjust nil)
  4213. ("constcgs" constants-unit-system cgs)
  4214. ("constSI" constants-unit-system SI)
  4215. ("noptag" org-tag-persistent-alist nil)
  4216. ("hideblocks" org-hide-block-startup t)
  4217. ("nohideblocks" org-hide-block-startup nil)
  4218. ("beamer" org-startup-with-beamer-mode t)
  4219. ("entitiespretty" org-pretty-entities t)
  4220. ("entitiesplain" org-pretty-entities nil))
  4221. "Variable associated with STARTUP options for org-mode.
  4222. Each element is a list of three items: the startup options (as written
  4223. in the #+STARTUP line), the corresponding variable, and the value to set
  4224. this variable to if the option is found. An optional forth element PUSH
  4225. means to push this value onto the list in the variable.")
  4226. (defun org-update-property-plist (key val props)
  4227. "Update PROPS with KEY and VAL."
  4228. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4229. (key (if appending (substring key 0 (- (length key) 1)) key))
  4230. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4231. (previous (cdr (assoc key props))))
  4232. (if appending
  4233. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4234. (cons (cons key val) remainder))))
  4235. (defcustom org-group-tags t
  4236. "When non-nil (the default), use group tags.
  4237. This can be turned on/off through `org-toggle-tags-groups'."
  4238. :group 'org-tags
  4239. :group 'org-startup
  4240. :type 'boolean)
  4241. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4242. (defun org-toggle-tags-groups ()
  4243. "Toggle support for group tags.
  4244. Support for group tags is controlled by the option
  4245. `org-group-tags', which is non-nil by default."
  4246. (interactive)
  4247. (setq org-group-tags (not org-group-tags))
  4248. (cond ((and (derived-mode-p 'org-agenda-mode)
  4249. org-group-tags)
  4250. (org-agenda-redo))
  4251. ((derived-mode-p 'org-mode)
  4252. (let ((org-inhibit-startup t)) (org-mode))))
  4253. (message "Groups tags support has been turned %s"
  4254. (if org-group-tags "on" "off")))
  4255. (defun org-set-regexps-and-options-for-tags ()
  4256. "Precompute variables used for tags."
  4257. (when (derived-mode-p 'org-mode)
  4258. (org-set-local 'org-file-tags nil)
  4259. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4260. (splitre "[ \t]+")
  4261. (start 0)
  4262. tags ftags key value)
  4263. (save-excursion
  4264. (save-restriction
  4265. (widen)
  4266. (goto-char (point-min))
  4267. (while (re-search-forward re nil t)
  4268. (setq key (upcase (org-match-string-no-properties 1))
  4269. value (org-match-string-no-properties 2))
  4270. (if (stringp value) (setq value (org-trim value)))
  4271. (cond
  4272. ((equal key "TAGS")
  4273. (setq tags (append tags (if tags '("\\n") nil)
  4274. (org-split-string value splitre))))
  4275. ((equal key "FILETAGS")
  4276. (when (string-match "\\S-" value)
  4277. (setq ftags
  4278. (append
  4279. ftags
  4280. (apply 'append
  4281. (mapcar (lambda (x) (org-split-string x ":"))
  4282. (org-split-string value)))))))))))
  4283. ;; Process the file tags.
  4284. (and ftags (org-set-local 'org-file-tags
  4285. (mapcar 'org-add-prop-inherited ftags)))
  4286. (org-set-local 'org-tag-groups-alist nil)
  4287. ;; Process the tags.
  4288. (when (and (not tags) org-tag-alist)
  4289. (setq tags
  4290. (mapcar
  4291. (lambda (tg) (cond ((eq (car tg) :startgroup) "{")
  4292. ((eq (car tg) :endgroup) "}")
  4293. ((eq (car tg) :grouptags) ":")
  4294. ((eq (car tg) :newline) "\n")
  4295. (t (concat (car tg)
  4296. (if (characterp (cdr tg))
  4297. (format "(%s)" (char-to-string (cdr tg))) "")))))
  4298. org-tag-alist)))
  4299. (let (e tgs g)
  4300. (while (setq e (pop tags))
  4301. (cond
  4302. ((equal e "{")
  4303. (progn (push '(:startgroup) tgs)
  4304. (when (equal (nth 1 tags) ":")
  4305. (push (list (replace-regexp-in-string
  4306. "(.+)$" "" (nth 0 tags)))
  4307. org-tag-groups-alist)
  4308. (setq g 0))))
  4309. ((equal e ":") (push '(:grouptags) tgs))
  4310. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4311. ((equal e "\\n") (push '(:newline) tgs))
  4312. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4313. (push (cons (match-string 1 e)
  4314. (string-to-char (match-string 2 e))) tgs)
  4315. (if (and g (> g 0))
  4316. (setcar org-tag-groups-alist
  4317. (append (car org-tag-groups-alist)
  4318. (list (match-string 1 e)))))
  4319. (if g (setq g (1+ g))))
  4320. (t (push (list e) tgs)
  4321. (if (and g (> g 0))
  4322. (setcar org-tag-groups-alist
  4323. (append (car org-tag-groups-alist) (list e))))
  4324. (if g (setq g (1+ g))))))
  4325. (org-set-local 'org-tag-alist nil)
  4326. (while (setq e (pop tgs))
  4327. (or (and (stringp (car e))
  4328. (assoc (car e) org-tag-alist))
  4329. (push e org-tag-alist)))
  4330. ;; Return a list with tag variables
  4331. (list org-file-tags org-tag-alist org-tag-groups-alist)))))
  4332. (defvar org-ota nil)
  4333. (defun org-set-regexps-and-options ()
  4334. "Precompute regular expressions used in the current buffer."
  4335. (when (derived-mode-p 'org-mode)
  4336. (org-set-local 'org-todo-kwd-alist nil)
  4337. (org-set-local 'org-todo-key-alist nil)
  4338. (org-set-local 'org-todo-key-trigger nil)
  4339. (org-set-local 'org-todo-keywords-1 nil)
  4340. (org-set-local 'org-done-keywords nil)
  4341. (org-set-local 'org-todo-heads nil)
  4342. (org-set-local 'org-todo-sets nil)
  4343. (org-set-local 'org-todo-log-states nil)
  4344. (org-set-local 'org-file-properties nil)
  4345. (let ((re (org-make-options-regexp
  4346. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4347. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4348. "SETUPFILE" "OPTIONS")
  4349. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4350. (splitre "[ \t]+")
  4351. (scripts org-use-sub-superscripts)
  4352. kwds kws0 kwsa key log value cat arch const links hw dws
  4353. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4354. (start 0))
  4355. (save-excursion
  4356. (save-restriction
  4357. (widen)
  4358. (goto-char (point-min))
  4359. (while
  4360. (or (and
  4361. ext-setup-or-nil
  4362. (not org-ota)
  4363. (let (ret)
  4364. (with-temp-buffer
  4365. (insert ext-setup-or-nil)
  4366. (let ((major-mode 'org-mode) org-ota)
  4367. (setq ret (save-match-data
  4368. (org-set-regexps-and-options-for-tags)))))
  4369. ;; Append setupfile tags to existing tags
  4370. (setq org-ota t)
  4371. (setq org-file-tags
  4372. (delq nil (append org-file-tags (nth 0 ret)))
  4373. org-tag-alist
  4374. (delq nil (append org-tag-alist (nth 1 ret)))
  4375. org-tag-groups-alist
  4376. (delq nil (append org-tag-groups-alist (nth 2 ret))))))
  4377. (and ext-setup-or-nil
  4378. (string-match re ext-setup-or-nil start)
  4379. (setq start (match-end 0)))
  4380. (and (setq ext-setup-or-nil nil start 0)
  4381. (re-search-forward re nil t)))
  4382. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4383. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4384. (if (stringp value) (setq value (org-trim value)))
  4385. (cond
  4386. ((equal key "CATEGORY")
  4387. (setq cat value))
  4388. ((member key '("SEQ_TODO" "TODO"))
  4389. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4390. ((equal key "TYP_TODO")
  4391. (push (cons 'type (org-split-string value splitre)) kwds))
  4392. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4393. ;; general TODO-like setup
  4394. (push (cons (intern (downcase (match-string 1 key)))
  4395. (org-split-string value splitre)) kwds))
  4396. ((equal key "COLUMNS")
  4397. (org-set-local 'org-columns-default-format value))
  4398. ((equal key "LINK")
  4399. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4400. (push (cons (match-string 1 value)
  4401. (org-trim (match-string 2 value)))
  4402. links)))
  4403. ((equal key "PRIORITIES")
  4404. (setq prio (org-split-string value " +")))
  4405. ((equal key "PROPERTY")
  4406. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4407. (setq props (org-update-property-plist (match-string 1 value)
  4408. (match-string 2 value)
  4409. props))))
  4410. ((equal key "CONSTANTS")
  4411. (org-table-set-constants))
  4412. ((equal key "STARTUP")
  4413. (let ((opts (org-split-string value splitre))
  4414. l var val)
  4415. (while (setq l (pop opts))
  4416. (when (setq l (assoc l org-startup-options))
  4417. (setq var (nth 1 l) val (nth 2 l))
  4418. (if (not (nth 3 l))
  4419. (set (make-local-variable var) val)
  4420. (if (not (listp (symbol-value var)))
  4421. (set (make-local-variable var) nil))
  4422. (set (make-local-variable var) (symbol-value var))
  4423. (add-to-list var val))))))
  4424. ((equal key "ARCHIVE")
  4425. (setq arch value)
  4426. (remove-text-properties 0 (length arch)
  4427. '(face t fontified t) arch))
  4428. ((equal key "OPTIONS")
  4429. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4430. (setq scripts (read (match-string 2 value)))))
  4431. ((and (equal key "SETUPFILE")
  4432. ;; Prevent checking in Gnus messages
  4433. (not buffer-read-only))
  4434. (setq setup-contents (org-file-contents
  4435. (expand-file-name
  4436. (org-remove-double-quotes value))
  4437. 'noerror))
  4438. (if (not ext-setup-or-nil)
  4439. (setq ext-setup-or-nil setup-contents start 0)
  4440. (setq ext-setup-or-nil
  4441. (concat (substring ext-setup-or-nil 0 start)
  4442. "\n" setup-contents "\n"
  4443. (substring ext-setup-or-nil start)))))))
  4444. ;; search for property blocks
  4445. (goto-char (point-min))
  4446. (while (re-search-forward org-block-regexp nil t)
  4447. (when (equal "PROPERTY" (upcase (match-string 1)))
  4448. (setq value (replace-regexp-in-string
  4449. "[\n\r]" " " (match-string 4)))
  4450. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4451. (setq props (org-update-property-plist (match-string 1 value)
  4452. (match-string 2 value)
  4453. props)))))))
  4454. (org-set-local 'org-use-sub-superscripts scripts)
  4455. (when cat
  4456. (org-set-local 'org-category (intern cat))
  4457. (push (cons "CATEGORY" cat) props))
  4458. (when prio
  4459. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4460. (setq prio (mapcar 'string-to-char prio))
  4461. (org-set-local 'org-highest-priority (nth 0 prio))
  4462. (org-set-local 'org-lowest-priority (nth 1 prio))
  4463. (org-set-local 'org-default-priority (nth 2 prio)))
  4464. (and props (org-set-local 'org-file-properties (nreverse props)))
  4465. (and arch (org-set-local 'org-archive-location arch))
  4466. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4467. ;; Process the TODO keywords
  4468. (unless kwds
  4469. ;; Use the global values as if they had been given locally.
  4470. (setq kwds (default-value 'org-todo-keywords))
  4471. (if (stringp (car kwds))
  4472. (setq kwds (list (cons org-todo-interpretation
  4473. (default-value 'org-todo-keywords)))))
  4474. (setq kwds (reverse kwds)))
  4475. (setq kwds (nreverse kwds))
  4476. (let (inter kws kw)
  4477. (while (setq kws (pop kwds))
  4478. (let ((kws (or
  4479. (run-hook-with-args-until-success
  4480. 'org-todo-setup-filter-hook kws)
  4481. kws)))
  4482. (setq inter (pop kws) sep (member "|" kws)
  4483. kws0 (delete "|" (copy-sequence kws))
  4484. kwsa nil
  4485. kws1 (mapcar
  4486. (lambda (x)
  4487. ;; 1 2
  4488. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4489. (progn
  4490. (setq kw (match-string 1 x)
  4491. key (and (match-end 2) (match-string 2 x))
  4492. log (org-extract-log-state-settings x))
  4493. (push (cons kw (and key (string-to-char key))) kwsa)
  4494. (and log (push log org-todo-log-states))
  4495. kw)
  4496. (error "Invalid TODO keyword %s" x)))
  4497. kws0)
  4498. kwsa (if kwsa (append '((:startgroup))
  4499. (nreverse kwsa)
  4500. '((:endgroup))))
  4501. hw (car kws1)
  4502. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4503. tail (list inter hw (car dws) (org-last dws))))
  4504. (add-to-list 'org-todo-heads hw 'append)
  4505. (push kws1 org-todo-sets)
  4506. (setq org-done-keywords (append org-done-keywords dws nil))
  4507. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4508. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4509. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4510. (setq org-todo-sets (nreverse org-todo-sets)
  4511. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4512. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4513. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4514. ;; Compute the regular expressions and other local variables.
  4515. ;; Using `org-outline-regexp-bol' would complicate them much,
  4516. ;; because of the fixed white space at the end of that string.
  4517. (if (not org-done-keywords)
  4518. (setq org-done-keywords (and org-todo-keywords-1
  4519. (list (org-last org-todo-keywords-1)))))
  4520. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4521. (length org-scheduled-string)
  4522. (length org-clock-string)
  4523. (length org-closed-string)))
  4524. org-not-done-keywords
  4525. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4526. org-todo-regexp
  4527. (concat "\\("
  4528. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4529. "\\)")
  4530. org-not-done-regexp
  4531. (concat "\\("
  4532. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4533. "\\)")
  4534. org-not-done-heading-regexp
  4535. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4536. org-todo-line-regexp
  4537. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4538. org-complex-heading-regexp
  4539. (concat "^\\(\\*+\\)"
  4540. "\\(?: +" org-todo-regexp "\\)?"
  4541. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4542. "\\(?: +\\(.*?\\)\\)??"
  4543. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4544. "[ \t]*$")
  4545. org-complex-heading-regexp-format
  4546. (concat "^\\(\\*+\\)"
  4547. "\\(?: +" org-todo-regexp "\\)?"
  4548. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4549. "\\(?: +"
  4550. ;; Stats cookies can be stuck to body.
  4551. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4552. "\\(%s\\)"
  4553. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4554. "\\)"
  4555. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4556. "[ \t]*$")
  4557. org-todo-line-tags-regexp
  4558. (concat "^\\(\\*+\\)"
  4559. "\\(?: +" org-todo-regexp "\\)?"
  4560. "\\(?: +\\(.*?\\)\\)??"
  4561. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4562. "[ \t]*$")
  4563. org-deadline-regexp (concat "\\<" org-deadline-string)
  4564. org-deadline-time-regexp
  4565. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4566. org-deadline-time-hour-regexp
  4567. (concat "\\<" org-deadline-string
  4568. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4569. org-deadline-line-regexp
  4570. (concat "\\<\\(" org-deadline-string "\\).*")
  4571. org-scheduled-regexp
  4572. (concat "\\<" org-scheduled-string)
  4573. org-scheduled-time-regexp
  4574. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4575. org-scheduled-time-hour-regexp
  4576. (concat "\\<" org-scheduled-string
  4577. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4578. org-closed-time-regexp
  4579. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4580. org-keyword-time-regexp
  4581. (concat "\\<\\(" org-scheduled-string
  4582. "\\|" org-deadline-string
  4583. "\\|" org-closed-string
  4584. "\\|" org-clock-string "\\)"
  4585. " *[[<]\\([^]>]+\\)[]>]")
  4586. org-keyword-time-not-clock-regexp
  4587. (concat "\\<\\(" org-scheduled-string
  4588. "\\|" org-deadline-string
  4589. "\\|" org-closed-string
  4590. "\\)"
  4591. " *[[<]\\([^]>]+\\)[]>]")
  4592. org-maybe-keyword-time-regexp
  4593. (concat "\\(\\<\\(" org-scheduled-string
  4594. "\\|" org-deadline-string
  4595. "\\|" org-closed-string
  4596. "\\|" org-clock-string "\\)\\)?"
  4597. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4598. org-all-time-keywords
  4599. (mapcar (lambda (w) (substring w 0 -1))
  4600. (list org-scheduled-string org-deadline-string
  4601. org-clock-string org-closed-string)))
  4602. (setq org-ota nil)
  4603. (org-compute-latex-and-related-regexp))))
  4604. (defun org-file-contents (file &optional noerror)
  4605. "Return the contents of FILE, as a string."
  4606. (if (and file (file-readable-p file))
  4607. (with-temp-buffer
  4608. (insert-file-contents file)
  4609. (buffer-string))
  4610. (funcall (if noerror #'message #'error)
  4611. "Cannot read file \"%s\"%s"
  4612. file
  4613. (let ((from (buffer-file-name (buffer-base-buffer))))
  4614. (if from (concat " (referenced in file \"" from "\")") "")))))
  4615. (defun org-extract-log-state-settings (x)
  4616. "Extract the log state setting from a TODO keyword string.
  4617. This will extract info from a string like \"WAIT(w@/!)\"."
  4618. (let (kw key log1 log2)
  4619. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4620. (setq kw (match-string 1 x)
  4621. key (and (match-end 2) (match-string 2 x))
  4622. log1 (and (match-end 3) (match-string 3 x))
  4623. log2 (and (match-end 4) (match-string 4 x)))
  4624. (and (or log1 log2)
  4625. (list kw
  4626. (and log1 (if (equal log1 "!") 'time 'note))
  4627. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4628. (defun org-remove-keyword-keys (list)
  4629. "Remove a pair of parenthesis at the end of each string in LIST."
  4630. (mapcar (lambda (x)
  4631. (if (string-match "(.*)$" x)
  4632. (substring x 0 (match-beginning 0))
  4633. x))
  4634. list))
  4635. (defun org-assign-fast-keys (alist)
  4636. "Assign fast keys to a keyword-key alist.
  4637. Respect keys that are already there."
  4638. (let (new e (alt ?0))
  4639. (while (setq e (pop alist))
  4640. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4641. (cdr e)) ;; Key already assigned.
  4642. (push e new)
  4643. (let ((clist (string-to-list (downcase (car e))))
  4644. (used (append new alist)))
  4645. (when (= (car clist) ?@)
  4646. (pop clist))
  4647. (while (and clist (rassoc (car clist) used))
  4648. (pop clist))
  4649. (unless clist
  4650. (while (rassoc alt used)
  4651. (incf alt)))
  4652. (push (cons (car e) (or (car clist) alt)) new))))
  4653. (nreverse new)))
  4654. ;;; Some variables used in various places
  4655. (defvar org-window-configuration nil
  4656. "Used in various places to store a window configuration.")
  4657. (defvar org-selected-window nil
  4658. "Used in various places to store a window configuration.")
  4659. (defvar org-finish-function nil
  4660. "Function to be called when `C-c C-c' is used.
  4661. This is for getting out of special buffers like capture.")
  4662. ;; FIXME: Occasionally check by commenting these, to make sure
  4663. ;; no other functions uses these, forgetting to let-bind them.
  4664. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4665. (defvar org-last-state)
  4666. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4667. ;; Defined somewhere in this file, but used before definition.
  4668. (defvar org-entities) ;; defined in org-entities.el
  4669. (defvar org-struct-menu)
  4670. (defvar org-org-menu)
  4671. (defvar org-tbl-menu)
  4672. ;;;; Define the Org-mode
  4673. ;; We use a before-change function to check if a table might need
  4674. ;; an update.
  4675. (defvar org-table-may-need-update t
  4676. "Indicates that a table might need an update.
  4677. This variable is set by `org-before-change-function'.
  4678. `org-table-align' sets it back to nil.")
  4679. (defun org-before-change-function (beg end)
  4680. "Every change indicates that a table might need an update."
  4681. (setq org-table-may-need-update t))
  4682. (defvar org-mode-map)
  4683. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4684. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4685. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4686. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4687. (defvar org-table-buffer-is-an nil)
  4688. (defvar bidi-paragraph-direction)
  4689. (defvar buffer-face-mode-face)
  4690. (require 'outline)
  4691. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4692. (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"))
  4693. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4694. ;; Other stuff we need.
  4695. (require 'time-date)
  4696. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4697. (require 'easymenu)
  4698. (require 'overlay)
  4699. ;; (require 'org-macs) moved higher up in the file before it is first used
  4700. (require 'org-entities)
  4701. ;; (require 'org-compat) moved higher up in the file before it is first used
  4702. (require 'org-faces)
  4703. (require 'org-list)
  4704. (require 'org-pcomplete)
  4705. (require 'org-src)
  4706. (require 'org-footnote)
  4707. (require 'org-macro)
  4708. ;; babel
  4709. (require 'ob)
  4710. ;;;###autoload
  4711. (define-derived-mode org-mode outline-mode "Org"
  4712. "Outline-based notes management and organizer, alias
  4713. \"Carsten's outline-mode for keeping track of everything.\"
  4714. Org-mode develops organizational tasks around a NOTES file which
  4715. contains information about projects as plain text. Org-mode is
  4716. implemented on top of outline-mode, which is ideal to keep the content
  4717. of large files well structured. It supports ToDo items, deadlines and
  4718. time stamps, which magically appear in the diary listing of the Emacs
  4719. calendar. Tables are easily created with a built-in table editor.
  4720. Plain text URL-like links connect to websites, emails (VM), Usenet
  4721. messages (Gnus), BBDB entries, and any files related to the project.
  4722. For printing and sharing of notes, an Org-mode file (or a part of it)
  4723. can be exported as a structured ASCII or HTML file.
  4724. The following commands are available:
  4725. \\{org-mode-map}"
  4726. ;; Get rid of Outline menus, they are not needed
  4727. ;; Need to do this here because define-derived-mode sets up
  4728. ;; the keymap so late. Still, it is a waste to call this each time
  4729. ;; we switch another buffer into org-mode.
  4730. (if (featurep 'xemacs)
  4731. (when (boundp 'outline-mode-menu-heading)
  4732. ;; Assume this is Greg's port, it uses easymenu
  4733. (easy-menu-remove outline-mode-menu-heading)
  4734. (easy-menu-remove outline-mode-menu-show)
  4735. (easy-menu-remove outline-mode-menu-hide))
  4736. (define-key org-mode-map [menu-bar headings] 'undefined)
  4737. (define-key org-mode-map [menu-bar hide] 'undefined)
  4738. (define-key org-mode-map [menu-bar show] 'undefined))
  4739. (org-load-modules-maybe)
  4740. (easy-menu-add org-org-menu)
  4741. (easy-menu-add org-tbl-menu)
  4742. (org-install-agenda-files-menu)
  4743. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4744. (add-to-invisibility-spec '(org-cwidth))
  4745. (add-to-invisibility-spec '(org-hide-block . t))
  4746. (when (featurep 'xemacs)
  4747. (org-set-local 'line-move-ignore-invisible t))
  4748. (org-set-local 'outline-regexp org-outline-regexp)
  4749. (org-set-local 'outline-level 'org-outline-level)
  4750. (setq bidi-paragraph-direction 'left-to-right)
  4751. (when (and org-ellipsis
  4752. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4753. (fboundp 'make-glyph-code))
  4754. (unless org-display-table
  4755. (setq org-display-table (make-display-table)))
  4756. (set-display-table-slot
  4757. org-display-table 4
  4758. (vconcat (mapcar
  4759. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4760. org-ellipsis)))
  4761. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4762. (setq buffer-display-table org-display-table))
  4763. (org-set-regexps-and-options-for-tags)
  4764. (org-set-regexps-and-options)
  4765. (org-set-font-lock-defaults)
  4766. (when (and org-tag-faces (not org-tags-special-faces-re))
  4767. ;; tag faces set outside customize.... force initialization.
  4768. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4769. ;; Calc embedded
  4770. (org-set-local 'calc-embedded-open-mode "# ")
  4771. ;; Modify a few syntax entries
  4772. (modify-syntax-entry ?@ "w")
  4773. (modify-syntax-entry ?\" "\"")
  4774. (modify-syntax-entry ?\\ "_")
  4775. (modify-syntax-entry ?~ "_")
  4776. (if org-startup-truncated (setq truncate-lines t))
  4777. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4778. (org-set-local 'font-lock-unfontify-region-function
  4779. 'org-unfontify-region)
  4780. ;; Activate before-change-function
  4781. (org-set-local 'org-table-may-need-update t)
  4782. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4783. 'local)
  4784. ;; Check for running clock before killing a buffer
  4785. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4786. ;; Initialize macros templates.
  4787. (org-macro-initialize-templates)
  4788. ;; Initialize radio targets.
  4789. (org-update-radio-target-regexp)
  4790. ;; Indentation.
  4791. (org-set-local 'indent-line-function 'org-indent-line)
  4792. (org-set-local 'indent-region-function 'org-indent-region)
  4793. ;; Filling and auto-filling.
  4794. (org-setup-filling)
  4795. ;; Comments.
  4796. (org-setup-comments-handling)
  4797. ;; Initialize cache.
  4798. (org-element-cache-reset)
  4799. ;; Beginning/end of defun
  4800. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4801. (org-set-local 'end-of-defun-function
  4802. (lambda ()
  4803. (if (not (org-at-heading-p))
  4804. (org-forward-element)
  4805. (org-forward-element)
  4806. (forward-char -1))))
  4807. ;; Next error for sparse trees
  4808. (org-set-local 'next-error-function 'org-occur-next-match)
  4809. ;; Make sure dependence stuff works reliably, even for users who set it
  4810. ;; too late :-(
  4811. (if org-enforce-todo-dependencies
  4812. (add-hook 'org-blocker-hook
  4813. 'org-block-todo-from-children-or-siblings-or-parent)
  4814. (remove-hook 'org-blocker-hook
  4815. 'org-block-todo-from-children-or-siblings-or-parent))
  4816. (if org-enforce-todo-checkbox-dependencies
  4817. (add-hook 'org-blocker-hook
  4818. 'org-block-todo-from-checkboxes)
  4819. (remove-hook 'org-blocker-hook
  4820. 'org-block-todo-from-checkboxes))
  4821. ;; Align options lines
  4822. (org-set-local
  4823. 'align-mode-rules-list
  4824. '((org-in-buffer-settings
  4825. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4826. (modes . '(org-mode)))))
  4827. ;; Imenu
  4828. (org-set-local 'imenu-create-index-function
  4829. 'org-imenu-get-tree)
  4830. ;; Make isearch reveal context
  4831. (if (or (featurep 'xemacs)
  4832. (not (boundp 'outline-isearch-open-invisible-function)))
  4833. ;; Emacs 21 and XEmacs make use of the hook
  4834. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4835. ;; Emacs 22 deals with this through a special variable
  4836. (org-set-local 'outline-isearch-open-invisible-function
  4837. (lambda (&rest ignore) (org-show-context 'isearch)))
  4838. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4839. ;; Setup the pcomplete hooks
  4840. (set (make-local-variable 'pcomplete-command-completion-function)
  4841. 'org-pcomplete-initial)
  4842. (set (make-local-variable 'pcomplete-command-name-function)
  4843. 'org-command-at-point)
  4844. (set (make-local-variable 'pcomplete-default-completion-function)
  4845. 'ignore)
  4846. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4847. 'org-parse-arguments)
  4848. (set (make-local-variable 'pcomplete-termination-string) "")
  4849. (when (>= emacs-major-version 23)
  4850. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4851. ;; If empty file that did not turn on org-mode automatically, make it to.
  4852. (if (and org-insert-mode-line-in-empty-file
  4853. (org-called-interactively-p 'any)
  4854. (= (point-min) (point-max)))
  4855. (insert "# -*- mode: org -*-\n\n"))
  4856. (unless org-inhibit-startup
  4857. (org-unmodified
  4858. (and org-startup-with-beamer-mode (org-beamer-mode))
  4859. (when org-startup-align-all-tables
  4860. (org-table-map-tables 'org-table-align 'quietly))
  4861. (when org-startup-with-inline-images
  4862. (org-display-inline-images))
  4863. (when org-startup-with-latex-preview
  4864. (org-preview-latex-fragment))
  4865. (unless org-inhibit-startup-visibility-stuff
  4866. (org-set-startup-visibility))))
  4867. ;; Try to set org-hide correctly
  4868. (let ((foreground (org-find-invisible-foreground)))
  4869. (if foreground
  4870. (set-face-foreground 'org-hide foreground))))
  4871. ;; Update `customize-package-emacs-version-alist'
  4872. (add-to-list 'customize-package-emacs-version-alist
  4873. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4874. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4875. ("8.2.6" . "24.4")))
  4876. (defvar org-mode-transpose-word-syntax-table
  4877. (let ((st (make-syntax-table)))
  4878. (mapc (lambda(c) (modify-syntax-entry
  4879. (string-to-char (car c)) "w p" st))
  4880. org-emphasis-alist)
  4881. st))
  4882. (when (fboundp 'abbrev-table-put)
  4883. (abbrev-table-put org-mode-abbrev-table
  4884. :parents (list text-mode-abbrev-table)))
  4885. (defsubst org-fix-ellipsis-at-bol ()
  4886. (save-excursion (goto-char (window-start)) (recenter 0)))
  4887. (defun org-find-invisible-foreground ()
  4888. (let ((candidates (remove
  4889. "unspecified-bg"
  4890. (nconc
  4891. (list (face-background 'default)
  4892. (face-background 'org-default))
  4893. (mapcar
  4894. (lambda (alist)
  4895. (when (boundp alist)
  4896. (cdr (assoc 'background-color (symbol-value alist)))))
  4897. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4898. (list (face-foreground 'org-hide))))))
  4899. (car (remove nil candidates))))
  4900. (defun org-current-time (&optional rounding-minutes past)
  4901. "Current time, possibly rounded to ROUNDING-MINUTES.
  4902. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4903. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4904. the rounding returns a past time."
  4905. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4906. (car org-time-stamp-rounding-minutes)))
  4907. (time (decode-time)) res)
  4908. (if (< r 1)
  4909. (current-time)
  4910. (setq res
  4911. (apply 'encode-time
  4912. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4913. (nthcdr 2 time))))
  4914. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4915. (seconds-to-time (- (org-float-time res) (* r 60)))
  4916. res))))
  4917. (defun org-today ()
  4918. "Return today date, considering `org-extend-today-until'."
  4919. (time-to-days
  4920. (time-subtract (current-time)
  4921. (list 0 (* 3600 org-extend-today-until) 0))))
  4922. ;;;; Font-Lock stuff, including the activators
  4923. (defvar org-mouse-map (make-sparse-keymap))
  4924. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4925. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4926. (when org-mouse-1-follows-link
  4927. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4928. (when org-tab-follows-link
  4929. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4930. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4931. (require 'font-lock)
  4932. (defconst org-non-link-chars "]\t\n\r<>")
  4933. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4934. "file+sys" "news" "shell" "elisp" "doi" "message"
  4935. "help"))
  4936. (defvar org-link-types-re nil
  4937. "Matches a link that has a url-like prefix like \"http:\"")
  4938. (defvar org-link-re-with-space nil
  4939. "Matches a link with spaces, optional angular brackets around it.")
  4940. (defvar org-link-re-with-space2 nil
  4941. "Matches a link with spaces, optional angular brackets around it.")
  4942. (defvar org-link-re-with-space3 nil
  4943. "Matches a link with spaces, only for internal part in bracket links.")
  4944. (defvar org-angle-link-re nil
  4945. "Matches link with angular brackets, spaces are allowed.")
  4946. (defvar org-plain-link-re nil
  4947. "Matches plain link, without spaces.")
  4948. (defvar org-bracket-link-regexp nil
  4949. "Matches a link in double brackets.")
  4950. (defvar org-bracket-link-analytic-regexp nil
  4951. "Regular expression used to analyze links.
  4952. Here is what the match groups contain after a match:
  4953. 1: http:
  4954. 2: http
  4955. 3: path
  4956. 4: [desc]
  4957. 5: desc")
  4958. (defvar org-bracket-link-analytic-regexp++ nil
  4959. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4960. (defvar org-any-link-re nil
  4961. "Regular expression matching any link.")
  4962. (defconst org-match-sexp-depth 3
  4963. "Number of stacked braces for sub/superscript matching.")
  4964. (defun org-create-multibrace-regexp (left right n)
  4965. "Create a regular expression which will match a balanced sexp.
  4966. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4967. as single character strings.
  4968. The regexp returned will match the entire expression including the
  4969. delimiters. It will also define a single group which contains the
  4970. match except for the outermost delimiters. The maximum depth of
  4971. stacked delimiters is N. Escaping delimiters is not possible."
  4972. (let* ((nothing (concat "[^" left right "]*?"))
  4973. (or "\\|")
  4974. (re nothing)
  4975. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4976. (while (> n 1)
  4977. (setq n (1- n)
  4978. re (concat re or next)
  4979. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4980. (concat left "\\(" re "\\)" right)))
  4981. (defconst org-match-substring-regexp
  4982. (concat
  4983. "\\(\\S-\\)\\([_^]\\)\\("
  4984. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4985. "\\|"
  4986. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4987. "\\|"
  4988. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  4989. "The regular expression matching a sub- or superscript.")
  4990. (defconst org-match-substring-with-braces-regexp
  4991. (concat
  4992. "\\(\\S-\\)\\([_^]\\)"
  4993. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  4994. "The regular expression matching a sub- or superscript, forcing braces.")
  4995. (defun org-make-link-regexps ()
  4996. "Update the link regular expressions.
  4997. This should be called after the variable `org-link-types' has changed."
  4998. (let ((types-re (regexp-opt org-link-types t)))
  4999. (setq org-link-types-re
  5000. (concat "\\`" types-re ":")
  5001. org-link-re-with-space
  5002. (concat "<?" types-re ":"
  5003. "\\([^" org-non-link-chars " ]"
  5004. "[^" org-non-link-chars "]*"
  5005. "[^" org-non-link-chars " ]\\)>?")
  5006. org-link-re-with-space2
  5007. (concat "<?" types-re ":"
  5008. "\\([^" org-non-link-chars " ]"
  5009. "[^\t\n\r]*"
  5010. "[^" org-non-link-chars " ]\\)>?")
  5011. org-link-re-with-space3
  5012. (concat "<?" types-re ":"
  5013. "\\([^" org-non-link-chars " ]"
  5014. "[^\t\n\r]*\\)")
  5015. org-angle-link-re
  5016. (concat "<" types-re ":"
  5017. "\\([^" org-non-link-chars " ]"
  5018. "[^" org-non-link-chars "]*"
  5019. "\\)>")
  5020. org-plain-link-re
  5021. (concat
  5022. "\\<" types-re ":"
  5023. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  5024. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5025. org-bracket-link-regexp
  5026. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5027. org-bracket-link-analytic-regexp
  5028. (concat
  5029. "\\[\\["
  5030. "\\(" types-re ":\\)?"
  5031. "\\([^]]+\\)"
  5032. "\\]"
  5033. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5034. "\\]")
  5035. org-bracket-link-analytic-regexp++
  5036. (concat
  5037. "\\[\\["
  5038. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5039. "\\([^]]+\\)"
  5040. "\\]"
  5041. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5042. "\\]")
  5043. org-any-link-re
  5044. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5045. org-angle-link-re "\\)\\|\\("
  5046. org-plain-link-re "\\)"))))
  5047. (org-make-link-regexps)
  5048. (defvar org-emph-face nil)
  5049. (defun org-do-emphasis-faces (limit)
  5050. "Run through the buffer and emphasize strings."
  5051. (let (rtn a)
  5052. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5053. (let* ((border (char-after (match-beginning 3)))
  5054. (bre (regexp-quote (char-to-string border))))
  5055. (if (and (not (= border (char-after (match-beginning 4))))
  5056. (not (save-match-data
  5057. (string-match (concat bre ".*" bre)
  5058. (replace-regexp-in-string
  5059. "\n" " "
  5060. (substring (match-string 2) 1 -1))))))
  5061. (progn
  5062. (setq rtn t)
  5063. (setq a (assoc (match-string 3) org-emphasis-alist))
  5064. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5065. 'face
  5066. (nth 1 a))
  5067. (and (nth 2 a)
  5068. (org-remove-flyspell-overlays-in
  5069. (match-beginning 0) (match-end 0)))
  5070. (add-text-properties (match-beginning 2) (match-end 2)
  5071. '(font-lock-multiline t org-emphasis t))
  5072. (when org-hide-emphasis-markers
  5073. (add-text-properties (match-end 4) (match-beginning 5)
  5074. '(invisible org-link))
  5075. (add-text-properties (match-beginning 3) (match-end 3)
  5076. '(invisible org-link))))))
  5077. (goto-char (1+ (match-beginning 0))))
  5078. rtn))
  5079. (defun org-emphasize (&optional char)
  5080. "Insert or change an emphasis, i.e. a font like bold or italic.
  5081. If there is an active region, change that region to a new emphasis.
  5082. If there is no region, just insert the marker characters and position
  5083. the cursor between them.
  5084. CHAR should be the marker character. If it is a space, it means to
  5085. remove the emphasis of the selected region.
  5086. If CHAR is not given (for example in an interactive call) it will be
  5087. prompted for."
  5088. (interactive)
  5089. (let ((erc org-emphasis-regexp-components)
  5090. (prompt "")
  5091. (string "") beg end move c s)
  5092. (if (org-region-active-p)
  5093. (setq beg (region-beginning) end (region-end)
  5094. string (buffer-substring beg end))
  5095. (setq move t))
  5096. (unless char
  5097. (message "Emphasis marker or tag: [%s]"
  5098. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5099. (setq char (read-char-exclusive)))
  5100. (if (equal char ?\ )
  5101. (setq s "" move nil)
  5102. (unless (assoc (char-to-string char) org-emphasis-alist)
  5103. (user-error "No such emphasis marker: \"%c\"" char))
  5104. (setq s (char-to-string char)))
  5105. (while (and (> (length string) 1)
  5106. (equal (substring string 0 1) (substring string -1))
  5107. (assoc (substring string 0 1) org-emphasis-alist))
  5108. (setq string (substring string 1 -1)))
  5109. (setq string (concat s string s))
  5110. (if beg (delete-region beg end))
  5111. (unless (or (bolp)
  5112. (string-match (concat "[" (nth 0 erc) "\n]")
  5113. (char-to-string (char-before (point)))))
  5114. (insert " "))
  5115. (unless (or (eobp)
  5116. (string-match (concat "[" (nth 1 erc) "\n]")
  5117. (char-to-string (char-after (point)))))
  5118. (insert " ") (backward-char 1))
  5119. (insert string)
  5120. (and move (backward-char 1))))
  5121. (defconst org-nonsticky-props
  5122. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5123. (defsubst org-rear-nonsticky-at (pos)
  5124. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5125. (defun org-activate-plain-links (limit)
  5126. "Run through the buffer and add overlays to links."
  5127. (let (f hl)
  5128. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5129. (not (org-in-src-block-p)))
  5130. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5131. (setq f (get-text-property (match-beginning 0) 'face))
  5132. (setq hl (org-match-string-no-properties 0))
  5133. (if (or (eq f 'org-tag)
  5134. (and (listp f) (memq 'org-tag f)))
  5135. nil
  5136. (add-text-properties (match-beginning 0) (match-end 0)
  5137. (list 'mouse-face 'highlight
  5138. 'face 'org-link
  5139. 'htmlize-link `(:uri ,hl)
  5140. 'keymap org-mouse-map))
  5141. (org-rear-nonsticky-at (match-end 0)))
  5142. t)))
  5143. (defun org-activate-code (limit)
  5144. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5145. (progn
  5146. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5147. (remove-text-properties (match-beginning 0) (match-end 0)
  5148. '(display t invisible t intangible t))
  5149. t)))
  5150. (defcustom org-src-fontify-natively t
  5151. "When non-nil, fontify code in code blocks."
  5152. :type 'boolean
  5153. :version "24.4"
  5154. :package-version '(Org . "8.3")
  5155. :group 'org-appearance
  5156. :group 'org-babel)
  5157. (defcustom org-allow-promoting-top-level-subtree nil
  5158. "When non-nil, allow promoting a top level subtree.
  5159. The leading star of the top level headline will be replaced
  5160. by a #."
  5161. :type 'boolean
  5162. :version "24.1"
  5163. :group 'org-appearance)
  5164. (defun org-fontify-meta-lines-and-blocks (limit)
  5165. (condition-case nil
  5166. (org-fontify-meta-lines-and-blocks-1 limit)
  5167. (error (message "org-mode fontification error"))))
  5168. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5169. "Fontify #+ lines and blocks."
  5170. (let ((case-fold-search t))
  5171. (if (re-search-forward
  5172. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5173. limit t)
  5174. (let ((beg (match-beginning 0))
  5175. (block-start (match-end 0))
  5176. (block-end nil)
  5177. (lang (match-string 7))
  5178. (beg1 (line-beginning-position 2))
  5179. (dc1 (downcase (match-string 2)))
  5180. (dc3 (downcase (match-string 3)))
  5181. end end1 quoting block-type ovl)
  5182. (cond
  5183. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5184. ;; a single line of backend-specific content
  5185. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5186. (remove-text-properties (match-beginning 0) (match-end 0)
  5187. '(display t invisible t intangible t))
  5188. (add-text-properties (match-beginning 1) (match-end 3)
  5189. '(font-lock-fontified t face org-meta-line))
  5190. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5191. '(font-lock-fontified t face org-block))
  5192. ; for backend-specific code
  5193. t)
  5194. ((and (match-end 4) (equal dc3 "+begin"))
  5195. ;; Truly a block
  5196. (setq block-type (downcase (match-string 5))
  5197. quoting (member block-type org-protecting-blocks))
  5198. (when (re-search-forward
  5199. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5200. nil t) ;; on purpose, we look further than LIMIT
  5201. (setq end (min (point-max) (match-end 0))
  5202. end1 (min (point-max) (1- (match-beginning 0))))
  5203. (setq block-end (match-beginning 0))
  5204. (when quoting
  5205. (org-remove-flyspell-overlays-in beg1 end1)
  5206. (remove-text-properties beg end
  5207. '(display t invisible t intangible t)))
  5208. (add-text-properties
  5209. beg end '(font-lock-fontified t font-lock-multiline t))
  5210. (add-text-properties beg beg1 '(face org-meta-line))
  5211. (org-remove-flyspell-overlays-in beg beg1)
  5212. (add-text-properties ; For end_src
  5213. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5214. (org-remove-flyspell-overlays-in end1 end)
  5215. (cond
  5216. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5217. (org-src-font-lock-fontify-block lang block-start block-end)
  5218. ;; remove old background overlays
  5219. (mapc (lambda (ov)
  5220. (if (eq (overlay-get ov 'face) 'org-block-background)
  5221. (delete-overlay ov)))
  5222. (overlays-at (/ (+ beg1 block-end) 2)))
  5223. ;; add a background overlay
  5224. (setq ovl (make-overlay beg1 block-end))
  5225. (overlay-put ovl 'face 'org-block-background)
  5226. (overlay-put ovl 'evaporate t)) ; make it go away when empty
  5227. (quoting
  5228. (add-text-properties beg1 (min (point-max) (1+ end1))
  5229. '(face org-block))) ; end of source block
  5230. ((not org-fontify-quote-and-verse-blocks))
  5231. ((string= block-type "quote")
  5232. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5233. ((string= block-type "verse")
  5234. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5235. (add-text-properties beg beg1 '(face org-block-begin-line))
  5236. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5237. '(face org-block-end-line))
  5238. t))
  5239. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5240. (org-remove-flyspell-overlays-in
  5241. (match-beginning 0)
  5242. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5243. (add-text-properties
  5244. beg (match-end 3)
  5245. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5246. '(font-lock-fontified t invisible t)
  5247. '(font-lock-fontified t face org-document-info-keyword)))
  5248. (add-text-properties
  5249. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5250. (if (string-equal dc1 "+title:")
  5251. '(font-lock-fontified t face org-document-title)
  5252. '(font-lock-fontified t face org-document-info))))
  5253. ((or (equal dc1 "+results")
  5254. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5255. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5256. "+call:" "+header:" "+headers:" "+name:"))
  5257. (and (match-end 4) (equal dc3 "+attr")))
  5258. (org-remove-flyspell-overlays-in
  5259. (match-beginning 0)
  5260. (if (equal "+caption:" dc1) (match-end 2) (match-end 0)))
  5261. (add-text-properties
  5262. beg (match-end 0)
  5263. '(font-lock-fontified t face org-meta-line))
  5264. t)
  5265. ((member dc3 '(" " ""))
  5266. (org-remove-flyspell-overlays-in beg (match-end 0))
  5267. (add-text-properties
  5268. beg (match-end 0)
  5269. '(font-lock-fontified t face font-lock-comment-face)))
  5270. (t ;; just any other in-buffer setting, but not indented
  5271. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5272. (add-text-properties
  5273. beg (match-end 0)
  5274. '(font-lock-fontified t face org-meta-line))
  5275. t))))))
  5276. (defun org-fontify-drawers (limit)
  5277. "Fontify drawers."
  5278. (when (re-search-forward org-drawer-regexp limit t)
  5279. (add-text-properties
  5280. (match-beginning 0) (match-end 0)
  5281. '(font-lock-fontified t face org-special-keyword))
  5282. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5283. t))
  5284. (defun org-fontify-macros (limit)
  5285. "Fontify macros."
  5286. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5287. (add-text-properties
  5288. (match-beginning 0) (match-end 0)
  5289. '(font-lock-fontified t face org-macro))
  5290. (when org-hide-macro-markers
  5291. (add-text-properties (match-end 2) (match-beginning 2)
  5292. '(invisible t))
  5293. (add-text-properties (match-beginning 1) (match-end 1)
  5294. '(invisible t)))
  5295. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5296. t))
  5297. (defun org-activate-angle-links (limit)
  5298. "Run through the buffer and add overlays to links."
  5299. (if (and (re-search-forward org-angle-link-re limit t)
  5300. (not (org-in-src-block-p)))
  5301. (progn
  5302. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5303. (add-text-properties (match-beginning 0) (match-end 0)
  5304. (list 'mouse-face 'highlight
  5305. 'keymap org-mouse-map))
  5306. (org-rear-nonsticky-at (match-end 0))
  5307. t)))
  5308. (defun org-activate-footnote-links (limit)
  5309. "Run through the buffer and add overlays to footnotes."
  5310. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5311. (when fn
  5312. (let* ((beg (nth 1 fn))
  5313. (end (nth 2 fn))
  5314. (label (car fn))
  5315. (referencep (/= (line-beginning-position) beg)))
  5316. (when (and referencep (nth 3 fn))
  5317. (save-excursion
  5318. (goto-char beg)
  5319. (search-forward (or label "fn:"))
  5320. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5321. (add-text-properties beg end
  5322. (list 'mouse-face 'highlight
  5323. 'keymap org-mouse-map
  5324. 'help-echo
  5325. (if referencep "Footnote reference"
  5326. "Footnote definition")
  5327. 'font-lock-fontified t
  5328. 'font-lock-multiline t
  5329. 'face 'org-footnote))))))
  5330. (defun org-activate-bracket-links (limit)
  5331. "Run through the buffer and add overlays to bracketed links."
  5332. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5333. (not (org-in-src-block-p)))
  5334. (let* ((hl (org-match-string-no-properties 1))
  5335. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5336. (ip (org-maybe-intangible
  5337. (list 'invisible 'org-link
  5338. 'keymap org-mouse-map 'mouse-face 'highlight
  5339. 'font-lock-multiline t 'help-echo help
  5340. 'htmlize-link `(:uri ,hl))))
  5341. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5342. 'font-lock-multiline t 'help-echo help
  5343. 'htmlize-link `(:uri ,hl))))
  5344. ;; We need to remove the invisible property here. Table narrowing
  5345. ;; may have made some of this invisible.
  5346. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5347. (remove-text-properties (match-beginning 0) (match-end 0)
  5348. '(invisible nil))
  5349. (if (match-end 3)
  5350. (progn
  5351. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5352. (org-rear-nonsticky-at (match-beginning 3))
  5353. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5354. (org-rear-nonsticky-at (match-end 3))
  5355. (add-text-properties (match-end 3) (match-end 0) ip)
  5356. (org-rear-nonsticky-at (match-end 0)))
  5357. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5358. (org-rear-nonsticky-at (match-beginning 1))
  5359. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5360. (org-rear-nonsticky-at (match-end 1))
  5361. (add-text-properties (match-end 1) (match-end 0) ip)
  5362. (org-rear-nonsticky-at (match-end 0)))
  5363. t)))
  5364. (defun org-activate-dates (limit)
  5365. "Run through the buffer and add overlays to dates."
  5366. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5367. (not (equal (char-before (match-beginning 0)) 91)))
  5368. (progn
  5369. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5370. (add-text-properties (match-beginning 0) (match-end 0)
  5371. (list 'mouse-face 'highlight
  5372. 'keymap org-mouse-map))
  5373. (org-rear-nonsticky-at (match-end 0))
  5374. (when org-display-custom-times
  5375. (if (match-end 3)
  5376. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5377. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5378. t)))
  5379. (defvar org-target-link-regexp nil
  5380. "Regular expression matching radio targets in plain text.")
  5381. (make-variable-buffer-local 'org-target-link-regexp)
  5382. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5383. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5384. border border border))
  5385. "Regular expression matching a link target.")
  5386. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5387. "Regular expression matching a radio target.")
  5388. (defconst org-any-target-regexp
  5389. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5390. "Regular expression matching any target.")
  5391. (defun org-activate-target-links (limit)
  5392. "Run through the buffer and add overlays to target matches."
  5393. (when org-target-link-regexp
  5394. (let ((case-fold-search t))
  5395. (if (re-search-forward org-target-link-regexp limit t)
  5396. (progn
  5397. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5398. (add-text-properties (match-beginning 1) (match-end 1)
  5399. (list 'mouse-face 'highlight
  5400. 'keymap org-mouse-map
  5401. 'help-echo "Radio target link"
  5402. 'org-linked-text t))
  5403. (org-rear-nonsticky-at (match-end 1))
  5404. t)))))
  5405. (defun org-update-radio-target-regexp ()
  5406. "Find all radio targets in this file and update the regular expression.
  5407. Also refresh fontification if needed."
  5408. (interactive)
  5409. (let ((old-regexp org-target-link-regexp)
  5410. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5411. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5412. (targets
  5413. (org-with-wide-buffer
  5414. (goto-char (point-min))
  5415. (let (rtn)
  5416. (while (re-search-forward org-radio-target-regexp nil t)
  5417. ;; Make sure point is really within the object.
  5418. (backward-char)
  5419. (let ((obj (org-element-context)))
  5420. (when (eq (org-element-type obj) 'radio-target)
  5421. (add-to-list 'rtn (org-element-property :value obj)))))
  5422. rtn))))
  5423. (setq org-target-link-regexp
  5424. (and targets
  5425. (concat before-re
  5426. (mapconcat
  5427. #'(lambda (x)
  5428. (replace-regexp-in-string
  5429. " +" "\\s-+" (regexp-quote x) t t))
  5430. targets
  5431. "\\|")
  5432. after-re)))
  5433. (unless (equal old-regexp org-target-link-regexp)
  5434. ;; Clean-up cache.
  5435. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5436. ((not org-target-link-regexp) old-regexp)
  5437. (t
  5438. (concat before-re
  5439. (mapconcat
  5440. #'(lambda (re)
  5441. (substring re (length before-re)
  5442. (- (length after-re))))
  5443. (list old-regexp org-target-link-regexp)
  5444. "\\|")
  5445. after-re)))))
  5446. (org-with-wide-buffer
  5447. (goto-char (point-min))
  5448. (while (re-search-forward regexp nil t)
  5449. (org-element-cache-refresh (match-beginning 1)))))
  5450. ;; Re fontify buffer.
  5451. (when (memq 'radio org-highlight-links)
  5452. (org-restart-font-lock)))))
  5453. (defun org-hide-wide-columns (limit)
  5454. (let (s e)
  5455. (setq s (text-property-any (point) (or limit (point-max))
  5456. 'org-cwidth t))
  5457. (when s
  5458. (setq e (next-single-property-change s 'org-cwidth))
  5459. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5460. (goto-char e)
  5461. t)))
  5462. (defvar org-latex-and-related-regexp nil
  5463. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5464. (defun org-compute-latex-and-related-regexp ()
  5465. "Compute regular expression for LaTeX, entities and sub/superscript.
  5466. Result depends on variable `org-highlight-latex-and-related'."
  5467. (org-set-local
  5468. 'org-latex-and-related-regexp
  5469. (let* ((re-sub
  5470. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5471. ((eq org-use-sub-superscripts '{})
  5472. (list org-match-substring-with-braces-regexp))
  5473. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5474. (re-latex
  5475. (when (memq 'latex org-highlight-latex-and-related)
  5476. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5477. (delq nil
  5478. (mapcar (lambda (x)
  5479. (and (member (car x) matchers) (nth 1 x)))
  5480. org-latex-regexps)))))
  5481. (re-entities
  5482. (when (memq 'entities org-highlight-latex-and-related)
  5483. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5484. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5485. (defun org-do-latex-and-related (limit)
  5486. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5487. LIMIT bounds the search for syntax to highlight. Stop at first
  5488. highlighted object, if any. Return t if some highlighting was
  5489. done, nil otherwise."
  5490. (when (org-string-nw-p org-latex-and-related-regexp)
  5491. (catch 'found
  5492. (while (re-search-forward org-latex-and-related-regexp limit t)
  5493. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5494. 'face))
  5495. '(org-code org-verbatim underline))
  5496. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5497. '(?_ ?^))
  5498. 1
  5499. 0)))
  5500. (font-lock-prepend-text-property
  5501. (+ offset (match-beginning 0)) (match-end 0)
  5502. 'face 'org-latex-and-related)
  5503. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5504. '(font-lock-multiline t)))
  5505. (throw 'found t)))
  5506. nil)))
  5507. (defun org-restart-font-lock ()
  5508. "Restart `font-lock-mode', to force refontification."
  5509. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5510. (font-lock-mode -1)
  5511. (font-lock-mode 1)))
  5512. (defun org-activate-tags (limit)
  5513. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5514. (progn
  5515. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5516. (add-text-properties (match-beginning 1) (match-end 1)
  5517. (list 'mouse-face 'highlight
  5518. 'keymap org-mouse-map))
  5519. (org-rear-nonsticky-at (match-end 1))
  5520. t)))
  5521. (defun org-outline-level ()
  5522. "Compute the outline level of the heading at point.
  5523. If this is called at a normal headline, the level is the number of stars.
  5524. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5525. (save-excursion
  5526. (if (not (condition-case nil
  5527. (org-back-to-heading t)
  5528. (error nil)))
  5529. 0
  5530. (looking-at org-outline-regexp)
  5531. (1- (- (match-end 0) (match-beginning 0))))))
  5532. (defvar org-font-lock-keywords nil)
  5533. (defsubst org-re-property (property &optional literal)
  5534. "Return a regexp matching a PROPERTY line.
  5535. Match group 3 will be set to the value if it exists."
  5536. (concat "^\\(?4:[ \t]*\\)\\(?1::\\(?2:"
  5537. (if literal property (regexp-quote property))
  5538. "\\):\\)[ \t]+\\(?3:[^ \t\r\n].*?\\)\\(?5:[ \t]*\\)$"))
  5539. (defconst org-property-re
  5540. (org-re-property ".*?" 'literal)
  5541. "Regular expression matching a property line.
  5542. There are four matching groups:
  5543. 1: :PROPKEY: including the leading and trailing colon,
  5544. 2: PROPKEY without the leading and trailing colon,
  5545. 3: PROPVAL without leading or trailing spaces,
  5546. 4: the indentation of the current line,
  5547. 5: trailing whitespace.")
  5548. (defvar org-font-lock-hook nil
  5549. "Functions to be called for special font lock stuff.")
  5550. (defvar org-font-lock-set-keywords-hook nil
  5551. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5552. This is called after `org-font-lock-extra-keywords' is defined, but before
  5553. it is installed to be used by font lock. This can be useful if something
  5554. needs to be inserted at a specific position in the font-lock sequence.")
  5555. (defun org-font-lock-hook (limit)
  5556. "Run `org-font-lock-hook' within LIMIT."
  5557. (run-hook-with-args 'org-font-lock-hook limit))
  5558. (defun org-set-font-lock-defaults ()
  5559. "Set font lock defaults for the current buffer."
  5560. (let* ((em org-fontify-emphasized-text)
  5561. (lk org-highlight-links)
  5562. (org-font-lock-extra-keywords
  5563. (list
  5564. ;; Call the hook
  5565. '(org-font-lock-hook)
  5566. ;; Headlines
  5567. `(,(if org-fontify-whole-heading-line
  5568. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5569. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5570. (1 (org-get-level-face 1))
  5571. (2 (org-get-level-face 2))
  5572. (3 (org-get-level-face 3)))
  5573. ;; Table lines
  5574. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5575. (1 'org-table t))
  5576. ;; Table internals
  5577. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5578. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5579. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5580. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5581. ;; Drawers
  5582. '(org-fontify-drawers)
  5583. ;; Properties
  5584. (list org-property-re
  5585. '(1 'org-special-keyword t)
  5586. '(3 'org-property-value t))
  5587. ;; Links
  5588. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5589. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5590. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5591. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5592. (if (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5593. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5594. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5595. ;; Targets.
  5596. (list org-any-target-regexp '(0 'org-target t))
  5597. ;; Diary sexps.
  5598. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5599. ;; Macro
  5600. '(org-fontify-macros)
  5601. '(org-hide-wide-columns (0 nil append))
  5602. ;; TODO keyword
  5603. (list (format org-heading-keyword-regexp-format
  5604. org-todo-regexp)
  5605. '(2 (org-get-todo-face 2) t))
  5606. ;; DONE
  5607. (if org-fontify-done-headline
  5608. (list (format org-heading-keyword-regexp-format
  5609. (concat
  5610. "\\(?:"
  5611. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5612. "\\)"))
  5613. '(2 'org-headline-done t))
  5614. nil)
  5615. ;; Priorities
  5616. '(org-font-lock-add-priority-faces)
  5617. ;; Tags
  5618. '(org-font-lock-add-tag-faces)
  5619. ;; Tags groups
  5620. (if (and org-group-tags org-tag-groups-alist)
  5621. (list (concat org-outline-regexp-bol ".+\\(:"
  5622. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5623. ":\\).*$")
  5624. '(1 'org-tag-group prepend)))
  5625. ;; Special keywords
  5626. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5627. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5628. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5629. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5630. ;; Emphasis
  5631. (if em
  5632. (if (featurep 'xemacs)
  5633. '(org-do-emphasis-faces (0 nil append))
  5634. '(org-do-emphasis-faces)))
  5635. ;; Checkboxes
  5636. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5637. 1 'org-checkbox prepend)
  5638. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5639. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5640. (0 (org-get-checkbox-statistics-face) t)))
  5641. ;; Description list items
  5642. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5643. 1 'org-list-dt prepend)
  5644. ;; ARCHIVEd headings
  5645. (list (concat
  5646. org-outline-regexp-bol
  5647. "\\(.*:" org-archive-tag ":.*\\)")
  5648. '(1 'org-archived prepend))
  5649. ;; Specials
  5650. '(org-do-latex-and-related)
  5651. '(org-fontify-entities)
  5652. '(org-raise-scripts)
  5653. ;; Code
  5654. '(org-activate-code (1 'org-code t))
  5655. ;; COMMENT
  5656. (list (format
  5657. "^\\*\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5658. org-todo-regexp
  5659. org-comment-string)
  5660. '(9 'org-special-keyword t))
  5661. ;; Blocks and meta lines
  5662. '(org-fontify-meta-lines-and-blocks))))
  5663. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5664. (run-hooks 'org-font-lock-set-keywords-hook)
  5665. ;; Now set the full font-lock-keywords
  5666. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5667. (org-set-local 'font-lock-defaults
  5668. '(org-font-lock-keywords t nil nil backward-paragraph))
  5669. (kill-local-variable 'font-lock-keywords) nil))
  5670. (defun org-toggle-pretty-entities ()
  5671. "Toggle the composition display of entities as UTF8 characters."
  5672. (interactive)
  5673. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5674. (org-restart-font-lock)
  5675. (if org-pretty-entities
  5676. (message "Entities are now displayed as UTF8 characters")
  5677. (save-restriction
  5678. (widen)
  5679. (org-decompose-region (point-min) (point-max))
  5680. (message "Entities are now displayed as plain text"))))
  5681. (defvar org-custom-properties-overlays nil
  5682. "List of overlays used for custom properties.")
  5683. (make-variable-buffer-local 'org-custom-properties-overlays)
  5684. (defun org-toggle-custom-properties-visibility ()
  5685. "Display or hide properties in `org-custom-properties'."
  5686. (interactive)
  5687. (if org-custom-properties-overlays
  5688. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5689. (setq org-custom-properties-overlays nil))
  5690. (unless (not org-custom-properties)
  5691. (save-excursion
  5692. (save-restriction
  5693. (widen)
  5694. (goto-char (point-min))
  5695. (while (re-search-forward org-property-re nil t)
  5696. (mapc (lambda(p)
  5697. (when (equal p (substring (match-string 1) 1 -1))
  5698. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5699. (overlay-put o 'invisible t)
  5700. (overlay-put o 'org-custom-property t)
  5701. (push o org-custom-properties-overlays))))
  5702. org-custom-properties)))))))
  5703. (defun org-fontify-entities (limit)
  5704. "Find an entity to fontify."
  5705. (let (ee)
  5706. (when org-pretty-entities
  5707. (catch 'match
  5708. (while (re-search-forward
  5709. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5710. limit t)
  5711. (if (and (not (org-at-comment-p))
  5712. (setq ee (org-entity-get (match-string 1)))
  5713. (= (length (nth 6 ee)) 1))
  5714. (let*
  5715. ((end (if (equal (match-string 2) "{}")
  5716. (match-end 2)
  5717. (match-end 1))))
  5718. (add-text-properties
  5719. (match-beginning 0) end
  5720. (list 'font-lock-fontified t))
  5721. (compose-region (match-beginning 0) end
  5722. (nth 6 ee) nil)
  5723. (backward-char 1)
  5724. (throw 'match t))))
  5725. nil))))
  5726. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5727. "Fontify string S like in Org-mode."
  5728. (with-temp-buffer
  5729. (insert s)
  5730. (let ((org-odd-levels-only odd-levels))
  5731. (org-mode)
  5732. (font-lock-fontify-buffer)
  5733. (buffer-string))))
  5734. (defvar org-m nil)
  5735. (defvar org-l nil)
  5736. (defvar org-f nil)
  5737. (defun org-get-level-face (n)
  5738. "Get the right face for match N in font-lock matching of headlines."
  5739. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5740. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5741. (if org-cycle-level-faces
  5742. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5743. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5744. (cond
  5745. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5746. ((eq n 2) org-f)
  5747. (t (if org-level-color-stars-only nil org-f))))
  5748. (defun org-get-todo-face (kwd)
  5749. "Get the right face for a TODO keyword KWD.
  5750. If KWD is a number, get the corresponding match group."
  5751. (if (numberp kwd) (setq kwd (match-string kwd)))
  5752. (or (org-face-from-face-or-color
  5753. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5754. (and (member kwd org-done-keywords) 'org-done)
  5755. 'org-todo))
  5756. (defun org-face-from-face-or-color (context inherit face-or-color)
  5757. "Create a face list that inherits INHERIT, but sets the foreground color.
  5758. When FACE-OR-COLOR is not a string, just return it."
  5759. (if (stringp face-or-color)
  5760. (list :inherit inherit
  5761. (cdr (assoc context org-faces-easy-properties))
  5762. face-or-color)
  5763. face-or-color))
  5764. (defun org-font-lock-add-tag-faces (limit)
  5765. "Add the special tag faces."
  5766. (when (and org-tag-faces org-tags-special-faces-re)
  5767. (while (re-search-forward org-tags-special-faces-re limit t)
  5768. (add-text-properties (match-beginning 1) (match-end 1)
  5769. (list 'face (org-get-tag-face 1)
  5770. 'font-lock-fontified t))
  5771. (backward-char 1))))
  5772. (defun org-font-lock-add-priority-faces (limit)
  5773. "Add the special priority faces."
  5774. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5775. (when (save-match-data (org-at-heading-p))
  5776. (add-text-properties
  5777. (match-beginning 0) (match-end 0)
  5778. (list 'face (or (org-face-from-face-or-color
  5779. 'priority 'org-priority
  5780. (cdr (assoc (char-after (match-beginning 1))
  5781. org-priority-faces)))
  5782. 'org-priority)
  5783. 'font-lock-fontified t)))))
  5784. (defun org-get-tag-face (kwd)
  5785. "Get the right face for a TODO keyword KWD.
  5786. If KWD is a number, get the corresponding match group."
  5787. (if (numberp kwd) (setq kwd (match-string kwd)))
  5788. (or (org-face-from-face-or-color
  5789. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5790. 'org-tag))
  5791. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5792. "Remove fontification and activation overlays from links."
  5793. (font-lock-default-unfontify-region beg end)
  5794. (let* ((buffer-undo-list t)
  5795. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5796. (inhibit-modification-hooks t)
  5797. deactivate-mark buffer-file-name buffer-file-truename)
  5798. (org-decompose-region beg end)
  5799. (remove-text-properties beg end
  5800. '(mouse-face t keymap t org-linked-text t
  5801. invisible t intangible t
  5802. org-emphasis t))
  5803. (org-remove-font-lock-display-properties beg end)))
  5804. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5805. ((raise 0.3) (height 0.7))
  5806. ((raise -0.5))
  5807. ((raise 0.5)))
  5808. "Display properties for showing superscripts and subscripts.")
  5809. (defun org-remove-font-lock-display-properties (beg end)
  5810. "Remove specific display properties that have been added by font lock.
  5811. The will remove the raise properties that are used to show superscripts
  5812. and subscripts."
  5813. (let (next prop)
  5814. (while (< beg end)
  5815. (setq next (next-single-property-change beg 'display nil end)
  5816. prop (get-text-property beg 'display))
  5817. (if (member prop org-script-display)
  5818. (put-text-property beg next 'display nil))
  5819. (setq beg next))))
  5820. (defun org-raise-scripts (limit)
  5821. "Add raise properties to sub/superscripts."
  5822. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5823. (if (re-search-forward
  5824. (if (eq org-use-sub-superscripts t)
  5825. org-match-substring-regexp
  5826. org-match-substring-with-braces-regexp)
  5827. limit t)
  5828. (let* ((pos (point)) table-p comment-p
  5829. (mpos (match-beginning 3))
  5830. (emph-p (get-text-property mpos 'org-emphasis))
  5831. (link-p (get-text-property mpos 'mouse-face))
  5832. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5833. (goto-char (point-at-bol))
  5834. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5835. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5836. (goto-char pos)
  5837. ;; Handle a_b^c
  5838. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5839. (if (or comment-p emph-p link-p keyw-p)
  5840. t
  5841. (put-text-property (match-beginning 3) (match-end 0)
  5842. 'display
  5843. (if (equal (char-after (match-beginning 2)) ?^)
  5844. (nth (if table-p 3 1) org-script-display)
  5845. (nth (if table-p 2 0) org-script-display)))
  5846. (add-text-properties (match-beginning 2) (match-end 2)
  5847. (list 'invisible t
  5848. 'org-dwidth t 'org-dwidth-n 1))
  5849. (if (and (eq (char-after (match-beginning 3)) ?{)
  5850. (eq (char-before (match-end 3)) ?}))
  5851. (progn
  5852. (add-text-properties
  5853. (match-beginning 3) (1+ (match-beginning 3))
  5854. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5855. (add-text-properties
  5856. (1- (match-end 3)) (match-end 3)
  5857. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5858. t)))))
  5859. ;;;; Visibility cycling, including org-goto and indirect buffer
  5860. ;;; Cycling
  5861. (defvar org-cycle-global-status nil)
  5862. (make-variable-buffer-local 'org-cycle-global-status)
  5863. (put 'org-cycle-global-status 'org-state t)
  5864. (defvar org-cycle-subtree-status nil)
  5865. (make-variable-buffer-local 'org-cycle-subtree-status)
  5866. (put 'org-cycle-subtree-status 'org-state t)
  5867. (defvar org-inlinetask-min-level)
  5868. (defun org-unlogged-message (&rest args)
  5869. "Display a message, but avoid logging it in the *Messages* buffer."
  5870. (let ((message-log-max nil))
  5871. (apply 'message args)))
  5872. ;;;###autoload
  5873. (defun org-cycle (&optional arg)
  5874. "TAB-action and visibility cycling for Org-mode.
  5875. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5876. is outline visibility cycling, but it also invokes other actions
  5877. in special contexts.
  5878. - When this function is called with a prefix argument, rotate the entire
  5879. buffer through 3 states (global cycling)
  5880. 1. OVERVIEW: Show only top-level headlines.
  5881. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5882. 3. SHOW ALL: Show everything.
  5883. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5884. determined by the variable `org-startup-folded', and by any VISIBILITY
  5885. properties in the buffer.
  5886. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5887. including any drawers.
  5888. - When inside a table, re-align the table and move to the next field.
  5889. - When point is at the beginning of a headline, rotate the subtree started
  5890. by this line through 3 different states (local cycling)
  5891. 1. FOLDED: Only the main headline is shown.
  5892. 2. CHILDREN: The main headline and the direct children are shown.
  5893. From this state, you can move to one of the children
  5894. and zoom in further.
  5895. 3. SUBTREE: Show the entire subtree, including body text.
  5896. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5897. - When point is at the beginning of an empty headline and the variable
  5898. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5899. of the headline by demoting and promoting it to likely levels. This
  5900. speeds up creation document structure by pressing TAB once or several
  5901. times right after creating a new headline.
  5902. - When there is a numeric prefix, go up to a heading with level ARG, do
  5903. a `show-subtree' and return to the previous cursor position. If ARG
  5904. is negative, go up that many levels.
  5905. - When point is not at the beginning of a headline, execute the global
  5906. binding for TAB, which is re-indenting the line. See the option
  5907. `org-cycle-emulate-tab' for details.
  5908. - Special case: if point is at the beginning of the buffer and there is
  5909. no headline in line 1, this function will act as if called with prefix arg
  5910. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5911. But only if also the variable `org-cycle-global-at-bob' is t."
  5912. (interactive "P")
  5913. (org-load-modules-maybe)
  5914. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5915. (and org-cycle-level-after-item/entry-creation
  5916. (or (org-cycle-level)
  5917. (org-cycle-item-indentation))))
  5918. (let* ((limit-level
  5919. (or org-cycle-max-level
  5920. (and (boundp 'org-inlinetask-min-level)
  5921. org-inlinetask-min-level
  5922. (1- org-inlinetask-min-level))))
  5923. (nstars (and limit-level
  5924. (if org-odd-levels-only
  5925. (and limit-level (1- (* limit-level 2)))
  5926. limit-level)))
  5927. (org-outline-regexp
  5928. (if (not (derived-mode-p 'org-mode))
  5929. outline-regexp
  5930. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5931. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5932. (not (looking-at org-outline-regexp))))
  5933. (org-cycle-hook
  5934. (if bob-special
  5935. (delq 'org-optimize-window-after-visibility-change
  5936. (copy-sequence org-cycle-hook))
  5937. org-cycle-hook))
  5938. (pos (point)))
  5939. (if (or bob-special (equal arg '(4)))
  5940. ;; special case: use global cycling
  5941. (setq arg t))
  5942. (cond
  5943. ((equal arg '(16))
  5944. (setq last-command 'dummy)
  5945. (org-set-startup-visibility)
  5946. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5947. ((equal arg '(64))
  5948. (show-all)
  5949. (org-unlogged-message "Entire buffer visible, including drawers"))
  5950. ;; Try cdlatex TAB completion
  5951. ((org-try-cdlatex-tab))
  5952. ;; Table: enter it or move to the next field.
  5953. ((org-at-table-p 'any)
  5954. (if (org-at-table.el-p)
  5955. (message "Use C-c ' to edit table.el tables")
  5956. (if arg (org-table-edit-field t)
  5957. (org-table-justify-field-maybe)
  5958. (call-interactively 'org-table-next-field))))
  5959. ((run-hook-with-args-until-success
  5960. 'org-tab-after-check-for-table-hook))
  5961. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5962. ((eq arg t) (org-cycle-internal-global))
  5963. ;; Drawers: delegate to `org-flag-drawer'.
  5964. ((save-excursion
  5965. (beginning-of-line 1)
  5966. (looking-at org-drawer-regexp))
  5967. (org-flag-drawer ; toggle block visibility
  5968. (not (get-char-property (match-end 0) 'invisible))))
  5969. ;; Show-subtree, ARG levels up from here.
  5970. ((integerp arg)
  5971. (save-excursion
  5972. (org-back-to-heading)
  5973. (outline-up-heading (if (< arg 0) (- arg)
  5974. (- (funcall outline-level) arg)))
  5975. (org-show-subtree)))
  5976. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5977. ((and (featurep 'org-inlinetask)
  5978. (org-inlinetask-at-task-p)
  5979. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5980. (org-inlinetask-toggle-visibility))
  5981. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5982. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5983. (save-excursion (beginning-of-line 1)
  5984. (looking-at org-outline-regexp)))
  5985. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5986. (org-cycle-internal-local))
  5987. ;; From there: TAB emulation and template completion.
  5988. (buffer-read-only (org-back-to-heading))
  5989. ((run-hook-with-args-until-success
  5990. 'org-tab-after-check-for-cycling-hook))
  5991. ((org-try-structure-completion))
  5992. ((run-hook-with-args-until-success
  5993. 'org-tab-before-tab-emulation-hook))
  5994. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5995. (or (not (bolp))
  5996. (not (looking-at org-outline-regexp))))
  5997. (call-interactively (global-key-binding "\t")))
  5998. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5999. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  6000. (or (and (eq org-cycle-emulate-tab 'white)
  6001. (= (match-end 0) (point-at-eol)))
  6002. (and (eq org-cycle-emulate-tab 'whitestart)
  6003. (>= (match-end 0) pos))))
  6004. t
  6005. (eq org-cycle-emulate-tab t))
  6006. (call-interactively (global-key-binding "\t")))
  6007. (t (save-excursion
  6008. (org-back-to-heading)
  6009. (org-cycle)))))))
  6010. (defun org-cycle-internal-global ()
  6011. "Do the global cycling action."
  6012. ;; Hack to avoid display of messages for .org attachments in Gnus
  6013. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6014. (cond
  6015. ((and (eq last-command this-command)
  6016. (eq org-cycle-global-status 'overview))
  6017. ;; We just created the overview - now do table of contents
  6018. ;; This can be slow in very large buffers, so indicate action
  6019. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6020. (unless ga (org-unlogged-message "CONTENTS..."))
  6021. (org-content)
  6022. (unless ga (org-unlogged-message "CONTENTS...done"))
  6023. (setq org-cycle-global-status 'contents)
  6024. (run-hook-with-args 'org-cycle-hook 'contents))
  6025. ((and (eq last-command this-command)
  6026. (eq org-cycle-global-status 'contents))
  6027. ;; We just showed the table of contents - now show everything
  6028. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6029. (show-all)
  6030. (unless ga (org-unlogged-message "SHOW ALL"))
  6031. (setq org-cycle-global-status 'all)
  6032. (run-hook-with-args 'org-cycle-hook 'all))
  6033. (t
  6034. ;; Default action: go to overview
  6035. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6036. (org-overview)
  6037. (unless ga (org-unlogged-message "OVERVIEW"))
  6038. (setq org-cycle-global-status 'overview)
  6039. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6040. (defvar org-called-with-limited-levels);Dyn-bound in ̀org-with-limited-levels'.
  6041. (defun org-cycle-internal-local ()
  6042. "Do the local cycling action."
  6043. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6044. ;; First, determine end of headline (EOH), end of subtree or item
  6045. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6046. (save-excursion
  6047. (if (org-at-item-p)
  6048. (progn
  6049. (beginning-of-line)
  6050. (setq struct (org-list-struct))
  6051. (setq eoh (point-at-eol))
  6052. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6053. (setq has-children (org-list-has-child-p (point) struct)))
  6054. (org-back-to-heading)
  6055. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6056. (setq eos (save-excursion (org-end-of-subtree t t)
  6057. (when (bolp) (backward-char)) (point)))
  6058. (setq has-children
  6059. (or (save-excursion
  6060. (let ((level (funcall outline-level)))
  6061. (outline-next-heading)
  6062. (and (org-at-heading-p t)
  6063. (> (funcall outline-level) level))))
  6064. (save-excursion
  6065. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6066. ;; Determine end invisible part of buffer (EOL)
  6067. (beginning-of-line 2)
  6068. ;; XEmacs doesn't have `next-single-char-property-change'
  6069. (if (featurep 'xemacs)
  6070. (while (and (not (eobp)) ;; this is like `next-line'
  6071. (get-char-property (1- (point)) 'invisible))
  6072. (beginning-of-line 2))
  6073. (while (and (not (eobp)) ;; this is like `next-line'
  6074. (get-char-property (1- (point)) 'invisible))
  6075. (goto-char (next-single-char-property-change (point) 'invisible))
  6076. (and (eolp) (beginning-of-line 2))))
  6077. (setq eol (point)))
  6078. ;; Find out what to do next and set `this-command'
  6079. (cond
  6080. ((= eos eoh)
  6081. ;; Nothing is hidden behind this heading
  6082. (unless (org-before-first-heading-p)
  6083. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6084. (org-unlogged-message "EMPTY ENTRY")
  6085. (setq org-cycle-subtree-status nil)
  6086. (save-excursion
  6087. (goto-char eos)
  6088. (outline-next-heading)
  6089. (if (outline-invisible-p) (org-flag-heading nil))))
  6090. ((and (or (>= eol eos)
  6091. (not (string-match "\\S-" (buffer-substring eol eos))))
  6092. (or has-children
  6093. (not (setq children-skipped
  6094. org-cycle-skip-children-state-if-no-children))))
  6095. ;; Entire subtree is hidden in one line: children view
  6096. (unless (org-before-first-heading-p)
  6097. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6098. (if (org-at-item-p)
  6099. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6100. (org-show-entry)
  6101. (org-with-limited-levels (show-children))
  6102. ;; FIXME: This slows down the func way too much.
  6103. ;; How keep drawers hidden in subtree anyway?
  6104. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6105. ;; (org-cycle-hide-drawers 'subtree))
  6106. ;; Fold every list in subtree to top-level items.
  6107. (when (eq org-cycle-include-plain-lists 'integrate)
  6108. (save-excursion
  6109. (org-back-to-heading)
  6110. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6111. (beginning-of-line 1)
  6112. (let* ((struct (org-list-struct))
  6113. (prevs (org-list-prevs-alist struct))
  6114. (end (org-list-get-bottom-point struct)))
  6115. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6116. (org-list-get-all-items (point) struct prevs))
  6117. (goto-char (if (< end eos) end eos)))))))
  6118. (org-unlogged-message "CHILDREN")
  6119. (save-excursion
  6120. (goto-char eos)
  6121. (outline-next-heading)
  6122. (if (outline-invisible-p) (org-flag-heading nil)))
  6123. (setq org-cycle-subtree-status 'children)
  6124. (unless (org-before-first-heading-p)
  6125. (run-hook-with-args 'org-cycle-hook 'children)))
  6126. ((or children-skipped
  6127. (and (eq last-command this-command)
  6128. (eq org-cycle-subtree-status 'children)))
  6129. ;; We just showed the children, or no children are there,
  6130. ;; now show everything.
  6131. (unless (org-before-first-heading-p)
  6132. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6133. (outline-flag-region eoh eos nil)
  6134. (org-unlogged-message
  6135. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6136. (setq org-cycle-subtree-status 'subtree)
  6137. (unless (org-before-first-heading-p)
  6138. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6139. (t
  6140. ;; Default action: hide the subtree.
  6141. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6142. (outline-flag-region eoh eos t)
  6143. (org-unlogged-message "FOLDED")
  6144. (setq org-cycle-subtree-status 'folded)
  6145. (unless (org-before-first-heading-p)
  6146. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6147. ;;;###autoload
  6148. (defun org-global-cycle (&optional arg)
  6149. "Cycle the global visibility. For details see `org-cycle'.
  6150. With \\[universal-argument] prefix arg, switch to startup visibility.
  6151. With a numeric prefix, show all headlines up to that level."
  6152. (interactive "P")
  6153. (let ((org-cycle-include-plain-lists
  6154. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6155. (cond
  6156. ((integerp arg)
  6157. (show-all)
  6158. (hide-sublevels arg)
  6159. (setq org-cycle-global-status 'contents))
  6160. ((equal arg '(4))
  6161. (org-set-startup-visibility)
  6162. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6163. (t
  6164. (org-cycle '(4))))))
  6165. (defun org-set-startup-visibility ()
  6166. "Set the visibility required by startup options and properties."
  6167. (cond
  6168. ((eq org-startup-folded t)
  6169. (org-overview))
  6170. ((eq org-startup-folded 'content)
  6171. (org-content))
  6172. ((or (eq org-startup-folded 'showeverything)
  6173. (eq org-startup-folded nil))
  6174. (show-all)))
  6175. (unless (eq org-startup-folded 'showeverything)
  6176. (if org-hide-block-startup (org-hide-block-all))
  6177. (org-set-visibility-according-to-property 'no-cleanup)
  6178. (org-cycle-hide-archived-subtrees 'all)
  6179. (org-cycle-hide-drawers 'all)
  6180. (org-cycle-show-empty-lines t)))
  6181. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6182. "Switch subtree visibilities according to :VISIBILITY: property."
  6183. (interactive)
  6184. (let (org-show-entry-below state)
  6185. (save-excursion
  6186. (goto-char (point-min))
  6187. (while (re-search-forward
  6188. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6189. nil t)
  6190. (setq state (match-string 1))
  6191. (save-excursion
  6192. (org-back-to-heading t)
  6193. (hide-subtree)
  6194. (org-reveal)
  6195. (cond
  6196. ((equal state '("fold" "folded"))
  6197. (hide-subtree))
  6198. ((equal state "children")
  6199. (org-show-hidden-entry)
  6200. (show-children))
  6201. ((equal state "content")
  6202. (save-excursion
  6203. (save-restriction
  6204. (org-narrow-to-subtree)
  6205. (org-content))))
  6206. ((member state '("all" "showall"))
  6207. (show-subtree)))))
  6208. (unless no-cleanup
  6209. (org-cycle-hide-archived-subtrees 'all)
  6210. (org-cycle-hide-drawers 'all)
  6211. (org-cycle-show-empty-lines 'all)))))
  6212. ;; This function uses outline-regexp instead of the more fundamental
  6213. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6214. ;; buffers, where outline-regexp is needed.
  6215. (defun org-overview ()
  6216. "Switch to overview mode, showing only top-level headlines.
  6217. This shows all headlines with a level equal or greater than the level
  6218. of the first headline in the buffer. This is important, because if the
  6219. first headline is not level one, then (hide-sublevels 1) gives confusing
  6220. results."
  6221. (interactive)
  6222. (save-excursion
  6223. (let ((level
  6224. (save-excursion
  6225. (goto-char (point-min))
  6226. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6227. (progn
  6228. (goto-char (match-beginning 0))
  6229. (funcall outline-level))))))
  6230. (and level (hide-sublevels level)))))
  6231. (defun org-content (&optional arg)
  6232. "Show all headlines in the buffer, like a table of contents.
  6233. With numerical argument N, show content up to level N."
  6234. (interactive "P")
  6235. (org-overview)
  6236. (save-excursion
  6237. ;; Visit all headings and show their offspring
  6238. (and (integerp arg) (org-overview))
  6239. (goto-char (point-max))
  6240. (catch 'exit
  6241. (while (and (progn (condition-case nil
  6242. (outline-previous-visible-heading 1)
  6243. (error (goto-char (point-min))))
  6244. t)
  6245. (looking-at org-outline-regexp))
  6246. (if (integerp arg)
  6247. (show-children (1- arg))
  6248. (show-branches))
  6249. (if (bobp) (throw 'exit nil))))))
  6250. (defun org-optimize-window-after-visibility-change (state)
  6251. "Adjust the window after a change in outline visibility.
  6252. This function is the default value of the hook `org-cycle-hook'."
  6253. (when (get-buffer-window (current-buffer))
  6254. (cond
  6255. ((eq state 'content) nil)
  6256. ((eq state 'all) nil)
  6257. ((eq state 'folded) nil)
  6258. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6259. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6260. (defun org-remove-empty-overlays-at (pos)
  6261. "Remove outline overlays that do not contain non-white stuff."
  6262. (mapc
  6263. (lambda (o)
  6264. (and (eq 'outline (overlay-get o 'invisible))
  6265. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6266. (overlay-end o))))
  6267. (delete-overlay o)))
  6268. (overlays-at pos)))
  6269. (defun org-clean-visibility-after-subtree-move ()
  6270. "Fix visibility issues after moving a subtree."
  6271. ;; First, find a reasonable region to look at:
  6272. ;; Start two siblings above, end three below
  6273. (let* ((beg (save-excursion
  6274. (and (org-get-last-sibling)
  6275. (org-get-last-sibling))
  6276. (point)))
  6277. (end (save-excursion
  6278. (and (org-get-next-sibling)
  6279. (org-get-next-sibling)
  6280. (org-get-next-sibling))
  6281. (if (org-at-heading-p)
  6282. (point-at-eol)
  6283. (point))))
  6284. (level (looking-at "\\*+"))
  6285. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6286. (save-excursion
  6287. (save-restriction
  6288. (narrow-to-region beg end)
  6289. (when re
  6290. ;; Properly fold already folded siblings
  6291. (goto-char (point-min))
  6292. (while (re-search-forward re nil t)
  6293. (if (and (not (outline-invisible-p))
  6294. (save-excursion
  6295. (goto-char (point-at-eol)) (outline-invisible-p)))
  6296. (hide-entry))))
  6297. (org-cycle-show-empty-lines 'overview)
  6298. (org-cycle-hide-drawers 'overview)))))
  6299. (defun org-cycle-show-empty-lines (state)
  6300. "Show empty lines above all visible headlines.
  6301. The region to be covered depends on STATE when called through
  6302. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6303. entire buffer covered. Note that an empty line is only shown if there
  6304. are at least `org-cycle-separator-lines' empty lines before the headline."
  6305. (when (not (= org-cycle-separator-lines 0))
  6306. (save-excursion
  6307. (let* ((n (abs org-cycle-separator-lines))
  6308. (re (cond
  6309. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6310. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6311. (t (let ((ns (number-to-string (- n 2))))
  6312. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6313. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6314. beg end b e)
  6315. (cond
  6316. ((memq state '(overview contents t))
  6317. (setq beg (point-min) end (point-max)))
  6318. ((memq state '(children folded))
  6319. (setq beg (point) end (progn (org-end-of-subtree t t)
  6320. (beginning-of-line 2)
  6321. (point)))))
  6322. (when beg
  6323. (goto-char beg)
  6324. (while (re-search-forward re end t)
  6325. (unless (get-char-property (match-end 1) 'invisible)
  6326. (setq e (match-end 1))
  6327. (if (< org-cycle-separator-lines 0)
  6328. (setq b (save-excursion
  6329. (goto-char (match-beginning 0))
  6330. (org-back-over-empty-lines)
  6331. (if (save-excursion
  6332. (goto-char (max (point-min) (1- (point))))
  6333. (org-at-heading-p))
  6334. (1- (point))
  6335. (point))))
  6336. (setq b (match-beginning 1)))
  6337. (outline-flag-region b e nil)))))))
  6338. ;; Never hide empty lines at the end of the file.
  6339. (save-excursion
  6340. (goto-char (point-max))
  6341. (outline-previous-heading)
  6342. (outline-end-of-heading)
  6343. (if (and (looking-at "[ \t\n]+")
  6344. (= (match-end 0) (point-max)))
  6345. (outline-flag-region (point) (match-end 0) nil))))
  6346. (defun org-show-empty-lines-in-parent ()
  6347. "Move to the parent and re-show empty lines before visible headlines."
  6348. (save-excursion
  6349. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6350. (org-cycle-show-empty-lines context))))
  6351. (defun org-files-list ()
  6352. "Return `org-agenda-files' list, plus all open org-mode files.
  6353. This is useful for operations that need to scan all of a user's
  6354. open and agenda-wise Org files."
  6355. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6356. (dolist (buf (buffer-list))
  6357. (with-current-buffer buf
  6358. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6359. (let ((file (expand-file-name (buffer-file-name))))
  6360. (unless (member file files)
  6361. (push file files))))))
  6362. files))
  6363. (defsubst org-entry-beginning-position ()
  6364. "Return the beginning position of the current entry."
  6365. (save-excursion (outline-back-to-heading t) (point)))
  6366. (defsubst org-entry-end-position ()
  6367. "Return the end position of the current entry."
  6368. (save-excursion (outline-next-heading) (point)))
  6369. (defun org-cycle-hide-drawers (state &optional exceptions)
  6370. "Re-hide all drawers after a visibility state change.
  6371. When non-nil, optional argument EXCEPTIONS is a list of strings
  6372. specifying which drawers should not be hidden."
  6373. (when (and (derived-mode-p 'org-mode)
  6374. (not (memq state '(overview folded contents))))
  6375. (save-excursion
  6376. (let* ((globalp (memq state '(contents all)))
  6377. (beg (if globalp (point-min) (point)))
  6378. (end (if globalp (point-max)
  6379. (if (eq state 'children)
  6380. (save-excursion (outline-next-heading) (point))
  6381. (org-end-of-subtree t)))))
  6382. (goto-char beg)
  6383. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6384. (unless (member-ignore-case (match-string 1) exceptions)
  6385. (let ((drawer (org-element-at-point)))
  6386. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6387. (org-flag-drawer t drawer)
  6388. ;; Make sure to skip drawer entirely or we might flag
  6389. ;; it another time when matching its ending line with
  6390. ;; `org-drawer-regexp'.
  6391. (goto-char (org-element-property :end drawer))))))))))
  6392. (defun org-cycle-hide-inline-tasks (state)
  6393. "Re-hide inline tasks when switching to 'contents or 'children
  6394. visibility state."
  6395. (case state
  6396. (contents
  6397. (when (org-bound-and-true-p org-inlinetask-min-level)
  6398. (hide-sublevels (1- org-inlinetask-min-level))))
  6399. (children
  6400. (when (featurep 'org-inlinetask)
  6401. (save-excursion
  6402. (while (and (outline-next-heading)
  6403. (org-inlinetask-at-task-p))
  6404. (org-inlinetask-toggle-visibility)
  6405. (org-inlinetask-goto-end)))))))
  6406. (defun org-flag-drawer (flag &optional element)
  6407. "When FLAG is non-nil, hide the drawer we are at.
  6408. Otherwise make it visible. When optional argument ELEMENT is
  6409. a parsed drawer, as returned by `org-element-at-point', hide or
  6410. show that drawer instead."
  6411. (when (save-excursion
  6412. (beginning-of-line)
  6413. (org-looking-at-p org-drawer-regexp))
  6414. (let ((drawer (or element (org-element-at-point))))
  6415. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6416. (let ((post (org-element-property :post-affiliated drawer)))
  6417. (save-excursion
  6418. (outline-flag-region
  6419. (progn (goto-char post) (line-end-position))
  6420. (progn (goto-char (org-element-property :end drawer))
  6421. (skip-chars-backward " \r\t\n")
  6422. (line-end-position))
  6423. flag))
  6424. ;; When the drawer is hidden away, make sure point lies in
  6425. ;; a visible part of the buffer.
  6426. (when (and flag (> (line-beginning-position) post))
  6427. (goto-char post)))))))
  6428. (defun org-subtree-end-visible-p ()
  6429. "Is the end of the current subtree visible?"
  6430. (pos-visible-in-window-p
  6431. (save-excursion (org-end-of-subtree t) (point))))
  6432. (defun org-first-headline-recenter (&optional N)
  6433. "Move cursor to the first headline and recenter the headline.
  6434. Optional argument N means put the headline into the Nth line of the window."
  6435. (goto-char (point-min))
  6436. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6437. (beginning-of-line)
  6438. (recenter (prefix-numeric-value N))))
  6439. ;;; Saving and restoring visibility
  6440. (defun org-outline-overlay-data (&optional use-markers)
  6441. "Return a list of the locations of all outline overlays.
  6442. These are overlays with the `invisible' property value `outline'.
  6443. The return value is a list of cons cells, with start and stop
  6444. positions for each overlay.
  6445. If USE-MARKERS is set, return the positions as markers."
  6446. (let (beg end)
  6447. (save-excursion
  6448. (save-restriction
  6449. (widen)
  6450. (delq nil
  6451. (mapcar (lambda (o)
  6452. (when (eq (overlay-get o 'invisible) 'outline)
  6453. (setq beg (overlay-start o)
  6454. end (overlay-end o))
  6455. (and beg end (> end beg)
  6456. (if use-markers
  6457. (cons (copy-marker beg)
  6458. (copy-marker end t))
  6459. (cons beg end)))))
  6460. (overlays-in (point-min) (point-max))))))))
  6461. (defun org-set-outline-overlay-data (data)
  6462. "Create visibility overlays for all positions in DATA.
  6463. DATA should have been made by `org-outline-overlay-data'."
  6464. (let (o)
  6465. (save-excursion
  6466. (save-restriction
  6467. (widen)
  6468. (show-all)
  6469. (mapc (lambda (c)
  6470. (outline-flag-region (car c) (cdr c) t))
  6471. data)))))
  6472. ;;; Folding of blocks
  6473. (defvar org-hide-block-overlays nil
  6474. "Overlays hiding blocks.")
  6475. (make-variable-buffer-local 'org-hide-block-overlays)
  6476. (defun org-block-map (function &optional start end)
  6477. "Call FUNCTION at the head of all source blocks in the current buffer.
  6478. Optional arguments START and END can be used to limit the range."
  6479. (let ((start (or start (point-min)))
  6480. (end (or end (point-max))))
  6481. (save-excursion
  6482. (goto-char start)
  6483. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6484. (save-excursion
  6485. (save-match-data
  6486. (goto-char (match-beginning 0))
  6487. (funcall function)))))))
  6488. (defun org-hide-block-toggle-all ()
  6489. "Toggle the visibility of all blocks in the current buffer."
  6490. (org-block-map #'org-hide-block-toggle))
  6491. (defun org-hide-block-all ()
  6492. "Fold all blocks in the current buffer."
  6493. (interactive)
  6494. (org-show-block-all)
  6495. (org-block-map #'org-hide-block-toggle-maybe))
  6496. (defun org-show-block-all ()
  6497. "Unfold all blocks in the current buffer."
  6498. (interactive)
  6499. (mapc 'delete-overlay org-hide-block-overlays)
  6500. (setq org-hide-block-overlays nil))
  6501. (defun org-hide-block-toggle-maybe ()
  6502. "Toggle visibility of block at point."
  6503. (interactive)
  6504. (let ((case-fold-search t))
  6505. (if (save-excursion
  6506. (beginning-of-line 1)
  6507. (looking-at org-block-regexp))
  6508. (progn (org-hide-block-toggle)
  6509. t) ;; to signal that we took action
  6510. nil))) ;; to signal that we did not
  6511. (defun org-hide-block-toggle (&optional force)
  6512. "Toggle the visibility of the current block."
  6513. (interactive)
  6514. (save-excursion
  6515. (beginning-of-line)
  6516. (if (re-search-forward org-block-regexp nil t)
  6517. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6518. (end (match-end 0)) ;; end of entire body
  6519. ov)
  6520. (if (memq t (mapcar (lambda (overlay)
  6521. (eq (overlay-get overlay 'invisible)
  6522. 'org-hide-block))
  6523. (overlays-at start)))
  6524. (if (or (not force) (eq force 'off))
  6525. (mapc (lambda (ov)
  6526. (when (member ov org-hide-block-overlays)
  6527. (setq org-hide-block-overlays
  6528. (delq ov org-hide-block-overlays)))
  6529. (when (eq (overlay-get ov 'invisible)
  6530. 'org-hide-block)
  6531. (delete-overlay ov)))
  6532. (overlays-at start)))
  6533. (setq ov (make-overlay start end))
  6534. (overlay-put ov 'invisible 'org-hide-block)
  6535. ;; make the block accessible to isearch
  6536. (overlay-put
  6537. ov 'isearch-open-invisible
  6538. (lambda (ov)
  6539. (when (member ov org-hide-block-overlays)
  6540. (setq org-hide-block-overlays
  6541. (delq ov org-hide-block-overlays)))
  6542. (when (eq (overlay-get ov 'invisible)
  6543. 'org-hide-block)
  6544. (delete-overlay ov))))
  6545. (push ov org-hide-block-overlays)))
  6546. (user-error "Not looking at a source block"))))
  6547. ;; org-tab-after-check-for-cycling-hook
  6548. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6549. ;; Remove overlays when changing major mode
  6550. (add-hook 'org-mode-hook
  6551. (lambda () (org-add-hook 'change-major-mode-hook
  6552. 'org-show-block-all 'append 'local)))
  6553. ;;; Org-goto
  6554. (defvar org-goto-window-configuration nil)
  6555. (defvar org-goto-marker nil)
  6556. (defvar org-goto-map)
  6557. (defun org-goto-map ()
  6558. "Set the keymap `org-goto'."
  6559. (setq org-goto-map
  6560. (let ((map (make-sparse-keymap)))
  6561. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6562. mouse-drag-region universal-argument org-occur))
  6563. cmd)
  6564. (while (setq cmd (pop cmds))
  6565. (substitute-key-definition cmd cmd map global-map)))
  6566. (suppress-keymap map)
  6567. (org-defkey map "\C-m" 'org-goto-ret)
  6568. (org-defkey map [(return)] 'org-goto-ret)
  6569. (org-defkey map [(left)] 'org-goto-left)
  6570. (org-defkey map [(right)] 'org-goto-right)
  6571. (org-defkey map [(control ?g)] 'org-goto-quit)
  6572. (org-defkey map "\C-i" 'org-cycle)
  6573. (org-defkey map [(tab)] 'org-cycle)
  6574. (org-defkey map [(down)] 'outline-next-visible-heading)
  6575. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6576. (if org-goto-auto-isearch
  6577. (if (fboundp 'define-key-after)
  6578. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6579. nil)
  6580. (org-defkey map "q" 'org-goto-quit)
  6581. (org-defkey map "n" 'outline-next-visible-heading)
  6582. (org-defkey map "p" 'outline-previous-visible-heading)
  6583. (org-defkey map "f" 'outline-forward-same-level)
  6584. (org-defkey map "b" 'outline-backward-same-level)
  6585. (org-defkey map "u" 'outline-up-heading))
  6586. (org-defkey map "/" 'org-occur)
  6587. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6588. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6589. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6590. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6591. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6592. map)))
  6593. (defconst org-goto-help
  6594. "Browse buffer copy, to find location or copy text.%s
  6595. RET=jump to location C-g=quit and return to previous location
  6596. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6597. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6598. (defun org-goto (&optional alternative-interface)
  6599. "Look up a different location in the current file, keeping current visibility.
  6600. When you want look-up or go to a different location in a
  6601. document, the fastest way is often to fold the entire buffer and
  6602. then dive into the tree. This method has the disadvantage, that
  6603. the previous location will be folded, which may not be what you
  6604. want.
  6605. This command works around this by showing a copy of the current
  6606. buffer in an indirect buffer, in overview mode. You can dive
  6607. into the tree in that copy, use org-occur and incremental search
  6608. to find a location. When pressing RET or `Q', the command
  6609. returns to the original buffer in which the visibility is still
  6610. unchanged. After RET it will also jump to the location selected
  6611. in the indirect buffer and expose the headline hierarchy above.
  6612. With a prefix argument, use the alternative interface: e.g. if
  6613. `org-goto-interface' is 'outline use 'outline-path-completion."
  6614. (interactive "P")
  6615. (org-goto-map)
  6616. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6617. (org-refile-use-outline-path t)
  6618. (org-refile-target-verify-function nil)
  6619. (interface
  6620. (if (not alternative-interface)
  6621. org-goto-interface
  6622. (if (eq org-goto-interface 'outline)
  6623. 'outline-path-completion
  6624. 'outline)))
  6625. (org-goto-start-pos (point))
  6626. (selected-point
  6627. (if (eq interface 'outline)
  6628. (car (org-get-location (current-buffer) org-goto-help))
  6629. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6630. (org-refile-check-position pa)
  6631. (nth 3 pa)))))
  6632. (if selected-point
  6633. (progn
  6634. (org-mark-ring-push org-goto-start-pos)
  6635. (goto-char selected-point)
  6636. (if (or (outline-invisible-p) (org-invisible-p2))
  6637. (org-show-context 'org-goto)))
  6638. (message "Quit"))))
  6639. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6640. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6641. (defvar org-goto-local-auto-isearch-map) ; defined below
  6642. (defun org-get-location (buf help)
  6643. "Let the user select a location in the Org-mode buffer BUF.
  6644. This function uses a recursive edit. It returns the selected position
  6645. or nil."
  6646. (org-no-popups
  6647. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6648. (isearch-hide-immediately nil)
  6649. (isearch-search-fun-function
  6650. (lambda () 'org-goto-local-search-headings))
  6651. (org-goto-selected-point org-goto-exit-command))
  6652. (save-excursion
  6653. (save-window-excursion
  6654. (delete-other-windows)
  6655. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6656. (org-pop-to-buffer-same-window
  6657. (condition-case nil
  6658. (make-indirect-buffer (current-buffer) "*org-goto*")
  6659. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6660. (with-output-to-temp-buffer "*Org Help*"
  6661. (princ (format help (if org-goto-auto-isearch
  6662. " Just type for auto-isearch."
  6663. " n/p/f/b/u to navigate, q to quit."))))
  6664. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6665. (setq buffer-read-only nil)
  6666. (let ((org-startup-truncated t)
  6667. (org-startup-folded nil)
  6668. (org-startup-align-all-tables nil))
  6669. (org-mode)
  6670. (org-overview))
  6671. (setq buffer-read-only t)
  6672. (if (and (boundp 'org-goto-start-pos)
  6673. (integer-or-marker-p org-goto-start-pos))
  6674. (let ((org-show-hierarchy-above t)
  6675. (org-show-siblings t)
  6676. (org-show-following-heading t))
  6677. (goto-char org-goto-start-pos)
  6678. (and (outline-invisible-p) (org-show-context)))
  6679. (goto-char (point-min)))
  6680. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6681. (message "Select location and press RET")
  6682. (use-local-map org-goto-map)
  6683. (recursive-edit)))
  6684. (kill-buffer "*org-goto*")
  6685. (cons org-goto-selected-point org-goto-exit-command))))
  6686. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6687. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6688. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6689. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6690. (defun org-goto-local-search-headings (string bound noerror)
  6691. "Search and make sure that any matches are in headlines."
  6692. (catch 'return
  6693. (while (if isearch-forward
  6694. (search-forward string bound noerror)
  6695. (search-backward string bound noerror))
  6696. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6697. (and (member :headline context)
  6698. (not (member :tags context))))
  6699. (throw 'return (point))))))
  6700. (defun org-goto-local-auto-isearch ()
  6701. "Start isearch."
  6702. (interactive)
  6703. (goto-char (point-min))
  6704. (let ((keys (this-command-keys)))
  6705. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6706. (isearch-mode t)
  6707. (isearch-process-search-char (string-to-char keys)))))
  6708. (defun org-goto-ret (&optional arg)
  6709. "Finish `org-goto' by going to the new location."
  6710. (interactive "P")
  6711. (setq org-goto-selected-point (point)
  6712. org-goto-exit-command 'return)
  6713. (throw 'exit nil))
  6714. (defun org-goto-left ()
  6715. "Finish `org-goto' by going to the new location."
  6716. (interactive)
  6717. (if (org-at-heading-p)
  6718. (progn
  6719. (beginning-of-line 1)
  6720. (setq org-goto-selected-point (point)
  6721. org-goto-exit-command 'left)
  6722. (throw 'exit nil))
  6723. (user-error "Not on a heading")))
  6724. (defun org-goto-right ()
  6725. "Finish `org-goto' by going to the new location."
  6726. (interactive)
  6727. (if (org-at-heading-p)
  6728. (progn
  6729. (setq org-goto-selected-point (point)
  6730. org-goto-exit-command 'right)
  6731. (throw 'exit nil))
  6732. (user-error "Not on a heading")))
  6733. (defun org-goto-quit ()
  6734. "Finish `org-goto' without cursor motion."
  6735. (interactive)
  6736. (setq org-goto-selected-point nil)
  6737. (setq org-goto-exit-command 'quit)
  6738. (throw 'exit nil))
  6739. ;;; Indirect buffer display of subtrees
  6740. (defvar org-indirect-dedicated-frame nil
  6741. "This is the frame being used for indirect tree display.")
  6742. (defvar org-last-indirect-buffer nil)
  6743. (defun org-tree-to-indirect-buffer (&optional arg)
  6744. "Create indirect buffer and narrow it to current subtree.
  6745. With a numerical prefix ARG, go up to this level and then take that tree.
  6746. If ARG is negative, go up that many levels.
  6747. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6748. indirect buffer previously made with this command, to avoid proliferation of
  6749. indirect buffers. However, when you call the command with a \
  6750. \\[universal-argument] prefix, or
  6751. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6752. is kept so that you can work with several indirect buffers at the same time.
  6753. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6754. \\[universal-argument] prefix also
  6755. requests that a new frame be made for the new buffer, so that the dedicated
  6756. frame is not changed."
  6757. (interactive "P")
  6758. (let ((cbuf (current-buffer))
  6759. (cwin (selected-window))
  6760. (pos (point))
  6761. beg end level heading ibuf)
  6762. (save-excursion
  6763. (org-back-to-heading t)
  6764. (when (numberp arg)
  6765. (setq level (org-outline-level))
  6766. (if (< arg 0) (setq arg (+ level arg)))
  6767. (while (> (setq level (org-outline-level)) arg)
  6768. (org-up-heading-safe)))
  6769. (setq beg (point)
  6770. heading (org-get-heading))
  6771. (org-end-of-subtree t t)
  6772. (if (org-at-heading-p) (backward-char 1))
  6773. (setq end (point)))
  6774. (if (and (buffer-live-p org-last-indirect-buffer)
  6775. (not (eq org-indirect-buffer-display 'new-frame))
  6776. (not arg))
  6777. (kill-buffer org-last-indirect-buffer))
  6778. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6779. org-last-indirect-buffer ibuf)
  6780. (cond
  6781. ((or (eq org-indirect-buffer-display 'new-frame)
  6782. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6783. (select-frame (make-frame))
  6784. (delete-other-windows)
  6785. (org-pop-to-buffer-same-window ibuf)
  6786. (org-set-frame-title heading))
  6787. ((eq org-indirect-buffer-display 'dedicated-frame)
  6788. (raise-frame
  6789. (select-frame (or (and org-indirect-dedicated-frame
  6790. (frame-live-p org-indirect-dedicated-frame)
  6791. org-indirect-dedicated-frame)
  6792. (setq org-indirect-dedicated-frame (make-frame)))))
  6793. (delete-other-windows)
  6794. (org-pop-to-buffer-same-window ibuf)
  6795. (org-set-frame-title (concat "Indirect: " heading)))
  6796. ((eq org-indirect-buffer-display 'current-window)
  6797. (org-pop-to-buffer-same-window ibuf))
  6798. ((eq org-indirect-buffer-display 'other-window)
  6799. (pop-to-buffer ibuf))
  6800. (t (error "Invalid value")))
  6801. (if (featurep 'xemacs)
  6802. (save-excursion (org-mode) (turn-on-font-lock)))
  6803. (narrow-to-region beg end)
  6804. (show-all)
  6805. (goto-char pos)
  6806. (run-hook-with-args 'org-cycle-hook 'all)
  6807. (and (window-live-p cwin) (select-window cwin))))
  6808. (defun org-get-indirect-buffer (&optional buffer heading)
  6809. (setq buffer (or buffer (current-buffer)))
  6810. (let ((n 1) (base (buffer-name buffer)) bname)
  6811. (while (buffer-live-p
  6812. (get-buffer
  6813. (setq bname
  6814. (concat base "-"
  6815. (if heading (concat heading "-" (number-to-string n))
  6816. (number-to-string n))))))
  6817. (setq n (1+ n)))
  6818. (condition-case nil
  6819. (make-indirect-buffer buffer bname 'clone)
  6820. (error (make-indirect-buffer buffer bname)))))
  6821. (defun org-set-frame-title (title)
  6822. "Set the title of the current frame to the string TITLE."
  6823. ;; FIXME: how to name a single frame in XEmacs???
  6824. (unless (featurep 'xemacs)
  6825. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6826. ;;;; Structure editing
  6827. ;;; Inserting headlines
  6828. (defun org-previous-line-empty-p (&optional next)
  6829. "Is the previous line a blank line?
  6830. When NEXT is non-nil, check the next line instead."
  6831. (save-excursion
  6832. (and (not (bobp))
  6833. (or (beginning-of-line (if next 2 0)) t)
  6834. (save-match-data
  6835. (looking-at "[ \t]*$")))))
  6836. (defun org-insert-heading (&optional arg invisible-ok)
  6837. "Insert a new heading or item with same depth at point.
  6838. If point is in a plain list and ARG is nil, add a new list item.
  6839. With one universal prefix argument, insert a heading even when
  6840. the point is within a list.
  6841. With two universal prefix arguments, insert the heading at the
  6842. end of the grandparent subtree. For example, if point is within
  6843. a 2nd-level heading, then it will insert a 2nd-level heading at
  6844. the end of the 1st-level parent heading.
  6845. If point is at the beginning of a headline, insert a sibling
  6846. before the current headline. If point is not at the beginning,
  6847. split the line and create a new headline with the text in the
  6848. current line after point \(see `org-M-RET-may-split-line' on how
  6849. to modify this behavior).
  6850. If point is at the beginning of a normal line, turn this line
  6851. into a heading.
  6852. When INVISIBLE-OK is set, stop at invisible headlines when going
  6853. back. This is important for non-interactive uses of the
  6854. command."
  6855. (interactive "P")
  6856. (if (org-called-interactively-p 'any) (org-reveal))
  6857. (let ((itemp (org-in-item-p))
  6858. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6859. (respect-content (or org-insert-heading-respect-content
  6860. (equal arg '(4))))
  6861. (initial-content "")
  6862. (adjust-empty-lines t))
  6863. (cond
  6864. ((or (= (buffer-size) 0)
  6865. (and (not (save-excursion
  6866. (and (ignore-errors (org-back-to-heading invisible-ok))
  6867. (org-at-heading-p))))
  6868. (or arg (not itemp))))
  6869. ;; At beginning of buffer or so high up that only a heading
  6870. ;; makes sense.
  6871. (when (and (org-before-first-heading-p) (not (bolp)))
  6872. (re-search-forward org-outline-regexp-bol)
  6873. (beginning-of-line 0))
  6874. (insert
  6875. (if (or (bobp) (org-previous-line-empty-p)) "" "\n")
  6876. (if (org-in-src-block-p) ",* " "* "))
  6877. (run-hooks 'org-insert-heading-hook))
  6878. ((and itemp (not (equal arg '(4))))
  6879. ;; Insert an item
  6880. (org-insert-item))
  6881. (t
  6882. ;; Maybe move at the end of the subtree
  6883. (when (equal arg '(16))
  6884. (org-up-heading-safe)
  6885. (org-end-of-subtree t))
  6886. ;; Insert a heading
  6887. (save-restriction
  6888. (widen)
  6889. (let* ((level nil)
  6890. (on-heading (org-at-heading-p))
  6891. (empty-line-p (if on-heading
  6892. (org-previous-line-empty-p)
  6893. ;; We will decide later
  6894. nil))
  6895. ;; Get a level string to fall back on
  6896. (fix-level
  6897. (if (org-before-first-heading-p) "*"
  6898. (save-excursion
  6899. (org-back-to-heading t)
  6900. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6901. (looking-at org-outline-regexp)
  6902. (make-string (1- (length (match-string 0))) ?*))))
  6903. (stars
  6904. (save-excursion
  6905. (condition-case nil
  6906. (progn
  6907. (org-back-to-heading invisible-ok)
  6908. (when (and (not on-heading)
  6909. (featurep 'org-inlinetask)
  6910. (integerp org-inlinetask-min-level)
  6911. (>= (length (match-string 0))
  6912. org-inlinetask-min-level))
  6913. ;; Find a heading level before the inline task
  6914. (while (and (setq level (org-up-heading-safe))
  6915. (>= level org-inlinetask-min-level)))
  6916. (if (org-at-heading-p)
  6917. (org-back-to-heading invisible-ok)
  6918. (error "This should not happen")))
  6919. (unless (and (save-excursion
  6920. (save-match-data
  6921. (org-backward-heading-same-level
  6922. 1 invisible-ok))
  6923. (= (point) (match-beginning 0)))
  6924. (not (org-previous-line-empty-p t)))
  6925. (setq empty-line-p (or empty-line-p
  6926. (org-previous-line-empty-p))))
  6927. (match-string 0))
  6928. (error (or fix-level "* ")))))
  6929. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6930. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6931. pos hide-previous previous-pos)
  6932. ;; If we insert after content, move there and clean up whitespace
  6933. (when (and respect-content
  6934. (not (org-looking-at-p org-outline-regexp-bol))
  6935. (not (bolp)))
  6936. (if (not (org-before-first-heading-p))
  6937. (org-end-of-subtree nil t)
  6938. (re-search-forward org-outline-regexp-bol)
  6939. (beginning-of-line 0))
  6940. (skip-chars-backward " \r\n")
  6941. (and (not (looking-back "^\\*+"))
  6942. (looking-at "[ \t]+") (replace-match ""))
  6943. (unless (eobp) (forward-char 1))
  6944. (when (looking-at "^\\*")
  6945. (unless (bobp) (backward-char 1))
  6946. (insert "\n")))
  6947. ;; If we are splitting, grab the text that should be moved to the new headline
  6948. (when may-split
  6949. (if (org-on-heading-p)
  6950. ;; This is a heading, we split intelligently (keeping tags)
  6951. (let ((pos (point)))
  6952. (goto-char (point-at-bol))
  6953. (unless (looking-at org-complex-heading-regexp)
  6954. (error "This should not happen"))
  6955. (when (and (match-beginning 4)
  6956. (> pos (match-beginning 4))
  6957. (< pos (match-end 4)))
  6958. (setq initial-content (buffer-substring pos (match-end 4)))
  6959. (goto-char pos)
  6960. (delete-region (point) (match-end 4))
  6961. (if (looking-at "[ \t]*$")
  6962. (replace-match "")
  6963. (insert (make-string (length initial-content) ?\ )))
  6964. (setq initial-content (org-trim initial-content)))
  6965. (goto-char pos))
  6966. ;; a normal line
  6967. (setq initial-content
  6968. (org-trim (buffer-substring (point) (point-at-eol))))
  6969. (delete-region (point) (point-at-eol))))
  6970. ;; If we are at the beginning of the line, insert before it. Else after
  6971. (cond
  6972. ((and (bolp) (looking-at "[ \t]*$")))
  6973. ((and (bolp) (not (looking-at "[ \t]*$")))
  6974. (open-line 1))
  6975. (t
  6976. (goto-char (point-at-eol))
  6977. (insert "\n")))
  6978. ;; Insert the new heading
  6979. (insert stars)
  6980. (just-one-space)
  6981. (insert initial-content)
  6982. (when adjust-empty-lines
  6983. (if (or (not blank)
  6984. (and blank (not (org-previous-line-empty-p))))
  6985. (org-N-empty-lines-before-current (if blank 1 0))))
  6986. (run-hooks 'org-insert-heading-hook)))))))
  6987. (defun org-N-empty-lines-before-current (N)
  6988. "Make the number of empty lines before current exactly N.
  6989. So this will delete or add empty lines."
  6990. (save-excursion
  6991. (goto-char (point-at-bol))
  6992. (if (looking-back "\\s-+" nil 'greedy)
  6993. (replace-match ""))
  6994. (or (bobp) (insert "\n"))
  6995. (while (> N 0)
  6996. (insert "\n")
  6997. (setq N (1- N)))))
  6998. (defun org-get-heading (&optional no-tags no-todo)
  6999. "Return the heading of the current entry, without the stars.
  7000. When NO-TAGS is non-nil, don't include tags.
  7001. When NO-TODO is non-nil, don't include TODO keywords."
  7002. (save-excursion
  7003. (org-back-to-heading t)
  7004. (cond
  7005. ((and no-tags no-todo)
  7006. (looking-at org-complex-heading-regexp)
  7007. (match-string 4))
  7008. (no-tags
  7009. (looking-at (concat org-outline-regexp
  7010. "\\(.*?\\)"
  7011. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7012. (match-string 1))
  7013. (no-todo
  7014. (looking-at org-todo-line-regexp)
  7015. (match-string 3))
  7016. (t (looking-at org-heading-regexp)
  7017. (match-string 2)))))
  7018. (defvar orgstruct-mode) ; defined below
  7019. (defun org-heading-components ()
  7020. "Return the components of the current heading.
  7021. This is a list with the following elements:
  7022. - the level as an integer
  7023. - the reduced level, different if `org-odd-levels-only' is set.
  7024. - the TODO keyword, or nil
  7025. - the priority character, like ?A, or nil if no priority is given
  7026. - the headline text itself, or the tags string if no headline text
  7027. - the tags string, or nil."
  7028. (save-excursion
  7029. (org-back-to-heading t)
  7030. (if (let (case-fold-search)
  7031. (looking-at
  7032. (if orgstruct-mode
  7033. org-heading-regexp
  7034. org-complex-heading-regexp)))
  7035. (if orgstruct-mode
  7036. (list (length (match-string 1))
  7037. (org-reduced-level (length (match-string 1)))
  7038. nil
  7039. nil
  7040. (match-string 2)
  7041. nil)
  7042. (list (length (match-string 1))
  7043. (org-reduced-level (length (match-string 1)))
  7044. (org-match-string-no-properties 2)
  7045. (and (match-end 3) (aref (match-string 3) 2))
  7046. (org-match-string-no-properties 4)
  7047. (org-match-string-no-properties 5))))))
  7048. (defun org-get-entry ()
  7049. "Get the entry text, after heading, entire subtree."
  7050. (save-excursion
  7051. (org-back-to-heading t)
  7052. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7053. (defun org-insert-heading-after-current ()
  7054. "Insert a new heading with same level as current, after current subtree."
  7055. (interactive)
  7056. (org-back-to-heading)
  7057. (org-insert-heading)
  7058. (org-move-subtree-down)
  7059. (end-of-line 1))
  7060. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  7061. "Insert heading with `org-insert-heading-respect-content' set to t."
  7062. (interactive "P")
  7063. (let ((org-insert-heading-respect-content t))
  7064. (org-insert-heading '(4) invisible-ok)))
  7065. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7066. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7067. (interactive "P")
  7068. (let ((org-insert-heading-respect-content t))
  7069. (org-insert-todo-heading force-state '(4))))
  7070. (defun org-insert-todo-heading (arg &optional force-heading)
  7071. "Insert a new heading with the same level and TODO state as current heading.
  7072. If the heading has no TODO state, or if the state is DONE, use the first
  7073. state (TODO by default). Also with one prefix arg, force first state. With
  7074. two prefix args, force inserting at the end of the parent subtree."
  7075. (interactive "P")
  7076. (when (or force-heading (not (org-insert-item 'checkbox)))
  7077. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7078. force-heading))
  7079. (save-excursion
  7080. (org-back-to-heading)
  7081. (outline-previous-heading)
  7082. (looking-at org-todo-line-regexp))
  7083. (let*
  7084. ((new-mark-x
  7085. (if (or (equal arg '(4))
  7086. (not (match-beginning 2))
  7087. (member (match-string 2) org-done-keywords))
  7088. (car org-todo-keywords-1)
  7089. (match-string 2)))
  7090. (new-mark
  7091. (or
  7092. (run-hook-with-args-until-success
  7093. 'org-todo-get-default-hook new-mark-x nil)
  7094. new-mark-x)))
  7095. (beginning-of-line 1)
  7096. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7097. (if org-treat-insert-todo-heading-as-state-change
  7098. (org-todo new-mark)
  7099. (insert new-mark " "))))
  7100. (when org-provide-todo-statistics
  7101. (org-update-parent-todo-statistics))))
  7102. (defun org-insert-subheading (arg)
  7103. "Insert a new subheading and demote it.
  7104. Works for outline headings and for plain lists alike."
  7105. (interactive "P")
  7106. (org-insert-heading arg)
  7107. (cond
  7108. ((org-at-heading-p) (org-do-demote))
  7109. ((org-at-item-p) (org-indent-item))))
  7110. (defun org-insert-todo-subheading (arg)
  7111. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7112. Works for outline headings and for plain lists alike."
  7113. (interactive "P")
  7114. (org-insert-todo-heading arg)
  7115. (cond
  7116. ((org-at-heading-p) (org-do-demote))
  7117. ((org-at-item-p) (org-indent-item))))
  7118. ;;; Promotion and Demotion
  7119. (defvar org-after-demote-entry-hook nil
  7120. "Hook run after an entry has been demoted.
  7121. The cursor will be at the beginning of the entry.
  7122. When a subtree is being demoted, the hook will be called for each node.")
  7123. (defvar org-after-promote-entry-hook nil
  7124. "Hook run after an entry has been promoted.
  7125. The cursor will be at the beginning of the entry.
  7126. When a subtree is being promoted, the hook will be called for each node.")
  7127. (defun org-promote-subtree ()
  7128. "Promote the entire subtree.
  7129. See also `org-promote'."
  7130. (interactive)
  7131. (save-excursion
  7132. (org-with-limited-levels (org-map-tree 'org-promote)))
  7133. (org-fix-position-after-promote))
  7134. (defun org-demote-subtree ()
  7135. "Demote the entire subtree. See `org-demote'.
  7136. See also `org-promote'."
  7137. (interactive)
  7138. (save-excursion
  7139. (org-with-limited-levels (org-map-tree 'org-demote)))
  7140. (org-fix-position-after-promote))
  7141. (defun org-do-promote ()
  7142. "Promote the current heading higher up the tree.
  7143. If the region is active in `transient-mark-mode', promote all headings
  7144. in the region."
  7145. (interactive)
  7146. (save-excursion
  7147. (if (org-region-active-p)
  7148. (org-map-region 'org-promote (region-beginning) (region-end))
  7149. (org-promote)))
  7150. (org-fix-position-after-promote))
  7151. (defun org-do-demote ()
  7152. "Demote the current heading lower down the tree.
  7153. If the region is active in `transient-mark-mode', demote all headings
  7154. in the region."
  7155. (interactive)
  7156. (save-excursion
  7157. (if (org-region-active-p)
  7158. (org-map-region 'org-demote (region-beginning) (region-end))
  7159. (org-demote)))
  7160. (org-fix-position-after-promote))
  7161. (defun org-fix-position-after-promote ()
  7162. "Make sure that after pro/demotion cursor position is right."
  7163. (let ((pos (point)))
  7164. (when (save-excursion
  7165. (beginning-of-line 1)
  7166. (looking-at org-todo-line-regexp)
  7167. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7168. (cond ((eobp) (insert " "))
  7169. ((eolp) (insert " "))
  7170. ((equal (char-after) ?\ ) (forward-char 1))))))
  7171. (defun org-current-level ()
  7172. "Return the level of the current entry, or nil if before the first headline.
  7173. The level is the number of stars at the beginning of the headline."
  7174. (save-excursion
  7175. (org-with-limited-levels
  7176. (if (ignore-errors (org-back-to-heading t))
  7177. (funcall outline-level)))))
  7178. (defun org-get-previous-line-level ()
  7179. "Return the outline depth of the last headline before the current line.
  7180. Returns 0 for the first headline in the buffer, and nil if before the
  7181. first headline."
  7182. (and (org-current-level)
  7183. (or (and (/= (line-beginning-position) (point-min))
  7184. (save-excursion (beginning-of-line 0) (org-current-level)))
  7185. 0)))
  7186. (defun org-reduced-level (l)
  7187. "Compute the effective level of a heading.
  7188. This takes into account the setting of `org-odd-levels-only'."
  7189. (cond
  7190. ((zerop l) 0)
  7191. (org-odd-levels-only (1+ (floor (/ l 2))))
  7192. (t l)))
  7193. (defun org-level-increment ()
  7194. "Return the number of stars that will be added or removed at a
  7195. time to headlines when structure editing, based on the value of
  7196. `org-odd-levels-only'."
  7197. (if org-odd-levels-only 2 1))
  7198. (defun org-get-valid-level (level &optional change)
  7199. "Rectify a level change under the influence of `org-odd-levels-only'
  7200. LEVEL is a current level, CHANGE is by how much the level should be
  7201. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7202. even level numbers will become the next higher odd number."
  7203. (if org-odd-levels-only
  7204. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7205. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7206. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7207. (max 1 (+ level (or change 0)))))
  7208. (if (boundp 'define-obsolete-function-alias)
  7209. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7210. (define-obsolete-function-alias 'org-get-legal-level
  7211. 'org-get-valid-level)
  7212. (define-obsolete-function-alias 'org-get-legal-level
  7213. 'org-get-valid-level "23.1")))
  7214. (defun org-promote ()
  7215. "Promote the current heading higher up the tree.
  7216. If the region is active in `transient-mark-mode', promote all headings
  7217. in the region."
  7218. (org-back-to-heading t)
  7219. (let* ((level (save-match-data (funcall outline-level)))
  7220. (after-change-functions (remove 'flyspell-after-change-function
  7221. after-change-functions))
  7222. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7223. (diff (abs (- level (length up-head) -1))))
  7224. (cond ((and (= level 1) org-called-with-limited-levels
  7225. org-allow-promoting-top-level-subtree)
  7226. (replace-match "# " nil t))
  7227. ((= level 1)
  7228. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7229. (t (replace-match up-head nil t)))
  7230. ;; Fixup tag positioning
  7231. (unless (= level 1)
  7232. (and org-auto-align-tags (org-set-tags nil 'ignore-column))
  7233. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7234. (run-hooks 'org-after-promote-entry-hook)))
  7235. (defun org-demote ()
  7236. "Demote the current heading lower down the tree.
  7237. If the region is active in `transient-mark-mode', demote all headings
  7238. in the region."
  7239. (org-back-to-heading t)
  7240. (let* ((level (save-match-data (funcall outline-level)))
  7241. (after-change-functions (remove 'flyspell-after-change-function
  7242. after-change-functions))
  7243. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7244. (diff (abs (- level (length down-head) -1))))
  7245. (replace-match down-head nil t)
  7246. ;; Fixup tag positioning
  7247. (and org-auto-align-tags (org-set-tags nil 'ignore-column))
  7248. (if org-adapt-indentation (org-fixup-indentation diff))
  7249. (run-hooks 'org-after-demote-entry-hook)))
  7250. (defun org-cycle-level ()
  7251. "Cycle the level of an empty headline through possible states.
  7252. This goes first to child, then to parent, level, then up the hierarchy.
  7253. After top level, it switches back to sibling level."
  7254. (interactive)
  7255. (let ((org-adapt-indentation nil))
  7256. (when (org-point-at-end-of-empty-headline)
  7257. (setq this-command 'org-cycle-level) ; Only needed for caching
  7258. (let ((cur-level (org-current-level))
  7259. (prev-level (org-get-previous-line-level)))
  7260. (cond
  7261. ;; If first headline in file, promote to top-level.
  7262. ((= prev-level 0)
  7263. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7264. do (org-do-promote)))
  7265. ;; If same level as prev, demote one.
  7266. ((= prev-level cur-level)
  7267. (org-do-demote))
  7268. ;; If parent is top-level, promote to top level if not already.
  7269. ((= prev-level 1)
  7270. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7271. do (org-do-promote)))
  7272. ;; If top-level, return to prev-level.
  7273. ((= cur-level 1)
  7274. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7275. do (org-do-demote)))
  7276. ;; If less than prev-level, promote one.
  7277. ((< cur-level prev-level)
  7278. (org-do-promote))
  7279. ;; If deeper than prev-level, promote until higher than
  7280. ;; prev-level.
  7281. ((> cur-level prev-level)
  7282. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7283. do (org-do-promote))))
  7284. t))))
  7285. (defun org-map-tree (fun)
  7286. "Call FUN for every heading underneath the current one."
  7287. (org-back-to-heading)
  7288. (let ((level (funcall outline-level)))
  7289. (save-excursion
  7290. (funcall fun)
  7291. (while (and (progn
  7292. (outline-next-heading)
  7293. (> (funcall outline-level) level))
  7294. (not (eobp)))
  7295. (funcall fun)))))
  7296. (defun org-map-region (fun beg end)
  7297. "Call FUN for every heading between BEG and END."
  7298. (let ((org-ignore-region t))
  7299. (save-excursion
  7300. (setq end (copy-marker end))
  7301. (goto-char beg)
  7302. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7303. (< (point) end))
  7304. (funcall fun))
  7305. (while (and (progn
  7306. (outline-next-heading)
  7307. (< (point) end))
  7308. (not (eobp)))
  7309. (funcall fun)))))
  7310. (defvar org-property-end-re) ; silence byte-compiler
  7311. (defun org-fixup-indentation (diff)
  7312. "Change the indentation in the current entry by DIFF.
  7313. However, if any line in the current entry has no indentation, or if it
  7314. would end up with no indentation after the change, nothing at all is done."
  7315. (save-excursion
  7316. (let ((end (save-excursion (outline-next-heading)
  7317. (point-marker)))
  7318. (prohibit (if (> diff 0)
  7319. "^\\S-"
  7320. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7321. col)
  7322. (unless (save-excursion (end-of-line 1)
  7323. (re-search-forward prohibit end t))
  7324. (while (and (< (point) end)
  7325. (re-search-forward "^[ \t]+" end t))
  7326. (goto-char (match-end 0))
  7327. (setq col (current-column))
  7328. (if (< diff 0) (replace-match ""))
  7329. (org-indent-to-column (+ diff col))))
  7330. (move-marker end nil))))
  7331. (defun org-convert-to-odd-levels ()
  7332. "Convert an org-mode file with all levels allowed to one with odd levels.
  7333. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7334. level 5 etc."
  7335. (interactive)
  7336. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7337. (let ((outline-level 'org-outline-level)
  7338. (org-odd-levels-only nil) n)
  7339. (save-excursion
  7340. (goto-char (point-min))
  7341. (while (re-search-forward "^\\*\\*+ " nil t)
  7342. (setq n (- (length (match-string 0)) 2))
  7343. (while (>= (setq n (1- n)) 0)
  7344. (org-demote))
  7345. (end-of-line 1))))))
  7346. (defun org-convert-to-oddeven-levels ()
  7347. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7348. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7349. file contains a section with an even level, conversion would
  7350. destroy the structure of the file. An error is signaled in this
  7351. case."
  7352. (interactive)
  7353. (goto-char (point-min))
  7354. ;; First check if there are no even levels
  7355. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7356. (org-show-context t)
  7357. (error "Not all levels are odd in this file. Conversion not possible"))
  7358. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7359. (let ((outline-regexp org-outline-regexp)
  7360. (outline-level 'org-outline-level)
  7361. (org-odd-levels-only nil) n)
  7362. (save-excursion
  7363. (goto-char (point-min))
  7364. (while (re-search-forward "^\\*\\*+ " nil t)
  7365. (setq n (/ (1- (length (match-string 0))) 2))
  7366. (while (>= (setq n (1- n)) 0)
  7367. (org-promote))
  7368. (end-of-line 1))))))
  7369. (defun org-tr-level (n)
  7370. "Make N odd if required."
  7371. (if org-odd-levels-only (1+ (/ n 2)) n))
  7372. ;;; Vertical tree motion, cutting and pasting of subtrees
  7373. (defun org-move-subtree-up (&optional arg)
  7374. "Move the current subtree up past ARG headlines of the same level."
  7375. (interactive "p")
  7376. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7377. (defun org-move-subtree-down (&optional arg)
  7378. "Move the current subtree down past ARG headlines of the same level."
  7379. (interactive "p")
  7380. (setq arg (prefix-numeric-value arg))
  7381. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7382. 'org-get-last-sibling))
  7383. (ins-point (make-marker))
  7384. (cnt (abs arg))
  7385. (col (current-column))
  7386. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7387. ;; Select the tree
  7388. (org-back-to-heading)
  7389. (setq beg0 (point))
  7390. (save-excursion
  7391. (setq ne-beg (org-back-over-empty-lines))
  7392. (setq beg (point)))
  7393. (save-match-data
  7394. (save-excursion (outline-end-of-heading)
  7395. (setq folded (outline-invisible-p)))
  7396. (progn (org-end-of-subtree nil t)
  7397. (unless (eobp) (backward-char))))
  7398. (outline-next-heading)
  7399. (setq ne-end (org-back-over-empty-lines))
  7400. (setq end (point))
  7401. (goto-char beg0)
  7402. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7403. ;; include less whitespace
  7404. (save-excursion
  7405. (goto-char beg)
  7406. (forward-line (- ne-beg ne-end))
  7407. (setq beg (point))))
  7408. ;; Find insertion point, with error handling
  7409. (while (> cnt 0)
  7410. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7411. (progn (goto-char beg0)
  7412. (user-error "Cannot move past superior level or buffer limit")))
  7413. (setq cnt (1- cnt)))
  7414. (if (> arg 0)
  7415. ;; Moving forward - still need to move over subtree
  7416. (progn (org-end-of-subtree t t)
  7417. (save-excursion
  7418. (org-back-over-empty-lines)
  7419. (or (bolp) (newline)))))
  7420. (setq ne-ins (org-back-over-empty-lines))
  7421. (move-marker ins-point (point))
  7422. (setq txt (buffer-substring beg end))
  7423. (org-save-markers-in-region beg end)
  7424. (delete-region beg end)
  7425. (org-remove-empty-overlays-at beg)
  7426. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7427. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7428. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7429. (let ((bbb (point)))
  7430. (insert-before-markers txt)
  7431. (org-reinstall-markers-in-region bbb)
  7432. (move-marker ins-point bbb))
  7433. (or (bolp) (insert "\n"))
  7434. (setq ins-end (point))
  7435. (goto-char ins-point)
  7436. (org-skip-whitespace)
  7437. (when (and (< arg 0)
  7438. (org-first-sibling-p)
  7439. (> ne-ins ne-beg))
  7440. ;; Move whitespace back to beginning
  7441. (save-excursion
  7442. (goto-char ins-end)
  7443. (let ((kill-whole-line t))
  7444. (kill-line (- ne-ins ne-beg)) (point)))
  7445. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7446. (move-marker ins-point nil)
  7447. (if folded
  7448. (hide-subtree)
  7449. (org-show-entry)
  7450. (show-children)
  7451. (org-cycle-hide-drawers 'children))
  7452. (org-clean-visibility-after-subtree-move)
  7453. ;; move back to the initial column we were at
  7454. (move-to-column col)))
  7455. (defvar org-subtree-clip ""
  7456. "Clipboard for cut and paste of subtrees.
  7457. This is actually only a copy of the kill, because we use the normal kill
  7458. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7459. (defvar org-subtree-clip-folded nil
  7460. "Was the last copied subtree folded?
  7461. This is used to fold the tree back after pasting.")
  7462. (defun org-cut-subtree (&optional n)
  7463. "Cut the current subtree into the clipboard.
  7464. With prefix arg N, cut this many sequential subtrees.
  7465. This is a short-hand for marking the subtree and then cutting it."
  7466. (interactive "p")
  7467. (org-copy-subtree n 'cut))
  7468. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7469. "Copy the current subtree it in the clipboard.
  7470. With prefix arg N, copy this many sequential subtrees.
  7471. This is a short-hand for marking the subtree and then copying it.
  7472. If CUT is non-nil, actually cut the subtree.
  7473. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7474. of some markers in the region, even if CUT is non-nil. This is
  7475. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7476. (interactive "p")
  7477. (let (beg end folded (beg0 (point)))
  7478. (if (org-called-interactively-p 'any)
  7479. (org-back-to-heading nil) ; take what looks like a subtree
  7480. (org-back-to-heading t)) ; take what is really there
  7481. (setq beg (point))
  7482. (skip-chars-forward " \t\r\n")
  7483. (save-match-data
  7484. (if nosubtrees
  7485. (outline-next-heading)
  7486. (save-excursion (outline-end-of-heading)
  7487. (setq folded (outline-invisible-p)))
  7488. (condition-case nil
  7489. (org-forward-heading-same-level (1- n) t)
  7490. (error nil))
  7491. (org-end-of-subtree t t)))
  7492. (setq end (point))
  7493. (goto-char beg0)
  7494. (when (> end beg)
  7495. (setq org-subtree-clip-folded folded)
  7496. (when (or cut force-store-markers)
  7497. (org-save-markers-in-region beg end))
  7498. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7499. (setq org-subtree-clip (current-kill 0))
  7500. (message "%s: Subtree(s) with %d characters"
  7501. (if cut "Cut" "Copied")
  7502. (length org-subtree-clip)))))
  7503. (defun org-paste-subtree (&optional level tree for-yank)
  7504. "Paste the clipboard as a subtree, with modification of headline level.
  7505. The entire subtree is promoted or demoted in order to match a new headline
  7506. level.
  7507. If the cursor is at the beginning of a headline, the same level as
  7508. that headline is used to paste the tree.
  7509. If not, the new level is derived from the *visible* headings
  7510. before and after the insertion point, and taken to be the inferior headline
  7511. level of the two. So if the previous visible heading is level 3 and the
  7512. next is level 4 (or vice versa), level 4 will be used for insertion.
  7513. This makes sure that the subtree remains an independent subtree and does
  7514. not swallow low level entries.
  7515. You can also force a different level, either by using a numeric prefix
  7516. argument, or by inserting the heading marker by hand. For example, if the
  7517. cursor is after \"*****\", then the tree will be shifted to level 5.
  7518. If optional TREE is given, use this text instead of the kill ring.
  7519. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7520. move back over whitespace before inserting, and move point to the end of
  7521. the inserted text when done."
  7522. (interactive "P")
  7523. (setq tree (or tree (and kill-ring (current-kill 0))))
  7524. (unless (org-kill-is-subtree-p tree)
  7525. (user-error "%s"
  7526. (substitute-command-keys
  7527. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7528. (org-with-limited-levels
  7529. (let* ((visp (not (outline-invisible-p)))
  7530. (txt tree)
  7531. (^re_ "\\(\\*+\\)[ \t]*")
  7532. (old-level (if (string-match org-outline-regexp-bol txt)
  7533. (- (match-end 0) (match-beginning 0) 1)
  7534. -1))
  7535. (force-level (cond (level (prefix-numeric-value level))
  7536. ((and (looking-at "[ \t]*$")
  7537. (string-match
  7538. "^\\*+$" (buffer-substring
  7539. (point-at-bol) (point))))
  7540. (- (match-end 1) (match-beginning 1)))
  7541. ((and (bolp)
  7542. (looking-at org-outline-regexp))
  7543. (- (match-end 0) (point) 1))))
  7544. (previous-level (save-excursion
  7545. (condition-case nil
  7546. (progn
  7547. (outline-previous-visible-heading 1)
  7548. (if (looking-at ^re_)
  7549. (- (match-end 0) (match-beginning 0) 1)
  7550. 1))
  7551. (error 1))))
  7552. (next-level (save-excursion
  7553. (condition-case nil
  7554. (progn
  7555. (or (looking-at org-outline-regexp)
  7556. (outline-next-visible-heading 1))
  7557. (if (looking-at ^re_)
  7558. (- (match-end 0) (match-beginning 0) 1)
  7559. 1))
  7560. (error 1))))
  7561. (new-level (or force-level (max previous-level next-level)))
  7562. (shift (if (or (= old-level -1)
  7563. (= new-level -1)
  7564. (= old-level new-level))
  7565. 0
  7566. (- new-level old-level)))
  7567. (delta (if (> shift 0) -1 1))
  7568. (func (if (> shift 0) 'org-demote 'org-promote))
  7569. (org-odd-levels-only nil)
  7570. beg end newend)
  7571. ;; Remove the forced level indicator
  7572. (if force-level
  7573. (delete-region (point-at-bol) (point)))
  7574. ;; Paste
  7575. (beginning-of-line (if (bolp) 1 2))
  7576. (setq beg (point))
  7577. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7578. (insert-before-markers txt)
  7579. (unless (string-match "\n\\'" txt) (insert "\n"))
  7580. (setq newend (point))
  7581. (org-reinstall-markers-in-region beg)
  7582. (setq end (point))
  7583. (goto-char beg)
  7584. (skip-chars-forward " \t\n\r")
  7585. (setq beg (point))
  7586. (if (and (outline-invisible-p) visp)
  7587. (save-excursion (outline-show-heading)))
  7588. ;; Shift if necessary
  7589. (unless (= shift 0)
  7590. (save-restriction
  7591. (narrow-to-region beg end)
  7592. (while (not (= shift 0))
  7593. (org-map-region func (point-min) (point-max))
  7594. (setq shift (+ delta shift)))
  7595. (goto-char (point-min))
  7596. (setq newend (point-max))))
  7597. (when (or (org-called-interactively-p 'interactive) for-yank)
  7598. (message "Clipboard pasted as level %d subtree" new-level))
  7599. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7600. kill-ring
  7601. (eq org-subtree-clip (current-kill 0))
  7602. org-subtree-clip-folded)
  7603. ;; The tree was folded before it was killed/copied
  7604. (hide-subtree))
  7605. (and for-yank (goto-char newend)))))
  7606. (defun org-kill-is-subtree-p (&optional txt)
  7607. "Check if the current kill is an outline subtree, or a set of trees.
  7608. Returns nil if kill does not start with a headline, or if the first
  7609. headline level is not the largest headline level in the tree.
  7610. So this will actually accept several entries of equal levels as well,
  7611. which is OK for `org-paste-subtree'.
  7612. If optional TXT is given, check this string instead of the current kill."
  7613. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7614. (re (org-get-limited-outline-regexp))
  7615. (^re (concat "^" re))
  7616. (start-level (and kill
  7617. (string-match
  7618. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7619. kill)
  7620. (- (match-end 2) (match-beginning 2) 1)))
  7621. (start (1+ (or (match-beginning 2) -1))))
  7622. (if (not start-level)
  7623. (progn
  7624. nil) ;; does not even start with a heading
  7625. (catch 'exit
  7626. (while (setq start (string-match ^re kill (1+ start)))
  7627. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7628. (throw 'exit nil)))
  7629. t))))
  7630. (defvar org-markers-to-move nil
  7631. "Markers that should be moved with a cut-and-paste operation.
  7632. Those markers are stored together with their positions relative to
  7633. the start of the region.")
  7634. (defun org-save-markers-in-region (beg end)
  7635. "Check markers in region.
  7636. If these markers are between BEG and END, record their position relative
  7637. to BEG, so that after moving the block of text, we can put the markers back
  7638. into place.
  7639. This function gets called just before an entry or tree gets cut from the
  7640. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7641. called immediately, to move the markers with the entries."
  7642. (setq org-markers-to-move nil)
  7643. (when (featurep 'org-clock)
  7644. (org-clock-save-markers-for-cut-and-paste beg end))
  7645. (when (featurep 'org-agenda)
  7646. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7647. (defun org-check-and-save-marker (marker beg end)
  7648. "Check if MARKER is between BEG and END.
  7649. If yes, remember the marker and the distance to BEG."
  7650. (when (and (marker-buffer marker)
  7651. (equal (marker-buffer marker) (current-buffer)))
  7652. (if (and (>= marker beg) (< marker end))
  7653. (push (cons marker (- marker beg)) org-markers-to-move))))
  7654. (defun org-reinstall-markers-in-region (beg)
  7655. "Move all remembered markers to their position relative to BEG."
  7656. (mapc (lambda (x)
  7657. (move-marker (car x) (+ beg (cdr x))))
  7658. org-markers-to-move)
  7659. (setq org-markers-to-move nil))
  7660. (defun org-narrow-to-subtree ()
  7661. "Narrow buffer to the current subtree."
  7662. (interactive)
  7663. (save-excursion
  7664. (save-match-data
  7665. (org-with-limited-levels
  7666. (narrow-to-region
  7667. (progn (org-back-to-heading t) (point))
  7668. (progn (org-end-of-subtree t t)
  7669. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7670. (point)))))))
  7671. (defun org-narrow-to-block ()
  7672. "Narrow buffer to the current block."
  7673. (interactive)
  7674. (let* ((case-fold-search t)
  7675. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7676. "^[ \t]*#\\+end_.*")))
  7677. (if blockp
  7678. (narrow-to-region (car blockp) (cdr blockp))
  7679. (user-error "Not in a block"))))
  7680. (eval-when-compile
  7681. (defvar org-property-drawer-re))
  7682. (defvar org-property-start-re) ;; defined below
  7683. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7684. "Clone the task (subtree) at point N times.
  7685. The clones will be inserted as siblings.
  7686. In interactive use, the user will be prompted for the number of
  7687. clones to be produced. If the entry has a timestamp, the user
  7688. will also be prompted for a time shift, which may be a repeater
  7689. as used in time stamps, for example `+3d'. To disable this,
  7690. you can call the function with a universal prefix argument.
  7691. When a valid repeater is given and the entry contains any time
  7692. stamps, the clones will become a sequence in time, with time
  7693. stamps in the subtree shifted for each clone produced. If SHIFT
  7694. is nil or the empty string, time stamps will be left alone. The
  7695. ID property of the original subtree is removed.
  7696. If the original subtree did contain time stamps with a repeater,
  7697. the following will happen:
  7698. - the repeater will be removed in each clone
  7699. - an additional clone will be produced, with the current, unshifted
  7700. date(s) in the entry.
  7701. - the original entry will be placed *after* all the clones, with
  7702. repeater intact.
  7703. - the start days in the repeater in the original entry will be shifted
  7704. to past the last clone.
  7705. In this way you can spell out a number of instances of a repeating task,
  7706. and still retain the repeater to cover future instances of the task."
  7707. (interactive "nNumber of clones to produce: ")
  7708. (let ((shift
  7709. (or shift
  7710. (if (and (not (equal current-prefix-arg '(4)))
  7711. (save-excursion
  7712. (re-search-forward org-ts-regexp-both
  7713. (save-excursion
  7714. (org-end-of-subtree t)
  7715. (point)) t)))
  7716. (read-from-minibuffer
  7717. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7718. ""))) ;; No time shift
  7719. (n-no-remove -1)
  7720. (drawer-re org-drawer-regexp)
  7721. beg end template task idprop
  7722. shift-n shift-what doshift nmin nmax)
  7723. (if (not (and (integerp n) (> n 0)))
  7724. (user-error "Invalid number of replications %s" n))
  7725. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7726. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7727. shift)))
  7728. (user-error "Invalid shift specification %s" shift))
  7729. (when doshift
  7730. (setq shift-n (string-to-number (match-string 1 shift))
  7731. shift-what (cdr (assoc (match-string 2 shift)
  7732. '(("d" . day) ("w" . week)
  7733. ("m" . month) ("y" . year))))))
  7734. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7735. (setq nmin 1 nmax n)
  7736. (org-back-to-heading t)
  7737. (setq beg (point))
  7738. (setq idprop (org-entry-get nil "ID"))
  7739. (org-end-of-subtree t t)
  7740. (or (bolp) (insert "\n"))
  7741. (setq end (point))
  7742. (setq template (buffer-substring beg end))
  7743. (when (and doshift
  7744. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7745. (delete-region beg end)
  7746. (setq end beg)
  7747. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7748. (goto-char end)
  7749. (loop for n from nmin to nmax do
  7750. ;; prepare clone
  7751. (with-temp-buffer
  7752. (insert template)
  7753. (org-mode)
  7754. (goto-char (point-min))
  7755. (org-show-subtree)
  7756. (and idprop (if org-clone-delete-id
  7757. (org-entry-delete nil "ID")
  7758. (org-id-get-create t)))
  7759. (unless (= n 0)
  7760. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7761. (kill-whole-line))
  7762. (goto-char (point-min))
  7763. (while (re-search-forward drawer-re nil t)
  7764. (org-remove-empty-drawer-at (point))))
  7765. (goto-char (point-min))
  7766. (when doshift
  7767. (while (re-search-forward org-ts-regexp-both nil t)
  7768. (org-timestamp-change (* n shift-n) shift-what))
  7769. (unless (= n n-no-remove)
  7770. (goto-char (point-min))
  7771. (while (re-search-forward org-ts-regexp nil t)
  7772. (save-excursion
  7773. (goto-char (match-beginning 0))
  7774. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7775. (delete-region (match-beginning 1) (match-end 1)))))))
  7776. (setq task (buffer-string)))
  7777. (insert task))
  7778. (goto-char beg)))
  7779. ;;; Outline Sorting
  7780. (defun org-sort (with-case)
  7781. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7782. Optional argument WITH-CASE means sort case-sensitively."
  7783. (interactive "P")
  7784. (cond
  7785. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7786. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7787. (t
  7788. (org-call-with-arg 'org-sort-entries with-case))))
  7789. (defun org-sort-remove-invisible (s)
  7790. "Remove invisible links from string S."
  7791. (remove-text-properties 0 (length s) org-rm-props s)
  7792. (while (string-match org-bracket-link-regexp s)
  7793. (setq s (replace-match (if (match-end 2)
  7794. (match-string 3 s)
  7795. (match-string 1 s)) t t s)))
  7796. (let ((st (format " %s " s)))
  7797. (while (string-match org-emph-re st)
  7798. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7799. (setq s (substring st 1 -1)))
  7800. s)
  7801. (defvar org-priority-regexp) ; defined later in the file
  7802. (defvar org-after-sorting-entries-or-items-hook nil
  7803. "Hook that is run after a bunch of entries or items have been sorted.
  7804. When children are sorted, the cursor is in the parent line when this
  7805. hook gets called. When a region or a plain list is sorted, the cursor
  7806. will be in the first entry of the sorted region/list.")
  7807. (defun org-sort-entries
  7808. (&optional with-case sorting-type getkey-func compare-func property)
  7809. "Sort entries on a certain level of an outline tree.
  7810. If there is an active region, the entries in the region are sorted.
  7811. Else, if the cursor is before the first entry, sort the top-level items.
  7812. Else, the children of the entry at point are sorted.
  7813. Sorting can be alphabetically, numerically, by date/time as given by
  7814. a time stamp, by a property, by priority order, or by a custom function.
  7815. The command prompts for the sorting type unless it has been given to the
  7816. function through the SORTING-TYPE argument, which needs to be a character,
  7817. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7818. the precise meaning of each character:
  7819. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7820. c By creation time, which is assumed to be the first inactive time stamp
  7821. at the beginning of a line.
  7822. d By deadline date/time.
  7823. k By clocking time.
  7824. n Numerically, by converting the beginning of the entry/item to a number.
  7825. o By order of TODO keywords.
  7826. p By priority according to the cookie.
  7827. r By the value of a property.
  7828. s By scheduled date/time.
  7829. t By date/time, either the first active time stamp in the entry, or, if
  7830. none exist, by the first inactive one.
  7831. Capital letters will reverse the sort order.
  7832. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7833. called with point at the beginning of the record. It must return either
  7834. a string or a number that should serve as the sorting key for that record.
  7835. Comparing entries ignores case by default. However, with an optional argument
  7836. WITH-CASE, the sorting considers case as well.
  7837. Sorting is done against the visible part of the headlines, it ignores hidden
  7838. links.
  7839. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7840. (interactive "P")
  7841. (let ((case-func (if with-case 'identity 'downcase))
  7842. (cmstr
  7843. ;; The clock marker is lost when using `sort-subr', let's
  7844. ;; store the clocking string.
  7845. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7846. (save-excursion
  7847. (goto-char org-clock-marker)
  7848. (looking-back "^.*") (match-string-no-properties 0))))
  7849. start beg end stars re re2
  7850. txt what tmp)
  7851. ;; Find beginning and end of region to sort
  7852. (cond
  7853. ((org-region-active-p)
  7854. ;; we will sort the region
  7855. (setq end (region-end)
  7856. what "region")
  7857. (goto-char (region-beginning))
  7858. (if (not (org-at-heading-p)) (outline-next-heading))
  7859. (setq start (point)))
  7860. ((or (org-at-heading-p)
  7861. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7862. ;; we will sort the children of the current headline
  7863. (org-back-to-heading)
  7864. (setq start (point)
  7865. end (progn (org-end-of-subtree t t)
  7866. (or (bolp) (insert "\n"))
  7867. (when (>= (org-back-over-empty-lines) 1)
  7868. (forward-line 1))
  7869. (point))
  7870. what "children")
  7871. (goto-char start)
  7872. (show-subtree)
  7873. (outline-next-heading))
  7874. (t
  7875. ;; we will sort the top-level entries in this file
  7876. (goto-char (point-min))
  7877. (or (org-at-heading-p) (outline-next-heading))
  7878. (setq start (point))
  7879. (goto-char (point-max))
  7880. (beginning-of-line 1)
  7881. (when (looking-at ".*?\\S-")
  7882. ;; File ends in a non-white line
  7883. (end-of-line 1)
  7884. (insert "\n"))
  7885. (setq end (point-max))
  7886. (setq what "top-level")
  7887. (goto-char start)
  7888. (show-all)))
  7889. (setq beg (point))
  7890. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7891. (looking-at "\\(\\*+\\)")
  7892. (setq stars (match-string 1)
  7893. re (concat "^" (regexp-quote stars) " +")
  7894. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7895. txt (buffer-substring beg end))
  7896. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7897. (if (and (not (equal stars "*")) (string-match re2 txt))
  7898. (user-error "Region to sort contains a level above the first entry"))
  7899. (unless sorting-type
  7900. (message
  7901. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7902. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  7903. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  7904. what)
  7905. (setq sorting-type (read-char-exclusive))
  7906. (unless getkey-func
  7907. (and (= (downcase sorting-type) ?f)
  7908. (setq getkey-func
  7909. (org-icompleting-read "Sort using function: "
  7910. obarray 'fboundp t nil nil))
  7911. (setq getkey-func (intern getkey-func))))
  7912. (and (= (downcase sorting-type) ?r)
  7913. (not property)
  7914. (setq property
  7915. (org-icompleting-read "Property: "
  7916. (mapcar 'list (org-buffer-property-keys t))
  7917. nil t))))
  7918. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  7919. (message "Sorting entries...")
  7920. (save-restriction
  7921. (narrow-to-region start end)
  7922. (let ((dcst (downcase sorting-type))
  7923. (case-fold-search nil)
  7924. (now (current-time)))
  7925. (sort-subr
  7926. (/= dcst sorting-type)
  7927. ;; This function moves to the beginning character of the "record" to
  7928. ;; be sorted.
  7929. (lambda nil
  7930. (if (re-search-forward re nil t)
  7931. (goto-char (match-beginning 0))
  7932. (goto-char (point-max))))
  7933. ;; This function moves to the last character of the "record" being
  7934. ;; sorted.
  7935. (lambda nil
  7936. (save-match-data
  7937. (condition-case nil
  7938. (outline-forward-same-level 1)
  7939. (error
  7940. (goto-char (point-max))))))
  7941. ;; This function returns the value that gets sorted against.
  7942. (lambda nil
  7943. (cond
  7944. ((= dcst ?n)
  7945. (if (looking-at org-complex-heading-regexp)
  7946. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7947. nil))
  7948. ((= dcst ?a)
  7949. (if (looking-at org-complex-heading-regexp)
  7950. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7951. nil))
  7952. ((= dcst ?k)
  7953. (or (get-text-property (point) :org-clock-minutes) 0))
  7954. ((= dcst ?t)
  7955. (let ((end (save-excursion (outline-next-heading) (point))))
  7956. (if (or (re-search-forward org-ts-regexp end t)
  7957. (re-search-forward org-ts-regexp-both end t))
  7958. (org-time-string-to-seconds (match-string 0))
  7959. (org-float-time now))))
  7960. ((= dcst ?c)
  7961. (let ((end (save-excursion (outline-next-heading) (point))))
  7962. (if (re-search-forward
  7963. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7964. end t)
  7965. (org-time-string-to-seconds (match-string 0))
  7966. (org-float-time now))))
  7967. ((= dcst ?s)
  7968. (let ((end (save-excursion (outline-next-heading) (point))))
  7969. (if (re-search-forward org-scheduled-time-regexp end t)
  7970. (org-time-string-to-seconds (match-string 1))
  7971. (org-float-time now))))
  7972. ((= dcst ?d)
  7973. (let ((end (save-excursion (outline-next-heading) (point))))
  7974. (if (re-search-forward org-deadline-time-regexp end t)
  7975. (org-time-string-to-seconds (match-string 1))
  7976. (org-float-time now))))
  7977. ((= dcst ?p)
  7978. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7979. (string-to-char (match-string 2))
  7980. org-default-priority))
  7981. ((= dcst ?r)
  7982. (or (org-entry-get nil property) ""))
  7983. ((= dcst ?o)
  7984. (if (looking-at org-complex-heading-regexp)
  7985. (let* ((m (match-string 2))
  7986. (s (if (member m org-done-keywords) '- '+)))
  7987. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  7988. ((= dcst ?f)
  7989. (if getkey-func
  7990. (progn
  7991. (setq tmp (funcall getkey-func))
  7992. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7993. tmp)
  7994. (error "Invalid key function `%s'" getkey-func)))
  7995. (t (error "Invalid sorting type `%c'" sorting-type))))
  7996. nil
  7997. (cond
  7998. ((= dcst ?a) 'string<)
  7999. ((= dcst ?f) compare-func)
  8000. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8001. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8002. ;; Reset the clock marker if needed
  8003. (when cmstr
  8004. (save-excursion
  8005. (goto-char start)
  8006. (search-forward cmstr nil t)
  8007. (move-marker org-clock-marker (point))))
  8008. (message "Sorting entries...done")))
  8009. (defun org-do-sort (table what &optional with-case sorting-type)
  8010. "Sort TABLE of WHAT according to SORTING-TYPE.
  8011. The user will be prompted for the SORTING-TYPE if the call to this
  8012. function does not specify it.
  8013. WHAT is only for the prompt, to indicate what is being sorted.
  8014. The sorting key will be extracted from the car of the elements of
  8015. the table.
  8016. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  8017. (unless sorting-type
  8018. (message
  8019. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  8020. what)
  8021. (setq sorting-type (read-char-exclusive)))
  8022. (let ((dcst (downcase sorting-type))
  8023. extractfun comparefun)
  8024. ;; Define the appropriate functions
  8025. (cond
  8026. ((= dcst ?n)
  8027. (setq extractfun 'string-to-number
  8028. comparefun (if (= dcst sorting-type) '< '>)))
  8029. ((= dcst ?a)
  8030. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  8031. (lambda(x) (downcase (org-sort-remove-invisible x))))
  8032. comparefun (if (= dcst sorting-type)
  8033. 'string<
  8034. (lambda (a b) (and (not (string< a b))
  8035. (not (string= a b)))))))
  8036. ((= dcst ?t)
  8037. (setq extractfun
  8038. (lambda (x)
  8039. (cond ((or (string-match org-ts-regexp x)
  8040. (string-match org-ts-regexp-both x))
  8041. (org-float-time
  8042. (org-time-string-to-time (match-string 0 x))))
  8043. ((string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" x)
  8044. (org-hh:mm-string-to-minutes x))
  8045. (t 0)))
  8046. comparefun (if (= dcst sorting-type) '< '>)))
  8047. (t (error "Invalid sorting type `%c'" sorting-type)))
  8048. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  8049. table)
  8050. (lambda (a b) (funcall comparefun (car a) (car b))))))
  8051. ;;; The orgstruct minor mode
  8052. ;; Define a minor mode which can be used in other modes in order to
  8053. ;; integrate the org-mode structure editing commands.
  8054. ;; This is really a hack, because the org-mode structure commands use
  8055. ;; keys which normally belong to the major mode. Here is how it
  8056. ;; works: The minor mode defines all the keys necessary to operate the
  8057. ;; structure commands, but wraps the commands into a function which
  8058. ;; tests if the cursor is currently at a headline or a plain list
  8059. ;; item. If that is the case, the structure command is used,
  8060. ;; temporarily setting many Org-mode variables like regular
  8061. ;; expressions for filling etc. However, when any of those keys is
  8062. ;; used at a different location, function uses `key-binding' to look
  8063. ;; up if the key has an associated command in another currently active
  8064. ;; keymap (minor modes, major mode, global), and executes that
  8065. ;; command. There might be problems if any of the keys is otherwise
  8066. ;; used as a prefix key.
  8067. (defcustom orgstruct-heading-prefix-regexp ""
  8068. "Regexp that matches the custom prefix of Org headlines in
  8069. orgstruct(++)-mode."
  8070. :group 'org
  8071. :version "24.4"
  8072. :package-version '(Org . "8.3")
  8073. :type 'regexp)
  8074. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8075. (defcustom orgstruct-setup-hook nil
  8076. "Hook run after orgstruct-mode-map is filled."
  8077. :group 'org
  8078. :version "24.4"
  8079. :package-version '(Org . "8.0")
  8080. :type 'hook)
  8081. (defvar orgstruct-initialized nil)
  8082. (defvar org-local-vars nil
  8083. "List of local variables, for use by `orgstruct-mode'.")
  8084. ;;;###autoload
  8085. (define-minor-mode orgstruct-mode
  8086. "Toggle the minor mode `orgstruct-mode'.
  8087. This mode is for using Org-mode structure commands in other
  8088. modes. The following keys behave as if Org-mode were active, if
  8089. the cursor is on a headline, or on a plain list item (both as
  8090. defined by Org-mode)."
  8091. nil " OrgStruct" (make-sparse-keymap)
  8092. (funcall (if orgstruct-mode
  8093. 'add-to-invisibility-spec
  8094. 'remove-from-invisibility-spec)
  8095. '(outline . t))
  8096. (when orgstruct-mode
  8097. (org-load-modules-maybe)
  8098. (unless orgstruct-initialized
  8099. (orgstruct-setup)
  8100. (setq orgstruct-initialized t))))
  8101. ;;;###autoload
  8102. (defun turn-on-orgstruct ()
  8103. "Unconditionally turn on `orgstruct-mode'."
  8104. (orgstruct-mode 1))
  8105. (defvar org-fb-vars nil)
  8106. (make-variable-buffer-local 'org-fb-vars)
  8107. (defun orgstruct++-mode (&optional arg)
  8108. "Toggle `orgstruct-mode', the enhanced version of it.
  8109. In addition to setting orgstruct-mode, this also exports all
  8110. indentation and autofilling variables from org-mode into the
  8111. buffer. It will also recognize item context in multiline items."
  8112. (interactive "P")
  8113. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8114. (if (< arg 1)
  8115. (progn (orgstruct-mode -1)
  8116. (mapc (lambda(v)
  8117. (org-set-local (car v)
  8118. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8119. org-fb-vars))
  8120. (orgstruct-mode 1)
  8121. (setq org-fb-vars nil)
  8122. (unless org-local-vars
  8123. (setq org-local-vars (org-get-local-variables)))
  8124. (let (var val)
  8125. (mapc
  8126. (lambda (x)
  8127. (when (string-match
  8128. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8129. (symbol-name (car x)))
  8130. (setq var (car x) val (nth 1 x))
  8131. (push (list var `(quote ,(eval var))) org-fb-vars)
  8132. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8133. org-local-vars)
  8134. (org-set-local 'orgstruct-is-++ t))))
  8135. (defvar orgstruct-is-++ nil
  8136. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8137. (make-variable-buffer-local 'orgstruct-is-++)
  8138. ;;;###autoload
  8139. (defun turn-on-orgstruct++ ()
  8140. "Unconditionally turn on `orgstruct++-mode'."
  8141. (orgstruct++-mode 1))
  8142. (defun orgstruct-error ()
  8143. "Error when there is no default binding for a structure key."
  8144. (interactive)
  8145. (funcall (if (fboundp 'user-error)
  8146. 'user-error
  8147. 'error)
  8148. "This key has no function outside structure elements"))
  8149. (defun orgstruct-setup ()
  8150. "Setup orgstruct keymap."
  8151. (dolist (cell '((org-demote . t)
  8152. (org-metaleft . t)
  8153. (org-metaright . t)
  8154. (org-promote . t)
  8155. (org-shiftmetaleft . t)
  8156. (org-shiftmetaright . t)
  8157. org-backward-element
  8158. org-backward-heading-same-level
  8159. org-ctrl-c-ret
  8160. org-ctrl-c-minus
  8161. org-ctrl-c-star
  8162. org-cycle
  8163. org-forward-heading-same-level
  8164. org-insert-heading
  8165. org-insert-heading-respect-content
  8166. org-kill-note-or-show-branches
  8167. org-mark-subtree
  8168. org-meta-return
  8169. org-metadown
  8170. org-metaup
  8171. org-narrow-to-subtree
  8172. org-promote-subtree
  8173. org-reveal
  8174. org-shiftdown
  8175. org-shiftleft
  8176. org-shiftmetadown
  8177. org-shiftmetaup
  8178. org-shiftright
  8179. org-shifttab
  8180. org-shifttab
  8181. org-shiftup
  8182. org-show-subtree
  8183. org-sort
  8184. org-up-element
  8185. outline-demote
  8186. outline-next-visible-heading
  8187. outline-previous-visible-heading
  8188. outline-promote
  8189. outline-up-heading
  8190. show-children))
  8191. (let ((f (or (car-safe cell) cell))
  8192. (disable-when-heading-prefix (cdr-safe cell)))
  8193. (when (fboundp f)
  8194. (let ((new-bindings))
  8195. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8196. (where-is-internal f outline-mode-map)))
  8197. (push binding new-bindings)
  8198. ;; TODO use local-function-key-map
  8199. (dolist (rep '(("<tab>" . "TAB")
  8200. ("<return>" . "RET")
  8201. ("<escape>" . "ESC")
  8202. ("<delete>" . "DEL")))
  8203. (setq binding (read-kbd-macro
  8204. (let ((case-fold-search))
  8205. (replace-regexp-in-string
  8206. (regexp-quote (cdr rep))
  8207. (car rep)
  8208. (key-description binding)))))
  8209. (pushnew binding new-bindings :test 'equal)))
  8210. (dolist (binding new-bindings)
  8211. (let ((key (lookup-key orgstruct-mode-map binding)))
  8212. (when (or (not key) (numberp key))
  8213. (condition-case nil
  8214. (org-defkey orgstruct-mode-map
  8215. binding
  8216. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8217. (error nil)))))))))
  8218. (run-hooks 'orgstruct-setup-hook))
  8219. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8220. "Create a function for binding in the structure minor mode.
  8221. FUN is the command to call inside a table. KEY is the key that
  8222. should be checked in for a command to execute outside of tables.
  8223. Non-nil `disable-when-heading-prefix' means to disable the command
  8224. if `orgstruct-heading-prefix-regexp' is not empty."
  8225. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8226. (let ((nname name)
  8227. (i 0))
  8228. (while (fboundp (intern nname))
  8229. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8230. (setq name (intern nname)))
  8231. (eval
  8232. (let ((bindings '((org-heading-regexp
  8233. (concat "^"
  8234. orgstruct-heading-prefix-regexp
  8235. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8236. (org-outline-regexp
  8237. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8238. (org-outline-regexp-bol
  8239. (concat "^" org-outline-regexp))
  8240. (outline-regexp org-outline-regexp)
  8241. (outline-heading-end-regexp "\n")
  8242. (outline-level 'org-outline-level)
  8243. (outline-heading-alist))))
  8244. `(defun ,name (arg)
  8245. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8246. "Outside of structure, run the binding of `"
  8247. (key-description key) "'."
  8248. (when disable-when-heading-prefix
  8249. (concat
  8250. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8251. "back to the default binding due to limitations of Org's implementation of\n"
  8252. "`" (symbol-name fun) "'.")))
  8253. (interactive "p")
  8254. (let* ((disable
  8255. ,(and disable-when-heading-prefix
  8256. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8257. (fallback
  8258. (or disable
  8259. (not
  8260. (let* ,bindings
  8261. (org-context-p 'headline 'item
  8262. ,(when (memq fun
  8263. '(org-insert-heading
  8264. org-insert-heading-respect-content
  8265. org-meta-return))
  8266. '(when orgstruct-is-++
  8267. 'item-body))))))))
  8268. (if fallback
  8269. (let* ((orgstruct-mode)
  8270. (binding
  8271. (loop with key = ,key
  8272. for rep in
  8273. '(nil
  8274. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8275. ("<\\([^>]*\\)return>" . "\\1RET")
  8276. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8277. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8278. do
  8279. (when rep
  8280. (setq key (read-kbd-macro
  8281. (let ((case-fold-search))
  8282. (replace-regexp-in-string
  8283. (car rep)
  8284. (cdr rep)
  8285. (key-description key))))))
  8286. thereis (key-binding key))))
  8287. (if (keymapp binding)
  8288. (org-set-transient-map binding)
  8289. (let ((func (or binding
  8290. (unless disable
  8291. 'orgstruct-error))))
  8292. (when func
  8293. (call-interactively func)))))
  8294. (org-run-like-in-org-mode
  8295. (lambda ()
  8296. (interactive)
  8297. (let* ,bindings
  8298. (call-interactively ',fun)))))))))
  8299. name))
  8300. (defun org-contextualize-keys (alist contexts)
  8301. "Return valid elements in ALIST depending on CONTEXTS.
  8302. `org-agenda-custom-commands' or `org-capture-templates' are the
  8303. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8304. or `org-capture-templates-contexts' are the associated contexts
  8305. definitions."
  8306. (let ((contexts
  8307. ;; normalize contexts
  8308. (mapcar
  8309. (lambda(c) (cond ((listp (cadr c))
  8310. (list (car c) (car c) (cadr c)))
  8311. ((string= "" (cadr c))
  8312. (list (car c) (car c) (caddr c)))
  8313. (t c))) contexts))
  8314. (a alist) c r s)
  8315. ;; loop over all commands or templates
  8316. (while (setq c (pop a))
  8317. (let (vrules repl)
  8318. (cond
  8319. ((not (assoc (car c) contexts))
  8320. (push c r))
  8321. ((and (assoc (car c) contexts)
  8322. (setq vrules (org-contextualize-validate-key
  8323. (car c) contexts)))
  8324. (mapc (lambda (vr)
  8325. (when (not (equal (car vr) (cadr vr)))
  8326. (setq repl vr))) vrules)
  8327. (if (not repl) (push c r)
  8328. (push (cadr repl) s)
  8329. (push
  8330. (cons (car c)
  8331. (cdr (or (assoc (cadr repl) alist)
  8332. (error "Undefined key `%s' as contextual replacement for `%s'"
  8333. (cadr repl) (car c)))))
  8334. r))))))
  8335. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8336. (delq
  8337. nil
  8338. (delete-dups
  8339. (mapcar (lambda (x)
  8340. (let ((tpl (car x)))
  8341. (when (not (delq
  8342. nil
  8343. (mapcar (lambda(y)
  8344. (equal y tpl)) s))) x)))
  8345. (reverse r))))))
  8346. (defun org-contextualize-validate-key (key contexts)
  8347. "Check CONTEXTS for agenda or capture KEY."
  8348. (let (r rr res)
  8349. (while (setq r (pop contexts))
  8350. (mapc
  8351. (lambda (rr)
  8352. (when
  8353. (and (equal key (car r))
  8354. (if (functionp rr) (funcall rr)
  8355. (or (and (eq (car rr) 'in-file)
  8356. (buffer-file-name)
  8357. (string-match (cdr rr) (buffer-file-name)))
  8358. (and (eq (car rr) 'in-mode)
  8359. (string-match (cdr rr) (symbol-name major-mode)))
  8360. (and (eq (car rr) 'in-buffer)
  8361. (string-match (cdr rr) (buffer-name)))
  8362. (when (and (eq (car rr) 'not-in-file)
  8363. (buffer-file-name))
  8364. (not (string-match (cdr rr) (buffer-file-name))))
  8365. (when (eq (car rr) 'not-in-mode)
  8366. (not (string-match (cdr rr) (symbol-name major-mode))))
  8367. (when (eq (car rr) 'not-in-buffer)
  8368. (not (string-match (cdr rr) (buffer-name)))))))
  8369. (push r res)))
  8370. (car (last r))))
  8371. (delete-dups (delq nil res))))
  8372. (defun org-context-p (&rest contexts)
  8373. "Check if local context is any of CONTEXTS.
  8374. Possible values in the list of contexts are `table', `headline', and `item'."
  8375. (let ((pos (point)))
  8376. (goto-char (point-at-bol))
  8377. (prog1 (or (and (memq 'table contexts)
  8378. (looking-at "[ \t]*|"))
  8379. (and (memq 'headline contexts)
  8380. (looking-at org-outline-regexp))
  8381. (and (memq 'item contexts)
  8382. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8383. (and (memq 'item-body contexts)
  8384. (org-in-item-p)))
  8385. (goto-char pos))))
  8386. (defun org-get-local-variables ()
  8387. "Return a list of all local variables in an Org mode buffer."
  8388. (let (varlist)
  8389. (with-current-buffer (get-buffer-create "*Org tmp*")
  8390. (erase-buffer)
  8391. (org-mode)
  8392. (setq varlist (buffer-local-variables)))
  8393. (kill-buffer "*Org tmp*")
  8394. (delq nil
  8395. (mapcar
  8396. (lambda (x)
  8397. (setq x
  8398. (if (symbolp x)
  8399. (list x)
  8400. (list (car x) (cdr x))))
  8401. (if (and (not (get (car x) 'org-state))
  8402. (string-match
  8403. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8404. (symbol-name (car x))))
  8405. x nil))
  8406. varlist))))
  8407. (defun org-clone-local-variables (from-buffer &optional regexp)
  8408. "Clone local variables from FROM-BUFFER.
  8409. Optional argument REGEXP selects variables to clone."
  8410. (mapc
  8411. (lambda (pair)
  8412. (and (symbolp (car pair))
  8413. (or (null regexp)
  8414. (string-match regexp (symbol-name (car pair))))
  8415. (set (make-local-variable (car pair))
  8416. (cdr pair))))
  8417. (buffer-local-variables from-buffer)))
  8418. ;;;###autoload
  8419. (defun org-run-like-in-org-mode (cmd)
  8420. "Run a command, pretending that the current buffer is in Org-mode.
  8421. This will temporarily bind local variables that are typically bound in
  8422. Org-mode to the values they have in Org-mode, and then interactively
  8423. call CMD."
  8424. (org-load-modules-maybe)
  8425. (unless org-local-vars
  8426. (setq org-local-vars (org-get-local-variables)))
  8427. (let (binds)
  8428. (dolist (var org-local-vars)
  8429. (when (or (not (boundp (car var)))
  8430. (eq (symbol-value (car var))
  8431. (default-value (car var))))
  8432. (push (list (car var) `(quote ,(cadr var))) binds)))
  8433. (eval `(let ,binds
  8434. (call-interactively (quote ,cmd))))))
  8435. (defun org-get-category (&optional pos force-refresh)
  8436. "Get the category applying to position POS."
  8437. (save-match-data
  8438. (if force-refresh (org-refresh-category-properties))
  8439. (let ((pos (or pos (point))))
  8440. (or (get-text-property pos 'org-category)
  8441. (progn (org-refresh-category-properties)
  8442. (get-text-property pos 'org-category))))))
  8443. ;;; Refresh properties
  8444. (defun org-refresh-category-properties ()
  8445. "Refresh category text properties in the buffer."
  8446. (let ((case-fold-search t)
  8447. (inhibit-read-only t)
  8448. (def-cat (cond
  8449. ((null org-category)
  8450. (if buffer-file-name
  8451. (file-name-sans-extension
  8452. (file-name-nondirectory buffer-file-name))
  8453. "???"))
  8454. ((symbolp org-category) (symbol-name org-category))
  8455. (t org-category)))
  8456. beg end cat pos optionp)
  8457. (org-with-silent-modifications
  8458. (save-excursion
  8459. (save-restriction
  8460. (widen)
  8461. (goto-char (point-min))
  8462. (put-text-property (point) (point-max) 'org-category def-cat)
  8463. (while (re-search-forward
  8464. "^[ \t]*\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8465. (setq pos (match-end 0)
  8466. optionp (equal (char-after (match-beginning 0)) ?#)
  8467. cat (org-trim (match-string 2)))
  8468. (if optionp
  8469. (setq beg (point-at-bol) end (point-max))
  8470. (org-back-to-heading t)
  8471. (setq beg (point) end (org-end-of-subtree t t)))
  8472. (put-text-property beg end 'org-category cat)
  8473. (goto-char pos)))))))
  8474. (defun org-refresh-stats-properties ()
  8475. "Refresh stats text properties in the buffer."
  8476. (let (stats)
  8477. (org-with-silent-modifications
  8478. (save-excursion
  8479. (save-restriction
  8480. (widen)
  8481. (goto-char (point-min))
  8482. (while (re-search-forward
  8483. (concat org-outline-regexp-bol ".*"
  8484. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8485. nil t)
  8486. (setq stats (cond ((equal (match-string 3) "0") 0)
  8487. ((match-string 2)
  8488. (/ (* (string-to-number (match-string 2)) 100)
  8489. (string-to-number (match-string 3))))
  8490. (t (string-to-number (match-string 1)))))
  8491. (org-back-to-heading t)
  8492. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8493. 'org-stats stats)))))))
  8494. (defun org-refresh-properties (dprop tprop)
  8495. "Refresh buffer text properties.
  8496. DPROP is the drawer property and TPROP is either the
  8497. corresponding text property to set, or an alist with each element
  8498. being a text property (as a symbol) and a function to apply to
  8499. the value of the drawer property."
  8500. (let ((case-fold-search t)
  8501. (inhibit-read-only t))
  8502. (org-with-silent-modifications
  8503. (save-excursion
  8504. (save-restriction
  8505. (widen)
  8506. (goto-char (point-min))
  8507. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8508. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8509. (defun org-refresh-property (tprop p)
  8510. "Refresh the buffer text property TPROP from the drawer property P.
  8511. The refresh happens only for the current tree (not subtree)."
  8512. (save-excursion
  8513. (org-back-to-heading t)
  8514. ;; tprop is a text property symbol
  8515. (if (symbolp tprop)
  8516. (put-text-property
  8517. (point) (or (outline-next-heading) (point-max)) tprop p)
  8518. ;; tprop is an alist with (properties . function) elements
  8519. (mapc (lambda(al)
  8520. (save-excursion
  8521. (put-text-property
  8522. (point-at-bol) (or (outline-next-heading) (point-max))
  8523. (car al)
  8524. (funcall (cdr al) p))))
  8525. tprop))))
  8526. ;;;; Link Stuff
  8527. ;;; Link abbreviations
  8528. (defun org-link-expand-abbrev (link)
  8529. "Apply replacements as defined in `org-link-abbrev-alist'."
  8530. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8531. (let* ((key (match-string 1 link))
  8532. (as (or (assoc key org-link-abbrev-alist-local)
  8533. (assoc key org-link-abbrev-alist)))
  8534. (tag (and (match-end 2) (match-string 3 link)))
  8535. rpl)
  8536. (if (not as)
  8537. link
  8538. (setq rpl (cdr as))
  8539. (cond
  8540. ((symbolp rpl) (funcall rpl tag))
  8541. ((string-match "%(\\([^)]+\\))" rpl)
  8542. (replace-match
  8543. (save-match-data
  8544. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8545. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8546. ((string-match "%h" rpl)
  8547. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8548. (t (concat rpl tag)))))
  8549. link))
  8550. ;;; Storing and inserting links
  8551. (defvar org-insert-link-history nil
  8552. "Minibuffer history for links inserted with `org-insert-link'.")
  8553. (defvar org-stored-links nil
  8554. "Contains the links stored with `org-store-link'.")
  8555. (defvar org-store-link-plist nil
  8556. "Plist with info about the most recently link created with `org-store-link'.")
  8557. (defvar org-link-protocols nil
  8558. "Link protocols added to Org-mode using `org-add-link-type'.")
  8559. (defvar org-store-link-functions nil
  8560. "List of functions that are called to create and store a link.
  8561. Each function will be called in turn until one returns a non-nil
  8562. value. Each function should check if it is responsible for creating
  8563. this link (for example by looking at the major mode).
  8564. If not, it must exit and return nil.
  8565. If yes, it should return a non-nil value after a calling
  8566. `org-store-link-props' with a list of properties and values.
  8567. Special properties are:
  8568. :type The link prefix, like \"http\". This must be given.
  8569. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8570. This is obligatory as well.
  8571. :description Optional default description for the second pair
  8572. of brackets in an Org-mode link. The user can still change
  8573. this when inserting this link into an Org-mode buffer.
  8574. In addition to these, any additional properties can be specified
  8575. and then used in capture templates.")
  8576. (defun org-add-link-type (type &optional follow export)
  8577. "Add TYPE to the list of `org-link-types'.
  8578. Re-compute all regular expressions depending on `org-link-types'
  8579. FOLLOW and EXPORT are two functions.
  8580. FOLLOW should take the link path as the single argument and do whatever
  8581. is necessary to follow the link, for example find a file or display
  8582. a mail message.
  8583. EXPORT should format the link path for export to one of the export formats.
  8584. It should be a function accepting three arguments:
  8585. path the path of the link, the text after the prefix (like \"http:\")
  8586. desc the description of the link, if any, or a description added by
  8587. org-export-normalize-links if there is none
  8588. format the export format, a symbol like `html' or `latex' or `ascii'..
  8589. The function may use the FORMAT information to return different values
  8590. depending on the format. The return value will be put literally into
  8591. the exported file. If the return value is nil, this means Org should
  8592. do what it normally does with links which do not have EXPORT defined.
  8593. Org-mode has a built-in default for exporting links. If you are happy with
  8594. this default, there is no need to define an export function for the link
  8595. type. For a simple example of an export function, see `org-bbdb.el'."
  8596. (add-to-list 'org-link-types type t)
  8597. (org-make-link-regexps)
  8598. (if (assoc type org-link-protocols)
  8599. (setcdr (assoc type org-link-protocols) (list follow export))
  8600. (push (list type follow export) org-link-protocols)))
  8601. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8602. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8603. ;;;###autoload
  8604. (defun org-store-link (arg)
  8605. "\\<org-mode-map>Store an org-link to the current location.
  8606. This link is added to `org-stored-links' and can later be inserted
  8607. into an org-buffer with \\[org-insert-link].
  8608. For some link types, a prefix arg is interpreted.
  8609. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8610. For file links, arg negates `org-context-in-file-links'.
  8611. A double prefix arg force skipping storing functions that are not
  8612. part of Org's core.
  8613. A triple prefix arg force storing a link for each line in the
  8614. active region."
  8615. (interactive "P")
  8616. (org-load-modules-maybe)
  8617. (if (and (equal arg '(64)) (org-region-active-p))
  8618. (save-excursion
  8619. (let ((end (region-end)))
  8620. (goto-char (region-beginning))
  8621. (set-mark (point))
  8622. (while (< (point-at-eol) end)
  8623. (move-end-of-line 1) (activate-mark)
  8624. (let (current-prefix-arg)
  8625. (call-interactively 'org-store-link))
  8626. (move-beginning-of-line 2)
  8627. (set-mark (point)))))
  8628. (org-with-limited-levels
  8629. (setq org-store-link-plist nil)
  8630. (let (link cpltxt desc description search
  8631. txt custom-id agenda-link sfuns sfunsn)
  8632. (cond
  8633. ;; Store a link using an external link type
  8634. ((and (not (equal arg '(16)))
  8635. (setq sfuns
  8636. (delq
  8637. nil (mapcar (lambda (f)
  8638. (let (fs) (if (funcall f) (push f fs))))
  8639. org-store-link-functions))
  8640. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8641. (or (and (cdr sfuns)
  8642. (funcall (intern
  8643. (completing-read
  8644. "Which function for creating the link? "
  8645. sfunsn nil t (car sfunsn)))))
  8646. (funcall (caar sfuns)))
  8647. (setq link (plist-get org-store-link-plist :link)
  8648. desc (or (plist-get org-store-link-plist
  8649. :description) link))))
  8650. ;; Store a link from a source code buffer
  8651. ((org-src-edit-buffer-p)
  8652. (let (label gc)
  8653. (while (or (not label)
  8654. (save-excursion
  8655. (save-restriction
  8656. (widen)
  8657. (goto-char (point-min))
  8658. (re-search-forward
  8659. (regexp-quote (format org-coderef-label-format label))
  8660. nil t))))
  8661. (when label (message "Label exists already") (sit-for 2))
  8662. (setq label (read-string "Code line label: " label)))
  8663. (end-of-line 1)
  8664. (setq link (format org-coderef-label-format label))
  8665. (setq gc (- 79 (length link)))
  8666. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8667. (insert link)
  8668. (setq link (concat "(" label ")") desc nil)))
  8669. ;; We are in the agenda, link to referenced location
  8670. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8671. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8672. (get-text-property (point) 'org-marker))))
  8673. (when m
  8674. (org-with-point-at m
  8675. (setq agenda-link
  8676. (if (org-called-interactively-p 'any)
  8677. (call-interactively 'org-store-link)
  8678. (org-store-link nil)))))))
  8679. ((eq major-mode 'calendar-mode)
  8680. (let ((cd (calendar-cursor-to-date)))
  8681. (setq link
  8682. (format-time-string
  8683. (car org-time-stamp-formats)
  8684. (apply 'encode-time
  8685. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8686. nil nil nil))))
  8687. (org-store-link-props :type "calendar" :date cd)))
  8688. ((eq major-mode 'help-mode)
  8689. (setq link (concat "help:" (save-excursion
  8690. (goto-char (point-min))
  8691. (looking-at "^[^ ]+")
  8692. (match-string 0))))
  8693. (org-store-link-props :type "help"))
  8694. ((eq major-mode 'w3-mode)
  8695. (setq cpltxt (if (and (buffer-name)
  8696. (not (string-match "Untitled" (buffer-name))))
  8697. (buffer-name)
  8698. (url-view-url t))
  8699. link (url-view-url t))
  8700. (org-store-link-props :type "w3" :url (url-view-url t)))
  8701. ((eq major-mode 'image-mode)
  8702. (setq cpltxt (concat "file:"
  8703. (abbreviate-file-name buffer-file-name))
  8704. link cpltxt)
  8705. (org-store-link-props :type "image" :file buffer-file-name))
  8706. ;; In dired, store a link to the file of the current line
  8707. ((derived-mode-p 'dired-mode)
  8708. (let ((file (dired-get-filename nil t)))
  8709. (setq file (if file
  8710. (abbreviate-file-name
  8711. (expand-file-name (dired-get-filename nil t)))
  8712. ;; otherwise, no file so use current directory.
  8713. default-directory))
  8714. (setq cpltxt (concat "file:" file)
  8715. link cpltxt)))
  8716. ((setq search (run-hook-with-args-until-success
  8717. 'org-create-file-search-functions))
  8718. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8719. "::" search))
  8720. (setq cpltxt (or description link)))
  8721. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8722. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8723. (cond
  8724. ;; Store a link using the target at point
  8725. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8726. (setq cpltxt
  8727. (concat "file:"
  8728. (abbreviate-file-name
  8729. (buffer-file-name (buffer-base-buffer)))
  8730. "::" (match-string 1))
  8731. link cpltxt))
  8732. ((and (featurep 'org-id)
  8733. (or (eq org-id-link-to-org-use-id t)
  8734. (and (org-called-interactively-p 'any)
  8735. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8736. (and (eq org-id-link-to-org-use-id
  8737. 'create-if-interactive-and-no-custom-id)
  8738. (not custom-id))))
  8739. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8740. ;; Store a link using the ID at point
  8741. (setq link (condition-case nil
  8742. (prog1 (org-id-store-link)
  8743. (setq desc (or (plist-get org-store-link-plist
  8744. :description)
  8745. "")))
  8746. (error
  8747. ;; Probably before first headline, link only to file
  8748. (concat "file:"
  8749. (abbreviate-file-name
  8750. (buffer-file-name (buffer-base-buffer))))))))
  8751. (t
  8752. ;; Just link to current headline
  8753. (setq cpltxt (concat "file:"
  8754. (abbreviate-file-name
  8755. (buffer-file-name (buffer-base-buffer)))))
  8756. ;; Add a context search string
  8757. (when (org-xor org-context-in-file-links arg)
  8758. (let* ((ee (org-element-at-point))
  8759. (et (org-element-type ee))
  8760. (ev (plist-get (cadr ee) :value))
  8761. (ek (plist-get (cadr ee) :key))
  8762. (eok (and (stringp ek) (string-match "name" ek))))
  8763. (setq txt (cond
  8764. ((org-at-heading-p) nil)
  8765. ((and (eq et 'keyword) eok) ev)
  8766. ((org-region-active-p)
  8767. (buffer-substring (region-beginning) (region-end)))))
  8768. (when (or (null txt) (string-match "\\S-" txt))
  8769. (setq cpltxt
  8770. (concat cpltxt "::"
  8771. (condition-case nil
  8772. (org-make-org-heading-search-string txt)
  8773. (error "")))
  8774. desc (or (and (eq et 'keyword) eok ev)
  8775. (nth 4 (ignore-errors (org-heading-components)))
  8776. "NONE")))))
  8777. (if (string-match "::\\'" cpltxt)
  8778. (setq cpltxt (substring cpltxt 0 -2)))
  8779. (setq link cpltxt))))
  8780. ((buffer-file-name (buffer-base-buffer))
  8781. ;; Just link to this file here.
  8782. (setq cpltxt (concat "file:"
  8783. (abbreviate-file-name
  8784. (buffer-file-name (buffer-base-buffer)))))
  8785. ;; Add a context string.
  8786. (when (org-xor org-context-in-file-links arg)
  8787. (setq txt (if (org-region-active-p)
  8788. (buffer-substring (region-beginning) (region-end))
  8789. (buffer-substring (point-at-bol) (point-at-eol))))
  8790. ;; Only use search option if there is some text.
  8791. (when (string-match "\\S-" txt)
  8792. (setq cpltxt
  8793. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8794. desc "NONE")))
  8795. (setq link cpltxt))
  8796. ((org-called-interactively-p 'interactive)
  8797. (user-error "No method for storing a link from this buffer"))
  8798. (t (setq link nil)))
  8799. ;; We're done setting link and desc, clean up
  8800. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8801. (setq link (or link cpltxt)
  8802. desc (or desc cpltxt))
  8803. (cond ((equal desc "NONE") (setq desc nil))
  8804. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8805. (let ((d0 (match-string 3 desc))
  8806. (p0 (match-string 5 desc)))
  8807. (setq desc
  8808. (replace-regexp-in-string
  8809. org-bracket-link-regexp
  8810. (concat (or p0 d0)
  8811. (if (equal (length (match-string 0 desc))
  8812. (length desc)) "*" "")) desc)))))
  8813. ;; Return the link
  8814. (if (not (and (or (org-called-interactively-p 'any)
  8815. executing-kbd-macro) link))
  8816. (or agenda-link (and link (org-make-link-string link desc)))
  8817. (push (list link desc) org-stored-links)
  8818. (message "Stored: %s" (or desc link))
  8819. (when custom-id
  8820. (setq link (concat "file:" (abbreviate-file-name
  8821. (buffer-file-name)) "::#" custom-id))
  8822. (push (list link desc) org-stored-links))
  8823. (car org-stored-links))))))
  8824. (defun org-store-link-props (&rest plist)
  8825. "Store link properties, extract names and addresses."
  8826. (let (x adr)
  8827. (when (setq x (plist-get plist :from))
  8828. (setq adr (mail-extract-address-components x))
  8829. (setq plist (plist-put plist :fromname (car adr)))
  8830. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8831. (when (setq x (plist-get plist :to))
  8832. (setq adr (mail-extract-address-components x))
  8833. (setq plist (plist-put plist :toname (car adr)))
  8834. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8835. (let ((from (plist-get plist :from))
  8836. (to (plist-get plist :to)))
  8837. (when (and from to org-from-is-user-regexp)
  8838. (setq plist
  8839. (plist-put plist :fromto
  8840. (if (string-match org-from-is-user-regexp from)
  8841. (concat "to %t")
  8842. (concat "from %f"))))))
  8843. (setq org-store-link-plist plist))
  8844. (defun org-add-link-props (&rest plist)
  8845. "Add these properties to the link property list."
  8846. (let (key value)
  8847. (while plist
  8848. (setq key (pop plist) value (pop plist))
  8849. (setq org-store-link-plist
  8850. (plist-put org-store-link-plist key value)))))
  8851. (defun org-email-link-description (&optional fmt)
  8852. "Return the description part of an email link.
  8853. This takes information from `org-store-link-plist' and formats it
  8854. according to FMT (default from `org-email-link-description-format')."
  8855. (setq fmt (or fmt org-email-link-description-format))
  8856. (let* ((p org-store-link-plist)
  8857. (to (plist-get p :toaddress))
  8858. (from (plist-get p :fromaddress))
  8859. (table
  8860. (list
  8861. (cons "%c" (plist-get p :fromto))
  8862. (cons "%F" (plist-get p :from))
  8863. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8864. (cons "%T" (plist-get p :to))
  8865. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8866. (cons "%s" (plist-get p :subject))
  8867. (cons "%d" (plist-get p :date))
  8868. (cons "%m" (plist-get p :message-id)))))
  8869. (when (string-match "%c" fmt)
  8870. ;; Check if the user wrote this message
  8871. (if (and org-from-is-user-regexp from to
  8872. (save-match-data (string-match org-from-is-user-regexp from)))
  8873. (setq fmt (replace-match "to %t" t t fmt))
  8874. (setq fmt (replace-match "from %f" t t fmt))))
  8875. (org-replace-escapes fmt table)))
  8876. (defun org-make-org-heading-search-string (&optional string)
  8877. "Make search string for the current headline or STRING."
  8878. (let ((s (or string
  8879. (and (derived-mode-p 'org-mode)
  8880. (save-excursion
  8881. (org-back-to-heading t)
  8882. (org-element-property :raw-value (org-element-at-point))))))
  8883. (lines org-context-in-file-links))
  8884. (or string (setq s (concat "*" s))) ; Add * for headlines
  8885. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8886. (when (and string (integerp lines) (> lines 0))
  8887. (let ((slines (org-split-string s "\n")))
  8888. (when (< lines (length slines))
  8889. (setq s (mapconcat
  8890. 'identity
  8891. (reverse (nthcdr (- (length slines) lines)
  8892. (reverse slines))) "\n")))))
  8893. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8894. (defun org-make-link-string (link &optional description)
  8895. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8896. (unless (string-match "\\S-" link)
  8897. (error "Empty link"))
  8898. (when (and description
  8899. (stringp description)
  8900. (not (string-match "\\S-" description)))
  8901. (setq description nil))
  8902. (when (stringp description)
  8903. ;; Remove brackets from the description, they are fatal.
  8904. (while (string-match "\\[" description)
  8905. (setq description (replace-match "{" t t description)))
  8906. (while (string-match "\\]" description)
  8907. (setq description (replace-match "}" t t description))))
  8908. (when (equal link description)
  8909. ;; No description needed, it is identical
  8910. (setq description nil))
  8911. (when (and (not description)
  8912. (not (string-match (org-image-file-name-regexp) link))
  8913. (not (equal link (org-link-escape link))))
  8914. (setq description (org-extract-attributes link)))
  8915. (setq link
  8916. (cond ((string-match (org-image-file-name-regexp) link) link)
  8917. ((string-match org-link-types-re link)
  8918. (concat (match-string 1 link)
  8919. (org-link-escape (substring link (match-end 1)))))
  8920. (t (org-link-escape link))))
  8921. (concat "[[" link "]"
  8922. (if description (concat "[" description "]") "")
  8923. "]"))
  8924. (defconst org-link-escape-chars
  8925. ;;%20 %5B %5D
  8926. '(?\ ?\[ ?\])
  8927. "List of characters that should be escaped in a link when stored to Org.
  8928. This is the list that is used for internal purposes.")
  8929. (defconst org-link-escape-chars-browser
  8930. ;;%20 %22
  8931. '(?\ ?\")
  8932. "List of characters to be escaped before handing over to the browser.
  8933. If you consider using this constant then you probably want to use
  8934. the function `org-link-escape-browser' instead. See there why
  8935. this constant is a candidate to be removed once Org drops support
  8936. for Emacs 24.1 and 24.2.")
  8937. (defun org-link-escape (text &optional table merge)
  8938. "Return percent escaped representation of TEXT.
  8939. TEXT is a string with the text to escape.
  8940. Optional argument TABLE is a list with characters that should be
  8941. escaped. When nil, `org-link-escape-chars' is used.
  8942. If optional argument MERGE is set, merge TABLE into
  8943. `org-link-escape-chars'."
  8944. ;; Don't escape chars in internal links
  8945. (if (string-match "^\\*[[:alnum:]]+" text)
  8946. text
  8947. (cond
  8948. ((and table merge)
  8949. (mapc (lambda (defchr)
  8950. (unless (member defchr table)
  8951. (setq table (cons defchr table))))
  8952. org-link-escape-chars))
  8953. ((null table)
  8954. (setq table org-link-escape-chars)))
  8955. (mapconcat
  8956. (lambda (char)
  8957. (if (or (member char table)
  8958. (and (or (< char 32) (= char ?\%) (> char 126))
  8959. org-url-hexify-p))
  8960. (mapconcat (lambda (sequence-element)
  8961. (format "%%%.2X" sequence-element))
  8962. (or (encode-coding-char char 'utf-8)
  8963. (error "Unable to percent escape character: %s"
  8964. (char-to-string char))) "")
  8965. (char-to-string char))) text "")))
  8966. (defun org-link-escape-browser (text)
  8967. "Escape some characters before handing over to the browser.
  8968. This function is a candidate to be removed together with the
  8969. constant `org-link-escape-chars-browser' once Org drops support
  8970. for Emacs 24.1 and 24.2. All calls to this function will have to
  8971. be replaced with `url-encode-url' which is available since Emacs
  8972. 24.3.1."
  8973. ;; Example with the Org link
  8974. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  8975. ;; to open the browser with +subject:"Release 8.2" filled into the
  8976. ;; query field: In this case the variable TEXT contains the
  8977. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  8978. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  8979. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  8980. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  8981. (if (fboundp 'url-encode-url)
  8982. (url-encode-url text)
  8983. (if (org-string-match-p
  8984. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  8985. text)
  8986. (org-link-escape text org-link-escape-chars-browser)
  8987. text)))
  8988. (defun org-link-unescape (str)
  8989. "Unhex hexified Unicode strings as returned from the JavaScript function
  8990. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8991. (unless (and (null str) (string= "" str))
  8992. (let ((pos 0) (case-fold-search t) unhexed)
  8993. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8994. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8995. (setq str (replace-match unhexed t t str))
  8996. (setq pos (+ pos (length unhexed))))))
  8997. str)
  8998. (defun org-link-unescape-compound (hex)
  8999. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9000. Note: this function also decodes single byte encodings like
  9001. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9002. (save-match-data
  9003. (let* ((bytes (cdr (split-string hex "%")))
  9004. (ret "")
  9005. (eat 0)
  9006. (sum 0))
  9007. (while bytes
  9008. (let* ((val (string-to-number (pop bytes) 16))
  9009. (shift-xor
  9010. (if (= 0 eat)
  9011. (cond
  9012. ((>= val 252) (cons 6 252))
  9013. ((>= val 248) (cons 5 248))
  9014. ((>= val 240) (cons 4 240))
  9015. ((>= val 224) (cons 3 224))
  9016. ((>= val 192) (cons 2 192))
  9017. (t (cons 0 0)))
  9018. (cons 6 128))))
  9019. (if (>= val 192) (setq eat (car shift-xor)))
  9020. (setq val (logxor val (cdr shift-xor)))
  9021. (setq sum (+ (lsh sum (car shift-xor)) val))
  9022. (if (> eat 0) (setq eat (- eat 1)))
  9023. (cond
  9024. ((= 0 eat) ;multi byte
  9025. (setq ret (concat ret (org-char-to-string sum)))
  9026. (setq sum 0))
  9027. ((not bytes) ; single byte(s)
  9028. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9029. ret)))
  9030. (defun org-link-unescape-single-byte-sequence (hex)
  9031. "Unhexify hex-encoded single byte character sequences."
  9032. (mapconcat (lambda (byte)
  9033. (char-to-string (string-to-number byte 16)))
  9034. (cdr (split-string hex "%")) ""))
  9035. (defun org-xor (a b)
  9036. "Exclusive or."
  9037. (if a (not b) b))
  9038. (defun org-fixup-message-id-for-http (s)
  9039. "Replace special characters in a message id, so it can be used in an http query."
  9040. (when (string-match "%" s)
  9041. (setq s (mapconcat (lambda (c)
  9042. (if (eq c ?%)
  9043. "%25"
  9044. (char-to-string c)))
  9045. s "")))
  9046. (while (string-match "<" s)
  9047. (setq s (replace-match "%3C" t t s)))
  9048. (while (string-match ">" s)
  9049. (setq s (replace-match "%3E" t t s)))
  9050. (while (string-match "@" s)
  9051. (setq s (replace-match "%40" t t s)))
  9052. s)
  9053. (defun org-link-prettify (link)
  9054. "Return a human-readable representation of LINK.
  9055. The car of LINK must be a raw link.
  9056. The cdr of LINK must be either a link description or nil."
  9057. (let ((desc (or (cadr link) "<no description>")))
  9058. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9059. "<" (car link) ">")))
  9060. ;;;###autoload
  9061. (defun org-insert-link-global ()
  9062. "Insert a link like Org-mode does.
  9063. This command can be called in any mode to insert a link in Org-mode syntax."
  9064. (interactive)
  9065. (org-load-modules-maybe)
  9066. (org-run-like-in-org-mode 'org-insert-link))
  9067. (defun org-insert-all-links (arg &optional pre post)
  9068. "Insert all links in `org-stored-links'.
  9069. When a universal prefix, do not delete the links from `org-stored-links'.
  9070. When `ARG' is a number, insert the last N link(s).
  9071. `PRE' and `POST' are optional arguments to define a string to
  9072. prepend or to append."
  9073. (interactive "P")
  9074. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9075. (links (copy-seq org-stored-links))
  9076. (pr (or pre "- "))
  9077. (po (or post "\n"))
  9078. (cnt 1) l)
  9079. (if (null org-stored-links)
  9080. (message "No link to insert")
  9081. (while (and (or (listp arg) (>= arg cnt))
  9082. (setq l (if (listp arg)
  9083. (pop links)
  9084. (pop org-stored-links))))
  9085. (setq cnt (1+ cnt))
  9086. (insert pr)
  9087. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9088. (insert po)))))
  9089. (defun org-insert-last-stored-link (arg)
  9090. "Insert the last link stored in `org-stored-links'."
  9091. (interactive "p")
  9092. (org-insert-all-links arg "" "\n"))
  9093. (defun org-link-fontify-links-to-this-file ()
  9094. "Fontify links to the current file in `org-stored-links'."
  9095. (let ((f (buffer-file-name)) a b)
  9096. (setq a (mapcar (lambda(l)
  9097. (let ((ll (car l)))
  9098. (when (and (string-match "^file:\\(.+\\)::" ll)
  9099. (equal f (expand-file-name (match-string 1 ll))))
  9100. ll)))
  9101. org-stored-links))
  9102. (when (featurep 'org-id)
  9103. (setq b (mapcar (lambda(l)
  9104. (let ((ll (car l)))
  9105. (when (and (string-match "^id:\\(.+\\)$" ll)
  9106. (equal f (expand-file-name
  9107. (or (org-id-find-id-file
  9108. (match-string 1 ll)) ""))))
  9109. ll)))
  9110. org-stored-links)))
  9111. (mapcar (lambda(l)
  9112. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9113. (delq nil (append a b)))))
  9114. (defvar org-link-links-in-this-file nil)
  9115. (defun org-insert-link (&optional complete-file link-location default-description)
  9116. "Insert a link. At the prompt, enter the link.
  9117. Completion can be used to insert any of the link protocol prefixes like
  9118. http or ftp in use.
  9119. The history can be used to select a link previously stored with
  9120. `org-store-link'. When the empty string is entered (i.e. if you just
  9121. press RET at the prompt), the link defaults to the most recently
  9122. stored link. As SPC triggers completion in the minibuffer, you need to
  9123. use M-SPC or C-q SPC to force the insertion of a space character.
  9124. You will also be prompted for a description, and if one is given, it will
  9125. be displayed in the buffer instead of the link.
  9126. If there is already a link at point, this command will allow you to edit link
  9127. and description parts.
  9128. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9129. be selected using completion. The path to the file will be relative to the
  9130. current directory if the file is in the current directory or a subdirectory.
  9131. Otherwise, the link will be the absolute path as completed in the minibuffer
  9132. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9133. option `org-link-file-path-type'.
  9134. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9135. the current directory or below.
  9136. With three \\[universal-argument] prefixes, negate the meaning of
  9137. `org-keep-stored-link-after-insertion'.
  9138. If `org-make-link-description-function' is non-nil, this function will be
  9139. called with the link target, and the result will be the default
  9140. link description.
  9141. If the LINK-LOCATION parameter is non-nil, this value will be
  9142. used as the link location instead of reading one interactively.
  9143. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9144. be used as the default description."
  9145. (interactive "P")
  9146. (let* ((wcf (current-window-configuration))
  9147. (origbuf (current-buffer))
  9148. (region (if (org-region-active-p)
  9149. (buffer-substring (region-beginning) (region-end))))
  9150. (remove (and region (list (region-beginning) (region-end))))
  9151. (desc region)
  9152. tmphist ; byte-compile incorrectly complains about this
  9153. (link link-location)
  9154. (abbrevs org-link-abbrev-alist-local)
  9155. entry file all-prefixes auto-desc)
  9156. (cond
  9157. (link-location) ; specified by arg, just use it.
  9158. ((org-in-regexp org-bracket-link-regexp 1)
  9159. ;; We do have a link at point, and we are going to edit it.
  9160. (setq remove (list (match-beginning 0) (match-end 0)))
  9161. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9162. (setq link (read-string "Link: "
  9163. (org-link-unescape
  9164. (org-match-string-no-properties 1)))))
  9165. ((or (org-in-regexp org-angle-link-re)
  9166. (org-in-regexp org-plain-link-re))
  9167. ;; Convert to bracket link
  9168. (setq remove (list (match-beginning 0) (match-end 0))
  9169. link (read-string "Link: "
  9170. (org-remove-angle-brackets (match-string 0)))))
  9171. ((member complete-file '((4) (16)))
  9172. ;; Completing read for file names.
  9173. (setq link (org-file-complete-link complete-file)))
  9174. (t
  9175. ;; Read link, with completion for stored links.
  9176. (org-link-fontify-links-to-this-file)
  9177. (org-switch-to-buffer-other-window "*Org Links*")
  9178. (with-current-buffer "*Org Links*"
  9179. (erase-buffer)
  9180. (insert "Insert a link.
  9181. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9182. (when org-stored-links
  9183. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9184. (insert (mapconcat 'org-link-prettify
  9185. (reverse org-stored-links) "\n")))
  9186. (goto-char (point-min)))
  9187. (let ((cw (selected-window)))
  9188. (select-window (get-buffer-window "*Org Links*" 'visible))
  9189. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9190. (unless (pos-visible-in-window-p (point-max))
  9191. (org-fit-window-to-buffer))
  9192. (and (window-live-p cw) (select-window cw)))
  9193. ;; Fake a link history, containing the stored links.
  9194. (setq tmphist (append (mapcar 'car org-stored-links)
  9195. org-insert-link-history))
  9196. (setq all-prefixes (append (mapcar 'car abbrevs)
  9197. (mapcar 'car org-link-abbrev-alist)
  9198. org-link-types))
  9199. (unwind-protect
  9200. (progn
  9201. (setq link
  9202. (org-completing-read
  9203. "Link: "
  9204. (append
  9205. (mapcar (lambda (x) (concat x ":"))
  9206. all-prefixes)
  9207. (mapcar 'car org-stored-links))
  9208. nil nil nil
  9209. 'tmphist
  9210. (caar org-stored-links)))
  9211. (if (not (string-match "\\S-" link))
  9212. (user-error "No link selected"))
  9213. (mapc (lambda(l)
  9214. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9215. org-stored-links)
  9216. (if (or (member link all-prefixes)
  9217. (and (equal ":" (substring link -1))
  9218. (member (substring link 0 -1) all-prefixes)
  9219. (setq link (substring link 0 -1))))
  9220. (setq link (with-current-buffer origbuf
  9221. (org-link-try-special-completion link)))))
  9222. (set-window-configuration wcf)
  9223. (kill-buffer "*Org Links*"))
  9224. (setq entry (assoc link org-stored-links))
  9225. (or entry (push link org-insert-link-history))
  9226. (setq desc (or desc (nth 1 entry)))))
  9227. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9228. (not org-keep-stored-link-after-insertion))
  9229. (setq org-stored-links (delq (assoc link org-stored-links)
  9230. org-stored-links)))
  9231. (if (and (string-match org-plain-link-re link)
  9232. (not (string-match org-ts-regexp link)))
  9233. ;; URL-like link, normalize the use of angular brackets.
  9234. (setq link (org-remove-angle-brackets link)))
  9235. ;; Check if we are linking to the current file with a search
  9236. ;; option If yes, simplify the link by using only the search
  9237. ;; option.
  9238. (when (and buffer-file-name
  9239. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9240. (let* ((path (match-string 1 link))
  9241. (case-fold-search nil)
  9242. (search (match-string 2 link)))
  9243. (save-match-data
  9244. (if (equal (file-truename buffer-file-name) (file-truename path))
  9245. ;; We are linking to this same file, with a search option
  9246. (setq link search)))))
  9247. ;; Check if we can/should use a relative path. If yes, simplify the link
  9248. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9249. (let* ((type (match-string 1 link))
  9250. (path (match-string 2 link))
  9251. (origpath path)
  9252. (case-fold-search nil))
  9253. (cond
  9254. ((or (eq org-link-file-path-type 'absolute)
  9255. (equal complete-file '(16)))
  9256. (setq path (abbreviate-file-name (expand-file-name path))))
  9257. ((eq org-link-file-path-type 'noabbrev)
  9258. (setq path (expand-file-name path)))
  9259. ((eq org-link-file-path-type 'relative)
  9260. (setq path (file-relative-name path)))
  9261. (t
  9262. (save-match-data
  9263. (if (string-match (concat "^" (regexp-quote
  9264. (expand-file-name
  9265. (file-name-as-directory
  9266. default-directory))))
  9267. (expand-file-name path))
  9268. ;; We are linking a file with relative path name.
  9269. (setq path (substring (expand-file-name path)
  9270. (match-end 0)))
  9271. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9272. (setq link (concat type path))
  9273. (if (equal desc origpath)
  9274. (setq desc path))))
  9275. (if org-make-link-description-function
  9276. (setq desc
  9277. (or (condition-case nil
  9278. (funcall org-make-link-description-function link desc)
  9279. (error (progn (message "Can't get link description from `%s'"
  9280. (symbol-name org-make-link-description-function))
  9281. (sit-for 2) nil)))
  9282. (read-string "Description: " default-description)))
  9283. (if default-description (setq desc default-description)
  9284. (setq desc (or (and auto-desc desc)
  9285. (read-string "Description: " desc)))))
  9286. (unless (string-match "\\S-" desc) (setq desc nil))
  9287. (if remove (apply 'delete-region remove))
  9288. (insert (org-make-link-string link desc))))
  9289. (defun org-link-try-special-completion (type)
  9290. "If there is completion support for link type TYPE, offer it."
  9291. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9292. (if (functionp fun)
  9293. (funcall fun)
  9294. (read-string "Link (no completion support): " (concat type ":")))))
  9295. (defun org-file-complete-link (&optional arg)
  9296. "Create a file link using completion."
  9297. (let (file link)
  9298. (setq file (org-iread-file-name "File: "))
  9299. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9300. (pwd1 (file-name-as-directory (abbreviate-file-name
  9301. (expand-file-name ".")))))
  9302. (cond
  9303. ((equal arg '(16))
  9304. (setq link (concat
  9305. "file:"
  9306. (abbreviate-file-name (expand-file-name file)))))
  9307. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9308. (setq link (concat "file:" (match-string 1 file))))
  9309. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9310. (expand-file-name file))
  9311. (setq link (concat
  9312. "file:" (match-string 1 (expand-file-name file)))))
  9313. (t (setq link (concat "file:" file)))))
  9314. link))
  9315. (defun org-iread-file-name (&rest args)
  9316. "Read-file-name using `ido-mode' speedup if available.
  9317. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9318. See `read-file-name' for a description of parameters."
  9319. (org-without-partial-completion
  9320. (if (and org-completion-use-ido
  9321. (fboundp 'ido-read-file-name)
  9322. (boundp 'ido-mode) ido-mode
  9323. (listp (second args)))
  9324. (let ((ido-enter-matching-directory nil))
  9325. (apply 'ido-read-file-name args))
  9326. (apply 'read-file-name args))))
  9327. (defun org-completing-read (&rest args)
  9328. "Completing-read with SPACE being a normal character."
  9329. (let ((enable-recursive-minibuffers t)
  9330. (minibuffer-local-completion-map
  9331. (copy-keymap minibuffer-local-completion-map)))
  9332. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9333. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9334. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9335. (apply 'org-icompleting-read args)))
  9336. (defun org-completing-read-no-i (&rest args)
  9337. (let (org-completion-use-ido org-completion-use-iswitchb)
  9338. (apply 'org-completing-read args)))
  9339. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9340. "Use iswitch as a completing-read replacement to choose from choices.
  9341. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9342. from."
  9343. (let* ((iswitchb-use-virtual-buffers nil)
  9344. (iswitchb-make-buflist-hook
  9345. (lambda ()
  9346. (setq iswitchb-temp-buflist choices))))
  9347. (iswitchb-read-buffer prompt)))
  9348. (defun org-icompleting-read (&rest args)
  9349. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9350. (org-without-partial-completion
  9351. (if (and org-completion-use-ido
  9352. (fboundp 'ido-completing-read)
  9353. (boundp 'ido-mode) ido-mode
  9354. (listp (second args)))
  9355. (let ((ido-enter-matching-directory nil))
  9356. (apply 'ido-completing-read (concat (car args))
  9357. (if (consp (car (nth 1 args)))
  9358. (mapcar 'car (nth 1 args))
  9359. (nth 1 args))
  9360. (cddr args)))
  9361. (if (and org-completion-use-iswitchb
  9362. (boundp 'iswitchb-mode) iswitchb-mode
  9363. (listp (second args)))
  9364. (apply 'org-iswitchb-completing-read (concat (car args))
  9365. (if (consp (car (nth 1 args)))
  9366. (mapcar 'car (nth 1 args))
  9367. (nth 1 args))
  9368. (cddr args))
  9369. (apply 'completing-read args)))))
  9370. (defun org-extract-attributes (s)
  9371. "Extract the attributes cookie from a string and set as text property."
  9372. (let (a attr (start 0) key value)
  9373. (save-match-data
  9374. (when (string-match "{{\\([^}]+\\)}}$" s)
  9375. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9376. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9377. (setq key (match-string 1 a) value (match-string 2 a)
  9378. start (match-end 0)
  9379. attr (plist-put attr (intern key) value))))
  9380. (org-add-props s nil 'org-attr attr))
  9381. s))
  9382. ;;; Opening/following a link
  9383. (defvar org-link-search-failed nil)
  9384. (defvar org-open-link-functions nil
  9385. "Hook for functions finding a plain text link.
  9386. These functions must take a single argument, the link content.
  9387. They will be called for links that look like [[link text][description]]
  9388. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9389. like a filename (e.g. \"./blue.png\").
  9390. These functions will be called *before* Org attempts to resolve the
  9391. link by doing text searches in the current buffer - so if you want a
  9392. link \"[[target]]\" to still find \"<<target>>\", your function should
  9393. handle this as a special case.
  9394. When the function does handle the link, it must return a non-nil value.
  9395. If it decides that it is not responsible for this link, it must return
  9396. nil to indicate that that Org-mode can continue with other options
  9397. like exact and fuzzy text search.")
  9398. (defun org-next-link (&optional search-backward)
  9399. "Move forward to the next link.
  9400. If the link is in hidden text, expose it."
  9401. (interactive "P")
  9402. (when (and org-link-search-failed (eq this-command last-command))
  9403. (goto-char (point-min))
  9404. (message "Link search wrapped back to beginning of buffer"))
  9405. (setq org-link-search-failed nil)
  9406. (let* ((pos (point))
  9407. (ct (org-context))
  9408. (a (assoc :link ct))
  9409. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9410. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9411. ((looking-at org-any-link-re)
  9412. ;; Don't stay stuck at link without an org-link face
  9413. (forward-char (if search-backward -1 1))))
  9414. (if (funcall srch-fun org-any-link-re nil t)
  9415. (progn
  9416. (goto-char (match-beginning 0))
  9417. (if (outline-invisible-p) (org-show-context)))
  9418. (goto-char pos)
  9419. (setq org-link-search-failed t)
  9420. (message "No further link found"))))
  9421. (defun org-previous-link ()
  9422. "Move backward to the previous link.
  9423. If the link is in hidden text, expose it."
  9424. (interactive)
  9425. (funcall 'org-next-link t))
  9426. (defun org-translate-link (s)
  9427. "Translate a link string if a translation function has been defined."
  9428. (if (and org-link-translation-function
  9429. (fboundp org-link-translation-function)
  9430. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9431. (progn
  9432. (setq s (funcall org-link-translation-function
  9433. (match-string 1 s) (match-string 2 s)))
  9434. (concat (car s) ":" (cdr s)))
  9435. s))
  9436. (defun org-translate-link-from-planner (type path)
  9437. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9438. This is still an experimental function, your mileage may vary."
  9439. (cond
  9440. ((member type '("http" "https" "news" "ftp"))
  9441. ;; standard Internet links are the same.
  9442. nil)
  9443. ((and (equal type "irc") (string-match "^//" path))
  9444. ;; Planner has two / at the beginning of an irc link, we have 1.
  9445. ;; We should have zero, actually....
  9446. (setq path (substring path 1)))
  9447. ((and (equal type "lisp") (string-match "^/" path))
  9448. ;; Planner has a slash, we do not.
  9449. (setq type "elisp" path (substring path 1)))
  9450. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9451. ;; A typical message link. Planner has the id after the final slash,
  9452. ;; we separate it with a hash mark
  9453. (setq path (concat (match-string 1 path) "#"
  9454. (org-remove-angle-brackets (match-string 2 path))))))
  9455. (cons type path))
  9456. (defun org-find-file-at-mouse (ev)
  9457. "Open file link or URL at mouse."
  9458. (interactive "e")
  9459. (mouse-set-point ev)
  9460. (org-open-at-point 'in-emacs))
  9461. (defun org-open-at-mouse (ev)
  9462. "Open file link or URL at mouse.
  9463. See the docstring of `org-open-file' for details."
  9464. (interactive "e")
  9465. (mouse-set-point ev)
  9466. (if (eq major-mode 'org-agenda-mode)
  9467. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9468. (org-open-at-point))
  9469. (defvar org-window-config-before-follow-link nil
  9470. "The window configuration before following a link.
  9471. This is saved in case the need arises to restore it.")
  9472. (defvar org-open-link-marker (make-marker)
  9473. "Marker pointing to the location where `org-open-at-point' was called.")
  9474. ;;;###autoload
  9475. (defun org-open-at-point-global ()
  9476. "Follow a link like Org-mode does.
  9477. This command can be called in any mode to follow a link that has
  9478. Org-mode syntax."
  9479. (interactive)
  9480. (org-run-like-in-org-mode 'org-open-at-point))
  9481. ;;;###autoload
  9482. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9483. "Open a link in the string S, as if it was in Org-mode."
  9484. (interactive "sLink: \nP")
  9485. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9486. (with-temp-buffer
  9487. (let ((org-inhibit-startup (not reference-buffer)))
  9488. (org-mode)
  9489. (insert s)
  9490. (goto-char (point-min))
  9491. (when reference-buffer
  9492. (setq org-link-abbrev-alist-local
  9493. (with-current-buffer reference-buffer
  9494. org-link-abbrev-alist-local)))
  9495. (org-open-at-point arg reference-buffer)))))
  9496. (defvar org-open-at-point-functions nil
  9497. "Hook that is run when following a link at point.
  9498. Functions in this hook must return t if they identify and follow
  9499. a link at point. If they don't find anything interesting at point,
  9500. they must return nil.")
  9501. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9502. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9503. (defun org-open-at-point (&optional arg reference-buffer)
  9504. "Open link, timestamp, footnote or tags at point.
  9505. When point is on a link, follow it. Normally, files will be
  9506. opened by an appropriate application. If the optional prefix
  9507. argument ARG is non-nil, Emacs will visit the file. With
  9508. a double prefix argument, try to open outside of Emacs, in the
  9509. application the system uses for this file type.
  9510. When point is on a timestamp, open the agenda at the day
  9511. specified.
  9512. When point is a footnote definition, move to the first reference
  9513. found. If it is on a reference, move to the associated
  9514. definition.
  9515. When point is on a headline, display a list of every link in the
  9516. entry, so it is possible to pick one, or all, of them. If point
  9517. is on a tag, call `org-tags-view' instead.
  9518. When optional argument REFERENCE-BUFFER is non-nil, it should
  9519. specify a buffer from where the link search should happen. This
  9520. is used internally by `org-open-link-from-string'.
  9521. On top of syntactically correct links, this function will open
  9522. the link at point in comments and the first link in a property
  9523. drawer line."
  9524. (interactive "P")
  9525. ;; On a code block, open block's results.
  9526. (unless (call-interactively #'org-babel-open-src-block-result)
  9527. (org-load-modules-maybe)
  9528. (move-marker org-open-link-marker (point))
  9529. (setq org-window-config-before-follow-link (current-window-configuration))
  9530. (org-remove-occur-highlights nil nil t)
  9531. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9532. (let* ((context (org-element-context)) type value)
  9533. ;; On an unsupported type, check if point is contained within
  9534. ;; a support one.
  9535. (while (and (not (memq (setq type (org-element-type context))
  9536. '(comment headline inlinetask link
  9537. footnote-definition footnote-reference
  9538. node-property timestamp)))
  9539. (setq context (org-element-property :parent context))))
  9540. (setq value (org-element-property :value context))
  9541. (cond
  9542. ;; Blank lines at the beginning of buffer: bail out.
  9543. ((not context) (user-error "No link found"))
  9544. ;; Exception n°1: links in property drawers
  9545. ((eq type 'node-property)
  9546. (org-open-link-from-string
  9547. (and (string-match org-any-link-re value)
  9548. (match-string-no-properties 0 value))))
  9549. ;; Exception n°2: links in comments.
  9550. ((eq type 'comment)
  9551. (let ((string-rear (replace-regexp-in-string
  9552. "^[ \t]*# [ \t]*" ""
  9553. (buffer-substring (point) (line-beginning-position))))
  9554. (string-front (buffer-substring (point) (line-end-position))))
  9555. (with-temp-buffer
  9556. (let ((org-inhibit-startup t)) (org-mode))
  9557. (insert value)
  9558. (goto-char (point-min))
  9559. (when (and (search-forward string-rear nil t)
  9560. (search-forward string-front (line-end-position) t))
  9561. (goto-char (match-beginning 0))
  9562. (org-open-at-point)
  9563. (when (string= string-rear "") (forward-char))))))
  9564. ;; On a headline or an inlinetask, but not on a timestamp,
  9565. ;; a link, a footnote reference or on tags.
  9566. ((and (memq type '(headline inlinetask))
  9567. ;; Not on tags.
  9568. (progn (save-excursion (beginning-of-line)
  9569. (looking-at org-complex-heading-regexp))
  9570. (or (not (match-beginning 5))
  9571. (< (point) (match-beginning 5)))))
  9572. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9573. (links (car data))
  9574. (links-end (cdr data)))
  9575. (if links
  9576. (dolist (link (if (stringp links) (list links) links))
  9577. (search-forward link nil links-end)
  9578. (goto-char (match-beginning 0))
  9579. (org-open-at-point))
  9580. (require 'org-attach)
  9581. (org-attach-reveal 'if-exists))))
  9582. ;; Do nothing on white spaces after an object, unless point
  9583. ;; is right after it.
  9584. ((> (point)
  9585. (save-excursion
  9586. (goto-char (org-element-property :end context))
  9587. (skip-chars-backward " \t")
  9588. (point)))
  9589. (user-error "No link found"))
  9590. ((eq type 'timestamp) (org-follow-timestamp-link))
  9591. ;; On tags within a headline or an inlinetask.
  9592. ((and (memq type '(headline inlinetask))
  9593. (progn (save-excursion (beginning-of-line)
  9594. (looking-at org-complex-heading-regexp))
  9595. (and (match-beginning 5)
  9596. (>= (point) (match-beginning 5)))))
  9597. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9598. ((eq type 'link)
  9599. (let ((type (org-element-property :type context))
  9600. (path (org-link-unescape (org-element-property :path context))))
  9601. ;; Switch back to REFERENCE-BUFFER needed when called in
  9602. ;; a temporary buffer through `org-open-link-from-string'.
  9603. (with-current-buffer (or reference-buffer (current-buffer))
  9604. (cond
  9605. ((equal type "file")
  9606. (if (string-match "[*?{]" (file-name-nondirectory path))
  9607. (dired path)
  9608. ;; Look into `org-link-protocols' in order to find
  9609. ;; a DEDICATED-FUNCTION to open file. The function
  9610. ;; will be applied on raw link instead of parsed
  9611. ;; link due to the limitation in `org-add-link-type'
  9612. ;; ("open" function called with a single argument).
  9613. ;; If no such function is found, fallback to
  9614. ;; `org-open-file'.
  9615. ;;
  9616. ;; Note : "file+emacs" and "file+sys" types are
  9617. ;; hard-coded in order to escape the previous
  9618. ;; limitation.
  9619. (let* ((option (org-element-property :search-option context))
  9620. (app (org-element-property :application context))
  9621. (dedicated-function
  9622. (nth 1 (assoc app org-link-protocols))))
  9623. (if dedicated-function
  9624. (funcall dedicated-function
  9625. (concat path
  9626. (and option (concat "::" option))))
  9627. (apply #'org-open-file
  9628. path
  9629. (cond (arg)
  9630. ((equal app "emacs") 'emacs)
  9631. ((equal app "sys") 'system))
  9632. (cond ((not option) nil)
  9633. ((org-string-match-p "\\`[0-9]+\\'" option)
  9634. (list (string-to-number option)))
  9635. (t (list nil
  9636. (org-link-unescape option)))))))))
  9637. ((assoc type org-link-protocols)
  9638. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9639. ((equal type "help")
  9640. (let ((f-or-v (intern path)))
  9641. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9642. ((boundp f-or-v) (describe-variable f-or-v))
  9643. (t (error "Not a known function or variable")))))
  9644. ((member type '("http" "https" "ftp" "mailto" "news"))
  9645. (browse-url (org-link-escape-browser (concat type ":" path))))
  9646. ((equal type "doi")
  9647. (browse-url
  9648. (org-link-escape-browser (concat org-doi-server-url path))))
  9649. ((equal type "message") (browse-url (concat type ":" path)))
  9650. ((equal type "shell")
  9651. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9652. (cmd path))
  9653. (if (or (and (org-string-nw-p
  9654. org-confirm-shell-link-not-regexp)
  9655. (string-match
  9656. org-confirm-shell-link-not-regexp cmd))
  9657. (not org-confirm-shell-link-function)
  9658. (funcall org-confirm-shell-link-function
  9659. (format "Execute \"%s\" in shell? "
  9660. (org-add-props cmd nil
  9661. 'face 'org-warning))))
  9662. (progn
  9663. (message "Executing %s" cmd)
  9664. (shell-command cmd buf)
  9665. (when (featurep 'midnight)
  9666. (setq clean-buffer-list-kill-buffer-names
  9667. (cons buf
  9668. clean-buffer-list-kill-buffer-names))))
  9669. (user-error "Abort"))))
  9670. ((equal type "elisp")
  9671. (let ((cmd path))
  9672. (if (or (and (org-string-nw-p
  9673. org-confirm-elisp-link-not-regexp)
  9674. (org-string-match-p
  9675. org-confirm-elisp-link-not-regexp cmd))
  9676. (not org-confirm-elisp-link-function)
  9677. (funcall org-confirm-elisp-link-function
  9678. (format "Execute \"%s\" as elisp? "
  9679. (org-add-props cmd nil
  9680. 'face 'org-warning))))
  9681. (message "%s => %s" cmd
  9682. (if (eq (string-to-char cmd) ?\()
  9683. (eval (read cmd))
  9684. (call-interactively (read cmd))))
  9685. (user-error "Abort"))))
  9686. ((equal type "id")
  9687. (require 'ord-id)
  9688. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9689. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9690. (unless (run-hook-with-args-until-success
  9691. 'org-open-link-functions path)
  9692. (if (not arg) (org-mark-ring-push)
  9693. (switch-to-buffer-other-window
  9694. (org-get-buffer-for-internal-link (current-buffer))))
  9695. (let ((cmd `(org-link-search
  9696. ,(if (member type '("custom-id" "coderef"))
  9697. (org-element-property :raw-link context)
  9698. path)
  9699. ,(cond ((equal arg '(4)) 'occur)
  9700. ((equal arg '(16)) 'org-occur))
  9701. ,(org-element-property :begin context))))
  9702. (condition-case nil
  9703. (let ((org-link-search-inhibit-query t))
  9704. (eval cmd))
  9705. (error (progn (widen) (eval cmd)))))))
  9706. (t (browse-url-at-point))))))
  9707. ;; On a footnote reference or at a footnote definition's label.
  9708. ((or (eq type 'footnote-reference)
  9709. (and (eq type 'footnote-definition)
  9710. (save-excursion
  9711. ;; Do not validate action when point is on the
  9712. ;; spaces right after the footnote label, in
  9713. ;; order to be on par with behaviour on links.
  9714. (skip-chars-forward " \t")
  9715. (let ((begin
  9716. (org-element-property :contents-begin context)))
  9717. (if begin (< (point) begin)
  9718. (= (org-element-property :post-affiliated context)
  9719. (line-beginning-position)))))))
  9720. (org-footnote-action))
  9721. (t (user-error "No link found")))))
  9722. (move-marker org-open-link-marker nil)
  9723. (run-hook-with-args 'org-follow-link-hook)))
  9724. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9725. "Offer links in the current entry and return the selected link.
  9726. If there is only one link, return it.
  9727. If NTH is an integer, return the NTH link found.
  9728. If ZERO is a string, check also this string for a link, and if
  9729. there is one, return it."
  9730. (with-current-buffer buffer
  9731. (save-excursion
  9732. (save-restriction
  9733. (widen)
  9734. (goto-char marker)
  9735. (let ((cnt ?0)
  9736. (in-emacs (if (integerp nth) nil nth))
  9737. have-zero end links link c)
  9738. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9739. (push (match-string 0 zero) links)
  9740. (setq cnt (1- cnt) have-zero t))
  9741. (save-excursion
  9742. (org-back-to-heading t)
  9743. (setq end (save-excursion (outline-next-heading) (point)))
  9744. (while (re-search-forward org-any-link-re end t)
  9745. (push (match-string 0) links))
  9746. (setq links (org-uniquify (reverse links))))
  9747. (cond
  9748. ((null links)
  9749. (message "No links"))
  9750. ((equal (length links) 1)
  9751. (setq link (car links)))
  9752. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9753. (setq link (nth (if have-zero nth (1- nth)) links)))
  9754. (t ; we have to select a link
  9755. (save-excursion
  9756. (save-window-excursion
  9757. (delete-other-windows)
  9758. (with-output-to-temp-buffer "*Select Link*"
  9759. (mapc (lambda (l)
  9760. (if (not (string-match org-bracket-link-regexp l))
  9761. (princ (format "[%c] %s\n" (incf cnt)
  9762. (org-remove-angle-brackets l)))
  9763. (if (match-end 3)
  9764. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9765. (match-string 3 l) (match-string 1 l)))
  9766. (princ (format "[%c] %s\n" (incf cnt)
  9767. (match-string 1 l))))))
  9768. links))
  9769. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9770. (message "Select link to open, RET to open all:")
  9771. (setq c (read-char-exclusive))
  9772. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9773. (when (equal c ?q) (user-error "Abort"))
  9774. (if (equal c ?\C-m)
  9775. (setq link links)
  9776. (setq nth (- c ?0))
  9777. (if have-zero (setq nth (1+ nth)))
  9778. (unless (and (integerp nth) (>= (length links) nth))
  9779. (user-error "Invalid link selection"))
  9780. (setq link (nth (1- nth) links)))))
  9781. (cons link end))))))
  9782. ;; TODO: These functions are deprecated since `org-open-at-point'
  9783. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9784. (defun org-open-file-with-system (path)
  9785. "Open file at PATH using the system way of opening it."
  9786. (org-open-file path 'system))
  9787. (defun org-open-file-with-emacs (path)
  9788. "Open file at PATH in Emacs."
  9789. (org-open-file path 'emacs))
  9790. ;;; File search
  9791. (defvar org-create-file-search-functions nil
  9792. "List of functions to construct the right search string for a file link.
  9793. These functions are called in turn with point at the location to
  9794. which the link should point.
  9795. A function in the hook should first test if it would like to
  9796. handle this file type, for example by checking the `major-mode'
  9797. or the file extension. If it decides not to handle this file, it
  9798. should just return nil to give other functions a chance. If it
  9799. does handle the file, it must return the search string to be used
  9800. when following the link. The search string will be part of the
  9801. file link, given after a double colon, and `org-open-at-point'
  9802. will automatically search for it. If special measures must be
  9803. taken to make the search successful, another function should be
  9804. added to the companion hook `org-execute-file-search-functions',
  9805. which see.
  9806. A function in this hook may also use `setq' to set the variable
  9807. `description' to provide a suggestion for the descriptive text to
  9808. be used for this link when it gets inserted into an Org-mode
  9809. buffer with \\[org-insert-link].")
  9810. (defvar org-execute-file-search-functions nil
  9811. "List of functions to execute a file search triggered by a link.
  9812. Functions added to this hook must accept a single argument, the
  9813. search string that was part of the file link, the part after the
  9814. double colon. The function must first check if it would like to
  9815. handle this search, for example by checking the `major-mode' or
  9816. the file extension. If it decides not to handle this search, it
  9817. should just return nil to give other functions a chance. If it
  9818. does handle the search, it must return a non-nil value to keep
  9819. other functions from trying.
  9820. Each function can access the current prefix argument through the
  9821. variable `current-prefix-arg'. Note that a single prefix is used
  9822. to force opening a link in Emacs, so it may be good to only use a
  9823. numeric or double prefix to guide the search function.
  9824. In case this is needed, a function in this hook can also restore
  9825. the window configuration before `org-open-at-point' was called using:
  9826. (set-window-configuration org-window-config-before-follow-link)")
  9827. (defun org-link-search (s &optional type avoid-pos stealth)
  9828. "Search for a link search option.
  9829. If S is surrounded by forward slashes, it is interpreted as a
  9830. regular expression. In org-mode files, this will create an `org-occur'
  9831. sparse tree. In ordinary files, `occur' will be used to list matches.
  9832. If the current buffer is in `dired-mode', grep will be used to search
  9833. in all files. If AVOID-POS is given, ignore matches near that position.
  9834. When optional argument STEALTH is non-nil, do not modify
  9835. visibility around point, thus ignoring
  9836. `org-show-hierarchy-above', `org-show-following-heading' and
  9837. `org-show-siblings' variables."
  9838. (let ((case-fold-search t)
  9839. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9840. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9841. (append '(("") (" ") ("\t") ("\n"))
  9842. org-emphasis-alist)
  9843. "\\|") "\\)"))
  9844. (pos (point))
  9845. (pre nil) (post nil)
  9846. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9847. (cond
  9848. ;; First check if there are any special search functions
  9849. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9850. ;; Now try the builtin stuff
  9851. ((and (equal (string-to-char s0) ?#)
  9852. (> (length s0) 1)
  9853. (save-excursion
  9854. (goto-char (point-min))
  9855. (and
  9856. (re-search-forward
  9857. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9858. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9859. (setq type 'dedicated
  9860. pos (match-beginning 0))))
  9861. ;; There is an exact target for this
  9862. (goto-char pos)
  9863. (org-back-to-heading t)))
  9864. ((save-excursion
  9865. (goto-char (point-min))
  9866. (and
  9867. (re-search-forward
  9868. (concat "<<" (regexp-quote s0) ">>") nil t)
  9869. (setq type 'dedicated
  9870. pos (match-beginning 0))))
  9871. ;; There is an exact target for this
  9872. (goto-char pos))
  9873. ((save-excursion
  9874. (goto-char (point-min))
  9875. (and
  9876. (re-search-forward
  9877. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9878. (setq type 'dedicated pos (match-beginning 0))))
  9879. ;; Found an element with a matching #+name affiliated keyword.
  9880. (goto-char pos))
  9881. ((and (string-match "^(\\(.*\\))$" s0)
  9882. (save-excursion
  9883. (goto-char (point-min))
  9884. (and
  9885. (re-search-forward
  9886. (concat "[^[]" (regexp-quote
  9887. (format org-coderef-label-format
  9888. (match-string 1 s0))))
  9889. nil t)
  9890. (setq type 'dedicated
  9891. pos (1+ (match-beginning 0))))))
  9892. ;; There is a coderef target for this
  9893. (goto-char pos))
  9894. ((string-match "^/\\(.*\\)/$" s)
  9895. ;; A regular expression
  9896. (cond
  9897. ((derived-mode-p 'org-mode)
  9898. (org-occur (match-string 1 s)))
  9899. (t (org-do-occur (match-string 1 s)))))
  9900. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9901. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9902. (goto-char (point-min))
  9903. (cond
  9904. ((let (case-fold-search)
  9905. (re-search-forward (format org-complex-heading-regexp-format
  9906. (regexp-quote s))
  9907. nil t))
  9908. ;; OK, found a match
  9909. (setq type 'dedicated)
  9910. (goto-char (match-beginning 0)))
  9911. ((and (not org-link-search-inhibit-query)
  9912. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9913. (y-or-n-p "No match - create this as a new heading? "))
  9914. (goto-char (point-max))
  9915. (or (bolp) (newline))
  9916. (insert "* " s "\n")
  9917. (beginning-of-line 0))
  9918. (t
  9919. (goto-char pos)
  9920. (error "No match"))))
  9921. (t
  9922. ;; A normal search string
  9923. (when (equal (string-to-char s) ?*)
  9924. ;; Anchor on headlines, post may include tags.
  9925. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9926. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9927. s (substring s 1)))
  9928. (remove-text-properties
  9929. 0 (length s)
  9930. '(face nil mouse-face nil keymap nil fontified nil) s)
  9931. ;; Make a series of regular expressions to find a match
  9932. (setq words (org-split-string s "[ \n\r\t]+")
  9933. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9934. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9935. "\\)" markers)
  9936. re2a_ (concat "\\(" (mapconcat 'downcase words
  9937. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9938. re2a (concat "[ \t\r\n]" re2a_)
  9939. re4_ (concat "\\(" (mapconcat 'downcase words
  9940. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9941. re4 (concat "[^a-zA-Z_]" re4_)
  9942. re1 (concat pre re2 post)
  9943. re3 (concat pre (if pre re4_ re4) post)
  9944. re5 (concat pre ".*" re4)
  9945. re2 (concat pre re2)
  9946. re2a (concat pre (if pre re2a_ re2a))
  9947. re4 (concat pre (if pre re4_ re4))
  9948. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9949. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9950. re5 "\\)"))
  9951. (cond
  9952. ((eq type 'org-occur) (org-occur reall))
  9953. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9954. (t (goto-char (point-min))
  9955. (setq type 'fuzzy)
  9956. (if (or (and (org-search-not-self 1 re0 nil t)
  9957. (setq type 'dedicated))
  9958. (org-search-not-self 1 re1 nil t)
  9959. (org-search-not-self 1 re2 nil t)
  9960. (org-search-not-self 1 re2a nil t)
  9961. (org-search-not-self 1 re3 nil t)
  9962. (org-search-not-self 1 re4 nil t)
  9963. (org-search-not-self 1 re5 nil t))
  9964. (goto-char (match-beginning 1))
  9965. (goto-char pos)
  9966. (error "No match"))))))
  9967. (and (derived-mode-p 'org-mode)
  9968. (not stealth)
  9969. (org-show-context 'link-search))
  9970. type))
  9971. (defun org-search-not-self (group &rest args)
  9972. "Execute `re-search-forward', but only accept matches that do not
  9973. enclose the position of `org-open-link-marker'."
  9974. (let ((m org-open-link-marker))
  9975. (catch 'exit
  9976. (while (apply 're-search-forward args)
  9977. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9978. (goto-char (match-end group))
  9979. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9980. (> (match-beginning 0) (marker-position m))
  9981. (< (match-end 0) (marker-position m)))
  9982. (save-match-data
  9983. (or (not (org-in-regexp
  9984. org-bracket-link-analytic-regexp 1))
  9985. (not (match-end 4)) ; no description
  9986. (and (<= (match-beginning 4) (point))
  9987. (>= (match-end 4) (point))))))
  9988. (throw 'exit (point))))))))
  9989. (defun org-get-buffer-for-internal-link (buffer)
  9990. "Return a buffer to be used for displaying the link target of internal links."
  9991. (cond
  9992. ((not org-display-internal-link-with-indirect-buffer)
  9993. buffer)
  9994. ((string-match "(Clone)$" (buffer-name buffer))
  9995. (message "Buffer is already a clone, not making another one")
  9996. ;; we also do not modify visibility in this case
  9997. buffer)
  9998. (t ; make a new indirect buffer for displaying the link
  9999. (let* ((bn (buffer-name buffer))
  10000. (ibn (concat bn "(Clone)"))
  10001. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10002. (with-current-buffer ib (org-overview))
  10003. ib))))
  10004. (defun org-do-occur (regexp &optional cleanup)
  10005. "Call the Emacs command `occur'.
  10006. If CLEANUP is non-nil, remove the printout of the regular expression
  10007. in the *Occur* buffer. This is useful if the regex is long and not useful
  10008. to read."
  10009. (occur regexp)
  10010. (when cleanup
  10011. (let ((cwin (selected-window)) win beg end)
  10012. (when (setq win (get-buffer-window "*Occur*"))
  10013. (select-window win))
  10014. (goto-char (point-min))
  10015. (when (re-search-forward "match[a-z]+" nil t)
  10016. (setq beg (match-end 0))
  10017. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10018. (setq end (1- (match-beginning 0)))))
  10019. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10020. (goto-char (point-min))
  10021. (select-window cwin))))
  10022. ;;; The mark ring for links jumps
  10023. (defvar org-mark-ring nil
  10024. "Mark ring for positions before jumps in Org-mode.")
  10025. (defvar org-mark-ring-last-goto nil
  10026. "Last position in the mark ring used to go back.")
  10027. ;; Fill and close the ring
  10028. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10029. (loop for i from 1 to org-mark-ring-length do
  10030. (push (make-marker) org-mark-ring))
  10031. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10032. org-mark-ring)
  10033. (defun org-mark-ring-push (&optional pos buffer)
  10034. "Put the current position or POS into the mark ring and rotate it."
  10035. (interactive)
  10036. (setq pos (or pos (point)))
  10037. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10038. (move-marker (car org-mark-ring)
  10039. (or pos (point))
  10040. (or buffer (current-buffer)))
  10041. (message "%s"
  10042. (substitute-command-keys
  10043. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10044. (defun org-mark-ring-goto (&optional n)
  10045. "Jump to the previous position in the mark ring.
  10046. With prefix arg N, jump back that many stored positions. When
  10047. called several times in succession, walk through the entire ring.
  10048. Org-mode commands jumping to a different position in the current file,
  10049. or to another Org-mode file, automatically push the old position
  10050. onto the ring."
  10051. (interactive "p")
  10052. (let (p m)
  10053. (if (eq last-command this-command)
  10054. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10055. (setq p org-mark-ring))
  10056. (setq org-mark-ring-last-goto p)
  10057. (setq m (car p))
  10058. (org-pop-to-buffer-same-window (marker-buffer m))
  10059. (goto-char m)
  10060. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10061. (defun org-remove-angle-brackets (s)
  10062. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10063. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10064. s)
  10065. (defun org-add-angle-brackets (s)
  10066. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10067. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10068. s)
  10069. (defun org-remove-double-quotes (s)
  10070. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10071. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10072. s)
  10073. ;;; Following specific links
  10074. (defun org-follow-timestamp-link ()
  10075. "Open an agenda view for the time-stamp date/range at point."
  10076. (cond
  10077. ((org-at-date-range-p t)
  10078. (let ((org-agenda-start-on-weekday)
  10079. (t1 (match-string 1))
  10080. (t2 (match-string 2)) tt1 tt2)
  10081. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10082. tt2 (time-to-days (org-time-string-to-time t2)))
  10083. (let ((org-agenda-buffer-tmp-name
  10084. (format "*Org Agenda(a:%s)"
  10085. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10086. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10087. ((org-at-timestamp-p t)
  10088. (let ((org-agenda-buffer-tmp-name
  10089. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10090. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10091. (substring (match-string 1) 0 10)))
  10092. 1)))
  10093. (t (error "This should not happen"))))
  10094. ;;; Following file links
  10095. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10096. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10097. (declare-function mailcap-mime-info
  10098. "mailcap" (string &optional request no-decode))
  10099. (defvar org-wait nil)
  10100. (defun org-open-file (path &optional in-emacs line search)
  10101. "Open the file at PATH.
  10102. First, this expands any special file name abbreviations. Then the
  10103. configuration variable `org-file-apps' is checked if it contains an
  10104. entry for this file type, and if yes, the corresponding command is launched.
  10105. If no application is found, Emacs simply visits the file.
  10106. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10107. With a double \\[universal-argument] \\[universal-argument] \
  10108. prefix arg, Org tries to avoid opening in Emacs
  10109. and to use an external application to visit the file.
  10110. Optional LINE specifies a line to go to, optional SEARCH a string
  10111. to search for. If LINE or SEARCH is given, the file will be
  10112. opened in Emacs, unless an entry from org-file-apps that makes
  10113. use of groups in a regexp matches.
  10114. If you want to change the way frames are used when following a
  10115. link, please customize `org-link-frame-setup'.
  10116. If the file does not exist, an error is thrown."
  10117. (let* ((file (if (equal path "")
  10118. buffer-file-name
  10119. (substitute-in-file-name (expand-file-name path))))
  10120. (file-apps (append org-file-apps (org-default-apps)))
  10121. (apps (org-remove-if
  10122. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10123. (apps-dlink (org-remove-if-not
  10124. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10125. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10126. (dirp (if remp nil (file-directory-p file)))
  10127. (file (if (and dirp org-open-directory-means-index-dot-org)
  10128. (concat (file-name-as-directory file) "index.org")
  10129. file))
  10130. (a-m-a-p (assq 'auto-mode apps))
  10131. (dfile (downcase file))
  10132. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10133. (link (cond ((and (eq line nil)
  10134. (eq search nil))
  10135. file)
  10136. (line
  10137. (concat file "::" (number-to-string line)))
  10138. (search
  10139. (concat file "::" search))))
  10140. (dlink (downcase link))
  10141. (old-buffer (current-buffer))
  10142. (old-pos (point))
  10143. (old-mode major-mode)
  10144. ext cmd link-match-data)
  10145. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10146. (setq ext (match-string 1 dfile))
  10147. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10148. (setq ext (match-string 1 dfile))))
  10149. (cond
  10150. ((member in-emacs '((16) system))
  10151. (setq cmd (cdr (assoc 'system apps))))
  10152. (in-emacs (setq cmd 'emacs))
  10153. (t
  10154. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10155. (and dirp (cdr (assoc 'directory apps)))
  10156. ; first, try matching against apps-dlink
  10157. ; if we get a match here, store the match data for later
  10158. (let ((match (assoc-default dlink apps-dlink
  10159. 'string-match)))
  10160. (if match
  10161. (progn (setq link-match-data (match-data))
  10162. match)
  10163. (progn (setq in-emacs (or in-emacs line search))
  10164. nil))) ; if we have no match in apps-dlink,
  10165. ; always open the file in emacs if line or search
  10166. ; is given (for backwards compatibility)
  10167. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10168. 'string-match)
  10169. (cdr (assoc ext apps))
  10170. (cdr (assoc t apps))))))
  10171. (when (eq cmd 'system)
  10172. (setq cmd (cdr (assoc 'system apps))))
  10173. (when (eq cmd 'default)
  10174. (setq cmd (cdr (assoc t apps))))
  10175. (when (eq cmd 'mailcap)
  10176. (require 'mailcap)
  10177. (mailcap-parse-mailcaps)
  10178. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10179. (command (mailcap-mime-info mime-type)))
  10180. (if (stringp command)
  10181. (setq cmd command)
  10182. (setq cmd 'emacs))))
  10183. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10184. (not (file-exists-p file))
  10185. (not org-open-non-existing-files))
  10186. (user-error "No such file: %s" file))
  10187. (cond
  10188. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10189. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10190. (while (string-match "['\"]%s['\"]" cmd)
  10191. (setq cmd (replace-match "%s" t t cmd)))
  10192. (while (string-match "%s" cmd)
  10193. (setq cmd (replace-match
  10194. (save-match-data
  10195. (shell-quote-argument
  10196. (convert-standard-filename file)))
  10197. t t cmd)))
  10198. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10199. (save-match-data
  10200. (let ((match-index 1)
  10201. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10202. (set-match-data link-match-data)
  10203. (while (<= match-index number-of-groups)
  10204. (let ((regex (concat "%" (number-to-string match-index)))
  10205. (replace-with (match-string match-index dlink)))
  10206. (while (string-match regex cmd)
  10207. (setq cmd (replace-match replace-with t t cmd))))
  10208. (setq match-index (+ match-index 1)))))
  10209. (save-window-excursion
  10210. (message "Running %s...done" cmd)
  10211. (start-process-shell-command cmd nil cmd)
  10212. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10213. ((or (stringp cmd)
  10214. (eq cmd 'emacs))
  10215. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10216. (widen)
  10217. (if line (progn (org-goto-line line)
  10218. (if (derived-mode-p 'org-mode)
  10219. (org-reveal)))
  10220. (if search (org-link-search search))))
  10221. ((consp cmd)
  10222. (let ((file (convert-standard-filename file)))
  10223. (save-match-data
  10224. (set-match-data link-match-data)
  10225. (eval cmd))))
  10226. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10227. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10228. (or (not (equal old-buffer (current-buffer)))
  10229. (not (equal old-pos (point))))
  10230. (org-mark-ring-push old-pos old-buffer))))
  10231. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10232. "This function returns non-nil if `entry' uses a regular
  10233. expression which should be matched against the whole link by
  10234. org-open-file.
  10235. It assumes that is the case when the entry uses a regular
  10236. expression which has at least one grouping construct and the
  10237. action is either a lisp form or a command string containing
  10238. '%1', i.e. using at least one subexpression match as a
  10239. parameter."
  10240. (let ((selector (car entry))
  10241. (action (cdr entry)))
  10242. (if (stringp selector)
  10243. (and (> (regexp-opt-depth selector) 0)
  10244. (or (and (stringp action)
  10245. (string-match "%[0-9]" action))
  10246. (consp action)))
  10247. nil)))
  10248. (defun org-default-apps ()
  10249. "Return the default applications for this operating system."
  10250. (cond
  10251. ((eq system-type 'darwin)
  10252. org-file-apps-defaults-macosx)
  10253. ((eq system-type 'windows-nt)
  10254. org-file-apps-defaults-windowsnt)
  10255. (t org-file-apps-defaults-gnu)))
  10256. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10257. "Convert extensions to regular expressions in the cars of LIST.
  10258. Also, weed out any non-string entries, because the return value is used
  10259. only for regexp matching.
  10260. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10261. point to the symbol `emacs', indicating that the file should
  10262. be opened in Emacs."
  10263. (append
  10264. (delq nil
  10265. (mapcar (lambda (x)
  10266. (if (not (stringp (car x)))
  10267. nil
  10268. (if (string-match "\\W" (car x))
  10269. x
  10270. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10271. list))
  10272. (if add-auto-mode
  10273. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10274. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10275. (defun org-file-remote-p (file)
  10276. "Test whether FILE specifies a location on a remote system.
  10277. Return non-nil if the location is indeed remote.
  10278. For example, the filename \"/user@host:/foo\" specifies a location
  10279. on the system \"/user@host:\"."
  10280. (cond ((fboundp 'file-remote-p)
  10281. (file-remote-p file))
  10282. ((fboundp 'tramp-handle-file-remote-p)
  10283. (tramp-handle-file-remote-p file))
  10284. ((and (boundp 'ange-ftp-name-format)
  10285. (string-match (car ange-ftp-name-format) file))
  10286. t)))
  10287. ;;;; Refiling
  10288. (defun org-get-org-file ()
  10289. "Read a filename, with default directory `org-directory'."
  10290. (let ((default (or org-default-notes-file remember-data-file)))
  10291. (read-file-name (format "File name [%s]: " default)
  10292. (file-name-as-directory org-directory)
  10293. default)))
  10294. (defun org-notes-order-reversed-p ()
  10295. "Check if the current file should receive notes in reversed order."
  10296. (cond
  10297. ((not org-reverse-note-order) nil)
  10298. ((eq t org-reverse-note-order) t)
  10299. ((not (listp org-reverse-note-order)) nil)
  10300. (t (catch 'exit
  10301. (let ((all org-reverse-note-order)
  10302. entry)
  10303. (while (setq entry (pop all))
  10304. (if (string-match (car entry) buffer-file-name)
  10305. (throw 'exit (cdr entry))))
  10306. nil)))))
  10307. (defvar org-refile-target-table nil
  10308. "The list of refile targets, created by `org-refile'.")
  10309. (defvar org-agenda-new-buffers nil
  10310. "Buffers created to visit agenda files.")
  10311. (defvar org-refile-cache nil
  10312. "Cache for refile targets.")
  10313. (defvar org-refile-markers nil
  10314. "All the markers used for caching refile locations.")
  10315. (defun org-refile-marker (pos)
  10316. "Get a new refile marker, but only if caching is in use."
  10317. (if (not org-refile-use-cache)
  10318. pos
  10319. (let ((m (make-marker)))
  10320. (move-marker m pos)
  10321. (push m org-refile-markers)
  10322. m)))
  10323. (defun org-refile-cache-clear ()
  10324. "Clear the refile cache and disable all the markers."
  10325. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10326. (setq org-refile-markers nil)
  10327. (setq org-refile-cache nil)
  10328. (message "Refile cache has been cleared"))
  10329. (defun org-refile-cache-check-set (set)
  10330. "Check if all the markers in the cache still have live buffers."
  10331. (let (marker)
  10332. (catch 'exit
  10333. (while (and set (setq marker (nth 3 (pop set))))
  10334. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10335. (when (and marker (null (marker-buffer marker)))
  10336. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10337. (sit-for 3)
  10338. (throw 'exit nil)))
  10339. t)))
  10340. (defun org-refile-cache-put (set &rest identifiers)
  10341. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10342. (let* ((key (sha1 (prin1-to-string identifiers)))
  10343. (entry (assoc key org-refile-cache)))
  10344. (if entry
  10345. (setcdr entry set)
  10346. (push (cons key set) org-refile-cache))))
  10347. (defun org-refile-cache-get (&rest identifiers)
  10348. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10349. (cond
  10350. ((not org-refile-cache) nil)
  10351. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10352. (t
  10353. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10354. org-refile-cache))))
  10355. (and set (org-refile-cache-check-set set) set)))))
  10356. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10357. "Produce a table with refile targets."
  10358. (let ((case-fold-search nil)
  10359. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10360. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10361. targets tgs txt re files f desc descre fast-path-p level pos0)
  10362. (message "Getting targets...")
  10363. (with-current-buffer (or default-buffer (current-buffer))
  10364. (while (setq entry (pop entries))
  10365. (setq files (car entry) desc (cdr entry))
  10366. (setq fast-path-p nil)
  10367. (cond
  10368. ((null files) (setq files (list (current-buffer))))
  10369. ((eq files 'org-agenda-files)
  10370. (setq files (org-agenda-files 'unrestricted)))
  10371. ((and (symbolp files) (fboundp files))
  10372. (setq files (funcall files)))
  10373. ((and (symbolp files) (boundp files))
  10374. (setq files (symbol-value files))))
  10375. (if (stringp files) (setq files (list files)))
  10376. (cond
  10377. ((eq (car desc) :tag)
  10378. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10379. ((eq (car desc) :todo)
  10380. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10381. ((eq (car desc) :regexp)
  10382. (setq descre (cdr desc)))
  10383. ((eq (car desc) :level)
  10384. (setq descre (concat "^\\*\\{" (number-to-string
  10385. (if org-odd-levels-only
  10386. (1- (* 2 (cdr desc)))
  10387. (cdr desc)))
  10388. "\\}[ \t]")))
  10389. ((eq (car desc) :maxlevel)
  10390. (setq fast-path-p t)
  10391. (setq descre (concat "^\\*\\{1," (number-to-string
  10392. (if org-odd-levels-only
  10393. (1- (* 2 (cdr desc)))
  10394. (cdr desc)))
  10395. "\\}[ \t]")))
  10396. (t (error "Bad refiling target description %s" desc)))
  10397. (while (setq f (pop files))
  10398. (with-current-buffer
  10399. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10400. (or
  10401. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10402. (progn
  10403. (if (bufferp f) (setq f (buffer-file-name
  10404. (buffer-base-buffer f))))
  10405. (setq f (and f (expand-file-name f)))
  10406. (if (eq org-refile-use-outline-path 'file)
  10407. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10408. (save-excursion
  10409. (save-restriction
  10410. (widen)
  10411. (goto-char (point-min))
  10412. (while (re-search-forward descre nil t)
  10413. (goto-char (setq pos0 (point-at-bol)))
  10414. (catch 'next
  10415. (when org-refile-target-verify-function
  10416. (save-match-data
  10417. (or (funcall org-refile-target-verify-function)
  10418. (throw 'next t))))
  10419. (when (and (looking-at org-complex-heading-regexp)
  10420. (not (member (match-string 4) excluded-entries))
  10421. (match-string 4))
  10422. (setq level (org-reduced-level
  10423. (- (match-end 1) (match-beginning 1)))
  10424. txt (org-link-display-format (match-string 4))
  10425. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10426. re (format org-complex-heading-regexp-format
  10427. (regexp-quote (match-string 4))))
  10428. (when org-refile-use-outline-path
  10429. (setq txt (mapconcat
  10430. 'org-protect-slash
  10431. (append
  10432. (if (eq org-refile-use-outline-path
  10433. 'file)
  10434. (list (file-name-nondirectory
  10435. (buffer-file-name
  10436. (buffer-base-buffer))))
  10437. (if (eq org-refile-use-outline-path
  10438. 'full-file-path)
  10439. (list (buffer-file-name
  10440. (buffer-base-buffer)))))
  10441. (org-get-outline-path fast-path-p
  10442. level txt)
  10443. (list txt))
  10444. "/")))
  10445. (push (list txt f re (org-refile-marker (point)))
  10446. tgs)))
  10447. (when (= (point) pos0)
  10448. ;; verification function has not moved point
  10449. (goto-char (point-at-eol))))))))
  10450. (when org-refile-use-cache
  10451. (org-refile-cache-put tgs (buffer-file-name) descre))
  10452. (setq targets (append tgs targets))))))
  10453. (message "Getting targets...done")
  10454. (nreverse targets)))
  10455. (defun org-protect-slash (s)
  10456. (while (string-match "/" s)
  10457. (setq s (replace-match "\\" t t s)))
  10458. s)
  10459. (defvar org-olpa (make-vector 20 nil))
  10460. (defun org-get-outline-path (&optional fastp level heading)
  10461. "Return the outline path to the current entry, as a list.
  10462. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10463. routine which makes outline path derivations for an entire file,
  10464. avoiding backtracing. Refile target collection makes use of that."
  10465. (if fastp
  10466. (progn
  10467. (if (> level 19)
  10468. (error "Outline path failure, more than 19 levels"))
  10469. (loop for i from level upto 19 do
  10470. (aset org-olpa i nil))
  10471. (prog1
  10472. (delq nil (append org-olpa nil))
  10473. (aset org-olpa level heading)))
  10474. (let (rtn case-fold-search)
  10475. (save-excursion
  10476. (save-restriction
  10477. (widen)
  10478. (while (org-up-heading-safe)
  10479. (when (looking-at org-complex-heading-regexp)
  10480. (push (org-trim
  10481. (replace-regexp-in-string
  10482. ;; Remove statistical/checkboxes cookies
  10483. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10484. (org-match-string-no-properties 4)))
  10485. rtn)))
  10486. rtn)))))
  10487. (defun org-format-outline-path (path &optional width prefix separator)
  10488. "Format the outline path PATH for display.
  10489. WIDTH is the maximum number of characters that is available.
  10490. PREFIX is a prefix to be included in the returned string,
  10491. such as the file name.
  10492. SEPARATOR is inserted between the different parts of the path,
  10493. the default is \"/\"."
  10494. (setq width (or width 79))
  10495. (if prefix (setq width (- width (length prefix))))
  10496. (if (not path)
  10497. (or prefix "")
  10498. (let* ((nsteps (length path))
  10499. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10500. (maxwidth (if (<= total-width width)
  10501. 10000 ;; everything fits
  10502. ;; we need to shorten the level headings
  10503. (/ (- width nsteps) nsteps)))
  10504. (org-odd-levels-only nil)
  10505. (n 0)
  10506. (total (1+ (length prefix))))
  10507. (setq maxwidth (max maxwidth 10))
  10508. (concat prefix
  10509. (if prefix (or separator "/"))
  10510. (mapconcat
  10511. (lambda (h)
  10512. (setq n (1+ n))
  10513. (if (and (= n nsteps) (< maxwidth 10000))
  10514. (setq maxwidth (- total-width total)))
  10515. (if (< (length h) maxwidth)
  10516. (progn (setq total (+ total (length h) 1)) h)
  10517. (setq h (substring h 0 (- maxwidth 2))
  10518. total (+ total maxwidth 1))
  10519. (if (string-match "[ \t]+\\'" h)
  10520. (setq h (substring h 0 (match-beginning 0))))
  10521. (setq h (concat h "..")))
  10522. (org-add-props h nil 'face
  10523. (nth (% (1- n) org-n-level-faces)
  10524. org-level-faces))
  10525. h)
  10526. path (or separator "/"))))))
  10527. (defun org-display-outline-path (&optional file current separator just-return-string)
  10528. "Display the current outline path in the echo area.
  10529. If FILE is non-nil, prepend the output with the file name.
  10530. If CURRENT is non-nil, append the current heading to the output.
  10531. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10532. the different parts of the path and defaults to \"/\".
  10533. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10534. (interactive "P")
  10535. (let* (case-fold-search
  10536. (bfn (buffer-file-name (buffer-base-buffer)))
  10537. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10538. res)
  10539. (if current (setq path (append path
  10540. (save-excursion
  10541. (org-back-to-heading t)
  10542. (if (looking-at org-complex-heading-regexp)
  10543. (list (match-string 4)))))))
  10544. (setq res
  10545. (org-format-outline-path
  10546. path
  10547. (1- (frame-width))
  10548. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10549. separator))
  10550. (if just-return-string
  10551. (org-no-properties res)
  10552. (org-unlogged-message "%s" res))))
  10553. (defvar org-refile-history nil
  10554. "History for refiling operations.")
  10555. (defvar org-after-refile-insert-hook nil
  10556. "Hook run after `org-refile' has inserted its stuff at the new location.
  10557. Note that this is still *before* the stuff will be removed from
  10558. the *old* location.")
  10559. (defvar org-capture-last-stored-marker)
  10560. (defvar org-refile-keep nil
  10561. "Non-nil means `org-refile' will copy instead of refile.")
  10562. (defun org-copy ()
  10563. "Like `org-refile', but copy."
  10564. (interactive)
  10565. (let ((org-refile-keep t))
  10566. (funcall 'org-refile nil nil nil "Copy")))
  10567. (defun org-refile (&optional arg default-buffer rfloc msg)
  10568. "Move the entry or entries at point to another heading.
  10569. The list of target headings is compiled using the information in
  10570. `org-refile-targets', which see.
  10571. At the target location, the entry is filed as a subitem of the
  10572. target heading. Depending on `org-reverse-note-order', the new
  10573. subitem will either be the first or the last subitem.
  10574. If there is an active region, all entries in that region will be
  10575. refiled. However, the region must fulfill the requirement that
  10576. the first heading sets the top-level of the moved text.
  10577. With prefix arg ARG, the command will only visit the target
  10578. location and not actually move anything.
  10579. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10580. refiling operation has put the subtree.
  10581. With a numeric prefix argument of `2', refile to the running clock.
  10582. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10583. being set to `t' and copy to the target location, don't move it.
  10584. Beware that keeping refiled entries may result in duplicated ID
  10585. properties.
  10586. RFLOC can be a refile location obtained in a different way.
  10587. MSG is a string to replace \"Refile\" in the default prompt with
  10588. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10589. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10590. If you are using target caching (see `org-refile-use-cache'), you
  10591. have to clear the target cache in order to find new targets.
  10592. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10593. prefix argument (`C-u C-u C-u C-c C-w')."
  10594. (interactive "P")
  10595. (if (member arg '(0 (64)))
  10596. (org-refile-cache-clear)
  10597. (let* ((actionmsg (cond (msg msg)
  10598. ((equal arg 3) "Refile (and keep)")
  10599. (t "Refile")))
  10600. (cbuf (current-buffer))
  10601. (regionp (org-region-active-p))
  10602. (region-start (and regionp (region-beginning)))
  10603. (region-end (and regionp (region-end)))
  10604. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10605. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10606. pos it nbuf file re level reversed)
  10607. (setq last-command nil)
  10608. (when regionp
  10609. (goto-char region-start)
  10610. (or (bolp) (goto-char (point-at-bol)))
  10611. (setq region-start (point))
  10612. (unless (or (org-kill-is-subtree-p
  10613. (buffer-substring region-start region-end))
  10614. (prog1 org-refile-active-region-within-subtree
  10615. (let ((s (point-at-eol)))
  10616. (org-toggle-heading)
  10617. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10618. (user-error "The region is not a (sequence of) subtree(s)")))
  10619. (if (equal arg '(16))
  10620. (org-refile-goto-last-stored)
  10621. (when (or
  10622. (and (equal arg 2)
  10623. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10624. (prog1
  10625. (setq it (list (or org-clock-heading "running clock")
  10626. (buffer-file-name
  10627. (marker-buffer org-clock-hd-marker))
  10628. ""
  10629. (marker-position org-clock-hd-marker)))
  10630. (setq arg nil)))
  10631. (setq it (or rfloc
  10632. (let (heading-text)
  10633. (save-excursion
  10634. (unless (and arg (listp arg))
  10635. (org-back-to-heading t)
  10636. (setq heading-text
  10637. (replace-regexp-in-string
  10638. org-bracket-link-regexp
  10639. "\\3"
  10640. (nth 4 (org-heading-components)))))
  10641. (org-refile-get-location
  10642. (cond ((and arg (listp arg)) "Goto")
  10643. (regionp (concat actionmsg " region to"))
  10644. (t (concat actionmsg " subtree \""
  10645. heading-text "\" to")))
  10646. default-buffer
  10647. (and (not (equal '(4) arg))
  10648. org-refile-allow-creating-parent-nodes)
  10649. arg))))))
  10650. (setq file (nth 1 it)
  10651. re (nth 2 it)
  10652. pos (nth 3 it))
  10653. (if (and (not arg)
  10654. pos
  10655. (equal (buffer-file-name) file)
  10656. (if regionp
  10657. (and (>= pos region-start)
  10658. (<= pos region-end))
  10659. (and (>= pos (point))
  10660. (< pos (save-excursion
  10661. (org-end-of-subtree t t))))))
  10662. (error "Cannot refile to position inside the tree or region"))
  10663. (setq nbuf (or (find-buffer-visiting file)
  10664. (find-file-noselect file)))
  10665. (if (and arg (not (equal arg 3)))
  10666. (progn
  10667. (org-pop-to-buffer-same-window nbuf)
  10668. (goto-char pos)
  10669. (org-show-context 'org-goto))
  10670. (if regionp
  10671. (progn
  10672. (org-kill-new (buffer-substring region-start region-end))
  10673. (org-save-markers-in-region region-start region-end))
  10674. (org-copy-subtree 1 nil t))
  10675. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10676. (find-file-noselect file)))
  10677. (setq reversed (org-notes-order-reversed-p))
  10678. (save-excursion
  10679. (save-restriction
  10680. (widen)
  10681. (if pos
  10682. (progn
  10683. (goto-char pos)
  10684. (looking-at org-outline-regexp)
  10685. (setq level (org-get-valid-level (funcall outline-level) 1))
  10686. (goto-char
  10687. (if reversed
  10688. (or (outline-next-heading) (point-max))
  10689. (or (save-excursion (org-get-next-sibling))
  10690. (org-end-of-subtree t t)
  10691. (point-max)))))
  10692. (setq level 1)
  10693. (if (not reversed)
  10694. (goto-char (point-max))
  10695. (goto-char (point-min))
  10696. (or (outline-next-heading) (goto-char (point-max)))))
  10697. (if (not (bolp)) (newline))
  10698. (org-paste-subtree level)
  10699. (when org-log-refile
  10700. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10701. (unless (eq org-log-refile 'note)
  10702. (save-excursion (org-add-log-note))))
  10703. (and org-auto-align-tags
  10704. (let ((org-loop-over-headlines-in-active-region nil))
  10705. (org-set-tags nil t)))
  10706. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10707. :last-refile)))
  10708. (when bookmark-name
  10709. (with-demoted-errors
  10710. (bookmark-set bookmark-name))))
  10711. ;; If we are refiling for capture, make sure that the
  10712. ;; last-capture pointers point here
  10713. (when (org-bound-and-true-p org-refile-for-capture)
  10714. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10715. :last-capture-marker)))
  10716. (when bookmark-name
  10717. (with-demoted-errors
  10718. (bookmark-set bookmark-name))))
  10719. (move-marker org-capture-last-stored-marker (point)))
  10720. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10721. (run-hooks 'org-after-refile-insert-hook))))
  10722. (unless org-refile-keep
  10723. (if regionp
  10724. (delete-region (point) (+ (point) (- region-end region-start)))
  10725. (delete-region
  10726. (and (org-back-to-heading t) (point))
  10727. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10728. (when (featurep 'org-inlinetask)
  10729. (org-inlinetask-remove-END-maybe))
  10730. (setq org-markers-to-move nil)
  10731. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10732. (defun org-refile-goto-last-stored ()
  10733. "Go to the location where the last refile was stored."
  10734. (interactive)
  10735. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10736. (message "This is the location of the last refile"))
  10737. (defun org-refile--get-location (refloc tbl)
  10738. "When user refile to REFLOC, find the associated target in TBL.
  10739. Also check `org-refile-target-table'."
  10740. (car (delq
  10741. nil
  10742. (mapcar
  10743. (lambda (r) (or (assoc r tbl)
  10744. (assoc r org-refile-target-table)))
  10745. (list (replace-regexp-in-string "/$" "" refloc)
  10746. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10747. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10748. no-exclude)
  10749. "Prompt the user for a refile location, using PROMPT.
  10750. PROMPT should not be suffixed with a colon and a space, because
  10751. this function appends the default value from
  10752. `org-refile-history' automatically, if that is not empty.
  10753. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10754. this is used for the GOTO interface."
  10755. (let ((org-refile-targets org-refile-targets)
  10756. (org-refile-use-outline-path org-refile-use-outline-path)
  10757. excluded-entries)
  10758. (when (and (derived-mode-p 'org-mode)
  10759. (not org-refile-use-cache)
  10760. (not no-exclude))
  10761. (org-map-tree
  10762. (lambda()
  10763. (setq excluded-entries
  10764. (append excluded-entries (list (org-get-heading t t)))))))
  10765. (setq org-refile-target-table
  10766. (org-refile-get-targets default-buffer excluded-entries)))
  10767. (unless org-refile-target-table
  10768. (user-error "No refile targets"))
  10769. (let* ((cbuf (current-buffer))
  10770. (partial-completion-mode nil)
  10771. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10772. (cfunc (if (and org-refile-use-outline-path
  10773. org-outline-path-complete-in-steps)
  10774. 'org-olpath-completing-read
  10775. 'org-icompleting-read))
  10776. (extra (if org-refile-use-outline-path "/" ""))
  10777. (cbnex (concat (buffer-name) extra))
  10778. (filename (and cfn (expand-file-name cfn)))
  10779. (tbl (mapcar
  10780. (lambda (x)
  10781. (if (and (not (member org-refile-use-outline-path
  10782. '(file full-file-path)))
  10783. (not (equal filename (nth 1 x))))
  10784. (cons (concat (car x) extra " ("
  10785. (file-name-nondirectory (nth 1 x)) ")")
  10786. (cdr x))
  10787. (cons (concat (car x) extra) (cdr x))))
  10788. org-refile-target-table))
  10789. (completion-ignore-case t)
  10790. cdef
  10791. (prompt (concat prompt
  10792. (or (and (car org-refile-history)
  10793. (concat " (default " (car org-refile-history) ")"))
  10794. (and (assoc cbnex tbl) (setq cdef cbnex)
  10795. (concat " (default " cbnex ")"))) ": "))
  10796. pa answ parent-target child parent old-hist)
  10797. (setq old-hist org-refile-history)
  10798. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10799. nil 'org-refile-history (or cdef (car org-refile-history))))
  10800. (if (setq pa (org-refile--get-location answ tbl))
  10801. (progn
  10802. (org-refile-check-position pa)
  10803. (when (or (not org-refile-history)
  10804. (not (eq old-hist org-refile-history))
  10805. (not (equal (car pa) (car org-refile-history))))
  10806. (setq org-refile-history
  10807. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10808. org-refile-history
  10809. (cdr org-refile-history))))
  10810. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10811. (pop org-refile-history)))
  10812. pa)
  10813. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10814. (progn
  10815. (setq parent (match-string 1 answ)
  10816. child (match-string 2 answ))
  10817. (setq parent-target (org-refile--get-location parent tbl))
  10818. (when (and parent-target
  10819. (or (eq new-nodes t)
  10820. (and (eq new-nodes 'confirm)
  10821. (y-or-n-p (format "Create new node \"%s\"? "
  10822. child)))))
  10823. (org-refile-new-child parent-target child)))
  10824. (user-error "Invalid target location")))))
  10825. (declare-function org-string-nw-p "org-macs" (s))
  10826. (defun org-refile-check-position (refile-pointer)
  10827. "Check if the refile pointer matches the headline to which it points."
  10828. (let* ((file (nth 1 refile-pointer))
  10829. (re (nth 2 refile-pointer))
  10830. (pos (nth 3 refile-pointer))
  10831. buffer)
  10832. (if (and (not (markerp pos)) (not file))
  10833. (user-error "Please indicate a target file in the refile path")
  10834. (when (org-string-nw-p re)
  10835. (setq buffer (if (markerp pos)
  10836. (marker-buffer pos)
  10837. (or (find-buffer-visiting file)
  10838. (find-file-noselect file))))
  10839. (with-current-buffer buffer
  10840. (save-excursion
  10841. (save-restriction
  10842. (widen)
  10843. (goto-char pos)
  10844. (beginning-of-line 1)
  10845. (unless (org-looking-at-p re)
  10846. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10847. (defun org-refile-new-child (parent-target child)
  10848. "Use refile target PARENT-TARGET to add new CHILD below it."
  10849. (unless parent-target
  10850. (error "Cannot find parent for new node"))
  10851. (let ((file (nth 1 parent-target))
  10852. (pos (nth 3 parent-target))
  10853. level)
  10854. (with-current-buffer (or (find-buffer-visiting file)
  10855. (find-file-noselect file))
  10856. (save-excursion
  10857. (save-restriction
  10858. (widen)
  10859. (if pos
  10860. (goto-char pos)
  10861. (goto-char (point-max))
  10862. (if (not (bolp)) (newline)))
  10863. (when (looking-at org-outline-regexp)
  10864. (setq level (funcall outline-level))
  10865. (org-end-of-subtree t t))
  10866. (org-back-over-empty-lines)
  10867. (insert "\n" (make-string
  10868. (if pos (org-get-valid-level level 1) 1) ?*)
  10869. " " child "\n")
  10870. (beginning-of-line 0)
  10871. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10872. (defun org-olpath-completing-read (prompt collection &rest args)
  10873. "Read an outline path like a file name."
  10874. (let ((thetable collection)
  10875. (org-completion-use-ido nil) ; does not work with ido.
  10876. (org-completion-use-iswitchb nil)) ; or iswitchb
  10877. (apply
  10878. 'org-icompleting-read prompt
  10879. (lambda (string predicate &optional flag)
  10880. (let (rtn r f (l (length string)))
  10881. (cond
  10882. ((eq flag nil)
  10883. ;; try completion
  10884. (try-completion string thetable))
  10885. ((eq flag t)
  10886. ;; all-completions
  10887. (setq rtn (all-completions string thetable predicate))
  10888. (mapcar
  10889. (lambda (x)
  10890. (setq r (substring x l))
  10891. (if (string-match " ([^)]*)$" x)
  10892. (setq f (match-string 0 x))
  10893. (setq f ""))
  10894. (if (string-match "/" r)
  10895. (concat string (substring r 0 (match-end 0)) f)
  10896. x))
  10897. rtn))
  10898. ((eq flag 'lambda)
  10899. ;; exact match?
  10900. (assoc string thetable)))))
  10901. args)))
  10902. ;;;; Dynamic blocks
  10903. (defun org-find-dblock (name)
  10904. "Find the first dynamic block with name NAME in the buffer.
  10905. If not found, stay at current position and return nil."
  10906. (let ((case-fold-search t) pos)
  10907. (save-excursion
  10908. (goto-char (point-min))
  10909. (setq pos (and (re-search-forward
  10910. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10911. (match-beginning 0))))
  10912. (if pos (goto-char pos))
  10913. pos))
  10914. (defun org-create-dblock (plist)
  10915. "Create a dynamic block section, with parameters taken from PLIST.
  10916. PLIST must contain a :name entry which is used as the name of the block."
  10917. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10918. (end-of-line 1)
  10919. (newline))
  10920. (let ((col (current-column))
  10921. (name (plist-get plist :name)))
  10922. (insert "#+BEGIN: " name)
  10923. (while plist
  10924. (if (eq (car plist) :name)
  10925. (setq plist (cddr plist))
  10926. (insert " " (prin1-to-string (pop plist)))))
  10927. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10928. (beginning-of-line -2)))
  10929. (defun org-prepare-dblock ()
  10930. "Prepare dynamic block for refresh.
  10931. This empties the block, puts the cursor at the insert position and returns
  10932. the property list including an extra property :name with the block name."
  10933. (unless (looking-at org-dblock-start-re)
  10934. (user-error "Not at a dynamic block"))
  10935. (let* ((begdel (1+ (match-end 0)))
  10936. (name (org-no-properties (match-string 1)))
  10937. (params (append (list :name name)
  10938. (read (concat "(" (match-string 3) ")")))))
  10939. (save-excursion
  10940. (beginning-of-line 1)
  10941. (skip-chars-forward " \t")
  10942. (setq params (plist-put params :indentation-column (current-column))))
  10943. (unless (re-search-forward org-dblock-end-re nil t)
  10944. (error "Dynamic block not terminated"))
  10945. (setq params
  10946. (append params
  10947. (list :content (buffer-substring
  10948. begdel (match-beginning 0)))))
  10949. (delete-region begdel (match-beginning 0))
  10950. (goto-char begdel)
  10951. (open-line 1)
  10952. params))
  10953. (defun org-map-dblocks (&optional command)
  10954. "Apply COMMAND to all dynamic blocks in the current buffer.
  10955. If COMMAND is not given, use `org-update-dblock'."
  10956. (let ((cmd (or command 'org-update-dblock)))
  10957. (save-excursion
  10958. (goto-char (point-min))
  10959. (while (re-search-forward org-dblock-start-re nil t)
  10960. (goto-char (match-beginning 0))
  10961. (save-excursion
  10962. (condition-case nil
  10963. (funcall cmd)
  10964. (error (message "Error during update of dynamic block"))))
  10965. (unless (re-search-forward org-dblock-end-re nil t)
  10966. (error "Dynamic block not terminated"))))))
  10967. (defun org-dblock-update (&optional arg)
  10968. "User command for updating dynamic blocks.
  10969. Update the dynamic block at point. With prefix ARG, update all dynamic
  10970. blocks in the buffer."
  10971. (interactive "P")
  10972. (if arg
  10973. (org-update-all-dblocks)
  10974. (or (looking-at org-dblock-start-re)
  10975. (org-beginning-of-dblock))
  10976. (org-update-dblock)))
  10977. (defun org-update-dblock ()
  10978. "Update the dynamic block at point.
  10979. This means to empty the block, parse for parameters and then call
  10980. the correct writing function."
  10981. (interactive)
  10982. (save-excursion
  10983. (let* ((win (selected-window))
  10984. (pos (point))
  10985. (line (org-current-line))
  10986. (params (org-prepare-dblock))
  10987. (name (plist-get params :name))
  10988. (indent (plist-get params :indentation-column))
  10989. (cmd (intern (concat "org-dblock-write:" name))))
  10990. (message "Updating dynamic block `%s' at line %d..." name line)
  10991. (funcall cmd params)
  10992. (message "Updating dynamic block `%s' at line %d...done" name line)
  10993. (goto-char pos)
  10994. (when (and indent (> indent 0))
  10995. (setq indent (make-string indent ?\ ))
  10996. (save-excursion
  10997. (select-window win)
  10998. (org-beginning-of-dblock)
  10999. (forward-line 1)
  11000. (while (not (looking-at org-dblock-end-re))
  11001. (insert indent)
  11002. (beginning-of-line 2))
  11003. (when (looking-at org-dblock-end-re)
  11004. (and (looking-at "[ \t]+")
  11005. (replace-match ""))
  11006. (insert indent)))))))
  11007. (defun org-beginning-of-dblock ()
  11008. "Find the beginning of the dynamic block at point.
  11009. Error if there is no such block at point."
  11010. (let ((pos (point))
  11011. beg)
  11012. (end-of-line 1)
  11013. (if (and (re-search-backward org-dblock-start-re nil t)
  11014. (setq beg (match-beginning 0))
  11015. (re-search-forward org-dblock-end-re nil t)
  11016. (> (match-end 0) pos))
  11017. (goto-char beg)
  11018. (goto-char pos)
  11019. (error "Not in a dynamic block"))))
  11020. (defun org-update-all-dblocks ()
  11021. "Update all dynamic blocks in the buffer.
  11022. This function can be used in a hook."
  11023. (interactive)
  11024. (when (derived-mode-p 'org-mode)
  11025. (org-map-dblocks 'org-update-dblock)))
  11026. ;;;; Completion
  11027. (declare-function org-export-backend-name "org-export" (cl-x))
  11028. (declare-function org-export-backend-options "org-export" (cl-x))
  11029. (defun org-get-export-keywords ()
  11030. "Return a list of all currently understood export keywords.
  11031. Export keywords include options, block names, attributes and
  11032. keywords relative to each registered export back-end."
  11033. (let (keywords)
  11034. (dolist (backend
  11035. (org-bound-and-true-p org-export--registered-backends)
  11036. (delq nil keywords))
  11037. ;; Back-end name (for keywords, like #+LATEX:)
  11038. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11039. (dolist (option-entry (org-export-backend-options backend))
  11040. ;; Back-end options.
  11041. (push (nth 1 option-entry) keywords)))))
  11042. (defconst org-options-keywords
  11043. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11044. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11045. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11046. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11047. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11048. (defcustom org-structure-template-alist
  11049. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC" "<src lang=\"?\">\n\n</src>")
  11050. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE" "<example>\n?\n</example>")
  11051. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE" "<quote>\n?\n</quote>")
  11052. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE" "<verse>\n?\n</verse>")
  11053. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM" "<verbatim>\n?\n</verbatim>")
  11054. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER" "<center>\n?\n</center>")
  11055. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  11056. "<literal style=\"latex\">\n?\n</literal>")
  11057. ("L" "#+LaTeX: " "<literal style=\"latex\">?</literal>")
  11058. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  11059. "<literal style=\"html\">\n?\n</literal>")
  11060. ("H" "#+HTML: " "<literal style=\"html\">?</literal>")
  11061. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII" "")
  11062. ("A" "#+ASCII: " "")
  11063. ("i" "#+INDEX: ?" "#+INDEX: ?")
  11064. ("I" "#+INCLUDE: %file ?"
  11065. "<include file=%file markup=\"?\">"))
  11066. "Structure completion elements.
  11067. This is a list of abbreviation keys and values. The value gets inserted
  11068. if you type `<' followed by the key and then press the completion key,
  11069. usually `M-TAB'. %file will be replaced by a file name after prompting
  11070. for the file using completion. The cursor will be placed at the position
  11071. of the `?` in the template.
  11072. There are two templates for each key, the first uses the original Org syntax,
  11073. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11074. the default when the /org-mtags.el/ module has been loaded. See also the
  11075. variable `org-mtags-prefer-muse-templates'."
  11076. :group 'org-completion
  11077. :type '(repeat
  11078. (list
  11079. (string :tag "Key")
  11080. (string :tag "Template")
  11081. (string :tag "Muse Template"))))
  11082. (defun org-try-structure-completion ()
  11083. "Try to complete a structure template before point.
  11084. This looks for strings like \"<e\" on an otherwise empty line and
  11085. expands them."
  11086. (let ((l (buffer-substring (point-at-bol) (point)))
  11087. a)
  11088. (when (and (looking-at "[ \t]*$")
  11089. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11090. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11091. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11092. (match-beginning 1)) a)
  11093. t)))
  11094. (defun org-complete-expand-structure-template (start cell)
  11095. "Expand a structure template."
  11096. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  11097. (rpl (nth (if musep 2 1) cell))
  11098. (ind ""))
  11099. (delete-region start (point))
  11100. (when (string-match "\\`[ \t]*#\\+" rpl)
  11101. (cond
  11102. ((bolp))
  11103. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11104. (setq ind (buffer-substring (point-at-bol) (point))))
  11105. (t (newline))))
  11106. (setq start (point))
  11107. (if (string-match "%file" rpl)
  11108. (setq rpl (replace-match
  11109. (concat
  11110. "\""
  11111. (save-match-data
  11112. (abbreviate-file-name (read-file-name "Include file: ")))
  11113. "\"")
  11114. t t rpl)))
  11115. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11116. (concat "\n" ind)))
  11117. (insert rpl)
  11118. (if (re-search-backward "\\?" start t) (delete-char 1))))
  11119. ;;;; TODO, DEADLINE, Comments
  11120. (defun org-toggle-comment ()
  11121. "Change the COMMENT state of an entry."
  11122. (interactive)
  11123. (save-excursion
  11124. (org-back-to-heading)
  11125. (looking-at org-complex-heading-regexp)
  11126. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11127. (skip-chars-forward " \t")
  11128. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11129. (if (org-in-commented-heading-p t)
  11130. (delete-region (point)
  11131. (progn (search-forward " " (line-end-position) 'move)
  11132. (skip-chars-forward " \t")
  11133. (point)))
  11134. (insert org-comment-string)
  11135. (unless (eolp) (insert " ")))))
  11136. (defvar org-last-todo-state-is-todo nil
  11137. "This is non-nil when the last TODO state change led to a TODO state.
  11138. If the last change removed the TODO tag or switched to DONE, then
  11139. this is nil.")
  11140. (defvar org-setting-tags nil) ; dynamically skipped
  11141. (defvar org-todo-setup-filter-hook nil
  11142. "Hook for functions that pre-filter todo specs.
  11143. Each function takes a todo spec and returns either nil or the spec
  11144. transformed into canonical form." )
  11145. (defvar org-todo-get-default-hook nil
  11146. "Hook for functions that get a default item for todo.
  11147. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11148. nil or a string to be used for the todo mark." )
  11149. (defvar org-agenda-headline-snapshot-before-repeat)
  11150. (defun org-current-effective-time ()
  11151. "Return current time adjusted for `org-extend-today-until' variable."
  11152. (let* ((ct (org-current-time))
  11153. (dct (decode-time ct))
  11154. (ct1
  11155. (cond
  11156. (org-use-last-clock-out-time-as-effective-time
  11157. (or (org-clock-get-last-clock-out-time) ct))
  11158. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11159. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11160. (t ct))))
  11161. ct1))
  11162. (defun org-todo-yesterday (&optional arg)
  11163. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11164. (interactive "P")
  11165. (if (eq major-mode 'org-agenda-mode)
  11166. (apply 'org-agenda-todo-yesterday arg)
  11167. (let* ((hour (third (decode-time
  11168. (org-current-time))))
  11169. (org-extend-today-until (1+ hour)))
  11170. (org-todo arg))))
  11171. (defvar org-block-entry-blocking ""
  11172. "First entry preventing the TODO state change.")
  11173. (defun org-cancel-repeater ()
  11174. "Cancel a repeater by setting its numeric value to zero."
  11175. (interactive)
  11176. (save-excursion
  11177. (org-back-to-heading t)
  11178. (let ((bound1 (point))
  11179. (bound0 (save-excursion (outline-next-heading) (point))))
  11180. (when (re-search-forward
  11181. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11182. org-deadline-time-regexp "\\)\\|\\("
  11183. org-ts-regexp "\\)")
  11184. bound0 t)
  11185. (if (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]" bound1 t)
  11186. (replace-match "0" t nil nil 1))))))
  11187. (defun org-todo (&optional arg)
  11188. "Change the TODO state of an item.
  11189. The state of an item is given by a keyword at the start of the heading,
  11190. like
  11191. *** TODO Write paper
  11192. *** DONE Call mom
  11193. The different keywords are specified in the variable `org-todo-keywords'.
  11194. By default the available states are \"TODO\" and \"DONE\".
  11195. So for this example: when the item starts with TODO, it is changed to DONE.
  11196. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11197. DONE are present, add TODO at the beginning of the heading.
  11198. With \\[universal-argument] prefix arg, use completion to determine the new \
  11199. state.
  11200. With numeric prefix arg, switch to that state.
  11201. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11202. keywords (nextset).
  11203. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11204. With a numeric prefix arg of 0, inhibit note taking for the change.
  11205. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11206. When called through ELisp, arg is also interpreted in the following way:
  11207. 'none -> empty state
  11208. \"\"(empty string) -> switch to empty state
  11209. 'done -> switch to DONE
  11210. 'nextset -> switch to the next set of keywords
  11211. 'previousset -> switch to the previous set of keywords
  11212. \"WAITING\" -> switch to the specified keyword, but only if it
  11213. really is a member of `org-todo-keywords'."
  11214. (interactive "P")
  11215. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11216. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11217. 'region-start-level 'region))
  11218. org-loop-over-headlines-in-active-region)
  11219. (org-map-entries
  11220. `(org-todo ,arg)
  11221. org-loop-over-headlines-in-active-region
  11222. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11223. (if (equal arg '(16)) (setq arg 'nextset))
  11224. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11225. (let ((org-blocker-hook org-blocker-hook)
  11226. commentp
  11227. case-fold-search)
  11228. (when (equal arg '(64))
  11229. (setq arg nil org-blocker-hook nil))
  11230. (when (and org-blocker-hook
  11231. (or org-inhibit-blocking
  11232. (org-entry-get nil "NOBLOCKING")))
  11233. (setq org-blocker-hook nil))
  11234. (save-excursion
  11235. (catch 'exit
  11236. (org-back-to-heading t)
  11237. (when (org-in-commented-heading-p t)
  11238. (org-toggle-comment)
  11239. (setq commentp t))
  11240. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11241. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11242. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11243. (let* ((match-data (match-data))
  11244. (startpos (point-at-bol))
  11245. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11246. (org-log-done org-log-done)
  11247. (org-log-repeat org-log-repeat)
  11248. (org-todo-log-states org-todo-log-states)
  11249. (org-inhibit-logging
  11250. (if (equal arg 0)
  11251. (progn (setq arg nil) 'note) org-inhibit-logging))
  11252. (this (match-string 1))
  11253. (hl-pos (match-beginning 0))
  11254. (head (org-get-todo-sequence-head this))
  11255. (ass (assoc head org-todo-kwd-alist))
  11256. (interpret (nth 1 ass))
  11257. (done-word (nth 3 ass))
  11258. (final-done-word (nth 4 ass))
  11259. (org-last-state (or this ""))
  11260. (completion-ignore-case t)
  11261. (member (member this org-todo-keywords-1))
  11262. (tail (cdr member))
  11263. (org-state (cond
  11264. ((and org-todo-key-trigger
  11265. (or (and (equal arg '(4))
  11266. (eq org-use-fast-todo-selection 'prefix))
  11267. (and (not arg) org-use-fast-todo-selection
  11268. (not (eq org-use-fast-todo-selection
  11269. 'prefix)))))
  11270. ;; Use fast selection
  11271. (org-fast-todo-selection))
  11272. ((and (equal arg '(4))
  11273. (or (not org-use-fast-todo-selection)
  11274. (not org-todo-key-trigger)))
  11275. ;; Read a state with completion
  11276. (org-icompleting-read
  11277. "State: " (mapcar 'list org-todo-keywords-1)
  11278. nil t))
  11279. ((eq arg 'right)
  11280. (if this
  11281. (if tail (car tail) nil)
  11282. (car org-todo-keywords-1)))
  11283. ((eq arg 'left)
  11284. (if (equal member org-todo-keywords-1)
  11285. nil
  11286. (if this
  11287. (nth (- (length org-todo-keywords-1)
  11288. (length tail) 2)
  11289. org-todo-keywords-1)
  11290. (org-last org-todo-keywords-1))))
  11291. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11292. (setq arg nil))) ; hack to fall back to cycling
  11293. (arg
  11294. ;; user or caller requests a specific state
  11295. (cond
  11296. ((equal arg "") nil)
  11297. ((eq arg 'none) nil)
  11298. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11299. ((eq arg 'nextset)
  11300. (or (car (cdr (member head org-todo-heads)))
  11301. (car org-todo-heads)))
  11302. ((eq arg 'previousset)
  11303. (let ((org-todo-heads (reverse org-todo-heads)))
  11304. (or (car (cdr (member head org-todo-heads)))
  11305. (car org-todo-heads))))
  11306. ((car (member arg org-todo-keywords-1)))
  11307. ((stringp arg)
  11308. (user-error "State `%s' not valid in this file" arg))
  11309. ((nth (1- (prefix-numeric-value arg))
  11310. org-todo-keywords-1))))
  11311. ((null member) (or head (car org-todo-keywords-1)))
  11312. ((equal this final-done-word) nil) ;; -> make empty
  11313. ((null tail) nil) ;; -> first entry
  11314. ((memq interpret '(type priority))
  11315. (if (eq this-command last-command)
  11316. (car tail)
  11317. (if (> (length tail) 0)
  11318. (or done-word (car org-done-keywords))
  11319. nil)))
  11320. (t
  11321. (car tail))))
  11322. (org-state (or
  11323. (run-hook-with-args-until-success
  11324. 'org-todo-get-default-hook org-state org-last-state)
  11325. org-state))
  11326. (next (if org-state (concat " " org-state " ") " "))
  11327. (change-plist (list :type 'todo-state-change :from this :to org-state
  11328. :position startpos))
  11329. dolog now-done-p)
  11330. (when org-blocker-hook
  11331. (setq org-last-todo-state-is-todo
  11332. (not (member this org-done-keywords)))
  11333. (unless (save-excursion
  11334. (save-match-data
  11335. (org-with-wide-buffer
  11336. (run-hook-with-args-until-failure
  11337. 'org-blocker-hook change-plist))))
  11338. (if (org-called-interactively-p 'interactive)
  11339. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11340. this org-state org-block-entry-blocking)
  11341. ;; fail silently
  11342. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11343. this org-state org-block-entry-blocking)
  11344. (throw 'exit nil))))
  11345. (store-match-data match-data)
  11346. (replace-match next t t)
  11347. (unless (pos-visible-in-window-p hl-pos)
  11348. (message "TODO state changed to %s" (org-trim next)))
  11349. (unless head
  11350. (setq head (org-get-todo-sequence-head org-state)
  11351. ass (assoc head org-todo-kwd-alist)
  11352. interpret (nth 1 ass)
  11353. done-word (nth 3 ass)
  11354. final-done-word (nth 4 ass)))
  11355. (when (memq arg '(nextset previousset))
  11356. (message "Keyword-Set %d/%d: %s"
  11357. (- (length org-todo-sets) -1
  11358. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11359. (length org-todo-sets)
  11360. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11361. (setq org-last-todo-state-is-todo
  11362. (not (member org-state org-done-keywords)))
  11363. (setq now-done-p (and (member org-state org-done-keywords)
  11364. (not (member this org-done-keywords))))
  11365. (and logging (org-local-logging logging))
  11366. (when (and (or org-todo-log-states org-log-done)
  11367. (not (eq org-inhibit-logging t))
  11368. (not (memq arg '(nextset previousset))))
  11369. ;; we need to look at recording a time and note
  11370. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11371. (nth 2 (assoc this org-todo-log-states))))
  11372. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11373. (setq dolog 'time))
  11374. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11375. (and org-state
  11376. (member org-state org-not-done-keywords)
  11377. (not (member this org-not-done-keywords))))
  11378. ;; This is now a todo state and was not one before
  11379. ;; If there was a CLOSED time stamp, get rid of it.
  11380. (org-add-planning-info nil nil 'closed))
  11381. (when (and now-done-p org-log-done)
  11382. ;; It is now done, and it was not done before
  11383. (org-add-planning-info 'closed (org-current-effective-time))
  11384. (if (and (not dolog) (eq 'note org-log-done))
  11385. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11386. (when (and org-state dolog)
  11387. ;; This is a non-nil state, and we need to log it
  11388. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11389. ;; Fixup tag positioning
  11390. (org-todo-trigger-tag-changes org-state)
  11391. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11392. (when org-provide-todo-statistics
  11393. (org-update-parent-todo-statistics))
  11394. (run-hooks 'org-after-todo-state-change-hook)
  11395. (if (and arg (not (member org-state org-done-keywords)))
  11396. (setq head (org-get-todo-sequence-head org-state)))
  11397. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11398. ;; Do we need to trigger a repeat?
  11399. (when now-done-p
  11400. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11401. ;; This is for the agenda, take a snapshot of the headline.
  11402. (save-match-data
  11403. (setq org-agenda-headline-snapshot-before-repeat
  11404. (org-get-heading))))
  11405. (org-auto-repeat-maybe org-state))
  11406. ;; Fixup cursor location if close to the keyword
  11407. (if (and (outline-on-heading-p)
  11408. (not (bolp))
  11409. (save-excursion (beginning-of-line 1)
  11410. (looking-at org-todo-line-regexp))
  11411. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11412. (progn
  11413. (goto-char (or (match-end 2) (match-end 1)))
  11414. (and (looking-at " ") (just-one-space))))
  11415. (when org-trigger-hook
  11416. (save-excursion
  11417. (run-hook-with-args 'org-trigger-hook change-plist)))
  11418. (when commentp (org-toggle-comment))))))))
  11419. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11420. "Block turning an entry into a TODO, using the hierarchy.
  11421. This checks whether the current task should be blocked from state
  11422. changes. Such blocking occurs when:
  11423. 1. The task has children which are not all in a completed state.
  11424. 2. A task has a parent with the property :ORDERED:, and there
  11425. are siblings prior to the current task with incomplete
  11426. status.
  11427. 3. The parent of the task is blocked because it has siblings that should
  11428. be done first, or is child of a block grandparent TODO entry."
  11429. (if (not org-enforce-todo-dependencies)
  11430. t ; if locally turned off don't block
  11431. (catch 'dont-block
  11432. ;; If this is not a todo state change, or if this entry is already DONE,
  11433. ;; do not block
  11434. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11435. (member (plist-get change-plist :from)
  11436. (cons 'done org-done-keywords))
  11437. (member (plist-get change-plist :to)
  11438. (cons 'todo org-not-done-keywords))
  11439. (not (plist-get change-plist :to)))
  11440. (throw 'dont-block t))
  11441. ;; If this task has children, and any are undone, it's blocked
  11442. (save-excursion
  11443. (org-back-to-heading t)
  11444. (let ((this-level (funcall outline-level)))
  11445. (outline-next-heading)
  11446. (let ((child-level (funcall outline-level)))
  11447. (while (and (not (eobp))
  11448. (> child-level this-level))
  11449. ;; this todo has children, check whether they are all
  11450. ;; completed
  11451. (if (and (not (org-entry-is-done-p))
  11452. (org-entry-is-todo-p))
  11453. (progn (setq org-block-entry-blocking (org-get-heading))
  11454. (throw 'dont-block nil)))
  11455. (outline-next-heading)
  11456. (setq child-level (funcall outline-level))))))
  11457. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11458. ;; any previous siblings are undone, it's blocked
  11459. (save-excursion
  11460. (org-back-to-heading t)
  11461. (let* ((pos (point))
  11462. (parent-pos (and (org-up-heading-safe) (point))))
  11463. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11464. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11465. (forward-line 1)
  11466. (re-search-forward org-not-done-heading-regexp pos t))
  11467. (setq org-block-entry-blocking (match-string 0))
  11468. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11469. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11470. (while t
  11471. (goto-char parent-pos)
  11472. (if (not (looking-at org-not-done-heading-regexp))
  11473. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11474. (setq pos (point))
  11475. (setq parent-pos (and (org-up-heading-safe) (point)))
  11476. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11477. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11478. (forward-line 1)
  11479. (re-search-forward org-not-done-heading-regexp pos t)
  11480. (setq org-block-entry-blocking (org-get-heading)))
  11481. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11482. (defcustom org-track-ordered-property-with-tag nil
  11483. "Should the ORDERED property also be shown as a tag?
  11484. The ORDERED property decides if an entry should require subtasks to be
  11485. completed in sequence. Since a property is not very visible, setting
  11486. this option means that toggling the ORDERED property with the command
  11487. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11488. not relevant for the behavior, but it makes things more visible.
  11489. Note that toggling the tag with tags commands will not change the property
  11490. and therefore not influence behavior!
  11491. This can be t, meaning the tag ORDERED should be used, It can also be a
  11492. string to select a different tag for this task."
  11493. :group 'org-todo
  11494. :type '(choice
  11495. (const :tag "No tracking" nil)
  11496. (const :tag "Track with ORDERED tag" t)
  11497. (string :tag "Use other tag")))
  11498. (defun org-toggle-ordered-property ()
  11499. "Toggle the ORDERED property of the current entry.
  11500. For better visibility, you can track the value of this property with a tag.
  11501. See variable `org-track-ordered-property-with-tag'."
  11502. (interactive)
  11503. (let* ((t1 org-track-ordered-property-with-tag)
  11504. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11505. (save-excursion
  11506. (org-back-to-heading)
  11507. (if (org-entry-get nil "ORDERED")
  11508. (progn
  11509. (org-delete-property "ORDERED")
  11510. (and tag (org-toggle-tag tag 'off))
  11511. (message "Subtasks can be completed in arbitrary order"))
  11512. (org-entry-put nil "ORDERED" "t")
  11513. (and tag (org-toggle-tag tag 'on))
  11514. (message "Subtasks must be completed in sequence")))))
  11515. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11516. (defun org-block-todo-from-checkboxes (change-plist)
  11517. "Block turning an entry into a TODO, using checkboxes.
  11518. This checks whether the current task should be blocked from state
  11519. changes because there are unchecked boxes in this entry."
  11520. (if (not org-enforce-todo-checkbox-dependencies)
  11521. t ; if locally turned off don't block
  11522. (catch 'dont-block
  11523. ;; If this is not a todo state change, or if this entry is already DONE,
  11524. ;; do not block
  11525. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11526. (member (plist-get change-plist :from)
  11527. (cons 'done org-done-keywords))
  11528. (member (plist-get change-plist :to)
  11529. (cons 'todo org-not-done-keywords))
  11530. (not (plist-get change-plist :to)))
  11531. (throw 'dont-block t))
  11532. ;; If this task has checkboxes that are not checked, it's blocked
  11533. (save-excursion
  11534. (org-back-to-heading t)
  11535. (let ((beg (point)) end)
  11536. (outline-next-heading)
  11537. (setq end (point))
  11538. (goto-char beg)
  11539. (if (org-list-search-forward
  11540. (concat (org-item-beginning-re)
  11541. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11542. "\\[[- ]\\]")
  11543. end t)
  11544. (progn
  11545. (if (boundp 'org-blocked-by-checkboxes)
  11546. (setq org-blocked-by-checkboxes t))
  11547. (throw 'dont-block nil)))))
  11548. t))) ; do not block
  11549. (defun org-entry-blocked-p ()
  11550. "Is the current entry blocked?"
  11551. (org-with-silent-modifications
  11552. (if (org-entry-get nil "NOBLOCKING")
  11553. nil ;; Never block this entry
  11554. (not (run-hook-with-args-until-failure
  11555. 'org-blocker-hook
  11556. (list :type 'todo-state-change
  11557. :position (point)
  11558. :from 'todo
  11559. :to 'done))))))
  11560. (defun org-update-statistics-cookies (all)
  11561. "Update the statistics cookie, either from TODO or from checkboxes.
  11562. This should be called with the cursor in a line with a statistics cookie."
  11563. (interactive "P")
  11564. (if all
  11565. (progn
  11566. (org-update-checkbox-count 'all)
  11567. (org-map-entries 'org-update-parent-todo-statistics))
  11568. (if (not (org-at-heading-p))
  11569. (org-update-checkbox-count)
  11570. (let ((pos (point-marker))
  11571. end l1 l2)
  11572. (ignore-errors (org-back-to-heading t))
  11573. (if (not (org-at-heading-p))
  11574. (org-update-checkbox-count)
  11575. (setq l1 (org-outline-level))
  11576. (setq end (save-excursion
  11577. (outline-next-heading)
  11578. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11579. (point)))
  11580. (if (and (save-excursion
  11581. (re-search-forward
  11582. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11583. (not (save-excursion (re-search-forward
  11584. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11585. (org-update-checkbox-count)
  11586. (if (and l2 (> l2 l1))
  11587. (progn
  11588. (goto-char end)
  11589. (org-update-parent-todo-statistics))
  11590. (goto-char pos)
  11591. (beginning-of-line 1)
  11592. (while (re-search-forward
  11593. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11594. (point-at-eol) t)
  11595. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11596. (goto-char pos)
  11597. (move-marker pos nil)))))
  11598. (defvar org-entry-property-inherited-from) ;; defined below
  11599. (defun org-update-parent-todo-statistics ()
  11600. "Update any statistics cookie in the parent of the current headline.
  11601. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11602. the entire subtree and this will travel up the hierarchy and update
  11603. statistics everywhere."
  11604. (let* ((prop (save-excursion (org-up-heading-safe)
  11605. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11606. (recursive (or (not org-hierarchical-todo-statistics)
  11607. (and prop (string-match "\\<recursive\\>" prop))))
  11608. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11609. 0))
  11610. (first t)
  11611. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11612. level ltoggle l1 new ndel
  11613. (cnt-all 0) (cnt-done 0) is-percent kwd
  11614. checkbox-beg ov ovs ove cookie-present)
  11615. (catch 'exit
  11616. (save-excursion
  11617. (beginning-of-line 1)
  11618. (setq ltoggle (funcall outline-level))
  11619. ;; Three situations are to consider:
  11620. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11621. ;; to the top-level ancestor on the headline;
  11622. ;; 2. If parent has "recursive" property, repeat up to the
  11623. ;; headline setting that property, taking inheritance into
  11624. ;; account;
  11625. ;; 3. Else, move up to direct parent and proceed only once.
  11626. (while (and (setq level (org-up-heading-safe))
  11627. (or recursive first)
  11628. (>= (point) lim))
  11629. (setq first nil cookie-present nil)
  11630. (unless (and level
  11631. (not (string-match
  11632. "\\<checkbox\\>"
  11633. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11634. "")))))
  11635. (throw 'exit nil))
  11636. (while (re-search-forward box-re (point-at-eol) t)
  11637. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11638. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11639. (save-match-data
  11640. (unless (outline-next-heading) (throw 'exit nil))
  11641. (while (and (looking-at org-complex-heading-regexp)
  11642. (> (setq l1 (length (match-string 1))) level))
  11643. (setq kwd (and (or recursive (= l1 ltoggle))
  11644. (match-string 2)))
  11645. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11646. (and (eq org-provide-todo-statistics t)
  11647. (or (member kwd org-done-keywords)))
  11648. (and (listp org-provide-todo-statistics)
  11649. (stringp (car org-provide-todo-statistics))
  11650. (or (member kwd org-provide-todo-statistics)
  11651. (member kwd org-done-keywords)))
  11652. (and (listp org-provide-todo-statistics)
  11653. (listp (car org-provide-todo-statistics))
  11654. (or (member kwd (car org-provide-todo-statistics))
  11655. (and (member kwd org-done-keywords)
  11656. (member kwd (cadr org-provide-todo-statistics))))))
  11657. (setq cnt-all (1+ cnt-all))
  11658. (if (eq org-provide-todo-statistics t)
  11659. (and kwd (setq cnt-all (1+ cnt-all)))))
  11660. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11661. (member kwd org-done-keywords))
  11662. (and (listp org-provide-todo-statistics)
  11663. (listp (car org-provide-todo-statistics))
  11664. (member kwd org-done-keywords)
  11665. (member kwd (cadr org-provide-todo-statistics)))
  11666. (and (listp org-provide-todo-statistics)
  11667. (stringp (car org-provide-todo-statistics))
  11668. (member kwd org-done-keywords)))
  11669. (setq cnt-done (1+ cnt-done)))
  11670. (outline-next-heading)))
  11671. (setq new
  11672. (if is-percent
  11673. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11674. (format "[%d/%d]" cnt-done cnt-all))
  11675. ndel (- (match-end 0) checkbox-beg))
  11676. ;; handle overlays when updating cookie from column view
  11677. (when (setq ov (car (overlays-at checkbox-beg)))
  11678. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11679. (delete-overlay ov))
  11680. (goto-char checkbox-beg)
  11681. (insert new)
  11682. (delete-region (point) (+ (point) ndel))
  11683. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11684. (when ov (move-overlay ov ovs ove)))
  11685. (when cookie-present
  11686. (run-hook-with-args 'org-after-todo-statistics-hook
  11687. cnt-done (- cnt-all cnt-done))))))
  11688. (run-hooks 'org-todo-statistics-hook)))
  11689. (defvar org-after-todo-statistics-hook nil
  11690. "Hook that is called after a TODO statistics cookie has been updated.
  11691. Each function is called with two arguments: the number of not-done entries
  11692. and the number of done entries.
  11693. For example, the following function, when added to this hook, will switch
  11694. an entry to DONE when all children are done, and back to TODO when new
  11695. entries are set to a TODO status. Note that this hook is only called
  11696. when there is a statistics cookie in the headline!
  11697. (defun org-summary-todo (n-done n-not-done)
  11698. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11699. (let (org-log-done org-log-states) ; turn off logging
  11700. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11701. ")
  11702. (defvar org-todo-statistics-hook nil
  11703. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11704. This hook runs even if there is no statistics cookie present, in which case
  11705. `org-after-todo-statistics-hook' would not run.")
  11706. (defun org-todo-trigger-tag-changes (state)
  11707. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11708. (let ((l org-todo-state-tags-triggers)
  11709. changes)
  11710. (when (or (not state) (equal state ""))
  11711. (setq changes (append changes (cdr (assoc "" l)))))
  11712. (when (and (stringp state) (> (length state) 0))
  11713. (setq changes (append changes (cdr (assoc state l)))))
  11714. (when (member state org-not-done-keywords)
  11715. (setq changes (append changes (cdr (assoc 'todo l)))))
  11716. (when (member state org-done-keywords)
  11717. (setq changes (append changes (cdr (assoc 'done l)))))
  11718. (dolist (c changes)
  11719. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11720. (defun org-local-logging (value)
  11721. "Get logging settings from a property VALUE."
  11722. (let* (words w a)
  11723. ;; directly set the variables, they are already local.
  11724. (setq org-log-done nil
  11725. org-log-repeat nil
  11726. org-todo-log-states nil)
  11727. (setq words (org-split-string value))
  11728. (while (setq w (pop words))
  11729. (cond
  11730. ((setq a (assoc w org-startup-options))
  11731. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11732. (set (nth 1 a) (nth 2 a))))
  11733. ((setq a (org-extract-log-state-settings w))
  11734. (and (member (car a) org-todo-keywords-1)
  11735. (push a org-todo-log-states)))))))
  11736. (defun org-get-todo-sequence-head (kwd)
  11737. "Return the head of the TODO sequence to which KWD belongs.
  11738. If KWD is not set, check if there is a text property remembering the
  11739. right sequence."
  11740. (let (p)
  11741. (cond
  11742. ((not kwd)
  11743. (or (get-text-property (point-at-bol) 'org-todo-head)
  11744. (progn
  11745. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11746. nil (point-at-eol)))
  11747. (get-text-property p 'org-todo-head))))
  11748. ((not (member kwd org-todo-keywords-1))
  11749. (car org-todo-keywords-1))
  11750. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11751. (defun org-fast-todo-selection ()
  11752. "Fast TODO keyword selection with single keys.
  11753. Returns the new TODO keyword, or nil if no state change should occur."
  11754. (let* ((fulltable org-todo-key-alist)
  11755. (done-keywords org-done-keywords) ;; needed for the faces.
  11756. (maxlen (apply 'max (mapcar
  11757. (lambda (x)
  11758. (if (stringp (car x)) (string-width (car x)) 0))
  11759. fulltable)))
  11760. (expert nil)
  11761. (fwidth (+ maxlen 3 1 3))
  11762. (ncol (/ (- (window-width) 4) fwidth))
  11763. tg cnt e c tbl
  11764. groups ingroup)
  11765. (save-excursion
  11766. (save-window-excursion
  11767. (if expert
  11768. (set-buffer (get-buffer-create " *Org todo*"))
  11769. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11770. (erase-buffer)
  11771. (org-set-local 'org-done-keywords done-keywords)
  11772. (setq tbl fulltable cnt 0)
  11773. (while (setq e (pop tbl))
  11774. (cond
  11775. ((equal e '(:startgroup))
  11776. (push '() groups) (setq ingroup t)
  11777. (when (not (= cnt 0))
  11778. (setq cnt 0)
  11779. (insert "\n"))
  11780. (insert "{ "))
  11781. ((equal e '(:endgroup))
  11782. (setq ingroup nil cnt 0)
  11783. (insert "}\n"))
  11784. ((equal e '(:newline))
  11785. (when (not (= cnt 0))
  11786. (setq cnt 0)
  11787. (insert "\n")
  11788. (setq e (car tbl))
  11789. (while (equal (car tbl) '(:newline))
  11790. (insert "\n")
  11791. (setq tbl (cdr tbl)))))
  11792. (t
  11793. (setq tg (car e) c (cdr e))
  11794. (if ingroup (push tg (car groups)))
  11795. (setq tg (org-add-props tg nil 'face
  11796. (org-get-todo-face tg)))
  11797. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11798. (insert "[" c "] " tg (make-string
  11799. (- fwidth 4 (length tg)) ?\ ))
  11800. (when (= (setq cnt (1+ cnt)) ncol)
  11801. (insert "\n")
  11802. (if ingroup (insert " "))
  11803. (setq cnt 0)))))
  11804. (insert "\n")
  11805. (goto-char (point-min))
  11806. (if (not expert) (org-fit-window-to-buffer))
  11807. (message "[a-z..]:Set [SPC]:clear")
  11808. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11809. (cond
  11810. ((or (= c ?\C-g)
  11811. (and (= c ?q) (not (rassoc c fulltable))))
  11812. (setq quit-flag t))
  11813. ((= c ?\ ) nil)
  11814. ((setq e (rassoc c fulltable) tg (car e))
  11815. tg)
  11816. (t (setq quit-flag t)))))))
  11817. (defun org-entry-is-todo-p ()
  11818. (member (org-get-todo-state) org-not-done-keywords))
  11819. (defun org-entry-is-done-p ()
  11820. (member (org-get-todo-state) org-done-keywords))
  11821. (defun org-get-todo-state ()
  11822. "Return the TODO keyword of the current subtree."
  11823. (save-excursion
  11824. (org-back-to-heading t)
  11825. (and (looking-at org-todo-line-regexp)
  11826. (match-end 2)
  11827. (match-string 2))))
  11828. (defun org-at-date-range-p (&optional inactive-ok)
  11829. "Is the cursor inside a date range?"
  11830. (interactive)
  11831. (save-excursion
  11832. (catch 'exit
  11833. (let ((pos (point)))
  11834. (skip-chars-backward "^[<\r\n")
  11835. (skip-chars-backward "<[")
  11836. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11837. (>= (match-end 0) pos)
  11838. (throw 'exit t))
  11839. (skip-chars-backward "^<[\r\n")
  11840. (skip-chars-backward "<[")
  11841. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11842. (>= (match-end 0) pos)
  11843. (throw 'exit t)))
  11844. nil)))
  11845. (defun org-get-repeat (&optional tagline)
  11846. "Check if there is a deadline/schedule with repeater in this entry."
  11847. (save-match-data
  11848. (save-excursion
  11849. (org-back-to-heading t)
  11850. (and (re-search-forward (if tagline
  11851. (concat tagline "\\s-*" org-repeat-re)
  11852. org-repeat-re)
  11853. (org-entry-end-position) t)
  11854. (match-string-no-properties 1)))))
  11855. (defvar org-last-changed-timestamp)
  11856. (defvar org-last-inserted-timestamp)
  11857. (defvar org-log-post-message)
  11858. (defvar org-log-note-purpose)
  11859. (defvar org-log-note-how)
  11860. (defvar org-log-note-extra)
  11861. (defun org-auto-repeat-maybe (done-word)
  11862. "Check if the current headline contains a repeated deadline/schedule.
  11863. If yes, set TODO state back to what it was and change the base date
  11864. of repeating deadline/scheduled time stamps to new date.
  11865. This function is run automatically after each state change to a DONE state."
  11866. ;; last-state is dynamically scoped into this function
  11867. (let* ((repeat (org-get-repeat))
  11868. (aa (assoc org-last-state org-todo-kwd-alist))
  11869. (interpret (nth 1 aa))
  11870. (head (nth 2 aa))
  11871. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11872. (msg "Entry repeats: ")
  11873. (org-log-done nil)
  11874. (org-todo-log-states nil)
  11875. re type n what ts time to-state)
  11876. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11877. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11878. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11879. org-todo-repeat-to-state))
  11880. (unless (and to-state (member to-state org-todo-keywords-1))
  11881. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11882. (org-todo to-state)
  11883. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11884. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11885. (org-time-stamp-format t t))))
  11886. (when org-log-repeat
  11887. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11888. (memq 'org-add-log-note post-command-hook))
  11889. ;; OK, we are already setup for some record
  11890. (if (eq org-log-repeat 'note)
  11891. ;; make sure we take a note, not only a time stamp
  11892. (setq org-log-note-how 'note))
  11893. ;; Set up for taking a record
  11894. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11895. org-last-state
  11896. 'findpos org-log-repeat)))
  11897. (org-back-to-heading t)
  11898. (org-add-planning-info nil nil 'closed)
  11899. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11900. org-deadline-time-regexp "\\)\\|\\("
  11901. org-ts-regexp "\\)"))
  11902. (while (re-search-forward
  11903. re (save-excursion (outline-next-heading) (point)) t)
  11904. (setq type (if (match-end 1) org-scheduled-string
  11905. (if (match-end 3) org-deadline-string "Plain:"))
  11906. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11907. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11908. (setq n (string-to-number (match-string 2 ts))
  11909. what (match-string 3 ts))
  11910. (if (equal what "w") (setq n (* n 7) what "d"))
  11911. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11912. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11913. ;; Preparation, see if we need to modify the start date for the change
  11914. (when (match-end 1)
  11915. (setq time (save-match-data (org-time-string-to-time ts)))
  11916. (cond
  11917. ((equal (match-string 1 ts) ".")
  11918. ;; Shift starting date to today
  11919. (org-timestamp-change
  11920. (- (org-today) (time-to-days time))
  11921. 'day))
  11922. ((equal (match-string 1 ts) "+")
  11923. (let ((nshiftmax 10) (nshift 0))
  11924. (while (or (= nshift 0)
  11925. (<= (time-to-days time)
  11926. (time-to-days (current-time))))
  11927. (when (= (incf nshift) nshiftmax)
  11928. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11929. (user-error "Abort")))
  11930. (org-timestamp-change n (cdr (assoc what whata)))
  11931. (org-at-timestamp-p t)
  11932. (setq ts (match-string 1))
  11933. (setq time (save-match-data (org-time-string-to-time ts)))))
  11934. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11935. ;; rematch, so that we have everything in place for the real shift
  11936. (org-at-timestamp-p t)
  11937. (setq ts (match-string 1))
  11938. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11939. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11940. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11941. (setq org-log-post-message msg)
  11942. (message "%s" msg))))
  11943. (defun org-show-todo-tree (arg)
  11944. "Make a compact tree which shows all headlines marked with TODO.
  11945. The tree will show the lines where the regexp matches, and all higher
  11946. headlines above the match.
  11947. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11948. With a numeric prefix N, construct a sparse tree for the Nth element
  11949. of `org-todo-keywords-1'."
  11950. (interactive "P")
  11951. (let ((case-fold-search nil)
  11952. (kwd-re
  11953. (cond ((null arg) org-not-done-regexp)
  11954. ((equal arg '(4))
  11955. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11956. (mapcar 'list org-todo-keywords-1))))
  11957. (concat "\\("
  11958. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11959. "\\)\\>")))
  11960. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11961. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11962. org-todo-keywords-1)))
  11963. (t (user-error "Invalid prefix argument: %s" arg)))))
  11964. (message "%d TODO entries found"
  11965. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11966. (defun org-deadline (arg &optional time)
  11967. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11968. With one universal prefix argument, remove any deadline from the item.
  11969. With two universal prefix arguments, prompt for a warning delay.
  11970. With argument TIME, set the deadline at the corresponding date. TIME
  11971. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11972. (interactive "P")
  11973. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11974. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11975. 'region-start-level 'region))
  11976. org-loop-over-headlines-in-active-region)
  11977. (org-map-entries
  11978. `(org-deadline ',arg ,time)
  11979. org-loop-over-headlines-in-active-region
  11980. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11981. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11982. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11983. (repeater (and old-date
  11984. (string-match
  11985. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11986. old-date)
  11987. (match-string 1 old-date))))
  11988. (cond
  11989. ((equal arg '(4))
  11990. (when (and old-date org-log-redeadline)
  11991. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11992. org-log-redeadline))
  11993. (org-remove-timestamp-with-keyword org-deadline-string)
  11994. (message "Item no longer has a deadline."))
  11995. ((equal arg '(16))
  11996. (save-excursion
  11997. (org-back-to-heading t)
  11998. (if (re-search-forward
  11999. org-deadline-time-regexp
  12000. (save-excursion (outline-next-heading) (point)) t)
  12001. (let* ((rpl0 (match-string 1))
  12002. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12003. (replace-match
  12004. (concat org-deadline-string
  12005. " <" rpl
  12006. (format " -%dd"
  12007. (abs
  12008. (- (time-to-days
  12009. (save-match-data
  12010. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12011. (time-to-days old-date-time))))
  12012. ">") t t))
  12013. (user-error "No deadline information to update"))))
  12014. (t
  12015. (org-add-planning-info 'deadline time 'closed)
  12016. (when (and old-date org-log-redeadline
  12017. (not (equal old-date
  12018. (substring org-last-inserted-timestamp 1 -1))))
  12019. (org-add-log-setup 'redeadline nil old-date 'findpos
  12020. org-log-redeadline))
  12021. (when repeater
  12022. (save-excursion
  12023. (org-back-to-heading t)
  12024. (when (re-search-forward (concat org-deadline-string " "
  12025. org-last-inserted-timestamp)
  12026. (save-excursion
  12027. (outline-next-heading) (point)) t)
  12028. (goto-char (1- (match-end 0)))
  12029. (insert " " repeater)
  12030. (setq org-last-inserted-timestamp
  12031. (concat (substring org-last-inserted-timestamp 0 -1)
  12032. " " repeater
  12033. (substring org-last-inserted-timestamp -1))))))
  12034. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12035. (defun org-schedule (arg &optional time)
  12036. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12037. With one universal prefix argument, remove any scheduling date from the item.
  12038. With two universal prefix arguments, prompt for a delay cookie.
  12039. With argument TIME, scheduled at the corresponding date. TIME can
  12040. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12041. (interactive "P")
  12042. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12043. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12044. 'region-start-level 'region))
  12045. org-loop-over-headlines-in-active-region)
  12046. (org-map-entries
  12047. `(org-schedule ',arg ,time)
  12048. org-loop-over-headlines-in-active-region
  12049. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12050. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12051. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12052. (repeater (and old-date
  12053. (string-match
  12054. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12055. old-date)
  12056. (match-string 1 old-date))))
  12057. (cond
  12058. ((equal arg '(4))
  12059. (progn
  12060. (when (and old-date org-log-reschedule)
  12061. (org-add-log-setup 'delschedule nil old-date 'findpos
  12062. org-log-reschedule))
  12063. (org-remove-timestamp-with-keyword org-scheduled-string)
  12064. (message "Item is no longer scheduled.")))
  12065. ((equal arg '(16))
  12066. (save-excursion
  12067. (org-back-to-heading t)
  12068. (if (re-search-forward
  12069. org-scheduled-time-regexp
  12070. (save-excursion (outline-next-heading) (point)) t)
  12071. (let* ((rpl0 (match-string 1))
  12072. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12073. (replace-match
  12074. (concat org-scheduled-string
  12075. " <" rpl
  12076. (format " -%dd"
  12077. (abs
  12078. (- (time-to-days
  12079. (save-match-data
  12080. (org-read-date nil t nil "Delay until" old-date-time)))
  12081. (time-to-days old-date-time))))
  12082. ">") t t))
  12083. (user-error "No scheduled information to update"))))
  12084. (t
  12085. (org-add-planning-info 'scheduled time 'closed)
  12086. (when (and old-date org-log-reschedule
  12087. (not (equal old-date
  12088. (substring org-last-inserted-timestamp 1 -1))))
  12089. (org-add-log-setup 'reschedule nil old-date 'findpos
  12090. org-log-reschedule))
  12091. (when repeater
  12092. (save-excursion
  12093. (org-back-to-heading t)
  12094. (when (re-search-forward (concat org-scheduled-string " "
  12095. org-last-inserted-timestamp)
  12096. (save-excursion
  12097. (outline-next-heading) (point)) t)
  12098. (goto-char (1- (match-end 0)))
  12099. (insert " " repeater)
  12100. (setq org-last-inserted-timestamp
  12101. (concat (substring org-last-inserted-timestamp 0 -1)
  12102. " " repeater
  12103. (substring org-last-inserted-timestamp -1))))))
  12104. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12105. (defun org-get-scheduled-time (pom &optional inherit)
  12106. "Get the scheduled time as a time tuple, of a format suitable
  12107. for calling org-schedule with, or if there is no scheduling,
  12108. returns nil."
  12109. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12110. (when time
  12111. (apply 'encode-time (org-parse-time-string time)))))
  12112. (defun org-get-deadline-time (pom &optional inherit)
  12113. "Get the deadline as a time tuple, of a format suitable for
  12114. calling org-deadline with, or if there is no scheduling, returns
  12115. nil."
  12116. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12117. (when time
  12118. (apply 'encode-time (org-parse-time-string time)))))
  12119. (defun org-remove-timestamp-with-keyword (keyword)
  12120. "Remove all time stamps with KEYWORD in the current entry."
  12121. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12122. beg)
  12123. (save-excursion
  12124. (org-back-to-heading t)
  12125. (setq beg (point))
  12126. (outline-next-heading)
  12127. (while (re-search-backward re beg t)
  12128. (replace-match "")
  12129. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12130. (equal (char-before) ?\ ))
  12131. (backward-delete-char 1)
  12132. (if (string-match "^[ \t]*$" (buffer-substring
  12133. (point-at-bol) (point-at-eol)))
  12134. (delete-region (point-at-bol)
  12135. (min (point-max) (1+ (point-at-eol))))))))))
  12136. (defvar org-time-was-given) ; dynamically scoped parameter
  12137. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12138. (defun org-add-planning-info (what &optional time &rest remove)
  12139. "Insert new timestamp with keyword in the line directly after the headline.
  12140. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  12141. If non is given, the user is prompted for a date.
  12142. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  12143. be removed."
  12144. (interactive)
  12145. (let (org-time-was-given org-end-time-was-given ts
  12146. end default-time default-input)
  12147. (catch 'exit
  12148. (when (and (memq what '(scheduled deadline))
  12149. (or (not time)
  12150. (and (stringp time)
  12151. (string-match "^[-+]+[0-9]" time))))
  12152. ;; Try to get a default date/time from existing timestamp
  12153. (save-excursion
  12154. (org-back-to-heading t)
  12155. (setq end (save-excursion (outline-next-heading) (point)))
  12156. (when (re-search-forward (if (eq what 'scheduled)
  12157. org-scheduled-time-regexp
  12158. org-deadline-time-regexp)
  12159. end t)
  12160. (setq ts (match-string 1)
  12161. default-time
  12162. (apply 'encode-time (org-parse-time-string ts))
  12163. default-input (and ts (org-get-compact-tod ts))))))
  12164. (when what
  12165. (setq time
  12166. (if (stringp time)
  12167. ;; This is a string (relative or absolute), set proper date
  12168. (apply 'encode-time
  12169. (org-read-date-analyze
  12170. time default-time (decode-time default-time)))
  12171. ;; If necessary, get the time from the user
  12172. (or time (org-read-date nil 'to-time nil nil
  12173. default-time default-input)))))
  12174. (when (and org-insert-labeled-timestamps-at-point
  12175. (member what '(scheduled deadline)))
  12176. (insert
  12177. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  12178. (org-insert-time-stamp time org-time-was-given
  12179. nil nil nil (list org-end-time-was-given))
  12180. (setq what nil))
  12181. (save-excursion
  12182. (save-restriction
  12183. (let (col list elt ts buffer-invisibility-spec)
  12184. (org-back-to-heading t)
  12185. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  12186. (goto-char (match-end 1))
  12187. (setq col (current-column))
  12188. (goto-char (match-end 0))
  12189. (if (eobp) (insert "\n") (forward-char 1))
  12190. (when (and (not what)
  12191. (not (looking-at
  12192. (concat "[ \t]*"
  12193. org-keyword-time-not-clock-regexp))))
  12194. ;; Nothing to add, nothing to remove...... :-)
  12195. (throw 'exit nil))
  12196. (if (and (not (looking-at org-outline-regexp))
  12197. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  12198. "[^\r\n]*"))
  12199. (not (equal (match-string 1) org-clock-string)))
  12200. (narrow-to-region (match-beginning 0) (match-end 0))
  12201. (insert-before-markers "\n")
  12202. (backward-char 1)
  12203. (narrow-to-region (point) (point))
  12204. (and org-adapt-indentation (org-indent-to-column col)))
  12205. ;; Check if we have to remove something.
  12206. (setq list (cons what remove))
  12207. (while list
  12208. (setq elt (pop list))
  12209. (when (or (and (eq elt 'scheduled)
  12210. (re-search-forward org-scheduled-time-regexp nil t))
  12211. (and (eq elt 'deadline)
  12212. (re-search-forward org-deadline-time-regexp nil t))
  12213. (and (eq elt 'closed)
  12214. (re-search-forward org-closed-time-regexp nil t)))
  12215. (replace-match "")
  12216. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  12217. (and (looking-at "[ \t]+") (replace-match ""))
  12218. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  12219. (when what
  12220. (insert
  12221. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  12222. (cond ((eq what 'scheduled) org-scheduled-string)
  12223. ((eq what 'deadline) org-deadline-string)
  12224. ((eq what 'closed) org-closed-string))
  12225. " ")
  12226. (setq ts (org-insert-time-stamp
  12227. time
  12228. (or org-time-was-given
  12229. (and (eq what 'closed) org-log-done-with-time))
  12230. (eq what 'closed)
  12231. nil nil (list org-end-time-was-given)))
  12232. (insert
  12233. (if (not (or (bolp) (eq (char-before) ?\ )
  12234. (memq (char-after) '(32 10))
  12235. (eobp))) " " ""))
  12236. (end-of-line 1))
  12237. (goto-char (point-min))
  12238. (widen)
  12239. (if (and (looking-at "[ \t]*\n")
  12240. (equal (char-before) ?\n))
  12241. (delete-region (1- (point)) (point-at-eol)))
  12242. ts))))))
  12243. (defvar org-log-note-marker (make-marker))
  12244. (defvar org-log-note-purpose nil)
  12245. (defvar org-log-note-state nil)
  12246. (defvar org-log-note-previous-state nil)
  12247. (defvar org-log-note-how nil)
  12248. (defvar org-log-note-extra nil)
  12249. (defvar org-log-note-window-configuration nil)
  12250. (defvar org-log-note-return-to (make-marker))
  12251. (defvar org-log-note-effective-time nil
  12252. "Remembered current time so that dynamically scoped
  12253. `org-extend-today-until' affects tha timestamps in state change
  12254. log")
  12255. (defvar org-log-post-message nil
  12256. "Message to be displayed after a log note has been stored.
  12257. The auto-repeater uses this.")
  12258. (defun org-add-note ()
  12259. "Add a note to the current entry.
  12260. This is done in the same way as adding a state change note."
  12261. (interactive)
  12262. (org-add-log-setup 'note nil nil 'findpos nil))
  12263. (defvar org-property-end-re)
  12264. (defun org-add-log-setup (&optional purpose state prev-state
  12265. findpos how extra)
  12266. "Set up the post command hook to take a note.
  12267. If this is about to TODO state change, the new state is expected in STATE.
  12268. When FINDPOS is non-nil, find the correct position for the note in
  12269. the current entry. If not, assume that it can be inserted at point.
  12270. HOW is an indicator what kind of note should be created.
  12271. EXTRA is additional text that will be inserted into the notes buffer."
  12272. (let* ((org-log-into-drawer (org-log-into-drawer))
  12273. (drawer (cond ((stringp org-log-into-drawer)
  12274. org-log-into-drawer)
  12275. (org-log-into-drawer "LOGBOOK"))))
  12276. (save-restriction
  12277. (save-excursion
  12278. (when findpos
  12279. (org-back-to-heading t)
  12280. (narrow-to-region (point) (save-excursion
  12281. (outline-next-heading) (point)))
  12282. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12283. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12284. "[^\r\n]*\\)?"))
  12285. (goto-char (match-end 0))
  12286. (cond
  12287. (drawer
  12288. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12289. nil t)
  12290. (progn
  12291. (goto-char (match-end 0))
  12292. (or org-log-states-order-reversed
  12293. (and (re-search-forward org-property-end-re nil t)
  12294. (goto-char (1- (match-beginning 0))))))
  12295. (insert "\n:" drawer ":\n:END:")
  12296. (beginning-of-line 0)
  12297. (org-indent-line)
  12298. (beginning-of-line 2)
  12299. (org-indent-line)
  12300. (end-of-line 0)))
  12301. ((and org-log-state-notes-insert-after-drawers
  12302. (save-excursion
  12303. (forward-line) (looking-at org-drawer-regexp)))
  12304. (forward-line)
  12305. (while (looking-at org-drawer-regexp)
  12306. (goto-char (match-end 0))
  12307. (re-search-forward org-property-end-re (point-max) t)
  12308. (forward-line))
  12309. (forward-line -1)))
  12310. (unless org-log-states-order-reversed
  12311. (and (= (char-after) ?\n) (forward-char 1))
  12312. (org-skip-over-state-notes)
  12313. (skip-chars-backward " \t\n\r")))
  12314. (move-marker org-log-note-marker (point))
  12315. (setq org-log-note-purpose purpose
  12316. org-log-note-state state
  12317. org-log-note-previous-state prev-state
  12318. org-log-note-how how
  12319. org-log-note-extra extra
  12320. org-log-note-effective-time (org-current-effective-time))
  12321. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12322. (defun org-skip-over-state-notes ()
  12323. "Skip past the list of State notes in an entry."
  12324. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12325. (when (ignore-errors (goto-char (org-in-item-p)))
  12326. (let* ((struct (org-list-struct))
  12327. (prevs (org-list-prevs-alist struct)))
  12328. (while (looking-at "[ \t]*- State")
  12329. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12330. (org-list-get-item-end (point) struct)))))))
  12331. (defun org-add-log-note (&optional purpose)
  12332. "Pop up a window for taking a note, and add this note later at point."
  12333. (remove-hook 'post-command-hook 'org-add-log-note)
  12334. (setq org-log-note-window-configuration (current-window-configuration))
  12335. (delete-other-windows)
  12336. (move-marker org-log-note-return-to (point))
  12337. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12338. (goto-char org-log-note-marker)
  12339. (org-switch-to-buffer-other-window "*Org Note*")
  12340. (erase-buffer)
  12341. (if (memq org-log-note-how '(time state))
  12342. (let (current-prefix-arg) (org-store-log-note))
  12343. (let ((org-inhibit-startup t)) (org-mode))
  12344. (insert (format "# Insert note for %s.
  12345. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12346. (cond
  12347. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12348. ((eq org-log-note-purpose 'done) "closed todo item")
  12349. ((eq org-log-note-purpose 'state)
  12350. (format "state change from \"%s\" to \"%s\""
  12351. (or org-log-note-previous-state "")
  12352. (or org-log-note-state "")))
  12353. ((eq org-log-note-purpose 'reschedule)
  12354. "rescheduling")
  12355. ((eq org-log-note-purpose 'delschedule)
  12356. "no longer scheduled")
  12357. ((eq org-log-note-purpose 'redeadline)
  12358. "changing deadline")
  12359. ((eq org-log-note-purpose 'deldeadline)
  12360. "removing deadline")
  12361. ((eq org-log-note-purpose 'refile)
  12362. "refiling")
  12363. ((eq org-log-note-purpose 'note)
  12364. "this entry")
  12365. (t (error "This should not happen")))))
  12366. (if org-log-note-extra (insert org-log-note-extra))
  12367. (org-set-local 'org-finish-function 'org-store-log-note)
  12368. (run-hooks 'org-log-buffer-setup-hook)))
  12369. (defvar org-note-abort nil) ; dynamically scoped
  12370. (defun org-store-log-note ()
  12371. "Finish taking a log note, and insert it to where it belongs."
  12372. (let ((txt (buffer-string)))
  12373. (kill-buffer (current-buffer))
  12374. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12375. lines ind bul)
  12376. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12377. (setq txt (replace-match "" t t txt)))
  12378. (if (string-match "\\s-+\\'" txt)
  12379. (setq txt (replace-match "" t t txt)))
  12380. (setq lines (org-split-string txt "\n"))
  12381. (when (and note (string-match "\\S-" note))
  12382. (setq note
  12383. (org-replace-escapes
  12384. note
  12385. (list (cons "%u" (user-login-name))
  12386. (cons "%U" user-full-name)
  12387. (cons "%t" (format-time-string
  12388. (org-time-stamp-format 'long 'inactive)
  12389. org-log-note-effective-time))
  12390. (cons "%T" (format-time-string
  12391. (org-time-stamp-format 'long nil)
  12392. org-log-note-effective-time))
  12393. (cons "%d" (format-time-string
  12394. (org-time-stamp-format nil 'inactive)
  12395. org-log-note-effective-time))
  12396. (cons "%D" (format-time-string
  12397. (org-time-stamp-format nil nil)
  12398. org-log-note-effective-time))
  12399. (cons "%s" (if org-log-note-state
  12400. (concat "\"" org-log-note-state "\"")
  12401. ""))
  12402. (cons "%S" (if org-log-note-previous-state
  12403. (concat "\"" org-log-note-previous-state "\"")
  12404. "\"\"")))))
  12405. (if lines (setq note (concat note " \\\\")))
  12406. (push note lines))
  12407. (when (or current-prefix-arg org-note-abort)
  12408. (when org-log-into-drawer
  12409. (org-remove-empty-drawer-at org-log-note-marker))
  12410. (setq lines nil))
  12411. (when lines
  12412. (with-current-buffer (marker-buffer org-log-note-marker)
  12413. (save-excursion
  12414. (goto-char org-log-note-marker)
  12415. (move-marker org-log-note-marker nil)
  12416. (end-of-line 1)
  12417. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12418. (setq ind (save-excursion
  12419. (if (ignore-errors (goto-char (org-in-item-p)))
  12420. (let ((struct (org-list-struct)))
  12421. (org-list-get-ind
  12422. (org-list-get-top-point struct) struct))
  12423. (skip-chars-backward " \r\t\n")
  12424. (cond
  12425. ((and (org-at-heading-p)
  12426. org-adapt-indentation)
  12427. (1+ (org-current-level)))
  12428. ((org-at-heading-p) 0)
  12429. (t (org-get-indentation))))))
  12430. (setq bul (org-list-bullet-string "-"))
  12431. (org-indent-line-to ind)
  12432. (insert bul (pop lines))
  12433. (let ((ind-body (+ (length bul) ind)))
  12434. (while lines
  12435. (insert "\n")
  12436. (org-indent-line-to ind-body)
  12437. (insert (pop lines))))
  12438. (message "Note stored")
  12439. (org-back-to-heading t)
  12440. (org-cycle-hide-drawers 'children))
  12441. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12442. ;; from within `org-add-log-note' because `buffer-undo-list'
  12443. ;; is then modified outside of `org-with-remote-undo'.
  12444. (when (eq this-command 'org-agenda-todo)
  12445. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12446. ;; Don't add undo information when called from `org-agenda-todo'
  12447. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12448. (set-window-configuration org-log-note-window-configuration)
  12449. (with-current-buffer (marker-buffer org-log-note-return-to)
  12450. (goto-char org-log-note-return-to))
  12451. (move-marker org-log-note-return-to nil)
  12452. (and org-log-post-message (message "%s" org-log-post-message))))
  12453. (defun org-remove-empty-drawer-at (pos)
  12454. "Remove an empty drawer at position POS.
  12455. POS may also be a marker."
  12456. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12457. (org-with-wide-buffer
  12458. (goto-char pos)
  12459. (let ((drawer (org-element-at-point)))
  12460. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12461. (not (org-element-property :contents-begin drawer)))
  12462. (delete-region (org-element-property :begin drawer)
  12463. (progn (goto-char (org-element-property :end drawer))
  12464. (skip-chars-backward " \r\t\n")
  12465. (forward-line)
  12466. (point))))))))
  12467. (defvar org-ts-type nil)
  12468. (defun org-sparse-tree (&optional arg type)
  12469. "Create a sparse tree, prompt for the details.
  12470. This command can create sparse trees. You first need to select the type
  12471. of match used to create the tree:
  12472. t Show all TODO entries.
  12473. T Show entries with a specific TODO keyword.
  12474. m Show entries selected by a tags/property match.
  12475. p Enter a property name and its value (both with completion on existing
  12476. names/values) and show entries with that property.
  12477. r Show entries matching a regular expression (`/' can be used as well).
  12478. b Show deadlines and scheduled items before a date.
  12479. a Show deadlines and scheduled items after a date.
  12480. d Show deadlines due within `org-deadline-warning-days'.
  12481. D Show deadlines and scheduled items between a date range."
  12482. (interactive "P")
  12483. (setq type (or type org-sparse-tree-default-date-type))
  12484. (setq org-ts-type type)
  12485. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12486. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12487. [c]ycle through date types: %s"
  12488. (case type
  12489. (all "all timestamps")
  12490. (scheduled "only scheduled")
  12491. (deadline "only deadline")
  12492. (active "only active timestamps")
  12493. (inactive "only inactive timestamps")
  12494. (scheduled-or-deadline "scheduled/deadline")
  12495. (closed "with a closed time-stamp")
  12496. (otherwise "scheduled/deadline")))
  12497. (let ((answer (read-char-exclusive)))
  12498. (case answer
  12499. (?c
  12500. (org-sparse-tree
  12501. arg
  12502. (cadr (memq type '(scheduled-or-deadline all scheduled deadline active
  12503. inactive closed)))))
  12504. (?d (call-interactively #'org-check-deadlines))
  12505. (?b (call-interactively #'org-check-before-date))
  12506. (?a (call-interactively #'org-check-after-date))
  12507. (?D (call-interactively #'org-check-dates-range))
  12508. (?t (call-interactively #'org-show-todo-tree))
  12509. (?T (org-show-todo-tree '(4)))
  12510. (?m (call-interactively #'org-match-sparse-tree))
  12511. ((?p ?P)
  12512. (let* ((kwd (org-icompleting-read
  12513. "Property: " (mapcar #'list (org-buffer-property-keys))))
  12514. (value (org-icompleting-read
  12515. "Value: " (mapcar #'list (org-property-values kwd)))))
  12516. (unless (string-match "\\`{.*}\\'" value)
  12517. (setq value (concat "\"" value "\"")))
  12518. (org-match-sparse-tree arg (concat kwd "=" value))))
  12519. ((?r ?R ?/) (call-interactively #'org-occur))
  12520. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12521. (defvar org-occur-highlights nil
  12522. "List of overlays used for occur matches.")
  12523. (make-variable-buffer-local 'org-occur-highlights)
  12524. (defvar org-occur-parameters nil
  12525. "Parameters of the active org-occur calls.
  12526. This is a list, each call to org-occur pushes as cons cell,
  12527. containing the regular expression and the callback, onto the list.
  12528. The list can contain several entries if `org-occur' has been called
  12529. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12530. will only contain one set of parameters. When the highlights are
  12531. removed (for example with `C-c C-c', or with the next edit (depending
  12532. on `org-remove-highlights-with-change'), this variable is emptied
  12533. as well.")
  12534. (make-variable-buffer-local 'org-occur-parameters)
  12535. (defun org-occur (regexp &optional keep-previous callback)
  12536. "Make a compact tree which shows all matches of REGEXP.
  12537. The tree will show the lines where the regexp matches, and all higher
  12538. headlines above the match. It will also show the heading after the match,
  12539. to make sure editing the matching entry is easy.
  12540. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12541. call to `org-occur' will be kept, to allow stacking of calls to this
  12542. command.
  12543. If CALLBACK is non-nil, it is a function which is called to confirm
  12544. that the match should indeed be shown."
  12545. (interactive "sRegexp: \nP")
  12546. (when (equal regexp "")
  12547. (user-error "Regexp cannot be empty"))
  12548. (unless keep-previous
  12549. (org-remove-occur-highlights nil nil t))
  12550. (push (cons regexp callback) org-occur-parameters)
  12551. (let ((cnt 0))
  12552. (save-excursion
  12553. (goto-char (point-min))
  12554. (if (or (not keep-previous) ; do not want to keep
  12555. (not org-occur-highlights)) ; no previous matches
  12556. ;; hide everything
  12557. (org-overview))
  12558. (while (re-search-forward regexp nil t)
  12559. (when (or (not callback)
  12560. (save-match-data (funcall callback)))
  12561. (setq cnt (1+ cnt))
  12562. (when org-highlight-sparse-tree-matches
  12563. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12564. (org-show-context 'occur-tree))))
  12565. (when org-remove-highlights-with-change
  12566. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12567. nil 'local))
  12568. (unless org-sparse-tree-open-archived-trees
  12569. (org-hide-archived-subtrees (point-min) (point-max)))
  12570. (run-hooks 'org-occur-hook)
  12571. (if (org-called-interactively-p 'interactive)
  12572. (message "%d match(es) for regexp %s" cnt regexp))
  12573. cnt))
  12574. (defun org-occur-next-match (&optional n reset)
  12575. "Function for `next-error-function' to find sparse tree matches.
  12576. N is the number of matches to move, when negative move backwards.
  12577. RESET is entirely ignored - this function always goes back to the
  12578. starting point when no match is found."
  12579. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12580. (search-func (if (< n 0)
  12581. 'previous-single-char-property-change
  12582. 'next-single-char-property-change))
  12583. (n (abs n))
  12584. (pos (point))
  12585. p1)
  12586. (catch 'exit
  12587. (while (setq p1 (funcall search-func (point) 'org-type))
  12588. (when (equal p1 limit)
  12589. (goto-char pos)
  12590. (user-error "No more matches"))
  12591. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12592. (setq n (1- n))
  12593. (when (= n 0)
  12594. (goto-char p1)
  12595. (throw 'exit (point))))
  12596. (goto-char p1))
  12597. (goto-char p1)
  12598. (user-error "No more matches"))))
  12599. (defun org-show-context (&optional key)
  12600. "Make sure point and context are visible.
  12601. How much context is shown depends upon the variables
  12602. `org-show-hierarchy-above', `org-show-following-heading',
  12603. `org-show-entry-below' and `org-show-siblings'."
  12604. (let ((heading-p (org-at-heading-p t))
  12605. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12606. (following-p (org-get-alist-option org-show-following-heading key))
  12607. (entry-p (org-get-alist-option org-show-entry-below key))
  12608. (siblings-p (org-get-alist-option org-show-siblings key)))
  12609. ;; Show heading or entry text
  12610. (if (and heading-p (not entry-p))
  12611. (org-flag-heading nil) ; only show the heading
  12612. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12613. (org-show-hidden-entry))) ; show entire entry
  12614. (when following-p
  12615. ;; Show next sibling, or heading below text
  12616. (save-excursion
  12617. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12618. (org-flag-heading nil))))
  12619. (when siblings-p (org-show-siblings))
  12620. (when hierarchy-p
  12621. ;; show all higher headings, possibly with siblings
  12622. (save-excursion
  12623. (while (and (condition-case nil
  12624. (progn (org-up-heading-all 1) t)
  12625. (error nil))
  12626. (not (bobp)))
  12627. (org-flag-heading nil)
  12628. (when siblings-p (org-show-siblings)))))
  12629. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12630. (defvar org-reveal-start-hook nil
  12631. "Hook run before revealing a location.")
  12632. (defun org-reveal (&optional siblings)
  12633. "Show current entry, hierarchy above it, and the following headline.
  12634. This can be used to show a consistent set of context around locations
  12635. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12636. not t for the search context.
  12637. With optional argument SIBLINGS, on each level of the hierarchy all
  12638. siblings are shown. This repairs the tree structure to what it would
  12639. look like when opened with hierarchical calls to `org-cycle'.
  12640. With double optional argument \\[universal-argument] \\[universal-argument], \
  12641. go to the parent and show the
  12642. entire tree."
  12643. (interactive "P")
  12644. (run-hooks 'org-reveal-start-hook)
  12645. (let ((org-show-hierarchy-above t)
  12646. (org-show-following-heading t)
  12647. (org-show-siblings (if siblings t org-show-siblings)))
  12648. (org-show-context nil))
  12649. (when (equal siblings '(16))
  12650. (save-excursion
  12651. (when (org-up-heading-safe)
  12652. (org-show-subtree)
  12653. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12654. (defun org-highlight-new-match (beg end)
  12655. "Highlight from BEG to END and mark the highlight is an occur headline."
  12656. (let ((ov (make-overlay beg end)))
  12657. (overlay-put ov 'face 'secondary-selection)
  12658. (overlay-put ov 'org-type 'org-occur)
  12659. (push ov org-occur-highlights)))
  12660. (defun org-remove-occur-highlights (&optional beg end noremove)
  12661. "Remove the occur highlights from the buffer.
  12662. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12663. from the `before-change-functions' in the current buffer."
  12664. (interactive)
  12665. (unless org-inhibit-highlight-removal
  12666. (mapc 'delete-overlay org-occur-highlights)
  12667. (setq org-occur-highlights nil)
  12668. (setq org-occur-parameters nil)
  12669. (unless noremove
  12670. (remove-hook 'before-change-functions
  12671. 'org-remove-occur-highlights 'local))))
  12672. ;;;; Priorities
  12673. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12674. "Regular expression matching the priority indicator.")
  12675. (defvar org-remove-priority-next-time nil)
  12676. (defun org-priority-up ()
  12677. "Increase the priority of the current item."
  12678. (interactive)
  12679. (org-priority 'up))
  12680. (defun org-priority-down ()
  12681. "Decrease the priority of the current item."
  12682. (interactive)
  12683. (org-priority 'down))
  12684. (defun org-priority (&optional action show)
  12685. "Change the priority of an item.
  12686. ACTION can be `set', `up', `down', or a character."
  12687. (interactive "P")
  12688. (if (equal action '(4))
  12689. (org-show-priority)
  12690. (unless org-enable-priority-commands
  12691. (user-error "Priority commands are disabled"))
  12692. (setq action (or action 'set))
  12693. (let (current new news have remove)
  12694. (save-excursion
  12695. (org-back-to-heading t)
  12696. (if (looking-at org-priority-regexp)
  12697. (setq current (string-to-char (match-string 2))
  12698. have t))
  12699. (cond
  12700. ((eq action 'remove)
  12701. (setq remove t new ?\ ))
  12702. ((or (eq action 'set)
  12703. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12704. (if (not (eq action 'set))
  12705. (setq new action)
  12706. (message "Priority %c-%c, SPC to remove: "
  12707. org-highest-priority org-lowest-priority)
  12708. (save-match-data
  12709. (setq new (read-char-exclusive))))
  12710. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12711. (= (upcase org-lowest-priority) org-lowest-priority))
  12712. (setq new (upcase new)))
  12713. (cond ((equal new ?\ ) (setq remove t))
  12714. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12715. (user-error "Priority must be between `%c' and `%c'"
  12716. org-highest-priority org-lowest-priority))))
  12717. ((eq action 'up)
  12718. (setq new (if have
  12719. (1- current) ; normal cycling
  12720. ;; last priority was empty
  12721. (if (eq last-command this-command)
  12722. org-lowest-priority ; wrap around empty to lowest
  12723. ;; default
  12724. (if org-priority-start-cycle-with-default
  12725. org-default-priority
  12726. (1- org-default-priority))))))
  12727. ((eq action 'down)
  12728. (setq new (if have
  12729. (1+ current) ; normal cycling
  12730. ;; last priority was empty
  12731. (if (eq last-command this-command)
  12732. org-highest-priority ; wrap around empty to highest
  12733. ;; default
  12734. (if org-priority-start-cycle-with-default
  12735. org-default-priority
  12736. (1+ org-default-priority))))))
  12737. (t (user-error "Invalid action")))
  12738. (if (or (< (upcase new) org-highest-priority)
  12739. (> (upcase new) org-lowest-priority))
  12740. (if (and (memq action '(up down))
  12741. (not have) (not (eq last-command this-command)))
  12742. ;; `new' is from default priority
  12743. (error
  12744. "The default can not be set, see `org-default-priority' why")
  12745. ;; normal cycling: `new' is beyond highest/lowest priority
  12746. ;; and is wrapped around to the empty priority
  12747. (setq remove t)))
  12748. (setq news (format "%c" new))
  12749. (if have
  12750. (if remove
  12751. (replace-match "" t t nil 1)
  12752. (replace-match news t t nil 2))
  12753. (if remove
  12754. (user-error "No priority cookie found in line")
  12755. (let ((case-fold-search nil))
  12756. (looking-at org-todo-line-regexp))
  12757. (if (match-end 2)
  12758. (progn
  12759. (goto-char (match-end 2))
  12760. (insert " [#" news "]"))
  12761. (goto-char (match-beginning 3))
  12762. (insert "[#" news "] "))))
  12763. (org-set-tags nil 'align))
  12764. (if remove
  12765. (message "Priority removed")
  12766. (message "Priority of current item set to %s" news)))))
  12767. (defun org-show-priority ()
  12768. "Show the priority of the current item.
  12769. This priority is composed of the main priority given with the [#A] cookies,
  12770. and by additional input from the age of a schedules or deadline entry."
  12771. (interactive)
  12772. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12773. (org-get-at-bol 'priority)
  12774. (save-excursion
  12775. (save-match-data
  12776. (beginning-of-line)
  12777. (and (looking-at org-heading-regexp)
  12778. (org-get-priority (match-string 0))))))))
  12779. (message "Priority is %d" (if pri pri -1000))))
  12780. (defun org-get-priority (s)
  12781. "Find priority cookie and return priority."
  12782. (save-match-data
  12783. (if (functionp org-get-priority-function)
  12784. (funcall org-get-priority-function)
  12785. (if (not (string-match org-priority-regexp s))
  12786. (* 1000 (- org-lowest-priority org-default-priority))
  12787. (* 1000 (- org-lowest-priority
  12788. (string-to-char (match-string 2 s))))))))
  12789. ;;;; Tags
  12790. (defvar org-agenda-archives-mode)
  12791. (defvar org-map-continue-from nil
  12792. "Position from where mapping should continue.
  12793. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12794. (defvar org-scanner-tags nil
  12795. "The current tag list while the tags scanner is running.")
  12796. (defvar org-trust-scanner-tags nil
  12797. "Should `org-get-tags-at' use the tags for the scanner.
  12798. This is for internal dynamical scoping only.
  12799. When this is non-nil, the function `org-get-tags-at' will return the value
  12800. of `org-scanner-tags' instead of building the list by itself. This
  12801. can lead to large speed-ups when the tags scanner is used in a file with
  12802. many entries, and when the list of tags is retrieved, for example to
  12803. obtain a list of properties. Building the tags list for each entry in such
  12804. a file becomes an N^2 operation - but with this variable set, it scales
  12805. as N.")
  12806. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12807. "Sca headline tags with inheritance and produce output ACTION.
  12808. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12809. or `agenda' to produce an entry list for an agenda view. It can also be
  12810. a Lisp form or a function that should be called at each matched headline, in
  12811. this case the return value is a list of all return values from these calls.
  12812. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12813. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12814. only lines with a not-done TODO keyword are included in the output.
  12815. This should be the same variable that was scoped into
  12816. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12817. START-LEVEL can be a string with asterisks, reducing the scope to
  12818. headlines matching this string."
  12819. (require 'org-agenda)
  12820. (let* ((re (concat "^"
  12821. (if start-level
  12822. ;; Get the correct level to match
  12823. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12824. org-outline-regexp)
  12825. " *\\(\\<\\("
  12826. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12827. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12828. (props (list 'face 'default
  12829. 'done-face 'org-agenda-done
  12830. 'undone-face 'default
  12831. 'mouse-face 'highlight
  12832. 'org-not-done-regexp org-not-done-regexp
  12833. 'org-todo-regexp org-todo-regexp
  12834. 'org-complex-heading-regexp org-complex-heading-regexp
  12835. 'help-echo
  12836. (format "mouse-2 or RET jump to org file %s"
  12837. (abbreviate-file-name
  12838. (or (buffer-file-name (buffer-base-buffer))
  12839. (buffer-name (buffer-base-buffer)))))))
  12840. (org-map-continue-from nil)
  12841. lspos tags tags-list
  12842. (tags-alist (list (cons 0 org-file-tags)))
  12843. (llast 0) rtn rtn1 level category i txt
  12844. todo marker entry priority)
  12845. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12846. (setq action (list 'lambda nil action)))
  12847. (save-excursion
  12848. (goto-char (point-min))
  12849. (when (eq action 'sparse-tree)
  12850. (org-overview)
  12851. (org-remove-occur-highlights))
  12852. (while (let (case-fold-search)
  12853. (re-search-forward re nil t))
  12854. (setq org-map-continue-from nil)
  12855. (catch :skip
  12856. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12857. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12858. (goto-char (setq lspos (match-beginning 0)))
  12859. (setq level (org-reduced-level (org-outline-level))
  12860. category (org-get-category))
  12861. (setq i llast llast level)
  12862. ;; remove tag lists from same and sublevels
  12863. (while (>= i level)
  12864. (when (setq entry (assoc i tags-alist))
  12865. (setq tags-alist (delete entry tags-alist)))
  12866. (setq i (1- i)))
  12867. ;; add the next tags
  12868. (when tags
  12869. (setq tags (org-split-string tags ":")
  12870. tags-alist
  12871. (cons (cons level tags) tags-alist)))
  12872. ;; compile tags for current headline
  12873. (setq tags-list
  12874. (if org-use-tag-inheritance
  12875. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12876. tags)
  12877. org-scanner-tags tags-list)
  12878. (when org-use-tag-inheritance
  12879. (setcdr (car tags-alist)
  12880. (mapcar (lambda (x)
  12881. (setq x (copy-sequence x))
  12882. (org-add-prop-inherited x))
  12883. (cdar tags-alist))))
  12884. (when (and tags org-use-tag-inheritance
  12885. (or (not (eq t org-use-tag-inheritance))
  12886. org-tags-exclude-from-inheritance))
  12887. ;; selective inheritance, remove uninherited ones
  12888. (setcdr (car tags-alist)
  12889. (org-remove-uninherited-tags (cdar tags-alist))))
  12890. (when (and
  12891. ;; eval matcher only when the todo condition is OK
  12892. (and (or (not todo-only) (member todo org-not-done-keywords))
  12893. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12894. (eval matcher)))
  12895. ;; Call the skipper, but return t if it does not skip,
  12896. ;; so that the `and' form continues evaluating
  12897. (progn
  12898. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12899. t)
  12900. ;; Check if timestamps are deselecting this entry
  12901. (or (not todo-only)
  12902. (and (member todo org-not-done-keywords)
  12903. (or (not org-agenda-tags-todo-honor-ignore-options)
  12904. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12905. ;; select this headline
  12906. (cond
  12907. ((eq action 'sparse-tree)
  12908. (and org-highlight-sparse-tree-matches
  12909. (org-get-heading) (match-end 0)
  12910. (org-highlight-new-match
  12911. (match-beginning 1) (match-end 1)))
  12912. (org-show-context 'tags-tree))
  12913. ((eq action 'agenda)
  12914. (setq txt (org-agenda-format-item
  12915. ""
  12916. (concat
  12917. (if (eq org-tags-match-list-sublevels 'indented)
  12918. (make-string (1- level) ?.) "")
  12919. (org-get-heading))
  12920. level category
  12921. tags-list)
  12922. priority (org-get-priority txt))
  12923. (goto-char lspos)
  12924. (setq marker (org-agenda-new-marker))
  12925. (org-add-props txt props
  12926. 'org-marker marker 'org-hd-marker marker 'org-category category
  12927. 'todo-state todo
  12928. 'priority priority 'type "tagsmatch")
  12929. (push txt rtn))
  12930. ((functionp action)
  12931. (setq org-map-continue-from nil)
  12932. (save-excursion
  12933. (setq rtn1 (funcall action))
  12934. (push rtn1 rtn)))
  12935. (t (user-error "Invalid action")))
  12936. ;; if we are to skip sublevels, jump to end of subtree
  12937. (unless org-tags-match-list-sublevels
  12938. (org-end-of-subtree t)
  12939. (backward-char 1))))
  12940. ;; Get the correct position from where to continue
  12941. (if org-map-continue-from
  12942. (goto-char org-map-continue-from)
  12943. (and (= (point) lspos) (end-of-line 1)))))
  12944. (when (and (eq action 'sparse-tree)
  12945. (not org-sparse-tree-open-archived-trees))
  12946. (org-hide-archived-subtrees (point-min) (point-max)))
  12947. (nreverse rtn)))
  12948. (defun org-remove-uninherited-tags (tags)
  12949. "Remove all tags that are not inherited from the list TAGS."
  12950. (cond
  12951. ((eq org-use-tag-inheritance t)
  12952. (if org-tags-exclude-from-inheritance
  12953. (org-delete-all org-tags-exclude-from-inheritance tags)
  12954. tags))
  12955. ((not org-use-tag-inheritance) nil)
  12956. ((stringp org-use-tag-inheritance)
  12957. (delq nil (mapcar
  12958. (lambda (x)
  12959. (if (and (string-match org-use-tag-inheritance x)
  12960. (not (member x org-tags-exclude-from-inheritance)))
  12961. x nil))
  12962. tags)))
  12963. ((listp org-use-tag-inheritance)
  12964. (delq nil (mapcar
  12965. (lambda (x)
  12966. (if (member x org-use-tag-inheritance) x nil))
  12967. tags)))))
  12968. (defun org-match-sparse-tree (&optional todo-only match)
  12969. "Create a sparse tree according to tags string MATCH.
  12970. MATCH can contain positive and negative selection of tags, like
  12971. \"+WORK+URGENT-WITHBOSS\".
  12972. If optional argument TODO-ONLY is non-nil, only select lines that are
  12973. also TODO lines."
  12974. (interactive "P")
  12975. (org-agenda-prepare-buffers (list (current-buffer)))
  12976. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12977. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12978. (defvar org-cached-props nil)
  12979. (defun org-cached-entry-get (pom property)
  12980. (if (or (eq t org-use-property-inheritance)
  12981. (and (stringp org-use-property-inheritance)
  12982. (string-match org-use-property-inheritance property))
  12983. (and (listp org-use-property-inheritance)
  12984. (member property org-use-property-inheritance)))
  12985. ;; Caching is not possible, check it directly
  12986. (org-entry-get pom property 'inherit)
  12987. ;; Get all properties, so that we can do complicated checks easily
  12988. (cdr (assoc property (or org-cached-props
  12989. (setq org-cached-props
  12990. (org-entry-properties pom)))))))
  12991. (defun org-global-tags-completion-table (&optional files)
  12992. "Return the list of all tags in all agenda buffer/files.
  12993. Optional FILES argument is a list of files which can be used
  12994. instead of the agenda files."
  12995. (save-excursion
  12996. (org-uniquify
  12997. (delq nil
  12998. (apply 'append
  12999. (mapcar
  13000. (lambda (file)
  13001. (set-buffer (find-file-noselect file))
  13002. (append (org-get-buffer-tags)
  13003. (mapcar (lambda (x) (if (stringp (car-safe x))
  13004. (list (car-safe x)) nil))
  13005. org-tag-alist)))
  13006. (if (and files (car files))
  13007. files
  13008. (org-agenda-files))))))))
  13009. (defun org-make-tags-matcher (match)
  13010. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13011. The variable `todo-only' is scoped dynamically into this function.
  13012. It will be set to t if the matcher restricts matching to TODO entries,
  13013. otherwise will not be touched.
  13014. Returns a cons of the selection string MATCH and the constructed
  13015. lisp form implementing the matcher. The matcher is to be evaluated
  13016. at an Org entry, with point on the headline, and returns t if the
  13017. entry matches the selection string MATCH. The returned lisp form
  13018. references two variables with information about the entry, which
  13019. must be bound around the form's evaluation: todo, the TODO keyword
  13020. at the entry (or nil of none); and tags-list, the list of all tags
  13021. at the entry including inherited ones. Additionally, the category
  13022. of the entry (if any) must be specified as the text property
  13023. 'org-category on the headline.
  13024. See also `org-scan-tags'.
  13025. "
  13026. (declare (special todo-only))
  13027. (unless (boundp 'todo-only)
  13028. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  13029. (unless match
  13030. ;; Get a new match request, with completion against the global
  13031. ;; tags table and the local tags in current buffer
  13032. (let ((org-last-tags-completion-table
  13033. (org-uniquify
  13034. (delq nil (append (org-get-buffer-tags)
  13035. (org-global-tags-completion-table))))))
  13036. (setq match (org-completing-read-no-i
  13037. "Match: " 'org-tags-completion-function nil nil nil
  13038. 'org-tags-history))))
  13039. ;; Parse the string and create a lisp form
  13040. (let ((match0 match)
  13041. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13042. minus tag mm
  13043. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13044. orterms term orlist re-p str-p level-p level-op time-p
  13045. prop-p pn pv po gv rest (start 0) (ss 0))
  13046. ;; Expand group tags
  13047. (setq match (org-tags-expand match))
  13048. ;; Check if there is a TODO part of this match, which would be the
  13049. ;; part after a "/". TO make sure that this slash is not part of
  13050. ;; a property value to be matched against, we also check that there
  13051. ;; is no " after that slash.
  13052. ;; First, find the last slash
  13053. (while (string-match "/+" match ss)
  13054. (setq start (match-beginning 0) ss (match-end 0)))
  13055. (if (and (string-match "/+" match start)
  13056. (not (save-match-data (string-match "\"" match start))))
  13057. ;; match contains also a todo-matching request
  13058. (progn
  13059. (setq tagsmatch (substring match 0 (match-beginning 0))
  13060. todomatch (substring match (match-end 0)))
  13061. (if (string-match "^!" todomatch)
  13062. (setq todo-only t todomatch (substring todomatch 1)))
  13063. (if (string-match "^\\s-*$" todomatch)
  13064. (setq todomatch nil)))
  13065. ;; only matching tags
  13066. (setq tagsmatch match todomatch nil))
  13067. ;; Make the tags matcher
  13068. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13069. (setq tagsmatcher t)
  13070. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13071. (while (setq term (pop orterms))
  13072. (while (and (equal (substring term -1) "\\") orterms)
  13073. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13074. (while (string-match re term)
  13075. (setq rest (substring term (match-end 0))
  13076. minus (and (match-end 1)
  13077. (equal (match-string 1 term) "-"))
  13078. tag (save-match-data (replace-regexp-in-string
  13079. "\\\\-" "-"
  13080. (match-string 2 term)))
  13081. re-p (equal (string-to-char tag) ?{)
  13082. level-p (match-end 4)
  13083. prop-p (match-end 5)
  13084. mm (cond
  13085. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13086. (level-p
  13087. (setq level-op (org-op-to-function (match-string 3 term)))
  13088. `(,level-op level ,(string-to-number
  13089. (match-string 4 term))))
  13090. (prop-p
  13091. (setq pn (match-string 5 term)
  13092. po (match-string 6 term)
  13093. pv (match-string 7 term)
  13094. re-p (equal (string-to-char pv) ?{)
  13095. str-p (equal (string-to-char pv) ?\")
  13096. time-p (save-match-data
  13097. (string-match "^\"[[<].*[]>]\"$" pv))
  13098. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  13099. (if time-p (setq pv (org-matcher-time pv)))
  13100. (setq po (org-op-to-function po (if time-p 'time str-p)))
  13101. (cond
  13102. ((equal pn "CATEGORY")
  13103. (setq gv '(get-text-property (point) 'org-category)))
  13104. ((equal pn "TODO")
  13105. (setq gv 'todo))
  13106. (t
  13107. (setq gv `(org-cached-entry-get nil ,pn))))
  13108. (if re-p
  13109. (if (eq po 'org<>)
  13110. `(not (string-match ,pv (or ,gv "")))
  13111. `(string-match ,pv (or ,gv "")))
  13112. (if str-p
  13113. `(,po (or ,gv "") ,pv)
  13114. `(,po (string-to-number (or ,gv ""))
  13115. ,(string-to-number pv) ))))
  13116. (t `(member ,tag tags-list)))
  13117. mm (if minus (list 'not mm) mm)
  13118. term rest)
  13119. (push mm tagsmatcher))
  13120. (push (if (> (length tagsmatcher) 1)
  13121. (cons 'and tagsmatcher)
  13122. (car tagsmatcher))
  13123. orlist)
  13124. (setq tagsmatcher nil))
  13125. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13126. (setq tagsmatcher
  13127. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13128. ;; Make the todo matcher
  13129. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13130. (setq todomatcher t)
  13131. (setq orterms (org-split-string todomatch "|") orlist nil)
  13132. (while (setq term (pop orterms))
  13133. (while (string-match re term)
  13134. (setq minus (and (match-end 1)
  13135. (equal (match-string 1 term) "-"))
  13136. kwd (match-string 2 term)
  13137. re-p (equal (string-to-char kwd) ?{)
  13138. term (substring term (match-end 0))
  13139. mm (if re-p
  13140. `(string-match ,(substring kwd 1 -1) todo)
  13141. (list 'equal 'todo kwd))
  13142. mm (if minus (list 'not mm) mm))
  13143. (push mm todomatcher))
  13144. (push (if (> (length todomatcher) 1)
  13145. (cons 'and todomatcher)
  13146. (car todomatcher))
  13147. orlist)
  13148. (setq todomatcher nil))
  13149. (setq todomatcher (if (> (length orlist) 1)
  13150. (cons 'or orlist) (car orlist))))
  13151. ;; Return the string and lisp forms of the matcher
  13152. (setq matcher (if todomatcher
  13153. (list 'and tagsmatcher todomatcher)
  13154. tagsmatcher))
  13155. (when todo-only
  13156. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13157. matcher)))
  13158. (cons match0 matcher)))
  13159. (defun org-tags-expand (match &optional single-as-list downcased)
  13160. "Expand group tags in MATCH.
  13161. This replaces every group tag in MATCH with a regexp tag search.
  13162. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13163. will be replaced like this:
  13164. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  13165. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  13166. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  13167. Replacing by a regexp preserves the structure of the match.
  13168. E.g., this expansion
  13169. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13170. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13171. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13172. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13173. assumed to be a single group tag, and the function will return
  13174. the list of tags in this group.
  13175. When DOWNCASE is non-nil, expand downcased TAGS."
  13176. (if org-group-tags
  13177. (let* ((case-fold-search t)
  13178. (stable org-mode-syntax-table)
  13179. (tal (or org-tag-groups-alist-for-agenda
  13180. org-tag-groups-alist))
  13181. (tal (if downcased
  13182. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  13183. (tml (mapcar 'car tal))
  13184. (rtnmatch match) rpl)
  13185. ;; @ and _ are allowed as word-components in tags
  13186. (modify-syntax-entry ?@ "w" stable)
  13187. (modify-syntax-entry ?_ "w" stable)
  13188. (while (and tml
  13189. (with-syntax-table stable
  13190. (string-match
  13191. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13192. (regexp-opt tml) "\\>\\)") rtnmatch)))
  13193. (let* ((dir (match-string 1 rtnmatch))
  13194. (tag (match-string 2 rtnmatch))
  13195. (tag (if downcased (downcase tag) tag)))
  13196. (setq tml (delete tag tml))
  13197. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  13198. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  13199. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  13200. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  13201. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  13202. (if single-as-list
  13203. (or (reverse rpl) (list rtnmatch))
  13204. rtnmatch))
  13205. (if single-as-list (list (if downcased (downcase match) match))
  13206. match)))
  13207. (defun org-op-to-function (op &optional stringp)
  13208. "Turn an operator into the appropriate function."
  13209. (setq op
  13210. (cond
  13211. ((equal op "<" ) '(< string< org-time<))
  13212. ((equal op ">" ) '(> org-string> org-time>))
  13213. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13214. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13215. ((member op '("=" "==")) '(= string= org-time=))
  13216. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13217. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13218. (defun org<> (a b) (not (= a b)))
  13219. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13220. (defun org-string>= (a b) (not (string< a b)))
  13221. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13222. (defun org-string<> (a b) (not (string= a b)))
  13223. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13224. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13225. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13226. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13227. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13228. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13229. (defun org-2ft (s)
  13230. "Convert S to a floating point time.
  13231. If S is already a number, just return it. If it is a string, parse
  13232. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13233. (cond
  13234. ((numberp s) s)
  13235. ((stringp s)
  13236. (condition-case nil
  13237. (float-time (apply 'encode-time (org-parse-time-string s)))
  13238. (error 0.)))
  13239. (t 0.)))
  13240. (defun org-time-today ()
  13241. "Time in seconds today at 0:00.
  13242. Returns the float number of seconds since the beginning of the
  13243. epoch to the beginning of today (00:00)."
  13244. (float-time (apply 'encode-time
  13245. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13246. (defun org-matcher-time (s)
  13247. "Interpret a time comparison value."
  13248. (save-match-data
  13249. (cond
  13250. ((string= s "<now>") (float-time))
  13251. ((string= s "<today>") (org-time-today))
  13252. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13253. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13254. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13255. (+ (org-time-today)
  13256. (* (string-to-number (match-string 1 s))
  13257. (cdr (assoc (match-string 2 s)
  13258. '(("d" . 86400.0) ("w" . 604800.0)
  13259. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13260. (t (org-2ft s)))))
  13261. (defun org-match-any-p (re list)
  13262. "Does re match any element of list?"
  13263. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13264. (delq nil list))
  13265. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13266. (defvar org-tags-overlay (make-overlay 1 1))
  13267. (org-detach-overlay org-tags-overlay)
  13268. (defun org-get-local-tags-at (&optional pos)
  13269. "Get a list of tags defined in the current headline."
  13270. (org-get-tags-at pos 'local))
  13271. (defun org-get-local-tags ()
  13272. "Get a list of tags defined in the current headline."
  13273. (org-get-tags-at nil 'local))
  13274. (defun org-get-tags-at (&optional pos local)
  13275. "Get a list of all headline tags applicable at POS.
  13276. POS defaults to point. If tags are inherited, the list contains
  13277. the targets in the same sequence as the headlines appear, i.e.
  13278. the tags of the current headline come last.
  13279. When LOCAL is non-nil, only return tags from the current headline,
  13280. ignore inherited ones."
  13281. (interactive)
  13282. (if (and org-trust-scanner-tags
  13283. (or (not pos) (equal pos (point)))
  13284. (not local))
  13285. org-scanner-tags
  13286. (let (tags ltags lastpos parent)
  13287. (save-excursion
  13288. (save-restriction
  13289. (widen)
  13290. (goto-char (or pos (point)))
  13291. (save-match-data
  13292. (catch 'done
  13293. (condition-case nil
  13294. (progn
  13295. (org-back-to-heading t)
  13296. (while (not (equal lastpos (point)))
  13297. (setq lastpos (point))
  13298. (when (looking-at
  13299. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13300. (setq ltags (org-split-string
  13301. (org-match-string-no-properties 1) ":"))
  13302. (when parent
  13303. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13304. (setq tags (append
  13305. (if parent
  13306. (org-remove-uninherited-tags ltags)
  13307. ltags)
  13308. tags)))
  13309. (or org-use-tag-inheritance (throw 'done t))
  13310. (if local (throw 'done t))
  13311. (or (org-up-heading-safe) (error nil))
  13312. (setq parent t)))
  13313. (error nil)))))
  13314. (if local
  13315. tags
  13316. (reverse (delete-dups
  13317. (reverse (append
  13318. (org-remove-uninherited-tags
  13319. org-file-tags) tags)))))))))
  13320. (defun org-add-prop-inherited (s)
  13321. (add-text-properties 0 (length s) '(inherited t) s)
  13322. s)
  13323. (defun org-toggle-tag (tag &optional onoff)
  13324. "Toggle the tag TAG for the current line.
  13325. If ONOFF is `on' or `off', don't toggle but set to this state."
  13326. (let (res current)
  13327. (save-excursion
  13328. (org-back-to-heading t)
  13329. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13330. (point-at-eol) t)
  13331. (progn
  13332. (setq current (match-string 1))
  13333. (replace-match ""))
  13334. (setq current ""))
  13335. (setq current (nreverse (org-split-string current ":")))
  13336. (cond
  13337. ((eq onoff 'on)
  13338. (setq res t)
  13339. (or (member tag current) (push tag current)))
  13340. ((eq onoff 'off)
  13341. (or (not (member tag current)) (setq current (delete tag current))))
  13342. (t (if (member tag current)
  13343. (setq current (delete tag current))
  13344. (setq res t)
  13345. (push tag current))))
  13346. (end-of-line 1)
  13347. (if current
  13348. (progn
  13349. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13350. (org-set-tags nil t))
  13351. (delete-horizontal-space))
  13352. (run-hooks 'org-after-tags-change-hook))
  13353. res))
  13354. (defun org-align-tags-here (to-col)
  13355. ;; Assumes that this is a headline
  13356. "Align tags on the current headline to TO-COL."
  13357. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13358. (beginning-of-line 1)
  13359. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13360. (< pos (match-beginning 2)))
  13361. (progn
  13362. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13363. (goto-char (match-beginning 1))
  13364. (insert " ")
  13365. (delete-region (point) (1+ (match-beginning 2)))
  13366. (setq ncol (max (current-column)
  13367. (1+ col)
  13368. (if (> to-col 0)
  13369. to-col
  13370. (- (abs to-col) tags-l))))
  13371. (setq p (point))
  13372. (insert (make-string (- ncol (current-column)) ?\ ))
  13373. (setq ncol (current-column))
  13374. (when indent-tabs-mode (tabify p (point-at-eol)))
  13375. (org-move-to-column (min ncol col)))
  13376. (goto-char pos))))
  13377. (defun org-set-tags-command (&optional arg just-align)
  13378. "Call the set-tags command for the current entry."
  13379. (interactive "P")
  13380. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13381. (org-set-tags arg just-align)
  13382. (save-excursion
  13383. (unless (and (org-region-active-p)
  13384. org-loop-over-headlines-in-active-region)
  13385. (org-back-to-heading t))
  13386. (org-set-tags arg just-align))))
  13387. (defun org-set-tags-to (data)
  13388. "Set the tags of the current entry to DATA, replacing the current tags.
  13389. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13390. If DATA is nil or the empty string, any tags will be removed."
  13391. (interactive "sTags: ")
  13392. (setq data
  13393. (cond
  13394. ((eq data nil) "")
  13395. ((equal data "") "")
  13396. ((stringp data)
  13397. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13398. ":"))
  13399. ((listp data)
  13400. (concat ":" (mapconcat 'identity data ":") ":"))))
  13401. (when data
  13402. (save-excursion
  13403. (org-back-to-heading t)
  13404. (when (looking-at org-complex-heading-regexp)
  13405. (if (match-end 5)
  13406. (progn
  13407. (goto-char (match-beginning 5))
  13408. (insert data)
  13409. (delete-region (point) (point-at-eol))
  13410. (org-set-tags nil 'align))
  13411. (goto-char (point-at-eol))
  13412. (insert " " data)
  13413. (org-set-tags nil 'align)))
  13414. (beginning-of-line 1)
  13415. (if (looking-at ".*?\\([ \t]+\\)$")
  13416. (delete-region (match-beginning 1) (match-end 1))))))
  13417. (defun org-align-all-tags ()
  13418. "Align the tags i all headings."
  13419. (interactive)
  13420. (save-excursion
  13421. (or (ignore-errors (org-back-to-heading t))
  13422. (outline-next-heading))
  13423. (if (org-at-heading-p)
  13424. (org-set-tags t)
  13425. (message "No headings"))))
  13426. (defvar org-indent-indentation-per-level)
  13427. (defun org-set-tags (&optional arg just-align)
  13428. "Set the tags for the current headline.
  13429. With prefix ARG, realign all tags in headings in the current buffer.
  13430. When JUST-ALIGN is non-nil, only align tags."
  13431. (interactive "P")
  13432. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13433. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13434. 'region-start-level 'region))
  13435. org-loop-over-headlines-in-active-region)
  13436. (org-map-entries
  13437. ;; We don't use ARG and JUST-ALIGN here because these args
  13438. ;; are not useful when looping over headlines.
  13439. `(org-set-tags)
  13440. org-loop-over-headlines-in-active-region
  13441. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13442. (let* ((re org-outline-regexp-bol)
  13443. (current (unless arg (org-get-tags-string)))
  13444. (col (current-column))
  13445. (org-setting-tags t)
  13446. table current-tags inherited-tags ; computed below when needed
  13447. tags p0 c0 c1 rpl di tc level)
  13448. (if arg
  13449. (save-excursion
  13450. (goto-char (point-min))
  13451. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13452. (while (re-search-forward re nil t)
  13453. (org-set-tags nil t)
  13454. (end-of-line 1)))
  13455. (message "All tags realigned to column %d" org-tags-column))
  13456. (if just-align
  13457. (setq tags current)
  13458. ;; Get a new set of tags from the user
  13459. (save-excursion
  13460. (setq table (append org-tag-persistent-alist
  13461. (or org-tag-alist (org-get-buffer-tags))
  13462. (and
  13463. org-complete-tags-always-offer-all-agenda-tags
  13464. (org-global-tags-completion-table
  13465. (org-agenda-files))))
  13466. org-last-tags-completion-table table
  13467. current-tags (org-split-string current ":")
  13468. inherited-tags (nreverse
  13469. (nthcdr (length current-tags)
  13470. (nreverse (org-get-tags-at))))
  13471. tags
  13472. (if (or (eq t org-use-fast-tag-selection)
  13473. (and org-use-fast-tag-selection
  13474. (delq nil (mapcar 'cdr table))))
  13475. (org-fast-tag-selection
  13476. current-tags inherited-tags table
  13477. (if org-fast-tag-selection-include-todo
  13478. org-todo-key-alist))
  13479. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13480. (org-trim
  13481. (org-icompleting-read "Tags: "
  13482. 'org-tags-completion-function
  13483. nil nil current 'org-tags-history))))))
  13484. (while (string-match "[-+&]+" tags)
  13485. ;; No boolean logic, just a list
  13486. (setq tags (replace-match ":" t t tags))))
  13487. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13488. (if org-tags-sort-function
  13489. (setq tags (mapconcat 'identity
  13490. (sort (org-split-string
  13491. tags (org-re "[^[:alnum:]_@#%]+"))
  13492. org-tags-sort-function) ":")))
  13493. (if (string-match "\\`[\t ]*\\'" tags)
  13494. (setq tags "")
  13495. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13496. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13497. ;; Insert new tags at the correct column
  13498. (beginning-of-line 1)
  13499. (setq level (or (and (looking-at org-outline-regexp)
  13500. (- (match-end 0) (point) 1))
  13501. 1))
  13502. (cond
  13503. ((and (equal current "") (equal tags "")))
  13504. ((re-search-forward
  13505. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13506. (point-at-eol) t)
  13507. (if (equal tags "")
  13508. (setq rpl "")
  13509. (goto-char (match-beginning 0))
  13510. (setq c0 (current-column)
  13511. ;; compute offset for the case of org-indent-mode active
  13512. di (if (org-bound-and-true-p org-indent-mode)
  13513. (* (1- org-indent-indentation-per-level) (1- level))
  13514. 0)
  13515. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13516. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13517. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (string-width tags))))
  13518. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13519. (replace-match rpl t t)
  13520. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13521. tags)
  13522. (t (error "Tags alignment failed")))
  13523. (org-move-to-column col)
  13524. (unless just-align
  13525. (run-hooks 'org-after-tags-change-hook))))))
  13526. (defun org-change-tag-in-region (beg end tag off)
  13527. "Add or remove TAG for each entry in the region.
  13528. This works in the agenda, and also in an org-mode buffer."
  13529. (interactive
  13530. (list (region-beginning) (region-end)
  13531. (let ((org-last-tags-completion-table
  13532. (if (derived-mode-p 'org-mode)
  13533. (org-uniquify
  13534. (delq nil (append (org-get-buffer-tags)
  13535. (org-global-tags-completion-table))))
  13536. (org-global-tags-completion-table))))
  13537. (org-icompleting-read
  13538. "Tag: " 'org-tags-completion-function nil nil nil
  13539. 'org-tags-history))
  13540. (progn
  13541. (message "[s]et or [r]emove? ")
  13542. (equal (read-char-exclusive) ?r))))
  13543. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13544. (let ((agendap (equal major-mode 'org-agenda-mode))
  13545. l1 l2 m buf pos newhead (cnt 0))
  13546. (goto-char end)
  13547. (setq l2 (1- (org-current-line)))
  13548. (goto-char beg)
  13549. (setq l1 (org-current-line))
  13550. (loop for l from l1 to l2 do
  13551. (org-goto-line l)
  13552. (setq m (get-text-property (point) 'org-hd-marker))
  13553. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13554. (and agendap m))
  13555. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13556. pos (if agendap m (point)))
  13557. (with-current-buffer buf
  13558. (save-excursion
  13559. (save-restriction
  13560. (goto-char pos)
  13561. (setq cnt (1+ cnt))
  13562. (org-toggle-tag tag (if off 'off 'on))
  13563. (setq newhead (org-get-heading)))))
  13564. (and agendap (org-agenda-change-all-lines newhead m))))
  13565. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13566. (defun org-tags-completion-function (string predicate &optional flag)
  13567. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13568. (confirm (lambda (x) (stringp (car x)))))
  13569. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13570. (setq s1 (match-string 1 string)
  13571. s2 (match-string 2 string))
  13572. (setq s1 "" s2 string))
  13573. (cond
  13574. ((eq flag nil)
  13575. ;; try completion
  13576. (setq rtn (try-completion s2 ctable confirm))
  13577. (if (stringp rtn)
  13578. (setq rtn
  13579. (concat s1 s2 (substring rtn (length s2))
  13580. (if (and org-add-colon-after-tag-completion
  13581. (assoc rtn ctable))
  13582. ":" ""))))
  13583. rtn)
  13584. ((eq flag t)
  13585. ;; all-completions
  13586. (all-completions s2 ctable confirm))
  13587. ((eq flag 'lambda)
  13588. ;; exact match?
  13589. (assoc s2 ctable)))))
  13590. (defun org-fast-tag-insert (kwd tags face &optional end)
  13591. "Insert KDW, and the TAGS, the latter with face FACE.
  13592. Also insert END."
  13593. (insert (format "%-12s" (concat kwd ":"))
  13594. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13595. (or end "")))
  13596. (defun org-fast-tag-show-exit (flag)
  13597. (save-excursion
  13598. (org-goto-line 3)
  13599. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13600. (replace-match ""))
  13601. (when flag
  13602. (end-of-line 1)
  13603. (org-move-to-column (- (window-width) 19) t)
  13604. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13605. (defun org-set-current-tags-overlay (current prefix)
  13606. "Add an overlay to CURRENT tag with PREFIX."
  13607. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13608. (if (featurep 'xemacs)
  13609. (org-overlay-display org-tags-overlay (concat prefix s)
  13610. 'secondary-selection)
  13611. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13612. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13613. (defvar org-last-tag-selection-key nil)
  13614. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13615. "Fast tag selection with single keys.
  13616. CURRENT is the current list of tags in the headline, INHERITED is the
  13617. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13618. possibly with grouping information. TODO-TABLE is a similar table with
  13619. TODO keywords, should these have keys assigned to them.
  13620. If the keys are nil, a-z are automatically assigned.
  13621. Returns the new tags string, or nil to not change the current settings."
  13622. (let* ((fulltable (append table todo-table))
  13623. (maxlen (apply 'max (mapcar
  13624. (lambda (x)
  13625. (if (stringp (car x)) (string-width (car x)) 0))
  13626. fulltable)))
  13627. (buf (current-buffer))
  13628. (expert (eq org-fast-tag-selection-single-key 'expert))
  13629. (buffer-tags nil)
  13630. (fwidth (+ maxlen 3 1 3))
  13631. (ncol (/ (- (window-width) 4) fwidth))
  13632. (i-face 'org-done)
  13633. (c-face 'org-todo)
  13634. tg cnt e c char c1 c2 ntable tbl rtn
  13635. ov-start ov-end ov-prefix
  13636. (exit-after-next org-fast-tag-selection-single-key)
  13637. (done-keywords org-done-keywords)
  13638. groups ingroup)
  13639. (save-excursion
  13640. (beginning-of-line 1)
  13641. (if (looking-at
  13642. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13643. (setq ov-start (match-beginning 1)
  13644. ov-end (match-end 1)
  13645. ov-prefix "")
  13646. (setq ov-start (1- (point-at-eol))
  13647. ov-end (1+ ov-start))
  13648. (skip-chars-forward "^\n\r")
  13649. (setq ov-prefix
  13650. (concat
  13651. (buffer-substring (1- (point)) (point))
  13652. (if (> (current-column) org-tags-column)
  13653. " "
  13654. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13655. (move-overlay org-tags-overlay ov-start ov-end)
  13656. (save-window-excursion
  13657. (if expert
  13658. (set-buffer (get-buffer-create " *Org tags*"))
  13659. (delete-other-windows)
  13660. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13661. (org-switch-to-buffer-other-window " *Org tags*"))
  13662. (erase-buffer)
  13663. (org-set-local 'org-done-keywords done-keywords)
  13664. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13665. (org-fast-tag-insert "Current" current c-face "\n\n")
  13666. (org-fast-tag-show-exit exit-after-next)
  13667. (org-set-current-tags-overlay current ov-prefix)
  13668. (setq tbl fulltable char ?a cnt 0)
  13669. (while (setq e (pop tbl))
  13670. (cond
  13671. ((equal (car e) :startgroup)
  13672. (push '() groups) (setq ingroup t)
  13673. (when (not (= cnt 0))
  13674. (setq cnt 0)
  13675. (insert "\n"))
  13676. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13677. ((equal (car e) :endgroup)
  13678. (setq ingroup nil cnt 0)
  13679. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13680. ((equal e '(:newline))
  13681. (when (not (= cnt 0))
  13682. (setq cnt 0)
  13683. (insert "\n")
  13684. (setq e (car tbl))
  13685. (while (equal (car tbl) '(:newline))
  13686. (insert "\n")
  13687. (setq tbl (cdr tbl)))))
  13688. ((equal e '(:grouptags)) nil)
  13689. (t
  13690. (setq tg (copy-sequence (car e)) c2 nil)
  13691. (if (cdr e)
  13692. (setq c (cdr e))
  13693. ;; automatically assign a character.
  13694. (setq c1 (string-to-char
  13695. (downcase (substring
  13696. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13697. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13698. (while (or (rassoc char ntable) (rassoc char table))
  13699. (setq char (1+ char)))
  13700. (setq c2 c1))
  13701. (setq c (or c2 char)))
  13702. (if ingroup (push tg (car groups)))
  13703. (setq tg (org-add-props tg nil 'face
  13704. (cond
  13705. ((not (assoc tg table))
  13706. (org-get-todo-face tg))
  13707. ((member tg current) c-face)
  13708. ((member tg inherited) i-face))))
  13709. (if (equal (caar tbl) :grouptags)
  13710. (org-add-props tg nil 'face 'org-tag-group))
  13711. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13712. (insert "[" c "] " tg (make-string
  13713. (- fwidth 4 (length tg)) ?\ ))
  13714. (push (cons tg c) ntable)
  13715. (when (= (setq cnt (1+ cnt)) ncol)
  13716. (insert "\n")
  13717. (if ingroup (insert " "))
  13718. (setq cnt 0)))))
  13719. (setq ntable (nreverse ntable))
  13720. (insert "\n")
  13721. (goto-char (point-min))
  13722. (if (not expert) (org-fit-window-to-buffer))
  13723. (setq rtn
  13724. (catch 'exit
  13725. (while t
  13726. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13727. (if (not groups) "no " "")
  13728. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13729. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13730. (setq org-last-tag-selection-key c)
  13731. (cond
  13732. ((= c ?\r) (throw 'exit t))
  13733. ((= c ?!)
  13734. (setq groups (not groups))
  13735. (goto-char (point-min))
  13736. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13737. ((= c ?\C-c)
  13738. (if (not expert)
  13739. (org-fast-tag-show-exit
  13740. (setq exit-after-next (not exit-after-next)))
  13741. (setq expert nil)
  13742. (delete-other-windows)
  13743. (set-window-buffer (split-window-vertically) " *Org tags*")
  13744. (org-switch-to-buffer-other-window " *Org tags*")
  13745. (org-fit-window-to-buffer)))
  13746. ((or (= c ?\C-g)
  13747. (and (= c ?q) (not (rassoc c ntable))))
  13748. (org-detach-overlay org-tags-overlay)
  13749. (setq quit-flag t))
  13750. ((= c ?\ )
  13751. (setq current nil)
  13752. (if exit-after-next (setq exit-after-next 'now)))
  13753. ((= c ?\t)
  13754. (condition-case nil
  13755. (setq tg (org-icompleting-read
  13756. "Tag: "
  13757. (or buffer-tags
  13758. (with-current-buffer buf
  13759. (org-get-buffer-tags)))))
  13760. (quit (setq tg "")))
  13761. (when (string-match "\\S-" tg)
  13762. (add-to-list 'buffer-tags (list tg))
  13763. (if (member tg current)
  13764. (setq current (delete tg current))
  13765. (push tg current)))
  13766. (if exit-after-next (setq exit-after-next 'now)))
  13767. ((setq e (rassoc c todo-table) tg (car e))
  13768. (with-current-buffer buf
  13769. (save-excursion (org-todo tg)))
  13770. (if exit-after-next (setq exit-after-next 'now)))
  13771. ((setq e (rassoc c ntable) tg (car e))
  13772. (if (member tg current)
  13773. (setq current (delete tg current))
  13774. (loop for g in groups do
  13775. (if (member tg g)
  13776. (mapc (lambda (x)
  13777. (setq current (delete x current)))
  13778. g)))
  13779. (push tg current))
  13780. (if exit-after-next (setq exit-after-next 'now))))
  13781. ;; Create a sorted list
  13782. (setq current
  13783. (sort current
  13784. (lambda (a b)
  13785. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13786. (if (eq exit-after-next 'now) (throw 'exit t))
  13787. (goto-char (point-min))
  13788. (beginning-of-line 2)
  13789. (delete-region (point) (point-at-eol))
  13790. (org-fast-tag-insert "Current" current c-face)
  13791. (org-set-current-tags-overlay current ov-prefix)
  13792. (while (re-search-forward
  13793. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13794. (setq tg (match-string 1))
  13795. (add-text-properties
  13796. (match-beginning 1) (match-end 1)
  13797. (list 'face
  13798. (cond
  13799. ((member tg current) c-face)
  13800. ((member tg inherited) i-face)
  13801. (t (get-text-property (match-beginning 1) 'face))))))
  13802. (goto-char (point-min)))))
  13803. (org-detach-overlay org-tags-overlay)
  13804. (if rtn
  13805. (mapconcat 'identity current ":")
  13806. nil))))
  13807. (defun org-get-tags-string ()
  13808. "Get the TAGS string in the current headline."
  13809. (unless (org-at-heading-p t)
  13810. (user-error "Not on a heading"))
  13811. (save-excursion
  13812. (beginning-of-line 1)
  13813. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13814. (org-match-string-no-properties 1)
  13815. "")))
  13816. (defun org-get-tags ()
  13817. "Get the list of tags specified in the current headline."
  13818. (org-split-string (org-get-tags-string) ":"))
  13819. (defun org-get-buffer-tags ()
  13820. "Get a table of all tags used in the buffer, for completion."
  13821. (let (tags)
  13822. (save-excursion
  13823. (goto-char (point-min))
  13824. (while (re-search-forward
  13825. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13826. (when (equal (char-after (point-at-bol 0)) ?*)
  13827. (mapc (lambda (x) (add-to-list 'tags x))
  13828. (org-split-string (org-match-string-no-properties 1) ":")))))
  13829. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13830. (mapcar 'list tags)))
  13831. ;;;; The mapping API
  13832. (defun org-map-entries (func &optional match scope &rest skip)
  13833. "Call FUNC at each headline selected by MATCH in SCOPE.
  13834. FUNC is a function or a lisp form. The function will be called without
  13835. arguments, with the cursor positioned at the beginning of the headline.
  13836. The return values of all calls to the function will be collected and
  13837. returned as a list.
  13838. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13839. does not need to preserve point. After evaluation, the cursor will be
  13840. moved to the end of the line (presumably of the headline of the
  13841. processed entry) and search continues from there. Under some
  13842. circumstances, this may not produce the wanted results. For example,
  13843. if you have removed (e.g. archived) the current (sub)tree it could
  13844. mean that the next entry will be skipped entirely. In such cases, you
  13845. can specify the position from where search should continue by making
  13846. FUNC set the variable `org-map-continue-from' to the desired buffer
  13847. position.
  13848. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13849. Only headlines that are matched by this query will be considered during
  13850. the iteration. When MATCH is nil or t, all headlines will be
  13851. visited by the iteration.
  13852. SCOPE determines the scope of this command. It can be any of:
  13853. nil The current buffer, respecting the restriction if any
  13854. tree The subtree started with the entry at point
  13855. region The entries within the active region, if any
  13856. region-start-level
  13857. The entries within the active region, but only those at
  13858. the same level than the first one.
  13859. file The current buffer, without restriction
  13860. file-with-archives
  13861. The current buffer, and any archives associated with it
  13862. agenda All agenda files
  13863. agenda-with-archives
  13864. All agenda files with any archive files associated with them
  13865. \(file1 file2 ...)
  13866. If this is a list, all files in the list will be scanned
  13867. The remaining args are treated as settings for the skipping facilities of
  13868. the scanner. The following items can be given here:
  13869. archive skip trees with the archive tag
  13870. comment skip trees with the COMMENT keyword
  13871. function or Emacs Lisp form:
  13872. will be used as value for `org-agenda-skip-function', so
  13873. whenever the function returns a position, FUNC will not be
  13874. called for that entry and search will continue from the
  13875. position returned
  13876. If your function needs to retrieve the tags including inherited tags
  13877. at the *current* entry, you can use the value of the variable
  13878. `org-scanner-tags' which will be much faster than getting the value
  13879. with `org-get-tags-at'. If your function gets properties with
  13880. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13881. to t around the call to `org-entry-properties' to get the same speedup.
  13882. Note that if your function moves around to retrieve tags and properties at
  13883. a *different* entry, you cannot use these techniques."
  13884. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13885. (not (org-region-active-p)))
  13886. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13887. (org-agenda-skip-archived-trees (memq 'archive skip))
  13888. (org-agenda-skip-comment-trees (memq 'comment skip))
  13889. (org-agenda-skip-function
  13890. (car (org-delete-all '(comment archive) skip)))
  13891. (org-tags-match-list-sublevels t)
  13892. (start-level (eq scope 'region-start-level))
  13893. matcher file res
  13894. org-todo-keywords-for-agenda
  13895. org-done-keywords-for-agenda
  13896. org-todo-keyword-alist-for-agenda
  13897. org-tag-alist-for-agenda
  13898. todo-only)
  13899. (cond
  13900. ((eq match t) (setq matcher t))
  13901. ((eq match nil) (setq matcher t))
  13902. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13903. (save-excursion
  13904. (save-restriction
  13905. (cond ((eq scope 'tree)
  13906. (org-back-to-heading t)
  13907. (org-narrow-to-subtree)
  13908. (setq scope nil))
  13909. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13910. (org-region-active-p))
  13911. ;; If needed, set start-level to a string like "2"
  13912. (when start-level
  13913. (save-excursion
  13914. (goto-char (region-beginning))
  13915. (unless (org-at-heading-p) (outline-next-heading))
  13916. (setq start-level (org-current-level))))
  13917. (narrow-to-region (region-beginning)
  13918. (save-excursion
  13919. (goto-char (region-end))
  13920. (unless (and (bolp) (org-at-heading-p))
  13921. (outline-next-heading))
  13922. (point)))
  13923. (setq scope nil)))
  13924. (if (not scope)
  13925. (progn
  13926. (org-agenda-prepare-buffers
  13927. (list (buffer-file-name (current-buffer))))
  13928. (setq res (org-scan-tags func matcher todo-only start-level)))
  13929. ;; Get the right scope
  13930. (cond
  13931. ((and scope (listp scope) (symbolp (car scope)))
  13932. (setq scope (eval scope)))
  13933. ((eq scope 'agenda)
  13934. (setq scope (org-agenda-files t)))
  13935. ((eq scope 'agenda-with-archives)
  13936. (setq scope (org-agenda-files t))
  13937. (setq scope (org-add-archive-files scope)))
  13938. ((eq scope 'file)
  13939. (setq scope (list (buffer-file-name))))
  13940. ((eq scope 'file-with-archives)
  13941. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13942. (org-agenda-prepare-buffers scope)
  13943. (while (setq file (pop scope))
  13944. (with-current-buffer (org-find-base-buffer-visiting file)
  13945. (save-excursion
  13946. (save-restriction
  13947. (widen)
  13948. (goto-char (point-min))
  13949. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13950. res)))
  13951. ;;;; Properties
  13952. ;;; Setting and retrieving properties
  13953. (defconst org-special-properties
  13954. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13955. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13956. "The special properties valid in Org-mode.
  13957. These are properties that are not defined in the property drawer,
  13958. but in some other way.")
  13959. (defconst org-default-properties
  13960. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13961. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13962. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13963. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13964. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13965. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13966. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13967. "Some properties that are used by Org-mode for various purposes.
  13968. Being in this list makes sure that they are offered for completion.")
  13969. (defun org-property-action ()
  13970. "Do an action on properties."
  13971. (interactive)
  13972. (let (c)
  13973. (org-at-property-p)
  13974. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13975. (setq c (read-char-exclusive))
  13976. (cond
  13977. ((equal c ?s)
  13978. (call-interactively 'org-set-property))
  13979. ((equal c ?d)
  13980. (call-interactively 'org-delete-property))
  13981. ((equal c ?D)
  13982. (call-interactively 'org-delete-property-globally))
  13983. ((equal c ?c)
  13984. (call-interactively 'org-compute-property-at-point))
  13985. (t (user-error "No such property action %c" c)))))
  13986. (defun org-inc-effort ()
  13987. "Increment the value of the effort property in the current entry."
  13988. (interactive)
  13989. (org-set-effort nil t))
  13990. (defvar org-clock-effort) ;; Defined in org-clock.el
  13991. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13992. (defun org-set-effort (&optional value increment)
  13993. "Set the effort property of the current entry.
  13994. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13995. 10th allowed value.
  13996. When INCREMENT is non-nil, set the property to the next allowed value."
  13997. (interactive "P")
  13998. (if (equal value 0) (setq value 10))
  13999. (let* ((completion-ignore-case t)
  14000. (prop org-effort-property)
  14001. (cur (org-entry-get nil prop))
  14002. (allowed (org-property-get-allowed-values nil prop 'table))
  14003. (existing (mapcar 'list (org-property-values prop)))
  14004. (heading (nth 4 (org-heading-components)))
  14005. rpl
  14006. (val (cond
  14007. ((stringp value) value)
  14008. ((and allowed (integerp value))
  14009. (or (car (nth (1- value) allowed))
  14010. (car (org-last allowed))))
  14011. ((and allowed increment)
  14012. (or (caadr (member (list cur) allowed))
  14013. (user-error "Allowed effort values are not set")))
  14014. (allowed
  14015. (message "Select 1-9,0, [RET%s]: %s"
  14016. (if cur (concat "=" cur) "")
  14017. (mapconcat 'car allowed " "))
  14018. (setq rpl (read-char-exclusive))
  14019. (if (equal rpl ?\r)
  14020. cur
  14021. (setq rpl (- rpl ?0))
  14022. (if (equal rpl 0) (setq rpl 10))
  14023. (if (and (> rpl 0) (<= rpl (length allowed)))
  14024. (car (nth (1- rpl) allowed))
  14025. (org-completing-read "Effort: " allowed nil))))
  14026. (t
  14027. (let (org-completion-use-ido org-completion-use-iswitchb)
  14028. (org-completing-read
  14029. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14030. (concat "[" cur "]") "")
  14031. ": ")
  14032. existing nil nil "" nil cur))))))
  14033. (unless (equal (org-entry-get nil prop) val)
  14034. (org-entry-put nil prop val))
  14035. (org-refresh-property
  14036. '((effort . identity)
  14037. (effort-minutes . org-duration-string-to-minutes))
  14038. val)
  14039. (when (string= heading org-clock-current-task)
  14040. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14041. (org-clock-update-mode-line))
  14042. (message "%s is now %s" prop val)))
  14043. (defun org-at-property-p ()
  14044. "Non-nil when point is inside a property drawer.
  14045. See `org-property-re' for match data, if applicable."
  14046. (when (eq (org-element-type (org-element-at-point)) 'node-property)
  14047. (save-excursion
  14048. (beginning-of-line)
  14049. (looking-at org-property-re))))
  14050. (defun org-get-property-block (&optional beg end force)
  14051. "Return the (beg . end) range of the body of the property drawer.
  14052. BEG and END are the beginning and end of the current subtree, or of
  14053. the part before the first headline. If they are not given, they will
  14054. be found. If the drawer does not exist and FORCE is non-nil, create
  14055. the drawer."
  14056. (catch 'exit
  14057. (save-excursion
  14058. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  14059. (progn (org-back-to-heading t) (point))))
  14060. (end (or end (and (not (outline-next-heading)) (point-max))
  14061. (point))))
  14062. (goto-char beg)
  14063. (if (re-search-forward org-property-start-re end t)
  14064. (setq beg (1+ (match-end 0)))
  14065. (if force
  14066. (save-excursion
  14067. (org-insert-property-drawer)
  14068. (setq end (progn (outline-next-heading) (point))))
  14069. (throw 'exit nil))
  14070. (goto-char beg)
  14071. (if (re-search-forward org-property-start-re end t)
  14072. (setq beg (1+ (match-end 0)))))
  14073. (if (re-search-forward org-property-end-re end t)
  14074. (setq end (match-beginning 0))
  14075. (or force (throw 'exit nil))
  14076. (goto-char beg)
  14077. (setq end beg)
  14078. (org-indent-line)
  14079. (insert ":END:\n"))
  14080. (cons beg end)))))
  14081. (defun org-entry-properties (&optional pom which specific)
  14082. "Get all properties of the entry at point-or-marker POM.
  14083. This includes the TODO keyword, the tags, time strings for deadline,
  14084. scheduled, and clocking, and any additional properties defined in the
  14085. entry. The return value is an alist, keys may occur multiple times
  14086. if the property key was used several times.
  14087. POM may also be nil, in which case the current entry is used.
  14088. If WHICH is nil or `all', get all properties. If WHICH is
  14089. `special' or `standard', only get that subclass. If WHICH
  14090. is a string only get exactly this property. SPECIFIC can be a string, the
  14091. specific property we are interested in. Specifying it can speed
  14092. things up because then unnecessary parsing is avoided."
  14093. (setq which (or which 'all))
  14094. (org-with-wide-buffer
  14095. (org-with-point-at pom
  14096. (let ((clockstr (substring org-clock-string 0 -1))
  14097. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  14098. (case-fold-search nil)
  14099. beg end range props sum-props key key1 value string clocksum clocksumt)
  14100. (when (and (derived-mode-p 'org-mode)
  14101. (ignore-errors (org-back-to-heading t)))
  14102. (setq beg (point))
  14103. (setq sum-props (get-text-property (point) 'org-summaries))
  14104. (setq clocksum (get-text-property (point) :org-clock-minutes)
  14105. clocksumt (get-text-property (point) :org-clock-minutes-today))
  14106. (outline-next-heading)
  14107. (setq end (point))
  14108. (when (memq which '(all special))
  14109. ;; Get the special properties, like TODO and tags
  14110. (goto-char beg)
  14111. (when (and (or (not specific) (string= specific "TODO"))
  14112. (looking-at org-todo-line-regexp) (match-end 2))
  14113. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14114. (when (and (or (not specific) (string= specific "PRIORITY"))
  14115. (looking-at org-priority-regexp))
  14116. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14117. (when (or (not specific) (string= specific "FILE"))
  14118. (push (cons "FILE" buffer-file-name) props))
  14119. (when (and (or (not specific) (string= specific "TAGS"))
  14120. (setq value (org-get-tags-string))
  14121. (string-match "\\S-" value))
  14122. (push (cons "TAGS" value) props))
  14123. (when (and (or (not specific) (string= specific "ALLTAGS"))
  14124. (setq value (org-get-tags-at)))
  14125. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  14126. ":"))
  14127. props))
  14128. (when (or (not specific) (string= specific "BLOCKED"))
  14129. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  14130. (when (or (not specific)
  14131. (member specific
  14132. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  14133. "TIMESTAMP" "TIMESTAMP_IA")))
  14134. (catch 'match
  14135. (while (and (re-search-forward org-maybe-keyword-time-regexp end t)
  14136. (not (text-property-any 0 (length (match-string 0))
  14137. 'face 'font-lock-comment-face
  14138. (match-string 0))))
  14139. (setq key (if (match-end 1)
  14140. (substring (org-match-string-no-properties 1)
  14141. 0 -1))
  14142. string (if (equal key clockstr)
  14143. (org-trim
  14144. (buffer-substring-no-properties
  14145. (match-beginning 3) (goto-char
  14146. (point-at-eol))))
  14147. (substring (org-match-string-no-properties 3)
  14148. 1 -1)))
  14149. ;; Get the correct property name from the key. This is
  14150. ;; necessary if the user has configured time keywords.
  14151. (setq key1 (concat key ":"))
  14152. (cond
  14153. ((not key)
  14154. (setq key
  14155. (if (= (char-after (match-beginning 3)) ?\[)
  14156. "TIMESTAMP_IA" "TIMESTAMP")))
  14157. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  14158. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  14159. ((equal key1 org-closed-string) (setq key "CLOSED"))
  14160. ((equal key1 org-clock-string) (setq key "CLOCK")))
  14161. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  14162. (progn
  14163. (push (cons key string) props)
  14164. ;; no need to search further if match is found
  14165. (throw 'match t))
  14166. (when (or (equal key "CLOCK") (not (assoc key props)))
  14167. (push (cons key string) props)))))))
  14168. (when (memq which '(all standard))
  14169. ;; Get the standard properties, like :PROP: ...
  14170. (setq range (org-get-property-block beg end))
  14171. (when range
  14172. (goto-char (car range))
  14173. (while (re-search-forward org-property-re
  14174. (cdr range) t)
  14175. (setq key (org-match-string-no-properties 2)
  14176. value (org-trim (or (org-match-string-no-properties 3) "")))
  14177. (unless (member key excluded)
  14178. (push (cons key (or value "")) props)))))
  14179. (if clocksum
  14180. (push (cons "CLOCKSUM"
  14181. (org-columns-number-to-string (/ (float clocksum) 60.)
  14182. 'add_times))
  14183. props))
  14184. (if clocksumt
  14185. (push (cons "CLOCKSUM_T"
  14186. (org-columns-number-to-string (/ (float clocksumt) 60.)
  14187. 'add_times))
  14188. props))
  14189. (unless (assoc "CATEGORY" props)
  14190. (push (cons "CATEGORY" (org-get-category)) props))
  14191. (append sum-props (nreverse props)))))))
  14192. (defun org-entry-get (pom property &optional inherit literal-nil)
  14193. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14194. If INHERIT is non-nil and the entry does not have the property,
  14195. then also check higher levels of the hierarchy.
  14196. If INHERIT is the symbol `selective', use inheritance only if the setting
  14197. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  14198. If the property is present but empty, the return value is the empty string.
  14199. If the property is not present at all, nil is returned.
  14200. Return the value as a string.
  14201. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  14202. do not interpret it as the list atom nil. This is used for inheritance
  14203. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  14204. (org-with-point-at pom
  14205. (if (and inherit (if (eq inherit 'selective)
  14206. (org-property-inherit-p property)
  14207. t))
  14208. (org-entry-get-with-inheritance property literal-nil)
  14209. (if (member property org-special-properties)
  14210. ;; We need a special property. Use `org-entry-properties'
  14211. ;; to retrieve it, but specify the wanted property
  14212. (cdr (assoc property (org-entry-properties nil 'special property)))
  14213. (org-with-wide-buffer
  14214. (let ((range (org-get-property-block)))
  14215. (when (and range (not (eq (car range) (cdr range)))
  14216. (save-excursion
  14217. (goto-char (car range))
  14218. (re-search-forward
  14219. (concat (org-re-property property) "\\|"
  14220. (org-re-property (concat property "+")))
  14221. (cdr range) t)))
  14222. (let* ((props
  14223. (list (or (assoc property org-file-properties)
  14224. (assoc property org-global-properties)
  14225. (assoc property org-global-properties-fixed))))
  14226. (ap (lambda (key)
  14227. (when (re-search-forward
  14228. (org-re-property key) (cdr range) t)
  14229. (setq props
  14230. (org-update-property-plist
  14231. key
  14232. (if (match-end 3)
  14233. (org-match-string-no-properties 3) "")
  14234. props)))))
  14235. val)
  14236. (goto-char (car range))
  14237. (funcall ap property)
  14238. (goto-char (car range))
  14239. (while (funcall ap (concat property "+")))
  14240. (setq val (cdr (assoc property props)))
  14241. (when val (if literal-nil val (org-not-nil val)))))))))))
  14242. (defun org-property-or-variable-value (var &optional inherit)
  14243. "Check if there is a property fixing the value of VAR.
  14244. If yes, return this value. If not, return the current value of the variable."
  14245. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14246. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14247. (read prop)
  14248. (symbol-value var))))
  14249. (defun org-entry-delete (pom property)
  14250. "Delete the property PROPERTY from entry at point-or-marker POM."
  14251. (unless (member property org-special-properties)
  14252. (org-with-point-at pom
  14253. (let ((range (org-get-property-block)))
  14254. (if (and range
  14255. (goto-char (car range))
  14256. (re-search-forward
  14257. (org-re-property property)
  14258. (cdr range) t))
  14259. (progn
  14260. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14261. (org-remove-empty-drawer-at (car range))
  14262. t)
  14263. nil)))))
  14264. ;; Multi-values properties are properties that contain multiple values
  14265. ;; These values are assumed to be single words, separated by whitespace.
  14266. (defun org-entry-add-to-multivalued-property (pom property value)
  14267. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14268. (let* ((old (org-entry-get pom property))
  14269. (values (and old (org-split-string old "[ \t]"))))
  14270. (setq value (org-entry-protect-space value))
  14271. (unless (member value values)
  14272. (setq values (append values (list value)))
  14273. (org-entry-put pom property
  14274. (mapconcat 'identity values " ")))))
  14275. (defun org-entry-remove-from-multivalued-property (pom property value)
  14276. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14277. (let* ((old (org-entry-get pom property))
  14278. (values (and old (org-split-string old "[ \t]"))))
  14279. (setq value (org-entry-protect-space value))
  14280. (when (member value values)
  14281. (setq values (delete value values))
  14282. (org-entry-put pom property
  14283. (mapconcat 'identity values " ")))))
  14284. (defun org-entry-member-in-multivalued-property (pom property value)
  14285. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14286. (let* ((old (org-entry-get pom property))
  14287. (values (and old (org-split-string old "[ \t]"))))
  14288. (setq value (org-entry-protect-space value))
  14289. (member value values)))
  14290. (defun org-entry-get-multivalued-property (pom property)
  14291. "Return a list of values in a multivalued property."
  14292. (let* ((value (org-entry-get pom property))
  14293. (values (and value (org-split-string value "[ \t]"))))
  14294. (mapcar 'org-entry-restore-space values)))
  14295. (defun org-entry-put-multivalued-property (pom property &rest values)
  14296. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14297. VALUES should be a list of strings. Spaces will be protected."
  14298. (org-entry-put pom property
  14299. (mapconcat 'org-entry-protect-space values " "))
  14300. (let* ((value (org-entry-get pom property))
  14301. (values (and value (org-split-string value "[ \t]"))))
  14302. (mapcar 'org-entry-restore-space values)))
  14303. (defun org-entry-protect-space (s)
  14304. "Protect spaces and newline in string S."
  14305. (while (string-match " " s)
  14306. (setq s (replace-match "%20" t t s)))
  14307. (while (string-match "\n" s)
  14308. (setq s (replace-match "%0A" t t s)))
  14309. s)
  14310. (defun org-entry-restore-space (s)
  14311. "Restore spaces and newline in string S."
  14312. (while (string-match "%20" s)
  14313. (setq s (replace-match " " t t s)))
  14314. (while (string-match "%0A" s)
  14315. (setq s (replace-match "\n" t t s)))
  14316. s)
  14317. (defvar org-entry-property-inherited-from (make-marker)
  14318. "Marker pointing to the entry from where a property was inherited.
  14319. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14320. location of the entry where the inheritance search matched. If there was
  14321. no match, the marker will point nowhere.
  14322. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14323. is set.")
  14324. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14325. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14326. The search will stop at the first ancestor which has the property defined.
  14327. If the value found is \"nil\", return nil to show that the property
  14328. should be considered as undefined (this is the meaning of nil here).
  14329. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14330. (move-marker org-entry-property-inherited-from nil)
  14331. (let (tmp)
  14332. (save-excursion
  14333. (save-restriction
  14334. (widen)
  14335. (catch 'ex
  14336. (while t
  14337. (when (setq tmp (org-entry-get nil property nil literal-nil))
  14338. (or (ignore-errors (org-back-to-heading t))
  14339. (goto-char (point-min)))
  14340. (move-marker org-entry-property-inherited-from (point))
  14341. (throw 'ex tmp))
  14342. (or (ignore-errors (org-up-heading-safe))
  14343. (throw 'ex nil))))))
  14344. (setq tmp (or tmp
  14345. (cdr (assoc property org-file-properties))
  14346. (cdr (assoc property org-global-properties))
  14347. (cdr (assoc property org-global-properties-fixed))))
  14348. (if literal-nil tmp (org-not-nil tmp))))
  14349. (defvar org-property-changed-functions nil
  14350. "Hook called when the value of a property has changed.
  14351. Each hook function should accept two arguments, the name of the property
  14352. and the new value.")
  14353. (defun org-entry-put (pom property value)
  14354. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14355. If the value is `nil', it is converted to the empty string.
  14356. If it is not a string, an error is raised."
  14357. (cond ((null value) (setq value ""))
  14358. ((not (stringp value))
  14359. (error "Properties values should be strings.")))
  14360. (org-with-point-at pom
  14361. (org-back-to-heading t)
  14362. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14363. range)
  14364. (cond
  14365. ((equal property "TODO")
  14366. (when (and (string-match "\\S-" value)
  14367. (not (member value org-todo-keywords-1)))
  14368. (user-error "\"%s\" is not a valid TODO state" value))
  14369. (if (or (not value)
  14370. (not (string-match "\\S-" value)))
  14371. (setq value 'none))
  14372. (org-todo value)
  14373. (org-set-tags nil 'align))
  14374. ((equal property "PRIORITY")
  14375. (org-priority (if (and value (string-match "\\S-" value))
  14376. (string-to-char value) ?\ ))
  14377. (org-set-tags nil 'align))
  14378. ((equal property "CLOCKSUM")
  14379. (if (not (re-search-forward
  14380. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14381. (error "Cannot find a clock log")
  14382. (goto-char (- (match-end 1) 2))
  14383. (cond
  14384. ((eq value 'earlier) (org-timestamp-down))
  14385. ((eq value 'later) (org-timestamp-up)))
  14386. (org-clock-sum-current-item)))
  14387. ((equal property "SCHEDULED")
  14388. (if (re-search-forward org-scheduled-time-regexp end t)
  14389. (cond
  14390. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14391. ((eq value 'later) (org-timestamp-change 1 'day))
  14392. (t (call-interactively 'org-schedule)))
  14393. (call-interactively 'org-schedule)))
  14394. ((equal property "DEADLINE")
  14395. (if (re-search-forward org-deadline-time-regexp end t)
  14396. (cond
  14397. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14398. ((eq value 'later) (org-timestamp-change 1 'day))
  14399. (t (call-interactively 'org-deadline)))
  14400. (call-interactively 'org-deadline)))
  14401. ((member property org-special-properties)
  14402. (error "The %s property can not yet be set with `org-entry-put'"
  14403. property))
  14404. (t ; a non-special property
  14405. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14406. (setq range (org-get-property-block beg end 'force))
  14407. (goto-char (car range))
  14408. (if (re-search-forward
  14409. (org-re-property property) (cdr range) t)
  14410. (progn
  14411. (delete-region (match-beginning 0) (match-end 0))
  14412. (goto-char (match-beginning 0)))
  14413. (goto-char (cdr range))
  14414. (insert "\n")
  14415. (backward-char 1)
  14416. (org-indent-line))
  14417. (insert ":" property ":")
  14418. (and value (insert " " value))
  14419. (org-indent-line)))))
  14420. (run-hook-with-args 'org-property-changed-functions property value)))
  14421. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14422. "Get all property keys in the current buffer.
  14423. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14424. like tags and TODO state.
  14425. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14426. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14427. and others.
  14428. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14429. formats in the current buffer."
  14430. (let (rtn range cfmt s p)
  14431. (save-excursion
  14432. (save-restriction
  14433. (widen)
  14434. (goto-char (point-min))
  14435. (while (re-search-forward org-property-start-re nil t)
  14436. (catch 'cont
  14437. (setq range (or (org-get-property-block)
  14438. (if (y-or-n-p
  14439. (format "Malformed drawer at %d, repair?" (point)))
  14440. (org-get-property-block nil nil t)
  14441. (throw 'cont nil))))
  14442. (goto-char (car range))
  14443. (while (re-search-forward org-property-re
  14444. (cdr range) t)
  14445. (add-to-list 'rtn (org-match-string-no-properties 2)))
  14446. (outline-next-heading)))))
  14447. (when include-specials
  14448. (setq rtn (append org-special-properties rtn)))
  14449. (when include-defaults
  14450. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14451. (add-to-list 'rtn org-effort-property))
  14452. (when include-columns
  14453. (save-excursion
  14454. (save-restriction
  14455. (widen)
  14456. (goto-char (point-min))
  14457. (while (re-search-forward
  14458. "^[ \t]*\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14459. nil t)
  14460. (setq cfmt (match-string 2) s 0)
  14461. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14462. cfmt s)
  14463. (setq s (match-end 0)
  14464. p (match-string 1 cfmt))
  14465. (unless (or (equal p "ITEM")
  14466. (member p org-special-properties))
  14467. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14468. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14469. (defun org-property-values (key)
  14470. "Return a list of all values of property KEY in the current buffer."
  14471. (save-excursion
  14472. (save-restriction
  14473. (widen)
  14474. (goto-char (point-min))
  14475. (let ((re (org-re-property key))
  14476. values)
  14477. (while (re-search-forward re nil t)
  14478. (add-to-list 'values (org-trim (match-string 3))))
  14479. (delete "" values)))))
  14480. (defun org-insert-property-drawer ()
  14481. "Insert a property drawer into the current entry."
  14482. (org-back-to-heading t)
  14483. (looking-at org-outline-regexp)
  14484. (let ((indent (if org-adapt-indentation
  14485. (- (match-end 0) (match-beginning 0))
  14486. 0))
  14487. (beg (point))
  14488. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14489. end hiddenp)
  14490. (outline-next-heading)
  14491. (setq end (point))
  14492. (goto-char beg)
  14493. (while (re-search-forward re end t))
  14494. (setq hiddenp (outline-invisible-p))
  14495. (end-of-line 1)
  14496. (and (equal (char-after) ?\n) (forward-char 1))
  14497. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14498. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14499. ;; just skip this line
  14500. (beginning-of-line 2)
  14501. ;; Drawer start, find the end
  14502. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14503. (beginning-of-line 1)))
  14504. (org-skip-over-state-notes)
  14505. (skip-chars-backward " \t\n\r")
  14506. (if (and (eq (char-before) ?*) (not (eq (char-after) ?\n)))
  14507. (forward-char 1))
  14508. (goto-char (point-at-eol))
  14509. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14510. (beginning-of-line 0)
  14511. (org-indent-to-column indent)
  14512. (beginning-of-line 2)
  14513. (org-indent-to-column indent)
  14514. (beginning-of-line 0)
  14515. (if hiddenp
  14516. (save-excursion
  14517. (org-back-to-heading t)
  14518. (hide-entry))
  14519. (org-flag-drawer t))))
  14520. (defun org-insert-drawer (&optional arg drawer)
  14521. "Insert a drawer at point.
  14522. Optional argument DRAWER, when non-nil, is a string representing
  14523. drawer's name. Otherwise, the user is prompted for a name.
  14524. If a region is active, insert the drawer around that region
  14525. instead.
  14526. Point is left between drawer's boundaries."
  14527. (interactive "P")
  14528. (let* ((drawer (if arg "PROPERTIES"
  14529. (or drawer (read-from-minibuffer "Drawer: ")))))
  14530. (cond
  14531. ;; With C-u, fall back on `org-insert-property-drawer'
  14532. (arg (org-insert-property-drawer))
  14533. ;;
  14534. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14535. (user-error "Invalid drawer name"))
  14536. ;; With an active region, insert a drawer at point.
  14537. ((not (org-region-active-p))
  14538. (progn
  14539. (unless (bolp) (insert "\n"))
  14540. (insert (format ":%s:\n\n:END:\n" drawer))
  14541. (forward-line -2)))
  14542. ;; Otherwise, insert the drawer at point
  14543. (t
  14544. (let ((rbeg (region-beginning))
  14545. (rend (copy-marker (region-end))))
  14546. (unwind-protect
  14547. (progn
  14548. (goto-char rbeg)
  14549. (beginning-of-line)
  14550. (when (save-excursion
  14551. (re-search-forward org-outline-regexp-bol rend t))
  14552. (user-error "Drawers cannot contain headlines"))
  14553. ;; Position point at the beginning of the first
  14554. ;; non-blank line in region. Insert drawer's opening
  14555. ;; there, then indent it.
  14556. (org-skip-whitespace)
  14557. (beginning-of-line)
  14558. (insert ":" drawer ":\n")
  14559. (forward-line -1)
  14560. (indent-for-tab-command)
  14561. ;; Move point to the beginning of the first blank line
  14562. ;; after the last non-blank line in region. Insert
  14563. ;; drawer's closing, then indent it.
  14564. (goto-char rend)
  14565. (skip-chars-backward " \r\t\n")
  14566. (insert "\n:END:")
  14567. (deactivate-mark t)
  14568. (indent-for-tab-command)
  14569. (unless (eolp) (insert "\n")))
  14570. ;; Clear marker, whatever the outcome of insertion is.
  14571. (set-marker rend nil)))))))
  14572. (defvar org-property-set-functions-alist nil
  14573. "Property set function alist.
  14574. Each entry should have the following format:
  14575. (PROPERTY . READ-FUNCTION)
  14576. The read function will be called with the same argument as
  14577. `org-completing-read'.")
  14578. (defun org-set-property-function (property)
  14579. "Get the function that should be used to set PROPERTY.
  14580. This is computed according to `org-property-set-functions-alist'."
  14581. (or (cdr (assoc property org-property-set-functions-alist))
  14582. 'org-completing-read))
  14583. (defun org-read-property-value (property)
  14584. "Read PROPERTY value from user."
  14585. (let* ((completion-ignore-case t)
  14586. (allowed (org-property-get-allowed-values nil property 'table))
  14587. (cur (org-entry-get nil property))
  14588. (prompt (concat property " value"
  14589. (if (and cur (string-match "\\S-" cur))
  14590. (concat " [" cur "]") "") ": "))
  14591. (set-function (org-set-property-function property))
  14592. (val (if allowed
  14593. (funcall set-function prompt allowed nil
  14594. (not (get-text-property 0 'org-unrestricted
  14595. (caar allowed))))
  14596. (let (org-completion-use-ido org-completion-use-iswitchb)
  14597. (funcall set-function prompt
  14598. (mapcar 'list (org-property-values property))
  14599. nil nil "" nil cur)))))
  14600. (if (equal val "")
  14601. cur
  14602. val)))
  14603. (defvar org-last-set-property nil)
  14604. (defvar org-last-set-property-value nil)
  14605. (defun org-read-property-name ()
  14606. "Read a property name."
  14607. (let* ((completion-ignore-case t)
  14608. (keys (org-buffer-property-keys nil t t))
  14609. (default-prop (or (save-excursion
  14610. (save-match-data
  14611. (beginning-of-line)
  14612. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14613. (null (string= (match-string 1) "END"))
  14614. (match-string 1))))
  14615. org-last-set-property))
  14616. (property (org-icompleting-read
  14617. (concat "Property"
  14618. (if default-prop (concat " [" default-prop "]") "")
  14619. ": ")
  14620. (mapcar 'list keys)
  14621. nil nil nil nil
  14622. default-prop)))
  14623. (if (member property keys)
  14624. property
  14625. (or (cdr (assoc (downcase property)
  14626. (mapcar (lambda (x) (cons (downcase x) x))
  14627. keys)))
  14628. property))))
  14629. (defun org-set-property-and-value (use-last)
  14630. "Allow to set [PROPERTY]: [value] direction from prompt.
  14631. When use-default, don't even ask, just use the last
  14632. \"[PROPERTY]: [value]\" string from the history."
  14633. (interactive "P")
  14634. (let* ((completion-ignore-case t)
  14635. (pv (or (and use-last org-last-set-property-value)
  14636. (org-completing-read
  14637. "Enter a \"[Property]: [value]\" pair: "
  14638. nil nil nil nil nil
  14639. org-last-set-property-value)))
  14640. prop val)
  14641. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14642. (setq prop (match-string 1 pv)
  14643. val (match-string 2 pv))
  14644. (org-set-property prop val))))
  14645. (defun org-set-property (property value)
  14646. "In the current entry, set PROPERTY to VALUE.
  14647. When called interactively, this will prompt for a property name, offering
  14648. completion on existing and default properties. And then it will prompt
  14649. for a value, offering completion either on allowed values (via an inherited
  14650. xxx_ALL property) or on existing values in other instances of this property
  14651. in the current file."
  14652. (interactive (list nil nil))
  14653. (let* ((property (or property (org-read-property-name)))
  14654. (value (or value (org-read-property-value property)))
  14655. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14656. (setq org-last-set-property property)
  14657. (setq org-last-set-property-value (concat property ": " value))
  14658. ;; Possibly postprocess the inserted value:
  14659. (when fn (setq value (funcall fn value)))
  14660. (unless (equal (org-entry-get nil property) value)
  14661. (org-entry-put nil property value))))
  14662. (defun org-delete-property (property)
  14663. "In the current entry, delete PROPERTY."
  14664. (interactive
  14665. (let* ((completion-ignore-case t)
  14666. (cat (org-entry-get (point) "CATEGORY"))
  14667. (props0 (org-entry-properties nil 'standard))
  14668. (props (if cat props0
  14669. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14670. (prop (if (< 1 (length props))
  14671. (org-icompleting-read "Property: " props nil t)
  14672. (caar props))))
  14673. (list prop)))
  14674. (if (org-entry-delete nil property)
  14675. (message "Property %s deleted" property)))
  14676. (defun org-delete-property-globally (property)
  14677. "Remove PROPERTY globally, from all entries."
  14678. (interactive
  14679. (let* ((completion-ignore-case t)
  14680. (prop (org-icompleting-read
  14681. "Globally remove property: "
  14682. (mapcar 'list (org-buffer-property-keys)))))
  14683. (list prop)))
  14684. (save-excursion
  14685. (save-restriction
  14686. (widen)
  14687. (goto-char (point-min))
  14688. (let ((cnt 0))
  14689. (while (re-search-forward
  14690. (org-re-property property)
  14691. nil t)
  14692. (setq cnt (1+ cnt))
  14693. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14694. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14695. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14696. (defun org-compute-property-at-point ()
  14697. "Compute the property at point.
  14698. This looks for an enclosing column format, extracts the operator and
  14699. then applies it to the property in the column format's scope."
  14700. (interactive)
  14701. (unless (org-at-property-p)
  14702. (user-error "Not at a property"))
  14703. (let ((prop (org-match-string-no-properties 2)))
  14704. (org-columns-get-format-and-top-level)
  14705. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14706. (user-error "No operator defined for property %s" prop))
  14707. (org-columns-compute prop)))
  14708. (defvar org-property-allowed-value-functions nil
  14709. "Hook for functions supplying allowed values for a specific property.
  14710. The functions must take a single argument, the name of the property, and
  14711. return a flat list of allowed values. If \":ETC\" is one of
  14712. the values, this means that these values are intended as defaults for
  14713. completion, but that other values should be allowed too.
  14714. The functions must return nil if they are not responsible for this
  14715. property.")
  14716. (defun org-property-get-allowed-values (pom property &optional table)
  14717. "Get allowed values for the property PROPERTY.
  14718. When TABLE is non-nil, return an alist that can directly be used for
  14719. completion."
  14720. (let (vals)
  14721. (cond
  14722. ((equal property "TODO")
  14723. (setq vals (org-with-point-at pom
  14724. (append org-todo-keywords-1 '("")))))
  14725. ((equal property "PRIORITY")
  14726. (let ((n org-lowest-priority))
  14727. (while (>= n org-highest-priority)
  14728. (push (char-to-string n) vals)
  14729. (setq n (1- n)))))
  14730. ((member property org-special-properties))
  14731. ((setq vals (run-hook-with-args-until-success
  14732. 'org-property-allowed-value-functions property)))
  14733. (t
  14734. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14735. (when (and vals (string-match "\\S-" vals))
  14736. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14737. (setq vals (mapcar (lambda (x)
  14738. (cond ((stringp x) x)
  14739. ((numberp x) (number-to-string x))
  14740. ((symbolp x) (symbol-name x))
  14741. (t "???")))
  14742. vals)))))
  14743. (when (member ":ETC" vals)
  14744. (setq vals (remove ":ETC" vals))
  14745. (org-add-props (car vals) '(org-unrestricted t)))
  14746. (if table (mapcar 'list vals) vals)))
  14747. (defun org-property-previous-allowed-value (&optional previous)
  14748. "Switch to the next allowed value for this property."
  14749. (interactive)
  14750. (org-property-next-allowed-value t))
  14751. (defun org-property-next-allowed-value (&optional previous)
  14752. "Switch to the next allowed value for this property."
  14753. (interactive)
  14754. (unless (org-at-property-p)
  14755. (user-error "Not at a property"))
  14756. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14757. (key (match-string 2))
  14758. (value (match-string 3))
  14759. (allowed (or (org-property-get-allowed-values (point) key)
  14760. (and (member value '("[ ]" "[-]" "[X]"))
  14761. '("[ ]" "[X]"))))
  14762. (heading (save-match-data (nth 4 (org-heading-components))))
  14763. nval)
  14764. (unless allowed
  14765. (user-error "Allowed values for this property have not been defined"))
  14766. (if previous (setq allowed (reverse allowed)))
  14767. (if (member value allowed)
  14768. (setq nval (car (cdr (member value allowed)))))
  14769. (setq nval (or nval (car allowed)))
  14770. (if (equal nval value)
  14771. (user-error "Only one allowed value for this property"))
  14772. (org-at-property-p)
  14773. (replace-match (concat " :" key ": " nval) t t)
  14774. (org-indent-line)
  14775. (beginning-of-line 1)
  14776. (skip-chars-forward " \t")
  14777. (when (equal prop org-effort-property)
  14778. (org-refresh-property
  14779. '((effort . identity)
  14780. (effort-minutes . org-duration-string-to-minutes))
  14781. nval)
  14782. (when (string= org-clock-current-task heading)
  14783. (setq org-clock-effort nval)
  14784. (org-clock-update-mode-line)))
  14785. (run-hook-with-args 'org-property-changed-functions key nval)))
  14786. (defun org-find-olp (path &optional this-buffer)
  14787. "Return a marker pointing to the entry at outline path OLP.
  14788. If anything goes wrong, throw an error.
  14789. You can wrap this call to catch the error like this:
  14790. (condition-case msg
  14791. (org-mobile-locate-entry (match-string 4))
  14792. (error (nth 1 msg)))
  14793. The return value will then be either a string with the error message,
  14794. or a marker if everything is OK.
  14795. If THIS-BUFFER is set, the outline path does not contain a file,
  14796. only headings."
  14797. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14798. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14799. (level 1)
  14800. (lmin 1)
  14801. (lmax 1)
  14802. limit re end found pos heading cnt flevel)
  14803. (unless buffer (error "File not found :%s" file))
  14804. (with-current-buffer buffer
  14805. (save-excursion
  14806. (save-restriction
  14807. (widen)
  14808. (setq limit (point-max))
  14809. (goto-char (point-min))
  14810. (while (setq heading (pop path))
  14811. (setq re (format org-complex-heading-regexp-format
  14812. (regexp-quote heading)))
  14813. (setq cnt 0 pos (point))
  14814. (while (re-search-forward re end t)
  14815. (setq level (- (match-end 1) (match-beginning 1)))
  14816. (if (and (>= level lmin) (<= level lmax))
  14817. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14818. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14819. lmax heading))
  14820. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14821. lmax heading))
  14822. (goto-char found)
  14823. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14824. (setq end (save-excursion (org-end-of-subtree t t))))
  14825. (when (org-at-heading-p)
  14826. (point-marker)))))))
  14827. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14828. "Find node HEADING in BUFFER.
  14829. Return a marker to the heading if it was found, or nil if not.
  14830. If POS-ONLY is set, return just the position instead of a marker.
  14831. The heading text must match exact, but it may have a TODO keyword,
  14832. a priority cookie and tags in the standard locations."
  14833. (with-current-buffer (or buffer (current-buffer))
  14834. (save-excursion
  14835. (save-restriction
  14836. (widen)
  14837. (goto-char (point-min))
  14838. (let (case-fold-search)
  14839. (if (re-search-forward
  14840. (format org-complex-heading-regexp-format
  14841. (regexp-quote heading)) nil t)
  14842. (if pos-only
  14843. (match-beginning 0)
  14844. (move-marker (make-marker) (match-beginning 0)))))))))
  14845. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14846. "Find Org node headline HEADING in all .org files in directory DIR.
  14847. When the target headline is found, return a marker to this location."
  14848. (let ((files (directory-files (or dir default-directory)
  14849. t "\\`[^.#].*\\.org\\'"))
  14850. file visiting m buffer)
  14851. (catch 'found
  14852. (while (setq file (pop files))
  14853. (message "trying %s" file)
  14854. (setq visiting (org-find-base-buffer-visiting file))
  14855. (setq buffer (or visiting (find-file-noselect file)))
  14856. (setq m (org-find-exact-headline-in-buffer
  14857. heading buffer))
  14858. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14859. (and m (throw 'found m))))))
  14860. (defun org-find-entry-with-id (ident)
  14861. "Locate the entry that contains the ID property with exact value IDENT.
  14862. IDENT can be a string, a symbol or a number, this function will search for
  14863. the string representation of it.
  14864. Return the position where this entry starts, or nil if there is no such entry."
  14865. (interactive "sID: ")
  14866. (let ((id (cond
  14867. ((stringp ident) ident)
  14868. ((symbol-name ident) (symbol-name ident))
  14869. ((numberp ident) (number-to-string ident))
  14870. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14871. (case-fold-search nil))
  14872. (save-excursion
  14873. (save-restriction
  14874. (widen)
  14875. (goto-char (point-min))
  14876. (when (re-search-forward
  14877. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14878. nil t)
  14879. (org-back-to-heading t)
  14880. (point))))))
  14881. ;;;; Timestamps
  14882. (defvar org-last-changed-timestamp nil)
  14883. (defvar org-last-inserted-timestamp nil
  14884. "The last time stamp inserted with `org-insert-time-stamp'.")
  14885. (defvar org-ts-what) ; dynamically scoped parameter
  14886. (defun org-time-stamp (arg &optional inactive)
  14887. "Prompt for a date/time and insert a time stamp.
  14888. If the user specifies a time like HH:MM or if this command is
  14889. called with at least one prefix argument, the time stamp contains
  14890. the date and the time. Otherwise, only the date is be included.
  14891. All parts of a date not specified by the user is filled in from
  14892. the current date/time. So if you just press return without
  14893. typing anything, the time stamp will represent the current
  14894. date/time.
  14895. If there is already a timestamp at the cursor, it will be
  14896. modified.
  14897. With two universal prefix arguments, insert an active timestamp
  14898. with the current time without prompting the user.
  14899. When called from lisp, the timestamp is inactive if INACTIVE is
  14900. non-nil."
  14901. (interactive "P")
  14902. (let* ((ts nil)
  14903. (default-time
  14904. ;; Default time is either today, or, when entering a range,
  14905. ;; the range start.
  14906. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14907. (save-excursion
  14908. (re-search-backward
  14909. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14910. (- (point) 20) t)))
  14911. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14912. (current-time)))
  14913. (default-input (and ts (org-get-compact-tod ts)))
  14914. (repeater (save-excursion
  14915. (save-match-data
  14916. (beginning-of-line)
  14917. (when (re-search-forward
  14918. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14919. (save-excursion (progn (end-of-line) (point))) t)
  14920. (match-string 0)))))
  14921. org-time-was-given org-end-time-was-given time)
  14922. (cond
  14923. ((and (org-at-timestamp-p t)
  14924. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14925. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14926. (insert "--")
  14927. (setq time (let ((this-command this-command))
  14928. (org-read-date arg 'totime nil nil
  14929. default-time default-input inactive)))
  14930. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14931. ((org-at-timestamp-p t)
  14932. (setq time (let ((this-command this-command))
  14933. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14934. (when (org-at-timestamp-p t) ; just to get the match data
  14935. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14936. (replace-match "")
  14937. (setq org-last-changed-timestamp
  14938. (org-insert-time-stamp
  14939. time (or org-time-was-given arg)
  14940. inactive nil nil (list org-end-time-was-given)))
  14941. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14942. (setq org-last-changed-timestamp
  14943. (concat (substring org-last-inserted-timestamp 0 -1)
  14944. " " repeater ">"))))
  14945. (message "Timestamp updated"))
  14946. ((equal arg '(16))
  14947. (org-insert-time-stamp (current-time) t inactive))
  14948. (t
  14949. (setq time (let ((this-command this-command))
  14950. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14951. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14952. nil nil (list org-end-time-was-given))))))
  14953. ;; FIXME: can we use this for something else, like computing time differences?
  14954. (defun org-get-compact-tod (s)
  14955. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14956. (let* ((t1 (match-string 1 s))
  14957. (h1 (string-to-number (match-string 2 s)))
  14958. (m1 (string-to-number (match-string 3 s)))
  14959. (t2 (and (match-end 4) (match-string 5 s)))
  14960. (h2 (and t2 (string-to-number (match-string 6 s))))
  14961. (m2 (and t2 (string-to-number (match-string 7 s))))
  14962. dh dm)
  14963. (if (not t2)
  14964. t1
  14965. (setq dh (- h2 h1) dm (- m2 m1))
  14966. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14967. (concat t1 "+" (number-to-string dh)
  14968. (and (/= 0 dm) (format ":%02d" dm)))))))
  14969. (defun org-time-stamp-inactive (&optional arg)
  14970. "Insert an inactive time stamp.
  14971. An inactive time stamp is enclosed in square brackets instead of angle
  14972. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14973. does not link to the calendar and cannot be changed with the S-cursor keys.
  14974. So these are more for recording a certain time/date."
  14975. (interactive "P")
  14976. (org-time-stamp arg 'inactive))
  14977. (defvar org-date-ovl (make-overlay 1 1))
  14978. (overlay-put org-date-ovl 'face 'org-date-selected)
  14979. (org-detach-overlay org-date-ovl)
  14980. (defvar org-ans1) ; dynamically scoped parameter
  14981. (defvar org-ans2) ; dynamically scoped parameter
  14982. (defvar org-plain-time-of-day-regexp) ; defined below
  14983. (defvar org-overriding-default-time nil) ; dynamically scoped
  14984. (defvar org-read-date-overlay nil)
  14985. (defvar org-dcst nil) ; dynamically scoped
  14986. (defvar org-read-date-history nil)
  14987. (defvar org-read-date-final-answer nil)
  14988. (defvar org-read-date-analyze-futurep nil)
  14989. (defvar org-read-date-analyze-forced-year nil)
  14990. (defvar org-read-date-inactive)
  14991. (defvar org-read-date-minibuffer-local-map
  14992. (let* ((org-replace-disputed-keys nil)
  14993. (map (make-sparse-keymap)))
  14994. (set-keymap-parent map minibuffer-local-map)
  14995. (org-defkey map (kbd ".")
  14996. (lambda () (interactive)
  14997. ;; Are we at the beginning of the prompt?
  14998. (if (looking-back "^[^:]+: ")
  14999. (org-eval-in-calendar '(calendar-goto-today))
  15000. (insert "."))))
  15001. (org-defkey map (kbd "C-.")
  15002. (lambda () (interactive)
  15003. (org-eval-in-calendar '(calendar-goto-today))))
  15004. (org-defkey map [(meta shift left)]
  15005. (lambda () (interactive)
  15006. (org-eval-in-calendar '(calendar-backward-month 1))))
  15007. (org-defkey map [(meta shift right)]
  15008. (lambda () (interactive)
  15009. (org-eval-in-calendar '(calendar-forward-month 1))))
  15010. (org-defkey map [(meta shift up)]
  15011. (lambda () (interactive)
  15012. (org-eval-in-calendar '(calendar-backward-year 1))))
  15013. (org-defkey map [(meta shift down)]
  15014. (lambda () (interactive)
  15015. (org-eval-in-calendar '(calendar-forward-year 1))))
  15016. (org-defkey map [?\e (shift left)]
  15017. (lambda () (interactive)
  15018. (org-eval-in-calendar '(calendar-backward-month 1))))
  15019. (org-defkey map [?\e (shift right)]
  15020. (lambda () (interactive)
  15021. (org-eval-in-calendar '(calendar-forward-month 1))))
  15022. (org-defkey map [?\e (shift up)]
  15023. (lambda () (interactive)
  15024. (org-eval-in-calendar '(calendar-backward-year 1))))
  15025. (org-defkey map [?\e (shift down)]
  15026. (lambda () (interactive)
  15027. (org-eval-in-calendar '(calendar-forward-year 1))))
  15028. (org-defkey map [(shift up)]
  15029. (lambda () (interactive)
  15030. (org-eval-in-calendar '(calendar-backward-week 1))))
  15031. (org-defkey map [(shift down)]
  15032. (lambda () (interactive)
  15033. (org-eval-in-calendar '(calendar-forward-week 1))))
  15034. (org-defkey map [(shift left)]
  15035. (lambda () (interactive)
  15036. (org-eval-in-calendar '(calendar-backward-day 1))))
  15037. (org-defkey map [(shift right)]
  15038. (lambda () (interactive)
  15039. (org-eval-in-calendar '(calendar-forward-day 1))))
  15040. (org-defkey map "!"
  15041. (lambda () (interactive)
  15042. (org-eval-in-calendar '(diary-view-entries))
  15043. (message "")))
  15044. (org-defkey map ">"
  15045. (lambda () (interactive)
  15046. (org-eval-in-calendar '(scroll-calendar-left 1))))
  15047. (org-defkey map "<"
  15048. (lambda () (interactive)
  15049. (org-eval-in-calendar '(scroll-calendar-right 1))))
  15050. (org-defkey map "\C-v"
  15051. (lambda () (interactive)
  15052. (org-eval-in-calendar
  15053. '(calendar-scroll-left-three-months 1))))
  15054. (org-defkey map "\M-v"
  15055. (lambda () (interactive)
  15056. (org-eval-in-calendar
  15057. '(calendar-scroll-right-three-months 1))))
  15058. map)
  15059. "Keymap for minibuffer commands when using `org-read-date'.")
  15060. (defvar org-def)
  15061. (defvar org-defdecode)
  15062. (defvar org-with-time)
  15063. (defun org-read-date (&optional org-with-time to-time from-string prompt
  15064. default-time default-input inactive)
  15065. "Read a date, possibly a time, and make things smooth for the user.
  15066. The prompt will suggest to enter an ISO date, but you can also enter anything
  15067. which will at least partially be understood by `parse-time-string'.
  15068. Unrecognized parts of the date will default to the current day, month, year,
  15069. hour and minute. If this command is called to replace a timestamp at point,
  15070. or to enter the second timestamp of a range, the default time is taken
  15071. from the existing stamp. Furthermore, the command prefers the future,
  15072. so if you are giving a date where the year is not given, and the day-month
  15073. combination is already past in the current year, it will assume you
  15074. mean next year. For details, see the manual. A few examples:
  15075. 3-2-5 --> 2003-02-05
  15076. feb 15 --> currentyear-02-15
  15077. 2/15 --> currentyear-02-15
  15078. sep 12 9 --> 2009-09-12
  15079. 12:45 --> today 12:45
  15080. 22 sept 0:34 --> currentyear-09-22 0:34
  15081. 12 --> currentyear-currentmonth-12
  15082. Fri --> nearest Friday after today
  15083. -Tue --> last Tuesday
  15084. etc.
  15085. Furthermore you can specify a relative date by giving, as the *first* thing
  15086. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15087. change in days weeks, months, years.
  15088. With a single plus or minus, the date is relative to today. With a double
  15089. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15090. +4d --> four days from today
  15091. +4 --> same as above
  15092. +2w --> two weeks from today
  15093. ++5 --> five days from default date
  15094. The function understands only English month and weekday abbreviations.
  15095. While prompting, a calendar is popped up - you can also select the
  15096. date with the mouse (button 1). The calendar shows a period of three
  15097. months. To scroll it to other months, use the keys `>' and `<'.
  15098. If you don't like the calendar, turn it off with
  15099. \(setq org-read-date-popup-calendar nil)
  15100. With optional argument TO-TIME, the date will immediately be converted
  15101. to an internal time.
  15102. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  15103. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  15104. still enter a time, and this function will inform the calling routine
  15105. about this change. The calling routine may then choose to change the
  15106. format used to insert the time stamp into the buffer to include the time.
  15107. With optional argument FROM-STRING, read from this string instead from
  15108. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15109. the time/date that is used for everything that is not specified by the
  15110. user."
  15111. (require 'parse-time)
  15112. (let* ((org-time-stamp-rounding-minutes
  15113. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  15114. (org-dcst org-display-custom-times)
  15115. (ct (org-current-time))
  15116. (org-def (or org-overriding-default-time default-time ct))
  15117. (org-defdecode (decode-time org-def))
  15118. (dummy (progn
  15119. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15120. (setcar (nthcdr 2 org-defdecode) -1)
  15121. (setcar (nthcdr 1 org-defdecode) 59)
  15122. (setq org-def (apply 'encode-time org-defdecode)
  15123. org-defdecode (decode-time org-def)))))
  15124. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15125. (calendar-frame-setup nil)
  15126. (calendar-setup nil)
  15127. (calendar-move-hook nil)
  15128. (calendar-view-diary-initially-flag nil)
  15129. (calendar-view-holidays-initially-flag nil)
  15130. (timestr (format-time-string
  15131. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15132. (prompt (concat (if prompt (concat prompt " ") "")
  15133. (format "Date+time [%s]: " timestr)))
  15134. ans (org-ans0 "") org-ans1 org-ans2 final)
  15135. (cond
  15136. (from-string (setq ans from-string))
  15137. (org-read-date-popup-calendar
  15138. (save-excursion
  15139. (save-window-excursion
  15140. (calendar)
  15141. (org-eval-in-calendar '(setq cursor-type nil) t)
  15142. (unwind-protect
  15143. (progn
  15144. (calendar-forward-day (- (time-to-days org-def)
  15145. (calendar-absolute-from-gregorian
  15146. (calendar-current-date))))
  15147. (org-eval-in-calendar nil t)
  15148. (let* ((old-map (current-local-map))
  15149. (map (copy-keymap calendar-mode-map))
  15150. (minibuffer-local-map
  15151. (copy-keymap org-read-date-minibuffer-local-map)))
  15152. (org-defkey map (kbd "RET") 'org-calendar-select)
  15153. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15154. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15155. (unwind-protect
  15156. (progn
  15157. (use-local-map map)
  15158. (setq org-read-date-inactive inactive)
  15159. (add-hook 'post-command-hook 'org-read-date-display)
  15160. (setq org-ans0 (read-string prompt default-input
  15161. 'org-read-date-history nil))
  15162. ;; org-ans0: from prompt
  15163. ;; org-ans1: from mouse click
  15164. ;; org-ans2: from calendar motion
  15165. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15166. (remove-hook 'post-command-hook 'org-read-date-display)
  15167. (use-local-map old-map)
  15168. (when org-read-date-overlay
  15169. (delete-overlay org-read-date-overlay)
  15170. (setq org-read-date-overlay nil)))))
  15171. (bury-buffer "*Calendar*")))))
  15172. (t ; Naked prompt only
  15173. (unwind-protect
  15174. (setq ans (read-string prompt default-input
  15175. 'org-read-date-history timestr))
  15176. (when org-read-date-overlay
  15177. (delete-overlay org-read-date-overlay)
  15178. (setq org-read-date-overlay nil)))))
  15179. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15180. (when org-read-date-analyze-forced-year
  15181. (message "Year was forced into %s"
  15182. (if org-read-date-force-compatible-dates
  15183. "compatible range (1970-2037)"
  15184. "range representable on this machine"))
  15185. (ding))
  15186. ;; One round trip to get rid of 34th of August and stuff like that....
  15187. (setq final (decode-time (apply 'encode-time final)))
  15188. (setq org-read-date-final-answer ans)
  15189. (if to-time
  15190. (apply 'encode-time final)
  15191. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15192. (format "%04d-%02d-%02d %02d:%02d"
  15193. (nth 5 final) (nth 4 final) (nth 3 final)
  15194. (nth 2 final) (nth 1 final))
  15195. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15196. (defun org-read-date-display ()
  15197. "Display the current date prompt interpretation in the minibuffer."
  15198. (when org-read-date-display-live
  15199. (when org-read-date-overlay
  15200. (delete-overlay org-read-date-overlay))
  15201. (when (minibufferp (current-buffer))
  15202. (save-excursion
  15203. (end-of-line 1)
  15204. (while (not (equal (buffer-substring
  15205. (max (point-min) (- (point) 4)) (point))
  15206. " "))
  15207. (insert " ")))
  15208. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15209. " " (or org-ans1 org-ans2)))
  15210. (org-end-time-was-given nil)
  15211. (f (org-read-date-analyze ans org-def org-defdecode))
  15212. (fmts (if org-dcst
  15213. org-time-stamp-custom-formats
  15214. org-time-stamp-formats))
  15215. (fmt (if (or org-with-time
  15216. (and (boundp 'org-time-was-given) org-time-was-given))
  15217. (cdr fmts)
  15218. (car fmts)))
  15219. (txt (format-time-string fmt (apply 'encode-time f)))
  15220. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15221. (txt (concat "=> " txt)))
  15222. (when (and org-end-time-was-given
  15223. (string-match org-plain-time-of-day-regexp txt))
  15224. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15225. org-end-time-was-given
  15226. (substring txt (match-end 0)))))
  15227. (when org-read-date-analyze-futurep
  15228. (setq txt (concat txt " (=>F)")))
  15229. (setq org-read-date-overlay
  15230. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15231. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15232. (defun org-read-date-analyze (ans org-def org-defdecode)
  15233. "Analyze the combined answer of the date prompt."
  15234. ;; FIXME: cleanup and comment
  15235. (let ((nowdecode (decode-time (current-time)))
  15236. delta deltan deltaw deltadef year month day
  15237. hour minute second wday pm h2 m2 tl wday1
  15238. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15239. (setq org-read-date-analyze-futurep nil
  15240. org-read-date-analyze-forced-year nil)
  15241. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15242. (setq ans "+0"))
  15243. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15244. (setq ans (replace-match "" t t ans)
  15245. deltan (car delta)
  15246. deltaw (nth 1 delta)
  15247. deltadef (nth 2 delta)))
  15248. ;; Check if there is an iso week date in there. If yes, store the
  15249. ;; info and postpone interpreting it until the rest of the parsing
  15250. ;; is done.
  15251. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15252. (setq iso-year (if (match-end 1)
  15253. (org-small-year-to-year
  15254. (string-to-number (match-string 1 ans))))
  15255. iso-weekday (if (match-end 3)
  15256. (string-to-number (match-string 3 ans)))
  15257. iso-week (string-to-number (match-string 2 ans)))
  15258. (setq ans (replace-match "" t t ans)))
  15259. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15260. (when (string-match
  15261. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15262. (setq year (if (match-end 2)
  15263. (string-to-number (match-string 2 ans))
  15264. (progn (setq kill-year t)
  15265. (string-to-number (format-time-string "%Y"))))
  15266. month (string-to-number (match-string 3 ans))
  15267. day (string-to-number (match-string 4 ans)))
  15268. (if (< year 100) (setq year (+ 2000 year)))
  15269. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15270. t nil ans)))
  15271. ;; Help matching dotted european dates
  15272. (when (string-match
  15273. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15274. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15275. (setq kill-year t)
  15276. (string-to-number (format-time-string "%Y")))
  15277. day (string-to-number (match-string 1 ans))
  15278. month (string-to-number (match-string 2 ans))
  15279. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15280. t nil ans)))
  15281. ;; Help matching american dates, like 5/30 or 5/30/7
  15282. (when (string-match
  15283. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15284. (setq year (if (match-end 4)
  15285. (string-to-number (match-string 4 ans))
  15286. (progn (setq kill-year t)
  15287. (string-to-number (format-time-string "%Y"))))
  15288. month (string-to-number (match-string 1 ans))
  15289. day (string-to-number (match-string 2 ans)))
  15290. (if (< year 100) (setq year (+ 2000 year)))
  15291. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15292. t nil ans)))
  15293. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15294. ;; If there is a time with am/pm, and *no* time without it, we convert
  15295. ;; so that matching will be successful.
  15296. (loop for i from 1 to 2 do ; twice, for end time as well
  15297. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15298. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15299. (setq hour (string-to-number (match-string 1 ans))
  15300. minute (if (match-end 3)
  15301. (string-to-number (match-string 3 ans))
  15302. 0)
  15303. pm (equal ?p
  15304. (string-to-char (downcase (match-string 4 ans)))))
  15305. (if (and (= hour 12) (not pm))
  15306. (setq hour 0)
  15307. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15308. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15309. t t ans))))
  15310. ;; Check if a time range is given as a duration
  15311. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15312. (setq hour (string-to-number (match-string 1 ans))
  15313. h2 (+ hour (string-to-number (match-string 3 ans)))
  15314. minute (string-to-number (match-string 2 ans))
  15315. m2 (+ minute (if (match-end 5) (string-to-number
  15316. (match-string 5 ans))0)))
  15317. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15318. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15319. t t ans)))
  15320. ;; Check if there is a time range
  15321. (when (boundp 'org-end-time-was-given)
  15322. (setq org-time-was-given nil)
  15323. (when (and (string-match org-plain-time-of-day-regexp ans)
  15324. (match-end 8))
  15325. (setq org-end-time-was-given (match-string 8 ans))
  15326. (setq ans (concat (substring ans 0 (match-beginning 7))
  15327. (substring ans (match-end 7))))))
  15328. (setq tl (parse-time-string ans)
  15329. day (or (nth 3 tl) (nth 3 org-defdecode))
  15330. month (or (nth 4 tl)
  15331. (if (and org-read-date-prefer-future
  15332. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15333. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15334. (nth 4 org-defdecode)))
  15335. year (or (and (not kill-year) (nth 5 tl))
  15336. (if (and org-read-date-prefer-future
  15337. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15338. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15339. (nth 5 org-defdecode)))
  15340. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15341. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15342. second (or (nth 0 tl) 0)
  15343. wday (nth 6 tl))
  15344. (when (and (eq org-read-date-prefer-future 'time)
  15345. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15346. (equal day (nth 3 nowdecode))
  15347. (equal month (nth 4 nowdecode))
  15348. (equal year (nth 5 nowdecode))
  15349. (nth 2 tl)
  15350. (or (< (nth 2 tl) (nth 2 nowdecode))
  15351. (and (= (nth 2 tl) (nth 2 nowdecode))
  15352. (nth 1 tl)
  15353. (< (nth 1 tl) (nth 1 nowdecode)))))
  15354. (setq day (1+ day)
  15355. futurep t))
  15356. ;; Special date definitions below
  15357. (cond
  15358. (iso-week
  15359. ;; There was an iso week
  15360. (require 'cal-iso)
  15361. (setq futurep nil)
  15362. (setq year (or iso-year year)
  15363. day (or iso-weekday wday 1)
  15364. wday nil ; to make sure that the trigger below does not match
  15365. iso-date (calendar-gregorian-from-absolute
  15366. (calendar-absolute-from-iso
  15367. (list iso-week day year))))
  15368. ; FIXME: Should we also push ISO weeks into the future?
  15369. ; (when (and org-read-date-prefer-future
  15370. ; (not iso-year)
  15371. ; (< (calendar-absolute-from-gregorian iso-date)
  15372. ; (time-to-days (current-time))))
  15373. ; (setq year (1+ year)
  15374. ; iso-date (calendar-gregorian-from-absolute
  15375. ; (calendar-absolute-from-iso
  15376. ; (list iso-week day year)))))
  15377. (setq month (car iso-date)
  15378. year (nth 2 iso-date)
  15379. day (nth 1 iso-date)))
  15380. (deltan
  15381. (setq futurep nil)
  15382. (unless deltadef
  15383. (let ((now (decode-time (current-time))))
  15384. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15385. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15386. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15387. ((equal deltaw "m") (setq month (+ month deltan)))
  15388. ((equal deltaw "y") (setq year (+ year deltan)))))
  15389. ((and wday (not (nth 3 tl)))
  15390. ;; Weekday was given, but no day, so pick that day in the week
  15391. ;; on or after the derived date.
  15392. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15393. (unless (equal wday wday1)
  15394. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15395. (if (and (boundp 'org-time-was-given)
  15396. (nth 2 tl))
  15397. (setq org-time-was-given t))
  15398. (if (< year 100) (setq year (+ 2000 year)))
  15399. ;; Check of the date is representable
  15400. (if org-read-date-force-compatible-dates
  15401. (progn
  15402. (if (< year 1970)
  15403. (setq year 1970 org-read-date-analyze-forced-year t))
  15404. (if (> year 2037)
  15405. (setq year 2037 org-read-date-analyze-forced-year t)))
  15406. (condition-case nil
  15407. (ignore (encode-time second minute hour day month year))
  15408. (error
  15409. (setq year (nth 5 org-defdecode))
  15410. (setq org-read-date-analyze-forced-year t))))
  15411. (setq org-read-date-analyze-futurep futurep)
  15412. (list second minute hour day month year)))
  15413. (defvar parse-time-weekdays)
  15414. (defun org-read-date-get-relative (s today default)
  15415. "Check string S for special relative date string.
  15416. TODAY and DEFAULT are internal times, for today and for a default.
  15417. Return shift list (N what def-flag)
  15418. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15419. N is the number of WHATs to shift.
  15420. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15421. the DEFAULT date rather than TODAY."
  15422. (require 'parse-time)
  15423. (when (and
  15424. (string-match
  15425. (concat
  15426. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15427. "\\([0-9]+\\)?"
  15428. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15429. "\\([ \t]\\|$\\)") s)
  15430. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15431. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15432. (string-to-char (substring (match-string 1 s) -1))
  15433. ?+))
  15434. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15435. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15436. (what (if (match-end 3) (match-string 3 s) "d"))
  15437. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15438. (date (if rel default today))
  15439. (wday (nth 6 (decode-time date)))
  15440. delta)
  15441. (if wday1
  15442. (progn
  15443. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15444. (if (= delta 0) (setq delta 7))
  15445. (if (= dir ?-)
  15446. (progn
  15447. (setq delta (- delta 7))
  15448. (if (= delta 0) (setq delta -7))))
  15449. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15450. (list delta "d" rel))
  15451. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15452. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15453. "Turn a user-specified date into the internal representation.
  15454. The internal representation needed by the calendar is (month day year).
  15455. This is a wrapper to handle the brain-dead convention in calendar that
  15456. user function argument order change dependent on argument order."
  15457. (if (boundp 'calendar-date-style)
  15458. (cond
  15459. ((eq calendar-date-style 'american)
  15460. (list arg1 arg2 arg3))
  15461. ((eq calendar-date-style 'european)
  15462. (list arg2 arg1 arg3))
  15463. ((eq calendar-date-style 'iso)
  15464. (list arg2 arg3 arg1)))
  15465. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15466. (if (org-bound-and-true-p european-calendar-style)
  15467. (list arg2 arg1 arg3)
  15468. (list arg1 arg2 arg3)))))
  15469. (defun org-eval-in-calendar (form &optional keepdate)
  15470. "Eval FORM in the calendar window and return to current window.
  15471. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15472. otherwise stick to the current value of `org-ans2'."
  15473. (let ((sf (selected-frame))
  15474. (sw (selected-window)))
  15475. (select-window (get-buffer-window "*Calendar*" t))
  15476. (eval form)
  15477. (when (and (not keepdate) (calendar-cursor-to-date))
  15478. (let* ((date (calendar-cursor-to-date))
  15479. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15480. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15481. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15482. (select-window sw)
  15483. (org-select-frame-set-input-focus sf)))
  15484. (defun org-calendar-select ()
  15485. "Return to `org-read-date' with the date currently selected.
  15486. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15487. (interactive)
  15488. (when (calendar-cursor-to-date)
  15489. (let* ((date (calendar-cursor-to-date))
  15490. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15491. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15492. (if (active-minibuffer-window) (exit-minibuffer))))
  15493. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15494. "Insert a date stamp for the date given by the internal TIME.
  15495. WITH-HM means use the stamp format that includes the time of the day.
  15496. INACTIVE means use square brackets instead of angular ones, so that the
  15497. stamp will not contribute to the agenda.
  15498. PRE and POST are optional strings to be inserted before and after the
  15499. stamp.
  15500. The command returns the inserted time stamp."
  15501. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15502. stamp)
  15503. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15504. (insert-before-markers (or pre ""))
  15505. (when (listp extra)
  15506. (setq extra (car extra))
  15507. (if (and (stringp extra)
  15508. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15509. (setq extra (format "-%02d:%02d"
  15510. (string-to-number (match-string 1 extra))
  15511. (string-to-number (match-string 2 extra))))
  15512. (setq extra nil)))
  15513. (when extra
  15514. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15515. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15516. (insert-before-markers (or post ""))
  15517. (setq org-last-inserted-timestamp stamp)))
  15518. (defun org-toggle-time-stamp-overlays ()
  15519. "Toggle the use of custom time stamp formats."
  15520. (interactive)
  15521. (setq org-display-custom-times (not org-display-custom-times))
  15522. (unless org-display-custom-times
  15523. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15524. (while (setq p (next-single-property-change p 'display))
  15525. (if (and (get-text-property p 'display)
  15526. (eq (get-text-property p 'face) 'org-date))
  15527. (remove-text-properties
  15528. p (setq p (next-single-property-change p 'display))
  15529. '(display t))))
  15530. (set-buffer-modified-p bmp)))
  15531. (if (featurep 'xemacs)
  15532. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15533. (org-restart-font-lock)
  15534. (setq org-table-may-need-update t)
  15535. (if org-display-custom-times
  15536. (message "Time stamps are overlaid with custom format")
  15537. (message "Time stamp overlays removed")))
  15538. (defun org-display-custom-time (beg end)
  15539. "Overlay modified time stamp format over timestamp between BEG and END."
  15540. (let* ((ts (buffer-substring beg end))
  15541. t1 w1 with-hm tf time str w2 (off 0))
  15542. (save-match-data
  15543. (setq t1 (org-parse-time-string ts t))
  15544. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15545. (setq off (- (match-end 0) (match-beginning 0)))))
  15546. (setq end (- end off))
  15547. (setq w1 (- end beg)
  15548. with-hm (and (nth 1 t1) (nth 2 t1))
  15549. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15550. time (org-fix-decoded-time t1)
  15551. str (org-add-props
  15552. (format-time-string
  15553. (substring tf 1 -1) (apply 'encode-time time))
  15554. nil 'mouse-face 'highlight)
  15555. w2 (length str))
  15556. (if (not (= w2 w1))
  15557. (add-text-properties (1+ beg) (+ 2 beg)
  15558. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15559. (if (featurep 'xemacs)
  15560. (progn
  15561. (put-text-property beg end 'invisible t)
  15562. (put-text-property beg end 'end-glyph (make-glyph str)))
  15563. (put-text-property beg end 'display str))))
  15564. (defun org-translate-time (string)
  15565. "Translate all timestamps in STRING to custom format.
  15566. But do this only if the variable `org-display-custom-times' is set."
  15567. (when org-display-custom-times
  15568. (save-match-data
  15569. (let* ((start 0)
  15570. (re org-ts-regexp-both)
  15571. t1 with-hm inactive tf time str beg end)
  15572. (while (setq start (string-match re string start))
  15573. (setq beg (match-beginning 0)
  15574. end (match-end 0)
  15575. t1 (save-match-data
  15576. (org-parse-time-string (substring string beg end) t))
  15577. with-hm (and (nth 1 t1) (nth 2 t1))
  15578. inactive (equal (substring string beg (1+ beg)) "[")
  15579. tf (funcall (if with-hm 'cdr 'car)
  15580. org-time-stamp-custom-formats)
  15581. time (org-fix-decoded-time t1)
  15582. str (format-time-string
  15583. (concat
  15584. (if inactive "[" "<") (substring tf 1 -1)
  15585. (if inactive "]" ">"))
  15586. (apply 'encode-time time))
  15587. string (replace-match str t t string)
  15588. start (+ start (length str)))))))
  15589. string)
  15590. (defun org-fix-decoded-time (time)
  15591. "Set 0 instead of nil for the first 6 elements of time.
  15592. Don't touch the rest."
  15593. (let ((n 0))
  15594. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15595. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15596. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15597. "Difference between TIMESTAMP-STRING and now in days.
  15598. If SECONDS is non-nil, return the difference in seconds."
  15599. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15600. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15601. (funcall fdiff (current-time)))))
  15602. (defun org-deadline-close (timestamp-string &optional ndays)
  15603. "Is the time in TIMESTAMP-STRING close to the current date?"
  15604. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15605. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15606. (not (org-entry-is-done-p))))
  15607. (defun org-get-wdays (ts &optional delay zero-delay)
  15608. "Get the deadline lead time appropriate for timestring TS.
  15609. When DELAY is non-nil, get the delay time for scheduled items
  15610. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15611. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15612. don't try to find the delay cookie in the scheduled timestamp."
  15613. (let ((tv (if delay org-scheduled-delay-days
  15614. org-deadline-warning-days)))
  15615. (cond
  15616. ((or (and delay (< tv 0))
  15617. (and delay zero-delay (<= tv 0))
  15618. (and (not delay) (<= tv 0)))
  15619. ;; Enforce this value no matter what
  15620. (- tv))
  15621. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15622. ;; lead time is specified.
  15623. (floor (* (string-to-number (match-string 1 ts))
  15624. (cdr (assoc (match-string 2 ts)
  15625. '(("d" . 1) ("w" . 7)
  15626. ("m" . 30.4) ("y" . 365.25)
  15627. ("h" . 0.041667)))))))
  15628. ;; go for the default.
  15629. (t tv))))
  15630. (defun org-calendar-select-mouse (ev)
  15631. "Return to `org-read-date' with the date currently selected.
  15632. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15633. (interactive "e")
  15634. (mouse-set-point ev)
  15635. (when (calendar-cursor-to-date)
  15636. (let* ((date (calendar-cursor-to-date))
  15637. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15638. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15639. (if (active-minibuffer-window) (exit-minibuffer))))
  15640. (defun org-check-deadlines (ndays)
  15641. "Check if there are any deadlines due or past due.
  15642. A deadline is considered due if it happens within `org-deadline-warning-days'
  15643. days from today's date. If the deadline appears in an entry marked DONE,
  15644. it is not shown. The prefix arg NDAYS can be used to test that many
  15645. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15646. (interactive "P")
  15647. (let* ((org-warn-days
  15648. (cond
  15649. ((equal ndays '(4)) 100000)
  15650. (ndays (prefix-numeric-value ndays))
  15651. (t (abs org-deadline-warning-days))))
  15652. (case-fold-search nil)
  15653. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15654. (callback
  15655. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15656. (message "%d deadlines past-due or due within %d days"
  15657. (org-occur regexp nil callback)
  15658. org-warn-days)))
  15659. (defsubst org-re-timestamp (type)
  15660. "Return a regexp for timestamp TYPE.
  15661. Allowed values for TYPE are:
  15662. all: all timestamps
  15663. active: only active timestamps (<...>)
  15664. inactive: only inactive timestamps ([...])
  15665. scheduled: only scheduled timestamps
  15666. deadline: only deadline timestamps
  15667. closed: only closed time-stamps
  15668. When TYPE is nil, fall back on returning a regexp that matches
  15669. both scheduled and deadline timestamps."
  15670. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15671. ((eq type 'active) org-ts-regexp)
  15672. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15673. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15674. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15675. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15676. ((eq type 'scheduled-or-deadline)
  15677. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15678. (defun org-check-before-date (date)
  15679. "Check if there are deadlines or scheduled entries before DATE."
  15680. (interactive (list (org-read-date)))
  15681. (let ((case-fold-search nil)
  15682. (regexp (org-re-timestamp org-ts-type))
  15683. (callback
  15684. (lambda () (time-less-p
  15685. (org-time-string-to-time (match-string 1))
  15686. (org-time-string-to-time date)))))
  15687. (message "%d entries before %s"
  15688. (org-occur regexp nil callback) date)))
  15689. (defun org-check-after-date (date)
  15690. "Check if there are deadlines or scheduled entries after DATE."
  15691. (interactive (list (org-read-date)))
  15692. (let ((case-fold-search nil)
  15693. (regexp (org-re-timestamp org-ts-type))
  15694. (callback
  15695. (lambda () (not
  15696. (time-less-p
  15697. (org-time-string-to-time (match-string 1))
  15698. (org-time-string-to-time date))))))
  15699. (message "%d entries after %s"
  15700. (org-occur regexp nil callback) date)))
  15701. (defun org-check-dates-range (start-date end-date)
  15702. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15703. (interactive (list (org-read-date nil nil nil "Range starts")
  15704. (org-read-date nil nil nil "Range end")))
  15705. (let ((case-fold-search nil)
  15706. (regexp (org-re-timestamp org-ts-type))
  15707. (callback
  15708. (lambda ()
  15709. (let ((match (match-string 1)))
  15710. (and
  15711. (not (time-less-p
  15712. (org-time-string-to-time match)
  15713. (org-time-string-to-time start-date)))
  15714. (time-less-p
  15715. (org-time-string-to-time match)
  15716. (org-time-string-to-time end-date)))))))
  15717. (message "%d entries between %s and %s"
  15718. (org-occur regexp nil callback) start-date end-date)))
  15719. (defun org-evaluate-time-range (&optional to-buffer)
  15720. "Evaluate a time range by computing the difference between start and end.
  15721. Normally the result is just printed in the echo area, but with prefix arg
  15722. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15723. If the time range is actually in a table, the result is inserted into the
  15724. next column.
  15725. For time difference computation, a year is assumed to be exactly 365
  15726. days in order to avoid rounding problems."
  15727. (interactive "P")
  15728. (or
  15729. (org-clock-update-time-maybe)
  15730. (save-excursion
  15731. (unless (org-at-date-range-p t)
  15732. (goto-char (point-at-bol))
  15733. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15734. (if (not (org-at-date-range-p t))
  15735. (user-error "Not at a time-stamp range, and none found in current line")))
  15736. (let* ((ts1 (match-string 1))
  15737. (ts2 (match-string 2))
  15738. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15739. (match-end (match-end 0))
  15740. (time1 (org-time-string-to-time ts1))
  15741. (time2 (org-time-string-to-time ts2))
  15742. (t1 (org-float-time time1))
  15743. (t2 (org-float-time time2))
  15744. (diff (abs (- t2 t1)))
  15745. (negative (< (- t2 t1) 0))
  15746. ;; (ys (floor (* 365 24 60 60)))
  15747. (ds (* 24 60 60))
  15748. (hs (* 60 60))
  15749. (fy "%dy %dd %02d:%02d")
  15750. (fy1 "%dy %dd")
  15751. (fd "%dd %02d:%02d")
  15752. (fd1 "%dd")
  15753. (fh "%02d:%02d")
  15754. y d h m align)
  15755. (if havetime
  15756. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15757. y 0
  15758. d (floor (/ diff ds)) diff (mod diff ds)
  15759. h (floor (/ diff hs)) diff (mod diff hs)
  15760. m (floor (/ diff 60)))
  15761. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15762. y 0
  15763. d (floor (+ (/ diff ds) 0.5))
  15764. h 0 m 0))
  15765. (if (not to-buffer)
  15766. (message "%s" (org-make-tdiff-string y d h m))
  15767. (if (org-at-table-p)
  15768. (progn
  15769. (goto-char match-end)
  15770. (setq align t)
  15771. (and (looking-at " *|") (goto-char (match-end 0))))
  15772. (goto-char match-end))
  15773. (if (looking-at
  15774. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15775. (replace-match ""))
  15776. (if negative (insert " -"))
  15777. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15778. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15779. (insert " " (format fh h m))))
  15780. (if align (org-table-align))
  15781. (message "Time difference inserted")))))
  15782. (defun org-make-tdiff-string (y d h m)
  15783. (let ((fmt "")
  15784. (l nil))
  15785. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15786. l (push y l)))
  15787. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15788. l (push d l)))
  15789. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15790. l (push h l)))
  15791. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15792. l (push m l)))
  15793. (apply 'format fmt (nreverse l))))
  15794. (defun org-time-string-to-time (s &optional buffer pos)
  15795. "Convert a timestamp string into internal time."
  15796. (condition-case errdata
  15797. (apply 'encode-time (org-parse-time-string s))
  15798. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15799. s (if (not (and buffer pos))
  15800. ""
  15801. (format " at %d in buffer `%s'" pos buffer))
  15802. (cdr errdata)))))
  15803. (defun org-time-string-to-seconds (s)
  15804. "Convert a timestamp string to a number of seconds."
  15805. (org-float-time (org-time-string-to-time s)))
  15806. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15807. "Convert a time stamp to an absolute day number.
  15808. If there is a specifier for a cyclic time stamp, get the closest
  15809. date to DAYNR.
  15810. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15811. The variable `date' is bound by the calendar when this is called."
  15812. (cond
  15813. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15814. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15815. daynr
  15816. (+ daynr 1000)))
  15817. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15818. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15819. (time-to-days (current-time))) (match-string 0 s)
  15820. prefer show-all))
  15821. (t (time-to-days
  15822. (condition-case errdata
  15823. (apply 'encode-time (org-parse-time-string s))
  15824. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15825. s (if (not (and buffer pos))
  15826. ""
  15827. (format " at %d in buffer `%s'" pos buffer))
  15828. (cdr errdata))))))))
  15829. (defun org-days-to-iso-week (days)
  15830. "Return the iso week number."
  15831. (require 'cal-iso)
  15832. (car (calendar-iso-from-absolute days)))
  15833. (defun org-small-year-to-year (year)
  15834. "Convert 2-digit years into 4-digit years.
  15835. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15836. The year 2000 cannot be abbreviated. Any year larger than 99
  15837. is returned unchanged."
  15838. (if (< year 38)
  15839. (setq year (+ 2000 year))
  15840. (if (< year 100)
  15841. (setq year (+ 1900 year))))
  15842. year)
  15843. (defun org-time-from-absolute (d)
  15844. "Return the time corresponding to date D.
  15845. D may be an absolute day number, or a calendar-type list (month day year)."
  15846. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15847. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15848. (defun org-calendar-holiday ()
  15849. "List of holidays, for Diary display in Org-mode."
  15850. (require 'holidays)
  15851. (let ((hl (funcall
  15852. (if (fboundp 'calendar-check-holidays)
  15853. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15854. (if hl (mapconcat 'identity hl "; "))))
  15855. (defun org-diary-sexp-entry (sexp entry date)
  15856. "Process a SEXP diary ENTRY for DATE."
  15857. (require 'diary-lib)
  15858. (let ((result (if calendar-debug-sexp
  15859. (let ((stack-trace-on-error t))
  15860. (eval (car (read-from-string sexp))))
  15861. (condition-case nil
  15862. (eval (car (read-from-string sexp)))
  15863. (error
  15864. (beep)
  15865. (message "Bad sexp at line %d in %s: %s"
  15866. (org-current-line)
  15867. (buffer-file-name) sexp)
  15868. (sleep-for 2))))))
  15869. (cond ((stringp result) (split-string result "; "))
  15870. ((and (consp result)
  15871. (not (consp (cdr result)))
  15872. (stringp (cdr result))) (cdr result))
  15873. ((and (consp result)
  15874. (stringp (car result))) result)
  15875. (result entry))))
  15876. (defun org-diary-to-ical-string (frombuf)
  15877. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15878. This uses the icalendar.el library."
  15879. (let* ((tmpdir (if (featurep 'xemacs)
  15880. (temp-directory)
  15881. temporary-file-directory))
  15882. (tmpfile (make-temp-name
  15883. (expand-file-name "orgics" tmpdir)))
  15884. buf rtn b e)
  15885. (with-current-buffer frombuf
  15886. (icalendar-export-region (point-min) (point-max) tmpfile)
  15887. (setq buf (find-buffer-visiting tmpfile))
  15888. (set-buffer buf)
  15889. (goto-char (point-min))
  15890. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15891. (setq b (match-beginning 0)))
  15892. (goto-char (point-max))
  15893. (if (re-search-backward "^END:VEVENT" nil t)
  15894. (setq e (match-end 0)))
  15895. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15896. (kill-buffer buf)
  15897. (delete-file tmpfile)
  15898. rtn))
  15899. (defun org-closest-date (start current change prefer show-all)
  15900. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15901. When PREFER is `past', return a date that is either CURRENT or past.
  15902. When PREFER is `future', return a date that is either CURRENT or future.
  15903. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15904. ;; Make the proper lists from the dates
  15905. (catch 'exit
  15906. (let ((a1 '(("h" . hour)
  15907. ("d" . day)
  15908. ("w" . week)
  15909. ("m" . month)
  15910. ("y" . year)))
  15911. (shour (nth 2 (org-parse-time-string start)))
  15912. dn dw sday cday n1 n2 n0
  15913. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15914. (setq start (org-date-to-gregorian start)
  15915. current (org-date-to-gregorian
  15916. (if show-all
  15917. current
  15918. (time-to-days (current-time))))
  15919. sday (calendar-absolute-from-gregorian start)
  15920. cday (calendar-absolute-from-gregorian current))
  15921. (if (<= cday sday) (throw 'exit sday))
  15922. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15923. (setq dn (string-to-number (match-string 1 change))
  15924. dw (cdr (assoc (match-string 2 change) a1)))
  15925. (user-error "Invalid change specifier: %s" change))
  15926. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15927. (cond
  15928. ((eq dw 'hour)
  15929. (let ((missing-hours
  15930. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15931. dn)))
  15932. (setq n1 (if (zerop missing-hours) cday
  15933. (- cday (1+ (floor (/ missing-hours 24)))))
  15934. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15935. ((eq dw 'day)
  15936. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15937. n2 (+ n1 dn)))
  15938. ((eq dw 'year)
  15939. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15940. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15941. (setq date1 (list m d y1)
  15942. n1 (calendar-absolute-from-gregorian date1)
  15943. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15944. n2 (calendar-absolute-from-gregorian date2)))
  15945. ((eq dw 'month)
  15946. ;; approx number of month between the two dates
  15947. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15948. ;; How often does dn fit in there?
  15949. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15950. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15951. m (+ m nm)
  15952. ny (floor (/ m 12))
  15953. y (+ y ny)
  15954. m (- m (* ny 12)))
  15955. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15956. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15957. (setq m2 (+ m dn) y2 y)
  15958. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15959. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15960. (while (<= n2 cday)
  15961. (setq n1 n2 m m2 y y2)
  15962. (setq m2 (+ m dn) y2 y)
  15963. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15964. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15965. ;; Make sure n1 is the earlier date
  15966. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15967. (if show-all
  15968. (cond
  15969. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15970. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15971. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15972. (cond
  15973. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15974. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15975. (t (if (= cday n1) n1 n2)))))))
  15976. (defun org-date-to-gregorian (date)
  15977. "Turn any specification of DATE into a Gregorian date for the calendar."
  15978. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15979. ((and (listp date) (= (length date) 3)) date)
  15980. ((stringp date)
  15981. (setq date (org-parse-time-string date))
  15982. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15983. ((listp date)
  15984. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15985. (defun org-parse-time-string (s &optional nodefault)
  15986. "Parse the standard Org-mode time string.
  15987. This should be a lot faster than the normal `parse-time-string'.
  15988. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15989. hour and minute fields will be nil if not given."
  15990. (cond ((string-match org-ts-regexp0 s)
  15991. (list 0
  15992. (if (or (match-beginning 8) (not nodefault))
  15993. (string-to-number (or (match-string 8 s) "0")))
  15994. (if (or (match-beginning 7) (not nodefault))
  15995. (string-to-number (or (match-string 7 s) "0")))
  15996. (string-to-number (match-string 4 s))
  15997. (string-to-number (match-string 3 s))
  15998. (string-to-number (match-string 2 s))
  15999. nil nil nil))
  16000. ((string-match "^<[^>]+>$" s)
  16001. (decode-time (seconds-to-time (org-matcher-time s))))
  16002. (t (error "Not a standard Org-mode time string: %s" s))))
  16003. (defun org-timestamp-up (&optional arg)
  16004. "Increase the date item at the cursor by one.
  16005. If the cursor is on the year, change the year. If it is on the month,
  16006. the day or the time, change that.
  16007. With prefix ARG, change by that many units."
  16008. (interactive "p")
  16009. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16010. (defun org-timestamp-down (&optional arg)
  16011. "Decrease the date item at the cursor by one.
  16012. If the cursor is on the year, change the year. If it is on the month,
  16013. the day or the time, change that.
  16014. With prefix ARG, change by that many units."
  16015. (interactive "p")
  16016. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16017. (defun org-timestamp-up-day (&optional arg)
  16018. "Increase the date in the time stamp by one day.
  16019. With prefix ARG, change that many days."
  16020. (interactive "p")
  16021. (if (and (not (org-at-timestamp-p t))
  16022. (org-at-heading-p))
  16023. (org-todo 'up)
  16024. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16025. (defun org-timestamp-down-day (&optional arg)
  16026. "Decrease the date in the time stamp by one day.
  16027. With prefix ARG, change that many days."
  16028. (interactive "p")
  16029. (if (and (not (org-at-timestamp-p t))
  16030. (org-at-heading-p))
  16031. (org-todo 'down)
  16032. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16033. (defun org-at-timestamp-p (&optional inactive-ok)
  16034. "Determine if the cursor is in or at a timestamp."
  16035. (interactive)
  16036. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16037. (pos (point))
  16038. (ans (or (looking-at tsr)
  16039. (save-excursion
  16040. (skip-chars-backward "^[<\n\r\t")
  16041. (if (> (point) (point-min)) (backward-char 1))
  16042. (and (looking-at tsr)
  16043. (> (- (match-end 0) pos) -1))))))
  16044. (and ans
  16045. (boundp 'org-ts-what)
  16046. (setq org-ts-what
  16047. (cond
  16048. ((= pos (match-beginning 0)) 'bracket)
  16049. ;; Point is considered to be "on the bracket" whether
  16050. ;; it's really on it or right after it.
  16051. ((= pos (1- (match-end 0))) 'bracket)
  16052. ((= pos (match-end 0)) 'after)
  16053. ((org-pos-in-match-range pos 2) 'year)
  16054. ((org-pos-in-match-range pos 3) 'month)
  16055. ((org-pos-in-match-range pos 7) 'hour)
  16056. ((org-pos-in-match-range pos 8) 'minute)
  16057. ((or (org-pos-in-match-range pos 4)
  16058. (org-pos-in-match-range pos 5)) 'day)
  16059. ((and (> pos (or (match-end 8) (match-end 5)))
  16060. (< pos (match-end 0)))
  16061. (- pos (or (match-end 8) (match-end 5))))
  16062. (t 'day))))
  16063. ans))
  16064. (defun org-toggle-timestamp-type ()
  16065. "Toggle the type (<active> or [inactive]) of a time stamp."
  16066. (interactive)
  16067. (when (org-at-timestamp-p t)
  16068. (let ((beg (match-beginning 0)) (end (match-end 0))
  16069. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16070. (save-excursion
  16071. (goto-char beg)
  16072. (while (re-search-forward "[][<>]" end t)
  16073. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16074. t t)))
  16075. (message "Timestamp is now %sactive"
  16076. (if (equal (char-after beg) ?<) "" "in")))))
  16077. (defun org-at-clock-log-p nil
  16078. "Is the cursor on the clock log line?"
  16079. (save-excursion
  16080. (move-beginning-of-line 1)
  16081. (looking-at "^[ \t]*CLOCK:")))
  16082. (defvar org-clock-history) ; defined in org-clock.el
  16083. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16084. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16085. "Change the date in the time stamp at point.
  16086. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16087. `year', `minute', `second'. If WHAT is not given, the cursor position
  16088. in the timestamp determines what will be changed.
  16089. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16090. (let ((origin (point)) origin-cat
  16091. with-hm inactive
  16092. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16093. org-ts-what
  16094. extra rem
  16095. ts time time0 fixnext clrgx)
  16096. (if (not (org-at-timestamp-p t))
  16097. (user-error "Not at a timestamp"))
  16098. (if (and (not what) (eq org-ts-what 'bracket))
  16099. (org-toggle-timestamp-type)
  16100. ;; Point isn't on brackets. Remember the part of the time-stamp
  16101. ;; the point was in. Indeed, size of time-stamps may change,
  16102. ;; but point must be kept in the same category nonetheless.
  16103. (setq origin-cat org-ts-what)
  16104. (if (and (not what) (not (eq org-ts-what 'day))
  16105. org-display-custom-times
  16106. (get-text-property (point) 'display)
  16107. (not (get-text-property (1- (point)) 'display)))
  16108. (setq org-ts-what 'day))
  16109. (setq org-ts-what (or what org-ts-what)
  16110. inactive (= (char-after (match-beginning 0)) ?\[)
  16111. ts (match-string 0))
  16112. (replace-match "")
  16113. (when (string-match
  16114. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16115. ts)
  16116. (setq extra (match-string 1 ts))
  16117. (if suppress-tmp-delay
  16118. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16119. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16120. (setq with-hm t))
  16121. (setq time0 (org-parse-time-string ts))
  16122. (when (and updown
  16123. (eq org-ts-what 'minute)
  16124. (not current-prefix-arg))
  16125. ;; This looks like s-up and s-down. Change by one rounding step.
  16126. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16127. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  16128. (setcar (cdr time0) (+ (nth 1 time0)
  16129. (if (> n 0) (- rem) (- dm rem))))))
  16130. (setq time
  16131. (encode-time (or (car time0) 0)
  16132. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16133. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16134. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16135. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16136. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16137. (nthcdr 6 time0)))
  16138. (when (and (member org-ts-what '(hour minute))
  16139. extra
  16140. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16141. (setq extra (org-modify-ts-extra
  16142. extra
  16143. (if (eq org-ts-what 'hour) 2 5)
  16144. n dm)))
  16145. (when (integerp org-ts-what)
  16146. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16147. (if (eq what 'calendar)
  16148. (let ((cal-date (org-get-date-from-calendar)))
  16149. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16150. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16151. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16152. (setcar time0 (or (car time0) 0))
  16153. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16154. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16155. (setq time (apply 'encode-time time0))))
  16156. ;; Insert the new time-stamp, and ensure point stays in the same
  16157. ;; category as before (i.e. not after the last position in that
  16158. ;; category).
  16159. (let ((pos (point)))
  16160. ;; Stay before inserted string. `save-excursion' is of no use.
  16161. (setq org-last-changed-timestamp
  16162. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16163. (goto-char pos))
  16164. (save-match-data
  16165. (looking-at org-ts-regexp3)
  16166. (goto-char (cond
  16167. ;; `day' category ends before `hour' if any, or at
  16168. ;; the end of the day name.
  16169. ((eq origin-cat 'day)
  16170. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16171. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16172. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16173. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16174. ;; `year' and `month' have both fixed size: point
  16175. ;; couldn't have moved into another part.
  16176. (t origin))))
  16177. ;; Update clock if on a CLOCK line.
  16178. (org-clock-update-time-maybe)
  16179. ;; Maybe adjust the closest clock in `org-clock-history'
  16180. (when org-clock-adjust-closest
  16181. (if (not (and (org-at-clock-log-p)
  16182. (< 1 (length (delq nil (mapcar 'marker-position
  16183. org-clock-history))))))
  16184. (message "No clock to adjust")
  16185. (cond ((save-excursion ; fix previous clock?
  16186. (re-search-backward org-ts-regexp0 nil t)
  16187. (org-looking-back (concat org-clock-string " \\[")))
  16188. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16189. ((save-excursion ; fix next clock?
  16190. (re-search-backward org-ts-regexp0 nil t)
  16191. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16192. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16193. (save-window-excursion
  16194. ;; Find closest clock to point, adjust the previous/next one in history
  16195. (let* ((p (save-excursion (org-back-to-heading t)))
  16196. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16197. (clfixnth
  16198. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  16199. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16200. (if (not clfixpos)
  16201. (message "No clock to adjust")
  16202. (save-excursion
  16203. (org-goto-marker-or-bmk clfixpos)
  16204. (org-show-subtree)
  16205. (when (re-search-forward clrgx nil t)
  16206. (goto-char (match-beginning 1))
  16207. (let (org-clock-adjust-closest)
  16208. (org-timestamp-change n org-ts-what updown))
  16209. (message "Clock adjusted in %s for heading: %s"
  16210. (file-name-nondirectory (buffer-file-name))
  16211. (org-get-heading t t)))))))))
  16212. ;; Try to recenter the calendar window, if any.
  16213. (if (and org-calendar-follow-timestamp-change
  16214. (get-buffer-window "*Calendar*" t)
  16215. (memq org-ts-what '(day month year)))
  16216. (org-recenter-calendar (time-to-days time))))))
  16217. (defun org-modify-ts-extra (s pos n dm)
  16218. "Change the different parts of the lead-time and repeat fields in timestamp."
  16219. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16220. ng h m new rem)
  16221. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16222. (cond
  16223. ((or (org-pos-in-match-range pos 2)
  16224. (org-pos-in-match-range pos 3))
  16225. (setq m (string-to-number (match-string 3 s))
  16226. h (string-to-number (match-string 2 s)))
  16227. (if (org-pos-in-match-range pos 2)
  16228. (setq h (+ h n))
  16229. (setq n (* dm (org-no-warnings (signum n))))
  16230. (when (not (= 0 (setq rem (% m dm))))
  16231. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16232. (setq m (+ m n)))
  16233. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16234. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16235. (setq h (min 24 (max 0 h)))
  16236. (setq ng 1 new (format "-%02d:%02d" h m)))
  16237. ((org-pos-in-match-range pos 6)
  16238. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16239. ((org-pos-in-match-range pos 5)
  16240. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16241. ((org-pos-in-match-range pos 9)
  16242. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16243. ((org-pos-in-match-range pos 8)
  16244. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16245. (when ng
  16246. (setq s (concat
  16247. (substring s 0 (match-beginning ng))
  16248. new
  16249. (substring s (match-end ng))))))
  16250. s))
  16251. (defun org-recenter-calendar (date)
  16252. "If the calendar is visible, recenter it to DATE."
  16253. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16254. (when cwin
  16255. (let ((calendar-move-hook nil))
  16256. (with-selected-window cwin
  16257. (calendar-goto-date (if (listp date) date
  16258. (calendar-gregorian-from-absolute date))))))))
  16259. (defun org-goto-calendar (&optional arg)
  16260. "Go to the Emacs calendar at the current date.
  16261. If there is a time stamp in the current line, go to that date.
  16262. A prefix ARG can be used to force the current date."
  16263. (interactive "P")
  16264. (let ((tsr org-ts-regexp) diff
  16265. (calendar-move-hook nil)
  16266. (calendar-view-holidays-initially-flag nil)
  16267. (calendar-view-diary-initially-flag nil))
  16268. (if (or (org-at-timestamp-p)
  16269. (save-excursion
  16270. (beginning-of-line 1)
  16271. (looking-at (concat ".*" tsr))))
  16272. (let ((d1 (time-to-days (current-time)))
  16273. (d2 (time-to-days
  16274. (org-time-string-to-time (match-string 1)))))
  16275. (setq diff (- d2 d1))))
  16276. (calendar)
  16277. (calendar-goto-today)
  16278. (if (and diff (not arg)) (calendar-forward-day diff))))
  16279. (defun org-get-date-from-calendar ()
  16280. "Return a list (month day year) of date at point in calendar."
  16281. (with-current-buffer "*Calendar*"
  16282. (save-match-data
  16283. (calendar-cursor-to-date))))
  16284. (defun org-date-from-calendar ()
  16285. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16286. If there is already a time stamp at the cursor position, update it."
  16287. (interactive)
  16288. (if (org-at-timestamp-p t)
  16289. (org-timestamp-change 0 'calendar)
  16290. (let ((cal-date (org-get-date-from-calendar)))
  16291. (org-insert-time-stamp
  16292. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16293. (defcustom org-effort-durations
  16294. `(("h" . 60)
  16295. ("d" . ,(* 60 8))
  16296. ("w" . ,(* 60 8 5))
  16297. ("m" . ,(* 60 8 5 4))
  16298. ("y" . ,(* 60 8 5 40)))
  16299. "Conversion factor to minutes for an effort modifier.
  16300. Each entry has the form (MODIFIER . MINUTES).
  16301. In an effort string, a number followed by MODIFIER is multiplied
  16302. by the specified number of MINUTES to obtain an effort in
  16303. minutes.
  16304. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16305. effort string \"2hours\" is equivalent to 120 minutes."
  16306. :group 'org-agenda
  16307. :version "24.1"
  16308. :type '(alist :key-type (string :tag "Modifier")
  16309. :value-type (number :tag "Minutes")))
  16310. (defun org-minutes-to-clocksum-string (m)
  16311. "Format number of minutes as a clocksum string.
  16312. The format is determined by `org-time-clocksum-format',
  16313. `org-time-clocksum-use-fractional' and
  16314. `org-time-clocksum-fractional-format' and
  16315. `org-time-clocksum-use-effort-durations'."
  16316. (let ((clocksum "")
  16317. (m (round m)) ; Don't allow fractions of minutes
  16318. h d w mo y fmt n)
  16319. (setq h (if org-time-clocksum-use-effort-durations
  16320. (cdr (assoc "h" org-effort-durations)) 60)
  16321. d (if org-time-clocksum-use-effort-durations
  16322. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16323. w (if org-time-clocksum-use-effort-durations
  16324. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16325. mo (if org-time-clocksum-use-effort-durations
  16326. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16327. y (if org-time-clocksum-use-effort-durations
  16328. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16329. ;; fractional format
  16330. (if org-time-clocksum-use-fractional
  16331. (cond
  16332. ;; single format string
  16333. ((stringp org-time-clocksum-fractional-format)
  16334. (format org-time-clocksum-fractional-format (/ m (float h))))
  16335. ;; choice of fractional formats for different time units
  16336. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16337. (> (/ (truncate m) (* y d h)) 0))
  16338. (format fmt (/ m (* y d (float h)))))
  16339. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16340. (> (/ (truncate m) (* mo d h)) 0))
  16341. (format fmt (/ m (* mo d (float h)))))
  16342. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16343. (> (/ (truncate m) (* w d h)) 0))
  16344. (format fmt (/ m (* w d (float h)))))
  16345. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16346. (> (/ (truncate m) (* d h)) 0))
  16347. (format fmt (/ m (* d (float h)))))
  16348. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16349. (> (/ (truncate m) h) 0))
  16350. (format fmt (/ m (float h))))
  16351. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16352. (format fmt m))
  16353. ;; fall back to smallest time unit with a format
  16354. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16355. (format fmt (/ m (float h))))
  16356. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16357. (format fmt (/ m (* d (float h)))))
  16358. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16359. (format fmt (/ m (* w d (float h)))))
  16360. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16361. (format fmt (/ m (* mo d (float h)))))
  16362. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16363. (format fmt (/ m (* y d (float h))))))
  16364. ;; standard (non-fractional) format, with single format string
  16365. (if (stringp org-time-clocksum-format)
  16366. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16367. ;; separate formats components
  16368. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16369. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16370. (plist-get org-time-clocksum-format :require-years))
  16371. (setq clocksum (concat clocksum (format fmt n))
  16372. m (- m (* n y d h))))
  16373. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16374. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16375. (plist-get org-time-clocksum-format :require-months))
  16376. (setq clocksum (concat clocksum (format fmt n))
  16377. m (- m (* n mo d h))))
  16378. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16379. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16380. (plist-get org-time-clocksum-format :require-weeks))
  16381. (setq clocksum (concat clocksum (format fmt n))
  16382. m (- m (* n w d h))))
  16383. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16384. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16385. (plist-get org-time-clocksum-format :require-days))
  16386. (setq clocksum (concat clocksum (format fmt n))
  16387. m (- m (* n d h))))
  16388. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16389. (or (> (setq n (/ (truncate m) h)) 0)
  16390. (plist-get org-time-clocksum-format :require-hours))
  16391. (setq clocksum (concat clocksum (format fmt n))
  16392. m (- m (* n h))))
  16393. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16394. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16395. (setq clocksum (concat clocksum (format fmt m))))
  16396. ;; return formatted time duration
  16397. clocksum))))
  16398. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16399. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16400. "Org mode version 8.0")
  16401. (defun org-hours-to-clocksum-string (n)
  16402. (org-minutes-to-clocksum-string (* n 60)))
  16403. (defun org-hh:mm-string-to-minutes (s)
  16404. "Convert a string H:MM to a number of minutes.
  16405. If the string is just a number, interpret it as minutes.
  16406. In fact, the first hh:mm or number in the string will be taken,
  16407. there can be extra stuff in the string.
  16408. If no number is found, the return value is 0."
  16409. (cond
  16410. ((integerp s) s)
  16411. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16412. (+ (* (string-to-number (match-string 1 s)) 60)
  16413. (string-to-number (match-string 2 s))))
  16414. ((string-match "\\([0-9]+\\)" s)
  16415. (string-to-number (match-string 1 s)))
  16416. (t 0)))
  16417. (defcustom org-image-actual-width t
  16418. "Should we use the actual width of images when inlining them?
  16419. When set to `t', always use the image width.
  16420. When set to a number, use imagemagick (when available) to set
  16421. the image's width to this value.
  16422. When set to a number in a list, try to get the width from any
  16423. #+ATTR.* keyword if it matches a width specification like
  16424. #+ATTR_HTML: :width 300px
  16425. and fall back on that number if none is found.
  16426. When set to nil, try to get the width from an #+ATTR.* keyword
  16427. and fall back on the original width if none is found.
  16428. This requires Emacs >= 24.1, build with imagemagick support."
  16429. :group 'org-appearance
  16430. :version "24.4"
  16431. :package-version '(Org . "8.0")
  16432. :type '(choice
  16433. (const :tag "Use the image width" t)
  16434. (integer :tag "Use a number of pixels")
  16435. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16436. (const :tag "Use #+ATTR* or don't resize" nil)))
  16437. (defcustom org-agenda-inhibit-startup nil
  16438. "Inhibit startup when preparing agenda buffers.
  16439. When this variable is `t' (the default), the initialization of
  16440. the Org agenda buffers is inhibited: e.g. the visibility state
  16441. is not set, the tables are not re-aligned, etc."
  16442. :type 'boolean
  16443. :version "24.3"
  16444. :group 'org-agenda)
  16445. (define-obsolete-variable-alias
  16446. 'org-agenda-ignore-drawer-properties
  16447. 'org-agenda-ignore-properties "24.5")
  16448. (defcustom org-agenda-ignore-properties nil
  16449. "Avoid updating text properties when building the agenda.
  16450. Properties are used to prepare buffers for effort estimates,
  16451. appointments, statistics and subtree-local categories.
  16452. If you don't use these in the agenda, you can add them to this
  16453. list and agenda building will be a bit faster.
  16454. The value is a list, with zero or more of the symbols `effort', `appt',
  16455. `stats' or `category'."
  16456. :type '(set :greedy t
  16457. (const effort)
  16458. (const appt)
  16459. (const stats)
  16460. (const category))
  16461. :version "24.5"
  16462. :package-version '(Org . "8.3")
  16463. :group 'org-agenda)
  16464. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16465. "Convert a duration string S to minutes.
  16466. A bare number is interpreted as minutes, modifiers can be set by
  16467. customizing `org-effort-durations' (which see).
  16468. Entries containing a colon are interpreted as H:MM by
  16469. `org-hh:mm-string-to-minutes'."
  16470. (let ((result 0)
  16471. (re (concat "\\([0-9.]+\\) *\\("
  16472. (regexp-opt (mapcar 'car org-effort-durations))
  16473. "\\)")))
  16474. (while (string-match re s)
  16475. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16476. (string-to-number (match-string 1 s))))
  16477. (setq s (replace-match "" nil t s)))
  16478. (setq result (floor result))
  16479. (incf result (org-hh:mm-string-to-minutes s))
  16480. (if output-to-string (number-to-string result) result)))
  16481. ;;;; Files
  16482. (defun org-save-all-org-buffers ()
  16483. "Save all Org-mode buffers without user confirmation."
  16484. (interactive)
  16485. (message "Saving all Org-mode buffers...")
  16486. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16487. (when (featurep 'org-id) (org-id-locations-save))
  16488. (message "Saving all Org-mode buffers... done"))
  16489. (defun org-revert-all-org-buffers ()
  16490. "Revert all Org-mode buffers.
  16491. Prompt for confirmation when there are unsaved changes.
  16492. Be sure you know what you are doing before letting this function
  16493. overwrite your changes.
  16494. This function is useful in a setup where one tracks org files
  16495. with a version control system, to revert on one machine after pulling
  16496. changes from another. I believe the procedure must be like this:
  16497. 1. M-x org-save-all-org-buffers
  16498. 2. Pull changes from the other machine, resolve conflicts
  16499. 3. M-x org-revert-all-org-buffers"
  16500. (interactive)
  16501. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16502. (user-error "Abort"))
  16503. (save-excursion
  16504. (save-window-excursion
  16505. (mapc
  16506. (lambda (b)
  16507. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16508. (with-current-buffer b buffer-file-name))
  16509. (org-pop-to-buffer-same-window b)
  16510. (revert-buffer t 'no-confirm)))
  16511. (buffer-list))
  16512. (when (and (featurep 'org-id) org-id-track-globally)
  16513. (org-id-locations-load)))))
  16514. ;;;; Agenda files
  16515. ;;;###autoload
  16516. (defun org-switchb (&optional arg)
  16517. "Switch between Org buffers.
  16518. With one prefix argument, restrict available buffers to files.
  16519. With two prefix arguments, restrict available buffers to agenda files.
  16520. Defaults to `iswitchb' for buffer name completion.
  16521. Set `org-completion-use-ido' to make it use ido instead."
  16522. (interactive "P")
  16523. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16524. ((equal arg '(16)) (org-buffer-list 'agenda))
  16525. (t (org-buffer-list))))
  16526. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16527. (org-completion-use-ido org-completion-use-ido))
  16528. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16529. (setq org-completion-use-iswitchb t))
  16530. (org-pop-to-buffer-same-window
  16531. (org-icompleting-read "Org buffer: "
  16532. (mapcar 'list (mapcar 'buffer-name blist))
  16533. nil t))))
  16534. ;;; Define some older names previously used for this functionality
  16535. ;;;###autoload
  16536. (defalias 'org-ido-switchb 'org-switchb)
  16537. ;;;###autoload
  16538. (defalias 'org-iswitchb 'org-switchb)
  16539. (defun org-buffer-list (&optional predicate exclude-tmp)
  16540. "Return a list of Org buffers.
  16541. PREDICATE can be `export', `files' or `agenda'.
  16542. export restrict the list to Export buffers.
  16543. files restrict the list to buffers visiting Org files.
  16544. agenda restrict the list to buffers visiting agenda files.
  16545. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16546. (let* ((bfn nil)
  16547. (agenda-files (and (eq predicate 'agenda)
  16548. (mapcar 'file-truename (org-agenda-files t))))
  16549. (filter
  16550. (cond
  16551. ((eq predicate 'files)
  16552. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16553. ((eq predicate 'export)
  16554. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16555. ((eq predicate 'agenda)
  16556. (lambda (b)
  16557. (with-current-buffer b
  16558. (and (derived-mode-p 'org-mode)
  16559. (setq bfn (buffer-file-name b))
  16560. (member (file-truename bfn) agenda-files)))))
  16561. (t (lambda (b) (with-current-buffer b
  16562. (or (derived-mode-p 'org-mode)
  16563. (string-match "\*Org .*Export"
  16564. (buffer-name b)))))))))
  16565. (delq nil
  16566. (mapcar
  16567. (lambda(b)
  16568. (if (and (funcall filter b)
  16569. (or (not exclude-tmp)
  16570. (not (string-match "tmp" (buffer-name b)))))
  16571. b
  16572. nil))
  16573. (buffer-list)))))
  16574. (defun org-agenda-files (&optional unrestricted archives)
  16575. "Get the list of agenda files.
  16576. Optional UNRESTRICTED means return the full list even if a restriction
  16577. is currently in place.
  16578. When ARCHIVES is t, include all archive files that are really being
  16579. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16580. `org-agenda-archives-mode' is t."
  16581. (let ((files
  16582. (cond
  16583. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16584. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16585. ((listp org-agenda-files) org-agenda-files)
  16586. (t (error "Invalid value of `org-agenda-files'")))))
  16587. (setq files (apply 'append
  16588. (mapcar (lambda (f)
  16589. (if (file-directory-p f)
  16590. (directory-files
  16591. f t org-agenda-file-regexp)
  16592. (list f)))
  16593. files)))
  16594. (when org-agenda-skip-unavailable-files
  16595. (setq files (delq nil
  16596. (mapcar (function
  16597. (lambda (file)
  16598. (and (file-readable-p file) file)))
  16599. files))))
  16600. (when (or (eq archives t)
  16601. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16602. (setq files (org-add-archive-files files)))
  16603. files))
  16604. (defun org-agenda-file-p (&optional file)
  16605. "Return non-nil, if FILE is an agenda file.
  16606. If FILE is omitted, use the file associated with the current
  16607. buffer."
  16608. (member (or file (buffer-file-name))
  16609. (org-agenda-files t)))
  16610. (defun org-edit-agenda-file-list ()
  16611. "Edit the list of agenda files.
  16612. Depending on setup, this either uses customize to edit the variable
  16613. `org-agenda-files', or it visits the file that is holding the list. In the
  16614. latter case, the buffer is set up in a way that saving it automatically kills
  16615. the buffer and restores the previous window configuration."
  16616. (interactive)
  16617. (if (stringp org-agenda-files)
  16618. (let ((cw (current-window-configuration)))
  16619. (find-file org-agenda-files)
  16620. (org-set-local 'org-window-configuration cw)
  16621. (org-add-hook 'after-save-hook
  16622. (lambda ()
  16623. (set-window-configuration
  16624. (prog1 org-window-configuration
  16625. (kill-buffer (current-buffer))))
  16626. (org-install-agenda-files-menu)
  16627. (message "New agenda file list installed"))
  16628. nil 'local)
  16629. (message "%s" (substitute-command-keys
  16630. "Edit list and finish with \\[save-buffer]")))
  16631. (customize-variable 'org-agenda-files)))
  16632. (defun org-store-new-agenda-file-list (list)
  16633. "Set new value for the agenda file list and save it correctly."
  16634. (if (stringp org-agenda-files)
  16635. (let ((fe (org-read-agenda-file-list t)) b u)
  16636. (while (setq b (find-buffer-visiting org-agenda-files))
  16637. (kill-buffer b))
  16638. (with-temp-file org-agenda-files
  16639. (insert
  16640. (mapconcat
  16641. (lambda (f) ;; Keep un-expanded entries.
  16642. (if (setq u (assoc f fe))
  16643. (cdr u)
  16644. f))
  16645. list "\n")
  16646. "\n")))
  16647. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16648. (org-insert-mode-line-in-empty-file nil))
  16649. (setq org-agenda-files list)
  16650. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16651. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16652. "Read the list of agenda files from a file.
  16653. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16654. filenames, used by `org-store-new-agenda-file-list' to write back
  16655. un-expanded file names."
  16656. (when (file-directory-p org-agenda-files)
  16657. (error "`org-agenda-files' cannot be a single directory"))
  16658. (when (stringp org-agenda-files)
  16659. (with-temp-buffer
  16660. (insert-file-contents org-agenda-files)
  16661. (mapcar
  16662. (lambda (f)
  16663. (let ((e (expand-file-name (substitute-in-file-name f)
  16664. org-directory)))
  16665. (if pair-with-expansion
  16666. (cons e f)
  16667. e)))
  16668. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16669. ;;;###autoload
  16670. (defun org-cycle-agenda-files ()
  16671. "Cycle through the files in `org-agenda-files'.
  16672. If the current buffer visits an agenda file, find the next one in the list.
  16673. If the current buffer does not, find the first agenda file."
  16674. (interactive)
  16675. (let* ((fs (org-agenda-files t))
  16676. (files (append fs (list (car fs))))
  16677. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16678. file)
  16679. (unless files (user-error "No agenda files"))
  16680. (catch 'exit
  16681. (while (setq file (pop files))
  16682. (if (equal (file-truename file) tcf)
  16683. (when (car files)
  16684. (find-file (car files))
  16685. (throw 'exit t))))
  16686. (find-file (car fs)))
  16687. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16688. (defun org-agenda-file-to-front (&optional to-end)
  16689. "Move/add the current file to the top of the agenda file list.
  16690. If the file is not present in the list, it is added to the front. If it is
  16691. present, it is moved there. With optional argument TO-END, add/move to the
  16692. end of the list."
  16693. (interactive "P")
  16694. (let ((org-agenda-skip-unavailable-files nil)
  16695. (file-alist (mapcar (lambda (x)
  16696. (cons (file-truename x) x))
  16697. (org-agenda-files t)))
  16698. (ctf (file-truename
  16699. (or buffer-file-name
  16700. (user-error "Please save the current buffer to a file"))))
  16701. x had)
  16702. (setq x (assoc ctf file-alist) had x)
  16703. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16704. (if to-end
  16705. (setq file-alist (append (delq x file-alist) (list x)))
  16706. (setq file-alist (cons x (delq x file-alist))))
  16707. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16708. (org-install-agenda-files-menu)
  16709. (message "File %s to %s of agenda file list"
  16710. (if had "moved" "added") (if to-end "end" "front"))))
  16711. (defun org-remove-file (&optional file)
  16712. "Remove current file from the list of files in variable `org-agenda-files'.
  16713. These are the files which are being checked for agenda entries.
  16714. Optional argument FILE means use this file instead of the current."
  16715. (interactive)
  16716. (let* ((org-agenda-skip-unavailable-files nil)
  16717. (file (or file buffer-file-name
  16718. (user-error "Current buffer does not visit a file")))
  16719. (true-file (file-truename file))
  16720. (afile (abbreviate-file-name file))
  16721. (files (delq nil (mapcar
  16722. (lambda (x)
  16723. (if (equal true-file
  16724. (file-truename x))
  16725. nil x))
  16726. (org-agenda-files t)))))
  16727. (if (not (= (length files) (length (org-agenda-files t))))
  16728. (progn
  16729. (org-store-new-agenda-file-list files)
  16730. (org-install-agenda-files-menu)
  16731. (message "Removed file: %s" afile))
  16732. (message "File was not in list: %s (not removed)" afile))))
  16733. (defun org-file-menu-entry (file)
  16734. (vector file (list 'find-file file) t))
  16735. (defun org-check-agenda-file (file)
  16736. "Make sure FILE exists. If not, ask user what to do."
  16737. (when (not (file-exists-p file))
  16738. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16739. (abbreviate-file-name file))
  16740. (let ((r (downcase (read-char-exclusive))))
  16741. (cond
  16742. ((equal r ?r)
  16743. (org-remove-file file)
  16744. (throw 'nextfile t))
  16745. (t (user-error "Abort"))))))
  16746. (defun org-get-agenda-file-buffer (file)
  16747. "Get an agenda buffer visiting FILE.
  16748. If the buffer needs to be created, add it to the list of buffers
  16749. which might be released later."
  16750. (let ((buf (org-find-base-buffer-visiting file)))
  16751. (if buf
  16752. buf ; just return it
  16753. ;; Make a new buffer and remember it
  16754. (setq buf (find-file-noselect file))
  16755. (if buf (push buf org-agenda-new-buffers))
  16756. buf)))
  16757. (defun org-release-buffers (blist)
  16758. "Release all buffers in list, asking the user for confirmation when needed.
  16759. When a buffer is unmodified, it is just killed. When modified, it is saved
  16760. \(if the user agrees) and then killed."
  16761. (let (buf file)
  16762. (while (setq buf (pop blist))
  16763. (setq file (buffer-file-name buf))
  16764. (when (and (buffer-modified-p buf)
  16765. file
  16766. (y-or-n-p (format "Save file %s? " file)))
  16767. (with-current-buffer buf (save-buffer)))
  16768. (kill-buffer buf))))
  16769. (defun org-agenda-prepare-buffers (files)
  16770. "Create buffers for all agenda files, protect archived trees and comments."
  16771. (interactive)
  16772. (let ((pa '(:org-archived t))
  16773. (pc '(:org-comment t))
  16774. (pall '(:org-archived t :org-comment t))
  16775. (inhibit-read-only t)
  16776. (org-inhibit-startup org-agenda-inhibit-startup)
  16777. (rea (concat ":" org-archive-tag ":"))
  16778. file re pos)
  16779. (setq org-tag-alist-for-agenda nil
  16780. org-tag-groups-alist-for-agenda nil)
  16781. (save-excursion
  16782. (save-restriction
  16783. (while (setq file (pop files))
  16784. (catch 'nextfile
  16785. (if (bufferp file)
  16786. (set-buffer file)
  16787. (org-check-agenda-file file)
  16788. (set-buffer (org-get-agenda-file-buffer file)))
  16789. (widen)
  16790. (org-set-regexps-and-options-for-tags)
  16791. (setq pos (point))
  16792. (goto-char (point-min))
  16793. (let ((case-fold-search t))
  16794. (when (search-forward "#+setupfile" nil t)
  16795. ;; Don't set all regexps and options systematically as
  16796. ;; this is only run for setting agenda tags from setup
  16797. ;; file
  16798. (org-set-regexps-and-options)))
  16799. (or (memq 'category org-agenda-ignore-properties)
  16800. (org-refresh-category-properties))
  16801. (or (memq 'stats org-agenda-ignore-properties)
  16802. (org-refresh-stats-properties))
  16803. (or (memq 'effort org-agenda-ignore-properties)
  16804. (org-refresh-properties
  16805. org-effort-property
  16806. '((effort . identity)
  16807. (effort-minutes . org-duration-string-to-minutes))))
  16808. (or (memq 'appt org-agenda-ignore-properties)
  16809. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  16810. (setq org-todo-keywords-for-agenda
  16811. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16812. (setq org-done-keywords-for-agenda
  16813. (append org-done-keywords-for-agenda org-done-keywords))
  16814. (setq org-todo-keyword-alist-for-agenda
  16815. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16816. (setq org-tag-alist-for-agenda
  16817. (org-uniquify
  16818. (append org-tag-alist-for-agenda
  16819. org-tag-alist
  16820. org-tag-persistent-alist)))
  16821. (if org-group-tags
  16822. (setq org-tag-groups-alist-for-agenda
  16823. (org-uniquify-alist
  16824. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16825. (org-with-silent-modifications
  16826. (save-excursion
  16827. (remove-text-properties (point-min) (point-max) pall)
  16828. (when org-agenda-skip-archived-trees
  16829. (goto-char (point-min))
  16830. (while (re-search-forward rea nil t)
  16831. (if (org-at-heading-p t)
  16832. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16833. (goto-char (point-min))
  16834. (setq re (format "^\\* .*\\<%s\\>" org-comment-string))
  16835. (while (re-search-forward re nil t)
  16836. (when (save-match-data (org-in-commented-heading-p t))
  16837. (add-text-properties
  16838. (match-beginning 0) (org-end-of-subtree t) pc)))))
  16839. (goto-char pos)))))
  16840. (setq org-todo-keywords-for-agenda
  16841. (org-uniquify org-todo-keywords-for-agenda))
  16842. (setq org-todo-keyword-alist-for-agenda
  16843. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16844. ;;;; CDLaTeX minor mode
  16845. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16846. "Keymap for the minor `org-cdlatex-mode'.")
  16847. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16848. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16849. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16850. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16851. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16852. (defvar org-cdlatex-texmathp-advice-is-done nil
  16853. "Flag remembering if we have applied the advice to texmathp already.")
  16854. (define-minor-mode org-cdlatex-mode
  16855. "Toggle the minor `org-cdlatex-mode'.
  16856. This mode supports entering LaTeX environment and math in LaTeX fragments
  16857. in Org-mode.
  16858. \\{org-cdlatex-mode-map}"
  16859. nil " OCDL" nil
  16860. (when org-cdlatex-mode
  16861. (require 'cdlatex)
  16862. (run-hooks 'cdlatex-mode-hook)
  16863. (cdlatex-compute-tables))
  16864. (unless org-cdlatex-texmathp-advice-is-done
  16865. (setq org-cdlatex-texmathp-advice-is-done t)
  16866. (defadvice texmathp (around org-math-always-on activate)
  16867. "Always return t in org-mode buffers.
  16868. This is because we want to insert math symbols without dollars even outside
  16869. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16870. an embedded LaTeX fragment, let texmathp do its job.
  16871. \\[org-cdlatex-mode-map]"
  16872. (interactive)
  16873. (let (p)
  16874. (cond
  16875. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16876. ((eq this-command 'cdlatex-math-symbol)
  16877. (setq ad-return-value t
  16878. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16879. (t
  16880. (let ((p (org-inside-LaTeX-fragment-p)))
  16881. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16882. (setq ad-return-value t
  16883. texmathp-why '("Org-mode embedded math" . 0))
  16884. (if p ad-do-it)))))))))
  16885. (defun turn-on-org-cdlatex ()
  16886. "Unconditionally turn on `org-cdlatex-mode'."
  16887. (org-cdlatex-mode 1))
  16888. (defun org-try-cdlatex-tab ()
  16889. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16890. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16891. - inside a LaTeX fragment, or
  16892. - after the first word in a line, where an abbreviation expansion could
  16893. insert a LaTeX environment."
  16894. (when org-cdlatex-mode
  16895. (cond
  16896. ;; Before any word on the line: No expansion possible.
  16897. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16898. ;; Just after first word on the line: Expand it. Make sure it
  16899. ;; cannot happen on headlines, though.
  16900. ((save-excursion
  16901. (skip-chars-backward "a-zA-Z0-9*")
  16902. (skip-chars-backward " \t")
  16903. (and (bolp) (not (org-at-heading-p))))
  16904. (cdlatex-tab) t)
  16905. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16906. (defun org-cdlatex-underscore-caret (&optional arg)
  16907. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16908. Revert to the normal definition outside of these fragments."
  16909. (interactive "P")
  16910. (if (org-inside-LaTeX-fragment-p)
  16911. (call-interactively 'cdlatex-sub-superscript)
  16912. (let (org-cdlatex-mode)
  16913. (call-interactively (key-binding (vector last-input-event))))))
  16914. (defun org-cdlatex-math-modify (&optional arg)
  16915. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16916. Revert to the normal definition outside of these fragments."
  16917. (interactive "P")
  16918. (if (org-inside-LaTeX-fragment-p)
  16919. (call-interactively 'cdlatex-math-modify)
  16920. (let (org-cdlatex-mode)
  16921. (call-interactively (key-binding (vector last-input-event))))))
  16922. ;;;; LaTeX fragments
  16923. (defun org-inside-LaTeX-fragment-p ()
  16924. "Test if point is inside a LaTeX fragment.
  16925. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16926. sequence appearing also before point.
  16927. Even though the matchers for math are configurable, this function assumes
  16928. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16929. delimiters are skipped when they have been removed by customization.
  16930. The return value is nil, or a cons cell with the delimiter and the
  16931. position of this delimiter.
  16932. This function does a reasonably good job, but can locally be fooled by
  16933. for example currency specifications. For example it will assume being in
  16934. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16935. fragments that are properly closed, but during editing, we have to live
  16936. with the uncertainty caused by missing closing delimiters. This function
  16937. looks only before point, not after."
  16938. (catch 'exit
  16939. (let ((pos (point))
  16940. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16941. (lim (progn
  16942. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16943. (point)))
  16944. dd-on str (start 0) m re)
  16945. (goto-char pos)
  16946. (when dodollar
  16947. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16948. re (nth 1 (assoc "$" org-latex-regexps)))
  16949. (while (string-match re str start)
  16950. (cond
  16951. ((= (match-end 0) (length str))
  16952. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16953. ((= (match-end 0) (- (length str) 5))
  16954. (throw 'exit nil))
  16955. (t (setq start (match-end 0))))))
  16956. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16957. (goto-char pos)
  16958. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16959. (and (match-beginning 2) (throw 'exit nil))
  16960. ;; count $$
  16961. (while (re-search-backward "\\$\\$" lim t)
  16962. (setq dd-on (not dd-on)))
  16963. (goto-char pos)
  16964. (if dd-on (cons "$$" m))))))
  16965. (defun org-inside-latex-macro-p ()
  16966. "Is point inside a LaTeX macro or its arguments?"
  16967. (save-match-data
  16968. (org-in-regexp
  16969. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16970. (defvar org-latex-fragment-image-overlays nil
  16971. "List of overlays carrying the images of latex fragments.")
  16972. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16973. (defun org-remove-latex-fragment-image-overlays ()
  16974. "Remove all overlays with LaTeX fragment images in current buffer."
  16975. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16976. (setq org-latex-fragment-image-overlays nil))
  16977. (defun org-preview-latex-fragment (&optional subtree)
  16978. "Preview the LaTeX fragment at point, or all locally or globally.
  16979. If the cursor is in a LaTeX fragment, create the image and overlay
  16980. it over the source code. If there is no fragment at point, display
  16981. all fragments in the current text, from one headline to the next. With
  16982. prefix SUBTREE, display all fragments in the current subtree. With a
  16983. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16984. the cursor is before the first headline,
  16985. display all fragments in the buffer.
  16986. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16987. (interactive "P")
  16988. (unless buffer-file-name
  16989. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  16990. (when (display-graphic-p)
  16991. (org-remove-latex-fragment-image-overlays)
  16992. (save-excursion
  16993. (save-restriction
  16994. (let (beg end at msg)
  16995. (cond
  16996. ((or (equal subtree '(16))
  16997. (not (save-excursion
  16998. (re-search-backward org-outline-regexp-bol nil t))))
  16999. (setq beg (point-min) end (point-max)
  17000. msg "Creating images for buffer...%s"))
  17001. ((equal subtree '(4))
  17002. (org-back-to-heading)
  17003. (setq beg (point) end (org-end-of-subtree t)
  17004. msg "Creating images for subtree...%s"))
  17005. (t
  17006. (if (setq at (org-inside-LaTeX-fragment-p))
  17007. (goto-char (max (point-min) (- (cdr at) 2)))
  17008. (org-back-to-heading))
  17009. (setq beg (point) end (progn (outline-next-heading) (point))
  17010. msg (if at "Creating image...%s"
  17011. "Creating images for entry...%s"))))
  17012. (message msg "")
  17013. (narrow-to-region beg end)
  17014. (goto-char beg)
  17015. (org-format-latex
  17016. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  17017. (file-name-nondirectory
  17018. buffer-file-name)))
  17019. default-directory 'overlays msg at 'forbuffer
  17020. org-latex-create-formula-image-program)
  17021. (message msg "done. Use `C-c C-c' to remove images."))))))
  17022. (defun org-format-latex (prefix &optional dir overlays msg at
  17023. forbuffer processing-type)
  17024. "Replace LaTeX fragments with links to an image, and produce images.
  17025. Some of the options can be changed using the variable
  17026. `org-format-latex-options'."
  17027. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17028. (let* ((prefixnodir (file-name-nondirectory prefix))
  17029. (absprefix (expand-file-name prefix dir))
  17030. (todir (file-name-directory absprefix))
  17031. (opt org-format-latex-options)
  17032. (optnew org-format-latex-options)
  17033. (matchers (plist-get opt :matchers))
  17034. (re-list org-latex-regexps)
  17035. (cnt 0) txt hash link beg end re e checkdir
  17036. string
  17037. m n block-type block linkfile movefile ov)
  17038. ;; Check the different regular expressions
  17039. (while (setq e (pop re-list))
  17040. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  17041. block (if block-type "\n\n" ""))
  17042. (when (member m matchers)
  17043. (goto-char (point-min))
  17044. (while (re-search-forward re nil t)
  17045. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  17046. (or (not overlays)
  17047. (not (eq (get-char-property (match-beginning n)
  17048. 'org-overlay-type)
  17049. 'org-latex-overlay))))
  17050. (cond
  17051. ((eq processing-type 'verbatim))
  17052. ((eq processing-type 'mathjax)
  17053. ;; Prepare for MathJax processing.
  17054. (setq string (match-string n))
  17055. (when (member m '("$" "$1"))
  17056. (save-excursion
  17057. (delete-region (match-beginning n) (match-end n))
  17058. (goto-char (match-beginning n))
  17059. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  17060. ((or (eq processing-type 'dvipng)
  17061. (eq processing-type 'imagemagick))
  17062. ;; Process to an image.
  17063. (setq txt (match-string n)
  17064. beg (match-beginning n) end (match-end n)
  17065. cnt (1+ cnt))
  17066. (let ((face (face-at-point))
  17067. (fg (plist-get opt :foreground))
  17068. (bg (plist-get opt :background))
  17069. ;; Ensure full list is printed.
  17070. print-length print-level)
  17071. (when forbuffer
  17072. ;; Get the colors from the face at point.
  17073. (goto-char beg)
  17074. (when (eq fg 'auto)
  17075. (setq fg (face-attribute face :foreground nil 'default)))
  17076. (when (eq bg 'auto)
  17077. (setq bg (face-attribute face :background nil 'default)))
  17078. (setq optnew (copy-sequence opt))
  17079. (plist-put optnew :foreground fg)
  17080. (plist-put optnew :background bg))
  17081. (setq hash (sha1 (prin1-to-string
  17082. (list org-format-latex-header
  17083. org-latex-default-packages-alist
  17084. org-latex-packages-alist
  17085. org-format-latex-options
  17086. forbuffer txt fg bg)))
  17087. linkfile (format "%s_%s.png" prefix hash)
  17088. movefile (format "%s_%s.png" absprefix hash)))
  17089. (setq link (concat block "[[file:" linkfile "]]" block))
  17090. (if msg (message msg cnt))
  17091. (goto-char beg)
  17092. (unless checkdir ; Ensure the directory exists.
  17093. (setq checkdir t)
  17094. (or (file-directory-p todir) (make-directory todir t)))
  17095. (unless (file-exists-p movefile)
  17096. (org-create-formula-image
  17097. txt movefile optnew forbuffer processing-type))
  17098. (if overlays
  17099. (progn
  17100. (mapc (lambda (o)
  17101. (if (eq (overlay-get o 'org-overlay-type)
  17102. 'org-latex-overlay)
  17103. (delete-overlay o)))
  17104. (overlays-in beg end))
  17105. (setq ov (make-overlay beg end))
  17106. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  17107. (if (featurep 'xemacs)
  17108. (progn
  17109. (overlay-put ov 'invisible t)
  17110. (overlay-put
  17111. ov 'end-glyph
  17112. (make-glyph (vector 'png :file movefile))))
  17113. (overlay-put
  17114. ov 'display
  17115. (list 'image :type 'png :file movefile :ascent 'center)))
  17116. (push ov org-latex-fragment-image-overlays)
  17117. (goto-char end))
  17118. (delete-region beg end)
  17119. (insert (org-add-props link
  17120. (list 'org-latex-src
  17121. (replace-regexp-in-string
  17122. "\"" "" txt)
  17123. 'org-latex-src-embed-type
  17124. (if block-type 'paragraph 'character))))))
  17125. ((eq processing-type 'mathml)
  17126. ;; Process to MathML
  17127. (unless (save-match-data (org-format-latex-mathml-available-p))
  17128. (user-error "LaTeX to MathML converter not configured"))
  17129. (setq txt (match-string n)
  17130. beg (match-beginning n) end (match-end n)
  17131. cnt (1+ cnt))
  17132. (if msg (message msg cnt))
  17133. (goto-char beg)
  17134. (delete-region beg end)
  17135. (insert (org-format-latex-as-mathml
  17136. txt block-type prefix dir)))
  17137. (t
  17138. (error "Unknown conversion type %s for LaTeX fragments"
  17139. processing-type)))))))))
  17140. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17141. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17142. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17143. sucessful, return the portion between \"<math...> </math>\"
  17144. elements otherwise return nil. When MATHML-FILE is specified,
  17145. write the results in to that file. When invoked as an
  17146. interactive command, prompt for LATEX-FRAG, with initial value
  17147. set to the current active region and echo the results for user
  17148. inspection."
  17149. (interactive (list (let ((frag (when (org-region-active-p)
  17150. (buffer-substring-no-properties
  17151. (region-beginning) (region-end)))))
  17152. (read-string "LaTeX Fragment: " frag nil frag))))
  17153. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17154. (let* ((tmp-in-file (file-relative-name
  17155. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17156. (ignore (write-region latex-frag nil tmp-in-file))
  17157. (tmp-out-file (file-relative-name
  17158. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17159. (cmd (format-spec
  17160. org-latex-to-mathml-convert-command
  17161. `((?j . ,(shell-quote-argument
  17162. (expand-file-name org-latex-to-mathml-jar-file)))
  17163. (?I . ,(shell-quote-argument tmp-in-file))
  17164. (?o . ,(shell-quote-argument tmp-out-file)))))
  17165. mathml shell-command-output)
  17166. (when (org-called-interactively-p 'any)
  17167. (unless (org-format-latex-mathml-available-p)
  17168. (user-error "LaTeX to MathML converter not configured")))
  17169. (message "Running %s" cmd)
  17170. (setq shell-command-output (shell-command-to-string cmd))
  17171. (setq mathml
  17172. (when (file-readable-p tmp-out-file)
  17173. (with-current-buffer (find-file-noselect tmp-out-file t)
  17174. (goto-char (point-min))
  17175. (when (re-search-forward
  17176. (concat
  17177. (regexp-quote
  17178. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  17179. "\\(.\\|\n\\)*"
  17180. (regexp-quote "</math>")) nil t)
  17181. (prog1 (match-string 0) (kill-buffer))))))
  17182. (cond
  17183. (mathml
  17184. (setq mathml
  17185. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17186. (when mathml-file
  17187. (write-region mathml nil mathml-file))
  17188. (when (org-called-interactively-p 'any)
  17189. (message mathml)))
  17190. ((message "LaTeX to MathML conversion failed")
  17191. (message shell-command-output)))
  17192. (delete-file tmp-in-file)
  17193. (when (file-exists-p tmp-out-file)
  17194. (delete-file tmp-out-file))
  17195. mathml))
  17196. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17197. prefix &optional dir)
  17198. "Use `org-create-math-formula' but check local cache first."
  17199. (let* ((absprefix (expand-file-name prefix dir))
  17200. (print-length nil) (print-level nil)
  17201. (formula-id (concat
  17202. "formula-"
  17203. (sha1
  17204. (prin1-to-string
  17205. (list latex-frag
  17206. org-latex-to-mathml-convert-command)))))
  17207. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17208. (formula-cache-dir (file-name-directory formula-cache)))
  17209. (unless (file-directory-p formula-cache-dir)
  17210. (make-directory formula-cache-dir t))
  17211. (unless (file-exists-p formula-cache)
  17212. (org-create-math-formula latex-frag formula-cache))
  17213. (if (file-exists-p formula-cache)
  17214. ;; Successful conversion. Return the link to MathML file.
  17215. (org-add-props
  17216. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17217. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17218. 'org-latex-src-embed-type (if latex-frag-type
  17219. 'paragraph 'character)))
  17220. ;; Failed conversion. Return the LaTeX fragment verbatim
  17221. latex-frag)))
  17222. (defun org-create-formula-image (string tofile options buffer &optional type)
  17223. "Create an image from LaTeX source using dvipng or convert.
  17224. This function calls either `org-create-formula-image-with-dvipng'
  17225. or `org-create-formula-image-with-imagemagick' depending on the
  17226. value of `org-latex-create-formula-image-program' or on the value
  17227. of the optional TYPE variable.
  17228. Note: ultimately these two function should be combined as they
  17229. share a good deal of logic."
  17230. (org-check-external-command
  17231. "latex" "needed to convert LaTeX fragments to images")
  17232. (funcall
  17233. (case (or type org-latex-create-formula-image-program)
  17234. ('dvipng
  17235. (org-check-external-command
  17236. "dvipng" "needed to convert LaTeX fragments to images")
  17237. #'org-create-formula-image-with-dvipng)
  17238. ('imagemagick
  17239. (org-check-external-command
  17240. "convert" "you need to install imagemagick")
  17241. #'org-create-formula-image-with-imagemagick)
  17242. (t (error
  17243. "Invalid value of `org-latex-create-formula-image-program'")))
  17244. string tofile options buffer))
  17245. (declare-function org-export-get-backend "ox" (name))
  17246. (declare-function org-export--get-global-options "ox" (&optional backend))
  17247. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17248. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17249. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17250. (defun org-create-formula--latex-header ()
  17251. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17252. (let ((info (org-combine-plists (org-export--get-global-options
  17253. (org-export-get-backend 'latex))
  17254. (org-export--get-inbuffer-options
  17255. (org-export-get-backend 'latex)))))
  17256. (org-latex-guess-babel-language
  17257. (org-latex-guess-inputenc
  17258. (org-splice-latex-header
  17259. org-format-latex-header
  17260. org-latex-default-packages-alist
  17261. org-latex-packages-alist t
  17262. (plist-get info :latex-header)))
  17263. info)))
  17264. ;; This function borrows from Ganesh Swami's latex2png.el
  17265. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17266. "This calls dvipng."
  17267. (require 'ox-latex)
  17268. (let* ((tmpdir (if (featurep 'xemacs)
  17269. (temp-directory)
  17270. temporary-file-directory))
  17271. (texfilebase (make-temp-name
  17272. (expand-file-name "orgtex" tmpdir)))
  17273. (texfile (concat texfilebase ".tex"))
  17274. (dvifile (concat texfilebase ".dvi"))
  17275. (pngfile (concat texfilebase ".png"))
  17276. (fnh (if (featurep 'xemacs)
  17277. (font-height (face-font 'default))
  17278. (face-attribute 'default :height nil)))
  17279. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17280. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17281. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17282. "Black"))
  17283. (bg (or (plist-get options (if buffer :background :html-background))
  17284. "Transparent")))
  17285. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17286. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17287. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17288. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17289. (let ((latex-header (org-create-formula--latex-header)))
  17290. (with-temp-file texfile
  17291. (insert latex-header)
  17292. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17293. (let ((dir default-directory))
  17294. (condition-case nil
  17295. (progn
  17296. (cd tmpdir)
  17297. (call-process "latex" nil nil nil texfile))
  17298. (error nil))
  17299. (cd dir))
  17300. (if (not (file-exists-p dvifile))
  17301. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17302. (condition-case nil
  17303. (if (featurep 'xemacs)
  17304. (call-process "dvipng" nil nil nil
  17305. "-fg" fg "-bg" bg
  17306. "-T" "tight"
  17307. "-o" pngfile
  17308. dvifile)
  17309. (call-process "dvipng" nil nil nil
  17310. "-fg" fg "-bg" bg
  17311. "-D" dpi
  17312. ;;"-x" scale "-y" scale
  17313. "-T" "tight"
  17314. "-o" pngfile
  17315. dvifile))
  17316. (error nil))
  17317. (if (not (file-exists-p pngfile))
  17318. (if org-format-latex-signal-error
  17319. (error "Failed to create png file from %s" texfile)
  17320. (message "Failed to create png file from %s" texfile)
  17321. nil)
  17322. ;; Use the requested file name and clean up
  17323. (copy-file pngfile tofile 'replace)
  17324. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17325. (if (file-exists-p (concat texfilebase e))
  17326. (delete-file (concat texfilebase e))))
  17327. pngfile))))
  17328. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17329. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17330. "This calls convert, which is included into imagemagick."
  17331. (require 'ox-latex)
  17332. (let* ((tmpdir (if (featurep 'xemacs)
  17333. (temp-directory)
  17334. temporary-file-directory))
  17335. (texfilebase (make-temp-name
  17336. (expand-file-name "orgtex" tmpdir)))
  17337. (texfile (concat texfilebase ".tex"))
  17338. (pdffile (concat texfilebase ".pdf"))
  17339. (pngfile (concat texfilebase ".png"))
  17340. (fnh (if (featurep 'xemacs)
  17341. (font-height (face-font 'default))
  17342. (face-attribute 'default :height nil)))
  17343. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17344. (dpi (number-to-string (* scale (floor (if buffer fnh 120.)))))
  17345. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17346. "black"))
  17347. (bg (or (plist-get options (if buffer :background :html-background))
  17348. "white")))
  17349. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17350. (setq fg (org-latex-color-format fg)))
  17351. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17352. (setq bg (org-latex-color-format
  17353. (if (string= bg "Transparent") "white" bg))))
  17354. (let ((latex-header (org-create-formula--latex-header)))
  17355. (with-temp-file texfile
  17356. (insert latex-header)
  17357. (insert "\n\\begin{document}\n"
  17358. "\\definecolor{fg}{rgb}{" fg "}\n"
  17359. "\\definecolor{bg}{rgb}{" bg "}\n"
  17360. "\n\\pagecolor{bg}\n"
  17361. "\n{\\color{fg}\n"
  17362. string
  17363. "\n}\n"
  17364. "\n\\end{document}\n")))
  17365. (org-latex-compile texfile t)
  17366. (if (not (file-exists-p pdffile))
  17367. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17368. (condition-case nil
  17369. (if (featurep 'xemacs)
  17370. (call-process "convert" nil nil nil
  17371. "-density" "96"
  17372. "-trim"
  17373. "-antialias"
  17374. pdffile
  17375. "-quality" "100"
  17376. ;; "-sharpen" "0x1.0"
  17377. pngfile)
  17378. (call-process "convert" nil nil nil
  17379. "-density" dpi
  17380. "-trim"
  17381. "-antialias"
  17382. pdffile
  17383. "-quality" "100"
  17384. ;; "-sharpen" "0x1.0"
  17385. pngfile))
  17386. (error nil))
  17387. (if (not (file-exists-p pngfile))
  17388. (if org-format-latex-signal-error
  17389. (error "Failed to create png file from %s" texfile)
  17390. (message "Failed to create png file from %s" texfile)
  17391. nil)
  17392. ;; Use the requested file name and clean up
  17393. (copy-file pngfile tofile 'replace)
  17394. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17395. (if (file-exists-p (concat texfilebase e))
  17396. (delete-file (concat texfilebase e))))
  17397. pngfile))))
  17398. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17399. "Fill a LaTeX header template TPL.
  17400. In the template, the following place holders will be recognized:
  17401. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17402. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17403. [PACKAGES] \\usepackage statements for PKG
  17404. [NO-PACKAGES] do not include PKG
  17405. [EXTRA] the string EXTRA
  17406. [NO-EXTRA] do not include EXTRA
  17407. For backward compatibility, if both the positive and the negative place
  17408. holder is missing, the positive one (without the \"NO-\") will be
  17409. assumed to be present at the end of the template.
  17410. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17411. EXTRA is a string.
  17412. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17413. (let (rpl (end ""))
  17414. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17415. (setq rpl (if (or (match-end 1) (not def-pkg))
  17416. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17417. tpl (replace-match rpl t t tpl))
  17418. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17419. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17420. (setq rpl (if (or (match-end 1) (not pkg))
  17421. "" (org-latex-packages-to-string pkg snippets-p t))
  17422. tpl (replace-match rpl t t tpl))
  17423. (if pkg (setq end
  17424. (concat end "\n"
  17425. (org-latex-packages-to-string pkg snippets-p)))))
  17426. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17427. (setq rpl (if (or (match-end 1) (not extra))
  17428. "" (concat extra "\n"))
  17429. tpl (replace-match rpl t t tpl))
  17430. (if (and extra (string-match "\\S-" extra))
  17431. (setq end (concat end "\n" extra))))
  17432. (if (string-match "\\S-" end)
  17433. (concat tpl "\n" end)
  17434. tpl)))
  17435. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17436. "Turn an alist of packages into a string with the \\usepackage macros."
  17437. (setq pkg (mapconcat (lambda(p)
  17438. (cond
  17439. ((stringp p) p)
  17440. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17441. (format "%% Package %s omitted" (cadr p)))
  17442. ((equal "" (car p))
  17443. (format "\\usepackage{%s}" (cadr p)))
  17444. (t
  17445. (format "\\usepackage[%s]{%s}"
  17446. (car p) (cadr p)))))
  17447. pkg
  17448. "\n"))
  17449. (if newline (concat pkg "\n") pkg))
  17450. (defun org-dvipng-color (attr)
  17451. "Return a RGB color specification for dvipng."
  17452. (apply 'format "rgb %s %s %s"
  17453. (mapcar 'org-normalize-color
  17454. (if (featurep 'xemacs)
  17455. (color-rgb-components
  17456. (face-property 'default
  17457. (cond ((eq attr :foreground) 'foreground)
  17458. ((eq attr :background) 'background))))
  17459. (color-values (face-attribute 'default attr nil))))))
  17460. (defun org-dvipng-color-format (color-name)
  17461. "Convert COLOR-NAME to a RGB color value for dvipng."
  17462. (apply 'format "rgb %s %s %s"
  17463. (mapcar 'org-normalize-color
  17464. (color-values color-name))))
  17465. (defun org-latex-color (attr)
  17466. "Return a RGB color for the LaTeX color package."
  17467. (apply 'format "%s,%s,%s"
  17468. (mapcar 'org-normalize-color
  17469. (if (featurep 'xemacs)
  17470. (color-rgb-components
  17471. (face-property 'default
  17472. (cond ((eq attr :foreground) 'foreground)
  17473. ((eq attr :background) 'background))))
  17474. (color-values (face-attribute 'default attr nil))))))
  17475. (defun org-latex-color-format (color-name)
  17476. "Convert COLOR-NAME to a RGB color value."
  17477. (apply 'format "%s,%s,%s"
  17478. (mapcar 'org-normalize-color
  17479. (color-values color-name))))
  17480. (defun org-normalize-color (value)
  17481. "Return string to be used as color value for an RGB component."
  17482. (format "%g" (/ value 65535.0)))
  17483. ;; Image display
  17484. (defvar org-inline-image-overlays nil)
  17485. (make-variable-buffer-local 'org-inline-image-overlays)
  17486. (defun org-toggle-inline-images (&optional include-linked)
  17487. "Toggle the display of inline images.
  17488. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17489. (interactive "P")
  17490. (if org-inline-image-overlays
  17491. (progn
  17492. (org-remove-inline-images)
  17493. (message "Inline image display turned off"))
  17494. (org-display-inline-images include-linked)
  17495. (if (and (org-called-interactively-p)
  17496. org-inline-image-overlays)
  17497. (message "%d images displayed inline"
  17498. (length org-inline-image-overlays))
  17499. (message "No images to display inline"))))
  17500. (defun org-redisplay-inline-images ()
  17501. "Refresh the display of inline images."
  17502. (interactive)
  17503. (if (not org-inline-image-overlays)
  17504. (org-toggle-inline-images)
  17505. (org-toggle-inline-images)
  17506. (org-toggle-inline-images)))
  17507. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17508. "Display inline images.
  17509. An inline image is a link which follows either of these
  17510. conventions:
  17511. 1. Its path is a file with an extension matching return value
  17512. from `image-file-name-regexp' and it has no contents.
  17513. 2. Its description consists in a single link of the previous
  17514. type.
  17515. When optional argument INCLUDE-LINKED is non-nil, also links with
  17516. a text description part will be inlined. This can be nice for
  17517. a quick look at those images, but it does not reflect what
  17518. exported files will look like.
  17519. When optional argument REFRESH is non-nil, refresh existing
  17520. images between BEG and END. This will create new image displays
  17521. only if necessary. BEG and END default to the buffer
  17522. boundaries."
  17523. (interactive "P")
  17524. (when (display-graphic-p)
  17525. (unless refresh
  17526. (org-remove-inline-images)
  17527. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17528. (org-with-wide-buffer
  17529. (goto-char (or beg (point-min)))
  17530. (let ((case-fold-search t)
  17531. (file-extension-re (org-image-file-name-regexp)))
  17532. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17533. (let ((link (save-match-data (org-element-context))))
  17534. ;; Check if we're at an inline image.
  17535. (when (and (equal (org-element-property :type link) "file")
  17536. (or include-linked
  17537. (not (org-element-property :contents-begin link)))
  17538. (let ((parent (org-element-property :parent link)))
  17539. (or (not (eq (org-element-type parent) 'link))
  17540. (not (cdr (org-element-contents parent)))))
  17541. (org-string-match-p file-extension-re
  17542. (org-element-property :path link)))
  17543. (let ((file (expand-file-name (org-element-property :path link))))
  17544. (when (file-exists-p file)
  17545. (let ((width
  17546. ;; Apply `org-image-actual-width' specifications.
  17547. (cond
  17548. ((not (image-type-available-p 'imagemagick)) nil)
  17549. ((eq org-image-actual-width t) nil)
  17550. ((listp org-image-actual-width)
  17551. (or
  17552. ;; First try to find a width among
  17553. ;; attributes associated to the paragraph
  17554. ;; containing link.
  17555. (let ((paragraph
  17556. (let ((e link))
  17557. (while (and (setq e (org-element-property
  17558. :parent e))
  17559. (not (eq (org-element-type e)
  17560. 'paragraph))))
  17561. e)))
  17562. (when paragraph
  17563. (save-excursion
  17564. (goto-char (org-element-property :begin paragraph))
  17565. (when (save-match-data
  17566. (re-search-forward
  17567. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  17568. (org-element-property
  17569. :post-affiliated paragraph)
  17570. t))
  17571. (string-to-number (match-string 1))))))
  17572. ;; Otherwise, fall-back to provided number.
  17573. (car org-image-actual-width)))
  17574. ((numberp org-image-actual-width)
  17575. org-image-actual-width)))
  17576. (old (get-char-property-and-overlay
  17577. (org-element-property :begin link)
  17578. 'org-image-overlay)))
  17579. (if (and (car-safe old) refresh)
  17580. (image-refresh (overlay-get (cdr old) 'display))
  17581. (let ((image (save-match-data
  17582. (create-image file
  17583. (and width 'imagemagick)
  17584. nil
  17585. :width width))))
  17586. (when image
  17587. (let* ((link
  17588. ;; If inline image is the description
  17589. ;; of another link, be sure to
  17590. ;; consider the latter as the one to
  17591. ;; apply the overlay on.
  17592. (let ((parent
  17593. (org-element-property :parent link)))
  17594. (if (eq (org-element-type parent) 'link)
  17595. parent
  17596. link)))
  17597. (ov (make-overlay
  17598. (org-element-property :begin link)
  17599. (progn
  17600. (goto-char
  17601. (org-element-property :end link))
  17602. (skip-chars-backward " \t")
  17603. (point)))))
  17604. (overlay-put ov 'display image)
  17605. (overlay-put ov 'face 'default)
  17606. (overlay-put ov 'org-image-overlay t)
  17607. (overlay-put
  17608. ov 'modification-hooks
  17609. (list 'org-display-inline-remove-overlay))
  17610. (push ov org-inline-image-overlays)))))))))))))))
  17611. (define-obsolete-function-alias
  17612. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17613. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17614. "Remove inline-display overlay if a corresponding region is modified."
  17615. (let ((inhibit-modification-hooks t))
  17616. (when (and ov after)
  17617. (delete ov org-inline-image-overlays)
  17618. (delete-overlay ov))))
  17619. (defun org-remove-inline-images ()
  17620. "Remove inline display of images."
  17621. (interactive)
  17622. (mapc 'delete-overlay org-inline-image-overlays)
  17623. (setq org-inline-image-overlays nil))
  17624. ;;;; Key bindings
  17625. ;; Outline functions from `outline-mode-prefix-map'
  17626. ;; that can be remapped in Org:
  17627. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17628. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17629. (define-key org-mode-map [remap outline-forward-same-level]
  17630. 'org-forward-heading-same-level)
  17631. (define-key org-mode-map [remap outline-backward-same-level]
  17632. 'org-backward-heading-same-level)
  17633. (define-key org-mode-map [remap show-branches]
  17634. 'org-kill-note-or-show-branches)
  17635. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17636. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17637. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17638. ;; Outline functions from `outline-mode-prefix-map' that can not
  17639. ;; be remapped in Org:
  17640. ;;
  17641. ;; - the column "key binding" shows whether the Outline function is still
  17642. ;; available in Org mode on the same key that it has been bound to in
  17643. ;; Outline mode:
  17644. ;; - "overridden": key used for a different functionality in Org mode
  17645. ;; - else: key still bound to the same Outline function in Org mode
  17646. ;;
  17647. ;; | Outline function | key binding | Org replacement |
  17648. ;; |------------------------------------+-------------+-----------------------|
  17649. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17650. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17651. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17652. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17653. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17654. ;; | `show-entry' | overridden | no replacement |
  17655. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17656. ;; | `show-branches' | `C-c C-k' | still same function |
  17657. ;; | `show-subtree' | overridden | visibility cycling |
  17658. ;; | `show-all' | overridden | no replacement |
  17659. ;; | `hide-subtree' | overridden | visibility cycling |
  17660. ;; | `hide-body' | overridden | no replacement |
  17661. ;; | `hide-entry' | overridden | visibility cycling |
  17662. ;; | `hide-leaves' | overridden | no replacement |
  17663. ;; | `hide-sublevels' | overridden | no replacement |
  17664. ;; | `hide-other' | overridden | no replacement |
  17665. ;; Make `C-c C-x' a prefix key
  17666. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17667. ;; TAB key with modifiers
  17668. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17669. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17670. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17671. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17672. ;; The following line is necessary under Suse GNU/Linux
  17673. (unless (featurep 'xemacs)
  17674. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17675. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17676. (define-key org-mode-map [backtab] 'org-shifttab)
  17677. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17678. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17679. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17680. ;; Cursor keys with modifiers
  17681. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17682. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17683. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17684. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17685. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  17686. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  17687. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17688. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17689. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17690. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17691. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17692. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17693. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17694. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17695. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17696. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17697. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17698. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17699. ;; Babel keys
  17700. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17701. (mapc (lambda (pair)
  17702. (define-key org-babel-map (car pair) (cdr pair)))
  17703. org-babel-key-bindings)
  17704. ;;; Extra keys for tty access.
  17705. ;; We only set them when really needed because otherwise the
  17706. ;; menus don't show the simple keys
  17707. (when (or org-use-extra-keys
  17708. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17709. (not window-system))
  17710. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17711. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17712. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17713. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17714. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17715. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17716. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17717. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17718. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17719. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17720. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17721. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17722. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17723. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17724. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17725. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17726. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17727. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17728. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17729. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17730. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17731. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17732. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17733. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17734. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17735. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17736. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17737. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17738. ;; All the other keys
  17739. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17740. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17741. (if (boundp 'narrow-map)
  17742. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17743. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17744. (if (boundp 'narrow-map)
  17745. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17746. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17747. (if (boundp 'narrow-map)
  17748. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17749. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17750. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17751. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17752. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17753. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17754. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17755. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17756. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17757. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17758. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17759. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17760. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17761. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17762. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17763. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17764. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17765. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17766. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17767. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17768. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17769. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17770. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17771. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17772. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17773. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17774. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17775. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17776. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17777. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17778. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17779. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17780. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17781. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17782. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17783. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17784. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17785. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17786. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  17787. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17788. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17789. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17790. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17791. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17792. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17793. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17794. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17795. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17796. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17797. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17798. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17799. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17800. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17801. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17802. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17803. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17804. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17805. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17806. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17807. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17808. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17809. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17810. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17811. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  17812. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  17813. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  17814. (org-defkey org-mode-map "\C-m" 'org-return)
  17815. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17816. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17817. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17818. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17819. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17820. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17821. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17822. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17823. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17824. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17825. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17826. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17827. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17828. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17829. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  17830. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17831. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17832. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17833. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17834. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17835. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17836. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17837. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17838. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17839. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17840. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17841. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17842. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17843. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17844. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17845. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17846. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17847. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17848. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17849. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17850. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17851. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17852. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17853. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17854. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17855. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17856. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17857. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17858. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17859. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17860. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17861. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17862. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17863. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17864. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17865. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17866. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17867. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17868. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17869. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17870. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17871. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17872. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17873. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17874. (when (featurep 'xemacs)
  17875. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17876. (defconst org-speed-commands-default
  17877. '(
  17878. ("Outline Navigation")
  17879. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17880. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17881. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17882. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17883. ("F" . org-next-block)
  17884. ("B" . org-previous-block)
  17885. ("u" . (org-speed-move-safe 'outline-up-heading))
  17886. ("j" . org-goto)
  17887. ("g" . (org-refile t))
  17888. ("Outline Visibility")
  17889. ("c" . org-cycle)
  17890. ("C" . org-shifttab)
  17891. (" " . org-display-outline-path)
  17892. ("s" . org-narrow-to-subtree)
  17893. ("=" . org-columns)
  17894. ("Outline Structure Editing")
  17895. ("U" . org-shiftmetaup)
  17896. ("D" . org-shiftmetadown)
  17897. ("r" . org-metaright)
  17898. ("l" . org-metaleft)
  17899. ("R" . org-shiftmetaright)
  17900. ("L" . org-shiftmetaleft)
  17901. ("i" . (progn (forward-char 1) (call-interactively
  17902. 'org-insert-heading-respect-content)))
  17903. ("^" . org-sort)
  17904. ("w" . org-refile)
  17905. ("a" . org-archive-subtree-default-with-confirmation)
  17906. ("@" . org-mark-subtree)
  17907. ("#" . org-toggle-comment)
  17908. ("Clock Commands")
  17909. ("I" . org-clock-in)
  17910. ("O" . org-clock-out)
  17911. ("Meta Data Editing")
  17912. ("t" . org-todo)
  17913. ("," . (org-priority))
  17914. ("0" . (org-priority ?\ ))
  17915. ("1" . (org-priority ?A))
  17916. ("2" . (org-priority ?B))
  17917. ("3" . (org-priority ?C))
  17918. (":" . org-set-tags-command)
  17919. ("e" . org-set-effort)
  17920. ("E" . org-inc-effort)
  17921. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17922. (org-entry-put (point) "APPT_WARNTIME" m)))
  17923. ("Agenda Views etc")
  17924. ("v" . org-agenda)
  17925. ("/" . org-sparse-tree)
  17926. ("Misc")
  17927. ("o" . org-open-at-point)
  17928. ("?" . org-speed-command-help)
  17929. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17930. (">" . (org-agenda-remove-restriction-lock))
  17931. )
  17932. "The default speed commands.")
  17933. (defun org-print-speed-command (e)
  17934. (if (> (length (car e)) 1)
  17935. (progn
  17936. (princ "\n")
  17937. (princ (car e))
  17938. (princ "\n")
  17939. (princ (make-string (length (car e)) ?-))
  17940. (princ "\n"))
  17941. (princ (car e))
  17942. (princ " ")
  17943. (if (symbolp (cdr e))
  17944. (princ (symbol-name (cdr e)))
  17945. (prin1 (cdr e)))
  17946. (princ "\n")))
  17947. (defun org-speed-command-help ()
  17948. "Show the available speed commands."
  17949. (interactive)
  17950. (if (not org-use-speed-commands)
  17951. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  17952. (with-output-to-temp-buffer "*Help*"
  17953. (princ "User-defined Speed commands\n===========================\n")
  17954. (mapc 'org-print-speed-command org-speed-commands-user)
  17955. (princ "\n")
  17956. (princ "Built-in Speed commands\n=======================\n")
  17957. (mapc 'org-print-speed-command org-speed-commands-default))
  17958. (with-current-buffer "*Help*"
  17959. (setq truncate-lines t))))
  17960. (defun org-speed-move-safe (cmd)
  17961. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17962. If not, return to the original position and throw an error."
  17963. (interactive)
  17964. (let ((pos (point)))
  17965. (call-interactively cmd)
  17966. (unless (and (bolp) (org-at-heading-p))
  17967. (goto-char pos)
  17968. (error "Boundary reached while executing %s" cmd))))
  17969. (defvar org-self-insert-command-undo-counter 0)
  17970. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17971. (defvar org-speed-command nil)
  17972. (define-obsolete-function-alias
  17973. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17974. (defun org-speed-command-activate (keys)
  17975. "Hook for activating single-letter speed commands.
  17976. `org-speed-commands-default' specifies a minimal command set.
  17977. Use `org-speed-commands-user' for further customization."
  17978. (when (or (and (bolp) (looking-at org-outline-regexp))
  17979. (and (functionp org-use-speed-commands)
  17980. (funcall org-use-speed-commands)))
  17981. (cdr (assoc keys (append org-speed-commands-user
  17982. org-speed-commands-default)))))
  17983. (define-obsolete-function-alias
  17984. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17985. (defun org-babel-speed-command-activate (keys)
  17986. "Hook for activating single-letter code block commands."
  17987. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17988. (cdr (assoc keys org-babel-key-bindings))))
  17989. (defcustom org-speed-command-hook
  17990. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17991. "Hook for activating speed commands at strategic locations.
  17992. Hook functions are called in sequence until a valid handler is
  17993. found.
  17994. Each hook takes a single argument, a user-pressed command key
  17995. which is also a `self-insert-command' from the global map.
  17996. Within the hook, examine the cursor position and the command key
  17997. and return nil or a valid handler as appropriate. Handler could
  17998. be one of an interactive command, a function, or a form.
  17999. Set `org-use-speed-commands' to non-nil value to enable this
  18000. hook. The default setting is `org-speed-command-activate'."
  18001. :group 'org-structure
  18002. :version "24.1"
  18003. :type 'hook)
  18004. (defun org-self-insert-command (N)
  18005. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18006. If the cursor is in a table looking at whitespace, the whitespace is
  18007. overwritten, and the table is not marked as requiring realignment."
  18008. (interactive "p")
  18009. (org-check-before-invisible-edit 'insert)
  18010. (cond
  18011. ((and org-use-speed-commands
  18012. (setq org-speed-command
  18013. (run-hook-with-args-until-success
  18014. 'org-speed-command-hook (this-command-keys))))
  18015. (cond
  18016. ((commandp org-speed-command)
  18017. (setq this-command org-speed-command)
  18018. (call-interactively org-speed-command))
  18019. ((functionp org-speed-command)
  18020. (funcall org-speed-command))
  18021. ((and org-speed-command (listp org-speed-command))
  18022. (eval org-speed-command))
  18023. (t (let (org-use-speed-commands)
  18024. (call-interactively 'org-self-insert-command)))))
  18025. ((and
  18026. (org-table-p)
  18027. (progn
  18028. ;; check if we blank the field, and if that triggers align
  18029. (and (featurep 'org-table) org-table-auto-blank-field
  18030. (member last-command
  18031. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  18032. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  18033. ;; got extra space, this field does not determine column width
  18034. (let (org-table-may-need-update) (org-table-blank-field))
  18035. ;; no extra space, this field may determine column width
  18036. (org-table-blank-field)))
  18037. t)
  18038. (eq N 1)
  18039. (looking-at "[^|\n]* |"))
  18040. (let (org-table-may-need-update)
  18041. (goto-char (1- (match-end 0)))
  18042. (backward-delete-char 1)
  18043. (goto-char (match-beginning 0))
  18044. (self-insert-command N)))
  18045. (t
  18046. (setq org-table-may-need-update t)
  18047. (self-insert-command N)
  18048. (org-fix-tags-on-the-fly)
  18049. (if org-self-insert-cluster-for-undo
  18050. (if (not (eq last-command 'org-self-insert-command))
  18051. (setq org-self-insert-command-undo-counter 1)
  18052. (if (>= org-self-insert-command-undo-counter 20)
  18053. (setq org-self-insert-command-undo-counter 1)
  18054. (and (> org-self-insert-command-undo-counter 0)
  18055. buffer-undo-list (listp buffer-undo-list)
  18056. (not (cadr buffer-undo-list)) ; remove nil entry
  18057. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18058. (setq org-self-insert-command-undo-counter
  18059. (1+ org-self-insert-command-undo-counter))))))))
  18060. (defun org-check-before-invisible-edit (kind)
  18061. "Check is editing if kind KIND would be dangerous with invisible text around.
  18062. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18063. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18064. (if (and org-catch-invisible-edits
  18065. (or (not (boundp 'visible-mode)) (not visible-mode))
  18066. (or (get-char-property (point) 'invisible)
  18067. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18068. ;; OK, we need to take a closer look
  18069. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18070. (invisible-before-point (if (bobp) nil (get-char-property
  18071. (1- (point)) 'invisible)))
  18072. (border-and-ok-direction
  18073. (or
  18074. ;; Check if we are acting predictably before invisible text
  18075. (and invisible-at-point (not invisible-before-point)
  18076. (memq kind '(insert delete-backward)))
  18077. ;; Check if we are acting predictably after invisible text
  18078. ;; This works not well, and I have turned it off. It seems
  18079. ;; better to always show and stop after invisible text.
  18080. ;; (and (not invisible-at-point) invisible-before-point
  18081. ;; (memq kind '(insert delete)))
  18082. )))
  18083. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18084. (memq invisible-before-point '(outline org-hide-block t)))
  18085. (if (eq org-catch-invisible-edits 'error)
  18086. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18087. (if (and org-custom-properties-overlays
  18088. (y-or-n-p "Display invisible properties in this buffer? "))
  18089. (org-toggle-custom-properties-visibility)
  18090. ;; Make the area visible
  18091. (save-excursion
  18092. (if invisible-before-point
  18093. (goto-char (previous-single-char-property-change
  18094. (point) 'invisible)))
  18095. (show-subtree))
  18096. (cond
  18097. ((eq org-catch-invisible-edits 'show)
  18098. ;; That's it, we do the edit after showing
  18099. (message
  18100. "Unfolding invisible region around point before editing")
  18101. (sit-for 1))
  18102. ((and (eq org-catch-invisible-edits 'smart)
  18103. border-and-ok-direction)
  18104. (message "Unfolding invisible region around point before editing"))
  18105. (t
  18106. ;; Don't do the edit, make the user repeat it in full visibility
  18107. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18108. (defun org-fix-tags-on-the-fly ()
  18109. (when (and (equal (char-after (point-at-bol)) ?*)
  18110. (org-at-heading-p))
  18111. (org-align-tags-here org-tags-column)))
  18112. (defun org-delete-backward-char (N)
  18113. "Like `delete-backward-char', insert whitespace at field end in tables.
  18114. When deleting backwards, in tables this function will insert whitespace in
  18115. front of the next \"|\" separator, to keep the table aligned. The table will
  18116. still be marked for re-alignment if the field did fill the entire column,
  18117. because, in this case the deletion might narrow the column."
  18118. (interactive "p")
  18119. (save-match-data
  18120. (org-check-before-invisible-edit 'delete-backward)
  18121. (if (and (org-table-p)
  18122. (eq N 1)
  18123. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18124. (looking-at ".*?|"))
  18125. (let ((pos (point))
  18126. (noalign (looking-at "[^|\n\r]* |"))
  18127. (c org-table-may-need-update))
  18128. (backward-delete-char N)
  18129. (if (not overwrite-mode)
  18130. (progn
  18131. (skip-chars-forward "^|")
  18132. (insert " ")
  18133. (goto-char (1- pos))))
  18134. ;; noalign: if there were two spaces at the end, this field
  18135. ;; does not determine the width of the column.
  18136. (if noalign (setq org-table-may-need-update c)))
  18137. (backward-delete-char N)
  18138. (org-fix-tags-on-the-fly))))
  18139. (defun org-delete-char (N)
  18140. "Like `delete-char', but insert whitespace at field end in tables.
  18141. When deleting characters, in tables this function will insert whitespace in
  18142. front of the next \"|\" separator, to keep the table aligned. The table will
  18143. still be marked for re-alignment if the field did fill the entire column,
  18144. because, in this case the deletion might narrow the column."
  18145. (interactive "p")
  18146. (save-match-data
  18147. (org-check-before-invisible-edit 'delete)
  18148. (if (and (org-table-p)
  18149. (not (bolp))
  18150. (not (= (char-after) ?|))
  18151. (eq N 1))
  18152. (if (looking-at ".*?|")
  18153. (let ((pos (point))
  18154. (noalign (looking-at "[^|\n\r]* |"))
  18155. (c org-table-may-need-update))
  18156. (replace-match
  18157. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18158. (goto-char pos)
  18159. ;; noalign: if there were two spaces at the end, this field
  18160. ;; does not determine the width of the column.
  18161. (if noalign (setq org-table-may-need-update c)))
  18162. (delete-char N))
  18163. (delete-char N)
  18164. (org-fix-tags-on-the-fly))))
  18165. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18166. (put 'org-self-insert-command 'delete-selection
  18167. (lambda ()
  18168. (not (run-hook-with-args-until-success
  18169. 'self-insert-uses-region-functions))))
  18170. (put 'orgtbl-self-insert-command 'delete-selection
  18171. (lambda ()
  18172. (not (run-hook-with-args-until-success
  18173. 'self-insert-uses-region-functions))))
  18174. (put 'org-delete-char 'delete-selection 'supersede)
  18175. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18176. (put 'org-yank 'delete-selection 'yank)
  18177. ;; Make `flyspell-mode' delay after some commands
  18178. (put 'org-self-insert-command 'flyspell-delayed t)
  18179. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18180. (put 'org-delete-char 'flyspell-delayed t)
  18181. (put 'org-delete-backward-char 'flyspell-delayed t)
  18182. ;; Make pabbrev-mode expand after org-mode commands
  18183. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18184. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18185. (defun org-remap (map &rest commands)
  18186. "In MAP, remap the functions given in COMMANDS.
  18187. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18188. (let (new old)
  18189. (while commands
  18190. (setq old (pop commands) new (pop commands))
  18191. (if (fboundp 'command-remapping)
  18192. (org-defkey map (vector 'remap old) new)
  18193. (substitute-key-definition old new map global-map)))))
  18194. (defun org-transpose-words ()
  18195. "Transpose words for Org.
  18196. This uses the `org-mode-transpose-word-syntax-table' syntax
  18197. table, which interprets characters in `org-emphasis-alist' as
  18198. word constituents."
  18199. (interactive)
  18200. (with-syntax-table org-mode-transpose-word-syntax-table
  18201. (call-interactively 'transpose-words)))
  18202. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18203. (when (eq org-enable-table-editor 'optimized)
  18204. ;; If the user wants maximum table support, we need to hijack
  18205. ;; some standard editing functions
  18206. (org-remap org-mode-map
  18207. 'self-insert-command 'org-self-insert-command
  18208. 'delete-char 'org-delete-char
  18209. 'delete-backward-char 'org-delete-backward-char)
  18210. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18211. (defvar org-ctrl-c-ctrl-c-hook nil
  18212. "Hook for functions attaching themselves to `C-c C-c'.
  18213. This can be used to add additional functionality to the C-c C-c
  18214. key which executes context-dependent commands. This hook is run
  18215. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18216. run after the last test.
  18217. Each function will be called with no arguments. The function
  18218. must check if the context is appropriate for it to act. If yes,
  18219. it should do its thing and then return a non-nil value. If the
  18220. context is wrong, just do nothing and return nil.")
  18221. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18222. "Hook for functions attaching themselves to `C-c C-c'.
  18223. This can be used to add additional functionality to the C-c C-c
  18224. key which executes context-dependent commands. This hook is run
  18225. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18226. before the first test.
  18227. Each function will be called with no arguments. The function
  18228. must check if the context is appropriate for it to act. If yes,
  18229. it should do its thing and then return a non-nil value. If the
  18230. context is wrong, just do nothing and return nil.")
  18231. (defvar org-tab-first-hook nil
  18232. "Hook for functions to attach themselves to TAB.
  18233. See `org-ctrl-c-ctrl-c-hook' for more information.
  18234. This hook runs as the first action when TAB is pressed, even before
  18235. `org-cycle' messes around with the `outline-regexp' to cater for
  18236. inline tasks and plain list item folding.
  18237. If any function in this hook returns t, any other actions that
  18238. would have been caused by TAB (such as table field motion or visibility
  18239. cycling) will not occur.")
  18240. (defvar org-tab-after-check-for-table-hook nil
  18241. "Hook for functions to attach themselves to TAB.
  18242. See `org-ctrl-c-ctrl-c-hook' for more information.
  18243. This hook runs after it has been established that the cursor is not in a
  18244. table, but before checking if the cursor is in a headline or if global cycling
  18245. should be done.
  18246. If any function in this hook returns t, not other actions like visibility
  18247. cycling will be done.")
  18248. (defvar org-tab-after-check-for-cycling-hook nil
  18249. "Hook for functions to attach themselves to TAB.
  18250. See `org-ctrl-c-ctrl-c-hook' for more information.
  18251. This hook runs after it has been established that not table field motion and
  18252. not visibility should be done because of current context. This is probably
  18253. the place where a package like yasnippets can hook in.")
  18254. (defvar org-tab-before-tab-emulation-hook nil
  18255. "Hook for functions to attach themselves to TAB.
  18256. See `org-ctrl-c-ctrl-c-hook' for more information.
  18257. This hook runs after every other options for TAB have been exhausted, but
  18258. before indentation and \t insertion takes place.")
  18259. (defvar org-metaleft-hook nil
  18260. "Hook for functions attaching themselves to `M-left'.
  18261. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18262. (defvar org-metaright-hook nil
  18263. "Hook for functions attaching themselves to `M-right'.
  18264. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18265. (defvar org-metaup-hook nil
  18266. "Hook for functions attaching themselves to `M-up'.
  18267. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18268. (defvar org-metadown-hook nil
  18269. "Hook for functions attaching themselves to `M-down'.
  18270. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18271. (defvar org-shiftmetaleft-hook nil
  18272. "Hook for functions attaching themselves to `M-S-left'.
  18273. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18274. (defvar org-shiftmetaright-hook nil
  18275. "Hook for functions attaching themselves to `M-S-right'.
  18276. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18277. (defvar org-shiftmetaup-hook nil
  18278. "Hook for functions attaching themselves to `M-S-up'.
  18279. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18280. (defvar org-shiftmetadown-hook nil
  18281. "Hook for functions attaching themselves to `M-S-down'.
  18282. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18283. (defvar org-metareturn-hook nil
  18284. "Hook for functions attaching themselves to `M-RET'.
  18285. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18286. (defvar org-shiftup-hook nil
  18287. "Hook for functions attaching themselves to `S-up'.
  18288. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18289. (defvar org-shiftup-final-hook nil
  18290. "Hook for functions attaching themselves to `S-up'.
  18291. This one runs after all other options except shift-select have been excluded.
  18292. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18293. (defvar org-shiftdown-hook nil
  18294. "Hook for functions attaching themselves to `S-down'.
  18295. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18296. (defvar org-shiftdown-final-hook nil
  18297. "Hook for functions attaching themselves to `S-down'.
  18298. This one runs after all other options except shift-select have been excluded.
  18299. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18300. (defvar org-shiftleft-hook nil
  18301. "Hook for functions attaching themselves to `S-left'.
  18302. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18303. (defvar org-shiftleft-final-hook nil
  18304. "Hook for functions attaching themselves to `S-left'.
  18305. This one runs after all other options except shift-select have been excluded.
  18306. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18307. (defvar org-shiftright-hook nil
  18308. "Hook for functions attaching themselves to `S-right'.
  18309. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18310. (defvar org-shiftright-final-hook nil
  18311. "Hook for functions attaching themselves to `S-right'.
  18312. This one runs after all other options except shift-select have been excluded.
  18313. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18314. (defun org-modifier-cursor-error ()
  18315. "Throw an error, a modified cursor command was applied in wrong context."
  18316. (user-error "This command is active in special context like tables, headlines or items"))
  18317. (defun org-shiftselect-error ()
  18318. "Throw an error because Shift-Cursor command was applied in wrong context."
  18319. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18320. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18321. (user-error "This command works only in special context like headlines or timestamps")))
  18322. (defun org-call-for-shift-select (cmd)
  18323. (let ((this-command-keys-shift-translated t))
  18324. (call-interactively cmd)))
  18325. (defun org-shifttab (&optional arg)
  18326. "Global visibility cycling or move to previous table field.
  18327. Call `org-table-previous-field' within a table.
  18328. When ARG is nil, cycle globally through visibility states.
  18329. When ARG is a numeric prefix, show contents of this level."
  18330. (interactive "P")
  18331. (cond
  18332. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18333. ((integerp arg)
  18334. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18335. (message "Content view to level: %d" arg)
  18336. (org-content (prefix-numeric-value arg2))
  18337. (org-cycle-show-empty-lines t)
  18338. (setq org-cycle-global-status 'overview)))
  18339. (t (call-interactively 'org-global-cycle))))
  18340. (defun org-shiftmetaleft ()
  18341. "Promote subtree or delete table column.
  18342. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18343. `org-table-delete-column', depending on context. See the
  18344. individual commands for more information."
  18345. (interactive)
  18346. (cond
  18347. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18348. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18349. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18350. ((if (not (org-region-active-p)) (org-at-item-p)
  18351. (save-excursion (goto-char (region-beginning))
  18352. (org-at-item-p)))
  18353. (call-interactively 'org-outdent-item-tree))
  18354. (t (org-modifier-cursor-error))))
  18355. (defun org-shiftmetaright ()
  18356. "Demote subtree or insert table column.
  18357. Calls `org-demote-subtree', `org-indent-item-tree', or
  18358. `org-table-insert-column', depending on context. See the
  18359. individual commands for more information."
  18360. (interactive)
  18361. (cond
  18362. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18363. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18364. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18365. ((if (not (org-region-active-p)) (org-at-item-p)
  18366. (save-excursion (goto-char (region-beginning))
  18367. (org-at-item-p)))
  18368. (call-interactively 'org-indent-item-tree))
  18369. (t (org-modifier-cursor-error))))
  18370. (defun org-shiftmetaup (&optional arg)
  18371. "Move subtree up or kill table row.
  18372. Calls `org-move-subtree-up' or `org-table-kill-row' or
  18373. `org-move-item-up' or `org-timestamp-up', depending on context.
  18374. See the individual commands for more information."
  18375. (interactive "P")
  18376. (cond
  18377. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18378. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18379. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18380. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18381. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18382. (call-interactively 'org-timestamp-up)))
  18383. (t (call-interactively 'org-drag-line-backward))))
  18384. (defun org-shiftmetadown (&optional arg)
  18385. "Move subtree down or insert table row.
  18386. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18387. `org-move-item-down' or `org-timestamp-up', depending on context.
  18388. See the individual commands for more information."
  18389. (interactive "P")
  18390. (cond
  18391. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18392. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18393. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18394. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18395. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18396. (call-interactively 'org-timestamp-down)))
  18397. (t (call-interactively 'org-drag-line-forward))))
  18398. (defsubst org-hidden-tree-error ()
  18399. (user-error
  18400. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18401. (defun org-metaleft (&optional arg)
  18402. "Promote heading or move table column to left.
  18403. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18404. With no specific context, calls the Emacs default `backward-word'.
  18405. See the individual commands for more information."
  18406. (interactive "P")
  18407. (cond
  18408. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18409. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18410. ((org-with-limited-levels
  18411. (or (org-at-heading-p)
  18412. (and (org-region-active-p)
  18413. (save-excursion
  18414. (goto-char (region-beginning))
  18415. (org-at-heading-p)))))
  18416. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18417. (call-interactively 'org-do-promote))
  18418. ;; At an inline task.
  18419. ((org-at-heading-p)
  18420. (call-interactively 'org-inlinetask-promote))
  18421. ((or (org-at-item-p)
  18422. (and (org-region-active-p)
  18423. (save-excursion
  18424. (goto-char (region-beginning))
  18425. (org-at-item-p))))
  18426. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18427. (call-interactively 'org-outdent-item))
  18428. (t (call-interactively 'backward-word))))
  18429. (defun org-metaright (&optional arg)
  18430. "Demote a subtree, a list item or move table column to right.
  18431. In front of a drawer or a block keyword, indent it correctly.
  18432. With no specific context, calls the Emacs default `forward-word'.
  18433. See the individual commands for more information."
  18434. (interactive "P")
  18435. (cond
  18436. ((run-hook-with-args-until-success 'org-metaright-hook))
  18437. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18438. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18439. ((org-at-block-p) (call-interactively 'org-indent-block))
  18440. ((org-with-limited-levels
  18441. (or (org-at-heading-p)
  18442. (and (org-region-active-p)
  18443. (save-excursion
  18444. (goto-char (region-beginning))
  18445. (org-at-heading-p)))))
  18446. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18447. (call-interactively 'org-do-demote))
  18448. ;; At an inline task.
  18449. ((org-at-heading-p)
  18450. (call-interactively 'org-inlinetask-demote))
  18451. ((or (org-at-item-p)
  18452. (and (org-region-active-p)
  18453. (save-excursion
  18454. (goto-char (region-beginning))
  18455. (org-at-item-p))))
  18456. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18457. (call-interactively 'org-indent-item))
  18458. (t (call-interactively 'forward-word))))
  18459. (defun org-check-for-hidden (what)
  18460. "Check if there are hidden headlines/items in the current visual line.
  18461. WHAT can be either `headlines' or `items'. If the current line is
  18462. an outline or item heading and it has a folded subtree below it,
  18463. this function returns t, nil otherwise."
  18464. (let ((re (cond
  18465. ((eq what 'headlines) org-outline-regexp-bol)
  18466. ((eq what 'items) (org-item-beginning-re))
  18467. (t (error "This should not happen"))))
  18468. beg end)
  18469. (save-excursion
  18470. (catch 'exit
  18471. (unless (org-region-active-p)
  18472. (setq beg (point-at-bol))
  18473. (beginning-of-line 2)
  18474. (while (and (not (eobp)) ;; this is like `next-line'
  18475. (get-char-property (1- (point)) 'invisible))
  18476. (beginning-of-line 2))
  18477. (setq end (point))
  18478. (goto-char beg)
  18479. (goto-char (point-at-eol))
  18480. (setq end (max end (point)))
  18481. (while (re-search-forward re end t)
  18482. (if (get-char-property (match-beginning 0) 'invisible)
  18483. (throw 'exit t))))
  18484. nil))))
  18485. (defun org-metaup (&optional arg)
  18486. "Move subtree up or move table row up.
  18487. Calls `org-move-subtree-up' or `org-table-move-row' or
  18488. `org-move-item-up', depending on context. See the individual commands
  18489. for more information."
  18490. (interactive "P")
  18491. (cond
  18492. ((run-hook-with-args-until-success 'org-metaup-hook))
  18493. ((org-region-active-p)
  18494. (let* ((a (min (region-beginning) (region-end)))
  18495. (b (1- (max (region-beginning) (region-end))))
  18496. (c (save-excursion (goto-char a)
  18497. (move-beginning-of-line 0)))
  18498. (d (save-excursion (goto-char a)
  18499. (move-end-of-line 0) (point))))
  18500. (transpose-regions a b c d)
  18501. (goto-char c)))
  18502. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18503. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18504. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18505. (t (org-drag-element-backward))))
  18506. (defun org-metadown (&optional arg)
  18507. "Move subtree down or move table row down.
  18508. Calls `org-move-subtree-down' or `org-table-move-row' or
  18509. `org-move-item-down', depending on context. See the individual
  18510. commands for more information."
  18511. (interactive "P")
  18512. (cond
  18513. ((run-hook-with-args-until-success 'org-metadown-hook))
  18514. ((org-region-active-p)
  18515. (let* ((a (min (region-beginning) (region-end)))
  18516. (b (max (region-beginning) (region-end)))
  18517. (c (save-excursion (goto-char b)
  18518. (move-beginning-of-line 1)))
  18519. (d (save-excursion (goto-char b)
  18520. (move-end-of-line 1) (1+ (point)))))
  18521. (transpose-regions a b c d)
  18522. (goto-char d)))
  18523. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18524. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18525. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18526. (t (org-drag-element-forward))))
  18527. (defun org-shiftup (&optional arg)
  18528. "Increase item in timestamp or increase priority of current headline.
  18529. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18530. depending on context. See the individual commands for more information."
  18531. (interactive "P")
  18532. (cond
  18533. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18534. ((and org-support-shift-select (org-region-active-p))
  18535. (org-call-for-shift-select 'previous-line))
  18536. ((org-at-timestamp-p t)
  18537. (call-interactively (if org-edit-timestamp-down-means-later
  18538. 'org-timestamp-down 'org-timestamp-up)))
  18539. ((and (not (eq org-support-shift-select 'always))
  18540. org-enable-priority-commands
  18541. (org-at-heading-p))
  18542. (call-interactively 'org-priority-up))
  18543. ((and (not org-support-shift-select) (org-at-item-p))
  18544. (call-interactively 'org-previous-item))
  18545. ((org-clocktable-try-shift 'up arg))
  18546. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18547. (org-support-shift-select
  18548. (org-call-for-shift-select 'previous-line))
  18549. (t (org-shiftselect-error))))
  18550. (defun org-shiftdown (&optional arg)
  18551. "Decrease item in timestamp or decrease priority of current headline.
  18552. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18553. depending on context. See the individual commands for more information."
  18554. (interactive "P")
  18555. (cond
  18556. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18557. ((and org-support-shift-select (org-region-active-p))
  18558. (org-call-for-shift-select 'next-line))
  18559. ((org-at-timestamp-p t)
  18560. (call-interactively (if org-edit-timestamp-down-means-later
  18561. 'org-timestamp-up 'org-timestamp-down)))
  18562. ((and (not (eq org-support-shift-select 'always))
  18563. org-enable-priority-commands
  18564. (org-at-heading-p))
  18565. (call-interactively 'org-priority-down))
  18566. ((and (not org-support-shift-select) (org-at-item-p))
  18567. (call-interactively 'org-next-item))
  18568. ((org-clocktable-try-shift 'down arg))
  18569. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18570. (org-support-shift-select
  18571. (org-call-for-shift-select 'next-line))
  18572. (t (org-shiftselect-error))))
  18573. (defun org-shiftright (&optional arg)
  18574. "Cycle the thing at point or in the current line, depending on context.
  18575. Depending on context, this does one of the following:
  18576. - switch a timestamp at point one day into the future
  18577. - on a headline, switch to the next TODO keyword.
  18578. - on an item, switch entire list to the next bullet type
  18579. - on a property line, switch to the next allowed value
  18580. - on a clocktable definition line, move time block into the future"
  18581. (interactive "P")
  18582. (cond
  18583. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18584. ((and org-support-shift-select (org-region-active-p))
  18585. (org-call-for-shift-select 'forward-char))
  18586. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18587. ((and (not (eq org-support-shift-select 'always))
  18588. (org-at-heading-p))
  18589. (let ((org-inhibit-logging
  18590. (not org-treat-S-cursor-todo-selection-as-state-change))
  18591. (org-inhibit-blocking
  18592. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18593. (org-call-with-arg 'org-todo 'right)))
  18594. ((or (and org-support-shift-select
  18595. (not (eq org-support-shift-select 'always))
  18596. (org-at-item-bullet-p))
  18597. (and (not org-support-shift-select) (org-at-item-p)))
  18598. (org-call-with-arg 'org-cycle-list-bullet nil))
  18599. ((and (not (eq org-support-shift-select 'always))
  18600. (org-at-property-p))
  18601. (call-interactively 'org-property-next-allowed-value))
  18602. ((org-clocktable-try-shift 'right arg))
  18603. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18604. (org-support-shift-select
  18605. (org-call-for-shift-select 'forward-char))
  18606. (t (org-shiftselect-error))))
  18607. (defun org-shiftleft (&optional arg)
  18608. "Cycle the thing at point or in the current line, depending on context.
  18609. Depending on context, this does one of the following:
  18610. - switch a timestamp at point one day into the past
  18611. - on a headline, switch to the previous TODO keyword.
  18612. - on an item, switch entire list to the previous bullet type
  18613. - on a property line, switch to the previous allowed value
  18614. - on a clocktable definition line, move time block into the past"
  18615. (interactive "P")
  18616. (cond
  18617. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18618. ((and org-support-shift-select (org-region-active-p))
  18619. (org-call-for-shift-select 'backward-char))
  18620. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18621. ((and (not (eq org-support-shift-select 'always))
  18622. (org-at-heading-p))
  18623. (let ((org-inhibit-logging
  18624. (not org-treat-S-cursor-todo-selection-as-state-change))
  18625. (org-inhibit-blocking
  18626. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18627. (org-call-with-arg 'org-todo 'left)))
  18628. ((or (and org-support-shift-select
  18629. (not (eq org-support-shift-select 'always))
  18630. (org-at-item-bullet-p))
  18631. (and (not org-support-shift-select) (org-at-item-p)))
  18632. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18633. ((and (not (eq org-support-shift-select 'always))
  18634. (org-at-property-p))
  18635. (call-interactively 'org-property-previous-allowed-value))
  18636. ((org-clocktable-try-shift 'left arg))
  18637. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18638. (org-support-shift-select
  18639. (org-call-for-shift-select 'backward-char))
  18640. (t (org-shiftselect-error))))
  18641. (defun org-shiftcontrolright ()
  18642. "Switch to next TODO set."
  18643. (interactive)
  18644. (cond
  18645. ((and org-support-shift-select (org-region-active-p))
  18646. (org-call-for-shift-select 'forward-word))
  18647. ((and (not (eq org-support-shift-select 'always))
  18648. (org-at-heading-p))
  18649. (org-call-with-arg 'org-todo 'nextset))
  18650. (org-support-shift-select
  18651. (org-call-for-shift-select 'forward-word))
  18652. (t (org-shiftselect-error))))
  18653. (defun org-shiftcontrolleft ()
  18654. "Switch to previous TODO set."
  18655. (interactive)
  18656. (cond
  18657. ((and org-support-shift-select (org-region-active-p))
  18658. (org-call-for-shift-select 'backward-word))
  18659. ((and (not (eq org-support-shift-select 'always))
  18660. (org-at-heading-p))
  18661. (org-call-with-arg 'org-todo 'previousset))
  18662. (org-support-shift-select
  18663. (org-call-for-shift-select 'backward-word))
  18664. (t (org-shiftselect-error))))
  18665. (defun org-shiftcontrolup (&optional n)
  18666. "Change timestamps synchronously up in CLOCK log lines.
  18667. Optional argument N tells to change by that many units."
  18668. (interactive "P")
  18669. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18670. (let (org-support-shift-select)
  18671. (org-clock-timestamps-up n))
  18672. (user-error "Not at a clock log")))
  18673. (defun org-shiftcontroldown (&optional n)
  18674. "Change timestamps synchronously down in CLOCK log lines.
  18675. Optional argument N tells to change by that many units."
  18676. (interactive "P")
  18677. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18678. (let (org-support-shift-select)
  18679. (org-clock-timestamps-down n))
  18680. (user-error "Not at a clock log")))
  18681. (defun org-increase-number-at-point (&optional inc)
  18682. "Increment the number at point.
  18683. With an optional prefix numeric argument INC, increment using
  18684. this numeric value."
  18685. (interactive "p")
  18686. (if (not (number-at-point))
  18687. (user-error "Not on a number")
  18688. (unless inc (setq inc 1))
  18689. (let ((pos (point))
  18690. (beg (skip-chars-backward "-+^/*0-9eE."))
  18691. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  18692. (setq nap (buffer-substring-no-properties
  18693. (+ pos beg) (+ pos beg end)))
  18694. (delete-region (+ pos beg) (+ pos beg end))
  18695. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  18696. (when (org-at-table-p)
  18697. (org-table-align)
  18698. (org-table-end-of-field 1))))
  18699. (defun org-decrease-number-at-point (&optional inc)
  18700. "Decrement the number at point.
  18701. With an optional prefix numeric argument INC, decrement using
  18702. this numeric value."
  18703. (interactive "p")
  18704. (org-increase-number-at-point (- inc)))
  18705. (defun org-ctrl-c-ret ()
  18706. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18707. (interactive)
  18708. (cond
  18709. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18710. (t (call-interactively 'org-insert-heading))))
  18711. (defun org-find-visible ()
  18712. (let ((s (point)))
  18713. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18714. (get-char-property s 'invisible)))
  18715. s))
  18716. (defun org-find-invisible ()
  18717. (let ((s (point)))
  18718. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18719. (not (get-char-property s 'invisible))))
  18720. s))
  18721. (defun org-copy-visible (beg end)
  18722. "Copy the visible parts of the region."
  18723. (interactive "r")
  18724. (let (snippets s)
  18725. (save-excursion
  18726. (save-restriction
  18727. (narrow-to-region beg end)
  18728. (setq s (goto-char (point-min)))
  18729. (while (not (= (point) (point-max)))
  18730. (goto-char (org-find-invisible))
  18731. (push (buffer-substring s (point)) snippets)
  18732. (setq s (goto-char (org-find-visible))))))
  18733. (kill-new (apply 'concat (nreverse snippets)))))
  18734. (defun org-copy-special ()
  18735. "Copy region in table or copy current subtree.
  18736. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18737. See the individual commands for more information."
  18738. (interactive)
  18739. (call-interactively
  18740. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18741. (defun org-cut-special ()
  18742. "Cut region in table or cut current subtree.
  18743. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18744. See the individual commands for more information."
  18745. (interactive)
  18746. (call-interactively
  18747. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18748. (defun org-paste-special (arg)
  18749. "Paste rectangular region into table, or past subtree relative to level.
  18750. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18751. See the individual commands for more information."
  18752. (interactive "P")
  18753. (if (org-at-table-p)
  18754. (org-table-paste-rectangle)
  18755. (org-paste-subtree arg)))
  18756. (defsubst org-in-fixed-width-region-p ()
  18757. "Is point in a fixed-width region?"
  18758. (save-match-data
  18759. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18760. (defun org-edit-special (&optional arg)
  18761. "Call a special editor for the element at point.
  18762. When at a table, call the formula editor with `org-table-edit-formulas'.
  18763. When in a source code block, call `org-edit-src-code'.
  18764. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18765. When at an #+INCLUDE keyword, visit the included file.
  18766. On a link, call `ffap' to visit the link at point.
  18767. Otherwise, return a user error."
  18768. (interactive "P")
  18769. (let ((element (org-element-at-point)))
  18770. (assert (not buffer-read-only) nil
  18771. "Buffer is read-only: %s" (buffer-name))
  18772. (case (org-element-type element)
  18773. (src-block
  18774. (if (not arg) (org-edit-src-code)
  18775. (let* ((info (org-babel-get-src-block-info))
  18776. (lang (nth 0 info))
  18777. (params (nth 2 info))
  18778. (session (cdr (assq :session params))))
  18779. (if (not session) (org-edit-src-code)
  18780. ;; At a src-block with a session and function called with
  18781. ;; an ARG: switch to the buffer related to the inferior
  18782. ;; process.
  18783. (switch-to-buffer
  18784. (funcall (intern (concat "org-babel-prep-session:" lang))
  18785. session params))))))
  18786. (keyword
  18787. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18788. (find-file-other-window
  18789. (org-remove-double-quotes
  18790. (car (org-split-string (org-element-property :value element)))))
  18791. (user-error "No special environment to edit here")))
  18792. (table
  18793. (if (eq (org-element-property :type element) 'table.el)
  18794. (org-edit-src-code)
  18795. (call-interactively 'org-table-edit-formulas)))
  18796. ;; Only Org tables contain `table-row' type elements.
  18797. (table-row (call-interactively 'org-table-edit-formulas))
  18798. ((example-block export-block) (org-edit-src-code))
  18799. (fixed-width (org-edit-fixed-width-region))
  18800. (otherwise
  18801. ;; No notable element at point. Though, we may be at a link,
  18802. ;; which is an object. Thus, scan deeper.
  18803. (if (eq (org-element-type (org-element-context element)) 'link)
  18804. (call-interactively 'ffap)
  18805. (user-error "No special environment to edit here"))))))
  18806. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18807. (defun org-ctrl-c-ctrl-c (&optional arg)
  18808. "Set tags in headline, or update according to changed information at point.
  18809. This command does many different things, depending on context:
  18810. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18811. this is what we do.
  18812. - If the cursor is on a statistics cookie, update it.
  18813. - If the cursor is in a headline, prompt for tags and insert them
  18814. into the current line, aligned to `org-tags-column'. When called
  18815. with prefix arg, realign all tags in the current buffer.
  18816. - If the cursor is in one of the special #+KEYWORD lines, this
  18817. triggers scanning the buffer for these lines and updating the
  18818. information.
  18819. - If the cursor is inside a table, realign the table. This command
  18820. works even if the automatic table editor has been turned off.
  18821. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18822. the entire table.
  18823. - If the cursor is at a footnote reference or definition, jump to
  18824. the corresponding definition or references, respectively.
  18825. - If the cursor is a the beginning of a dynamic block, update it.
  18826. - If the current buffer is a capture buffer, close note and file it.
  18827. - If the cursor is on a <<<target>>>, update radio targets and
  18828. corresponding links in this buffer.
  18829. - If the cursor is on a numbered item in a plain list, renumber the
  18830. ordered list.
  18831. - If the cursor is on a checkbox, toggle it.
  18832. - If the cursor is on a code block, evaluate it. The variable
  18833. `org-confirm-babel-evaluate' can be used to control prompting
  18834. before code block evaluation, by default every code block
  18835. evaluation requires confirmation. Code block evaluation can be
  18836. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18837. (interactive "P")
  18838. (cond
  18839. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18840. org-occur-highlights
  18841. org-latex-fragment-image-overlays)
  18842. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18843. (org-remove-occur-highlights)
  18844. (org-remove-latex-fragment-image-overlays)
  18845. (message "Temporary highlights/overlays removed from current buffer"))
  18846. ((and (local-variable-p 'org-finish-function (current-buffer))
  18847. (fboundp org-finish-function))
  18848. (funcall org-finish-function))
  18849. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18850. (t
  18851. (let* ((context (org-element-context)) (type (org-element-type context)))
  18852. ;; Test if point is within a blank line.
  18853. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18854. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18855. (user-error "C-c C-c can do nothing useful at this location"))
  18856. (case type
  18857. ;; When at a link, act according to the parent instead.
  18858. (link (setq context (org-element-property :parent context))
  18859. (setq type (org-element-type context)))
  18860. ;; Unsupported object types: refer to the first supported
  18861. ;; element or object containing it.
  18862. ((bold code entity export-snippet inline-babel-call inline-src-block
  18863. italic latex-fragment line-break macro strike-through subscript
  18864. superscript underline verbatim)
  18865. (while (and (setq context (org-element-property :parent context))
  18866. (not (memq (setq type (org-element-type context))
  18867. '(radio-target paragraph verse-block
  18868. table-cell)))))))
  18869. ;; For convenience: at the first line of a paragraph on the
  18870. ;; same line as an item, apply function on that item instead.
  18871. (when (eq type 'paragraph)
  18872. (let ((parent (org-element-property :parent context)))
  18873. (when (and (eq (org-element-type parent) 'item)
  18874. (= (point-at-bol) (org-element-property :begin parent)))
  18875. (setq context parent type 'item))))
  18876. ;; Act according to type of element or object at point.
  18877. (case type
  18878. (clock (org-clock-update-time-maybe))
  18879. (dynamic-block
  18880. (save-excursion
  18881. (goto-char (org-element-property :post-affiliated context))
  18882. (org-update-dblock)))
  18883. (footnote-definition
  18884. (goto-char (org-element-property :post-affiliated context))
  18885. (call-interactively 'org-footnote-action))
  18886. (footnote-reference (call-interactively 'org-footnote-action))
  18887. ((headline inlinetask)
  18888. (save-excursion (goto-char (org-element-property :begin context))
  18889. (call-interactively 'org-set-tags)))
  18890. (item
  18891. ;; At an item: a double C-u set checkbox to "[-]"
  18892. ;; unconditionally, whereas a single one will toggle its
  18893. ;; presence. Without an universal argument, if the item
  18894. ;; has a checkbox, toggle it. Otherwise repair the list.
  18895. (let* ((box (org-element-property :checkbox context))
  18896. (struct (org-element-property :structure context))
  18897. (old-struct (copy-tree struct))
  18898. (parents (org-list-parents-alist struct))
  18899. (prevs (org-list-prevs-alist struct))
  18900. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18901. (org-list-set-checkbox
  18902. (org-element-property :begin context) struct
  18903. (cond ((equal arg '(16)) "[-]")
  18904. ((and (not box) (equal arg '(4))) "[ ]")
  18905. ((or (not box) (equal arg '(4))) nil)
  18906. ((eq box 'on) "[ ]")
  18907. (t "[X]")))
  18908. ;; Mimic `org-list-write-struct' but with grabbing
  18909. ;; a return value from `org-list-struct-fix-box'.
  18910. (org-list-struct-fix-ind struct parents 2)
  18911. (org-list-struct-fix-item-end struct)
  18912. (org-list-struct-fix-bul struct prevs)
  18913. (org-list-struct-fix-ind struct parents)
  18914. (let ((block-item
  18915. (org-list-struct-fix-box struct parents prevs orderedp)))
  18916. (if (and box (equal struct old-struct))
  18917. (if (equal arg '(16))
  18918. (message "Checkboxes already reset")
  18919. (user-error "Cannot toggle this checkbox: %s"
  18920. (if (eq box 'on)
  18921. "all subitems checked"
  18922. "unchecked subitems")))
  18923. (org-list-struct-apply-struct struct old-struct)
  18924. (org-update-checkbox-count-maybe))
  18925. (when block-item
  18926. (message "Checkboxes were removed due to empty box at line %d"
  18927. (org-current-line block-item))))))
  18928. (keyword
  18929. (let ((org-inhibit-startup-visibility-stuff t)
  18930. (org-startup-align-all-tables nil))
  18931. (when (boundp 'org-table-coordinate-overlays)
  18932. (mapc 'delete-overlay org-table-coordinate-overlays)
  18933. (setq org-table-coordinate-overlays nil))
  18934. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18935. (message "Local setup has been refreshed"))
  18936. (plain-list
  18937. ;; At a plain list, with a double C-u argument, set
  18938. ;; checkboxes of each item to "[-]", whereas a single one
  18939. ;; will toggle their presence according to the state of the
  18940. ;; first item in the list. Without an argument, repair the
  18941. ;; list.
  18942. (let* ((begin (org-element-property :contents-begin context))
  18943. (beginm (move-marker (make-marker) begin))
  18944. (struct (org-element-property :structure context))
  18945. (old-struct (copy-tree struct))
  18946. (first-box (save-excursion
  18947. (goto-char begin)
  18948. (looking-at org-list-full-item-re)
  18949. (match-string-no-properties 3)))
  18950. (new-box (cond ((equal arg '(16)) "[-]")
  18951. ((equal arg '(4)) (unless first-box "[ ]"))
  18952. ((equal first-box "[X]") "[ ]")
  18953. (t "[X]"))))
  18954. (cond
  18955. (arg
  18956. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18957. (org-list-get-all-items
  18958. begin struct (org-list-prevs-alist struct))))
  18959. ((and first-box (eq (point) begin))
  18960. ;; For convenience, when point is at bol on the first
  18961. ;; item of the list and no argument is provided, simply
  18962. ;; toggle checkbox of that item, if any.
  18963. (org-list-set-checkbox begin struct new-box)))
  18964. (org-list-write-struct
  18965. struct (org-list-parents-alist struct) old-struct)
  18966. (org-update-checkbox-count-maybe)
  18967. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18968. ((property-drawer node-property)
  18969. (call-interactively 'org-property-action))
  18970. ((radio-target target)
  18971. (call-interactively 'org-update-radio-target-regexp))
  18972. (statistics-cookie
  18973. (call-interactively 'org-update-statistics-cookies))
  18974. ((table table-cell table-row)
  18975. ;; At a table, recalculate every field and align it. Also
  18976. ;; send the table if necessary. If the table has
  18977. ;; a `table.el' type, just give up. At a table row or
  18978. ;; cell, maybe recalculate line but always align table.
  18979. (if (eq (org-element-property :type context) 'table.el)
  18980. (message "Use C-c ' to edit table.el tables")
  18981. (let ((org-enable-table-editor t))
  18982. (if (or (eq type 'table)
  18983. ;; Check if point is at a TBLFM line.
  18984. (and (eq type 'table-row)
  18985. (= (point) (org-element-property :end context))))
  18986. (save-excursion
  18987. (if (org-at-TBLFM-p)
  18988. (progn (require 'org-table)
  18989. (org-table-calc-current-TBLFM))
  18990. (goto-char (org-element-property :contents-begin context))
  18991. (org-call-with-arg 'org-table-recalculate (or arg t))
  18992. (orgtbl-send-table 'maybe)))
  18993. (org-table-maybe-eval-formula)
  18994. (cond (arg (call-interactively 'org-table-recalculate))
  18995. ((org-table-maybe-recalculate-line))
  18996. (t (org-table-align)))))))
  18997. (timestamp (org-timestamp-change 0 'day))
  18998. (otherwise
  18999. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19000. (user-error
  19001. "C-c C-c can do nothing useful at this location")))))))))
  19002. (defun org-mode-restart ()
  19003. (interactive)
  19004. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19005. (funcall major-mode)
  19006. (hack-local-variables)
  19007. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19008. (org-indent-mode -1)))
  19009. (message "%s restarted" major-mode))
  19010. (defun org-kill-note-or-show-branches ()
  19011. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19012. (interactive)
  19013. (if (not org-finish-function)
  19014. (progn
  19015. (hide-subtree)
  19016. (call-interactively 'show-branches))
  19017. (let ((org-note-abort t))
  19018. (funcall org-finish-function))))
  19019. (defun org-open-line (n)
  19020. "Insert a new row in tables, call `open-line' elsewhere.
  19021. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19022. (interactive "*p")
  19023. (cond
  19024. ((not org-special-ctrl-o)
  19025. (open-line n))
  19026. ((org-at-table-p)
  19027. (org-table-insert-row))
  19028. (t
  19029. (open-line n))))
  19030. (defun org-return (&optional indent)
  19031. "Goto next table row or insert a newline.
  19032. Calls `org-table-next-row' or `newline', depending on context.
  19033. See the individual commands for more information."
  19034. (interactive)
  19035. (let (org-ts-what)
  19036. (cond
  19037. ((or (bobp) (org-in-src-block-p))
  19038. (if indent (newline-and-indent) (newline)))
  19039. ((org-at-table-p)
  19040. (org-table-justify-field-maybe)
  19041. (call-interactively 'org-table-next-row))
  19042. ;; when `newline-and-indent' is called within a list, make sure
  19043. ;; text moved stays inside the item.
  19044. ((and (org-in-item-p) indent)
  19045. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  19046. (progn
  19047. (save-match-data (newline))
  19048. (org-indent-line-to (length (match-string 0))))
  19049. (let ((ind (org-get-indentation)))
  19050. (newline)
  19051. (if (org-looking-back org-list-end-re)
  19052. (org-indent-line)
  19053. (org-indent-line-to ind)))))
  19054. ((and org-return-follows-link
  19055. (org-at-timestamp-p t)
  19056. (not (eq org-ts-what 'after)))
  19057. (org-follow-timestamp-link))
  19058. ((and org-return-follows-link
  19059. (let ((tprop (get-text-property (point) 'face)))
  19060. (or (eq tprop 'org-link)
  19061. (and (listp tprop) (memq 'org-link tprop)))))
  19062. (call-interactively 'org-open-at-point))
  19063. ((and (org-at-heading-p)
  19064. (looking-at
  19065. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  19066. (org-show-entry)
  19067. (end-of-line 1)
  19068. (newline))
  19069. (t (if indent (newline-and-indent) (newline))))))
  19070. (defun org-return-indent ()
  19071. "Goto next table row or insert a newline and indent.
  19072. Calls `org-table-next-row' or `newline-and-indent', depending on
  19073. context. See the individual commands for more information."
  19074. (interactive)
  19075. (org-return t))
  19076. (defun org-ctrl-c-star ()
  19077. "Compute table, or change heading status of lines.
  19078. Calls `org-table-recalculate' or `org-toggle-heading',
  19079. depending on context."
  19080. (interactive)
  19081. (cond
  19082. ((org-at-table-p)
  19083. (call-interactively 'org-table-recalculate))
  19084. (t
  19085. ;; Convert all lines in region to list items
  19086. (call-interactively 'org-toggle-heading))))
  19087. (defun org-ctrl-c-minus ()
  19088. "Insert separator line in table or modify bullet status of line.
  19089. Also turns a plain line or a region of lines into list items.
  19090. Calls `org-table-insert-hline', `org-toggle-item', or
  19091. `org-cycle-list-bullet', depending on context."
  19092. (interactive)
  19093. (cond
  19094. ((org-at-table-p)
  19095. (call-interactively 'org-table-insert-hline))
  19096. ((org-region-active-p)
  19097. (call-interactively 'org-toggle-item))
  19098. ((org-in-item-p)
  19099. (call-interactively 'org-cycle-list-bullet))
  19100. (t
  19101. (call-interactively 'org-toggle-item))))
  19102. (defun org-toggle-item (arg)
  19103. "Convert headings or normal lines to items, items to normal lines.
  19104. If there is no active region, only the current line is considered.
  19105. If the first non blank line in the region is a headline, convert
  19106. all headlines to items, shifting text accordingly.
  19107. If it is an item, convert all items to normal lines.
  19108. If it is normal text, change region into a list of items.
  19109. With a prefix argument ARG, change the region in a single item."
  19110. (interactive "P")
  19111. (let ((shift-text
  19112. (function
  19113. ;; Shift text in current section to IND, from point to END.
  19114. ;; The function leaves point to END line.
  19115. (lambda (ind end)
  19116. (let ((min-i 1000) (end (copy-marker end)))
  19117. ;; First determine the minimum indentation (MIN-I) of
  19118. ;; the text.
  19119. (save-excursion
  19120. (catch 'exit
  19121. (while (< (point) end)
  19122. (let ((i (org-get-indentation)))
  19123. (cond
  19124. ;; Skip blank lines and inline tasks.
  19125. ((looking-at "^[ \t]*$"))
  19126. ((looking-at org-outline-regexp-bol))
  19127. ;; We can't find less than 0 indentation.
  19128. ((zerop i) (throw 'exit (setq min-i 0)))
  19129. ((< i min-i) (setq min-i i))))
  19130. (forward-line))))
  19131. ;; Then indent each line so that a line indented to
  19132. ;; MIN-I becomes indented to IND. Ignore blank lines
  19133. ;; and inline tasks in the process.
  19134. (let ((delta (- ind min-i)))
  19135. (while (< (point) end)
  19136. (unless (or (looking-at "^[ \t]*$")
  19137. (looking-at org-outline-regexp-bol))
  19138. (org-indent-line-to (+ (org-get-indentation) delta)))
  19139. (forward-line)))))))
  19140. (skip-blanks
  19141. (function
  19142. ;; Return beginning of first non-blank line, starting from
  19143. ;; line at POS.
  19144. (lambda (pos)
  19145. (save-excursion
  19146. (goto-char pos)
  19147. (skip-chars-forward " \r\t\n")
  19148. (point-at-bol)))))
  19149. beg end)
  19150. ;; Determine boundaries of changes.
  19151. (if (org-region-active-p)
  19152. (setq beg (funcall skip-blanks (region-beginning))
  19153. end (copy-marker (region-end)))
  19154. (setq beg (funcall skip-blanks (point-at-bol))
  19155. end (copy-marker (point-at-eol))))
  19156. ;; Depending on the starting line, choose an action on the text
  19157. ;; between BEG and END.
  19158. (org-with-limited-levels
  19159. (save-excursion
  19160. (goto-char beg)
  19161. (cond
  19162. ;; Case 1. Start at an item: de-itemize. Note that it only
  19163. ;; happens when a region is active: `org-ctrl-c-minus'
  19164. ;; would call `org-cycle-list-bullet' otherwise.
  19165. ((org-at-item-p)
  19166. (while (< (point) end)
  19167. (when (org-at-item-p)
  19168. (skip-chars-forward " \t")
  19169. (delete-region (point) (match-end 0)))
  19170. (forward-line)))
  19171. ;; Case 2. Start at an heading: convert to items.
  19172. ((org-at-heading-p)
  19173. (let* ((bul (org-list-bullet-string "-"))
  19174. (bul-len (length bul))
  19175. (done (org-entry-is-done-p))
  19176. (todo (org-entry-is-todo-p))
  19177. ;; Indentation of the first heading. It should be
  19178. ;; relative to the indentation of its parent, if any.
  19179. (start-ind (save-excursion
  19180. (cond
  19181. ((not org-adapt-indentation) 0)
  19182. ((not (outline-previous-heading)) 0)
  19183. (t (length (match-string 0))))))
  19184. ;; Level of first heading. Further headings will be
  19185. ;; compared to it to determine hierarchy in the list.
  19186. (ref-level (org-reduced-level (org-outline-level))))
  19187. (when (or done todo) (org-todo ""))
  19188. (while (< (point) end)
  19189. (let* ((level (org-reduced-level (org-outline-level)))
  19190. (delta (max 0 (- level ref-level))))
  19191. ;; If current headline is less indented than the first
  19192. ;; one, set it as reference, in order to preserve
  19193. ;; subtrees.
  19194. (when (< level ref-level) (setq ref-level level))
  19195. (replace-match bul t t)
  19196. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19197. (when (or done todo)
  19198. (let* ((struct (org-list-struct))
  19199. (old (copy-tree struct)))
  19200. (org-list-set-checkbox (line-beginning-position)
  19201. struct
  19202. (if done "[X]" "[ ]"))
  19203. (org-list-write-struct struct
  19204. (org-list-parents-alist struct)
  19205. old)))
  19206. ;; Ensure all text down to END (or SECTION-END) belongs
  19207. ;; to the newly created item.
  19208. (let ((section-end (save-excursion
  19209. (or (outline-next-heading) (point)))))
  19210. (forward-line)
  19211. (funcall shift-text
  19212. (+ start-ind (* (1+ delta) bul-len))
  19213. (min end section-end)))))))
  19214. ;; Case 3. Normal line with ARG: make the first line of region
  19215. ;; an item, and shift indentation of others lines to
  19216. ;; set them as item's body.
  19217. (arg (let* ((bul (org-list-bullet-string "-"))
  19218. (bul-len (length bul))
  19219. (ref-ind (org-get-indentation)))
  19220. (skip-chars-forward " \t")
  19221. (insert bul)
  19222. (forward-line)
  19223. (while (< (point) end)
  19224. ;; Ensure that lines less indented than first one
  19225. ;; still get included in item body.
  19226. (funcall shift-text
  19227. (+ ref-ind bul-len)
  19228. (min end (save-excursion (or (outline-next-heading)
  19229. (point)))))
  19230. (forward-line))))
  19231. ;; Case 4. Normal line without ARG: turn each non-item line
  19232. ;; into an item.
  19233. (t
  19234. (while (< (point) end)
  19235. (unless (or (org-at-heading-p) (org-at-item-p))
  19236. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19237. (replace-match
  19238. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19239. (forward-line))))))))
  19240. (defun org-toggle-heading (&optional nstars)
  19241. "Convert headings to normal text, or items or text to headings.
  19242. If there is no active region, only convert the current line.
  19243. With a \\[universal-argument] prefix, convert the whole list at
  19244. point into heading.
  19245. In a region:
  19246. - If the first non blank line is a headline, remove the stars
  19247. from all headlines in the region.
  19248. - If it is a normal line, turn each and every normal line (i.e.,
  19249. not an heading or an item) in the region into headings. If you
  19250. want to convert only the first line of this region, use one
  19251. universal prefix argument.
  19252. - If it is a plain list item, turn all plain list items into headings.
  19253. When converting a line into a heading, the number of stars is chosen
  19254. such that the lines become children of the current entry. However,
  19255. when a numeric prefix argument is given, its value determines the
  19256. number of stars to add."
  19257. (interactive "P")
  19258. (let ((skip-blanks
  19259. (function
  19260. ;; Return beginning of first non-blank line, starting from
  19261. ;; line at POS.
  19262. (lambda (pos)
  19263. (save-excursion
  19264. (goto-char pos)
  19265. (while (org-at-comment-p) (forward-line))
  19266. (skip-chars-forward " \r\t\n")
  19267. (point-at-bol)))))
  19268. beg end toggled)
  19269. ;; Determine boundaries of changes. If a universal prefix has
  19270. ;; been given, put the list in a region. If region ends at a bol,
  19271. ;; do not consider the last line to be in the region.
  19272. (when (and current-prefix-arg (org-at-item-p))
  19273. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19274. (org-mark-element))
  19275. (if (org-region-active-p)
  19276. (setq beg (funcall skip-blanks (region-beginning))
  19277. end (copy-marker (save-excursion
  19278. (goto-char (region-end))
  19279. (if (bolp) (point) (point-at-eol)))))
  19280. (setq beg (funcall skip-blanks (point-at-bol))
  19281. end (copy-marker (point-at-eol))))
  19282. ;; Ensure inline tasks don't count as headings.
  19283. (org-with-limited-levels
  19284. (save-excursion
  19285. (goto-char beg)
  19286. (cond
  19287. ;; Case 1. Started at an heading: de-star headings.
  19288. ((org-at-heading-p)
  19289. (while (< (point) end)
  19290. (when (org-at-heading-p t)
  19291. (looking-at org-outline-regexp) (replace-match "")
  19292. (setq toggled t))
  19293. (forward-line)))
  19294. ;; Case 2. Started at an item: change items into headlines.
  19295. ;; One star will be added by `org-list-to-subtree'.
  19296. ((org-at-item-p)
  19297. (let* ((stars (make-string
  19298. ;; subtract the star that will be added again by
  19299. ;; `org-list-to-subtree'
  19300. (if (numberp nstars) (1- nstars)
  19301. (or (org-current-level) 0))
  19302. ?*))
  19303. (add-stars
  19304. (cond (nstars "") ; stars from prefix only
  19305. ((equal stars "") "") ; before first heading
  19306. (org-odd-levels-only "*") ; inside heading, odd
  19307. (t "")))) ; inside heading, oddeven
  19308. (while (< (point) end)
  19309. (when (org-at-item-p)
  19310. ;; Pay attention to cases when region ends before list.
  19311. (let* ((struct (org-list-struct))
  19312. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19313. (save-restriction
  19314. (narrow-to-region (point) list-end)
  19315. (insert
  19316. (org-list-to-subtree
  19317. (org-list-parse-list t)
  19318. '(:istart (concat stars add-stars (funcall get-stars depth))
  19319. :icount (concat stars add-stars (funcall get-stars depth)))))))
  19320. (setq toggled t))
  19321. (forward-line))))
  19322. ;; Case 3. Started at normal text: make every line an heading,
  19323. ;; skipping headlines and items.
  19324. (t (let* ((stars
  19325. (make-string
  19326. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19327. (add-stars
  19328. (cond (nstars "") ; stars from prefix only
  19329. ((equal stars "") "*") ; before first heading
  19330. (org-odd-levels-only "**") ; inside heading, odd
  19331. (t "*"))) ; inside heading, oddeven
  19332. (rpl (concat stars add-stars " "))
  19333. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19334. (while (< (point) (if (equal nstars '(4)) lend end))
  19335. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19336. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19337. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19338. (forward-line)))))))
  19339. (unless toggled (message "Cannot toggle heading from here"))))
  19340. (defun org-meta-return (&optional arg)
  19341. "Insert a new heading or wrap a region in a table.
  19342. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19343. on context. See the individual commands for more information."
  19344. (interactive "P")
  19345. (org-check-before-invisible-edit 'insert)
  19346. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19347. (let* ((element (org-element-at-point))
  19348. (type (org-element-type element)))
  19349. (when (eq type 'table-row)
  19350. (setq element (org-element-property :parent element))
  19351. (setq type 'table))
  19352. (if (and (eq type 'table)
  19353. (eq (org-element-property :type element) 'org)
  19354. (>= (point) (org-element-property :contents-begin element))
  19355. (< (point) (org-element-property :contents-end element)))
  19356. (call-interactively 'org-table-wrap-region)
  19357. (call-interactively 'org-insert-heading)))))
  19358. ;;; Menu entries
  19359. (defsubst org-in-subtree-not-table-p ()
  19360. "Are we in a subtree and not in a table?"
  19361. (and (not (org-before-first-heading-p))
  19362. (not (org-at-table-p))))
  19363. ;; Define the Org-mode menus
  19364. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19365. '("Tbl"
  19366. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19367. ["Next Field" org-cycle (org-at-table-p)]
  19368. ["Previous Field" org-shifttab (org-at-table-p)]
  19369. ["Next Row" org-return (org-at-table-p)]
  19370. "--"
  19371. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19372. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19373. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19374. "--"
  19375. ("Column"
  19376. ["Move Column Left" org-metaleft (org-at-table-p)]
  19377. ["Move Column Right" org-metaright (org-at-table-p)]
  19378. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19379. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19380. ("Row"
  19381. ["Move Row Up" org-metaup (org-at-table-p)]
  19382. ["Move Row Down" org-metadown (org-at-table-p)]
  19383. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19384. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19385. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19386. "--"
  19387. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19388. ("Rectangle"
  19389. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19390. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19391. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19392. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19393. "--"
  19394. ("Calculate"
  19395. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19396. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19397. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19398. "--"
  19399. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19400. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19401. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19402. "--"
  19403. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19404. "--"
  19405. ["Sum Column/Rectangle" org-table-sum
  19406. (or (org-at-table-p) (org-region-active-p))]
  19407. ["Which Column?" org-table-current-column (org-at-table-p)])
  19408. ["Debug Formulas"
  19409. org-table-toggle-formula-debugger
  19410. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19411. ["Show Col/Row Numbers"
  19412. org-table-toggle-coordinate-overlays
  19413. :style toggle
  19414. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19415. "--"
  19416. ["Create" org-table-create (and (not (org-at-table-p))
  19417. org-enable-table-editor)]
  19418. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19419. ["Import from File" org-table-import (not (org-at-table-p))]
  19420. ["Export to File" org-table-export (org-at-table-p)]
  19421. "--"
  19422. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  19423. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19424. '("Org"
  19425. ("Show/Hide"
  19426. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19427. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19428. ["Sparse Tree..." org-sparse-tree t]
  19429. ["Reveal Context" org-reveal t]
  19430. ["Show All" show-all t]
  19431. "--"
  19432. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19433. "--"
  19434. ["New Heading" org-insert-heading t]
  19435. ("Navigate Headings"
  19436. ["Up" outline-up-heading t]
  19437. ["Next" outline-next-visible-heading t]
  19438. ["Previous" outline-previous-visible-heading t]
  19439. ["Next Same Level" outline-forward-same-level t]
  19440. ["Previous Same Level" outline-backward-same-level t]
  19441. "--"
  19442. ["Jump" org-goto t])
  19443. ("Edit Structure"
  19444. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19445. "--"
  19446. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19447. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19448. "--"
  19449. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19450. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19451. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19452. "--"
  19453. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19454. "--"
  19455. ["Copy visible text" org-copy-visible t]
  19456. "--"
  19457. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19458. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19459. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19460. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19461. "--"
  19462. ["Sort Region/Children" org-sort t]
  19463. "--"
  19464. ["Convert to odd levels" org-convert-to-odd-levels t]
  19465. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19466. ("Editing"
  19467. ["Emphasis..." org-emphasize t]
  19468. ["Edit Source Example" org-edit-special t]
  19469. "--"
  19470. ["Footnote new/jump" org-footnote-action t]
  19471. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19472. ("Archive"
  19473. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19474. "--"
  19475. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19476. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19477. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19478. )
  19479. "--"
  19480. ("Hyperlinks"
  19481. ["Store Link (Global)" org-store-link t]
  19482. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19483. ["Insert Link" org-insert-link t]
  19484. ["Follow Link" org-open-at-point t]
  19485. "--"
  19486. ["Next link" org-next-link t]
  19487. ["Previous link" org-previous-link t]
  19488. "--"
  19489. ["Descriptive Links"
  19490. org-toggle-link-display
  19491. :style radio
  19492. :selected org-descriptive-links
  19493. ]
  19494. ["Literal Links"
  19495. org-toggle-link-display
  19496. :style radio
  19497. :selected (not org-descriptive-links)])
  19498. "--"
  19499. ("TODO Lists"
  19500. ["TODO/DONE/-" org-todo t]
  19501. ("Select keyword"
  19502. ["Next keyword" org-shiftright (org-at-heading-p)]
  19503. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19504. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19505. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19506. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19507. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19508. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19509. "--"
  19510. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19511. :selected org-enforce-todo-dependencies :style toggle :active t]
  19512. "Settings for tree at point"
  19513. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19514. :selected (org-entry-get nil "ORDERED")
  19515. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19516. ["Do Children parallel" org-toggle-ordered-property :style radio
  19517. :selected (not (org-entry-get nil "ORDERED"))
  19518. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19519. "--"
  19520. ["Set Priority" org-priority t]
  19521. ["Priority Up" org-shiftup t]
  19522. ["Priority Down" org-shiftdown t]
  19523. "--"
  19524. ["Get news from all feeds" org-feed-update-all t]
  19525. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19526. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19527. ("TAGS and Properties"
  19528. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19529. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19530. "--"
  19531. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19532. ["Column view of properties" org-columns t]
  19533. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19534. ("Dates and Scheduling"
  19535. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19536. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19537. ("Change Date"
  19538. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19539. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19540. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19541. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19542. ["Compute Time Range" org-evaluate-time-range t]
  19543. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19544. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19545. "--"
  19546. ["Custom time format" org-toggle-time-stamp-overlays
  19547. :style radio :selected org-display-custom-times]
  19548. "--"
  19549. ["Goto Calendar" org-goto-calendar t]
  19550. ["Date from Calendar" org-date-from-calendar t]
  19551. "--"
  19552. ["Start/Restart Timer" org-timer-start t]
  19553. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19554. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19555. ["Insert Timer String" org-timer t]
  19556. ["Insert Timer Item" org-timer-item t])
  19557. ("Logging work"
  19558. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19559. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19560. ["Clock out" org-clock-out t]
  19561. ["Clock cancel" org-clock-cancel t]
  19562. "--"
  19563. ["Mark as default task" org-clock-mark-default-task t]
  19564. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19565. ["Goto running clock" org-clock-goto t]
  19566. "--"
  19567. ["Display times" org-clock-display t]
  19568. ["Create clock table" org-clock-report t]
  19569. "--"
  19570. ["Record DONE time"
  19571. (progn (setq org-log-done (not org-log-done))
  19572. (message "Switching to %s will %s record a timestamp"
  19573. (car org-done-keywords)
  19574. (if org-log-done "automatically" "not")))
  19575. :style toggle :selected org-log-done])
  19576. "--"
  19577. ["Agenda Command..." org-agenda t]
  19578. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19579. ("File List for Agenda")
  19580. ("Special views current file"
  19581. ["TODO Tree" org-show-todo-tree t]
  19582. ["Check Deadlines" org-check-deadlines t]
  19583. ["Timeline" org-timeline t]
  19584. ["Tags/Property tree" org-match-sparse-tree t])
  19585. "--"
  19586. ["Export/Publish..." org-export-dispatch t]
  19587. ("LaTeX"
  19588. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19589. :selected org-cdlatex-mode]
  19590. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19591. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19592. ["Modify math symbol" org-cdlatex-math-modify
  19593. (org-inside-LaTeX-fragment-p)]
  19594. ["Insert citation" org-reftex-citation t]
  19595. "--"
  19596. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19597. "--"
  19598. ("MobileOrg"
  19599. ["Push Files and Views" org-mobile-push t]
  19600. ["Get Captured and Flagged" org-mobile-pull t]
  19601. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19602. "--"
  19603. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19604. "--"
  19605. ("Documentation"
  19606. ["Show Version" org-version t]
  19607. ["Info Documentation" org-info t])
  19608. ("Customize"
  19609. ["Browse Org Group" org-customize t]
  19610. "--"
  19611. ["Expand This Menu" org-create-customize-menu
  19612. (fboundp 'customize-menu-create)])
  19613. ["Send bug report" org-submit-bug-report t]
  19614. "--"
  19615. ("Refresh/Reload"
  19616. ["Refresh setup current buffer" org-mode-restart t]
  19617. ["Reload Org (after update)" org-reload t]
  19618. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19619. ))
  19620. (defun org-info (&optional node)
  19621. "Read documentation for Org-mode in the info system.
  19622. With optional NODE, go directly to that node."
  19623. (interactive)
  19624. (info (format "(org)%s" (or node ""))))
  19625. ;;;###autoload
  19626. (defun org-submit-bug-report ()
  19627. "Submit a bug report on Org-mode via mail.
  19628. Don't hesitate to report any problems or inaccurate documentation.
  19629. If you don't have setup sending mail from (X)Emacs, please copy the
  19630. output buffer into your mail program, as it gives us important
  19631. information about your Org-mode version and configuration."
  19632. (interactive)
  19633. (require 'reporter)
  19634. (org-load-modules-maybe)
  19635. (org-require-autoloaded-modules)
  19636. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19637. (reporter-submit-bug-report
  19638. "emacs-orgmode@gnu.org"
  19639. (org-version nil 'full)
  19640. (let (list)
  19641. (save-window-excursion
  19642. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19643. (delete-other-windows)
  19644. (erase-buffer)
  19645. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19646. We would like to add your full Org-mode and Outline configuration to the
  19647. bug report. This greatly simplifies the work of the maintainer and
  19648. other experts on the mailing list.
  19649. HOWEVER, some variables you have customized may contain private
  19650. information. The names of customers, colleagues, or friends, might
  19651. appear in the form of file names, tags, todo states, or search strings.
  19652. If you answer yes to the prompt, you might want to check and remove
  19653. such private information before sending the email.")
  19654. (add-text-properties (point-min) (point-max) '(face org-warning))
  19655. (when (yes-or-no-p "Include your Org-mode configuration ")
  19656. (mapatoms
  19657. (lambda (v)
  19658. (and (boundp v)
  19659. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19660. (or (and (symbol-value v)
  19661. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19662. (and
  19663. (get v 'custom-type) (get v 'standard-value)
  19664. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19665. (push v list)))))
  19666. (kill-buffer (get-buffer "*Warn about privacy*"))
  19667. list))
  19668. nil nil
  19669. "Remember to cover the basics, that is, what you expected to happen and
  19670. what in fact did happen. You don't know how to make a good report? See
  19671. http://orgmode.org/manual/Feedback.html#Feedback
  19672. Your bug report will be posted to the Org-mode mailing list.
  19673. ------------------------------------------------------------------------")
  19674. (save-excursion
  19675. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19676. (replace-match "\\1Bug: \\3 [\\2]")))))
  19677. (defun org-install-agenda-files-menu ()
  19678. (let ((bl (buffer-list)))
  19679. (save-excursion
  19680. (while bl
  19681. (set-buffer (pop bl))
  19682. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19683. (when (derived-mode-p 'org-mode)
  19684. (easy-menu-change
  19685. '("Org") "File List for Agenda"
  19686. (append
  19687. (list
  19688. ["Edit File List" (org-edit-agenda-file-list) t]
  19689. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19690. ["Remove Current File from List" org-remove-file t]
  19691. ["Cycle through agenda files" org-cycle-agenda-files t]
  19692. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19693. "--")
  19694. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19695. ;;;; Documentation
  19696. (defun org-require-autoloaded-modules ()
  19697. (interactive)
  19698. (mapc 'require
  19699. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19700. org-table org-timer)))
  19701. ;;;###autoload
  19702. (defun org-reload (&optional uncompiled)
  19703. "Reload all org lisp files.
  19704. With prefix arg UNCOMPILED, load the uncompiled versions."
  19705. (interactive "P")
  19706. (require 'loadhist)
  19707. (let* ((org-dir (org-find-library-dir "org"))
  19708. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19709. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19710. (remove-re (mapconcat 'identity
  19711. (mapcar (lambda (f) (concat "^" f "$"))
  19712. (list (if (featurep 'xemacs)
  19713. "org-colview"
  19714. "org-colview-xemacs")
  19715. "org" "org-loaddefs" "org-version"))
  19716. "\\|"))
  19717. (feats (delete-dups
  19718. (mapcar 'file-name-sans-extension
  19719. (mapcar 'file-name-nondirectory
  19720. (delq nil
  19721. (mapcar 'feature-file
  19722. features))))))
  19723. (lfeat (append
  19724. (sort
  19725. (setq feats
  19726. (delq nil (mapcar
  19727. (lambda (f)
  19728. (if (and (string-match feature-re f)
  19729. (not (string-match remove-re f)))
  19730. f nil))
  19731. feats)))
  19732. 'string-lessp)
  19733. (list "org-version" "org")))
  19734. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19735. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19736. load-uncore load-misses)
  19737. (setq load-misses
  19738. (delq 't
  19739. (mapcar (lambda (f)
  19740. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19741. (and (string= org-dir contrib-dir)
  19742. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19743. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19744. (add-to-list 'load-uncore f 'append)
  19745. 't)
  19746. f))
  19747. lfeat)))
  19748. (if load-uncore
  19749. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19750. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19751. (if load-misses
  19752. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19753. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19754. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19755. ;;;###autoload
  19756. (defun org-customize ()
  19757. "Call the customize function with org as argument."
  19758. (interactive)
  19759. (org-load-modules-maybe)
  19760. (org-require-autoloaded-modules)
  19761. (customize-browse 'org))
  19762. (defun org-create-customize-menu ()
  19763. "Create a full customization menu for Org-mode, insert it into the menu."
  19764. (interactive)
  19765. (org-load-modules-maybe)
  19766. (org-require-autoloaded-modules)
  19767. (if (fboundp 'customize-menu-create)
  19768. (progn
  19769. (easy-menu-change
  19770. '("Org") "Customize"
  19771. `(["Browse Org group" org-customize t]
  19772. "--"
  19773. ,(customize-menu-create 'org)
  19774. ["Set" Custom-set t]
  19775. ["Save" Custom-save t]
  19776. ["Reset to Current" Custom-reset-current t]
  19777. ["Reset to Saved" Custom-reset-saved t]
  19778. ["Reset to Standard Settings" Custom-reset-standard t]))
  19779. (message "\"Org\"-menu now contains full customization menu"))
  19780. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19781. ;;;; Miscellaneous stuff
  19782. ;;; Generally useful functions
  19783. (defsubst org-get-at-bol (property)
  19784. "Get text property PROPERTY at the beginning of line."
  19785. (get-text-property (point-at-bol) property))
  19786. (defsubst org-get-at-eol (property n)
  19787. "Get text property PROPERTY at the end of line less N characters."
  19788. (get-text-property (- (point-at-eol) n) property))
  19789. (defun org-find-text-property-in-string (prop s)
  19790. "Return the first non-nil value of property PROP in string S."
  19791. (or (get-text-property 0 prop s)
  19792. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19793. prop s)))
  19794. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19795. "Display the given MESSAGE as a warning."
  19796. (if (fboundp 'display-warning)
  19797. (display-warning 'org message
  19798. (if (featurep 'xemacs) 'warning :warning))
  19799. (let ((buf (get-buffer-create "*Org warnings*")))
  19800. (with-current-buffer buf
  19801. (goto-char (point-max))
  19802. (insert "Warning (Org): " message)
  19803. (unless (bolp)
  19804. (newline)))
  19805. (display-buffer buf)
  19806. (sit-for 0))))
  19807. (defun org-eval (form)
  19808. "Eval FORM and return result."
  19809. (condition-case error
  19810. (eval form)
  19811. (error (format "%%![Error: %s]" error))))
  19812. (defun org-in-clocktable-p ()
  19813. "Check if the cursor is in a clocktable."
  19814. (let ((pos (point)) start)
  19815. (save-excursion
  19816. (end-of-line 1)
  19817. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19818. (setq start (match-beginning 0))
  19819. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19820. (>= (match-end 0) pos)
  19821. start))))
  19822. (defun org-in-verbatim-emphasis ()
  19823. (save-match-data
  19824. (and (org-in-regexp org-emph-re 2)
  19825. (>= (point) (match-beginning 3))
  19826. (<= (point) (match-end 4))
  19827. (member (match-string 3) '("=" "~")))))
  19828. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19829. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19830. (if (and marker (marker-buffer marker)
  19831. (buffer-live-p (marker-buffer marker)))
  19832. (progn
  19833. (org-pop-to-buffer-same-window (marker-buffer marker))
  19834. (if (or (> marker (point-max)) (< marker (point-min)))
  19835. (widen))
  19836. (goto-char marker)
  19837. (org-show-context 'org-goto))
  19838. (if bookmark
  19839. (bookmark-jump bookmark)
  19840. (error "Cannot find location"))))
  19841. (defun org-quote-csv-field (s)
  19842. "Quote field for inclusion in CSV material."
  19843. (if (string-match "[\",]" s)
  19844. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19845. s))
  19846. (defun org-force-self-insert (N)
  19847. "Needed to enforce self-insert under remapping."
  19848. (interactive "p")
  19849. (self-insert-command N))
  19850. (defun org-string-width (s)
  19851. "Compute width of string, ignoring invisible characters.
  19852. This ignores character with invisibility property `org-link', and also
  19853. characters with property `org-cwidth', because these will become invisible
  19854. upon the next fontification round."
  19855. (let (b l)
  19856. (when (or (eq t buffer-invisibility-spec)
  19857. (assq 'org-link buffer-invisibility-spec))
  19858. (while (setq b (text-property-any 0 (length s)
  19859. 'invisible 'org-link s))
  19860. (setq s (concat (substring s 0 b)
  19861. (substring s (or (next-single-property-change
  19862. b 'invisible s) (length s)))))))
  19863. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19864. (setq s (concat (substring s 0 b)
  19865. (substring s (or (next-single-property-change
  19866. b 'org-cwidth s) (length s))))))
  19867. (setq l (string-width s) b -1)
  19868. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19869. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19870. l))
  19871. (defun org-shorten-string (s maxlength)
  19872. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19873. If the string is shorter or has length MAXLENGTH, just return the
  19874. original string. If it is longer, the functions finds a space in the
  19875. string, breaks this string off at that locations and adds three dots
  19876. as ellipsis. Including the ellipsis, the string will not be longer
  19877. than MAXLENGTH. If finding a good breaking point in the string does
  19878. not work, the string is just chopped off in the middle of a word
  19879. if necessary."
  19880. (if (<= (length s) maxlength)
  19881. s
  19882. (let* ((n (max (- maxlength 4) 1))
  19883. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19884. (if (string-match re s)
  19885. (concat (match-string 1 s) "...")
  19886. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19887. (defun org-get-indentation (&optional line)
  19888. "Get the indentation of the current line, interpreting tabs.
  19889. When LINE is given, assume it represents a line and compute its indentation."
  19890. (if line
  19891. (if (string-match "^ *" (org-remove-tabs line))
  19892. (match-end 0))
  19893. (save-excursion
  19894. (beginning-of-line 1)
  19895. (skip-chars-forward " \t")
  19896. (current-column))))
  19897. (defun org-get-string-indentation (s)
  19898. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19899. (let ((n -1) (i 0) (w tab-width) c)
  19900. (catch 'exit
  19901. (while (< (setq n (1+ n)) (length s))
  19902. (setq c (aref s n))
  19903. (cond ((= c ?\ ) (setq i (1+ i)))
  19904. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19905. (t (throw 'exit t)))))
  19906. i))
  19907. (defun org-remove-tabs (s &optional width)
  19908. "Replace tabulators in S with spaces.
  19909. Assumes that s is a single line, starting in column 0."
  19910. (setq width (or width tab-width))
  19911. (while (string-match "\t" s)
  19912. (setq s (replace-match
  19913. (make-string
  19914. (- (* width (/ (+ (match-beginning 0) width) width))
  19915. (match-beginning 0)) ?\ )
  19916. t t s)))
  19917. s)
  19918. (defun org-fix-indentation (line ind)
  19919. "Fix indentation in LINE.
  19920. IND is a cons cell with target and minimum indentation.
  19921. If the current indentation in LINE is smaller than the minimum,
  19922. leave it alone. If it is larger than ind, set it to the target."
  19923. (let* ((l (org-remove-tabs line))
  19924. (i (org-get-indentation l))
  19925. (i1 (car ind)) (i2 (cdr ind)))
  19926. (if (>= i i2) (setq l (substring line i2)))
  19927. (if (> i1 0)
  19928. (concat (make-string i1 ?\ ) l)
  19929. l)))
  19930. (defun org-remove-indentation (code &optional n)
  19931. "Remove the maximum common indentation from the lines in CODE.
  19932. N may optionally be the number of spaces to remove."
  19933. (with-temp-buffer
  19934. (insert code)
  19935. (org-do-remove-indentation n)
  19936. (buffer-string)))
  19937. (defun org-do-remove-indentation (&optional n)
  19938. "Remove the maximum common indentation from the buffer."
  19939. (untabify (point-min) (point-max))
  19940. (let ((min 10000) re)
  19941. (if n
  19942. (setq min n)
  19943. (goto-char (point-min))
  19944. (while (re-search-forward "^ *[^ \n]" nil t)
  19945. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19946. (unless (or (= min 0) (= min 10000))
  19947. (setq re (format "^ \\{%d\\}" min))
  19948. (goto-char (point-min))
  19949. (while (re-search-forward re nil t)
  19950. (replace-match "")
  19951. (end-of-line 1))
  19952. min)))
  19953. (defun org-fill-template (template alist)
  19954. "Find each %key of ALIST in TEMPLATE and replace it."
  19955. (let ((case-fold-search nil)
  19956. entry key value)
  19957. (setq alist (sort (copy-sequence alist)
  19958. (lambda (a b) (< (length (car a)) (length (car b))))))
  19959. (while (setq entry (pop alist))
  19960. (setq template
  19961. (replace-regexp-in-string
  19962. (concat "%" (regexp-quote (car entry)))
  19963. (or (cdr entry) "") template t t)))
  19964. template))
  19965. (defun org-base-buffer (buffer)
  19966. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19967. (if (not buffer)
  19968. buffer
  19969. (or (buffer-base-buffer buffer)
  19970. buffer)))
  19971. (defsubst org-trim (s)
  19972. "Remove whitespace at the beginning and the end of string S."
  19973. (replace-regexp-in-string
  19974. "\\`[ \t\n\r]+" ""
  19975. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  19976. (defun org-wrap (string &optional width lines)
  19977. "Wrap string to either a number of lines, or a width in characters.
  19978. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19979. that costs. If there is a word longer than WIDTH, the text is actually
  19980. wrapped to the length of that word.
  19981. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19982. many lines, whatever width that takes.
  19983. The return value is a list of lines, without newlines at the end."
  19984. (let* ((words (org-split-string string "[ \t\n]+"))
  19985. (maxword (apply 'max (mapcar 'org-string-width words)))
  19986. w ll)
  19987. (cond (width
  19988. (org-do-wrap words (max maxword width)))
  19989. (lines
  19990. (setq w maxword)
  19991. (setq ll (org-do-wrap words maxword))
  19992. (if (<= (length ll) lines)
  19993. ll
  19994. (setq ll words)
  19995. (while (> (length ll) lines)
  19996. (setq w (1+ w))
  19997. (setq ll (org-do-wrap words w)))
  19998. ll))
  19999. (t (error "Cannot wrap this")))))
  20000. (defun org-do-wrap (words width)
  20001. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20002. (let (lines line)
  20003. (while words
  20004. (setq line (pop words))
  20005. (while (and words (< (+ (length line) (length (car words))) width))
  20006. (setq line (concat line " " (pop words))))
  20007. (setq lines (push line lines)))
  20008. (nreverse lines)))
  20009. (defun org-split-string (string &optional separators)
  20010. "Splits STRING into substrings at SEPARATORS.
  20011. No empty strings are returned if there are matches at the beginning
  20012. and end of string."
  20013. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  20014. (start 0)
  20015. notfirst
  20016. (list nil))
  20017. (while (and (string-match rexp string
  20018. (if (and notfirst
  20019. (= start (match-beginning 0))
  20020. (< start (length string)))
  20021. (1+ start) start))
  20022. (< (match-beginning 0) (length string)))
  20023. (setq notfirst t)
  20024. (or (eq (match-beginning 0) 0)
  20025. (and (eq (match-beginning 0) (match-end 0))
  20026. (eq (match-beginning 0) start))
  20027. (setq list
  20028. (cons (substring string start (match-beginning 0))
  20029. list)))
  20030. (setq start (match-end 0)))
  20031. (or (eq start (length string))
  20032. (setq list
  20033. (cons (substring string start)
  20034. list)))
  20035. (nreverse list)))
  20036. (defun org-quote-vert (s)
  20037. "Replace \"|\" with \"\\vert\"."
  20038. (while (string-match "|" s)
  20039. (setq s (replace-match "\\vert" t t s)))
  20040. s)
  20041. (defun org-uuidgen-p (s)
  20042. "Is S an ID created by UUIDGEN?"
  20043. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20044. (defun org-in-src-block-p (&optional inside)
  20045. "Whether point is in a code source block.
  20046. When INSIDE is non-nil, don't consider we are within a src block
  20047. when point is at #+BEGIN_SRC or #+END_SRC."
  20048. (let ((case-fold-search t) ov)
  20049. (or (and (setq ov (overlays-at (point)))
  20050. (memq 'org-block-background
  20051. (overlay-properties (car ov))))
  20052. (and (not inside)
  20053. (save-match-data
  20054. (save-excursion
  20055. (beginning-of-line)
  20056. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20057. (defun org-context ()
  20058. "Return a list of contexts of the current cursor position.
  20059. If several contexts apply, all are returned.
  20060. Each context entry is a list with a symbol naming the context, and
  20061. two positions indicating start and end of the context. Possible
  20062. contexts are:
  20063. :headline anywhere in a headline
  20064. :headline-stars on the leading stars in a headline
  20065. :todo-keyword on a TODO keyword (including DONE) in a headline
  20066. :tags on the TAGS in a headline
  20067. :priority on the priority cookie in a headline
  20068. :item on the first line of a plain list item
  20069. :item-bullet on the bullet/number of a plain list item
  20070. :checkbox on the checkbox in a plain list item
  20071. :table in an org-mode table
  20072. :table-special on a special filed in a table
  20073. :table-table in a table.el table
  20074. :clocktable in a clocktable
  20075. :src-block in a source block
  20076. :link on a hyperlink
  20077. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20078. :target on a <<target>>
  20079. :radio-target on a <<<radio-target>>>
  20080. :latex-fragment on a LaTeX fragment
  20081. :latex-preview on a LaTeX fragment with overlaid preview image
  20082. This function expects the position to be visible because it uses font-lock
  20083. faces as a help to recognize the following contexts: :table-special, :link,
  20084. and :keyword."
  20085. (let* ((f (get-text-property (point) 'face))
  20086. (faces (if (listp f) f (list f)))
  20087. (case-fold-search t)
  20088. (p (point)) clist o)
  20089. ;; First the large context
  20090. (cond
  20091. ((org-at-heading-p t)
  20092. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20093. (when (progn
  20094. (beginning-of-line 1)
  20095. (looking-at org-todo-line-tags-regexp))
  20096. (push (org-point-in-group p 1 :headline-stars) clist)
  20097. (push (org-point-in-group p 2 :todo-keyword) clist)
  20098. (push (org-point-in-group p 4 :tags) clist))
  20099. (goto-char p)
  20100. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20101. (if (looking-at "\\[#[A-Z0-9]\\]")
  20102. (push (org-point-in-group p 0 :priority) clist)))
  20103. ((org-at-item-p)
  20104. (push (org-point-in-group p 2 :item-bullet) clist)
  20105. (push (list :item (point-at-bol)
  20106. (save-excursion (org-end-of-item) (point)))
  20107. clist)
  20108. (and (org-at-item-checkbox-p)
  20109. (push (org-point-in-group p 0 :checkbox) clist)))
  20110. ((org-at-table-p)
  20111. (push (list :table (org-table-begin) (org-table-end)) clist)
  20112. (if (memq 'org-formula faces)
  20113. (push (list :table-special
  20114. (previous-single-property-change p 'face)
  20115. (next-single-property-change p 'face)) clist)))
  20116. ((org-at-table-p 'any)
  20117. (push (list :table-table) clist)))
  20118. (goto-char p)
  20119. (let ((case-fold-search t))
  20120. ;; New the "medium" contexts: clocktables, source blocks
  20121. (cond ((org-in-clocktable-p)
  20122. (push (list :clocktable
  20123. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20124. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20125. (match-beginning 1))
  20126. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20127. (match-end 0))) clist))
  20128. ((org-in-src-block-p)
  20129. (push (list :src-block
  20130. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20131. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20132. (match-beginning 1))
  20133. (and (search-forward "#+END_SRC" nil t)
  20134. (match-beginning 0))) clist))))
  20135. (goto-char p)
  20136. ;; Now the small context
  20137. (cond
  20138. ((org-at-timestamp-p)
  20139. (push (org-point-in-group p 0 :timestamp) clist))
  20140. ((memq 'org-link faces)
  20141. (push (list :link
  20142. (previous-single-property-change p 'face)
  20143. (next-single-property-change p 'face)) clist))
  20144. ((memq 'org-special-keyword faces)
  20145. (push (list :keyword
  20146. (previous-single-property-change p 'face)
  20147. (next-single-property-change p 'face)) clist))
  20148. ((org-at-target-p)
  20149. (push (org-point-in-group p 0 :target) clist)
  20150. (goto-char (1- (match-beginning 0)))
  20151. (if (looking-at org-radio-target-regexp)
  20152. (push (org-point-in-group p 0 :radio-target) clist))
  20153. (goto-char p))
  20154. ((setq o (car (delq nil
  20155. (mapcar
  20156. (lambda (x)
  20157. (if (memq x org-latex-fragment-image-overlays) x))
  20158. (overlays-at (point))))))
  20159. (push (list :latex-fragment
  20160. (overlay-start o) (overlay-end o)) clist)
  20161. (push (list :latex-preview
  20162. (overlay-start o) (overlay-end o)) clist))
  20163. ((org-inside-LaTeX-fragment-p)
  20164. ;; FIXME: positions wrong.
  20165. (push (list :latex-fragment (point) (point)) clist)))
  20166. (setq clist (nreverse (delq nil clist)))
  20167. clist))
  20168. ;; FIXME: Compare with at-regexp-p Do we need both?
  20169. (defun org-in-regexp (re &optional nlines visually)
  20170. "Check if point is inside a match of regexp.
  20171. Normally only the current line is checked, but you can include NLINES extra
  20172. lines both before and after point into the search.
  20173. If VISUALLY is set, require that the cursor is not after the match but
  20174. really on, so that the block visually is on the match."
  20175. (catch 'exit
  20176. (let ((pos (point))
  20177. (eol (point-at-eol (+ 1 (or nlines 0))))
  20178. (inc (if visually 1 0)))
  20179. (save-excursion
  20180. (beginning-of-line (- 1 (or nlines 0)))
  20181. (while (re-search-forward re eol t)
  20182. (if (and (<= (match-beginning 0) pos)
  20183. (>= (+ inc (match-end 0)) pos))
  20184. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  20185. (defun org-at-regexp-p (regexp)
  20186. "Is point inside a match of REGEXP in the current line?"
  20187. (catch 'exit
  20188. (save-excursion
  20189. (let ((pos (point)) (end (point-at-eol)))
  20190. (beginning-of-line 1)
  20191. (while (re-search-forward regexp end t)
  20192. (if (and (<= (match-beginning 0) pos)
  20193. (>= (match-end 0) pos))
  20194. (throw 'exit t)))
  20195. nil))))
  20196. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20197. "Non-nil when point is between matches of START-RE and END-RE.
  20198. Also return a non-nil value when point is on one of the matches.
  20199. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20200. buffer positions. Default values are the positions of headlines
  20201. surrounding the point.
  20202. The functions returns a cons cell whose car (resp. cdr) is the
  20203. position before START-RE (resp. after END-RE)."
  20204. (save-match-data
  20205. (let ((pos (point))
  20206. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20207. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20208. beg end)
  20209. (save-excursion
  20210. ;; Point is on a block when on START-RE or if START-RE can be
  20211. ;; found before it...
  20212. (and (or (org-at-regexp-p start-re)
  20213. (re-search-backward start-re limit-up t))
  20214. (setq beg (match-beginning 0))
  20215. ;; ... and END-RE after it...
  20216. (goto-char (match-end 0))
  20217. (re-search-forward end-re limit-down t)
  20218. (> (setq end (match-end 0)) pos)
  20219. ;; ... without another START-RE in-between.
  20220. (goto-char (match-beginning 0))
  20221. (not (re-search-backward start-re (1+ beg) t))
  20222. ;; Return value.
  20223. (cons beg end))))))
  20224. (defun org-in-block-p (names)
  20225. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20226. NAMES is a list of strings containing names of blocks.
  20227. Return first block name matched, or nil. Beware that in case of
  20228. nested blocks, the returned name may not belong to the closest
  20229. block from point."
  20230. (save-match-data
  20231. (catch 'exit
  20232. (let ((case-fold-search t)
  20233. (lim-up (save-excursion (outline-previous-heading)))
  20234. (lim-down (save-excursion (outline-next-heading))))
  20235. (mapc (lambda (name)
  20236. (let ((n (regexp-quote name)))
  20237. (when (org-between-regexps-p
  20238. (concat "^[ \t]*#\\+begin_" n)
  20239. (concat "^[ \t]*#\\+end_" n)
  20240. lim-up lim-down)
  20241. (throw 'exit n))))
  20242. names))
  20243. nil)))
  20244. (defun org-in-drawer-p ()
  20245. "Non-nil if point is within a drawer.
  20246. If point is within a drawer, return it, as parsed data."
  20247. (let ((element (save-match-data (org-element-at-point))))
  20248. (while (and element (not (memq (org-element-type element)
  20249. '(drawer property-drawer))))
  20250. (setq element (org-element-property :parent element)))
  20251. element))
  20252. (defun org-occur-in-agenda-files (regexp &optional nlines)
  20253. "Call `multi-occur' with buffers for all agenda files."
  20254. (interactive "sOrg-files matching: \np")
  20255. (let* ((files (org-agenda-files))
  20256. (tnames (mapcar 'file-truename files))
  20257. (extra org-agenda-text-search-extra-files)
  20258. f)
  20259. (when (eq (car extra) 'agenda-archives)
  20260. (setq extra (cdr extra))
  20261. (setq files (org-add-archive-files files)))
  20262. (while (setq f (pop extra))
  20263. (unless (member (file-truename f) tnames)
  20264. (add-to-list 'files f 'append)
  20265. (add-to-list 'tnames (file-truename f) 'append)))
  20266. (multi-occur
  20267. (mapcar (lambda (x)
  20268. (with-current-buffer
  20269. (or (get-file-buffer x) (find-file-noselect x))
  20270. (widen)
  20271. (current-buffer)))
  20272. files)
  20273. regexp)))
  20274. (if (boundp 'occur-mode-find-occurrence-hook)
  20275. ;; Emacs 23
  20276. (add-hook 'occur-mode-find-occurrence-hook
  20277. (lambda ()
  20278. (when (derived-mode-p 'org-mode)
  20279. (org-reveal))))
  20280. ;; Emacs 22
  20281. (defadvice occur-mode-goto-occurrence
  20282. (after org-occur-reveal activate)
  20283. (and (derived-mode-p 'org-mode) (org-reveal)))
  20284. (defadvice occur-mode-goto-occurrence-other-window
  20285. (after org-occur-reveal activate)
  20286. (and (derived-mode-p 'org-mode) (org-reveal)))
  20287. (defadvice occur-mode-display-occurrence
  20288. (after org-occur-reveal activate)
  20289. (when (derived-mode-p 'org-mode)
  20290. (let ((pos (occur-mode-find-occurrence)))
  20291. (with-current-buffer (marker-buffer pos)
  20292. (save-excursion
  20293. (goto-char pos)
  20294. (org-reveal)))))))
  20295. (defun org-occur-link-in-agenda-files ()
  20296. "Create a link and search for it in the agendas.
  20297. The link is not stored in `org-stored-links', it is just created
  20298. for the search purpose."
  20299. (interactive)
  20300. (let ((link (condition-case nil
  20301. (org-store-link nil)
  20302. (error "Unable to create a link to here"))))
  20303. (org-occur-in-agenda-files (regexp-quote link))))
  20304. (defun org-reverse-string (string)
  20305. "Return the reverse of STRING."
  20306. (apply 'string (reverse (string-to-list string))))
  20307. (defun org-uniquify-alist (alist)
  20308. "Merge elements of ALIST with the same key.
  20309. For example, in this alist:
  20310. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20311. => '((a 1 3) (b 2))
  20312. merge (a 1) and (a 3) into (a 1 3).
  20313. The function returns the new ALIST."
  20314. (let (rtn)
  20315. (mapc
  20316. (lambda (e)
  20317. (let (n)
  20318. (if (not (assoc (car e) rtn))
  20319. (push e rtn)
  20320. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20321. (setq rtn (assq-delete-all (car e) rtn))
  20322. (push n rtn))))
  20323. alist)
  20324. rtn))
  20325. (defun org-delete-all (elts list)
  20326. "Remove all elements in ELTS from LIST."
  20327. (while elts
  20328. (setq list (delete (pop elts) list)))
  20329. list)
  20330. (defun org-count (cl-item cl-seq)
  20331. "Count the number of occurrences of ITEM in SEQ.
  20332. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20333. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20334. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20335. (while (< cl-start cl-end)
  20336. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20337. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20338. (setq cl-start (1+ cl-start)))
  20339. cl-count))
  20340. (defun org-remove-if (predicate seq)
  20341. "Remove everything from SEQ that fulfills PREDICATE."
  20342. (let (res e)
  20343. (while seq
  20344. (setq e (pop seq))
  20345. (if (not (funcall predicate e)) (push e res)))
  20346. (nreverse res)))
  20347. (defun org-remove-if-not (predicate seq)
  20348. "Remove everything from SEQ that does not fulfill PREDICATE."
  20349. (let (res e)
  20350. (while seq
  20351. (setq e (pop seq))
  20352. (if (funcall predicate e) (push e res)))
  20353. (nreverse res)))
  20354. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20355. "Reduce two-argument FUNCTION across SEQ.
  20356. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20357. \":initial-value\" removed."
  20358. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20359. (cadr (memq :initial-value cl-keys)))
  20360. (cl-seq (pop cl-seq))
  20361. (t (funcall cl-func)))))
  20362. (while cl-seq
  20363. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20364. cl-accum))
  20365. (defun org-every (pred seq)
  20366. "Return true if PREDICATE is true of every element of SEQ.
  20367. Adapted from `every' in cl.el."
  20368. (catch 'org-every
  20369. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20370. t))
  20371. (defun org-some (pred seq)
  20372. "Return true if PREDICATE is true of any element of SEQ.
  20373. Adapted from `some' in cl.el."
  20374. (catch 'org-some
  20375. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20376. nil))
  20377. (defun org-back-over-empty-lines ()
  20378. "Move backwards over whitespace, to the beginning of the first empty line.
  20379. Returns the number of empty lines passed."
  20380. (let ((pos (point)))
  20381. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20382. (skip-chars-backward " \t\n\r")
  20383. (unless (eobp)
  20384. (forward-line -1)))
  20385. (beginning-of-line 2)
  20386. (goto-char (min (point) pos))
  20387. (count-lines (point) pos)))
  20388. (defun org-skip-whitespace ()
  20389. (skip-chars-forward " \t\n\r"))
  20390. (defun org-point-in-group (point group &optional context)
  20391. "Check if POINT is in match-group GROUP.
  20392. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20393. match. If the match group does not exist or point is not inside it,
  20394. return nil."
  20395. (and (match-beginning group)
  20396. (>= point (match-beginning group))
  20397. (<= point (match-end group))
  20398. (if context
  20399. (list context (match-beginning group) (match-end group))
  20400. t)))
  20401. (defun org-switch-to-buffer-other-window (&rest args)
  20402. "Switch to buffer in a second window on the current frame.
  20403. In particular, do not allow pop-up frames.
  20404. Returns the newly created buffer."
  20405. (org-no-popups
  20406. (apply 'switch-to-buffer-other-window args)))
  20407. (defun org-combine-plists (&rest plists)
  20408. "Create a single property list from all plists in PLISTS.
  20409. The process starts by copying the first list, and then setting properties
  20410. from the other lists. Settings in the last list are the most significant
  20411. ones and overrule settings in the other lists."
  20412. (let ((rtn (copy-sequence (pop plists)))
  20413. p v ls)
  20414. (while plists
  20415. (setq ls (pop plists))
  20416. (while ls
  20417. (setq p (pop ls) v (pop ls))
  20418. (setq rtn (plist-put rtn p v))))
  20419. rtn))
  20420. (defun org-replace-escapes (string table)
  20421. "Replace %-escapes in STRING with values in TABLE.
  20422. TABLE is an association list with keys like \"%a\" and string values.
  20423. The sequences in STRING may contain normal field width and padding information,
  20424. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20425. so values can contain further %-escapes if they are define later in TABLE."
  20426. (let ((tbl (copy-alist table))
  20427. (case-fold-search nil)
  20428. (pchg 0)
  20429. e re rpl)
  20430. (while (setq e (pop tbl))
  20431. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20432. (when (and (cdr e) (string-match re (cdr e)))
  20433. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20434. (safe "SREF"))
  20435. (add-text-properties 0 3 (list 'sref sref) safe)
  20436. (setcdr e (replace-match safe t t (cdr e)))))
  20437. (while (string-match re string)
  20438. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20439. (cdr e)))
  20440. (setq string (replace-match rpl t t string))))
  20441. (while (setq pchg (next-property-change pchg string))
  20442. (let ((sref (get-text-property pchg 'sref string)))
  20443. (when (and sref (string-match "SREF" string pchg))
  20444. (setq string (replace-match sref t t string)))))
  20445. string))
  20446. (defun org-sublist (list start end)
  20447. "Return a section of LIST, from START to END.
  20448. Counting starts at 1."
  20449. (let (rtn (c start))
  20450. (setq list (nthcdr (1- start) list))
  20451. (while (and list (<= c end))
  20452. (push (pop list) rtn)
  20453. (setq c (1+ c)))
  20454. (nreverse rtn)))
  20455. (defun org-find-base-buffer-visiting (file)
  20456. "Like `find-buffer-visiting' but always return the base buffer and
  20457. not an indirect buffer."
  20458. (let ((buf (or (get-file-buffer file)
  20459. (find-buffer-visiting file))))
  20460. (if buf
  20461. (or (buffer-base-buffer buf) buf)
  20462. nil)))
  20463. (defun org-image-file-name-regexp (&optional extensions)
  20464. "Return regexp matching the file names of images.
  20465. If EXTENSIONS is given, only match these."
  20466. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20467. (image-file-name-regexp)
  20468. (let ((image-file-name-extensions
  20469. (or extensions
  20470. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20471. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20472. (concat "\\."
  20473. (regexp-opt (nconc (mapcar 'upcase
  20474. image-file-name-extensions)
  20475. image-file-name-extensions)
  20476. t)
  20477. "\\'"))))
  20478. (defun org-file-image-p (file &optional extensions)
  20479. "Return non-nil if FILE is an image."
  20480. (save-match-data
  20481. (string-match (org-image-file-name-regexp extensions) file)))
  20482. (defun org-get-cursor-date (&optional with-time)
  20483. "Return the date at cursor in as a time.
  20484. This works in the calendar and in the agenda, anywhere else it just
  20485. returns the current time.
  20486. If WITH-TIME is non-nil, returns the time of the event at point (in
  20487. the agenda) or the current time of the day."
  20488. (let (date day defd tp tm hod mod)
  20489. (when with-time
  20490. (setq tp (get-text-property (point) 'time))
  20491. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20492. (setq hod (string-to-number (match-string 1 tp))
  20493. mod (string-to-number (match-string 2 tp))))
  20494. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20495. mod (nth 1 (decode-time (current-time))))))
  20496. (cond
  20497. ((eq major-mode 'calendar-mode)
  20498. (setq date (calendar-cursor-to-date)
  20499. defd (encode-time 0 (or mod 0) (or hod 0)
  20500. (nth 1 date) (nth 0 date) (nth 2 date))))
  20501. ((eq major-mode 'org-agenda-mode)
  20502. (setq day (get-text-property (point) 'day))
  20503. (if day
  20504. (setq date (calendar-gregorian-from-absolute day)
  20505. defd (encode-time 0 (or mod 0) (or hod 0)
  20506. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20507. (or defd (current-time))))
  20508. (defun org-mark-subtree (&optional up)
  20509. "Mark the current subtree.
  20510. This puts point at the start of the current subtree, and mark at
  20511. the end. If a numeric prefix UP is given, move up into the
  20512. hierarchy of headlines by UP levels before marking the subtree."
  20513. (interactive "P")
  20514. (org-with-limited-levels
  20515. (cond ((org-at-heading-p) (beginning-of-line))
  20516. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20517. (t (outline-previous-visible-heading 1))))
  20518. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20519. (if (org-called-interactively-p 'any)
  20520. (call-interactively 'org-mark-element)
  20521. (org-mark-element)))
  20522. ;;; Indentation
  20523. (defun org--get-expected-indentation (element contentsp)
  20524. "Expected indentation column for current line, according to ELEMENT.
  20525. ELEMENT is an element containing point. CONTENTSP is non-nil
  20526. when indentation is to be computed according to contents of
  20527. ELEMENT."
  20528. (let ((type (org-element-type element))
  20529. (start (org-element-property :begin element)))
  20530. (org-with-wide-buffer
  20531. (cond
  20532. (contentsp
  20533. (case type
  20534. (footnote-definition 0)
  20535. ((headline inlinetask nil)
  20536. (if (not org-adapt-indentation) 0
  20537. (let ((level (org-current-level)))
  20538. (if level (1+ level) 0))))
  20539. ((item plain list)
  20540. (org-list-item-body-column
  20541. (or (org-element-property :post-affiliated element) start)))
  20542. (otherwise
  20543. (goto-char start)
  20544. (org-get-indentation))))
  20545. ((memq type '(headline inlinetask nil))
  20546. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  20547. (org--get-expected-indentation element t)
  20548. 0))
  20549. ((eq type 'footnote-definition) 0)
  20550. ;; First paragraph of a footnote definition or an item.
  20551. ;; Indent like parent.
  20552. ((< (line-beginning-position) start)
  20553. (org--get-expected-indentation
  20554. (org-element-property :parent element) t))
  20555. ;; At first line: indent according to previous sibling, if any,
  20556. ;; ignoring footnote definitions and inline tasks, or parent's
  20557. ;; contents.
  20558. ((= (line-beginning-position) start)
  20559. (catch 'exit
  20560. (while t
  20561. (if (= (point-min) start) (throw 'exit 0)
  20562. (goto-char (1- start))
  20563. (let ((previous (org-element-at-point)))
  20564. (while (let ((parent (org-element-property :parent previous)))
  20565. (and parent
  20566. (setq previous parent)
  20567. (<= (org-element-property :end parent) start))))
  20568. (cond ((or (not previous)
  20569. (> (org-element-property :end previous) start))
  20570. (throw 'exit (org--get-expected-indentation previous t)))
  20571. ((memq (org-element-type previous)
  20572. '(footnote-definition inlinetask))
  20573. (setq start (org-element-property :begin previous)))
  20574. (t (goto-char (org-element-property :begin previous))
  20575. (throw 'exit (org-get-indentation)))))))))
  20576. ;; Otherwise, move to the first non-blank line above.
  20577. (t
  20578. (beginning-of-line)
  20579. (let ((pos (point)))
  20580. (skip-chars-backward " \r\t\n")
  20581. (cond
  20582. ;; Two blank lines end a footnote definition or a plain
  20583. ;; list. When we indent an empty line after them, the
  20584. ;; containing list or footnote definition is over, so it
  20585. ;; qualifies as a previous sibling. Therefore, we indent
  20586. ;; like its first line.
  20587. ((and (memq type '(footnote-definition plain-list))
  20588. (> (count-lines (point) pos) 2))
  20589. (goto-char start)
  20590. (org-get-indentation))
  20591. ;; Line above is the first one of a paragraph at the
  20592. ;; beginning of an item or a footnote definition. Indent
  20593. ;; like parent.
  20594. ((< (line-beginning-position) start)
  20595. (org--get-expected-indentation
  20596. (org-element-property :parent element) t))
  20597. ;; POS is after contents in a greater element. Indent like
  20598. ;; the beginning of the element.
  20599. ;;
  20600. ;; As a special case, if point is at the end of a footnote
  20601. ;; definition or an item, indent like the very last element
  20602. ;; within.
  20603. ((let ((cend (org-element-property :contents-end element)))
  20604. (and cend (<= cend pos)))
  20605. (if (memq type '(footnote-definition item plain-list))
  20606. (org--get-expected-indentation (org-element-at-point) nil)
  20607. (goto-char start)
  20608. (org-get-indentation)))
  20609. ;; In any other case, indent like the current line.
  20610. (t (org-get-indentation)))))))))
  20611. (defun org--align-node-property ()
  20612. "Align node property at point.
  20613. Alignment is done according to `org-property-format', which see."
  20614. (when (save-excursion
  20615. (beginning-of-line)
  20616. (looking-at org-property-re))
  20617. (replace-match
  20618. (concat (match-string 4)
  20619. (format org-property-format (match-string 1) (match-string 3)))
  20620. t t)))
  20621. (defun org-indent-line ()
  20622. "Indent line depending on context.
  20623. Indentation is done according to the following rules:
  20624. - Footnote definitions, headlines and inline tasks have to
  20625. start at column 0.
  20626. - On the very first line of an element, consider, in order, the
  20627. next rules until one matches:
  20628. 1. If there's a sibling element before, ignoring footnote
  20629. definitions and inline tasks, indent like its first line.
  20630. 2. If element has a parent, indent like its contents. More
  20631. precisely, if parent is an item, indent after the
  20632. description part, if any, or the bullet (see
  20633. ``org-list-description-max-indent'). Else, indent like
  20634. parent's first line.
  20635. 3. Otherwise, indent relatively to current level, if
  20636. `org-adapt-indentation' is non-nil, or to left margin.
  20637. - On a blank line at the end of a plain list, an item, or
  20638. a footnote definition, indent like the very last element
  20639. within.
  20640. - In the code part of a source block, use language major mode
  20641. to indent current line if `org-src-tab-acts-natively' is
  20642. non-nil. If it is nil, do nothing.
  20643. - Otherwise, indent like the first non-blank line above.
  20644. The function doesn't indent an item as it could break the whole
  20645. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  20646. \\[org-shiftmetaright].
  20647. Also align node properties according to `org-property-format'."
  20648. (interactive)
  20649. (cond
  20650. (orgstruct-is-++
  20651. (let ((indent-line-function
  20652. (cadadr (assq 'indent-line-function org-fb-vars))))
  20653. (indent-according-to-mode)))
  20654. ((org-at-heading-p) 'noindent)
  20655. (t
  20656. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  20657. (type (org-element-type element)))
  20658. (cond ((and (memq type '(plain-list item))
  20659. (= (line-beginning-position)
  20660. (or (org-element-property :post-affiliated element)
  20661. (org-element-property :begin element))))
  20662. 'noindent)
  20663. ((and (eq type 'src-block)
  20664. org-src-tab-acts-natively
  20665. (> (line-beginning-position)
  20666. (org-element-property :post-affiliated element))
  20667. (< (line-beginning-position)
  20668. (org-with-wide-buffer
  20669. (goto-char (org-element-property :end element))
  20670. (skip-chars-backward " \r\t\n")
  20671. (line-beginning-position))))
  20672. (let ((org-src-strip-leading-and-trailing-blank-lines nil))
  20673. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB"))))
  20674. (t
  20675. (let ((column (org--get-expected-indentation element nil)))
  20676. ;; Preserve current column.
  20677. (if (<= (current-column) (current-indentation))
  20678. (org-indent-line-to column)
  20679. (save-excursion (org-indent-line-to column))))
  20680. ;; Align node property. Also preserve current column.
  20681. (when (eq type 'node-property)
  20682. (let ((column (current-column)))
  20683. (org--align-node-property)
  20684. (org-move-to-column column)))))))))
  20685. (defun org-indent-region (start end)
  20686. "Indent each non-blank line in the region.
  20687. Called from a program, START and END specify the region to
  20688. indent. The function will not indent contents of example blocks,
  20689. verse blocks and export blocks as leading white spaces are
  20690. assumed to be significant there."
  20691. (interactive "r")
  20692. (save-excursion
  20693. (goto-char start)
  20694. (beginning-of-line)
  20695. (let ((indent-to
  20696. (lambda (ind pos)
  20697. ;; Set IND as indentation for all lines between point and
  20698. ;; POS or END, whichever comes first. Blank lines are
  20699. ;; ignored. Leave point after POS once done.
  20700. (let ((limit (copy-marker (min end pos))))
  20701. (while (< (point) limit)
  20702. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  20703. (forward-line))
  20704. (set-marker limit nil))))
  20705. (end (copy-marker end)))
  20706. (while (< (point) end)
  20707. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  20708. (let* ((element (org-element-at-point))
  20709. (type (org-element-type element))
  20710. (element-end (copy-marker (org-element-property :end element)))
  20711. (ind (org--get-expected-indentation element nil)))
  20712. (if (or (memq type '(paragraph table table-row))
  20713. (not (or (org-element-property :contents-begin element)
  20714. (memq type
  20715. '(example-block export-block src-block)))))
  20716. ;; Elements here are indented as a single block. Also
  20717. ;; align node properties.
  20718. (progn
  20719. (when (eq type 'node-property)
  20720. (org--align-node-property)
  20721. (beginning-of-line))
  20722. (funcall indent-to ind element-end))
  20723. ;; Elements in this category consist of three parts:
  20724. ;; before the contents, the contents, and after the
  20725. ;; contents. The contents are treated specially,
  20726. ;; according to the element type, or not indented at
  20727. ;; all. Other parts are indented as a single block.
  20728. (let* ((post (copy-marker
  20729. (or (org-element-property :post-affiliated element)
  20730. (org-element-property :begin element))))
  20731. (cbeg
  20732. (copy-marker
  20733. (cond
  20734. ((not (org-element-property :contents-begin element))
  20735. ;; Fake contents for source blocks.
  20736. (org-with-wide-buffer
  20737. (goto-char post)
  20738. (forward-line)
  20739. (point)))
  20740. ((memq type '(footnote-definition item plain-list))
  20741. ;; Contents in these elements could start on
  20742. ;; the same line as the beginning of the
  20743. ;; element. Make sure we start indenting
  20744. ;; from the second line.
  20745. (org-with-wide-buffer
  20746. (goto-char post)
  20747. (forward-line)
  20748. (point)))
  20749. (t (org-element-property :contents-begin element)))))
  20750. (cend (copy-marker
  20751. (or (org-element-property :contents-end element)
  20752. ;; Fake contents for source blocks.
  20753. (org-with-wide-buffer
  20754. (goto-char element-end)
  20755. (skip-chars-backward " \r\t\n")
  20756. (line-beginning-position))))))
  20757. (funcall indent-to ind cbeg)
  20758. (when (< (point) end)
  20759. (case type
  20760. ((example-block export-block verse-block))
  20761. (src-block
  20762. ;; In a source block, indent source code according
  20763. ;; to language major mode, but only if
  20764. ;; `org-src-tab-acts-natively' is non-nil.
  20765. (when (and (< (point) end) org-src-tab-acts-natively)
  20766. (ignore-errors
  20767. (let (org-src-strip-leading-and-trailing-blank-lines
  20768. ;; Region boundaries in edit buffer.
  20769. (start (1+ (- (point) cbeg)))
  20770. (end (- (min cend end) cbeg)))
  20771. (org-babel-do-in-edit-buffer
  20772. (indent-region start end))))))
  20773. (t (org-indent-region (point) (min cend end))))
  20774. (goto-char (min cend end))
  20775. (when (< (point) end) (funcall indent-to ind element-end)))
  20776. (set-marker post nil)
  20777. (set-marker cbeg nil)
  20778. (set-marker cend nil)))
  20779. (set-marker element-end nil))))
  20780. (set-marker end nil))))
  20781. (defun org-indent-drawer ()
  20782. "Indent the drawer at point."
  20783. (interactive)
  20784. (unless (save-excursion
  20785. (beginning-of-line)
  20786. (org-looking-at-p org-drawer-regexp))
  20787. (user-error "Not at a drawer"))
  20788. (let ((element (org-element-at-point)))
  20789. (unless (memq (org-element-type element) '(drawer property-drawer))
  20790. (user-error "Not at a drawer"))
  20791. (org-with-wide-buffer
  20792. (org-indent-region (org-element-property :begin element)
  20793. (org-element-property :end element))))
  20794. (message "Drawer at point indented"))
  20795. (defun org-indent-block ()
  20796. "Indent the block at point."
  20797. (interactive)
  20798. (unless (save-excursion
  20799. (beginning-of-line)
  20800. (let ((case-fold-search t))
  20801. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  20802. (user-error "Not at a block"))
  20803. (let ((element (org-element-at-point)))
  20804. (unless (memq (org-element-type element)
  20805. '(comment-block center-block example-block export-block
  20806. quote-block special-block src-block
  20807. verse-block))
  20808. (user-error "Not at a block"))
  20809. (org-with-wide-buffer
  20810. (org-indent-region (org-element-property :begin element)
  20811. (org-element-property :end element))))
  20812. (message "Block at point indented"))
  20813. ;;; Filling
  20814. ;; We use our own fill-paragraph and auto-fill functions.
  20815. ;; `org-fill-paragraph' relies on adaptive filling and context
  20816. ;; checking. Appropriate `fill-prefix' is computed with
  20817. ;; `org-adaptive-fill-function'.
  20818. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20819. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20820. ;; `org-adaptive-fill-function' value. Internally,
  20821. ;; `org-comment-line-break-function' breaks the line.
  20822. ;; `org-setup-filling' installs filling and auto-filling related
  20823. ;; variables during `org-mode' initialization.
  20824. (defvar org-element-paragraph-separate) ; org-element.el
  20825. (defun org-setup-filling ()
  20826. (require 'org-element)
  20827. ;; Prevent auto-fill from inserting unwanted new items.
  20828. (when (boundp 'fill-nobreak-predicate)
  20829. (org-set-local
  20830. 'fill-nobreak-predicate
  20831. (org-uniquify
  20832. (append fill-nobreak-predicate
  20833. '(org-fill-line-break-nobreak-p
  20834. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20835. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  20836. (org-set-local 'paragraph-start paragraph-ending)
  20837. (org-set-local 'paragraph-separate paragraph-ending))
  20838. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20839. (org-set-local 'auto-fill-inhibit-regexp nil)
  20840. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20841. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20842. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20843. (defun org-fill-line-break-nobreak-p ()
  20844. "Non-nil when a new line at point would create an Org line break."
  20845. (save-excursion
  20846. (skip-chars-backward "[ \t]")
  20847. (skip-chars-backward "\\\\")
  20848. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20849. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20850. "Non-nil when a new line at point would split a timestamp."
  20851. (and (org-at-timestamp-p t)
  20852. (not (looking-at org-ts-regexp-both))))
  20853. (declare-function message-in-body-p "message" ())
  20854. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20855. (defun org-adaptive-fill-function ()
  20856. "Compute a fill prefix for the current line.
  20857. Return fill prefix, as a string, or nil if current line isn't
  20858. meant to be filled. For convenience, if `adaptive-fill-regexp'
  20859. matches in paragraphs or comments, use it."
  20860. (catch 'exit
  20861. (when (derived-mode-p 'message-mode)
  20862. (save-excursion
  20863. (beginning-of-line)
  20864. (cond ((or (not (message-in-body-p))
  20865. (looking-at orgtbl-line-start-regexp))
  20866. (throw 'exit nil))
  20867. ((looking-at message-cite-prefix-regexp)
  20868. (throw 'exit (match-string-no-properties 0)))
  20869. ((looking-at org-outline-regexp)
  20870. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20871. (org-with-wide-buffer
  20872. (let* ((p (line-beginning-position))
  20873. (element (save-excursion
  20874. (beginning-of-line)
  20875. (or (ignore-errors (org-element-at-point))
  20876. (user-error "An element cannot be parsed line %d"
  20877. (line-number-at-pos (point))))))
  20878. (type (org-element-type element))
  20879. (post-affiliated (org-element-property :post-affiliated element)))
  20880. (unless (and post-affiliated (< p post-affiliated))
  20881. (case type
  20882. (comment
  20883. (save-excursion
  20884. (beginning-of-line)
  20885. (looking-at "[ \t]*")
  20886. (concat (match-string 0) "# ")))
  20887. (footnote-definition "")
  20888. ((item plain-list)
  20889. (make-string (org-list-item-body-column
  20890. (or post-affiliated
  20891. (org-element-property :begin element)))
  20892. ? ))
  20893. (paragraph
  20894. ;; Fill prefix is usually the same as the current line,
  20895. ;; unless the paragraph is at the beginning of an item.
  20896. (let ((parent (org-element-property :parent element)))
  20897. (save-excursion
  20898. (beginning-of-line)
  20899. (cond ((eq (org-element-type parent) 'item)
  20900. (make-string (org-list-item-body-column
  20901. (org-element-property :begin parent))
  20902. ? ))
  20903. ((and adaptive-fill-regexp
  20904. ;; Locally disable
  20905. ;; `adaptive-fill-function' to let
  20906. ;; `fill-context-prefix' handle
  20907. ;; `adaptive-fill-regexp' variable.
  20908. (let (adaptive-fill-function)
  20909. (fill-context-prefix
  20910. post-affiliated
  20911. (org-element-property :end element)))))
  20912. ((looking-at "[ \t]+") (match-string 0))
  20913. (t "")))))
  20914. (comment-block
  20915. ;; Only fill contents if P is within block boundaries.
  20916. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20917. (forward-line)
  20918. (point)))
  20919. (cend (save-excursion
  20920. (goto-char (org-element-property :end element))
  20921. (skip-chars-backward " \r\t\n")
  20922. (line-beginning-position))))
  20923. (when (and (>= p cbeg) (< p cend))
  20924. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20925. (match-string 0)
  20926. ""))))))))))
  20927. (declare-function message-goto-body "message" ())
  20928. (defvar message-cite-prefix-regexp) ; From message.el
  20929. (defun org-fill-paragraph (&optional justify)
  20930. "Fill element at point, when applicable.
  20931. This function only applies to comment blocks, comments, example
  20932. blocks and paragraphs. Also, as a special case, re-align table
  20933. when point is at one.
  20934. If JUSTIFY is non-nil (interactively, with prefix argument),
  20935. justify as well. If `sentence-end-double-space' is non-nil, then
  20936. period followed by one space does not end a sentence, so don't
  20937. break a line there. The variable `fill-column' controls the
  20938. width for filling.
  20939. For convenience, when point is at a plain list, an item or
  20940. a footnote definition, try to fill the first paragraph within."
  20941. (interactive)
  20942. (if (and (derived-mode-p 'message-mode)
  20943. (or (not (message-in-body-p))
  20944. (save-excursion (move-beginning-of-line 1)
  20945. (looking-at message-cite-prefix-regexp))))
  20946. ;; First ensure filling is correct in message-mode.
  20947. (let ((fill-paragraph-function
  20948. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20949. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20950. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20951. (paragraph-separate
  20952. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20953. (fill-paragraph nil))
  20954. (with-syntax-table org-mode-transpose-word-syntax-table
  20955. ;; Move to end of line in order to get the first paragraph
  20956. ;; within a plain list or a footnote definition.
  20957. (let ((element (save-excursion
  20958. (end-of-line)
  20959. (or (ignore-errors (org-element-at-point))
  20960. (user-error "An element cannot be parsed line %d"
  20961. (line-number-at-pos (point)))))))
  20962. ;; First check if point is in a blank line at the beginning of
  20963. ;; the buffer. In that case, ignore filling.
  20964. (case (org-element-type element)
  20965. ;; Use major mode filling function is src blocks.
  20966. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20967. ;; Align Org tables, leave table.el tables as-is.
  20968. (table-row (org-table-align) t)
  20969. (table
  20970. (when (eq (org-element-property :type element) 'org)
  20971. (save-excursion
  20972. (goto-char (org-element-property :post-affiliated element))
  20973. (org-table-align)))
  20974. t)
  20975. (paragraph
  20976. ;; Paragraphs may contain `line-break' type objects.
  20977. (let ((beg (max (point-min)
  20978. (org-element-property :contents-begin element)))
  20979. (end (min (point-max)
  20980. (org-element-property :contents-end element))))
  20981. ;; Do nothing if point is at an affiliated keyword.
  20982. (if (< (line-end-position) beg) t
  20983. (when (derived-mode-p 'message-mode)
  20984. ;; In `message-mode', do not fill following citation
  20985. ;; in current paragraph nor text before message body.
  20986. (let ((body-start (save-excursion (message-goto-body))))
  20987. (when body-start (setq beg (max body-start beg))))
  20988. (when (save-excursion
  20989. (re-search-forward
  20990. (concat "^" message-cite-prefix-regexp) end t))
  20991. (setq end (match-beginning 0))))
  20992. ;; Fill paragraph, taking line breaks into account.
  20993. ;; For that, slice the paragraph using line breaks as
  20994. ;; separators, and fill the parts in reverse order to
  20995. ;; avoid messing with markers.
  20996. (save-excursion
  20997. (goto-char end)
  20998. (mapc
  20999. (lambda (pos)
  21000. (fill-region-as-paragraph pos (point) justify)
  21001. (goto-char pos))
  21002. ;; Find the list of ending positions for line breaks
  21003. ;; in the current paragraph. Add paragraph
  21004. ;; beginning to include first slice.
  21005. (nreverse
  21006. (cons beg
  21007. (org-element-map
  21008. (org-element--parse-objects
  21009. beg end nil (org-element-restriction 'paragraph))
  21010. 'line-break
  21011. (lambda (lb) (org-element-property :end lb)))))))
  21012. t)))
  21013. ;; Contents of `comment-block' type elements should be
  21014. ;; filled as plain text, but only if point is within block
  21015. ;; markers.
  21016. (comment-block
  21017. (let* ((case-fold-search t)
  21018. (beg (save-excursion
  21019. (goto-char (org-element-property :begin element))
  21020. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21021. (forward-line)
  21022. (point)))
  21023. (end (save-excursion
  21024. (goto-char (org-element-property :end element))
  21025. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21026. (line-beginning-position))))
  21027. (if (or (< (point) beg) (> (point) end)) t
  21028. (fill-region-as-paragraph
  21029. (save-excursion (end-of-line)
  21030. (re-search-backward "^[ \t]*$" beg 'move)
  21031. (line-beginning-position))
  21032. (save-excursion (beginning-of-line)
  21033. (re-search-forward "^[ \t]*$" end 'move)
  21034. (line-beginning-position))
  21035. justify))))
  21036. ;; Fill comments.
  21037. (comment
  21038. (let ((begin (org-element-property :post-affiliated element))
  21039. (end (org-element-property :end element)))
  21040. (when (and (>= (point) begin) (<= (point) end))
  21041. (let ((begin (save-excursion
  21042. (end-of-line)
  21043. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21044. (progn (forward-line) (point))
  21045. begin)))
  21046. (end (save-excursion
  21047. (end-of-line)
  21048. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21049. (1- (line-beginning-position))
  21050. (skip-chars-backward " \r\t\n")
  21051. (line-end-position)))))
  21052. ;; Do not fill comments when at a blank line.
  21053. (when (> end begin)
  21054. (let ((fill-prefix
  21055. (save-excursion
  21056. (beginning-of-line)
  21057. (looking-at "[ \t]*#")
  21058. (let ((comment-prefix (match-string 0)))
  21059. (goto-char (match-end 0))
  21060. (if (looking-at adaptive-fill-regexp)
  21061. (concat comment-prefix (match-string 0))
  21062. (concat comment-prefix " "))))))
  21063. (save-excursion
  21064. (fill-region-as-paragraph begin end justify))))))
  21065. t))
  21066. ;; Ignore every other element.
  21067. (otherwise t))))))
  21068. (defun org-auto-fill-function ()
  21069. "Auto-fill function."
  21070. ;; Check if auto-filling is meaningful.
  21071. (let ((fc (current-fill-column)))
  21072. (when (and fc (> (current-column) fc))
  21073. (let* ((fill-prefix (org-adaptive-fill-function))
  21074. ;; Enforce empty fill prefix, if required. Otherwise, it
  21075. ;; will be computed again.
  21076. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21077. (when fill-prefix (do-auto-fill))))))
  21078. (defun org-comment-line-break-function (&optional soft)
  21079. "Break line at point and indent, continuing comment if within one.
  21080. The inserted newline is marked hard if variable
  21081. `use-hard-newlines' is true, unless optional argument SOFT is
  21082. non-nil."
  21083. (if soft (insert-and-inherit ?\n) (newline 1))
  21084. (save-excursion (forward-char -1) (delete-horizontal-space))
  21085. (delete-horizontal-space)
  21086. (indent-to-left-margin)
  21087. (insert-before-markers-and-inherit fill-prefix))
  21088. ;;; Fixed Width Areas
  21089. (defun org-toggle-fixed-width ()
  21090. "Toggle fixed-width markup.
  21091. Add or remove fixed-width markup on current line, whenever it
  21092. makes sense. Return an error otherwise.
  21093. If a region is active and if it contains only fixed-width areas
  21094. or blank lines, remove all fixed-width markup in it. If the
  21095. region contains anything else, convert all non-fixed-width lines
  21096. to fixed-width ones.
  21097. Blank lines at the end of the region are ignored unless the
  21098. region only contains such lines."
  21099. (interactive)
  21100. (if (not (org-region-active-p))
  21101. ;; No region:
  21102. ;;
  21103. ;; Remove fixed width marker only in a fixed-with element.
  21104. ;;
  21105. ;; Add fixed width maker in paragraphs, in blank lines after
  21106. ;; elements or at the beginning of a headline or an inlinetask,
  21107. ;; and before any one-line elements (e.g., a clock).
  21108. (progn
  21109. (beginning-of-line)
  21110. (let* ((element (org-element-at-point))
  21111. (type (org-element-type element)))
  21112. (cond
  21113. ((and (eq type 'fixed-width)
  21114. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21115. (replace-match
  21116. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21117. ((and (memq type '(babel-call clock comment diary-sexp headline
  21118. horizontal-rule keyword paragraph
  21119. planning))
  21120. (or (not (org-element-property :post-affiliated element))
  21121. (<= (org-element-property :post-affiliated element)
  21122. (point))))
  21123. (skip-chars-forward " \t")
  21124. (insert ": "))
  21125. ((and (org-looking-at-p "[ \t]*$")
  21126. (or (eq type 'inlinetask)
  21127. (save-excursion
  21128. (skip-chars-forward " \r\t\n")
  21129. (<= (org-element-property :end element) (point)))))
  21130. (delete-region (point) (line-end-position))
  21131. (org-indent-line)
  21132. (insert ": "))
  21133. (t (user-error "Cannot insert a fixed-width line here")))))
  21134. ;; Region active.
  21135. (let* ((begin (save-excursion
  21136. (goto-char (region-beginning))
  21137. (line-beginning-position)))
  21138. (end (copy-marker
  21139. (save-excursion
  21140. (goto-char (region-end))
  21141. (unless (eolp) (beginning-of-line))
  21142. (if (save-excursion (re-search-backward "\\S-" begin t))
  21143. (progn (skip-chars-backward " \r\t\n") (point))
  21144. (point)))))
  21145. (all-fixed-width-p
  21146. (catch 'not-all-p
  21147. (save-excursion
  21148. (goto-char begin)
  21149. (skip-chars-forward " \r\t\n")
  21150. (when (eobp) (throw 'not-all-p nil))
  21151. (while (< (point) end)
  21152. (let ((element (org-element-at-point)))
  21153. (if (eq (org-element-type element) 'fixed-width)
  21154. (goto-char (org-element-property :end element))
  21155. (throw 'not-all-p nil))))
  21156. t))))
  21157. (if all-fixed-width-p
  21158. (save-excursion
  21159. (goto-char begin)
  21160. (while (< (point) end)
  21161. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21162. (replace-match
  21163. "" nil nil nil
  21164. (if (= (line-end-position) (match-end 0)) 0 1)))
  21165. (forward-line)))
  21166. (let ((min-ind (point-max)))
  21167. ;; Find minimum indentation across all lines.
  21168. (save-excursion
  21169. (goto-char begin)
  21170. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21171. (setq min-ind 0)
  21172. (catch 'zerop
  21173. (while (< (point) end)
  21174. (unless (org-looking-at-p "[ \t]*$")
  21175. (let ((ind (org-get-indentation)))
  21176. (setq min-ind (min min-ind ind))
  21177. (when (zerop ind) (throw 'zerop t))))
  21178. (forward-line)))))
  21179. ;; Loop over all lines and add fixed-width markup everywhere
  21180. ;; but in fixed-width lines.
  21181. (save-excursion
  21182. (goto-char begin)
  21183. (while (< (point) end)
  21184. (cond
  21185. ((org-at-heading-p)
  21186. (insert ": ")
  21187. (forward-line)
  21188. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21189. (insert ":")
  21190. (forward-line)))
  21191. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21192. (let* ((element (org-element-at-point))
  21193. (element-end (org-element-property :end element)))
  21194. (if (eq (org-element-type element) 'fixed-width)
  21195. (progn (goto-char element-end)
  21196. (skip-chars-backward " \r\t\n")
  21197. (forward-line))
  21198. (let ((limit (min end element-end)))
  21199. (while (< (point) limit)
  21200. (org-move-to-column min-ind t)
  21201. (insert ": ")
  21202. (forward-line))))))
  21203. (t
  21204. (org-move-to-column min-ind t)
  21205. (insert ": ")
  21206. (forward-line)))))))
  21207. (set-marker end nil))))
  21208. ;;; Comments
  21209. ;; Org comments syntax is quite complex. It requires the entire line
  21210. ;; to be just a comment. Also, even with the right syntax at the
  21211. ;; beginning of line, some some elements (i.e. verse-block or
  21212. ;; example-block) don't accept comments. Usual Emacs comment commands
  21213. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21214. ;; Org still relies on `comment-dwim', but cannot trust
  21215. ;; `comment-only-p'. So, `comment-region-function' and
  21216. ;; `uncomment-region-function' both point
  21217. ;; to`org-comment-or-uncomment-region'. Eventually,
  21218. ;; `org-insert-comment' takes care of insertion of comments at the
  21219. ;; beginning of line.
  21220. ;; `org-setup-comments-handling' install comments related variables
  21221. ;; during `org-mode' initialization.
  21222. (defun org-setup-comments-handling ()
  21223. (interactive)
  21224. (org-set-local 'comment-use-syntax nil)
  21225. (org-set-local 'comment-start "# ")
  21226. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21227. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  21228. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  21229. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  21230. (defun org-insert-comment ()
  21231. "Insert an empty comment above current line.
  21232. If the line is empty, insert comment at its beginning. When
  21233. point is within a source block, comment according to the related
  21234. major mode."
  21235. (if (let ((element (org-element-at-point)))
  21236. (and (eq (org-element-type element) 'src-block)
  21237. (< (save-excursion
  21238. (goto-char (org-element-property :post-affiliated element))
  21239. (line-end-position))
  21240. (point))
  21241. (> (save-excursion
  21242. (goto-char (org-element-property :end element))
  21243. (skip-chars-backward " \r\t\n")
  21244. (line-beginning-position))
  21245. (point))))
  21246. (org-babel-do-in-edit-buffer (call-interactively #'comment-dwim))
  21247. (beginning-of-line)
  21248. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21249. (open-line 1))
  21250. (org-indent-line)
  21251. (insert "# ")))
  21252. (defvar comment-empty-lines) ; From newcomment.el.
  21253. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  21254. "Comment or uncomment each non-blank line in the region.
  21255. Uncomment each non-blank line between BEG and END if it only
  21256. contains commented lines. Otherwise, comment them. If region is
  21257. strictly within a source block, use appropriate comment syntax."
  21258. (if (let ((element (org-element-at-point)))
  21259. (and (eq (org-element-type element) 'src-block)
  21260. (< (save-excursion
  21261. (goto-char (org-element-property :post-affiliated element))
  21262. (line-end-position))
  21263. beg)
  21264. (>= (save-excursion
  21265. (goto-char (org-element-property :end element))
  21266. (skip-chars-backward " \r\t\n")
  21267. (line-beginning-position))
  21268. end)))
  21269. (org-babel-do-in-edit-buffer (call-interactively #'comment-dwim))
  21270. (save-restriction
  21271. ;; Restrict region
  21272. (narrow-to-region (save-excursion (goto-char beg)
  21273. (skip-chars-forward " \r\t\n" end)
  21274. (line-beginning-position))
  21275. (save-excursion (goto-char end)
  21276. (skip-chars-backward " \r\t\n" beg)
  21277. (line-end-position)))
  21278. (let ((uncommentp
  21279. ;; UNCOMMENTP is non-nil when every non blank line between
  21280. ;; BEG and END is a comment.
  21281. (save-excursion
  21282. (goto-char (point-min))
  21283. (while (and (not (eobp))
  21284. (let ((element (org-element-at-point)))
  21285. (and (eq (org-element-type element) 'comment)
  21286. (goto-char (min (point-max)
  21287. (org-element-property
  21288. :end element)))))))
  21289. (eobp))))
  21290. (if uncommentp
  21291. ;; Only blank lines and comments in region: uncomment it.
  21292. (save-excursion
  21293. (goto-char (point-min))
  21294. (while (not (eobp))
  21295. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21296. (replace-match "" nil nil nil 1))
  21297. (forward-line)))
  21298. ;; Comment each line in region.
  21299. (let ((min-indent (point-max)))
  21300. ;; First find the minimum indentation across all lines.
  21301. (save-excursion
  21302. (goto-char (point-min))
  21303. (while (and (not (eobp)) (not (zerop min-indent)))
  21304. (unless (looking-at "[ \t]*$")
  21305. (setq min-indent (min min-indent (current-indentation))))
  21306. (forward-line)))
  21307. ;; Then loop over all lines.
  21308. (save-excursion
  21309. (goto-char (point-min))
  21310. (while (not (eobp))
  21311. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21312. ;; Don't get fooled by invisible text (e.g. link path)
  21313. ;; when moving to column MIN-INDENT.
  21314. (let ((buffer-invisibility-spec nil))
  21315. (org-move-to-column min-indent t))
  21316. (insert comment-start))
  21317. (forward-line)))))))))
  21318. (defun org-comment-dwim (arg)
  21319. (interactive "*P")
  21320. "Call `comment-dwim' within a source edit buffer if needed."
  21321. (if (org-in-src-block-p)
  21322. (org-babel-do-in-edit-buffer (call-interactively #'comment-dwim))
  21323. (call-interactively #'comment-dwim)))
  21324. ;;; Planning
  21325. ;; This section contains tools to operate on timestamp objects, as
  21326. ;; returned by, e.g. `org-element-context'.
  21327. (defun org-timestamp-has-time-p (timestamp)
  21328. "Non-nil when TIMESTAMP has a time specified."
  21329. (org-element-property :hour-start timestamp))
  21330. (defun org-timestamp-format (timestamp format &optional end utc)
  21331. "Format a TIMESTAMP element into a string.
  21332. FORMAT is a format specifier to be passed to
  21333. `format-time-string'.
  21334. When optional argument END is non-nil, use end of date-range or
  21335. time-range, if possible.
  21336. When optional argument UTC is non-nil, time will be expressed as
  21337. Universal Time."
  21338. (format-time-string
  21339. format
  21340. (apply 'encode-time
  21341. (cons 0
  21342. (mapcar
  21343. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21344. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21345. '(:minute-start :hour-start :day-start :month-start
  21346. :year-start)))))
  21347. utc))
  21348. (defun org-timestamp-split-range (timestamp &optional end)
  21349. "Extract a timestamp object from a date or time range.
  21350. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  21351. the end of the range. Otherwise, extract its start.
  21352. Return a new timestamp object sharing the same parent as
  21353. TIMESTAMP."
  21354. (let ((type (org-element-property :type timestamp)))
  21355. (if (memq type '(active inactive diary)) timestamp
  21356. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  21357. ;; Set new type.
  21358. (org-element-put-property
  21359. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21360. ;; Copy start properties over end properties if END is
  21361. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21362. (let ((p-alist '((:minute-start . :minute-end)
  21363. (:hour-start . :hour-end)
  21364. (:day-start . :day-end)
  21365. (:month-start . :month-end)
  21366. (:year-start . :year-end))))
  21367. (dolist (p-cell p-alist)
  21368. (org-element-put-property
  21369. split-ts
  21370. (funcall (if end 'car 'cdr) p-cell)
  21371. (org-element-property
  21372. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  21373. ;; Eventually refresh `:raw-value'.
  21374. (org-element-put-property split-ts :raw-value nil)
  21375. (org-element-put-property
  21376. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21377. (defun org-timestamp-translate (timestamp &optional boundary)
  21378. "Apply `org-translate-time' on a TIMESTAMP object.
  21379. When optional argument BOUNDARY is non-nil, it is either the
  21380. symbol `start' or `end'. In this case, only translate the
  21381. starting or ending part of TIMESTAMP if it is a date or time
  21382. range. Otherwise, translate both parts."
  21383. (if (and (not boundary)
  21384. (memq (org-element-property :type timestamp)
  21385. '(active-range inactive-range)))
  21386. (concat
  21387. (org-translate-time
  21388. (org-element-property :raw-value
  21389. (org-timestamp-split-range timestamp)))
  21390. "--"
  21391. (org-translate-time
  21392. (org-element-property :raw-value
  21393. (org-timestamp-split-range timestamp t))))
  21394. (org-translate-time
  21395. (org-element-property
  21396. :raw-value
  21397. (if (not boundary) timestamp
  21398. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  21399. ;;; Other stuff.
  21400. (defun org-reftex-citation ()
  21401. "Use reftex-citation to insert a citation into the buffer.
  21402. This looks for a line like
  21403. #+BIBLIOGRAPHY: foo plain option:-d
  21404. and derives from it that foo.bib is the bibliography file relevant
  21405. for this document. It then installs the necessary environment for RefTeX
  21406. to work in this buffer and calls `reftex-citation' to insert a citation
  21407. into the buffer.
  21408. Export of such citations to both LaTeX and HTML is handled by the contributed
  21409. package ox-bibtex by Taru Karttunen."
  21410. (interactive)
  21411. (let ((reftex-docstruct-symbol 'rds)
  21412. (reftex-cite-format "\\cite{%l}")
  21413. rds bib)
  21414. (save-excursion
  21415. (save-restriction
  21416. (widen)
  21417. (let ((case-fold-search t)
  21418. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21419. (if (not (save-excursion
  21420. (or (re-search-forward re nil t)
  21421. (re-search-backward re nil t))))
  21422. (user-error "No bibliography defined in file")
  21423. (setq bib (concat (match-string 1) ".bib")
  21424. rds (list (list 'bib bib)))))))
  21425. (call-interactively 'reftex-citation)))
  21426. ;;;; Functions extending outline functionality
  21427. (defun org-beginning-of-line (&optional arg)
  21428. "Go to the beginning of the current line. If that is invisible, continue
  21429. to a visible line beginning. This makes the function of C-a more intuitive.
  21430. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21431. first attempt, and only move to after the tags when the cursor is already
  21432. beyond the end of the headline."
  21433. (interactive "P")
  21434. (let ((pos (point))
  21435. (special (if (consp org-special-ctrl-a/e)
  21436. (car org-special-ctrl-a/e)
  21437. org-special-ctrl-a/e))
  21438. deactivate-mark refpos)
  21439. (if (org-bound-and-true-p visual-line-mode)
  21440. (beginning-of-visual-line 1)
  21441. (beginning-of-line 1))
  21442. (if (and arg (fboundp 'move-beginning-of-line))
  21443. (call-interactively 'move-beginning-of-line)
  21444. (if (bobp)
  21445. nil
  21446. (backward-char 1)
  21447. (if (org-truely-invisible-p)
  21448. (while (and (not (bobp)) (org-truely-invisible-p))
  21449. (backward-char 1)
  21450. (beginning-of-line 1))
  21451. (forward-char 1))))
  21452. (when special
  21453. (cond
  21454. ((and (looking-at org-complex-heading-regexp)
  21455. (= (char-after (match-end 1)) ?\ ))
  21456. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21457. (point-at-eol)))
  21458. (goto-char
  21459. (if (eq special t)
  21460. (cond ((> pos refpos) refpos)
  21461. ((= pos (point)) refpos)
  21462. (t (point)))
  21463. (cond ((> pos (point)) (point))
  21464. ((not (eq last-command this-command)) (point))
  21465. (t refpos)))))
  21466. ((org-at-item-p)
  21467. ;; Being at an item and not looking at an the item means point
  21468. ;; was previously moved to beginning of a visual line, which
  21469. ;; doesn't contain the item. Therefore, do nothing special,
  21470. ;; just stay here.
  21471. (when (looking-at org-list-full-item-re)
  21472. ;; Set special position at first white space character after
  21473. ;; bullet, and check-box, if any.
  21474. (let ((after-bullet
  21475. (let ((box (match-end 3)))
  21476. (if (not box) (match-end 1)
  21477. (let ((after (char-after box)))
  21478. (if (and after (= after ? )) (1+ box) box))))))
  21479. ;; Special case: Move point to special position when
  21480. ;; currently after it or at beginning of line.
  21481. (if (eq special t)
  21482. (when (or (> pos after-bullet) (= (point) pos))
  21483. (goto-char after-bullet))
  21484. ;; Reversed case: Move point to special position when
  21485. ;; point was already at beginning of line and command is
  21486. ;; repeated.
  21487. (when (and (= (point) pos) (eq last-command this-command))
  21488. (goto-char after-bullet))))))))
  21489. (org-no-warnings
  21490. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21491. (setq disable-point-adjustment
  21492. (or (not (invisible-p (point)))
  21493. (not (invisible-p (max (point-min) (1- (point))))))))
  21494. (defun org-end-of-line (&optional arg)
  21495. "Go to the end of the line.
  21496. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21497. tags on the first attempt, and only move to after the tags when
  21498. the cursor is already beyond the end of the headline."
  21499. (interactive "P")
  21500. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21501. org-special-ctrl-a/e))
  21502. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21503. 'end-of-visual-line)
  21504. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21505. (t 'end-of-line)))
  21506. deactivate-mark)
  21507. (if (or (not special) arg) (call-interactively move-fun)
  21508. (let* ((element (save-excursion (beginning-of-line)
  21509. (org-element-at-point)))
  21510. (type (org-element-type element)))
  21511. (cond
  21512. ((memq type '(headline inlinetask))
  21513. (let ((pos (point)))
  21514. (beginning-of-line 1)
  21515. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21516. (if (eq special t)
  21517. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21518. (goto-char (match-beginning 1))
  21519. (goto-char (match-end 0)))
  21520. (if (or (< pos (match-end 0))
  21521. (not (eq this-command last-command)))
  21522. (goto-char (match-end 0))
  21523. (goto-char (match-beginning 1))))
  21524. (call-interactively move-fun))))
  21525. ((outline-invisible-p (line-end-position))
  21526. ;; If element is hidden, `move-end-of-line' would put point
  21527. ;; after it. Use `end-of-line' to stay on current line.
  21528. (call-interactively 'end-of-line))
  21529. (t (call-interactively move-fun)))))
  21530. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21531. (setq disable-point-adjustment
  21532. (or (not (invisible-p (point)))
  21533. (not (invisible-p (max (point-min) (1- (point))))))))
  21534. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21535. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21536. (defun org-backward-sentence (&optional arg)
  21537. "Go to beginning of sentence, or beginning of table field.
  21538. This will call `backward-sentence' or `org-table-beginning-of-field',
  21539. depending on context."
  21540. (interactive "P")
  21541. (cond
  21542. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  21543. (t (call-interactively 'backward-sentence))))
  21544. (defun org-forward-sentence (&optional arg)
  21545. "Go to end of sentence, or end of table field.
  21546. This will call `forward-sentence' or `org-table-end-of-field',
  21547. depending on context."
  21548. (interactive "P")
  21549. (cond
  21550. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  21551. (t (call-interactively 'forward-sentence))))
  21552. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21553. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21554. (defun org-kill-line (&optional arg)
  21555. "Kill line, to tags or end of line."
  21556. (interactive "P")
  21557. (cond
  21558. ((or (not org-special-ctrl-k)
  21559. (bolp)
  21560. (not (org-at-heading-p)))
  21561. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21562. org-ctrl-k-protect-subtree)
  21563. (if (or (eq org-ctrl-k-protect-subtree 'error)
  21564. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  21565. (user-error "C-k aborted as it would kill a hidden subtree")))
  21566. (call-interactively
  21567. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  21568. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  21569. (kill-region (point) (match-beginning 1))
  21570. (org-set-tags nil t))
  21571. (t (kill-region (point) (point-at-eol)))))
  21572. (define-key org-mode-map "\C-k" 'org-kill-line)
  21573. (defun org-yank (&optional arg)
  21574. "Yank. If the kill is a subtree, treat it specially.
  21575. This command will look at the current kill and check if is a single
  21576. subtree, or a series of subtrees[1]. If it passes the test, and if the
  21577. cursor is at the beginning of a line or after the stars of a currently
  21578. empty headline, then the yank is handled specially. How exactly depends
  21579. on the value of the following variables, both set by default.
  21580. org-yank-folded-subtrees
  21581. When set, the subtree(s) will be folded after insertion, but only
  21582. if doing so would now swallow text after the yanked text.
  21583. org-yank-adjusted-subtrees
  21584. When set, the subtree will be promoted or demoted in order to
  21585. fit into the local outline tree structure, which means that the level
  21586. will be adjusted so that it becomes the smaller one of the two
  21587. *visible* surrounding headings.
  21588. Any prefix to this command will cause `yank' to be called directly with
  21589. no special treatment. In particular, a simple \\[universal-argument] prefix \
  21590. will just
  21591. plainly yank the text as it is.
  21592. \[1] The test checks if the first non-white line is a heading
  21593. and if there are no other headings with fewer stars."
  21594. (interactive "P")
  21595. (org-yank-generic 'yank arg))
  21596. (defun org-yank-generic (command arg)
  21597. "Perform some yank-like command.
  21598. This function implements the behavior described in the `org-yank'
  21599. documentation. However, it has been generalized to work for any
  21600. interactive command with similar behavior."
  21601. ;; pretend to be command COMMAND
  21602. (setq this-command command)
  21603. (if arg
  21604. (call-interactively command)
  21605. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  21606. (and (org-kill-is-subtree-p)
  21607. (or (bolp)
  21608. (and (looking-at "[ \t]*$")
  21609. (string-match
  21610. "\\`\\*+\\'"
  21611. (buffer-substring (point-at-bol) (point)))))))
  21612. swallowp)
  21613. (cond
  21614. ((and subtreep org-yank-folded-subtrees)
  21615. (let ((beg (point))
  21616. end)
  21617. (if (and subtreep org-yank-adjusted-subtrees)
  21618. (org-paste-subtree nil nil 'for-yank)
  21619. (call-interactively command))
  21620. (setq end (point))
  21621. (goto-char beg)
  21622. (when (and (bolp) subtreep
  21623. (not (setq swallowp
  21624. (org-yank-folding-would-swallow-text beg end))))
  21625. (org-with-limited-levels
  21626. (or (looking-at org-outline-regexp)
  21627. (re-search-forward org-outline-regexp-bol end t))
  21628. (while (and (< (point) end) (looking-at org-outline-regexp))
  21629. (hide-subtree)
  21630. (org-cycle-show-empty-lines 'folded)
  21631. (condition-case nil
  21632. (outline-forward-same-level 1)
  21633. (error (goto-char end))))))
  21634. (when swallowp
  21635. (message
  21636. "Inserted text not folded because that would swallow text"))
  21637. (goto-char end)
  21638. (skip-chars-forward " \t\n\r")
  21639. (beginning-of-line 1)
  21640. (push-mark beg 'nomsg)))
  21641. ((and subtreep org-yank-adjusted-subtrees)
  21642. (let ((beg (point-at-bol)))
  21643. (org-paste-subtree nil nil 'for-yank)
  21644. (push-mark beg 'nomsg)))
  21645. (t
  21646. (call-interactively command))))))
  21647. (defun org-yank-folding-would-swallow-text (beg end)
  21648. "Would hide-subtree at BEG swallow any text after END?"
  21649. (let (level)
  21650. (org-with-limited-levels
  21651. (save-excursion
  21652. (goto-char beg)
  21653. (when (or (looking-at org-outline-regexp)
  21654. (re-search-forward org-outline-regexp-bol end t))
  21655. (setq level (org-outline-level)))
  21656. (goto-char end)
  21657. (skip-chars-forward " \t\r\n\v\f")
  21658. (if (or (eobp)
  21659. (and (bolp) (looking-at org-outline-regexp)
  21660. (<= (org-outline-level) level)))
  21661. nil ; Nothing would be swallowed
  21662. t))))) ; something would swallow
  21663. (define-key org-mode-map "\C-y" 'org-yank)
  21664. (defun org-truely-invisible-p ()
  21665. "Check if point is at a character currently not visible.
  21666. This version does not only check the character property, but also
  21667. `visible-mode'."
  21668. ;; Early versions of noutline don't have `outline-invisible-p'.
  21669. (if (org-bound-and-true-p visible-mode)
  21670. nil
  21671. (outline-invisible-p)))
  21672. (defun org-invisible-p2 ()
  21673. "Check if point is at a character currently not visible."
  21674. (save-excursion
  21675. (if (and (eolp) (not (bobp))) (backward-char 1))
  21676. ;; Early versions of noutline don't have `outline-invisible-p'.
  21677. (outline-invisible-p)))
  21678. (defun org-back-to-heading (&optional invisible-ok)
  21679. "Call `outline-back-to-heading', but provide a better error message."
  21680. (condition-case nil
  21681. (outline-back-to-heading invisible-ok)
  21682. (error (error "Before first headline at position %d in buffer %s"
  21683. (point) (current-buffer)))))
  21684. (defun org-before-first-heading-p ()
  21685. "Before first heading?"
  21686. (save-excursion
  21687. (end-of-line)
  21688. (null (re-search-backward org-outline-regexp-bol nil t))))
  21689. (defun org-at-heading-p (&optional ignored)
  21690. (outline-on-heading-p t))
  21691. ;; Compatibility alias with Org versions < 7.8.03
  21692. (defalias 'org-on-heading-p 'org-at-heading-p)
  21693. (defun org-in-commented-heading-p (&optional no-inheritance)
  21694. "Non-nil if point is under a commented heading.
  21695. This function also checks ancestors of the current headline,
  21696. unless optional argument NO-INHERITANCE is non-nil."
  21697. (cond
  21698. ((org-before-first-heading-p) nil)
  21699. ((let ((headline (nth 4 (org-heading-components))))
  21700. (and headline
  21701. (let ((case-fold-search nil))
  21702. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  21703. headline)))))
  21704. (no-inheritance nil)
  21705. (t
  21706. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  21707. (defun org-at-comment-p nil
  21708. "Is cursor in a commented line?"
  21709. (save-excursion
  21710. (save-match-data
  21711. (beginning-of-line)
  21712. (looking-at "^[ \t]*# "))))
  21713. (defun org-at-drawer-p nil
  21714. "Is cursor at a drawer keyword?"
  21715. (save-excursion
  21716. (move-beginning-of-line 1)
  21717. (looking-at org-drawer-regexp)))
  21718. (defun org-at-block-p nil
  21719. "Is cursor at a block keyword?"
  21720. (save-excursion
  21721. (move-beginning-of-line 1)
  21722. (looking-at org-block-regexp)))
  21723. (defun org-point-at-end-of-empty-headline ()
  21724. "If point is at the end of an empty headline, return t, else nil.
  21725. If the heading only contains a TODO keyword, it is still still considered
  21726. empty."
  21727. (and (looking-at "[ \t]*$")
  21728. (when org-todo-line-regexp
  21729. (save-excursion
  21730. (beginning-of-line 1)
  21731. (let ((case-fold-search nil))
  21732. (looking-at org-todo-line-regexp)
  21733. (string= (match-string 3) ""))))))
  21734. (defun org-at-heading-or-item-p ()
  21735. (or (org-at-heading-p) (org-at-item-p)))
  21736. (defun org-at-target-p ()
  21737. (or (org-in-regexp org-radio-target-regexp)
  21738. (org-in-regexp org-target-regexp)))
  21739. ;; Compatibility alias with Org versions < 7.8.03
  21740. (defalias 'org-on-target-p 'org-at-target-p)
  21741. (defun org-up-heading-all (arg)
  21742. "Move to the heading line of which the present line is a subheading.
  21743. This function considers both visible and invisible heading lines.
  21744. With argument, move up ARG levels."
  21745. (if (fboundp 'outline-up-heading-all)
  21746. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21747. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21748. (defun org-up-heading-safe ()
  21749. "Move to the heading line of which the present line is a subheading.
  21750. This version will not throw an error. It will return the level of the
  21751. headline found, or nil if no higher level is found.
  21752. Also, this function will be a lot faster than `outline-up-heading',
  21753. because it relies on stars being the outline starters. This can really
  21754. make a significant difference in outlines with very many siblings."
  21755. (let (start-level re)
  21756. (org-back-to-heading t)
  21757. (setq start-level (funcall outline-level))
  21758. (if (equal start-level 1)
  21759. nil
  21760. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21761. (if (re-search-backward re nil t)
  21762. (funcall outline-level)))))
  21763. (defun org-first-sibling-p ()
  21764. "Is this heading the first child of its parents?"
  21765. (interactive)
  21766. (let ((re org-outline-regexp-bol)
  21767. level l)
  21768. (unless (org-at-heading-p t)
  21769. (user-error "Not at a heading"))
  21770. (setq level (funcall outline-level))
  21771. (save-excursion
  21772. (if (not (re-search-backward re nil t))
  21773. t
  21774. (setq l (funcall outline-level))
  21775. (< l level)))))
  21776. (defun org-goto-sibling (&optional previous)
  21777. "Goto the next sibling, even if it is invisible.
  21778. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21779. when a sibling was found. When none is found, return nil and don't
  21780. move point."
  21781. (let ((fun (if previous 're-search-backward 're-search-forward))
  21782. (pos (point))
  21783. (re org-outline-regexp-bol)
  21784. level l)
  21785. (when (condition-case nil (org-back-to-heading t) (error nil))
  21786. (setq level (funcall outline-level))
  21787. (catch 'exit
  21788. (or previous (forward-char 1))
  21789. (while (funcall fun re nil t)
  21790. (setq l (funcall outline-level))
  21791. (when (< l level) (goto-char pos) (throw 'exit nil))
  21792. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21793. (goto-char pos)
  21794. nil))))
  21795. (defun org-show-siblings ()
  21796. "Show all siblings of the current headline."
  21797. (save-excursion
  21798. (while (org-goto-sibling) (org-flag-heading nil)))
  21799. (save-excursion
  21800. (while (org-goto-sibling 'previous)
  21801. (org-flag-heading nil))))
  21802. (defun org-goto-first-child ()
  21803. "Goto the first child, even if it is invisible.
  21804. Return t when a child was found. Otherwise don't move point and
  21805. return nil."
  21806. (let (level (pos (point)) (re org-outline-regexp-bol))
  21807. (when (condition-case nil (org-back-to-heading t) (error nil))
  21808. (setq level (outline-level))
  21809. (forward-char 1)
  21810. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21811. (progn (goto-char (match-beginning 0)) t)
  21812. (goto-char pos) nil))))
  21813. (defun org-show-hidden-entry ()
  21814. "Show an entry where even the heading is hidden."
  21815. (save-excursion
  21816. (org-show-entry)))
  21817. (defun org-flag-heading (flag &optional entry)
  21818. "Flag the current heading. FLAG non-nil means make invisible.
  21819. When ENTRY is non-nil, show the entire entry."
  21820. (save-excursion
  21821. (org-back-to-heading t)
  21822. ;; Check if we should show the entire entry
  21823. (if entry
  21824. (progn
  21825. (org-show-entry)
  21826. (save-excursion
  21827. (and (outline-next-heading)
  21828. (org-flag-heading nil))))
  21829. (outline-flag-region (max (point-min) (1- (point)))
  21830. (save-excursion (outline-end-of-heading) (point))
  21831. flag))))
  21832. (defun org-get-next-sibling ()
  21833. "Move to next heading of the same level, and return point.
  21834. If there is no such heading, return nil.
  21835. This is like outline-next-sibling, but invisible headings are ok."
  21836. (let ((level (funcall outline-level)))
  21837. (outline-next-heading)
  21838. (while (and (not (eobp)) (> (funcall outline-level) level))
  21839. (outline-next-heading))
  21840. (if (or (eobp) (< (funcall outline-level) level))
  21841. nil
  21842. (point))))
  21843. (defun org-get-last-sibling ()
  21844. "Move to previous heading of the same level, and return point.
  21845. If there is no such heading, return nil."
  21846. (let ((opoint (point))
  21847. (level (funcall outline-level)))
  21848. (outline-previous-heading)
  21849. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21850. (while (and (> (funcall outline-level) level)
  21851. (not (bobp)))
  21852. (outline-previous-heading))
  21853. (if (< (funcall outline-level) level)
  21854. nil
  21855. (point)))))
  21856. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21857. "Goto to the end of a subtree."
  21858. ;; This contains an exact copy of the original function, but it uses
  21859. ;; `org-back-to-heading', to make it work also in invisible
  21860. ;; trees. And is uses an invisible-ok argument.
  21861. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21862. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21863. ;; with many children and grandchildren etc, this can be much faster
  21864. ;; than the outline version.
  21865. (org-back-to-heading invisible-ok)
  21866. (let ((first t)
  21867. (level (funcall outline-level)))
  21868. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21869. ;; A true heading (not a plain list item), in Org-mode
  21870. ;; This means we can easily find the end by looking
  21871. ;; only for the right number of stars. Using a regexp to do
  21872. ;; this is so much faster than using a Lisp loop.
  21873. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21874. (forward-char 1)
  21875. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21876. ;; something else, do it the slow way
  21877. (while (and (not (eobp))
  21878. (or first (> (funcall outline-level) level)))
  21879. (setq first nil)
  21880. (outline-next-heading)))
  21881. (unless to-heading
  21882. (if (memq (preceding-char) '(?\n ?\^M))
  21883. (progn
  21884. ;; Go to end of line before heading
  21885. (forward-char -1)
  21886. (if (memq (preceding-char) '(?\n ?\^M))
  21887. ;; leave blank line before heading
  21888. (forward-char -1))))))
  21889. (point))
  21890. (defun org-end-of-meta-data-and-drawers ()
  21891. "Jump to the first text after meta data and drawers in the current entry.
  21892. This will move over empty lines, lines with planning time stamps,
  21893. clocking lines, and drawers."
  21894. (org-back-to-heading t)
  21895. (let ((end (save-excursion (outline-next-heading) (point)))
  21896. (re (concat "\\(" org-drawer-regexp "\\)"
  21897. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21898. (forward-line 1)
  21899. (while (re-search-forward re end t)
  21900. (if (not (match-end 1))
  21901. ;; empty or planning line
  21902. (forward-line 1)
  21903. ;; a drawer, find the end
  21904. (re-search-forward "^[ \t]*:END:" end 'move)
  21905. (forward-line 1)))
  21906. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21907. (point)))
  21908. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21909. "Move forward to the ARG'th subheading at same level as this one.
  21910. Stop at the first and last subheadings of a superior heading.
  21911. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21912. non-nil it will also look at invisible ones."
  21913. (interactive "p")
  21914. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21915. (if (and arg (< arg 0))
  21916. (goto-char (point-min))
  21917. (outline-next-heading))
  21918. (org-at-heading-p)
  21919. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21920. (f (if (and arg (< arg 0))
  21921. 're-search-backward
  21922. 're-search-forward))
  21923. (count (if arg (abs arg) 1))
  21924. (result (point)))
  21925. (while (and (prog1 (> count 0)
  21926. (forward-char (if (and arg (< arg 0)) -1 1)))
  21927. (funcall f org-outline-regexp-bol nil 'move))
  21928. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21929. (cond ((< l level) (setq count 0))
  21930. ((and (= l level)
  21931. (or invisible-ok
  21932. (progn
  21933. (goto-char (line-beginning-position))
  21934. (not (outline-invisible-p)))))
  21935. (setq count (1- count))
  21936. (when (eq l level)
  21937. (setq result (point)))))))
  21938. (goto-char result))
  21939. (beginning-of-line 1)))
  21940. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21941. "Move backward to the ARG'th subheading at same level as this one.
  21942. Stop at the first and last subheadings of a superior heading."
  21943. (interactive "p")
  21944. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21945. (defun org-next-block (arg &optional backward block-regexp)
  21946. "Jump to the next block.
  21947. With a prefix argument ARG, jump forward ARG many source blocks.
  21948. When BACKWARD is non-nil, jump to the previous block.
  21949. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21950. (interactive "p")
  21951. (let ((re (or block-regexp org-block-regexp))
  21952. (re-search-fn (or (and backward 're-search-backward)
  21953. 're-search-forward)))
  21954. (if (looking-at re) (forward-char 1))
  21955. (condition-case nil
  21956. (funcall re-search-fn re nil nil arg)
  21957. (error (user-error "No %s code blocks"
  21958. (if backward "previous" "further" ))))
  21959. (goto-char (match-beginning 0)) (org-show-context)))
  21960. (defun org-previous-block (arg &optional block-regexp)
  21961. "Jump to the previous block.
  21962. With a prefix argument ARG, jump backward ARG many source blocks.
  21963. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21964. (interactive "p")
  21965. (org-next-block arg t block-regexp))
  21966. (defun org-forward-paragraph ()
  21967. "Move forward to beginning of next paragraph or equivalent.
  21968. The function moves point to the beginning of the next visible
  21969. structural element, which can be a paragraph, a table, a list
  21970. item, etc. It also provides some special moves for convenience:
  21971. - On an affiliated keyword, jump to the beginning of the
  21972. relative element.
  21973. - On an item or a footnote definition, move to the second
  21974. element inside, if any.
  21975. - On a table or a property drawer, jump after it.
  21976. - On a verse or source block, stop after blank lines."
  21977. (interactive)
  21978. (when (eobp) (user-error "Cannot move further down"))
  21979. (let* ((deactivate-mark nil)
  21980. (element (org-element-at-point))
  21981. (type (org-element-type element))
  21982. (post-affiliated (org-element-property :post-affiliated element))
  21983. (contents-begin (org-element-property :contents-begin element))
  21984. (contents-end (org-element-property :contents-end element))
  21985. (end (let ((end (org-element-property :end element)) (parent element))
  21986. (while (and (setq parent (org-element-property :parent parent))
  21987. (= (org-element-property :contents-end parent) end))
  21988. (setq end (org-element-property :end parent)))
  21989. end)))
  21990. (cond ((not element)
  21991. (skip-chars-forward " \r\t\n")
  21992. (or (eobp) (beginning-of-line)))
  21993. ;; On affiliated keywords, move to element's beginning.
  21994. ((and post-affiliated (< (point) post-affiliated))
  21995. (goto-char post-affiliated))
  21996. ;; At a table row, move to the end of the table. Similarly,
  21997. ;; at a node property, move to the end of the property
  21998. ;; drawer.
  21999. ((memq type '(node-property table-row))
  22000. (goto-char (org-element-property
  22001. :end (org-element-property :parent element))))
  22002. ((memq type '(property-drawer table)) (goto-char end))
  22003. ;; Consider blank lines as separators in verse and source
  22004. ;; blocks to ease editing.
  22005. ((memq type '(src-block verse-block))
  22006. (when (eq type 'src-block)
  22007. (setq contents-end
  22008. (save-excursion (goto-char end)
  22009. (skip-chars-backward " \r\t\n")
  22010. (line-beginning-position))))
  22011. (beginning-of-line)
  22012. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22013. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22014. (goto-char end)
  22015. (skip-chars-forward " \r\t\n")
  22016. (if (= (point) contents-end) (goto-char end)
  22017. (beginning-of-line))))
  22018. ;; With no contents, just skip element.
  22019. ((not contents-begin) (goto-char end))
  22020. ;; If contents are invisible, skip the element altogether.
  22021. ((outline-invisible-p (line-end-position))
  22022. (case type
  22023. (headline
  22024. (org-with-limited-levels (outline-next-visible-heading 1)))
  22025. ;; At a plain list, make sure we move to the next item
  22026. ;; instead of skipping the whole list.
  22027. (plain-list (forward-char)
  22028. (org-forward-paragraph))
  22029. (otherwise (goto-char end))))
  22030. ((>= (point) contents-end) (goto-char end))
  22031. ((>= (point) contents-begin)
  22032. ;; This can only happen on paragraphs and plain lists.
  22033. (case type
  22034. (paragraph (goto-char end))
  22035. ;; At a plain list, try to move to second element in
  22036. ;; first item, if possible.
  22037. (plain-list (end-of-line)
  22038. (org-forward-paragraph))))
  22039. ;; When contents start on the middle of a line (e.g. in
  22040. ;; items and footnote definitions), try to reach first
  22041. ;; element starting after current line.
  22042. ((> (line-end-position) contents-begin)
  22043. (end-of-line)
  22044. (org-forward-paragraph))
  22045. (t (goto-char contents-begin)))))
  22046. (defun org-backward-paragraph ()
  22047. "Move backward to start of previous paragraph or equivalent.
  22048. The function moves point to the beginning of the current
  22049. structural element, which can be a paragraph, a table, a list
  22050. item, etc., or to the beginning of the previous visible one if
  22051. point is already there. It also provides some special moves for
  22052. convenience:
  22053. - On an affiliated keyword, jump to the first one.
  22054. - On a table or a property drawer, move to its beginning.
  22055. - On a verse or source block, stop before blank lines."
  22056. (interactive)
  22057. (when (bobp) (user-error "Cannot move further up"))
  22058. (let* ((deactivate-mark nil)
  22059. (element (org-element-at-point))
  22060. (type (org-element-type element))
  22061. (contents-begin (org-element-property :contents-begin element))
  22062. (contents-end (org-element-property :contents-end element))
  22063. (post-affiliated (org-element-property :post-affiliated element))
  22064. (begin (org-element-property :begin element)))
  22065. (cond
  22066. ((not element) (goto-char (point-min)))
  22067. ((= (point) begin)
  22068. (backward-char)
  22069. (org-backward-paragraph))
  22070. ((and post-affiliated (<= (point) post-affiliated)) (goto-char begin))
  22071. ((memq type '(node-property table-row))
  22072. (goto-char (org-element-property
  22073. :post-affiliated (org-element-property :parent element))))
  22074. ((memq type '(property-drawer table)) (goto-char begin))
  22075. ((memq type '(src-block verse-block))
  22076. (when (eq type 'src-block)
  22077. (setq contents-begin
  22078. (save-excursion (goto-char begin) (forward-line) (point))))
  22079. (if (= (point) contents-begin) (goto-char post-affiliated)
  22080. ;; Inside a verse block, see blank lines as paragraph
  22081. ;; separators.
  22082. (let ((origin (point)))
  22083. (skip-chars-backward " \r\t\n" contents-begin)
  22084. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22085. (skip-chars-forward " \r\t\n" origin)
  22086. (if (= (point) origin) (goto-char contents-begin)
  22087. (beginning-of-line))))))
  22088. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22089. ((eq type 'paragraph)
  22090. (goto-char contents-begin)
  22091. ;; When at first paragraph in an item or a footnote definition,
  22092. ;; move directly to beginning of line.
  22093. (let ((parent-contents
  22094. (org-element-property
  22095. :contents-begin (org-element-property :parent element))))
  22096. (when (and parent-contents (= parent-contents contents-begin))
  22097. (beginning-of-line))))
  22098. ;; At the end of a greater element, move to the beginning of the
  22099. ;; last element within.
  22100. ((>= (point) contents-end)
  22101. (goto-char (1- contents-end))
  22102. (org-backward-paragraph))
  22103. (t (goto-char (or post-affiliated begin))))
  22104. ;; Ensure we never leave point invisible.
  22105. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22106. (defun org-forward-element ()
  22107. "Move forward by one element.
  22108. Move to the next element at the same level, when possible."
  22109. (interactive)
  22110. (cond ((eobp) (user-error "Cannot move further down"))
  22111. ((org-with-limited-levels (org-at-heading-p))
  22112. (let ((origin (point)))
  22113. (goto-char (org-end-of-subtree nil t))
  22114. (unless (org-with-limited-levels (org-at-heading-p))
  22115. (goto-char origin)
  22116. (user-error "Cannot move further down"))))
  22117. (t
  22118. (let* ((elem (org-element-at-point))
  22119. (end (org-element-property :end elem))
  22120. (parent (org-element-property :parent elem)))
  22121. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22122. (goto-char (org-element-property :end parent)))
  22123. ((integer-or-marker-p end) (goto-char end))
  22124. (t (message "No element at point")))))))
  22125. (defun org-backward-element ()
  22126. "Move backward by one element.
  22127. Move to the previous element at the same level, when possible."
  22128. (interactive)
  22129. (cond ((bobp) (user-error "Cannot move further up"))
  22130. ((org-with-limited-levels (org-at-heading-p))
  22131. ;; At a headline, move to the previous one, if any, or stay
  22132. ;; here.
  22133. (let ((origin (point)))
  22134. (org-with-limited-levels (org-backward-heading-same-level 1))
  22135. ;; When current headline has no sibling above, move to its
  22136. ;; parent.
  22137. (when (= (point) origin)
  22138. (or (org-with-limited-levels (org-up-heading-safe))
  22139. (progn (goto-char origin)
  22140. (user-error "Cannot move further up"))))))
  22141. (t
  22142. (let* ((elem (org-element-at-point))
  22143. (beg (org-element-property :begin elem)))
  22144. (cond
  22145. ;; Move to beginning of current element if point isn't
  22146. ;; there already.
  22147. ((null beg) (message "No element at point"))
  22148. ((/= (point) beg) (goto-char beg))
  22149. (t (goto-char beg)
  22150. (skip-chars-backward " \r\t\n")
  22151. (unless (bobp)
  22152. (let ((prev (org-element-at-point)))
  22153. (goto-char (org-element-property :begin prev))
  22154. (while (and (setq prev (org-element-property :parent prev))
  22155. (<= (org-element-property :end prev) beg))
  22156. (goto-char (org-element-property :begin prev)))))))))))
  22157. (defun org-up-element ()
  22158. "Move to upper element."
  22159. (interactive)
  22160. (if (org-with-limited-levels (org-at-heading-p))
  22161. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22162. (let* ((elem (org-element-at-point))
  22163. (parent (org-element-property :parent elem)))
  22164. (if parent (goto-char (org-element-property :begin parent))
  22165. (if (org-with-limited-levels (org-before-first-heading-p))
  22166. (user-error "No surrounding element")
  22167. (org-with-limited-levels (org-back-to-heading)))))))
  22168. (defvar org-element-greater-elements)
  22169. (defun org-down-element ()
  22170. "Move to inner element."
  22171. (interactive)
  22172. (let ((element (org-element-at-point)))
  22173. (cond
  22174. ((memq (org-element-type element) '(plain-list table))
  22175. (goto-char (org-element-property :contents-begin element))
  22176. (forward-char))
  22177. ((memq (org-element-type element) org-element-greater-elements)
  22178. ;; If contents are hidden, first disclose them.
  22179. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22180. (goto-char (or (org-element-property :contents-begin element)
  22181. (user-error "No content for this element"))))
  22182. (t (user-error "No inner element")))))
  22183. (defun org-drag-element-backward ()
  22184. "Move backward element at point."
  22185. (interactive)
  22186. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22187. (let* ((elem (org-element-at-point))
  22188. (prev-elem
  22189. (save-excursion
  22190. (goto-char (org-element-property :begin elem))
  22191. (skip-chars-backward " \r\t\n")
  22192. (unless (bobp)
  22193. (let* ((beg (org-element-property :begin elem))
  22194. (prev (org-element-at-point))
  22195. (up prev))
  22196. (while (and (setq up (org-element-property :parent up))
  22197. (<= (org-element-property :end prev) beg))
  22198. (setq prev up))
  22199. prev)))))
  22200. ;; Error out if no previous element or previous element is
  22201. ;; a parent of the current one.
  22202. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22203. (user-error "Cannot drag element backward")
  22204. (let ((pos (point)))
  22205. (org-element-swap-A-B prev-elem elem)
  22206. (goto-char (+ (org-element-property :begin prev-elem)
  22207. (- pos (org-element-property :begin elem)))))))))
  22208. (defun org-drag-element-forward ()
  22209. "Move forward element at point."
  22210. (interactive)
  22211. (let* ((pos (point))
  22212. (elem (org-element-at-point)))
  22213. (when (= (point-max) (org-element-property :end elem))
  22214. (user-error "Cannot drag element forward"))
  22215. (goto-char (org-element-property :end elem))
  22216. (let ((next-elem (org-element-at-point)))
  22217. (when (or (org-element-nested-p elem next-elem)
  22218. (and (eq (org-element-type next-elem) 'headline)
  22219. (not (eq (org-element-type elem) 'headline))))
  22220. (goto-char pos)
  22221. (user-error "Cannot drag element forward"))
  22222. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22223. ;; body's length (without final blanks) and by the length of
  22224. ;; blanks between ELEM and NEXT-ELEM.
  22225. (let ((size-next (- (save-excursion
  22226. (goto-char (org-element-property :end next-elem))
  22227. (skip-chars-backward " \r\t\n")
  22228. (forward-line)
  22229. ;; Small correction if buffer doesn't end
  22230. ;; with a newline character.
  22231. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22232. (org-element-property :begin next-elem)))
  22233. (size-blank (- (org-element-property :end elem)
  22234. (save-excursion
  22235. (goto-char (org-element-property :end elem))
  22236. (skip-chars-backward " \r\t\n")
  22237. (forward-line)
  22238. (point)))))
  22239. (org-element-swap-A-B elem next-elem)
  22240. (goto-char (+ pos size-next size-blank))))))
  22241. (defun org-drag-line-forward (arg)
  22242. "Drag the line at point ARG lines forward."
  22243. (interactive "p")
  22244. (dotimes (n (abs arg))
  22245. (let ((c (current-column)))
  22246. (if (< 0 arg)
  22247. (progn
  22248. (beginning-of-line 2)
  22249. (transpose-lines 1)
  22250. (beginning-of-line 0))
  22251. (transpose-lines 1)
  22252. (beginning-of-line -1))
  22253. (org-move-to-column c))))
  22254. (defun org-drag-line-backward (arg)
  22255. "Drag the line at point ARG lines backward."
  22256. (interactive "p")
  22257. (org-drag-line-forward (- arg)))
  22258. (defun org-mark-element ()
  22259. "Put point at beginning of this element, mark at end.
  22260. Interactively, if this command is repeated or (in Transient Mark
  22261. mode) if the mark is active, it marks the next element after the
  22262. ones already marked."
  22263. (interactive)
  22264. (let (deactivate-mark)
  22265. (if (and (org-called-interactively-p 'any)
  22266. (or (and (eq last-command this-command) (mark t))
  22267. (and transient-mark-mode mark-active)))
  22268. (set-mark
  22269. (save-excursion
  22270. (goto-char (mark))
  22271. (goto-char (org-element-property :end (org-element-at-point)))))
  22272. (let ((element (org-element-at-point)))
  22273. (end-of-line)
  22274. (push-mark (org-element-property :end element) t t)
  22275. (goto-char (org-element-property :begin element))))))
  22276. (defun org-narrow-to-element ()
  22277. "Narrow buffer to current element."
  22278. (interactive)
  22279. (let ((elem (org-element-at-point)))
  22280. (cond
  22281. ((eq (car elem) 'headline)
  22282. (narrow-to-region
  22283. (org-element-property :begin elem)
  22284. (org-element-property :end elem)))
  22285. ((memq (car elem) org-element-greater-elements)
  22286. (narrow-to-region
  22287. (org-element-property :contents-begin elem)
  22288. (org-element-property :contents-end elem)))
  22289. (t
  22290. (narrow-to-region
  22291. (org-element-property :begin elem)
  22292. (org-element-property :end elem))))))
  22293. (defun org-transpose-element ()
  22294. "Transpose current and previous elements, keeping blank lines between.
  22295. Point is moved after both elements."
  22296. (interactive)
  22297. (org-skip-whitespace)
  22298. (let ((end (org-element-property :end (org-element-at-point))))
  22299. (org-drag-element-backward)
  22300. (goto-char end)))
  22301. (defun org-unindent-buffer ()
  22302. "Un-indent the visible part of the buffer.
  22303. Relative indentation (between items, inside blocks, etc.) isn't
  22304. modified."
  22305. (interactive)
  22306. (unless (eq major-mode 'org-mode)
  22307. (user-error "Cannot un-indent a buffer not in Org mode"))
  22308. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  22309. unindent-tree ; For byte-compiler.
  22310. (unindent-tree
  22311. (function
  22312. (lambda (contents)
  22313. (mapc
  22314. (lambda (element)
  22315. (if (memq (org-element-type element) '(headline section))
  22316. (funcall unindent-tree (org-element-contents element))
  22317. (save-excursion
  22318. (save-restriction
  22319. (narrow-to-region
  22320. (org-element-property :begin element)
  22321. (org-element-property :end element))
  22322. (org-do-remove-indentation)))))
  22323. (reverse contents))))))
  22324. (funcall unindent-tree (org-element-contents parse-tree))))
  22325. (defun org-show-subtree ()
  22326. "Show everything after this heading at deeper levels."
  22327. (interactive)
  22328. (outline-flag-region
  22329. (point)
  22330. (save-excursion
  22331. (org-end-of-subtree t t))
  22332. nil))
  22333. (defun org-show-entry ()
  22334. "Show the body directly following this heading.
  22335. Show the heading too, if it is currently invisible."
  22336. (interactive)
  22337. (save-excursion
  22338. (condition-case nil
  22339. (progn
  22340. (org-back-to-heading t)
  22341. (outline-flag-region
  22342. (max (point-min) (1- (point)))
  22343. (save-excursion
  22344. (if (re-search-forward
  22345. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22346. (match-beginning 1)
  22347. (point-max)))
  22348. nil)
  22349. (org-cycle-hide-drawers 'children))
  22350. (error nil))))
  22351. (defun org-make-options-regexp (kwds &optional extra)
  22352. "Make a regular expression for keyword lines."
  22353. (concat
  22354. "^[ \t]*#\\+\\("
  22355. (mapconcat 'regexp-quote kwds "\\|")
  22356. (if extra (concat "\\|" extra))
  22357. "\\):[ \t]*\\(.*\\)"))
  22358. ;; Make isearch reveal the necessary context
  22359. (defun org-isearch-end ()
  22360. "Reveal context after isearch exits."
  22361. (when isearch-success ; only if search was successful
  22362. (if (featurep 'xemacs)
  22363. ;; Under XEmacs, the hook is run in the correct place,
  22364. ;; we directly show the context.
  22365. (org-show-context 'isearch)
  22366. ;; In Emacs the hook runs *before* restoring the overlays.
  22367. ;; So we have to use a one-time post-command-hook to do this.
  22368. ;; (Emacs 22 has a special variable, see function `org-mode')
  22369. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22370. isearch-mode-end-hook-quit)
  22371. ;; Only when the isearch was not quitted.
  22372. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22373. 'append 'local)))
  22374. (org-fix-ellipsis-at-bol)))
  22375. (defun org-isearch-post-command ()
  22376. "Remove self from hook, and show context."
  22377. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22378. (org-show-context 'isearch))
  22379. ;;;; Integration with and fixes for other packages
  22380. ;;; Imenu support
  22381. (defvar org-imenu-markers nil
  22382. "All markers currently used by Imenu.")
  22383. (make-variable-buffer-local 'org-imenu-markers)
  22384. (defun org-imenu-new-marker (&optional pos)
  22385. "Return a new marker for use by Imenu, and remember the marker."
  22386. (let ((m (make-marker)))
  22387. (move-marker m (or pos (point)))
  22388. (push m org-imenu-markers)
  22389. m))
  22390. (defun org-imenu-get-tree ()
  22391. "Produce the index for Imenu."
  22392. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  22393. (setq org-imenu-markers nil)
  22394. (let* ((n org-imenu-depth)
  22395. (re (concat "^" (org-get-limited-outline-regexp)))
  22396. (subs (make-vector (1+ n) nil))
  22397. (last-level 0)
  22398. m level head0 head)
  22399. (save-excursion
  22400. (save-restriction
  22401. (widen)
  22402. (goto-char (point-max))
  22403. (while (re-search-backward re nil t)
  22404. (setq level (org-reduced-level (funcall outline-level)))
  22405. (when (and (<= level n)
  22406. (looking-at org-complex-heading-regexp)
  22407. (setq head0 (org-match-string-no-properties 4)))
  22408. (setq head (org-link-display-format head0)
  22409. m (org-imenu-new-marker))
  22410. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22411. (if (>= level last-level)
  22412. (push (cons head m) (aref subs level))
  22413. (push (cons head (aref subs (1+ level))) (aref subs level))
  22414. (loop for i from (1+ level) to n do (aset subs i nil)))
  22415. (setq last-level level)))))
  22416. (aref subs 1)))
  22417. (eval-after-load "imenu"
  22418. '(progn
  22419. (add-hook 'imenu-after-jump-hook
  22420. (lambda ()
  22421. (if (derived-mode-p 'org-mode)
  22422. (org-show-context 'org-goto))))))
  22423. (defun org-link-display-format (link)
  22424. "Replace a link with its the description.
  22425. If there is no description, use the link target."
  22426. (save-match-data
  22427. (if (string-match org-bracket-link-analytic-regexp link)
  22428. (replace-match (if (match-end 5)
  22429. (match-string 5 link)
  22430. (concat (match-string 1 link)
  22431. (match-string 3 link)))
  22432. nil t link)
  22433. link)))
  22434. (defun org-toggle-link-display ()
  22435. "Toggle the literal or descriptive display of links."
  22436. (interactive)
  22437. (if org-descriptive-links
  22438. (progn (org-remove-from-invisibility-spec '(org-link))
  22439. (org-restart-font-lock)
  22440. (setq org-descriptive-links nil))
  22441. (progn (add-to-invisibility-spec '(org-link))
  22442. (org-restart-font-lock)
  22443. (setq org-descriptive-links t))))
  22444. ;; Speedbar support
  22445. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22446. "Overlay marking the agenda restriction line in speedbar.")
  22447. (overlay-put org-speedbar-restriction-lock-overlay
  22448. 'face 'org-agenda-restriction-lock)
  22449. (overlay-put org-speedbar-restriction-lock-overlay
  22450. 'help-echo "Agendas are currently limited to this item.")
  22451. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  22452. (defun org-speedbar-set-agenda-restriction ()
  22453. "Restrict future agenda commands to the location at point in speedbar.
  22454. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  22455. (interactive)
  22456. (require 'org-agenda)
  22457. (let (p m tp np dir txt)
  22458. (cond
  22459. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22460. 'org-imenu t))
  22461. (setq m (get-text-property p 'org-imenu-marker))
  22462. (with-current-buffer (marker-buffer m)
  22463. (goto-char m)
  22464. (org-agenda-set-restriction-lock 'subtree)))
  22465. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22466. 'speedbar-function 'speedbar-find-file))
  22467. (setq tp (previous-single-property-change
  22468. (1+ p) 'speedbar-function)
  22469. np (next-single-property-change
  22470. tp 'speedbar-function)
  22471. dir (speedbar-line-directory)
  22472. txt (buffer-substring-no-properties (or tp (point-min))
  22473. (or np (point-max))))
  22474. (with-current-buffer (find-file-noselect
  22475. (let ((default-directory dir))
  22476. (expand-file-name txt)))
  22477. (unless (derived-mode-p 'org-mode)
  22478. (user-error "Cannot restrict to non-Org-mode file"))
  22479. (org-agenda-set-restriction-lock 'file)))
  22480. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  22481. (move-overlay org-speedbar-restriction-lock-overlay
  22482. (point-at-bol) (point-at-eol))
  22483. (setq current-prefix-arg nil)
  22484. (org-agenda-maybe-redo)))
  22485. (defvar speedbar-file-key-map)
  22486. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  22487. (eval-after-load "speedbar"
  22488. '(progn
  22489. (speedbar-add-supported-extension ".org")
  22490. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  22491. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  22492. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  22493. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  22494. (add-hook 'speedbar-visiting-tag-hook
  22495. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  22496. ;;; Fixes and Hacks for problems with other packages
  22497. (defun org-mode-flyspell-verify ()
  22498. "Function used for `flyspell-generic-check-word-predicate'."
  22499. (if (org-at-heading-p)
  22500. ;; At a headline or an inlinetask, check title only. This is
  22501. ;; faster than relying on `org-element-at-point'.
  22502. (and (save-excursion (beginning-of-line)
  22503. (and (let ((case-fold-search t))
  22504. (not (looking-at "\\*+ END[ \t]*$")))
  22505. (looking-at org-complex-heading-regexp)))
  22506. (match-beginning 4)
  22507. (>= (point) (match-beginning 4))
  22508. (or (not (match-beginning 5))
  22509. (< (point) (match-beginning 5))))
  22510. (let* ((element (org-element-at-point))
  22511. (post-affiliated (org-element-property :post-affiliated element))
  22512. (object-check
  22513. (function
  22514. ;; Non-nil if checks can be done for object at point.
  22515. (lambda ()
  22516. (let ((object (save-excursion
  22517. (when (org-looking-at-p "\\>") (backward-char))
  22518. (org-element-context element))))
  22519. (case (org-element-type object)
  22520. ;; Prevent checks in links due to keybinding conflict
  22521. ;; with Flyspell.
  22522. ((code entity export-snippet inline-babel-call
  22523. inline-src-block line-break latex-fragment link macro
  22524. statistics-cookie target timestamp verbatim)
  22525. nil)
  22526. (footnote-reference
  22527. ;; Only in inline footnotes, within the definition.
  22528. (and (eq (org-element-property :type object) 'inline)
  22529. (< (save-excursion
  22530. (goto-char (org-element-property :begin object))
  22531. (search-forward ":" nil t 2))
  22532. (point))))
  22533. (otherwise t)))))))
  22534. (cond
  22535. ;; Ignore checks in all affiliated keywords but captions.
  22536. ((and post-affiliated (< (point) post-affiliated))
  22537. (and (save-excursion
  22538. (beginning-of-line)
  22539. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  22540. (> (point) (match-end 0))
  22541. (funcall object-check)))
  22542. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  22543. ((and org-log-into-drawer
  22544. (let ((log (or (org-string-nw-p org-log-into-drawer) "LOGBOOK"))
  22545. (parent element))
  22546. (while (and parent (not (eq (org-element-type parent) 'drawer)))
  22547. (setq parent (org-element-property :parent parent)))
  22548. (and parent
  22549. (eq (compare-strings
  22550. log nil nil
  22551. (org-element-property :drawer-name parent) nil nil t)
  22552. t))))
  22553. nil)
  22554. (t
  22555. (case (org-element-type element)
  22556. ((comment quote-section) t)
  22557. (comment-block
  22558. ;; Allow checks between block markers, not on them.
  22559. (and (> (line-beginning-position)
  22560. (org-element-property :post-affiliated element))
  22561. (save-excursion
  22562. (end-of-line)
  22563. (skip-chars-forward " \r\t\n")
  22564. (< (point) (org-element-property :end element)))))
  22565. ;; Arbitrary list of keywords where checks are meaningful.
  22566. ;; Make sure point is on the value part of the element.
  22567. (keyword
  22568. (and (member (org-element-property :key element)
  22569. '("DESCRIPTION" "TITLE"))
  22570. (< (save-excursion
  22571. (beginning-of-line) (search-forward ":") (point))
  22572. (point))))
  22573. ;; Check is globally allowed in paragraphs verse blocks and
  22574. ;; table rows (after affiliated keywords) but some objects
  22575. ;; must not be affected.
  22576. ((paragraph table-row verse-block)
  22577. (and (>= (point) (org-element-property :contents-begin element))
  22578. (< (point) (org-element-property :contents-end element))
  22579. (funcall object-check)))))))))
  22580. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  22581. (defun org-remove-flyspell-overlays-in (beg end)
  22582. "Remove flyspell overlays in region."
  22583. (and (org-bound-and-true-p flyspell-mode)
  22584. (fboundp 'flyspell-delete-region-overlays)
  22585. (flyspell-delete-region-overlays beg end)))
  22586. (defvar flyspell-delayed-commands)
  22587. (eval-after-load "flyspell"
  22588. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  22589. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  22590. (eval-after-load "bookmark"
  22591. '(if (boundp 'bookmark-after-jump-hook)
  22592. ;; We can use the hook
  22593. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  22594. ;; Hook not available, use advice
  22595. (defadvice bookmark-jump (after org-make-visible activate)
  22596. "Make the position visible."
  22597. (org-bookmark-jump-unhide))))
  22598. ;; Make sure saveplace shows the location if it was hidden
  22599. (eval-after-load "saveplace"
  22600. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  22601. "Make the position visible."
  22602. (org-bookmark-jump-unhide)))
  22603. ;; Make sure ecb shows the location if it was hidden
  22604. (eval-after-load "ecb"
  22605. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  22606. "Make hierarchy visible when jumping into location from ECB tree buffer."
  22607. (if (derived-mode-p 'org-mode)
  22608. (org-show-context))))
  22609. (defun org-bookmark-jump-unhide ()
  22610. "Unhide the current position, to show the bookmark location."
  22611. (and (derived-mode-p 'org-mode)
  22612. (or (outline-invisible-p)
  22613. (save-excursion (goto-char (max (point-min) (1- (point))))
  22614. (outline-invisible-p)))
  22615. (org-show-context 'bookmark-jump)))
  22616. (defun org-mark-jump-unhide ()
  22617. "Make the point visible with `org-show-context' after jumping to the mark."
  22618. (when (and (derived-mode-p 'org-mode)
  22619. (outline-invisible-p))
  22620. (org-show-context 'mark-goto)))
  22621. (eval-after-load "simple"
  22622. '(defadvice pop-to-mark-command (after org-make-visible activate)
  22623. "Make the point visible with `org-show-context'."
  22624. (org-mark-jump-unhide)))
  22625. (eval-after-load "simple"
  22626. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  22627. "Make the point visible with `org-show-context'."
  22628. (org-mark-jump-unhide)))
  22629. (eval-after-load "simple"
  22630. '(defadvice pop-global-mark (after org-make-visible activate)
  22631. "Make the point visible with `org-show-context'."
  22632. (org-mark-jump-unhide)))
  22633. ;; Make session.el ignore our circular variable
  22634. (defvar session-globals-exclude)
  22635. (eval-after-load "session"
  22636. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  22637. ;;;; Finish up
  22638. (provide 'org)
  22639. (run-hooks 'org-load-hook)
  22640. ;;; org.el ends here