org.el 934 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653
  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. (or (equal this-command 'eval-buffer)
  73. (condition-case nil
  74. (load (concat (file-name-directory load-file-name)
  75. "org-loaddefs.el")
  76. nil t t t)
  77. (error
  78. (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
  79. (sit-for 3)
  80. (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
  81. (sit-for 3))))
  82. (require 'org-macs)
  83. (require 'org-compat)
  84. (declare-function cdlatex-environment "ext:cdlatex" (environment item))
  85. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  86. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  87. ;;
  88. ;; In Org buffers, the value of `outline-regexp' is that of
  89. ;; `org-outline-regexp'. The only function still directly relying on
  90. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  91. ;; job when `orgstruct-mode' is active.
  92. (defvar org-outline-regexp "\\*+ "
  93. "Regexp to match Org headlines.")
  94. (defvar org-outline-regexp-bol "^\\*+ "
  95. "Regexp to match Org headlines.
  96. This is similar to `org-outline-regexp' but additionally makes
  97. sure that we are at the beginning of the line.")
  98. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  99. "Matches a headline, putting stars and text into groups.
  100. Stars are put in group 1 and the trimmed body in group 2.")
  101. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  102. (unless (boundp 'calendar-view-holidays-initially-flag)
  103. (org-defvaralias 'calendar-view-holidays-initially-flag
  104. 'view-calendar-holidays-initially))
  105. (unless (boundp 'calendar-view-diary-initially-flag)
  106. (org-defvaralias 'calendar-view-diary-initially-flag
  107. 'view-diary-entries-initially))
  108. (unless (boundp 'diary-fancy-buffer)
  109. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  110. (declare-function org-add-archive-files "org-archive" (files))
  111. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  112. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  113. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  114. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  115. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  116. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  117. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  118. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  119. (declare-function org-clock-sum "org-clock" (&optional tstart tend headline-filter propname))
  120. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  121. (declare-function org-clock-update-time-maybe "org-clock" ())
  122. (declare-function org-clocktable-shift "org-clock" (dir n))
  123. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  124. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body))
  125. (declare-function orgtbl-mode "org-table" (&optional arg))
  126. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  127. (declare-function org-beamer-mode "ox-beamer" ())
  128. (declare-function org-table-blank-field "org-table" ())
  129. (declare-function org-table-edit-field "org-table" (arg))
  130. (declare-function org-table-insert-row "org-table" (&optional arg))
  131. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  132. (declare-function org-table-set-constants "org-table" ())
  133. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  134. (declare-function org-id-get-create "org-id" (&optional force))
  135. (declare-function org-add-archive-files "org-archive" (files))
  136. (declare-function org-id-find-id-file "org-id" (id))
  137. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  138. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  139. (declare-function org-agenda-redo "org-agenda" (&optional all))
  140. (declare-function org-table-align "org-table" ())
  141. (declare-function org-table-begin "org-table" (&optional table-type))
  142. (declare-function org-table-blank-field "org-table" ())
  143. (declare-function org-table-end "org-table" (&optional table-type))
  144. (declare-function org-table-end-of-field "org-table" (&optional n))
  145. (declare-function org-table-insert-row "org-table" (&optional arg))
  146. (declare-function org-table-paste-rectangle "org-table" ())
  147. (declare-function org-table-maybe-eval-formula "org-table" ())
  148. (declare-function org-table-maybe-recalculate-line "org-table" ())
  149. (declare-function org-element-at-point "org-element" ())
  150. (declare-function org-element-cache-reset "org-element" (&optional all))
  151. (declare-function org-element-cache-refresh "org-element" (pos))
  152. (declare-function org-element-contents "org-element" (element))
  153. (declare-function org-element-context "org-element" (&optional element))
  154. (declare-function org-element-interpret-data "org-element"
  155. (data &optional parent))
  156. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  157. (declare-function org-element-parse-buffer "org-element"
  158. (&optional granularity visible-only))
  159. (declare-function org-element-property "org-element" (property element))
  160. (declare-function org-element-put-property "org-element"
  161. (element property value))
  162. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  163. (declare-function org-element-parse-buffer "org-element"
  164. (&optional granularity visible-only))
  165. (declare-function org-element-type "org-element" (element))
  166. (defsubst org-uniquify (list)
  167. "Non-destructively remove duplicate elements from LIST."
  168. (let ((res (copy-sequence list))) (delete-dups res)))
  169. (defsubst org-get-at-bol (property)
  170. "Get text property PROPERTY at the beginning of line."
  171. (get-text-property (point-at-bol) property))
  172. (defsubst org-trim (s)
  173. "Remove whitespace at the beginning and the end of string S."
  174. (replace-regexp-in-string
  175. "\\`[ \t\n\r]+" ""
  176. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  177. ;; load languages based on value of `org-babel-load-languages'
  178. (defvar org-babel-load-languages)
  179. ;;;###autoload
  180. (defun org-babel-do-load-languages (sym value)
  181. "Load the languages defined in `org-babel-load-languages'."
  182. (set-default sym value)
  183. (mapc (lambda (pair)
  184. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  185. (if active
  186. (progn
  187. (require (intern (concat "ob-" lang))))
  188. (progn
  189. (funcall 'fmakunbound
  190. (intern (concat "org-babel-execute:" lang)))
  191. (funcall 'fmakunbound
  192. (intern (concat "org-babel-expand-body:" lang)))))))
  193. org-babel-load-languages))
  194. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  195. ;;;###autoload
  196. (defun org-babel-load-file (file &optional compile)
  197. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  198. This function exports the source code using `org-babel-tangle'
  199. and then loads the resulting file using `load-file'. With prefix
  200. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  201. file to byte-code before it is loaded."
  202. (interactive "fFile to load: \nP")
  203. (let* ((age (lambda (file)
  204. (float-time
  205. (time-subtract (current-time)
  206. (nth 5 (or (file-attributes (file-truename file))
  207. (file-attributes file)))))))
  208. (base-name (file-name-sans-extension file))
  209. (exported-file (concat base-name ".el")))
  210. ;; tangle if the org-mode file is newer than the elisp file
  211. (unless (and (file-exists-p exported-file)
  212. (> (funcall age file) (funcall age exported-file)))
  213. (setq exported-file
  214. (car (org-babel-tangle-file file exported-file "emacs-lisp"))))
  215. (message "%s %s"
  216. (if compile
  217. (progn (byte-compile-file exported-file 'load)
  218. "Compiled and loaded")
  219. (progn (load-file exported-file) "Loaded"))
  220. exported-file)))
  221. (defcustom org-babel-load-languages '((emacs-lisp . t))
  222. "Languages which can be evaluated in Org-mode buffers.
  223. This list can be used to load support for any of the languages
  224. below, note that each language will depend on a different set of
  225. system executables and/or Emacs modes. When a language is
  226. \"loaded\", then code blocks in that language can be evaluated
  227. with `org-babel-execute-src-block' bound by default to C-c
  228. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  229. be set to remove code block evaluation from the C-c C-c
  230. keybinding. By default only Emacs Lisp (which has no
  231. requirements) is loaded."
  232. :group 'org-babel
  233. :set 'org-babel-do-load-languages
  234. :version "24.1"
  235. :type '(alist :tag "Babel Languages"
  236. :key-type
  237. (choice
  238. (const :tag "Awk" awk)
  239. (const :tag "C" C)
  240. (const :tag "R" R)
  241. (const :tag "Asymptote" asymptote)
  242. (const :tag "Calc" calc)
  243. (const :tag "Clojure" clojure)
  244. (const :tag "CSS" css)
  245. (const :tag "Ditaa" ditaa)
  246. (const :tag "Dot" dot)
  247. (const :tag "Emacs Lisp" emacs-lisp)
  248. (const :tag "Forth" forth)
  249. (const :tag "Fortran" fortran)
  250. (const :tag "Gnuplot" gnuplot)
  251. (const :tag "Haskell" haskell)
  252. (const :tag "IO" io)
  253. (const :tag "J" J)
  254. (const :tag "Java" java)
  255. (const :tag "Javascript" js)
  256. (const :tag "LaTeX" latex)
  257. (const :tag "Ledger" ledger)
  258. (const :tag "Lilypond" lilypond)
  259. (const :tag "Lisp" lisp)
  260. (const :tag "Makefile" makefile)
  261. (const :tag "Maxima" maxima)
  262. (const :tag "Matlab" matlab)
  263. (const :tag "Mscgen" mscgen)
  264. (const :tag "Ocaml" ocaml)
  265. (const :tag "Octave" octave)
  266. (const :tag "Org" org)
  267. (const :tag "Perl" perl)
  268. (const :tag "Pico Lisp" picolisp)
  269. (const :tag "PlantUML" plantuml)
  270. (const :tag "Python" python)
  271. (const :tag "Ruby" ruby)
  272. (const :tag "Sass" sass)
  273. (const :tag "Scala" scala)
  274. (const :tag "Scheme" scheme)
  275. (const :tag "Screen" screen)
  276. (const :tag "Shell Script" shell)
  277. (const :tag "Shen" shen)
  278. (const :tag "Sql" sql)
  279. (const :tag "Sqlite" sqlite)
  280. (const :tag "ebnf2ps" ebnf2ps))
  281. :value-type (boolean :tag "Activate" :value t)))
  282. ;;;; Customization variables
  283. (defcustom org-clone-delete-id nil
  284. "Remove ID property of clones of a subtree.
  285. When non-nil, clones of a subtree don't inherit the ID property.
  286. Otherwise they inherit the ID property with a new unique
  287. identifier."
  288. :type 'boolean
  289. :version "24.1"
  290. :group 'org-id)
  291. ;;; Version
  292. (org-check-version)
  293. ;;;###autoload
  294. (defun org-version (&optional here full message)
  295. "Show the org-mode version.
  296. Interactively, or when MESSAGE is non-nil, show it in echo area.
  297. With prefix argument, or when HERE is non-nil, insert it at point.
  298. In non-interactive uses, a reduced version string is output unless
  299. FULL is given."
  300. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  301. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  302. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  303. (load-suffixes (list ".el"))
  304. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  305. (org-trash (or
  306. (and (fboundp 'org-release) (fboundp 'org-git-version))
  307. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  308. (load-suffixes save-load-suffixes)
  309. (org-version (org-release))
  310. (git-version (org-git-version))
  311. (version (format "Org-mode version %s (%s @ %s)"
  312. org-version
  313. git-version
  314. (if org-install-dir
  315. (if (string= org-dir org-install-dir)
  316. org-install-dir
  317. (concat "mixed installation! " org-install-dir " and " org-dir))
  318. "org-loaddefs.el can not be found!")))
  319. (version1 (if full version org-version)))
  320. (when here (insert version1))
  321. (when message (message "%s" version1))
  322. version1))
  323. (defconst org-version (org-version))
  324. ;;; Syntax Constants
  325. ;;;; Block
  326. (defconst org-block-regexp
  327. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  328. "Regular expression for hiding blocks.")
  329. (defconst org-dblock-start-re
  330. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  331. "Matches the start line of a dynamic block, with parameters.")
  332. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  333. "Matches the end of a dynamic block.")
  334. ;;;; Clock and Planning
  335. (defconst org-clock-string "CLOCK:"
  336. "String used as prefix for timestamps clocking work hours on an item.")
  337. (defconst org-closed-string "CLOSED:"
  338. "String used as the prefix for timestamps logging closing a TODO entry.")
  339. (defconst org-deadline-string "DEADLINE:"
  340. "String to mark deadline entries.
  341. A deadline is this string, followed by a time stamp. Should be a word,
  342. terminated by a colon. You can insert a schedule keyword and
  343. a timestamp with \\[org-deadline].")
  344. (defconst org-scheduled-string "SCHEDULED:"
  345. "String to mark scheduled TODO entries.
  346. A schedule is this string, followed by a time stamp. Should be a word,
  347. terminated by a colon. You can insert a schedule keyword and
  348. a timestamp with \\[org-schedule].")
  349. (defconst org-planning-line-re
  350. (concat "^[ \t]*"
  351. (regexp-opt
  352. (list org-closed-string org-deadline-string org-scheduled-string)
  353. t))
  354. "Matches a line with planning info.
  355. Matched keyword is in group 1.")
  356. (defconst org-clock-line-re
  357. (concat "^[ \t]*" org-clock-string)
  358. "Matches a line with clock info.")
  359. ;;;; Drawer
  360. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  361. "Matches first or last line of a hidden block.
  362. Group 1 contains drawer's name or \"END\".")
  363. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  364. "Regular expression matching the first line of a property drawer.")
  365. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  366. "Regular expression matching the last line of a property drawer.")
  367. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  368. "Regular expression matching the first line of a clock drawer.")
  369. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  370. "Regular expression matching the last line of a clock drawer.")
  371. (defconst org-property-drawer-re
  372. (concat "\\(" org-property-start-re "\\)[^\000]*?\\("
  373. org-property-end-re "\\)\n?")
  374. "Matches an entire property drawer.")
  375. (defconst org-clock-drawer-re
  376. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  377. org-clock-drawer-end-re "\\)\n?")
  378. "Matches an entire clock drawer.")
  379. ;;;; Headline
  380. (defconst org-heading-keyword-regexp-format
  381. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  382. "Printf format for a regexp matching a headline with some keyword.
  383. This regexp will match the headline of any node which has the
  384. exact keyword that is put into the format. The keyword isn't in
  385. any group by default, but the stars and the body are.")
  386. (defconst org-heading-keyword-maybe-regexp-format
  387. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  388. "Printf format for a regexp matching a headline, possibly with some keyword.
  389. This regexp can match any headline with the specified keyword, or
  390. without a keyword. The keyword isn't in any group by default,
  391. but the stars and the body are.")
  392. (defconst org-archive-tag "ARCHIVE"
  393. "The tag that marks a subtree as archived.
  394. An archived subtree does not open during visibility cycling, and does
  395. not contribute to the agenda listings.")
  396. (defconst org-comment-string "COMMENT"
  397. "Entries starting with this keyword will never be exported.
  398. An entry can be toggled between COMMENT and normal with
  399. \\[org-toggle-comment].")
  400. ;;;; LaTeX Environments and Fragments
  401. (defconst org-latex-regexps
  402. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  403. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  404. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  405. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  406. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  407. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  408. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  409. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  410. "Regular expressions for matching embedded LaTeX.")
  411. ;;;; Node Property
  412. (defconst org-effort-property "Effort"
  413. "The property that is being used to keep track of effort estimates.
  414. Effort estimates given in this property need to have the format H:MM.")
  415. ;;;; Table
  416. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  417. "Detect an org-type or table-type table.")
  418. (defconst org-table-line-regexp "^[ \t]*|"
  419. "Detect an org-type table line.")
  420. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  421. "Detect an org-type table line.")
  422. (defconst org-table-hline-regexp "^[ \t]*|-"
  423. "Detect an org-type table hline.")
  424. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  425. "Detect a table-type table hline.")
  426. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  427. "Detect the first line outside a table when searching from within it.
  428. This works for both table types.")
  429. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  430. "Detect a #+TBLFM line.")
  431. ;;;; Timestamp
  432. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  433. "Regular expression for fast time stamp matching.")
  434. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  435. "Regular expression for fast time stamp matching.")
  436. (defconst org-ts-regexp0
  437. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  438. "Regular expression matching time strings for analysis.
  439. This one does not require the space after the date, so it can be used
  440. on a string that terminates immediately after the date.")
  441. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  442. "Regular expression matching time strings for analysis.")
  443. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  444. "Regular expression matching time stamps, with groups.")
  445. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  446. "Regular expression matching time stamps (also [..]), with groups.")
  447. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  448. "Regular expression matching a time stamp range.")
  449. (defconst org-tr-regexp-both
  450. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  451. "Regular expression matching a time stamp range.")
  452. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  453. org-ts-regexp "\\)?")
  454. "Regular expression matching a time stamp or time stamp range.")
  455. (defconst org-tsr-regexp-both
  456. (concat org-ts-regexp-both "\\(--?-?"
  457. org-ts-regexp-both "\\)?")
  458. "Regular expression matching a time stamp or time stamp range.
  459. The time stamps may be either active or inactive.")
  460. (defconst org-repeat-re
  461. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  462. "Regular expression for specifying repeated events.
  463. After a match, group 1 contains the repeat expression.")
  464. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  465. "Formats for `format-time-string' which are used for time stamps.")
  466. ;;; The custom variables
  467. (defgroup org nil
  468. "Outline-based notes management and organizer."
  469. :tag "Org"
  470. :group 'outlines
  471. :group 'calendar)
  472. (defcustom org-mode-hook nil
  473. "Mode hook for Org-mode, run after the mode was turned on."
  474. :group 'org
  475. :type 'hook)
  476. (defcustom org-load-hook nil
  477. "Hook that is run after org.el has been loaded."
  478. :group 'org
  479. :type 'hook)
  480. (defcustom org-log-buffer-setup-hook nil
  481. "Hook that is run after an Org log buffer is created."
  482. :group 'org
  483. :version "24.1"
  484. :type 'hook)
  485. (defvar org-modules) ; defined below
  486. (defvar org-modules-loaded nil
  487. "Have the modules been loaded already?")
  488. (defun org-load-modules-maybe (&optional force)
  489. "Load all extensions listed in `org-modules'."
  490. (when (or force (not org-modules-loaded))
  491. (mapc (lambda (ext)
  492. (condition-case nil (require ext)
  493. (error (message "Problems while trying to load feature `%s'" ext))))
  494. org-modules)
  495. (setq org-modules-loaded t)))
  496. (defun org-set-modules (var value)
  497. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  498. (set var value)
  499. (when (featurep 'org)
  500. (org-load-modules-maybe 'force)
  501. (org-element-cache-reset 'all)))
  502. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  503. "Modules that should always be loaded together with org.el.
  504. If a description starts with <C>, the file is not part of Emacs
  505. and loading it will require that you have downloaded and properly
  506. installed the Org mode distribution.
  507. You can also use this system to load external packages (i.e. neither Org
  508. core modules, nor modules from the CONTRIB directory). Just add symbols
  509. to the end of the list. If the package is called org-xyz.el, then you need
  510. to add the symbol `xyz', and the package must have a call to:
  511. \(provide 'org-xyz)
  512. For export specific modules, see also `org-export-backends'."
  513. :group 'org
  514. :set 'org-set-modules
  515. :version "24.4"
  516. :package-version '(Org . "8.0")
  517. :type
  518. '(set :greedy t
  519. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  520. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  521. (const :tag " crypt: Encryption of subtrees" org-crypt)
  522. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  523. (const :tag " docview: Links to doc-view buffers" org-docview)
  524. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  525. (const :tag " habit: Track your consistency with habits" org-habit)
  526. (const :tag " id: Global IDs for identifying entries" org-id)
  527. (const :tag " info: Links to Info nodes" org-info)
  528. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  529. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  530. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  531. (const :tag " mouse: Additional mouse support" org-mouse)
  532. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  533. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  534. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  535. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  536. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  537. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  538. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  539. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  540. (const :tag "C collector: Collect properties into tables" org-collector)
  541. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  542. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  543. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  544. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  545. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  546. (const :tag "C eval: Include command output as text" org-eval)
  547. (const :tag "C eww: Store link to url of eww" org-eww)
  548. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  549. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  550. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  551. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  552. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  553. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  554. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  555. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  556. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  557. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  558. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  559. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  560. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  561. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  562. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  563. (const :tag "C registry: A registry for Org-mode links" org-registry)
  564. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  565. (const :tag "C secretary: Team management with org-mode" org-secretary)
  566. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  567. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  568. (const :tag "C track: Keep up with Org-mode development" org-track)
  569. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  570. (const :tag "C vm: Links to VM folders/messages" org-vm)
  571. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  572. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  573. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  574. (defvar org-export--registered-backends) ; From ox.el.
  575. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  576. (declare-function org-export-backend-name "ox" (backend))
  577. (defcustom org-export-backends '(ascii html icalendar latex)
  578. "List of export back-ends that should be always available.
  579. If a description starts with <C>, the file is not part of Emacs
  580. and loading it will require that you have downloaded and properly
  581. installed the Org mode distribution.
  582. Unlike to `org-modules', libraries in this list will not be
  583. loaded along with Org, but only once the export framework is
  584. needed.
  585. This variable needs to be set before org.el is loaded. If you
  586. need to make a change while Emacs is running, use the customize
  587. interface or run the following code, where VAL stands for the new
  588. value of the variable, after updating it:
  589. \(progn
  590. \(setq org-export--registered-backends
  591. \(org-remove-if-not
  592. \(lambda (backend)
  593. \(let ((name (org-export-backend-name backend)))
  594. \(or (memq name val)
  595. \(catch 'parentp
  596. \(dolist (b val)
  597. \(and (org-export-derived-backend-p b name)
  598. \(throw 'parentp t)))))))
  599. org-export--registered-backends))
  600. \(let ((new-list (mapcar 'org-export-backend-name
  601. org-export--registered-backends)))
  602. \(dolist (backend val)
  603. \(cond
  604. \((not (load (format \"ox-%s\" backend) t t))
  605. \(message \"Problems while trying to load export back-end `%s'\"
  606. backend))
  607. \((not (memq backend new-list)) (push backend new-list))))
  608. \(set-default 'org-export-backends new-list)))
  609. Adding a back-end to this list will also pull the back-end it
  610. depends on, if any."
  611. :group 'org
  612. :group 'org-export
  613. :version "24.4"
  614. :package-version '(Org . "8.0")
  615. :initialize 'custom-initialize-set
  616. :set (lambda (var val)
  617. (if (not (featurep 'ox)) (set-default var val)
  618. ;; Any back-end not required anymore (not present in VAL and not
  619. ;; a parent of any back-end in the new value) is removed from the
  620. ;; list of registered back-ends.
  621. (setq org-export--registered-backends
  622. (org-remove-if-not
  623. (lambda (backend)
  624. (let ((name (org-export-backend-name backend)))
  625. (or (memq name val)
  626. (catch 'parentp
  627. (dolist (b val)
  628. (and (org-export-derived-backend-p b name)
  629. (throw 'parentp t)))))))
  630. org-export--registered-backends))
  631. ;; Now build NEW-LIST of both new back-ends and required
  632. ;; parents.
  633. (let ((new-list (mapcar 'org-export-backend-name
  634. org-export--registered-backends)))
  635. (dolist (backend val)
  636. (cond
  637. ((not (load (format "ox-%s" backend) t t))
  638. (message "Problems while trying to load export back-end `%s'"
  639. backend))
  640. ((not (memq backend new-list)) (push backend new-list))))
  641. ;; Set VAR to that list with fixed dependencies.
  642. (set-default var new-list))))
  643. :type '(set :greedy t
  644. (const :tag " ascii Export buffer to ASCII format" ascii)
  645. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  646. (const :tag " html Export buffer to HTML format" html)
  647. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  648. (const :tag " latex Export buffer to LaTeX format" latex)
  649. (const :tag " man Export buffer to MAN format" man)
  650. (const :tag " md Export buffer to Markdown format" md)
  651. (const :tag " odt Export buffer to ODT format" odt)
  652. (const :tag " org Export buffer to Org format" org)
  653. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  654. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  655. (const :tag "C deck Export buffer to deck.js presentations" deck)
  656. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  657. (const :tag "C groff Export buffer to Groff format" groff)
  658. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  659. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  660. (const :tag "C s5 Export buffer to s5 presentations" s5)
  661. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  662. (eval-after-load 'ox
  663. '(mapc
  664. (lambda (backend)
  665. (condition-case nil (require (intern (format "ox-%s" backend)))
  666. (error (message "Problems while trying to load export back-end `%s'"
  667. backend))))
  668. org-export-backends))
  669. (defcustom org-support-shift-select nil
  670. "Non-nil means make shift-cursor commands select text when possible.
  671. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  672. start selecting a region, or enlarge regions started in this way.
  673. In Org-mode, in special contexts, these same keys are used for
  674. other purposes, important enough to compete with shift selection.
  675. Org tries to balance these needs by supporting `shift-select-mode'
  676. outside these special contexts, under control of this variable.
  677. The default of this variable is nil, to avoid confusing behavior. Shifted
  678. cursor keys will then execute Org commands in the following contexts:
  679. - on a headline, changing TODO state (left/right) and priority (up/down)
  680. - on a time stamp, changing the time
  681. - in a plain list item, changing the bullet type
  682. - in a property definition line, switching between allowed values
  683. - in the BEGIN line of a clock table (changing the time block).
  684. Outside these contexts, the commands will throw an error.
  685. When this variable is t and the cursor is not in a special
  686. context, Org-mode will support shift-selection for making and
  687. enlarging regions. To make this more effective, the bullet
  688. cycling will no longer happen anywhere in an item line, but only
  689. if the cursor is exactly on the bullet.
  690. If you set this variable to the symbol `always', then the keys
  691. will not be special in headlines, property lines, and item lines,
  692. to make shift selection work there as well. If this is what you
  693. want, you can use the following alternative commands: `C-c C-t'
  694. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  695. can be used to switch TODO sets, `C-c -' to cycle item bullet
  696. types, and properties can be edited by hand or in column view.
  697. However, when the cursor is on a timestamp, shift-cursor commands
  698. will still edit the time stamp - this is just too good to give up.
  699. XEmacs user should have this variable set to nil, because
  700. `shift-select-mode' is in Emacs 23 or later only."
  701. :group 'org
  702. :type '(choice
  703. (const :tag "Never" nil)
  704. (const :tag "When outside special context" t)
  705. (const :tag "Everywhere except timestamps" always)))
  706. (defcustom org-loop-over-headlines-in-active-region nil
  707. "Shall some commands act upon headlines in the active region?
  708. When set to `t', some commands will be performed in all headlines
  709. within the active region.
  710. When set to `start-level', some commands will be performed in all
  711. headlines within the active region, provided that these headlines
  712. are of the same level than the first one.
  713. When set to a string, those commands will be performed on the
  714. matching headlines within the active region. Such string must be
  715. a tags/property/todo match as it is used in the agenda tags view.
  716. The list of commands is: `org-schedule', `org-deadline',
  717. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  718. `org-archive-to-archive-sibling'. The archiving commands skip
  719. already archived entries."
  720. :type '(choice (const :tag "Don't loop" nil)
  721. (const :tag "All headlines in active region" t)
  722. (const :tag "In active region, headlines at the same level than the first one" start-level)
  723. (string :tag "Tags/Property/Todo matcher"))
  724. :version "24.1"
  725. :group 'org-todo
  726. :group 'org-archive)
  727. (defgroup org-startup nil
  728. "Options concerning startup of Org-mode."
  729. :tag "Org Startup"
  730. :group 'org)
  731. (defcustom org-startup-folded t
  732. "Non-nil means entering Org-mode will switch to OVERVIEW.
  733. This can also be configured on a per-file basis by adding one of
  734. the following lines anywhere in the buffer:
  735. #+STARTUP: fold (or `overview', this is equivalent)
  736. #+STARTUP: nofold (or `showall', this is equivalent)
  737. #+STARTUP: content
  738. #+STARTUP: showeverything
  739. By default, this option is ignored when Org opens agenda files
  740. for the first time. If you want the agenda to honor the startup
  741. option, set `org-agenda-inhibit-startup' to nil."
  742. :group 'org-startup
  743. :type '(choice
  744. (const :tag "nofold: show all" nil)
  745. (const :tag "fold: overview" t)
  746. (const :tag "content: all headlines" content)
  747. (const :tag "show everything, even drawers" showeverything)))
  748. (defcustom org-startup-truncated t
  749. "Non-nil means entering Org-mode will set `truncate-lines'.
  750. This is useful since some lines containing links can be very long and
  751. uninteresting. Also tables look terrible when wrapped."
  752. :group 'org-startup
  753. :type 'boolean)
  754. (defcustom org-startup-indented nil
  755. "Non-nil means turn on `org-indent-mode' on startup.
  756. This can also be configured on a per-file basis by adding one of
  757. the following lines anywhere in the buffer:
  758. #+STARTUP: indent
  759. #+STARTUP: noindent"
  760. :group 'org-structure
  761. :type '(choice
  762. (const :tag "Not" nil)
  763. (const :tag "Globally (slow on startup in large files)" t)))
  764. (defcustom org-use-sub-superscripts t
  765. "Non-nil means interpret \"_\" and \"^\" for display.
  766. If you want to control how Org exports those characters, see
  767. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  768. used to be an alias for `org-export-with-sub-superscripts' in
  769. Org <8.0, it is not anymore.
  770. When this option is turned on, you can use TeX-like syntax for
  771. sub- and superscripts within the buffer. Several characters after
  772. \"_\" or \"^\" will be considered as a single item - so grouping
  773. with {} is normally not needed. For example, the following things
  774. will be parsed as single sub- or superscripts:
  775. 10^24 or 10^tau several digits will be considered 1 item.
  776. 10^-12 or 10^-tau a leading sign with digits or a word
  777. x^2-y^3 will be read as x^2 - y^3, because items are
  778. terminated by almost any nonword/nondigit char.
  779. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  780. Still, ambiguity is possible. So when in doubt, use {} to enclose
  781. the sub/superscript. If you set this variable to the symbol `{}',
  782. the braces are *required* in order to trigger interpretations as
  783. sub/superscript. This can be helpful in documents that need \"_\"
  784. frequently in plain text."
  785. :group 'org-startup
  786. :version "24.4"
  787. :package-version '(Org . "8.0")
  788. :type '(choice
  789. (const :tag "Always interpret" t)
  790. (const :tag "Only with braces" {})
  791. (const :tag "Never interpret" nil)))
  792. (defcustom org-startup-with-beamer-mode nil
  793. "Non-nil means turn on `org-beamer-mode' on startup.
  794. This can also be configured on a per-file basis by adding one of
  795. the following lines anywhere in the buffer:
  796. #+STARTUP: beamer"
  797. :group 'org-startup
  798. :version "24.1"
  799. :type 'boolean)
  800. (defcustom org-startup-align-all-tables nil
  801. "Non-nil means align all tables when visiting a file.
  802. This is useful when the column width in tables is forced with <N> cookies
  803. in table fields. Such tables will look correct only after the first re-align.
  804. This can also be configured on a per-file basis by adding one of
  805. the following lines anywhere in the buffer:
  806. #+STARTUP: align
  807. #+STARTUP: noalign"
  808. :group 'org-startup
  809. :type 'boolean)
  810. (defcustom org-startup-with-inline-images nil
  811. "Non-nil means show inline images when loading a new Org file.
  812. This can also be configured on a per-file basis by adding one of
  813. the following lines anywhere in the buffer:
  814. #+STARTUP: inlineimages
  815. #+STARTUP: noinlineimages"
  816. :group 'org-startup
  817. :version "24.1"
  818. :type 'boolean)
  819. (defcustom org-startup-with-latex-preview nil
  820. "Non-nil means preview LaTeX fragments when loading a new Org file.
  821. This can also be configured on a per-file basis by adding one of
  822. the following lines anywhere in the buffer:
  823. #+STARTUP: latexpreview
  824. #+STARTUP: nolatexpreview"
  825. :group 'org-startup
  826. :version "24.4"
  827. :package-version '(Org . "8.0")
  828. :type 'boolean)
  829. (defcustom org-insert-mode-line-in-empty-file nil
  830. "Non-nil means insert the first line setting Org-mode in empty files.
  831. When the function `org-mode' is called interactively in an empty file, this
  832. normally means that the file name does not automatically trigger Org-mode.
  833. To ensure that the file will always be in Org-mode in the future, a
  834. line enforcing Org-mode will be inserted into the buffer, if this option
  835. has been set."
  836. :group 'org-startup
  837. :type 'boolean)
  838. (defcustom org-replace-disputed-keys nil
  839. "Non-nil means use alternative key bindings for some keys.
  840. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  841. These keys are also used by other packages like shift-selection-mode'
  842. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  843. If you want to use Org-mode together with one of these other modes,
  844. or more generally if you would like to move some Org-mode commands to
  845. other keys, set this variable and configure the keys with the variable
  846. `org-disputed-keys'.
  847. This option is only relevant at load-time of Org-mode, and must be set
  848. *before* org.el is loaded. Changing it requires a restart of Emacs to
  849. become effective."
  850. :group 'org-startup
  851. :type 'boolean)
  852. (defcustom org-use-extra-keys nil
  853. "Non-nil means use extra key sequence definitions for certain commands.
  854. This happens automatically if you run XEmacs or if `window-system'
  855. is nil. This variable lets you do the same manually. You must
  856. set it before loading org.
  857. Example: on Carbon Emacs 22 running graphically, with an external
  858. keyboard on a Powerbook, the default way of setting M-left might
  859. not work for either Alt or ESC. Setting this variable will make
  860. it work for ESC."
  861. :group 'org-startup
  862. :type 'boolean)
  863. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  864. (defcustom org-disputed-keys
  865. '(([(shift up)] . [(meta p)])
  866. ([(shift down)] . [(meta n)])
  867. ([(shift left)] . [(meta -)])
  868. ([(shift right)] . [(meta +)])
  869. ([(control shift right)] . [(meta shift +)])
  870. ([(control shift left)] . [(meta shift -)]))
  871. "Keys for which Org-mode and other modes compete.
  872. This is an alist, cars are the default keys, second element specifies
  873. the alternative to use when `org-replace-disputed-keys' is t.
  874. Keys can be specified in any syntax supported by `define-key'.
  875. The value of this option takes effect only at Org-mode's startup,
  876. therefore you'll have to restart Emacs to apply it after changing."
  877. :group 'org-startup
  878. :type 'alist)
  879. (defun org-key (key)
  880. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  881. Or return the original if not disputed.
  882. Also apply the translations defined in `org-xemacs-key-equivalents'."
  883. (when org-replace-disputed-keys
  884. (let* ((nkey (key-description key))
  885. (x (org-find-if (lambda (x)
  886. (equal (key-description (car x)) nkey))
  887. org-disputed-keys)))
  888. (setq key (if x (cdr x) key))))
  889. (when (featurep 'xemacs)
  890. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  891. key)
  892. (defun org-find-if (predicate seq)
  893. (catch 'exit
  894. (while seq
  895. (if (funcall predicate (car seq))
  896. (throw 'exit (car seq))
  897. (pop seq)))))
  898. (defun org-defkey (keymap key def)
  899. "Define a key, possibly translated, as returned by `org-key'."
  900. (define-key keymap (org-key key) def))
  901. (defcustom org-ellipsis nil
  902. "The ellipsis to use in the Org-mode outline.
  903. When nil, just use the standard three dots.
  904. When a string, use that string instead.
  905. When a face, use the standard 3 dots, but with the specified face.
  906. The change affects only Org-mode (which will then use its own display table).
  907. Changing this requires executing \\[org-mode] in a buffer to become
  908. effective."
  909. :group 'org-startup
  910. :type '(choice (const :tag "Default" nil)
  911. (face :tag "Face" :value org-warning)
  912. (string :tag "String" :value "...#")))
  913. (defvar org-display-table nil
  914. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  915. (defgroup org-keywords nil
  916. "Keywords in Org-mode."
  917. :tag "Org Keywords"
  918. :group 'org)
  919. (defcustom org-closed-keep-when-no-todo nil
  920. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  921. :group 'org-todo
  922. :group 'org-keywords
  923. :version "24.4"
  924. :package-version '(Org . "8.0")
  925. :type 'boolean)
  926. (defgroup org-structure nil
  927. "Options concerning the general structure of Org-mode files."
  928. :tag "Org Structure"
  929. :group 'org)
  930. (defgroup org-reveal-location nil
  931. "Options about how to make context of a location visible."
  932. :tag "Org Reveal Location"
  933. :group 'org-structure)
  934. (defconst org-context-choice
  935. '(choice
  936. (const :tag "Always" t)
  937. (const :tag "Never" nil)
  938. (repeat :greedy t :tag "Individual contexts"
  939. (cons
  940. (choice :tag "Context"
  941. (const agenda)
  942. (const org-goto)
  943. (const occur-tree)
  944. (const tags-tree)
  945. (const link-search)
  946. (const mark-goto)
  947. (const bookmark-jump)
  948. (const isearch)
  949. (const default))
  950. (boolean))))
  951. "Contexts for the reveal options.")
  952. (defcustom org-show-hierarchy-above '((default . t))
  953. "Non-nil means show full hierarchy when revealing a location.
  954. Org-mode often shows locations in an org-mode file which might have
  955. been invisible before. When this is set, the hierarchy of headings
  956. above the exposed location is shown.
  957. Turning this off for example for sparse trees makes them very compact.
  958. Instead of t, this can also be an alist specifying this option for different
  959. contexts. Valid contexts are
  960. agenda when exposing an entry from the agenda
  961. org-goto when using the command `org-goto' on key C-c C-j
  962. occur-tree when using the command `org-occur' on key C-c /
  963. tags-tree when constructing a sparse tree based on tags matches
  964. link-search when exposing search matches associated with a link
  965. mark-goto when exposing the jump goal of a mark
  966. bookmark-jump when exposing a bookmark location
  967. isearch when exiting from an incremental search
  968. default default for all contexts not set explicitly"
  969. :group 'org-reveal-location
  970. :type org-context-choice)
  971. (defcustom org-show-following-heading '((default . nil))
  972. "Non-nil means show following heading when revealing a location.
  973. Org-mode often shows locations in an org-mode file which might have
  974. been invisible before. When this is set, the heading following the
  975. match is shown.
  976. Turning this off for example for sparse trees makes them very compact,
  977. but makes it harder to edit the location of the match. In such a case,
  978. use the command \\[org-reveal] to show more context.
  979. Instead of t, this can also be an alist specifying this option for different
  980. contexts. See `org-show-hierarchy-above' for valid contexts."
  981. :group 'org-reveal-location
  982. :type org-context-choice)
  983. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  984. "Non-nil means show all sibling heading when revealing a location.
  985. Org-mode often shows locations in an org-mode file which might have
  986. been invisible before. When this is set, the sibling of the current entry
  987. heading are all made visible. If `org-show-hierarchy-above' is t,
  988. the same happens on each level of the hierarchy above the current entry.
  989. By default this is on for the isearch context, off for all other contexts.
  990. Turning this off for example for sparse trees makes them very compact,
  991. but makes it harder to edit the location of the match. In such a case,
  992. use the command \\[org-reveal] to show more context.
  993. Instead of t, this can also be an alist specifying this option for different
  994. contexts. See `org-show-hierarchy-above' for valid contexts."
  995. :group 'org-reveal-location
  996. :type org-context-choice
  997. :version "24.4"
  998. :package-version '(Org . "8.0"))
  999. (defcustom org-show-entry-below '((default . nil))
  1000. "Non-nil means show the entry below a headline when revealing a location.
  1001. Org-mode often shows locations in an org-mode file which might have
  1002. been invisible before. When this is set, the text below the headline that is
  1003. exposed is also shown.
  1004. By default this is off for all contexts.
  1005. Instead of t, this can also be an alist specifying this option for different
  1006. contexts. See `org-show-hierarchy-above' for valid contexts."
  1007. :group 'org-reveal-location
  1008. :type org-context-choice)
  1009. (defcustom org-indirect-buffer-display 'other-window
  1010. "How should indirect tree buffers be displayed?
  1011. This applies to indirect buffers created with the commands
  1012. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1013. Valid values are:
  1014. current-window Display in the current window
  1015. other-window Just display in another window.
  1016. dedicated-frame Create one new frame, and re-use it each time.
  1017. new-frame Make a new frame each time. Note that in this case
  1018. previously-made indirect buffers are kept, and you need to
  1019. kill these buffers yourself."
  1020. :group 'org-structure
  1021. :group 'org-agenda-windows
  1022. :type '(choice
  1023. (const :tag "In current window" current-window)
  1024. (const :tag "In current frame, other window" other-window)
  1025. (const :tag "Each time a new frame" new-frame)
  1026. (const :tag "One dedicated frame" dedicated-frame)))
  1027. (defcustom org-use-speed-commands nil
  1028. "Non-nil means activate single letter commands at beginning of a headline.
  1029. This may also be a function to test for appropriate locations where speed
  1030. commands should be active.
  1031. For example, to activate speed commands when the point is on any
  1032. star at the beginning of the headline, you can do this:
  1033. (setq org-use-speed-commands
  1034. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1035. :group 'org-structure
  1036. :type '(choice
  1037. (const :tag "Never" nil)
  1038. (const :tag "At beginning of headline stars" t)
  1039. (function)))
  1040. (defcustom org-speed-commands-user nil
  1041. "Alist of additional speed commands.
  1042. This list will be checked before `org-speed-commands-default'
  1043. when the variable `org-use-speed-commands' is non-nil
  1044. and when the cursor is at the beginning of a headline.
  1045. The car if each entry is a string with a single letter, which must
  1046. be assigned to `self-insert-command' in the global map.
  1047. The cdr is either a command to be called interactively, a function
  1048. to be called, or a form to be evaluated.
  1049. An entry that is just a list with a single string will be interpreted
  1050. as a descriptive headline that will be added when listing the speed
  1051. commands in the Help buffer using the `?' speed command."
  1052. :group 'org-structure
  1053. :type '(repeat :value ("k" . ignore)
  1054. (choice :value ("k" . ignore)
  1055. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1056. (cons :tag "Letter and Command"
  1057. (string :tag "Command letter")
  1058. (choice
  1059. (function)
  1060. (sexp))))))
  1061. (defcustom org-bookmark-names-plist
  1062. '(:last-capture "org-capture-last-stored"
  1063. :last-refile "org-refile-last-stored"
  1064. :last-capture-marker "org-capture-last-stored-marker")
  1065. "Names for bookmarks automatically set by some Org commands.
  1066. This can provide strings as names for a number of bookmarks Org sets
  1067. automatically. The following keys are currently implemented:
  1068. :last-capture
  1069. :last-capture-marker
  1070. :last-refile
  1071. When a key does not show up in the property list, the corresponding bookmark
  1072. is not set."
  1073. :group 'org-structure
  1074. :type 'plist)
  1075. (defgroup org-cycle nil
  1076. "Options concerning visibility cycling in Org-mode."
  1077. :tag "Org Cycle"
  1078. :group 'org-structure)
  1079. (defcustom org-cycle-skip-children-state-if-no-children t
  1080. "Non-nil means skip CHILDREN state in entries that don't have any."
  1081. :group 'org-cycle
  1082. :type 'boolean)
  1083. (defcustom org-cycle-max-level nil
  1084. "Maximum level which should still be subject to visibility cycling.
  1085. Levels higher than this will, for cycling, be treated as text, not a headline.
  1086. When `org-odd-levels-only' is set, a value of N in this variable actually
  1087. means 2N-1 stars as the limiting headline.
  1088. When nil, cycle all levels.
  1089. Note that the limiting level of cycling is also influenced by
  1090. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1091. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1092. than its value."
  1093. :group 'org-cycle
  1094. :type '(choice
  1095. (const :tag "No limit" nil)
  1096. (integer :tag "Maximum level")))
  1097. (defcustom org-hide-block-startup nil
  1098. "Non-nil means entering Org-mode will fold all blocks.
  1099. This can also be set in on a per-file basis with
  1100. #+STARTUP: hideblocks
  1101. #+STARTUP: showblocks"
  1102. :group 'org-startup
  1103. :group 'org-cycle
  1104. :type 'boolean)
  1105. (defcustom org-cycle-global-at-bob nil
  1106. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1107. This makes it possible to do global cycling without having to use S-TAB or
  1108. \\[universal-argument] TAB. For this special case to work, the first line
  1109. of the buffer must not be a headline -- it may be empty or some other text.
  1110. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1111. sure the cursor stays at the beginning of the buffer. When this option is
  1112. nil, don't do anything special at the beginning of the buffer."
  1113. :group 'org-cycle
  1114. :type 'boolean)
  1115. (defcustom org-cycle-level-after-item/entry-creation t
  1116. "Non-nil means cycle entry level or item indentation in new empty entries.
  1117. When the cursor is at the end of an empty headline, i.e., with only stars
  1118. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1119. and then all possible ancestor states, before returning to the original state.
  1120. This makes data entry extremely fast: M-RET to create a new headline,
  1121. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1122. When the cursor is at the end of an empty plain list item, one TAB will
  1123. make it a subitem, two or more tabs will back up to make this an item
  1124. higher up in the item hierarchy."
  1125. :group 'org-cycle
  1126. :type 'boolean)
  1127. (defcustom org-cycle-emulate-tab t
  1128. "Where should `org-cycle' emulate TAB.
  1129. nil Never
  1130. white Only in completely white lines
  1131. whitestart Only at the beginning of lines, before the first non-white char
  1132. t Everywhere except in headlines
  1133. exc-hl-bol Everywhere except at the start of a headline
  1134. If TAB is used in a place where it does not emulate TAB, the current subtree
  1135. visibility is cycled."
  1136. :group 'org-cycle
  1137. :type '(choice (const :tag "Never" nil)
  1138. (const :tag "Only in completely white lines" white)
  1139. (const :tag "Before first char in a line" whitestart)
  1140. (const :tag "Everywhere except in headlines" t)
  1141. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1142. (defcustom org-cycle-separator-lines 2
  1143. "Number of empty lines needed to keep an empty line between collapsed trees.
  1144. If you leave an empty line between the end of a subtree and the following
  1145. headline, this empty line is hidden when the subtree is folded.
  1146. Org-mode will leave (exactly) one empty line visible if the number of
  1147. empty lines is equal or larger to the number given in this variable.
  1148. So the default 2 means at least 2 empty lines after the end of a subtree
  1149. are needed to produce free space between a collapsed subtree and the
  1150. following headline.
  1151. If the number is negative, and the number of empty lines is at least -N,
  1152. all empty lines are shown.
  1153. Special case: when 0, never leave empty lines in collapsed view."
  1154. :group 'org-cycle
  1155. :type 'integer)
  1156. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1157. (defcustom org-pre-cycle-hook nil
  1158. "Hook that is run before visibility cycling is happening.
  1159. The function(s) in this hook must accept a single argument which indicates
  1160. the new state that will be set right after running this hook. The
  1161. argument is a symbol. Before a global state change, it can have the values
  1162. `overview', `content', or `all'. Before a local state change, it can have
  1163. the values `folded', `children', or `subtree'."
  1164. :group 'org-cycle
  1165. :type 'hook)
  1166. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1167. org-cycle-hide-drawers
  1168. org-cycle-hide-inline-tasks
  1169. org-cycle-show-empty-lines
  1170. org-optimize-window-after-visibility-change)
  1171. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1172. The function(s) in this hook must accept a single argument which indicates
  1173. the new state that was set by the most recent `org-cycle' command. The
  1174. argument is a symbol. After a global state change, it can have the values
  1175. `overview', `contents', or `all'. After a local state change, it can have
  1176. the values `folded', `children', or `subtree'."
  1177. :group 'org-cycle
  1178. :type 'hook)
  1179. (defgroup org-edit-structure nil
  1180. "Options concerning structure editing in Org-mode."
  1181. :tag "Org Edit Structure"
  1182. :group 'org-structure)
  1183. (defcustom org-odd-levels-only nil
  1184. "Non-nil means skip even levels and only use odd levels for the outline.
  1185. This has the effect that two stars are being added/taken away in
  1186. promotion/demotion commands. It also influences how levels are
  1187. handled by the exporters.
  1188. Changing it requires restart of `font-lock-mode' to become effective
  1189. for fontification also in regions already fontified.
  1190. You may also set this on a per-file basis by adding one of the following
  1191. lines to the buffer:
  1192. #+STARTUP: odd
  1193. #+STARTUP: oddeven"
  1194. :group 'org-edit-structure
  1195. :group 'org-appearance
  1196. :type 'boolean)
  1197. (defcustom org-adapt-indentation t
  1198. "Non-nil means adapt indentation to outline node level.
  1199. When this variable is set, Org assumes that you write outlines by
  1200. indenting text in each node to align with the headline (after the stars).
  1201. The following issues are influenced by this variable:
  1202. - When this is set and the *entire* text in an entry is indented, the
  1203. indentation is increased by one space in a demotion command, and
  1204. decreased by one in a promotion command. If any line in the entry
  1205. body starts with text at column 0, indentation is not changed at all.
  1206. - Property drawers and planning information is inserted indented when
  1207. this variable s set. When nil, they will not be indented.
  1208. - TAB indents a line relative to context. The lines below a headline
  1209. will be indented when this variable is set.
  1210. Note that this is all about true indentation, by adding and removing
  1211. space characters. See also `org-indent.el' which does level-dependent
  1212. indentation in a virtual way, i.e. at display time in Emacs."
  1213. :group 'org-edit-structure
  1214. :type 'boolean)
  1215. (defcustom org-special-ctrl-a/e nil
  1216. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1217. When t, `C-a' will bring back the cursor to the beginning of the
  1218. headline text, i.e. after the stars and after a possible TODO
  1219. keyword. In an item, this will be the position after bullet and
  1220. check-box, if any. When the cursor is already at that position,
  1221. another `C-a' will bring it to the beginning of the line.
  1222. `C-e' will jump to the end of the headline, ignoring the presence
  1223. of tags in the headline. A second `C-e' will then jump to the
  1224. true end of the line, after any tags. This also means that, when
  1225. this variable is non-nil, `C-e' also will never jump beyond the
  1226. end of the heading of a folded section, i.e. not after the
  1227. ellipses.
  1228. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1229. normally, going to the true line boundary first. Only a directly
  1230. following, identical keypress will bring the cursor to the
  1231. special positions.
  1232. This may also be a cons cell where the behavior for `C-a' and
  1233. `C-e' is set separately."
  1234. :group 'org-edit-structure
  1235. :type '(choice
  1236. (const :tag "off" nil)
  1237. (const :tag "on: after stars/bullet and before tags first" t)
  1238. (const :tag "reversed: true line boundary first" reversed)
  1239. (cons :tag "Set C-a and C-e separately"
  1240. (choice :tag "Special C-a"
  1241. (const :tag "off" nil)
  1242. (const :tag "on: after stars/bullet first" t)
  1243. (const :tag "reversed: before stars/bullet first" reversed))
  1244. (choice :tag "Special C-e"
  1245. (const :tag "off" nil)
  1246. (const :tag "on: before tags first" t)
  1247. (const :tag "reversed: after tags first" reversed)))))
  1248. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1249. (defcustom org-special-ctrl-k nil
  1250. "Non-nil means `C-k' will behave specially in headlines.
  1251. When nil, `C-k' will call the default `kill-line' command.
  1252. When t, the following will happen while the cursor is in the headline:
  1253. - When the cursor is at the beginning of a headline, kill the entire
  1254. line and possible the folded subtree below the line.
  1255. - When in the middle of the headline text, kill the headline up to the tags.
  1256. - When after the headline text, kill the tags."
  1257. :group 'org-edit-structure
  1258. :type 'boolean)
  1259. (defcustom org-ctrl-k-protect-subtree nil
  1260. "Non-nil means, do not delete a hidden subtree with C-k.
  1261. When set to the symbol `error', simply throw an error when C-k is
  1262. used to kill (part-of) a headline that has hidden text behind it.
  1263. Any other non-nil value will result in a query to the user, if it is
  1264. OK to kill that hidden subtree. When nil, kill without remorse."
  1265. :group 'org-edit-structure
  1266. :version "24.1"
  1267. :type '(choice
  1268. (const :tag "Do not protect hidden subtrees" nil)
  1269. (const :tag "Protect hidden subtrees with a security query" t)
  1270. (const :tag "Never kill a hidden subtree with C-k" error)))
  1271. (defcustom org-special-ctrl-o t
  1272. "Non-nil means, make `C-o' insert a row in tables."
  1273. :group 'org-edit-structure
  1274. :type 'boolean)
  1275. (defcustom org-catch-invisible-edits nil
  1276. "Check if in invisible region before inserting or deleting a character.
  1277. Valid values are:
  1278. nil Do not check, so just do invisible edits.
  1279. error Throw an error and do nothing.
  1280. show Make point visible, and do the requested edit.
  1281. show-and-error Make point visible, then throw an error and abort the edit.
  1282. smart Make point visible, and do insertion/deletion if it is
  1283. adjacent to visible text and the change feels predictable.
  1284. Never delete a previously invisible character or add in the
  1285. middle or right after an invisible region. Basically, this
  1286. allows insertion and backward-delete right before ellipses.
  1287. FIXME: maybe in this case we should not even show?"
  1288. :group 'org-edit-structure
  1289. :version "24.1"
  1290. :type '(choice
  1291. (const :tag "Do not check" nil)
  1292. (const :tag "Throw error when trying to edit" error)
  1293. (const :tag "Unhide, but do not do the edit" show-and-error)
  1294. (const :tag "Show invisible part and do the edit" show)
  1295. (const :tag "Be smart and do the right thing" smart)))
  1296. (defcustom org-yank-folded-subtrees t
  1297. "Non-nil means when yanking subtrees, fold them.
  1298. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1299. it starts with a heading and all other headings in it are either children
  1300. or siblings, then fold all the subtrees. However, do this only if no
  1301. text after the yank would be swallowed into a folded tree by this action."
  1302. :group 'org-edit-structure
  1303. :type 'boolean)
  1304. (defcustom org-yank-adjusted-subtrees nil
  1305. "Non-nil means when yanking subtrees, adjust the level.
  1306. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1307. this function for details."
  1308. :group 'org-edit-structure
  1309. :type 'boolean)
  1310. (defcustom org-M-RET-may-split-line '((default . t))
  1311. "Non-nil means M-RET will split the line at the cursor position.
  1312. When nil, it will go to the end of the line before making a
  1313. new line.
  1314. You may also set this option in a different way for different
  1315. contexts. Valid contexts are:
  1316. headline when creating a new headline
  1317. item when creating a new item
  1318. table in a table field
  1319. default the value to be used for all contexts not explicitly
  1320. customized"
  1321. :group 'org-structure
  1322. :group 'org-table
  1323. :type '(choice
  1324. (const :tag "Always" t)
  1325. (const :tag "Never" nil)
  1326. (repeat :greedy t :tag "Individual contexts"
  1327. (cons
  1328. (choice :tag "Context"
  1329. (const headline)
  1330. (const item)
  1331. (const table)
  1332. (const default))
  1333. (boolean)))))
  1334. (defcustom org-insert-heading-respect-content nil
  1335. "Non-nil means insert new headings after the current subtree.
  1336. When nil, the new heading is created directly after the current line.
  1337. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1338. this variable on for the duration of the command."
  1339. :group 'org-structure
  1340. :type 'boolean)
  1341. (defcustom org-blank-before-new-entry '((heading . auto)
  1342. (plain-list-item . auto))
  1343. "Should `org-insert-heading' leave a blank line before new heading/item?
  1344. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1345. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1346. which case Org will look at the surrounding headings/items and try to
  1347. make an intelligent decision whether to insert a blank line or not.
  1348. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1349. the setting here is ignored and no empty line is inserted to avoid breaking
  1350. the list structure."
  1351. :group 'org-edit-structure
  1352. :type '(list
  1353. (cons (const heading)
  1354. (choice (const :tag "Never" nil)
  1355. (const :tag "Always" t)
  1356. (const :tag "Auto" auto)))
  1357. (cons (const plain-list-item)
  1358. (choice (const :tag "Never" nil)
  1359. (const :tag "Always" t)
  1360. (const :tag "Auto" auto)))))
  1361. (defcustom org-insert-heading-hook nil
  1362. "Hook being run after inserting a new heading."
  1363. :group 'org-edit-structure
  1364. :type 'hook)
  1365. (defcustom org-enable-fixed-width-editor t
  1366. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1367. This currently only means they are never auto-wrapped.
  1368. When nil, such lines will be treated like ordinary lines."
  1369. :group 'org-edit-structure
  1370. :type 'boolean)
  1371. (defcustom org-goto-auto-isearch t
  1372. "Non-nil means typing characters in `org-goto' starts incremental search.
  1373. When nil, you can use these keybindings to navigate the buffer:
  1374. q Quit the org-goto interface
  1375. n Go to the next visible heading
  1376. p Go to the previous visible heading
  1377. f Go one heading forward on same level
  1378. b Go one heading backward on same level
  1379. u Go one heading up"
  1380. :group 'org-edit-structure
  1381. :type 'boolean)
  1382. (defgroup org-sparse-trees nil
  1383. "Options concerning sparse trees in Org-mode."
  1384. :tag "Org Sparse Trees"
  1385. :group 'org-structure)
  1386. (defcustom org-highlight-sparse-tree-matches t
  1387. "Non-nil means highlight all matches that define a sparse tree.
  1388. The highlights will automatically disappear the next time the buffer is
  1389. changed by an edit command."
  1390. :group 'org-sparse-trees
  1391. :type 'boolean)
  1392. (defcustom org-remove-highlights-with-change t
  1393. "Non-nil means any change to the buffer will remove temporary highlights.
  1394. Such highlights are created by `org-occur' and `org-clock-display'.
  1395. When nil, `C-c C-c' needs to be used to get rid of the highlights.
  1396. The highlights created by `org-toggle-latex-fragment' always need
  1397. `C-c C-x C-l' to be removed."
  1398. :group 'org-sparse-trees
  1399. :group 'org-time
  1400. :type 'boolean)
  1401. (defcustom org-occur-hook '(org-first-headline-recenter)
  1402. "Hook that is run after `org-occur' has constructed a sparse tree.
  1403. This can be used to recenter the window to show as much of the structure
  1404. as possible."
  1405. :group 'org-sparse-trees
  1406. :type 'hook)
  1407. (defgroup org-imenu-and-speedbar nil
  1408. "Options concerning imenu and speedbar in Org-mode."
  1409. :tag "Org Imenu and Speedbar"
  1410. :group 'org-structure)
  1411. (defcustom org-imenu-depth 2
  1412. "The maximum level for Imenu access to Org-mode headlines.
  1413. This also applied for speedbar access."
  1414. :group 'org-imenu-and-speedbar
  1415. :type 'integer)
  1416. (defgroup org-table nil
  1417. "Options concerning tables in Org-mode."
  1418. :tag "Org Table"
  1419. :group 'org)
  1420. (defcustom org-enable-table-editor 'optimized
  1421. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1422. When nil, such lines will be treated like ordinary lines.
  1423. When equal to the symbol `optimized', the table editor will be optimized to
  1424. do the following:
  1425. - Automatic overwrite mode in front of whitespace in table fields.
  1426. This makes the structure of the table stay in tact as long as the edited
  1427. field does not exceed the column width.
  1428. - Minimize the number of realigns. Normally, the table is aligned each time
  1429. TAB or RET are pressed to move to another field. With optimization this
  1430. happens only if changes to a field might have changed the column width.
  1431. Optimization requires replacing the functions `self-insert-command',
  1432. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1433. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1434. very good at guessing when a re-align will be necessary, but you can always
  1435. force one with \\[org-ctrl-c-ctrl-c].
  1436. If you would like to use the optimized version in Org-mode, but the
  1437. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1438. This variable can be used to turn on and off the table editor during a session,
  1439. but in order to toggle optimization, a restart is required.
  1440. See also the variable `org-table-auto-blank-field'."
  1441. :group 'org-table
  1442. :type '(choice
  1443. (const :tag "off" nil)
  1444. (const :tag "on" t)
  1445. (const :tag "on, optimized" optimized)))
  1446. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1447. (version<= emacs-version "24.1"))
  1448. "Non-nil means cluster self-insert commands for undo when possible.
  1449. If this is set, then, like in the Emacs command loop, 20 consecutive
  1450. characters will be undone together.
  1451. This is configurable, because there is some impact on typing performance."
  1452. :group 'org-table
  1453. :type 'boolean)
  1454. (defcustom org-table-tab-recognizes-table.el t
  1455. "Non-nil means TAB will automatically notice a table.el table.
  1456. When it sees such a table, it moves point into it and - if necessary -
  1457. calls `table-recognize-table'."
  1458. :group 'org-table-editing
  1459. :type 'boolean)
  1460. (defgroup org-link nil
  1461. "Options concerning links in Org-mode."
  1462. :tag "Org Link"
  1463. :group 'org)
  1464. (defvar org-link-abbrev-alist-local nil
  1465. "Buffer-local version of `org-link-abbrev-alist', which see.
  1466. The value of this is taken from the #+LINK lines.")
  1467. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1468. (defcustom org-link-abbrev-alist nil
  1469. "Alist of link abbreviations.
  1470. The car of each element is a string, to be replaced at the start of a link.
  1471. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1472. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1473. [[linkkey:tag][description]]
  1474. The 'linkkey' must be a word word, starting with a letter, followed
  1475. by letters, numbers, '-' or '_'.
  1476. If REPLACE is a string, the tag will simply be appended to create the link.
  1477. If the string contains \"%s\", the tag will be inserted there. If the string
  1478. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1479. at that point (see the function `url-hexify-string'). If the string contains
  1480. the specifier \"%(my-function)\", then the custom function `my-function' will
  1481. be invoked: this function takes the tag as its only argument and must return
  1482. a string.
  1483. REPLACE may also be a function that will be called with the tag as the
  1484. only argument to create the link, which should be returned as a string.
  1485. See the manual for examples."
  1486. :group 'org-link
  1487. :type '(repeat
  1488. (cons
  1489. (string :tag "Protocol")
  1490. (choice
  1491. (string :tag "Format")
  1492. (function)))))
  1493. (defcustom org-descriptive-links t
  1494. "Non-nil means Org will display descriptive links.
  1495. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1496. \"Org Website\", hiding the link itself and just displaying its
  1497. description. When set to `nil', Org will display the full links
  1498. literally.
  1499. You can interactively set the value of this variable by calling
  1500. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1501. :group 'org-link
  1502. :type 'boolean)
  1503. (defcustom org-link-file-path-type 'adaptive
  1504. "How the path name in file links should be stored.
  1505. Valid values are:
  1506. relative Relative to the current directory, i.e. the directory of the file
  1507. into which the link is being inserted.
  1508. absolute Absolute path, if possible with ~ for home directory.
  1509. noabbrev Absolute path, no abbreviation of home directory.
  1510. adaptive Use relative path for files in the current directory and sub-
  1511. directories of it. For other files, use an absolute path."
  1512. :group 'org-link
  1513. :type '(choice
  1514. (const relative)
  1515. (const absolute)
  1516. (const noabbrev)
  1517. (const adaptive)))
  1518. (defvaralias 'org-activate-links 'org-highlight-links)
  1519. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1520. "Types of links that should be highlighted in Org-mode files.
  1521. This is a list of symbols, each one of them leading to the
  1522. highlighting of a certain link type.
  1523. You can still open links that are not highlighted.
  1524. In principle, it does not hurt to turn on highlighting for all
  1525. link types. There may be a small gain when turning off unused
  1526. link types. The types are:
  1527. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1528. angle Links in angular brackets that may contain whitespace like
  1529. <bbdb:Carsten Dominik>.
  1530. plain Plain links in normal text, no whitespace, like http://google.com.
  1531. radio Text that is matched by a radio target, see manual for details.
  1532. tag Tag settings in a headline (link to tag search).
  1533. date Time stamps (link to calendar).
  1534. footnote Footnote labels.
  1535. If you set this variable during an Emacs session, use `org-mode-restart'
  1536. in the Org buffer so that the change takes effect."
  1537. :group 'org-link
  1538. :group 'org-appearance
  1539. :type '(set :greedy t
  1540. (const :tag "Double bracket links" bracket)
  1541. (const :tag "Angular bracket links" angle)
  1542. (const :tag "Plain text links" plain)
  1543. (const :tag "Radio target matches" radio)
  1544. (const :tag "Tags" tag)
  1545. (const :tag "Timestamps" date)
  1546. (const :tag "Footnotes" footnote)))
  1547. (defcustom org-make-link-description-function nil
  1548. "Function to use for generating link descriptions from links.
  1549. When nil, the link location will be used. This function must take
  1550. two parameters: the first one is the link, the second one is the
  1551. description generated by `org-insert-link'. The function should
  1552. return the description to use."
  1553. :group 'org-link
  1554. :type '(choice (const nil) (function)))
  1555. (defgroup org-link-store nil
  1556. "Options concerning storing links in Org-mode."
  1557. :tag "Org Store Link"
  1558. :group 'org-link)
  1559. (defcustom org-url-hexify-p t
  1560. "When non-nil, hexify URL when creating a link."
  1561. :type 'boolean
  1562. :version "24.3"
  1563. :group 'org-link-store)
  1564. (defcustom org-email-link-description-format "Email %c: %.30s"
  1565. "Format of the description part of a link to an email or usenet message.
  1566. The following %-escapes will be replaced by corresponding information:
  1567. %F full \"From\" field
  1568. %f name, taken from \"From\" field, address if no name
  1569. %T full \"To\" field
  1570. %t first name in \"To\" field, address if no name
  1571. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1572. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1573. %s subject
  1574. %d date
  1575. %m message-id.
  1576. You may use normal field width specification between the % and the letter.
  1577. This is for example useful to limit the length of the subject.
  1578. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1579. :group 'org-link-store
  1580. :type 'string)
  1581. (defcustom org-from-is-user-regexp
  1582. (let (r1 r2)
  1583. (when (and user-mail-address (not (string= user-mail-address "")))
  1584. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1585. (when (and user-full-name (not (string= user-full-name "")))
  1586. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1587. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1588. "Regexp matched against the \"From:\" header of an email or usenet message.
  1589. It should match if the message is from the user him/herself."
  1590. :group 'org-link-store
  1591. :type 'regexp)
  1592. (defcustom org-context-in-file-links t
  1593. "Non-nil means file links from `org-store-link' contain context.
  1594. A search string will be added to the file name with :: as separator and
  1595. used to find the context when the link is activated by the command
  1596. `org-open-at-point'. When this option is t, the entire active region
  1597. will be placed in the search string of the file link. If set to a
  1598. positive integer, only the first n lines of context will be stored.
  1599. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1600. negates this setting for the duration of the command."
  1601. :group 'org-link-store
  1602. :type '(choice boolean integer))
  1603. (defcustom org-keep-stored-link-after-insertion nil
  1604. "Non-nil means keep link in list for entire session.
  1605. The command `org-store-link' adds a link pointing to the current
  1606. location to an internal list. These links accumulate during a session.
  1607. The command `org-insert-link' can be used to insert links into any
  1608. Org-mode file (offering completion for all stored links). When this
  1609. option is nil, every link which has been inserted once using \\[org-insert-link]
  1610. will be removed from the list, to make completing the unused links
  1611. more efficient."
  1612. :group 'org-link-store
  1613. :type 'boolean)
  1614. (defgroup org-link-follow nil
  1615. "Options concerning following links in Org-mode."
  1616. :tag "Org Follow Link"
  1617. :group 'org-link)
  1618. (defcustom org-link-translation-function nil
  1619. "Function to translate links with different syntax to Org syntax.
  1620. This can be used to translate links created for example by the Planner
  1621. or emacs-wiki packages to Org syntax.
  1622. The function must accept two parameters, a TYPE containing the link
  1623. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1624. which is everything after the link protocol. It should return a cons
  1625. with possibly modified values of type and path.
  1626. Org contains a function for this, so if you set this variable to
  1627. `org-translate-link-from-planner', you should be able follow many
  1628. links created by planner."
  1629. :group 'org-link-follow
  1630. :type '(choice (const nil) (function)))
  1631. (defcustom org-follow-link-hook nil
  1632. "Hook that is run after a link has been followed."
  1633. :group 'org-link-follow
  1634. :type 'hook)
  1635. (defcustom org-tab-follows-link nil
  1636. "Non-nil means on links TAB will follow the link.
  1637. Needs to be set before org.el is loaded.
  1638. This really should not be used, it does not make sense, and the
  1639. implementation is bad."
  1640. :group 'org-link-follow
  1641. :type 'boolean)
  1642. (defcustom org-return-follows-link nil
  1643. "Non-nil means on links RET will follow the link.
  1644. In tables, the special behavior of RET has precedence."
  1645. :group 'org-link-follow
  1646. :type 'boolean)
  1647. (defcustom org-mouse-1-follows-link
  1648. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1649. "Non-nil means mouse-1 on a link will follow the link.
  1650. A longer mouse click will still set point. Does not work on XEmacs.
  1651. Needs to be set before org.el is loaded."
  1652. :group 'org-link-follow
  1653. :version "24.4"
  1654. :package-version '(Org . "8.3")
  1655. :type '(choice
  1656. (const :tag "A double click follows the link" double)
  1657. (const :tag "Unconditionally follow the link with mouse-1" t)
  1658. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1659. (defcustom org-mark-ring-length 4
  1660. "Number of different positions to be recorded in the ring.
  1661. Changing this requires a restart of Emacs to work correctly."
  1662. :group 'org-link-follow
  1663. :type 'integer)
  1664. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1665. "Non-nil means internal links in Org files must exactly match a headline.
  1666. When nil, the link search tries to match a phrase with all words
  1667. in the search text."
  1668. :group 'org-link-follow
  1669. :version "24.1"
  1670. :type '(choice
  1671. (const :tag "Use fuzzy text search" nil)
  1672. (const :tag "Match only exact headline" t)
  1673. (const :tag "Match exact headline or query to create it"
  1674. query-to-create)))
  1675. (defcustom org-link-frame-setup
  1676. '((vm . vm-visit-folder-other-frame)
  1677. (vm-imap . vm-visit-imap-folder-other-frame)
  1678. (gnus . org-gnus-no-new-news)
  1679. (file . find-file-other-window)
  1680. (wl . wl-other-frame))
  1681. "Setup the frame configuration for following links.
  1682. When following a link with Emacs, it may often be useful to display
  1683. this link in another window or frame. This variable can be used to
  1684. set this up for the different types of links.
  1685. For VM, use any of
  1686. `vm-visit-folder'
  1687. `vm-visit-folder-other-window'
  1688. `vm-visit-folder-other-frame'
  1689. For Gnus, use any of
  1690. `gnus'
  1691. `gnus-other-frame'
  1692. `org-gnus-no-new-news'
  1693. For FILE, use any of
  1694. `find-file'
  1695. `find-file-other-window'
  1696. `find-file-other-frame'
  1697. For Wanderlust use any of
  1698. `wl'
  1699. `wl-other-frame'
  1700. For the calendar, use the variable `calendar-setup'.
  1701. For BBDB, it is currently only possible to display the matches in
  1702. another window."
  1703. :group 'org-link-follow
  1704. :type '(list
  1705. (cons (const vm)
  1706. (choice
  1707. (const vm-visit-folder)
  1708. (const vm-visit-folder-other-window)
  1709. (const vm-visit-folder-other-frame)))
  1710. (cons (const vm-imap)
  1711. (choice
  1712. (const vm-visit-imap-folder)
  1713. (const vm-visit-imap-folder-other-window)
  1714. (const vm-visit-imap-folder-other-frame)))
  1715. (cons (const gnus)
  1716. (choice
  1717. (const gnus)
  1718. (const gnus-other-frame)
  1719. (const org-gnus-no-new-news)))
  1720. (cons (const file)
  1721. (choice
  1722. (const find-file)
  1723. (const find-file-other-window)
  1724. (const find-file-other-frame)))
  1725. (cons (const wl)
  1726. (choice
  1727. (const wl)
  1728. (const wl-other-frame)))))
  1729. (defcustom org-display-internal-link-with-indirect-buffer nil
  1730. "Non-nil means use indirect buffer to display infile links.
  1731. Activating internal links (from one location in a file to another location
  1732. in the same file) normally just jumps to the location. When the link is
  1733. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1734. is displayed in
  1735. another window. When this option is set, the other window actually displays
  1736. an indirect buffer clone of the current buffer, to avoid any visibility
  1737. changes to the current buffer."
  1738. :group 'org-link-follow
  1739. :type 'boolean)
  1740. (defcustom org-open-non-existing-files nil
  1741. "Non-nil means `org-open-file' will open non-existing files.
  1742. When nil, an error will be generated.
  1743. This variable applies only to external applications because they
  1744. might choke on non-existing files. If the link is to a file that
  1745. will be opened in Emacs, the variable is ignored."
  1746. :group 'org-link-follow
  1747. :type 'boolean)
  1748. (defcustom org-open-directory-means-index-dot-org nil
  1749. "Non-nil means a link to a directory really means to index.org.
  1750. When nil, following a directory link will run dired or open a finder/explorer
  1751. window on that directory."
  1752. :group 'org-link-follow
  1753. :type 'boolean)
  1754. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1755. "Non-nil means ask for confirmation before executing shell links.
  1756. Shell links can be dangerous: just think about a link
  1757. [[shell: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-shell-link-not-regexp ""
  1772. "A regexp to skip confirmation for shell links."
  1773. :group 'org-link-follow
  1774. :version "24.1"
  1775. :type 'regexp)
  1776. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1777. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1778. Elisp links can be dangerous: just think about a link
  1779. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1780. This link would show up in your Org-mode document as \"Google Search\",
  1781. but really it would remove your entire home directory.
  1782. Therefore we advise against setting this variable to nil.
  1783. Just change it to `y-or-n-p' if you want to confirm with a
  1784. single keystroke rather than having to type \"yes\"."
  1785. :group 'org-link-follow
  1786. :type '(choice
  1787. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1788. (const :tag "with y-or-n (faster)" y-or-n-p)
  1789. (const :tag "no confirmation (dangerous)" nil)))
  1790. (put 'org-confirm-shell-link-function
  1791. 'safe-local-variable
  1792. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1793. (defcustom org-confirm-elisp-link-not-regexp ""
  1794. "A regexp to skip confirmation for Elisp links."
  1795. :group 'org-link-follow
  1796. :version "24.1"
  1797. :type 'regexp)
  1798. (defconst org-file-apps-defaults-gnu
  1799. '((remote . emacs)
  1800. (system . mailcap)
  1801. (t . mailcap))
  1802. "Default file applications on a UNIX or GNU/Linux system.
  1803. See `org-file-apps'.")
  1804. (defconst org-file-apps-defaults-macosx
  1805. '((remote . emacs)
  1806. (t . "open %s")
  1807. (system . "open %s")
  1808. ("ps.gz" . "gv %s")
  1809. ("eps.gz" . "gv %s")
  1810. ("dvi" . "xdvi %s")
  1811. ("fig" . "xfig %s"))
  1812. "Default file applications on a MacOS X system.
  1813. The system \"open\" is known as a default, but we use X11 applications
  1814. for some files for which the OS does not have a good default.
  1815. See `org-file-apps'.")
  1816. (defconst org-file-apps-defaults-windowsnt
  1817. (list
  1818. '(remote . emacs)
  1819. (cons t
  1820. (list (if (featurep 'xemacs)
  1821. 'mswindows-shell-execute
  1822. 'w32-shell-execute)
  1823. "open" 'file))
  1824. (cons 'system
  1825. (list (if (featurep 'xemacs)
  1826. 'mswindows-shell-execute
  1827. 'w32-shell-execute)
  1828. "open" 'file)))
  1829. "Default file applications on a Windows NT system.
  1830. The system \"open\" is used for most files.
  1831. See `org-file-apps'.")
  1832. (defcustom org-file-apps
  1833. '((auto-mode . emacs)
  1834. ("\\.mm\\'" . default)
  1835. ("\\.x?html?\\'" . default)
  1836. ("\\.pdf\\'" . default))
  1837. "External applications for opening `file:path' items in a document.
  1838. Org-mode uses system defaults for different file types, but
  1839. you can use this variable to set the application for a given file
  1840. extension. The entries in this list are cons cells where the car identifies
  1841. files and the cdr the corresponding command. Possible values for the
  1842. file identifier are
  1843. \"string\" A string as a file identifier can be interpreted in different
  1844. ways, depending on its contents:
  1845. - Alphanumeric characters only:
  1846. Match links with this file extension.
  1847. Example: (\"pdf\" . \"evince %s\")
  1848. to open PDFs with evince.
  1849. - Regular expression: Match links where the
  1850. filename matches the regexp. If you want to
  1851. use groups here, use shy groups.
  1852. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1853. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1854. to open *.html and *.xhtml with firefox.
  1855. - Regular expression which contains (non-shy) groups:
  1856. Match links where the whole link, including \"::\", and
  1857. anything after that, matches the regexp.
  1858. In a custom command string, %1, %2, etc. are replaced with
  1859. the parts of the link that were matched by the groups.
  1860. For backwards compatibility, if a command string is given
  1861. that does not use any of the group matches, this case is
  1862. handled identically to the second one (i.e. match against
  1863. file name only).
  1864. In a custom lisp form, you can access the group matches with
  1865. (match-string n link).
  1866. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1867. to open [[file:document.pdf::5]] with evince at page 5.
  1868. `directory' Matches a directory
  1869. `remote' Matches a remote file, accessible through tramp or efs.
  1870. Remote files most likely should be visited through Emacs
  1871. because external applications cannot handle such paths.
  1872. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1873. so all files Emacs knows how to handle. Using this with
  1874. command `emacs' will open most files in Emacs. Beware that this
  1875. will also open html files inside Emacs, unless you add
  1876. (\"html\" . default) to the list as well.
  1877. t Default for files not matched by any of the other options.
  1878. `system' The system command to open files, like `open' on Windows
  1879. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1880. that will be selected if you call `C-c C-o' with a double
  1881. \\[universal-argument] \\[universal-argument] prefix.
  1882. Possible values for the command are:
  1883. `emacs' The file will be visited by the current Emacs process.
  1884. `default' Use the default application for this file type, which is the
  1885. association for t in the list, most likely in the system-specific
  1886. part.
  1887. This can be used to overrule an unwanted setting in the
  1888. system-specific variable.
  1889. `system' Use the system command for opening files, like \"open\".
  1890. This command is specified by the entry whose car is `system'.
  1891. Most likely, the system-specific version of this variable
  1892. does define this command, but you can overrule/replace it
  1893. here.
  1894. string A command to be executed by a shell; %s will be replaced
  1895. by the path to the file.
  1896. sexp A Lisp form which will be evaluated. The file path will
  1897. be available in the Lisp variable `file'.
  1898. For more examples, see the system specific constants
  1899. `org-file-apps-defaults-macosx'
  1900. `org-file-apps-defaults-windowsnt'
  1901. `org-file-apps-defaults-gnu'."
  1902. :group 'org-link-follow
  1903. :type '(repeat
  1904. (cons (choice :value ""
  1905. (string :tag "Extension")
  1906. (const :tag "System command to open files" system)
  1907. (const :tag "Default for unrecognized files" t)
  1908. (const :tag "Remote file" remote)
  1909. (const :tag "Links to a directory" directory)
  1910. (const :tag "Any files that have Emacs modes"
  1911. auto-mode))
  1912. (choice :value ""
  1913. (const :tag "Visit with Emacs" emacs)
  1914. (const :tag "Use default" default)
  1915. (const :tag "Use the system command" system)
  1916. (string :tag "Command")
  1917. (sexp :tag "Lisp form")))))
  1918. (defcustom org-doi-server-url "http://dx.doi.org/"
  1919. "The URL of the DOI server."
  1920. :type 'string
  1921. :version "24.3"
  1922. :group 'org-link-follow)
  1923. (defgroup org-refile nil
  1924. "Options concerning refiling entries in Org-mode."
  1925. :tag "Org Refile"
  1926. :group 'org)
  1927. (defcustom org-directory "~/org"
  1928. "Directory with org files.
  1929. This is just a default location to look for Org files. There is no need
  1930. at all to put your files into this directory. It is only used in the
  1931. following situations:
  1932. 1. When a capture template specifies a target file that is not an
  1933. absolute path. The path will then be interpreted relative to
  1934. `org-directory'
  1935. 2. When a capture note is filed away in an interactive way (when exiting the
  1936. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1937. with `org-directory' as the default path."
  1938. :group 'org-refile
  1939. :group 'org-capture
  1940. :type 'directory)
  1941. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1942. "Default target for storing notes.
  1943. Used as a fall back file for org-capture.el, for templates that
  1944. do not specify a target file."
  1945. :group 'org-refile
  1946. :group 'org-capture
  1947. :type '(choice
  1948. (const :tag "Default from remember-data-file" nil)
  1949. file))
  1950. (defcustom org-goto-interface 'outline
  1951. "The default interface to be used for `org-goto'.
  1952. Allowed values are:
  1953. outline The interface shows an outline of the relevant file
  1954. and the correct heading is found by moving through
  1955. the outline or by searching with incremental search.
  1956. outline-path-completion Headlines in the current buffer are offered via
  1957. completion. This is the interface also used by
  1958. the refile command."
  1959. :group 'org-refile
  1960. :type '(choice
  1961. (const :tag "Outline" outline)
  1962. (const :tag "Outline-path-completion" outline-path-completion)))
  1963. (defcustom org-goto-max-level 5
  1964. "Maximum target level when running `org-goto' with refile interface."
  1965. :group 'org-refile
  1966. :type 'integer)
  1967. (defcustom org-reverse-note-order nil
  1968. "Non-nil means store new notes at the beginning of a file or entry.
  1969. When nil, new notes will be filed to the end of a file or entry.
  1970. This can also be a list with cons cells of regular expressions that
  1971. are matched against file names, and values."
  1972. :group 'org-capture
  1973. :group 'org-refile
  1974. :type '(choice
  1975. (const :tag "Reverse always" t)
  1976. (const :tag "Reverse never" nil)
  1977. (repeat :tag "By file name regexp"
  1978. (cons regexp boolean))))
  1979. (defcustom org-log-refile nil
  1980. "Information to record when a task is refiled.
  1981. Possible values are:
  1982. nil Don't add anything
  1983. time Add a time stamp to the task
  1984. note Prompt for a note and add it with template `org-log-note-headings'
  1985. This option can also be set with on a per-file-basis with
  1986. #+STARTUP: nologrefile
  1987. #+STARTUP: logrefile
  1988. #+STARTUP: lognoterefile
  1989. You can have local logging settings for a subtree by setting the LOGGING
  1990. property to one or more of these keywords.
  1991. When bulk-refiling from the agenda, the value `note' is forbidden and
  1992. will temporarily be changed to `time'."
  1993. :group 'org-refile
  1994. :group 'org-progress
  1995. :version "24.1"
  1996. :type '(choice
  1997. (const :tag "No logging" nil)
  1998. (const :tag "Record timestamp" time)
  1999. (const :tag "Record timestamp with note." note)))
  2000. (defcustom org-refile-targets nil
  2001. "Targets for refiling entries with \\[org-refile].
  2002. This is a list of cons cells. Each cell contains:
  2003. - a specification of the files to be considered, either a list of files,
  2004. or a symbol whose function or variable value will be used to retrieve
  2005. a file name or a list of file names. If you use `org-agenda-files' for
  2006. that, all agenda files will be scanned for targets. Nil means consider
  2007. headings in the current buffer.
  2008. - A specification of how to find candidate refile targets. This may be
  2009. any of:
  2010. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2011. This tag has to be present in all target headlines, inheritance will
  2012. not be considered.
  2013. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2014. todo keyword.
  2015. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2016. headlines that are refiling targets.
  2017. - a cons cell (:level . N). Any headline of level N is considered a target.
  2018. Note that, when `org-odd-levels-only' is set, level corresponds to
  2019. order in hierarchy, not to the number of stars.
  2020. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2021. Note that, when `org-odd-levels-only' is set, level corresponds to
  2022. order in hierarchy, not to the number of stars.
  2023. Each element of this list generates a set of possible targets.
  2024. The union of these sets is presented (with completion) to
  2025. the user by `org-refile'.
  2026. You can set the variable `org-refile-target-verify-function' to a function
  2027. to verify each headline found by the simple criteria above.
  2028. When this variable is nil, all top-level headlines in the current buffer
  2029. are used, equivalent to the value `((nil . (:level . 1))'."
  2030. :group 'org-refile
  2031. :type '(repeat
  2032. (cons
  2033. (choice :value org-agenda-files
  2034. (const :tag "All agenda files" org-agenda-files)
  2035. (const :tag "Current buffer" nil)
  2036. (function) (variable) (file))
  2037. (choice :tag "Identify target headline by"
  2038. (cons :tag "Specific tag" (const :value :tag) (string))
  2039. (cons :tag "TODO keyword" (const :value :todo) (string))
  2040. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2041. (cons :tag "Level number" (const :value :level) (integer))
  2042. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2043. (defcustom org-refile-target-verify-function nil
  2044. "Function to verify if the headline at point should be a refile target.
  2045. The function will be called without arguments, with point at the
  2046. beginning of the headline. It should return t and leave point
  2047. where it is if the headline is a valid target for refiling.
  2048. If the target should not be selected, the function must return nil.
  2049. In addition to this, it may move point to a place from where the search
  2050. should be continued. For example, the function may decide that the entire
  2051. subtree of the current entry should be excluded and move point to the end
  2052. of the subtree."
  2053. :group 'org-refile
  2054. :type '(choice
  2055. (const nil)
  2056. (function)))
  2057. (defcustom org-refile-use-cache nil
  2058. "Non-nil means cache refile targets to speed up the process.
  2059. The cache for a particular file will be updated automatically when
  2060. the buffer has been killed, or when any of the marker used for flagging
  2061. refile targets no longer points at a live buffer.
  2062. If you have added new entries to a buffer that might themselves be targets,
  2063. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  2064. find that easier, `C-u C-u C-u C-c C-w'."
  2065. :group 'org-refile
  2066. :version "24.1"
  2067. :type 'boolean)
  2068. (defcustom org-refile-use-outline-path nil
  2069. "Non-nil means provide refile targets as paths.
  2070. So a level 3 headline will be available as level1/level2/level3.
  2071. When the value is `file', also include the file name (without directory)
  2072. into the path. In this case, you can also stop the completion after
  2073. the file name, to get entries inserted as top level in the file.
  2074. When `full-file-path', include the full file path."
  2075. :group 'org-refile
  2076. :type '(choice
  2077. (const :tag "Not" nil)
  2078. (const :tag "Yes" t)
  2079. (const :tag "Start with file name" file)
  2080. (const :tag "Start with full file path" full-file-path)))
  2081. (defcustom org-outline-path-complete-in-steps t
  2082. "Non-nil means complete the outline path in hierarchical steps.
  2083. When Org-mode uses the refile interface to select an outline path
  2084. \(see variable `org-refile-use-outline-path'), the completion of
  2085. the path can be done is a single go, or if can be done in steps down
  2086. the headline hierarchy. Going in steps is probably the best if you
  2087. do not use a special completion package like `ido' or `icicles'.
  2088. However, when using these packages, going in one step can be very
  2089. fast, while still showing the whole path to the entry."
  2090. :group 'org-refile
  2091. :type 'boolean)
  2092. (defcustom org-refile-allow-creating-parent-nodes nil
  2093. "Non-nil means allow to create new nodes as refile targets.
  2094. New nodes are then created by adding \"/new node name\" to the completion
  2095. of an existing node. When the value of this variable is `confirm',
  2096. new node creation must be confirmed by the user (recommended).
  2097. When nil, the completion must match an existing entry.
  2098. Note that, if the new heading is not seen by the criteria
  2099. listed in `org-refile-targets', multiple instances of the same
  2100. heading would be created by trying again to file under the new
  2101. heading."
  2102. :group 'org-refile
  2103. :type '(choice
  2104. (const :tag "Never" nil)
  2105. (const :tag "Always" t)
  2106. (const :tag "Prompt for confirmation" confirm)))
  2107. (defcustom org-refile-active-region-within-subtree nil
  2108. "Non-nil means also refile active region within a subtree.
  2109. By default `org-refile' doesn't allow refiling regions if they
  2110. don't contain a set of subtrees, but it might be convenient to
  2111. do so sometimes: in that case, the first line of the region is
  2112. converted to a headline before refiling."
  2113. :group 'org-refile
  2114. :version "24.1"
  2115. :type 'boolean)
  2116. (defgroup org-todo nil
  2117. "Options concerning TODO items in Org-mode."
  2118. :tag "Org TODO"
  2119. :group 'org)
  2120. (defgroup org-progress nil
  2121. "Options concerning Progress logging in Org-mode."
  2122. :tag "Org Progress"
  2123. :group 'org-time)
  2124. (defvar org-todo-interpretation-widgets
  2125. '((:tag "Sequence (cycling hits every state)" sequence)
  2126. (:tag "Type (cycling directly to DONE)" type))
  2127. "The available interpretation symbols for customizing `org-todo-keywords'.
  2128. Interested libraries should add to this list.")
  2129. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2130. "List of TODO entry keyword sequences and their interpretation.
  2131. \\<org-mode-map>This is a list of sequences.
  2132. Each sequence starts with a symbol, either `sequence' or `type',
  2133. indicating if the keywords should be interpreted as a sequence of
  2134. action steps, or as different types of TODO items. The first
  2135. keywords are states requiring action - these states will select a headline
  2136. for inclusion into the global TODO list Org-mode produces. If one of
  2137. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2138. signify that no further action is necessary. If \"|\" is not found,
  2139. the last keyword is treated as the only DONE state of the sequence.
  2140. The command \\[org-todo] cycles an entry through these states, and one
  2141. additional state where no keyword is present. For details about this
  2142. cycling, see the manual.
  2143. TODO keywords and interpretation can also be set on a per-file basis with
  2144. the special #+SEQ_TODO and #+TYP_TODO lines.
  2145. Each keyword can optionally specify a character for fast state selection
  2146. \(in combination with the variable `org-use-fast-todo-selection')
  2147. and specifiers for state change logging, using the same syntax that
  2148. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2149. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2150. indicates to record a time stamp each time this state is selected.
  2151. Each keyword may also specify if a timestamp or a note should be
  2152. recorded when entering or leaving the state, by adding additional
  2153. characters in the parenthesis after the keyword. This looks like this:
  2154. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2155. record only the time of the state change. With X and Y being either
  2156. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2157. Y when leaving the state if and only if the *target* state does not
  2158. define X. You may omit any of the fast-selection key or X or /Y,
  2159. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2160. For backward compatibility, this variable may also be just a list
  2161. of keywords. In this case the interpretation (sequence or type) will be
  2162. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2163. :group 'org-todo
  2164. :group 'org-keywords
  2165. :type '(choice
  2166. (repeat :tag "Old syntax, just keywords"
  2167. (string :tag "Keyword"))
  2168. (repeat :tag "New syntax"
  2169. (cons
  2170. (choice
  2171. :tag "Interpretation"
  2172. ;;Quick and dirty way to see
  2173. ;;`org-todo-interpretations'. This takes the
  2174. ;;place of item arguments
  2175. :convert-widget
  2176. (lambda (widget)
  2177. (widget-put widget
  2178. :args (mapcar
  2179. (lambda (x)
  2180. (widget-convert
  2181. (cons 'const x)))
  2182. org-todo-interpretation-widgets))
  2183. widget))
  2184. (repeat
  2185. (string :tag "Keyword"))))))
  2186. (defvar org-todo-keywords-1 nil
  2187. "All TODO and DONE keywords active in a buffer.")
  2188. (make-variable-buffer-local 'org-todo-keywords-1)
  2189. (defvar org-todo-keywords-for-agenda nil)
  2190. (defvar org-done-keywords-for-agenda nil)
  2191. (defvar org-todo-keyword-alist-for-agenda nil)
  2192. (defvar org-tag-alist-for-agenda nil
  2193. "Alist of all tags from all agenda files.")
  2194. (defvar org-tag-groups-alist-for-agenda nil
  2195. "Alist of all groups tags from all current agenda files.")
  2196. (defvar org-tag-groups-alist nil)
  2197. (make-variable-buffer-local 'org-tag-groups-alist)
  2198. (defvar org-agenda-contributing-files nil)
  2199. (defvar org-not-done-keywords nil)
  2200. (make-variable-buffer-local 'org-not-done-keywords)
  2201. (defvar org-done-keywords nil)
  2202. (make-variable-buffer-local 'org-done-keywords)
  2203. (defvar org-todo-heads nil)
  2204. (make-variable-buffer-local 'org-todo-heads)
  2205. (defvar org-todo-sets nil)
  2206. (make-variable-buffer-local 'org-todo-sets)
  2207. (defvar org-todo-log-states nil)
  2208. (make-variable-buffer-local 'org-todo-log-states)
  2209. (defvar org-todo-kwd-alist nil)
  2210. (make-variable-buffer-local 'org-todo-kwd-alist)
  2211. (defvar org-todo-key-alist nil)
  2212. (make-variable-buffer-local 'org-todo-key-alist)
  2213. (defvar org-todo-key-trigger nil)
  2214. (make-variable-buffer-local 'org-todo-key-trigger)
  2215. (defcustom org-todo-interpretation 'sequence
  2216. "Controls how TODO keywords are interpreted.
  2217. This variable is in principle obsolete and is only used for
  2218. backward compatibility, if the interpretation of todo keywords is
  2219. not given already in `org-todo-keywords'. See that variable for
  2220. more information."
  2221. :group 'org-todo
  2222. :group 'org-keywords
  2223. :type '(choice (const sequence)
  2224. (const type)))
  2225. (defcustom org-use-fast-todo-selection t
  2226. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2227. This variable describes if and under what circumstances the cycling
  2228. mechanism for TODO keywords will be replaced by a single-key, direct
  2229. selection scheme.
  2230. When nil, fast selection is never used.
  2231. When the symbol `prefix', it will be used when `org-todo' is called
  2232. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2233. `C-u t' in an agenda buffer.
  2234. When t, fast selection is used by default. In this case, the prefix
  2235. argument forces cycling instead.
  2236. In all cases, the special interface is only used if access keys have
  2237. actually been assigned by the user, i.e. if keywords in the configuration
  2238. are followed by a letter in parenthesis, like TODO(t)."
  2239. :group 'org-todo
  2240. :type '(choice
  2241. (const :tag "Never" nil)
  2242. (const :tag "By default" t)
  2243. (const :tag "Only with C-u C-c C-t" prefix)))
  2244. (defcustom org-provide-todo-statistics t
  2245. "Non-nil means update todo statistics after insert and toggle.
  2246. ALL-HEADLINES means update todo statistics by including headlines
  2247. with no TODO keyword as well, counting them as not done.
  2248. A list of TODO keywords means the same, but skip keywords that are
  2249. not in this list.
  2250. When set to a list of two lists, the first list contains keywords
  2251. to consider as TODO keywords, the second list contains keywords
  2252. to consider as DONE keywords.
  2253. When this is set, todo statistics is updated in the parent of the
  2254. current entry each time a todo state is changed."
  2255. :group 'org-todo
  2256. :type '(choice
  2257. (const :tag "Yes, only for TODO entries" t)
  2258. (const :tag "Yes, including all entries" all-headlines)
  2259. (repeat :tag "Yes, for TODOs in this list"
  2260. (string :tag "TODO keyword"))
  2261. (list :tag "Yes, for TODOs and DONEs in these lists"
  2262. (repeat (string :tag "TODO keyword"))
  2263. (repeat (string :tag "DONE keyword")))
  2264. (other :tag "No TODO statistics" nil)))
  2265. (defcustom org-hierarchical-todo-statistics t
  2266. "Non-nil means TODO statistics covers just direct children.
  2267. When nil, all entries in the subtree are considered.
  2268. This has only an effect if `org-provide-todo-statistics' is set.
  2269. To set this to nil for only a single subtree, use a COOKIE_DATA
  2270. property and include the word \"recursive\" into the value."
  2271. :group 'org-todo
  2272. :type 'boolean)
  2273. (defcustom org-after-todo-state-change-hook nil
  2274. "Hook which is run after the state of a TODO item was changed.
  2275. The new state (a string with a TODO keyword, or nil) is available in the
  2276. Lisp variable `org-state'."
  2277. :group 'org-todo
  2278. :type 'hook)
  2279. (defvar org-blocker-hook nil
  2280. "Hook for functions that are allowed to block a state change.
  2281. Functions in this hook should not modify the buffer.
  2282. Each function gets as its single argument a property list,
  2283. see `org-trigger-hook' for more information about this list.
  2284. If any of the functions in this hook returns nil, the state change
  2285. is blocked.")
  2286. (defvar org-trigger-hook nil
  2287. "Hook for functions that are triggered by a state change.
  2288. Each function gets as its single argument a property list with at
  2289. least the following elements:
  2290. (:type type-of-change :position pos-at-entry-start
  2291. :from old-state :to new-state)
  2292. Depending on the type, more properties may be present.
  2293. This mechanism is currently implemented for:
  2294. TODO state changes
  2295. ------------------
  2296. :type todo-state-change
  2297. :from previous state (keyword as a string), or nil, or a symbol
  2298. 'todo' or 'done', to indicate the general type of state.
  2299. :to new state, like in :from")
  2300. (defcustom org-enforce-todo-dependencies nil
  2301. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2302. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2303. be blocked if any prior sibling is not yet done.
  2304. Finally, if the parent is blocked because of ordered siblings of its own,
  2305. the child will also be blocked."
  2306. :set (lambda (var val)
  2307. (set var val)
  2308. (if val
  2309. (add-hook 'org-blocker-hook
  2310. 'org-block-todo-from-children-or-siblings-or-parent)
  2311. (remove-hook 'org-blocker-hook
  2312. 'org-block-todo-from-children-or-siblings-or-parent)))
  2313. :group 'org-todo
  2314. :type 'boolean)
  2315. (defcustom org-enforce-todo-checkbox-dependencies nil
  2316. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2317. When this is nil, checkboxes have no influence on switching TODO states.
  2318. When non-nil, you first need to check off all check boxes before the TODO
  2319. entry can be switched to DONE.
  2320. This variable needs to be set before org.el is loaded, and you need to
  2321. restart Emacs after a change to make the change effective. The only way
  2322. to change is while Emacs is running is through the customize interface."
  2323. :set (lambda (var val)
  2324. (set var val)
  2325. (if val
  2326. (add-hook 'org-blocker-hook
  2327. 'org-block-todo-from-checkboxes)
  2328. (remove-hook 'org-blocker-hook
  2329. 'org-block-todo-from-checkboxes)))
  2330. :group 'org-todo
  2331. :type 'boolean)
  2332. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2333. "Non-nil means inserting a TODO heading is treated as state change.
  2334. So when the command \\[org-insert-todo-heading] is used, state change
  2335. logging will apply if appropriate. When nil, the new TODO item will
  2336. be inserted directly, and no logging will take place."
  2337. :group 'org-todo
  2338. :type 'boolean)
  2339. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2340. "Non-nil means switching TODO states with S-cursor counts as state change.
  2341. This is the default behavior. However, setting this to nil allows a
  2342. convenient way to select a TODO state and bypass any logging associated
  2343. with that."
  2344. :group 'org-todo
  2345. :type 'boolean)
  2346. (defcustom org-todo-state-tags-triggers nil
  2347. "Tag changes that should be triggered by TODO state changes.
  2348. This is a list. Each entry is
  2349. (state-change (tag . flag) .......)
  2350. State-change can be a string with a state, and empty string to indicate the
  2351. state that has no TODO keyword, or it can be one of the symbols `todo'
  2352. or `done', meaning any not-done or done state, respectively."
  2353. :group 'org-todo
  2354. :group 'org-tags
  2355. :type '(repeat
  2356. (cons (choice :tag "When changing to"
  2357. (const :tag "Not-done state" todo)
  2358. (const :tag "Done state" done)
  2359. (string :tag "State"))
  2360. (repeat
  2361. (cons :tag "Tag action"
  2362. (string :tag "Tag")
  2363. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2364. (defcustom org-log-done nil
  2365. "Information to record when a task moves to the DONE state.
  2366. Possible values are:
  2367. nil Don't add anything, just change the keyword
  2368. time Add a time stamp to the task
  2369. note Prompt for a note and add it with template `org-log-note-headings'
  2370. This option can also be set with on a per-file-basis with
  2371. #+STARTUP: nologdone
  2372. #+STARTUP: logdone
  2373. #+STARTUP: lognotedone
  2374. You can have local logging settings for a subtree by setting the LOGGING
  2375. property to one or more of these keywords."
  2376. :group 'org-todo
  2377. :group 'org-progress
  2378. :type '(choice
  2379. (const :tag "No logging" nil)
  2380. (const :tag "Record CLOSED timestamp" time)
  2381. (const :tag "Record CLOSED timestamp with note." note)))
  2382. ;; Normalize old uses of org-log-done.
  2383. (cond
  2384. ((eq org-log-done t) (setq org-log-done 'time))
  2385. ((and (listp org-log-done) (memq 'done org-log-done))
  2386. (setq org-log-done 'note)))
  2387. (defcustom org-log-reschedule nil
  2388. "Information to record when the scheduling date of a tasks is modified.
  2389. Possible values are:
  2390. nil Don't add anything, just change the date
  2391. time Add a time stamp to the task
  2392. note Prompt for a note and add it with template `org-log-note-headings'
  2393. This option can also be set with on a per-file-basis with
  2394. #+STARTUP: nologreschedule
  2395. #+STARTUP: logreschedule
  2396. #+STARTUP: lognotereschedule"
  2397. :group 'org-todo
  2398. :group 'org-progress
  2399. :type '(choice
  2400. (const :tag "No logging" nil)
  2401. (const :tag "Record timestamp" time)
  2402. (const :tag "Record timestamp with note." note)))
  2403. (defcustom org-log-redeadline nil
  2404. "Information to record when the deadline date of a tasks is modified.
  2405. Possible values are:
  2406. nil Don't add anything, just change the date
  2407. time Add a time stamp to the task
  2408. note Prompt for a note and add it with template `org-log-note-headings'
  2409. This option can also be set with on a per-file-basis with
  2410. #+STARTUP: nologredeadline
  2411. #+STARTUP: logredeadline
  2412. #+STARTUP: lognoteredeadline
  2413. You can have local logging settings for a subtree by setting the LOGGING
  2414. property to one or more of these keywords."
  2415. :group 'org-todo
  2416. :group 'org-progress
  2417. :type '(choice
  2418. (const :tag "No logging" nil)
  2419. (const :tag "Record timestamp" time)
  2420. (const :tag "Record timestamp with note." note)))
  2421. (defcustom org-log-note-clock-out nil
  2422. "Non-nil means record a note when clocking out of an item.
  2423. This can also be configured on a per-file basis by adding one of
  2424. the following lines anywhere in the buffer:
  2425. #+STARTUP: lognoteclock-out
  2426. #+STARTUP: nolognoteclock-out"
  2427. :group 'org-todo
  2428. :group 'org-progress
  2429. :type 'boolean)
  2430. (defcustom org-log-done-with-time t
  2431. "Non-nil means the CLOSED time stamp will contain date and time.
  2432. When nil, only the date will be recorded."
  2433. :group 'org-progress
  2434. :type 'boolean)
  2435. (defcustom org-log-note-headings
  2436. '((done . "CLOSING NOTE %t")
  2437. (state . "State %-12s from %-12S %t")
  2438. (note . "Note taken on %t")
  2439. (reschedule . "Rescheduled from %S on %t")
  2440. (delschedule . "Not scheduled, was %S on %t")
  2441. (redeadline . "New deadline from %S on %t")
  2442. (deldeadline . "Removed deadline, was %S on %t")
  2443. (refile . "Refiled on %t")
  2444. (clock-out . ""))
  2445. "Headings for notes added to entries.
  2446. The value is an alist, with the car being a symbol indicating the note
  2447. context, and the cdr is the heading to be used. The heading may also be the
  2448. empty string.
  2449. %t in the heading will be replaced by a time stamp.
  2450. %T will be an active time stamp instead the default inactive one
  2451. %d will be replaced by a short-format time stamp.
  2452. %D will be replaced by an active short-format time stamp.
  2453. %s will be replaced by the new TODO state, in double quotes.
  2454. %S will be replaced by the old TODO state, in double quotes.
  2455. %u will be replaced by the user name.
  2456. %U will be replaced by the full user name.
  2457. In fact, it is not a good idea to change the `state' entry, because
  2458. agenda log mode depends on the format of these entries."
  2459. :group 'org-todo
  2460. :group 'org-progress
  2461. :type '(list :greedy t
  2462. (cons (const :tag "Heading when closing an item" done) string)
  2463. (cons (const :tag
  2464. "Heading when changing todo state (todo sequence only)"
  2465. state) string)
  2466. (cons (const :tag "Heading when just taking a note" note) string)
  2467. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2468. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2469. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2470. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2471. (cons (const :tag "Heading when refiling" refile) string)
  2472. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2473. (unless (assq 'note org-log-note-headings)
  2474. (push '(note . "%t") org-log-note-headings))
  2475. (defcustom org-log-into-drawer nil
  2476. "Non-nil means insert state change notes and time stamps into a drawer.
  2477. When nil, state changes notes will be inserted after the headline and
  2478. any scheduling and clock lines, but not inside a drawer.
  2479. The value of this variable should be the name of the drawer to use.
  2480. LOGBOOK is proposed as the default drawer for this purpose, you can
  2481. also set this to a string to define the drawer of your choice.
  2482. A value of t is also allowed, representing \"LOGBOOK\".
  2483. A value of t or nil can also be set with on a per-file-basis with
  2484. #+STARTUP: logdrawer
  2485. #+STARTUP: nologdrawer
  2486. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2487. will be ignored.
  2488. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2489. a subtree."
  2490. :group 'org-todo
  2491. :group 'org-progress
  2492. :type '(choice
  2493. (const :tag "Not into a drawer" nil)
  2494. (const :tag "LOGBOOK" t)
  2495. (string :tag "Other")))
  2496. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2497. (defun org-log-into-drawer ()
  2498. "Return the value of `org-log-into-drawer', but let properties overrule.
  2499. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2500. used instead of the default value."
  2501. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2502. (cond
  2503. ((not p) org-log-into-drawer)
  2504. ((equal p "nil") nil)
  2505. ((equal p "t") "LOGBOOK")
  2506. (t p))))
  2507. (defcustom org-log-state-notes-insert-after-drawers nil
  2508. "Non-nil means insert state change notes after any drawers in entry.
  2509. Only the drawers that *immediately* follow the headline and the
  2510. deadline/scheduled line are skipped.
  2511. When nil, insert notes right after the heading and perhaps the line
  2512. with deadline/scheduling if present.
  2513. This variable will have no effect if `org-log-into-drawer' is
  2514. set."
  2515. :group 'org-todo
  2516. :group 'org-progress
  2517. :type 'boolean)
  2518. (defcustom org-log-states-order-reversed t
  2519. "Non-nil means the latest state note will be directly after heading.
  2520. When nil, the state change notes will be ordered according to time.
  2521. This option can also be set with on a per-file-basis with
  2522. #+STARTUP: logstatesreversed
  2523. #+STARTUP: nologstatesreversed"
  2524. :group 'org-todo
  2525. :group 'org-progress
  2526. :type 'boolean)
  2527. (defcustom org-todo-repeat-to-state nil
  2528. "The TODO state to which a repeater should return the repeating task.
  2529. By default this is the first task in a TODO sequence, or the previous state
  2530. in a TODO_TYP set. But you can specify another task here.
  2531. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2532. :group 'org-todo
  2533. :version "24.1"
  2534. :type '(choice (const :tag "Head of sequence" nil)
  2535. (string :tag "Specific state")))
  2536. (defcustom org-log-repeat 'time
  2537. "Non-nil means record moving through the DONE state when triggering repeat.
  2538. An auto-repeating task is immediately switched back to TODO when
  2539. marked DONE. If you are not logging state changes (by adding \"@\"
  2540. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2541. record a closing note, there will be no record of the task moving
  2542. through DONE. This variable forces taking a note anyway.
  2543. nil Don't force a record
  2544. time Record a time stamp
  2545. note Prompt for a note and add it with template `org-log-note-headings'
  2546. This option can also be set with on a per-file-basis with
  2547. #+STARTUP: nologrepeat
  2548. #+STARTUP: logrepeat
  2549. #+STARTUP: lognoterepeat
  2550. You can have local logging settings for a subtree by setting the LOGGING
  2551. property to one or more of these keywords."
  2552. :group 'org-todo
  2553. :group 'org-progress
  2554. :type '(choice
  2555. (const :tag "Don't force a record" nil)
  2556. (const :tag "Force recording the DONE state" time)
  2557. (const :tag "Force recording a note with the DONE state" note)))
  2558. (defgroup org-priorities nil
  2559. "Priorities in Org-mode."
  2560. :tag "Org Priorities"
  2561. :group 'org-todo)
  2562. (defcustom org-enable-priority-commands t
  2563. "Non-nil means priority commands are active.
  2564. When nil, these commands will be disabled, so that you never accidentally
  2565. set a priority."
  2566. :group 'org-priorities
  2567. :type 'boolean)
  2568. (defcustom org-highest-priority ?A
  2569. "The highest priority of TODO items. A character like ?A, ?B etc.
  2570. Must have a smaller ASCII number than `org-lowest-priority'."
  2571. :group 'org-priorities
  2572. :type 'character)
  2573. (defcustom org-lowest-priority ?C
  2574. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2575. Must have a larger ASCII number than `org-highest-priority'."
  2576. :group 'org-priorities
  2577. :type 'character)
  2578. (defcustom org-default-priority ?B
  2579. "The default priority of TODO items.
  2580. This is the priority an item gets if no explicit priority is given.
  2581. When starting to cycle on an empty priority the first step in the cycle
  2582. depends on `org-priority-start-cycle-with-default'. The resulting first
  2583. step priority must not exceed the range from `org-highest-priority' to
  2584. `org-lowest-priority' which means that `org-default-priority' has to be
  2585. in this range exclusive or inclusive the range boundaries. Else the
  2586. first step refuses to set the default and the second will fall back
  2587. to (depending on the command used) the highest or lowest priority."
  2588. :group 'org-priorities
  2589. :type 'character)
  2590. (defcustom org-priority-start-cycle-with-default t
  2591. "Non-nil means start with default priority when starting to cycle.
  2592. When this is nil, the first step in the cycle will be (depending on the
  2593. command used) one higher or lower than the default priority.
  2594. See also `org-default-priority'."
  2595. :group 'org-priorities
  2596. :type 'boolean)
  2597. (defcustom org-get-priority-function nil
  2598. "Function to extract the priority from a string.
  2599. The string is normally the headline. If this is nil Org computes the
  2600. priority from the priority cookie like [#A] in the headline. It returns
  2601. an integer, increasing by 1000 for each priority level.
  2602. The user can set a different function here, which should take a string
  2603. as an argument and return the numeric priority."
  2604. :group 'org-priorities
  2605. :version "24.1"
  2606. :type '(choice
  2607. (const nil)
  2608. (function)))
  2609. (defgroup org-time nil
  2610. "Options concerning time stamps and deadlines in Org-mode."
  2611. :tag "Org Time"
  2612. :group 'org)
  2613. (defcustom org-insert-labeled-timestamps-at-point nil
  2614. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2615. When nil, these labeled time stamps are forces into the second line of an
  2616. entry, just after the headline. When scheduling from the global TODO list,
  2617. the time stamp will always be forced into the second line."
  2618. :group 'org-time
  2619. :type 'boolean)
  2620. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2621. "Number of minutes to round time stamps to.
  2622. These are two values, the first applies when first creating a time stamp.
  2623. The second applies when changing it with the commands `S-up' and `S-down'.
  2624. When changing the time stamp, this means that it will change in steps
  2625. of N minutes, as given by the second value.
  2626. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2627. numbers should be factors of 60, so for example 5, 10, 15.
  2628. When this is larger than 1, you can still force an exact time stamp by using
  2629. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2630. and by using a prefix arg to `S-up/down' to specify the exact number
  2631. of minutes to shift."
  2632. :group 'org-time
  2633. :get (lambda (var) ; Make sure both elements are there
  2634. (if (integerp (default-value var))
  2635. (list (default-value var) 5)
  2636. (default-value var)))
  2637. :type '(list
  2638. (integer :tag "when inserting times")
  2639. (integer :tag "when modifying times")))
  2640. ;; Normalize old customizations of this variable.
  2641. (when (integerp org-time-stamp-rounding-minutes)
  2642. (setq org-time-stamp-rounding-minutes
  2643. (list org-time-stamp-rounding-minutes
  2644. org-time-stamp-rounding-minutes)))
  2645. (defcustom org-display-custom-times nil
  2646. "Non-nil means overlay custom formats over all time stamps.
  2647. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2648. To turn this on on a per-file basis, insert anywhere in the file:
  2649. #+STARTUP: customtime"
  2650. :group 'org-time
  2651. :set 'set-default
  2652. :type 'sexp)
  2653. (make-variable-buffer-local 'org-display-custom-times)
  2654. (defcustom org-time-stamp-custom-formats
  2655. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2656. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2657. These are overlaid over the default ISO format if the variable
  2658. `org-display-custom-times' is set. Time like %H:%M should be at the
  2659. end of the second format. The custom formats are also honored by export
  2660. commands, if custom time display is turned on at the time of export."
  2661. :group 'org-time
  2662. :type 'sexp)
  2663. (defun org-time-stamp-format (&optional long inactive)
  2664. "Get the right format for a time string."
  2665. (let ((f (if long (cdr org-time-stamp-formats)
  2666. (car org-time-stamp-formats))))
  2667. (if inactive
  2668. (concat "[" (substring f 1 -1) "]")
  2669. f)))
  2670. (defcustom org-time-clocksum-format
  2671. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2672. "The format string used when creating CLOCKSUM lines.
  2673. This is also used when Org mode generates a time duration.
  2674. The value can be a single format string containing two
  2675. %-sequences, which will be filled with the number of hours and
  2676. minutes in that order.
  2677. Alternatively, the value can be a plist associating any of the
  2678. keys :years, :months, :weeks, :days, :hours or :minutes with
  2679. format strings. The time duration is formatted using only the
  2680. time components that are needed and concatenating the results.
  2681. If a time unit in absent, it falls back to the next smallest
  2682. unit.
  2683. The keys :require-years, :require-months, :require-days,
  2684. :require-weeks, :require-hours, :require-minutes are also
  2685. meaningful. A non-nil value for these keys indicates that the
  2686. corresponding time component should always be included, even if
  2687. its value is 0.
  2688. For example,
  2689. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2690. :require-minutes t)
  2691. means durations longer than a day will be expressed in days,
  2692. hours and minutes, and durations less than a day will always be
  2693. expressed in hours and minutes (even for durations less than an
  2694. hour).
  2695. The value
  2696. \(:days \"%dd\" :minutes \"%dm\")
  2697. means durations longer than a day will be expressed in days and
  2698. minutes, and durations less than a day will be expressed entirely
  2699. in minutes (even for durations longer than an hour)."
  2700. :group 'org-time
  2701. :group 'org-clock
  2702. :version "24.4"
  2703. :package-version '(Org . "8.0")
  2704. :type '(choice (string :tag "Format string")
  2705. (set :tag "Plist"
  2706. (group :inline t (const :tag "Years" :years)
  2707. (string :tag "Format string"))
  2708. (group :inline t
  2709. (const :tag "Always show years" :require-years)
  2710. (const t))
  2711. (group :inline t (const :tag "Months" :months)
  2712. (string :tag "Format string"))
  2713. (group :inline t
  2714. (const :tag "Always show months" :require-months)
  2715. (const t))
  2716. (group :inline t (const :tag "Weeks" :weeks)
  2717. (string :tag "Format string"))
  2718. (group :inline t
  2719. (const :tag "Always show weeks" :require-weeks)
  2720. (const t))
  2721. (group :inline t (const :tag "Days" :days)
  2722. (string :tag "Format string"))
  2723. (group :inline t
  2724. (const :tag "Always show days" :require-days)
  2725. (const t))
  2726. (group :inline t (const :tag "Hours" :hours)
  2727. (string :tag "Format string"))
  2728. (group :inline t
  2729. (const :tag "Always show hours" :require-hours)
  2730. (const t))
  2731. (group :inline t (const :tag "Minutes" :minutes)
  2732. (string :tag "Format string"))
  2733. (group :inline t
  2734. (const :tag "Always show minutes" :require-minutes)
  2735. (const t)))))
  2736. (defcustom org-time-clocksum-use-fractional nil
  2737. "When non-nil, \\[org-clock-display] uses fractional times.
  2738. See `org-time-clocksum-format' for more on time clock formats."
  2739. :group 'org-time
  2740. :group 'org-clock
  2741. :version "24.3"
  2742. :type 'boolean)
  2743. (defcustom org-time-clocksum-use-effort-durations nil
  2744. "When non-nil, \\[org-clock-display] uses effort durations.
  2745. E.g. by default, one day is considered to be a 8 hours effort,
  2746. so a task that has been clocked for 16 hours will be displayed
  2747. as during 2 days in the clock display or in the clocktable.
  2748. See `org-effort-durations' on how to set effort durations
  2749. and `org-time-clocksum-format' for more on time clock formats."
  2750. :group 'org-time
  2751. :group 'org-clock
  2752. :version "24.4"
  2753. :package-version '(Org . "8.0")
  2754. :type 'boolean)
  2755. (defcustom org-time-clocksum-fractional-format "%.2f"
  2756. "The format string used when creating CLOCKSUM lines,
  2757. or when Org mode generates a time duration, if
  2758. `org-time-clocksum-use-fractional' is enabled.
  2759. The value can be a single format string containing one
  2760. %-sequence, which will be filled with the number of hours as
  2761. a float.
  2762. Alternatively, the value can be a plist associating any of the
  2763. keys :years, :months, :weeks, :days, :hours or :minutes with
  2764. a format string. The time duration is formatted using the
  2765. largest time unit which gives a non-zero integer part. If all
  2766. specified formats have zero integer part, the smallest time unit
  2767. is used."
  2768. :group 'org-time
  2769. :type '(choice (string :tag "Format string")
  2770. (set (group :inline t (const :tag "Years" :years)
  2771. (string :tag "Format string"))
  2772. (group :inline t (const :tag "Months" :months)
  2773. (string :tag "Format string"))
  2774. (group :inline t (const :tag "Weeks" :weeks)
  2775. (string :tag "Format string"))
  2776. (group :inline t (const :tag "Days" :days)
  2777. (string :tag "Format string"))
  2778. (group :inline t (const :tag "Hours" :hours)
  2779. (string :tag "Format string"))
  2780. (group :inline t (const :tag "Minutes" :minutes)
  2781. (string :tag "Format string")))))
  2782. (defcustom org-deadline-warning-days 14
  2783. "Number of days before expiration during which a deadline becomes active.
  2784. This variable governs the display in sparse trees and in the agenda.
  2785. When 0 or negative, it means use this number (the absolute value of it)
  2786. even if a deadline has a different individual lead time specified.
  2787. Custom commands can set this variable in the options section."
  2788. :group 'org-time
  2789. :group 'org-agenda-daily/weekly
  2790. :type 'integer)
  2791. (defcustom org-scheduled-delay-days 0
  2792. "Number of days before a scheduled item becomes active.
  2793. This variable governs the display in sparse trees and in the agenda.
  2794. The default value (i.e. 0) means: don't delay scheduled item.
  2795. When negative, it means use this number (the absolute value of it)
  2796. even if a scheduled item has a different individual delay time
  2797. specified.
  2798. Custom commands can set this variable in the options section."
  2799. :group 'org-time
  2800. :group 'org-agenda-daily/weekly
  2801. :version "24.4"
  2802. :package-version '(Org . "8.0")
  2803. :type 'integer)
  2804. (defcustom org-read-date-prefer-future t
  2805. "Non-nil means assume future for incomplete date input from user.
  2806. This affects the following situations:
  2807. 1. The user gives a month but not a year.
  2808. For example, if it is April and you enter \"feb 2\", this will be read
  2809. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2810. 2. The user gives a day, but no month.
  2811. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2812. read this as the third of *next* month. However, if you enter \"17\",
  2813. it will be considered as *this* month.
  2814. If you set this variable to the symbol `time', then also the following
  2815. will work:
  2816. 3. If the user gives a time.
  2817. If the time is before now, it will be interpreted as tomorrow.
  2818. Currently none of this works for ISO week specifications.
  2819. When this option is nil, the current day, month and year will always be
  2820. used as defaults.
  2821. See also `org-agenda-jump-prefer-future'."
  2822. :group 'org-time
  2823. :type '(choice
  2824. (const :tag "Never" nil)
  2825. (const :tag "Check month and day" t)
  2826. (const :tag "Check month, day, and time" time)))
  2827. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2828. "Should the agenda jump command prefer the future for incomplete dates?
  2829. The default is to do the same as configured in `org-read-date-prefer-future'.
  2830. But you can also set a deviating value here.
  2831. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2832. :group 'org-agenda
  2833. :group 'org-time
  2834. :version "24.1"
  2835. :type '(choice
  2836. (const :tag "Use org-read-date-prefer-future"
  2837. org-read-date-prefer-future)
  2838. (const :tag "Never" nil)
  2839. (const :tag "Always" t)))
  2840. (defcustom org-read-date-force-compatible-dates t
  2841. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2842. Depending on the system Emacs is running on, certain dates cannot
  2843. be represented with the type used internally to represent time.
  2844. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2845. correctly. Some systems allow for earlier dates, some for later,
  2846. some for both. One way to find out it to insert any date into an
  2847. Org buffer, putting the cursor on the year and hitting S-up and
  2848. S-down to test the range.
  2849. When this variable is set to t, the date/time prompt will not let
  2850. you specify dates outside the 1970-2037 range, so it is certain that
  2851. these dates will work in whatever version of Emacs you are
  2852. running, and also that you can move a file from one Emacs implementation
  2853. to another. WHenever Org is forcing the year for you, it will display
  2854. a message and beep.
  2855. When this variable is nil, Org will check if the date is
  2856. representable in the specific Emacs implementation you are using.
  2857. If not, it will force a year, usually the current year, and beep
  2858. to remind you. Currently this setting is not recommended because
  2859. the likelihood that you will open your Org files in an Emacs that
  2860. has limited date range is not negligible.
  2861. A workaround for this problem is to use diary sexp dates for time
  2862. stamps outside of this range."
  2863. :group 'org-time
  2864. :version "24.1"
  2865. :type 'boolean)
  2866. (defcustom org-read-date-display-live t
  2867. "Non-nil means display current interpretation of date prompt live.
  2868. This display will be in an overlay, in the minibuffer."
  2869. :group 'org-time
  2870. :type 'boolean)
  2871. (defcustom org-read-date-popup-calendar t
  2872. "Non-nil means pop up a calendar when prompting for a date.
  2873. In the calendar, the date can be selected with mouse-1. However, the
  2874. minibuffer will also be active, and you can simply enter the date as well.
  2875. When nil, only the minibuffer will be available."
  2876. :group 'org-time
  2877. :type 'boolean)
  2878. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2879. 'org-read-date-popup-calendar)
  2880. (make-obsolete-variable
  2881. 'org-read-date-minibuffer-setup-hook
  2882. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2883. (defcustom org-read-date-minibuffer-setup-hook nil
  2884. "Hook to be used to set up keys for the date/time interface.
  2885. Add key definitions to `minibuffer-local-map', which will be a
  2886. temporary copy.
  2887. WARNING: This option is obsolete, you should use
  2888. `org-read-date-minibuffer-local-map' to set up keys."
  2889. :group 'org-time
  2890. :type 'hook)
  2891. (defcustom org-extend-today-until 0
  2892. "The hour when your day really ends. Must be an integer.
  2893. This has influence for the following applications:
  2894. - When switching the agenda to \"today\". It it is still earlier than
  2895. the time given here, the day recognized as TODAY is actually yesterday.
  2896. - When a date is read from the user and it is still before the time given
  2897. here, the current date and time will be assumed to be yesterday, 23:59.
  2898. Also, timestamps inserted in capture templates follow this rule.
  2899. IMPORTANT: This is a feature whose implementation is and likely will
  2900. remain incomplete. Really, it is only here because past midnight seems to
  2901. be the favorite working time of John Wiegley :-)"
  2902. :group 'org-time
  2903. :type 'integer)
  2904. (defcustom org-use-effective-time nil
  2905. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2906. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2907. \"effective time\" of any timestamps between midnight and 8am will be
  2908. 23:59 of the previous day."
  2909. :group 'org-time
  2910. :version "24.1"
  2911. :type 'boolean)
  2912. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2913. "When non-nil, use the last clock out time for `org-todo'.
  2914. Note that this option has precedence over the combined use of
  2915. `org-use-effective-time' and `org-extend-today-until'."
  2916. :group 'org-time
  2917. :version "24.4"
  2918. :package-version '(Org . "8.0")
  2919. :type 'boolean)
  2920. (defcustom org-edit-timestamp-down-means-later nil
  2921. "Non-nil means S-down will increase the time in a time stamp.
  2922. When nil, S-up will increase."
  2923. :group 'org-time
  2924. :type 'boolean)
  2925. (defcustom org-calendar-follow-timestamp-change t
  2926. "Non-nil means make the calendar window follow timestamp changes.
  2927. When a timestamp is modified and the calendar window is visible, it will be
  2928. moved to the new date."
  2929. :group 'org-time
  2930. :type 'boolean)
  2931. (defgroup org-tags nil
  2932. "Options concerning tags in Org-mode."
  2933. :tag "Org Tags"
  2934. :group 'org)
  2935. (defcustom org-tag-alist nil
  2936. "List of tags allowed in Org-mode files.
  2937. When this list is nil, Org-mode will base TAG input on what is already in the
  2938. buffer.
  2939. The value of this variable is an alist, the car of each entry must be a
  2940. keyword as a string, the cdr may be a character that is used to select
  2941. that tag through the fast-tag-selection interface.
  2942. See the manual for details."
  2943. :group 'org-tags
  2944. :type '(repeat
  2945. (choice
  2946. (cons (string :tag "Tag name")
  2947. (character :tag "Access char"))
  2948. (list :tag "Start radio group"
  2949. (const :startgroup)
  2950. (option (string :tag "Group description")))
  2951. (list :tag "Group tags delimiter"
  2952. (const :grouptags))
  2953. (list :tag "End radio group"
  2954. (const :endgroup)
  2955. (option (string :tag "Group description")))
  2956. (const :tag "New line" (:newline)))))
  2957. (defcustom org-tag-persistent-alist nil
  2958. "List of tags that will always appear in all Org-mode files.
  2959. This is in addition to any in buffer settings or customizations
  2960. of `org-tag-alist'.
  2961. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2962. The value of this variable is an alist, the car of each entry must be a
  2963. keyword as a string, the cdr may be a character that is used to select
  2964. that tag through the fast-tag-selection interface.
  2965. See the manual for details.
  2966. To disable these tags on a per-file basis, insert anywhere in the file:
  2967. #+STARTUP: noptag"
  2968. :group 'org-tags
  2969. :type '(repeat
  2970. (choice
  2971. (cons (string :tag "Tag name")
  2972. (character :tag "Access char"))
  2973. (const :tag "Start radio group" (:startgroup))
  2974. (const :tag "Group tags delimiter" (:grouptags))
  2975. (const :tag "End radio group" (:endgroup))
  2976. (const :tag "New line" (:newline)))))
  2977. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2978. "If non-nil, always offer completion for all tags of all agenda files.
  2979. Instead of customizing this variable directly, you might want to
  2980. set it locally for capture buffers, because there no list of
  2981. tags in that file can be created dynamically (there are none).
  2982. (add-hook 'org-capture-mode-hook
  2983. (lambda ()
  2984. (set (make-local-variable
  2985. 'org-complete-tags-always-offer-all-agenda-tags)
  2986. t)))"
  2987. :group 'org-tags
  2988. :version "24.1"
  2989. :type 'boolean)
  2990. (defvar org-file-tags nil
  2991. "List of tags that can be inherited by all entries in the file.
  2992. The tags will be inherited if the variable `org-use-tag-inheritance'
  2993. says they should be.
  2994. This variable is populated from #+FILETAGS lines.")
  2995. (defcustom org-use-fast-tag-selection 'auto
  2996. "Non-nil means use fast tag selection scheme.
  2997. This is a special interface to select and deselect tags with single keys.
  2998. When nil, fast selection is never used.
  2999. When the symbol `auto', fast selection is used if and only if selection
  3000. characters for tags have been configured, either through the variable
  3001. `org-tag-alist' or through a #+TAGS line in the buffer.
  3002. When t, fast selection is always used and selection keys are assigned
  3003. automatically if necessary."
  3004. :group 'org-tags
  3005. :type '(choice
  3006. (const :tag "Always" t)
  3007. (const :tag "Never" nil)
  3008. (const :tag "When selection characters are configured" auto)))
  3009. (defcustom org-fast-tag-selection-single-key nil
  3010. "Non-nil means fast tag selection exits after first change.
  3011. When nil, you have to press RET to exit it.
  3012. During fast tag selection, you can toggle this flag with `C-c'.
  3013. This variable can also have the value `expert'. In this case, the window
  3014. displaying the tags menu is not even shown, until you press C-c again."
  3015. :group 'org-tags
  3016. :type '(choice
  3017. (const :tag "No" nil)
  3018. (const :tag "Yes" t)
  3019. (const :tag "Expert" expert)))
  3020. (defvar org-fast-tag-selection-include-todo nil
  3021. "Non-nil means fast tags selection interface will also offer TODO states.
  3022. This is an undocumented feature, you should not rely on it.")
  3023. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3024. "The column to which tags should be indented in a headline.
  3025. If this number is positive, it specifies the column. If it is negative,
  3026. it means that the tags should be flushright to that column. For example,
  3027. -80 works well for a normal 80 character screen.
  3028. When 0, place tags directly after headline text, with only one space in
  3029. between."
  3030. :group 'org-tags
  3031. :type 'integer)
  3032. (defcustom org-auto-align-tags t
  3033. "Non-nil keeps tags aligned when modifying headlines.
  3034. Some operations (i.e. demoting) change the length of a headline and
  3035. therefore shift the tags around. With this option turned on, after
  3036. each such operation the tags are again aligned to `org-tags-column'."
  3037. :group 'org-tags
  3038. :type 'boolean)
  3039. (defcustom org-use-tag-inheritance t
  3040. "Non-nil means tags in levels apply also for sublevels.
  3041. When nil, only the tags directly given in a specific line apply there.
  3042. This may also be a list of tags that should be inherited, or a regexp that
  3043. matches tags that should be inherited. Additional control is possible
  3044. with the variable `org-tags-exclude-from-inheritance' which gives an
  3045. explicit list of tags to be excluded from inheritance, even if the value of
  3046. `org-use-tag-inheritance' would select it for inheritance.
  3047. If this option is t, a match early-on in a tree can lead to a large
  3048. number of matches in the subtree when constructing the agenda or creating
  3049. a sparse tree. If you only want to see the first match in a tree during
  3050. a search, check out the variable `org-tags-match-list-sublevels'."
  3051. :group 'org-tags
  3052. :type '(choice
  3053. (const :tag "Not" nil)
  3054. (const :tag "Always" t)
  3055. (repeat :tag "Specific tags" (string :tag "Tag"))
  3056. (regexp :tag "Tags matched by regexp")))
  3057. (defcustom org-tags-exclude-from-inheritance nil
  3058. "List of tags that should never be inherited.
  3059. This is a way to exclude a few tags from inheritance. For way to do
  3060. the opposite, to actively allow inheritance for selected tags,
  3061. see the variable `org-use-tag-inheritance'."
  3062. :group 'org-tags
  3063. :type '(repeat (string :tag "Tag")))
  3064. (defun org-tag-inherit-p (tag)
  3065. "Check if TAG is one that should be inherited."
  3066. (cond
  3067. ((member tag org-tags-exclude-from-inheritance) nil)
  3068. ((eq org-use-tag-inheritance t) t)
  3069. ((not org-use-tag-inheritance) nil)
  3070. ((stringp org-use-tag-inheritance)
  3071. (string-match org-use-tag-inheritance tag))
  3072. ((listp org-use-tag-inheritance)
  3073. (member tag org-use-tag-inheritance))
  3074. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3075. (defcustom org-tags-match-list-sublevels t
  3076. "Non-nil means list also sublevels of headlines matching a search.
  3077. This variable applies to tags/property searches, and also to stuck
  3078. projects because this search is based on a tags match as well.
  3079. When set to the symbol `indented', sublevels are indented with
  3080. leading dots.
  3081. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3082. the sublevels of a headline matching a tag search often also match
  3083. the same search. Listing all of them can create very long lists.
  3084. Setting this variable to nil causes subtrees of a match to be skipped.
  3085. This variable is semi-obsolete and probably should always be true. It
  3086. is better to limit inheritance to certain tags using the variables
  3087. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3088. :group 'org-tags
  3089. :type '(choice
  3090. (const :tag "No, don't list them" nil)
  3091. (const :tag "Yes, do list them" t)
  3092. (const :tag "List them, indented with leading dots" indented)))
  3093. (defcustom org-tags-sort-function nil
  3094. "When set, tags are sorted using this function as a comparator."
  3095. :group 'org-tags
  3096. :type '(choice
  3097. (const :tag "No sorting" nil)
  3098. (const :tag "Alphabetical" string<)
  3099. (const :tag "Reverse alphabetical" string>)
  3100. (function :tag "Custom function" nil)))
  3101. (defvar org-tags-history nil
  3102. "History of minibuffer reads for tags.")
  3103. (defvar org-last-tags-completion-table nil
  3104. "The last used completion table for tags.")
  3105. (defvar org-after-tags-change-hook nil
  3106. "Hook that is run after the tags in a line have changed.")
  3107. (defgroup org-properties nil
  3108. "Options concerning properties in Org-mode."
  3109. :tag "Org Properties"
  3110. :group 'org)
  3111. (defcustom org-property-format "%-10s %s"
  3112. "How property key/value pairs should be formatted by `indent-line'.
  3113. When `indent-line' hits a property definition, it will format the line
  3114. according to this format, mainly to make sure that the values are
  3115. lined-up with respect to each other."
  3116. :group 'org-properties
  3117. :type 'string)
  3118. (defcustom org-properties-postprocess-alist nil
  3119. "Alist of properties and functions to adjust inserted values.
  3120. Elements of this alist must be of the form
  3121. ([string] [function])
  3122. where [string] must be a property name and [function] must be a
  3123. lambda expression: this lambda expression must take one argument,
  3124. the value to adjust, and return the new value as a string.
  3125. For example, this element will allow the property \"Remaining\"
  3126. to be updated wrt the relation between the \"Effort\" property
  3127. and the clock summary:
  3128. ((\"Remaining\" (lambda(value)
  3129. (let ((clocksum (org-clock-sum-current-item))
  3130. (effort (org-duration-string-to-minutes
  3131. (org-entry-get (point) \"Effort\"))))
  3132. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3133. :group 'org-properties
  3134. :version "24.1"
  3135. :type '(alist :key-type (string :tag "Property")
  3136. :value-type (function :tag "Function")))
  3137. (defcustom org-use-property-inheritance nil
  3138. "Non-nil means properties apply also for sublevels.
  3139. This setting is chiefly used during property searches. Turning it on can
  3140. cause significant overhead when doing a search, which is why it is not
  3141. on by default.
  3142. When nil, only the properties directly given in the current entry count.
  3143. When t, every property is inherited. The value may also be a list of
  3144. properties that should have inheritance, or a regular expression matching
  3145. properties that should be inherited.
  3146. However, note that some special properties use inheritance under special
  3147. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3148. and the properties ending in \"_ALL\" when they are used as descriptor
  3149. for valid values of a property.
  3150. Note for programmers:
  3151. When querying an entry with `org-entry-get', you can control if inheritance
  3152. should be used. By default, `org-entry-get' looks only at the local
  3153. properties. You can request inheritance by setting the inherit argument
  3154. to t (to force inheritance) or to `selective' (to respect the setting
  3155. in this variable)."
  3156. :group 'org-properties
  3157. :type '(choice
  3158. (const :tag "Not" nil)
  3159. (const :tag "Always" t)
  3160. (repeat :tag "Specific properties" (string :tag "Property"))
  3161. (regexp :tag "Properties matched by regexp")))
  3162. (defun org-property-inherit-p (property)
  3163. "Check if PROPERTY is one that should be inherited."
  3164. (cond
  3165. ((eq org-use-property-inheritance t) t)
  3166. ((not org-use-property-inheritance) nil)
  3167. ((stringp org-use-property-inheritance)
  3168. (string-match org-use-property-inheritance property))
  3169. ((listp org-use-property-inheritance)
  3170. (member property org-use-property-inheritance))
  3171. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3172. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3173. "The default column format, if no other format has been defined.
  3174. This variable can be set on the per-file basis by inserting a line
  3175. #+COLUMNS: %25ITEM ....."
  3176. :group 'org-properties
  3177. :type 'string)
  3178. (defcustom org-columns-ellipses ".."
  3179. "The ellipses to be used when a field in column view is truncated.
  3180. When this is the empty string, as many characters as possible are shown,
  3181. but then there will be no visual indication that the field has been truncated.
  3182. When this is a string of length N, the last N characters of a truncated
  3183. field are replaced by this string. If the column is narrower than the
  3184. ellipses string, only part of the ellipses string will be shown."
  3185. :group 'org-properties
  3186. :type 'string)
  3187. (defcustom org-columns-modify-value-for-display-function nil
  3188. "Function that modifies values for display in column view.
  3189. For example, it can be used to cut out a certain part from a time stamp.
  3190. The function must take 2 arguments:
  3191. column-title The title of the column (*not* the property name)
  3192. value The value that should be modified.
  3193. The function should return the value that should be displayed,
  3194. or nil if the normal value should be used."
  3195. :group 'org-properties
  3196. :type '(choice (const nil) (function)))
  3197. (defconst org-global-properties-fixed
  3198. '(("VISIBILITY_ALL" . "folded children content all")
  3199. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3200. "List of property/value pairs that can be inherited by any entry.
  3201. These are fixed values, for the preset properties. The user variable
  3202. that can be used to add to this list is `org-global-properties'.
  3203. The entries in this list are cons cells where the car is a property
  3204. name and cdr is a string with the value. If the value represents
  3205. multiple items like an \"_ALL\" property, separate the items by
  3206. spaces.")
  3207. (defcustom org-global-properties nil
  3208. "List of property/value pairs that can be inherited by any entry.
  3209. This list will be combined with the constant `org-global-properties-fixed'.
  3210. The entries in this list are cons cells where the car is a property
  3211. name and cdr is a string with the value.
  3212. You can set buffer-local values for the same purpose in the variable
  3213. `org-file-properties' this by adding lines like
  3214. #+PROPERTY: NAME VALUE"
  3215. :group 'org-properties
  3216. :type '(repeat
  3217. (cons (string :tag "Property")
  3218. (string :tag "Value"))))
  3219. (defvar org-file-properties nil
  3220. "List of property/value pairs that can be inherited by any entry.
  3221. Valid for the current buffer.
  3222. This variable is populated from #+PROPERTY lines.")
  3223. (make-variable-buffer-local 'org-file-properties)
  3224. (defgroup org-agenda nil
  3225. "Options concerning agenda views in Org-mode."
  3226. :tag "Org Agenda"
  3227. :group 'org)
  3228. (defvar org-category nil
  3229. "Variable used by org files to set a category for agenda display.
  3230. Such files should use a file variable to set it, for example
  3231. # -*- mode: org; org-category: \"ELisp\"
  3232. or contain a special line
  3233. #+CATEGORY: ELisp
  3234. If the file does not specify a category, then file's base name
  3235. is used instead.")
  3236. (make-variable-buffer-local 'org-category)
  3237. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3238. (defcustom org-agenda-files nil
  3239. "The files to be used for agenda display.
  3240. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3241. \\[org-remove-file]. You can also use customize to edit the list.
  3242. If an entry is a directory, all files in that directory that are matched by
  3243. `org-agenda-file-regexp' will be part of the file list.
  3244. If the value of the variable is not a list but a single file name, then
  3245. the list of agenda files is actually stored and maintained in that file, one
  3246. agenda file per line. In this file paths can be given relative to
  3247. `org-directory'. Tilde expansion and environment variable substitution
  3248. are also made."
  3249. :group 'org-agenda
  3250. :type '(choice
  3251. (repeat :tag "List of files and directories" file)
  3252. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3253. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3254. "Regular expression to match files for `org-agenda-files'.
  3255. If any element in the list in that variable contains a directory instead
  3256. of a normal file, all files in that directory that are matched by this
  3257. regular expression will be included."
  3258. :group 'org-agenda
  3259. :type 'regexp)
  3260. (defcustom org-agenda-text-search-extra-files nil
  3261. "List of extra files to be searched by text search commands.
  3262. These files will be searched in addition to the agenda files by the
  3263. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3264. Note that these files will only be searched for text search commands,
  3265. not for the other agenda views like todo lists, tag searches or the weekly
  3266. agenda. This variable is intended to list notes and possibly archive files
  3267. that should also be searched by these two commands.
  3268. In fact, if the first element in the list is the symbol `agenda-archives',
  3269. then all archive files of all agenda files will be added to the search
  3270. scope."
  3271. :group 'org-agenda
  3272. :type '(set :greedy t
  3273. (const :tag "Agenda Archives" agenda-archives)
  3274. (repeat :inline t (file))))
  3275. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3276. 'org-agenda-text-search-extra-files)
  3277. (defcustom org-agenda-skip-unavailable-files nil
  3278. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3279. A nil value means to remove them, after a query, from the list."
  3280. :group 'org-agenda
  3281. :type 'boolean)
  3282. (defcustom org-calendar-to-agenda-key [?c]
  3283. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3284. The command `org-calendar-goto-agenda' will be bound to this key. The
  3285. default is the character `c' because then `c' can be used to switch back and
  3286. forth between agenda and calendar."
  3287. :group 'org-agenda
  3288. :type 'sexp)
  3289. (defcustom org-calendar-insert-diary-entry-key [?i]
  3290. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3291. This option is irrelevant until `org-agenda-diary-file' has been configured
  3292. to point to an Org-mode file. When that is the case, the command
  3293. `org-agenda-diary-entry' will be bound to the key given here, by default
  3294. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3295. if you want to continue doing this, you need to change this to a different
  3296. key."
  3297. :group 'org-agenda
  3298. :type 'sexp)
  3299. (defcustom org-agenda-diary-file 'diary-file
  3300. "File to which to add new entries with the `i' key in agenda and calendar.
  3301. When this is the symbol `diary-file', the functionality in the Emacs
  3302. calendar will be used to add entries to the `diary-file'. But when this
  3303. points to a file, `org-agenda-diary-entry' will be used instead."
  3304. :group 'org-agenda
  3305. :type '(choice
  3306. (const :tag "The standard Emacs diary file" diary-file)
  3307. (file :tag "Special Org file diary entries")))
  3308. (eval-after-load "calendar"
  3309. '(progn
  3310. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3311. 'org-calendar-goto-agenda)
  3312. (add-hook 'calendar-mode-hook
  3313. (lambda ()
  3314. (unless (eq org-agenda-diary-file 'diary-file)
  3315. (define-key calendar-mode-map
  3316. org-calendar-insert-diary-entry-key
  3317. 'org-agenda-diary-entry))))))
  3318. (defgroup org-latex nil
  3319. "Options for embedding LaTeX code into Org-mode."
  3320. :tag "Org LaTeX"
  3321. :group 'org)
  3322. (defcustom org-format-latex-options
  3323. '(:foreground default :background default :scale 1.0
  3324. :html-foreground "Black" :html-background "Transparent"
  3325. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3326. "Options for creating images from LaTeX fragments.
  3327. This is a property list with the following properties:
  3328. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3329. `default' means use the foreground of the default face.
  3330. `auto' means use the foreground from the text face.
  3331. :background the background color, or \"Transparent\".
  3332. `default' means use the background of the default face.
  3333. `auto' means use the background from the text face.
  3334. :scale a scaling factor for the size of the images, to get more pixels
  3335. :html-foreground, :html-background, :html-scale
  3336. the same numbers for HTML export.
  3337. :matchers a list indicating which matchers should be used to
  3338. find LaTeX fragments. Valid members of this list are:
  3339. \"begin\" find environments
  3340. \"$1\" find single characters surrounded by $.$
  3341. \"$\" find math expressions surrounded by $...$
  3342. \"$$\" find math expressions surrounded by $$....$$
  3343. \"\\(\" find math expressions surrounded by \\(...\\)
  3344. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3345. :group 'org-latex
  3346. :type 'plist)
  3347. (defcustom org-format-latex-signal-error t
  3348. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3349. When nil, just push out a message."
  3350. :group 'org-latex
  3351. :version "24.1"
  3352. :type 'boolean)
  3353. (defcustom org-latex-to-mathml-jar-file nil
  3354. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3355. Use this to specify additional executable file say a jar file.
  3356. When using MathToWeb as the converter, specify the full-path to
  3357. your mathtoweb.jar file."
  3358. :group 'org-latex
  3359. :version "24.1"
  3360. :type '(choice
  3361. (const :tag "None" nil)
  3362. (file :tag "JAR file" :must-match t)))
  3363. (defcustom org-latex-to-mathml-convert-command nil
  3364. "Command to convert LaTeX fragments to MathML.
  3365. Replace format-specifiers in the command as noted below and use
  3366. `shell-command' to convert LaTeX to MathML.
  3367. %j: Executable file in fully expanded form as specified by
  3368. `org-latex-to-mathml-jar-file'.
  3369. %I: Input LaTeX file in fully expanded form
  3370. %o: Output MathML file
  3371. This command is used by `org-create-math-formula'.
  3372. When using MathToWeb as the converter, set this to
  3373. \"java -jar %j -unicode -force -df %o %I\"."
  3374. :group 'org-latex
  3375. :version "24.1"
  3376. :type '(choice
  3377. (const :tag "None" nil)
  3378. (string :tag "\nShell command")))
  3379. (defcustom org-latex-create-formula-image-program 'dvipng
  3380. "Program to convert LaTeX fragments with.
  3381. dvipng Process the LaTeX fragments to dvi file, then convert
  3382. dvi files to png files using dvipng.
  3383. This will also include processing of non-math environments.
  3384. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3385. to convert pdf files to png files"
  3386. :group 'org-latex
  3387. :version "24.1"
  3388. :type '(choice
  3389. (const :tag "dvipng" dvipng)
  3390. (const :tag "imagemagick" imagemagick)))
  3391. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3392. "Path to store latex preview images.
  3393. A relative path here creates many directories relative to the
  3394. processed org files paths. An absolute path puts all preview
  3395. images at the same place."
  3396. :group 'org-latex
  3397. :version "24.3"
  3398. :type 'string)
  3399. (defun org-format-latex-mathml-available-p ()
  3400. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3401. (save-match-data
  3402. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3403. org-latex-to-mathml-convert-command)
  3404. (let ((executable (car (split-string
  3405. org-latex-to-mathml-convert-command))))
  3406. (when (executable-find executable)
  3407. (if (string-match
  3408. "%j" org-latex-to-mathml-convert-command)
  3409. (file-readable-p org-latex-to-mathml-jar-file)
  3410. t))))))
  3411. (defcustom org-format-latex-header "\\documentclass{article}
  3412. \\usepackage[usenames]{color}
  3413. \[PACKAGES]
  3414. \[DEFAULT-PACKAGES]
  3415. \\pagestyle{empty} % do not remove
  3416. % The settings below are copied from fullpage.sty
  3417. \\setlength{\\textwidth}{\\paperwidth}
  3418. \\addtolength{\\textwidth}{-3cm}
  3419. \\setlength{\\oddsidemargin}{1.5cm}
  3420. \\addtolength{\\oddsidemargin}{-2.54cm}
  3421. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3422. \\setlength{\\textheight}{\\paperheight}
  3423. \\addtolength{\\textheight}{-\\headheight}
  3424. \\addtolength{\\textheight}{-\\headsep}
  3425. \\addtolength{\\textheight}{-\\footskip}
  3426. \\addtolength{\\textheight}{-3cm}
  3427. \\setlength{\\topmargin}{1.5cm}
  3428. \\addtolength{\\topmargin}{-2.54cm}"
  3429. "The document header used for processing LaTeX fragments.
  3430. It is imperative that this header make sure that no page number
  3431. appears on the page. The package defined in the variables
  3432. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3433. will either replace the placeholder \"[PACKAGES]\" in this
  3434. header, or they will be appended."
  3435. :group 'org-latex
  3436. :type 'string)
  3437. (defun org-set-packages-alist (var val)
  3438. "Set the packages alist and make sure it has 3 elements per entry."
  3439. (set var (mapcar (lambda (x)
  3440. (if (and (consp x) (= (length x) 2))
  3441. (list (car x) (nth 1 x) t)
  3442. x))
  3443. val)))
  3444. (defun org-get-packages-alist (var)
  3445. "Get the packages alist and make sure it has 3 elements per entry."
  3446. (mapcar (lambda (x)
  3447. (if (and (consp x) (= (length x) 2))
  3448. (list (car x) (nth 1 x) t)
  3449. x))
  3450. (default-value var)))
  3451. (defcustom org-latex-default-packages-alist
  3452. '(("AUTO" "inputenc" t)
  3453. ("T1" "fontenc" t)
  3454. ("" "fixltx2e" nil)
  3455. ("" "graphicx" t)
  3456. ("" "longtable" nil)
  3457. ("" "float" nil)
  3458. ("" "wrapfig" nil)
  3459. ("" "rotating" nil)
  3460. ("normalem" "ulem" t)
  3461. ("" "amsmath" t)
  3462. ("" "textcomp" t)
  3463. ("" "marvosym" t)
  3464. ("" "wasysym" t)
  3465. ("" "amssymb" t)
  3466. ("" "capt-of" nil)
  3467. ("" "hyperref" nil)
  3468. "\\tolerance=1000")
  3469. "Alist of default packages to be inserted in the header.
  3470. Change this only if one of the packages here causes an
  3471. incompatibility with another package you are using.
  3472. The packages in this list are needed by one part or another of
  3473. Org mode to function properly:
  3474. - inputenc, fontenc: for basic font and character selection
  3475. - fixltx2e: Important patches of LaTeX itself
  3476. - graphicx: for including images
  3477. - longtable: For multipage tables
  3478. - float, wrapfig: for figure placement
  3479. - rotating: for sideways figures and tables
  3480. - ulem: for underline and strike-through
  3481. - amsmath: for subscript and superscript and math environments
  3482. - textcomp, marvosymb, wasysym, amssymb: for various symbols used
  3483. for interpreting the entities in `org-entities'. You can skip
  3484. some of these packages if you don't use any of their symbols.
  3485. - capt-of: for captions outside of floats
  3486. - hyperref: for cross references
  3487. Therefore you should not modify this variable unless you know
  3488. what you are doing. The one reason to change it anyway is that
  3489. you might be loading some other package that conflicts with one
  3490. of the default packages. Each element is either a cell or
  3491. a string.
  3492. A cell is of the format:
  3493. \( \"options\" \"package\" SNIPPET-FLAG).
  3494. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3495. when compiling LaTeX snippets into images for inclusion into
  3496. non-LaTeX output.
  3497. A string will be inserted as-is in the header of the document."
  3498. :group 'org-latex
  3499. :group 'org-export-latex
  3500. :set 'org-set-packages-alist
  3501. :get 'org-get-packages-alist
  3502. :version "24.1"
  3503. :type '(repeat
  3504. (choice
  3505. (list :tag "options/package pair"
  3506. (string :tag "options")
  3507. (string :tag "package")
  3508. (boolean :tag "Snippet"))
  3509. (string :tag "A line of LaTeX"))))
  3510. (defcustom org-latex-packages-alist nil
  3511. "Alist of packages to be inserted in every LaTeX header.
  3512. These will be inserted after `org-latex-default-packages-alist'.
  3513. Each element is either a cell or a string.
  3514. A cell is of the format:
  3515. \(\"options\" \"package\" SNIPPET-FLAG)
  3516. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3517. needed when turning LaTeX snippets into images for inclusion into
  3518. non-LaTeX output.
  3519. A string will be inserted as-is in the header of the document.
  3520. Make sure that you only list packages here which:
  3521. - you want in every file;
  3522. - do not conflict with the setup in `org-format-latex-header';
  3523. - do not conflict with the default packages in
  3524. `org-latex-default-packages-alist'."
  3525. :group 'org-latex
  3526. :group 'org-export-latex
  3527. :set 'org-set-packages-alist
  3528. :get 'org-get-packages-alist
  3529. :type '(repeat
  3530. (choice
  3531. (list :tag "options/package pair"
  3532. (string :tag "options")
  3533. (string :tag "package")
  3534. (boolean :tag "Snippet"))
  3535. (string :tag "A line of LaTeX"))))
  3536. (defgroup org-appearance nil
  3537. "Settings for Org-mode appearance."
  3538. :tag "Org Appearance"
  3539. :group 'org)
  3540. (defcustom org-level-color-stars-only nil
  3541. "Non-nil means fontify only the stars in each headline.
  3542. When nil, the entire headline is fontified.
  3543. Changing it requires restart of `font-lock-mode' to become effective
  3544. also in regions already fontified."
  3545. :group 'org-appearance
  3546. :type 'boolean)
  3547. (defcustom org-hide-leading-stars nil
  3548. "Non-nil means hide the first N-1 stars in a headline.
  3549. This works by using the face `org-hide' for these stars. This
  3550. face is white for a light background, and black for a dark
  3551. background. You may have to customize the face `org-hide' to
  3552. make this work.
  3553. Changing it requires restart of `font-lock-mode' to become effective
  3554. also in regions already fontified.
  3555. You may also set this on a per-file basis by adding one of the following
  3556. lines to the buffer:
  3557. #+STARTUP: hidestars
  3558. #+STARTUP: showstars"
  3559. :group 'org-appearance
  3560. :type 'boolean)
  3561. (defcustom org-hidden-keywords nil
  3562. "List of symbols corresponding to keywords to be hidden the org buffer.
  3563. For example, a value '(title) for this list will make the document's title
  3564. appear in the buffer without the initial #+TITLE: keyword."
  3565. :group 'org-appearance
  3566. :version "24.1"
  3567. :type '(set (const :tag "#+AUTHOR" author)
  3568. (const :tag "#+DATE" date)
  3569. (const :tag "#+EMAIL" email)
  3570. (const :tag "#+TITLE" title)))
  3571. (defcustom org-custom-properties nil
  3572. "List of properties (as strings) with a special meaning.
  3573. The default use of these custom properties is to let the user
  3574. hide them with `org-toggle-custom-properties-visibility'."
  3575. :group 'org-properties
  3576. :group 'org-appearance
  3577. :version "24.3"
  3578. :type '(repeat (string :tag "Property Name")))
  3579. (defcustom org-fontify-done-headline nil
  3580. "Non-nil means change the face of a headline if it is marked DONE.
  3581. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3582. When this is non-nil, the headline after the keyword is set to the
  3583. `org-headline-done' as an additional indication."
  3584. :group 'org-appearance
  3585. :type 'boolean)
  3586. (defcustom org-fontify-emphasized-text t
  3587. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3588. Changing this variable requires a restart of Emacs to take effect."
  3589. :group 'org-appearance
  3590. :type 'boolean)
  3591. (defcustom org-fontify-whole-heading-line nil
  3592. "Non-nil means fontify the whole line for headings.
  3593. This is useful when setting a background color for the
  3594. org-level-* faces."
  3595. :group 'org-appearance
  3596. :type 'boolean)
  3597. (defcustom org-highlight-latex-and-related nil
  3598. "Non-nil means highlight LaTeX related syntax in the buffer.
  3599. When non nil, the value should be a list containing any of the
  3600. following symbols:
  3601. `latex' Highlight LaTeX snippets and environments.
  3602. `script' Highlight subscript and superscript.
  3603. `entities' Highlight entities."
  3604. :group 'org-appearance
  3605. :version "24.4"
  3606. :package-version '(Org . "8.0")
  3607. :type '(choice
  3608. (const :tag "No highlighting" nil)
  3609. (set :greedy t :tag "Highlight"
  3610. (const :tag "LaTeX snippets and environments" latex)
  3611. (const :tag "Subscript and superscript" script)
  3612. (const :tag "Entities" entities))))
  3613. (defcustom org-hide-emphasis-markers nil
  3614. "Non-nil mean font-lock should hide the emphasis marker characters."
  3615. :group 'org-appearance
  3616. :type 'boolean)
  3617. (defcustom org-hide-macro-markers nil
  3618. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3619. :group 'org-appearance
  3620. :type 'boolean)
  3621. (defcustom org-pretty-entities nil
  3622. "Non-nil means show entities as UTF8 characters.
  3623. When nil, the \\name form remains in the buffer."
  3624. :group 'org-appearance
  3625. :version "24.1"
  3626. :type 'boolean)
  3627. (defcustom org-pretty-entities-include-sub-superscripts t
  3628. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3629. :group 'org-appearance
  3630. :version "24.1"
  3631. :type 'boolean)
  3632. (defvar org-emph-re nil
  3633. "Regular expression for matching emphasis.
  3634. After a match, the match groups contain these elements:
  3635. 0 The match of the full regular expression, including the characters
  3636. before and after the proper match
  3637. 1 The character before the proper match, or empty at beginning of line
  3638. 2 The proper match, including the leading and trailing markers
  3639. 3 The leading marker like * or /, indicating the type of highlighting
  3640. 4 The text between the emphasis markers, not including the markers
  3641. 5 The character after the match, empty at the end of a line")
  3642. (defvar org-verbatim-re nil
  3643. "Regular expression for matching verbatim text.")
  3644. (defvar org-emphasis-regexp-components) ; defined just below
  3645. (defvar org-emphasis-alist) ; defined just below
  3646. (defun org-set-emph-re (var val)
  3647. "Set variable and compute the emphasis regular expression."
  3648. (set var val)
  3649. (when (and (boundp 'org-emphasis-alist)
  3650. (boundp 'org-emphasis-regexp-components)
  3651. org-emphasis-alist org-emphasis-regexp-components)
  3652. (let* ((e org-emphasis-regexp-components)
  3653. (pre (car e))
  3654. (post (nth 1 e))
  3655. (border (nth 2 e))
  3656. (body (nth 3 e))
  3657. (nl (nth 4 e))
  3658. (body1 (concat body "*?"))
  3659. (markers (mapconcat 'car org-emphasis-alist ""))
  3660. (vmarkers (mapconcat
  3661. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3662. org-emphasis-alist "")))
  3663. ;; make sure special characters appear at the right position in the class
  3664. (if (string-match "\\^" markers)
  3665. (setq markers (concat (replace-match "" t t markers) "^")))
  3666. (if (string-match "-" markers)
  3667. (setq markers (concat (replace-match "" t t markers) "-")))
  3668. (if (string-match "\\^" vmarkers)
  3669. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3670. (if (string-match "-" vmarkers)
  3671. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3672. (if (> nl 0)
  3673. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3674. (int-to-string nl) "\\}")))
  3675. ;; Make the regexp
  3676. (setq org-emph-re
  3677. (concat "\\([" pre "]\\|^\\)"
  3678. "\\("
  3679. "\\([" markers "]\\)"
  3680. "\\("
  3681. "[^" border "]\\|"
  3682. "[^" border "]"
  3683. body1
  3684. "[^" border "]"
  3685. "\\)"
  3686. "\\3\\)"
  3687. "\\([" post "]\\|$\\)"))
  3688. (setq org-verbatim-re
  3689. (concat "\\([" pre "]\\|^\\)"
  3690. "\\("
  3691. "\\([" vmarkers "]\\)"
  3692. "\\("
  3693. "[^" border "]\\|"
  3694. "[^" border "]"
  3695. body1
  3696. "[^" border "]"
  3697. "\\)"
  3698. "\\3\\)"
  3699. "\\([" post "]\\|$\\)")))))
  3700. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3701. ;; set this option proved cumbersome. See this message/thread:
  3702. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3703. (defvar org-emphasis-regexp-components
  3704. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n,\"'" "." 1)
  3705. "Components used to build the regular expression for emphasis.
  3706. This is a list with five entries. Terminology: In an emphasis string
  3707. like \" *strong word* \", we call the initial space PREMATCH, the final
  3708. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3709. and \"trong wor\" is the body. The different components in this variable
  3710. specify what is allowed/forbidden in each part:
  3711. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3712. post Chars allowed as postmatch. End of line will be allowed too.
  3713. border The chars *forbidden* as border characters.
  3714. body-regexp A regexp like \".\" to match a body character. Don't use
  3715. non-shy groups here, and don't allow newline here.
  3716. newline The maximum number of newlines allowed in an emphasis exp.
  3717. You need to reload Org or to restart Emacs after customizing this.")
  3718. (defcustom org-emphasis-alist
  3719. `(("*" bold)
  3720. ("/" italic)
  3721. ("_" underline)
  3722. ("=" org-verbatim verbatim)
  3723. ("~" org-code verbatim)
  3724. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3725. "Alist of characters and faces to emphasize text.
  3726. Text starting and ending with a special character will be emphasized,
  3727. for example *bold*, _underlined_ and /italic/. This variable sets the
  3728. marker characters and the face to be used by font-lock for highlighting
  3729. in Org-mode Emacs buffers.
  3730. You need to reload Org or to restart Emacs after customizing this."
  3731. :group 'org-appearance
  3732. :set 'org-set-emph-re
  3733. :version "24.4"
  3734. :package-version '(Org . "8.0")
  3735. :type '(repeat
  3736. (list
  3737. (string :tag "Marker character")
  3738. (choice
  3739. (face :tag "Font-lock-face")
  3740. (plist :tag "Face property list"))
  3741. (option (const verbatim)))))
  3742. (defvar org-protecting-blocks
  3743. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3744. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3745. This is needed for font-lock setup.")
  3746. ;;; Miscellaneous options
  3747. (defgroup org-completion nil
  3748. "Completion in Org-mode."
  3749. :tag "Org Completion"
  3750. :group 'org)
  3751. (defcustom org-completion-use-ido nil
  3752. "Non-nil means use ido completion wherever possible.
  3753. Note that `ido-mode' must be active for this variable to be relevant.
  3754. If you decide to turn this variable on, you might well want to turn off
  3755. `org-outline-path-complete-in-steps'.
  3756. See also `org-completion-use-iswitchb'."
  3757. :group 'org-completion
  3758. :type 'boolean)
  3759. (defcustom org-completion-use-iswitchb nil
  3760. "Non-nil means use iswitchb completion wherever possible.
  3761. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3762. If you decide to turn this variable on, you might well want to turn off
  3763. `org-outline-path-complete-in-steps'.
  3764. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3765. :group 'org-completion
  3766. :type 'boolean)
  3767. (defcustom org-completion-fallback-command 'hippie-expand
  3768. "The expansion command called by \\[pcomplete] in normal context.
  3769. Normal means, no org-mode-specific context."
  3770. :group 'org-completion
  3771. :type 'function)
  3772. ;;; Functions and variables from their packages
  3773. ;; Declared here to avoid compiler warnings
  3774. ;; XEmacs only
  3775. (defvar outline-mode-menu-heading)
  3776. (defvar outline-mode-menu-show)
  3777. (defvar outline-mode-menu-hide)
  3778. (defvar zmacs-regions) ; XEmacs regions
  3779. ;; Emacs only
  3780. (defvar mark-active)
  3781. ;; Various packages
  3782. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3783. (declare-function calendar-forward-day "cal-move" (arg))
  3784. (declare-function calendar-goto-date "cal-move" (date))
  3785. (declare-function calendar-goto-today "cal-move" ())
  3786. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3787. (defvar calc-embedded-close-formula)
  3788. (defvar calc-embedded-open-formula)
  3789. (declare-function cdlatex-tab "ext:cdlatex" ())
  3790. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3791. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3792. (defvar font-lock-unfontify-region-function)
  3793. (declare-function iswitchb-read-buffer "iswitchb"
  3794. (prompt &optional default require-match start matches-set))
  3795. (defvar iswitchb-temp-buflist)
  3796. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3797. (defvar org-agenda-tags-todo-honor-ignore-options)
  3798. (declare-function org-agenda-skip "org-agenda" ())
  3799. (declare-function
  3800. org-agenda-format-item "org-agenda"
  3801. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3802. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3803. (declare-function org-agenda-change-all-lines "org-agenda"
  3804. (newhead hdmarker &optional fixface just-this))
  3805. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3806. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3807. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3808. (beg end))
  3809. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3810. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3811. "org-agenda" (&optional end))
  3812. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3813. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3814. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3815. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3816. (declare-function org-indent-mode "org-indent" (&optional arg))
  3817. (declare-function parse-time-string "parse-time" (string))
  3818. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3819. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3820. (defvar remember-data-file)
  3821. (defvar texmathp-why)
  3822. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3823. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3824. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3825. ;;;###autoload
  3826. (defun turn-on-orgtbl ()
  3827. "Unconditionally turn on `orgtbl-mode'."
  3828. (require 'org-table)
  3829. (orgtbl-mode 1))
  3830. (defun org-at-table-p (&optional table-type)
  3831. "Return t if the cursor is inside an org-type table.
  3832. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3833. (if org-enable-table-editor
  3834. (save-excursion
  3835. (beginning-of-line 1)
  3836. (looking-at (if table-type org-table-any-line-regexp
  3837. org-table-line-regexp)))
  3838. nil))
  3839. (defsubst org-table-p () (org-at-table-p))
  3840. (defun org-at-table.el-p ()
  3841. "Return t if and only if we are at a table.el table."
  3842. (and (org-at-table-p 'any)
  3843. (save-excursion
  3844. (goto-char (org-table-begin 'any))
  3845. (looking-at org-table1-hline-regexp))))
  3846. (defun org-table-recognize-table.el ()
  3847. "If there is a table.el table nearby, recognize it and move into it."
  3848. (if org-table-tab-recognizes-table.el
  3849. (if (org-at-table.el-p)
  3850. (progn
  3851. (beginning-of-line 1)
  3852. (if (looking-at org-table-dataline-regexp)
  3853. nil
  3854. (if (looking-at org-table1-hline-regexp)
  3855. (progn
  3856. (beginning-of-line 2)
  3857. (if (looking-at org-table-any-border-regexp)
  3858. (beginning-of-line -1)))))
  3859. (if (re-search-forward "|" (org-table-end t) t)
  3860. (progn
  3861. (require 'table)
  3862. (if (table--at-cell-p (point))
  3863. t
  3864. (message "recognizing table.el table...")
  3865. (table-recognize-table)
  3866. (message "recognizing table.el table...done")))
  3867. (error "This should not happen"))
  3868. t)
  3869. nil)
  3870. nil))
  3871. (defun org-at-table-hline-p ()
  3872. "Return t if the cursor is inside a hline in a table."
  3873. (if org-enable-table-editor
  3874. (save-excursion
  3875. (beginning-of-line 1)
  3876. (looking-at org-table-hline-regexp))
  3877. nil))
  3878. (defun org-table-map-tables (function &optional quietly)
  3879. "Apply FUNCTION to the start of all tables in the buffer."
  3880. (save-excursion
  3881. (save-restriction
  3882. (widen)
  3883. (goto-char (point-min))
  3884. (while (re-search-forward org-table-any-line-regexp nil t)
  3885. (unless quietly
  3886. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3887. (beginning-of-line 1)
  3888. (when (and (looking-at org-table-line-regexp)
  3889. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3890. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3891. (save-excursion (funcall function))
  3892. (or (looking-at org-table-line-regexp)
  3893. (forward-char 1)))
  3894. (re-search-forward org-table-any-border-regexp nil 1))))
  3895. (unless quietly (message "Mapping tables: done")))
  3896. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3897. (declare-function org-clock-update-mode-line "org-clock" ())
  3898. (declare-function org-resolve-clocks "org-clock"
  3899. (&optional also-non-dangling-p prompt last-valid))
  3900. (defun org-at-TBLFM-p (&optional pos)
  3901. "Return t when point (or POS) is in #+TBLFM line."
  3902. (save-excursion
  3903. (let ((pos pos)))
  3904. (goto-char (or pos (point)))
  3905. (beginning-of-line 1)
  3906. (looking-at org-TBLFM-regexp)))
  3907. (defvar org-clock-start-time)
  3908. (defvar org-clock-marker (make-marker)
  3909. "Marker recording the last clock-in.")
  3910. (defvar org-clock-hd-marker (make-marker)
  3911. "Marker recording the last clock-in, but the headline position.")
  3912. (defvar org-clock-heading ""
  3913. "The heading of the current clock entry.")
  3914. (defun org-clock-is-active ()
  3915. "Return the buffer where the clock is currently running.
  3916. Return nil if no clock is running."
  3917. (marker-buffer org-clock-marker))
  3918. (defun org-check-running-clock ()
  3919. "Check if the current buffer contains the running clock.
  3920. If yes, offer to stop it and to save the buffer with the changes."
  3921. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3922. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3923. (buffer-name))))
  3924. (org-clock-out)
  3925. (when (y-or-n-p "Save changed buffer?")
  3926. (save-buffer))))
  3927. (defun org-clocktable-try-shift (dir n)
  3928. "Check if this line starts a clock table, if yes, shift the time block."
  3929. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3930. (org-clocktable-shift dir n)))
  3931. ;;;###autoload
  3932. (defun org-clock-persistence-insinuate ()
  3933. "Set up hooks for clock persistence."
  3934. (require 'org-clock)
  3935. (add-hook 'org-mode-hook 'org-clock-load)
  3936. (add-hook 'kill-emacs-hook 'org-clock-save))
  3937. (defgroup org-archive nil
  3938. "Options concerning archiving in Org-mode."
  3939. :tag "Org Archive"
  3940. :group 'org-structure)
  3941. (defcustom org-archive-location "%s_archive::"
  3942. "The location where subtrees should be archived.
  3943. The value of this variable is a string, consisting of two parts,
  3944. separated by a double-colon. The first part is a filename and
  3945. the second part is a headline.
  3946. When the filename is omitted, archiving happens in the same file.
  3947. %s in the filename will be replaced by the current file
  3948. name (without the directory part). Archiving to a different file
  3949. is useful to keep archived entries from contributing to the
  3950. Org-mode Agenda.
  3951. The archived entries will be filed as subtrees of the specified
  3952. headline. When the headline is omitted, the subtrees are simply
  3953. filed away at the end of the file, as top-level entries. Also in
  3954. the heading you can use %s to represent the file name, this can be
  3955. useful when using the same archive for a number of different files.
  3956. Here are a few examples:
  3957. \"%s_archive::\"
  3958. If the current file is Projects.org, archive in file
  3959. Projects.org_archive, as top-level trees. This is the default.
  3960. \"::* Archived Tasks\"
  3961. Archive in the current file, under the top-level headline
  3962. \"* Archived Tasks\".
  3963. \"~/org/archive.org::\"
  3964. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3965. \"~/org/archive.org::* From %s\"
  3966. Archive in file ~/org/archive.org (absolute path), under headlines
  3967. \"From FILENAME\" where file name is the current file name.
  3968. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3969. The \"datetree/\" string is special, signifying to archive
  3970. items to the datetree. Items are placed in either the CLOSED
  3971. date of the item, or the current date if there is no CLOSED date.
  3972. The heading will be a subentry to the current date. There doesn't
  3973. need to be a heading, but there always needs to be a slash after
  3974. datetree. For example, to store archived items directly in the
  3975. datetree, use \"~/org/datetree.org::datetree/\".
  3976. \"basement::** Finished Tasks\"
  3977. Archive in file ./basement (relative path), as level 3 trees
  3978. below the level 2 heading \"** Finished Tasks\".
  3979. You may set this option on a per-file basis by adding to the buffer a
  3980. line like
  3981. #+ARCHIVE: basement::** Finished Tasks
  3982. You may also define it locally for a subtree by setting an ARCHIVE property
  3983. in the entry. If such a property is found in an entry, or anywhere up
  3984. the hierarchy, it will be used."
  3985. :group 'org-archive
  3986. :type 'string)
  3987. (defcustom org-agenda-skip-archived-trees t
  3988. "Non-nil means the agenda will skip any items located in archived trees.
  3989. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3990. variable is no longer recommended, you should leave it at the value t.
  3991. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3992. :group 'org-archive
  3993. :group 'org-agenda-skip
  3994. :type 'boolean)
  3995. (defcustom org-columns-skip-archived-trees t
  3996. "Non-nil means ignore archived trees when creating column view."
  3997. :group 'org-archive
  3998. :group 'org-properties
  3999. :type 'boolean)
  4000. (defcustom org-cycle-open-archived-trees nil
  4001. "Non-nil means `org-cycle' will open archived trees.
  4002. An archived tree is a tree marked with the tag ARCHIVE.
  4003. When nil, archived trees will stay folded. You can still open them with
  4004. normal outline commands like `show-all', but not with the cycling commands."
  4005. :group 'org-archive
  4006. :group 'org-cycle
  4007. :type 'boolean)
  4008. (defcustom org-sparse-tree-open-archived-trees nil
  4009. "Non-nil means sparse tree construction shows matches in archived trees.
  4010. When nil, matches in these trees are highlighted, but the trees are kept in
  4011. collapsed state."
  4012. :group 'org-archive
  4013. :group 'org-sparse-trees
  4014. :type 'boolean)
  4015. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  4016. "The default date type when building a sparse tree.
  4017. When this is nil, a date is a scheduled or a deadline timestamp.
  4018. Otherwise, these types are allowed:
  4019. all: all timestamps
  4020. active: only active timestamps (<...>)
  4021. inactive: only inactive timestamps (<...)
  4022. scheduled: only scheduled timestamps
  4023. deadline: only deadline timestamps"
  4024. :type '(choice (const :tag "Scheduled or deadline" scheduled-or-deadline)
  4025. (const :tag "All timestamps" all)
  4026. (const :tag "Only active timestamps" active)
  4027. (const :tag "Only inactive timestamps" inactive)
  4028. (const :tag "Only scheduled timestamps" scheduled)
  4029. (const :tag "Only deadline timestamps" deadline)
  4030. (const :tag "Only closed timestamps" closed))
  4031. :version "24.3"
  4032. :group 'org-sparse-trees)
  4033. (defun org-cycle-hide-archived-subtrees (state)
  4034. "Re-hide all archived subtrees after a visibility state change."
  4035. (when (and (not org-cycle-open-archived-trees)
  4036. (not (memq state '(overview folded))))
  4037. (save-excursion
  4038. (let* ((globalp (memq state '(contents all)))
  4039. (beg (if globalp (point-min) (point)))
  4040. (end (if globalp (point-max) (org-end-of-subtree t))))
  4041. (org-hide-archived-subtrees beg end)
  4042. (goto-char beg)
  4043. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4044. (message "%s" (substitute-command-keys
  4045. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4046. (defun org-force-cycle-archived ()
  4047. "Cycle subtree even if it is archived."
  4048. (interactive)
  4049. (setq this-command 'org-cycle)
  4050. (let ((org-cycle-open-archived-trees t))
  4051. (call-interactively 'org-cycle)))
  4052. (defun org-hide-archived-subtrees (beg end)
  4053. "Re-hide all archived subtrees after a visibility state change."
  4054. (save-excursion
  4055. (let* ((re (concat ":" org-archive-tag ":")))
  4056. (goto-char beg)
  4057. (while (re-search-forward re end t)
  4058. (when (org-at-heading-p)
  4059. (org-flag-subtree t)
  4060. (org-end-of-subtree t))))))
  4061. (declare-function outline-end-of-heading "outline" ())
  4062. (declare-function outline-flag-region "outline" (from to flag))
  4063. (defun org-flag-subtree (flag)
  4064. (save-excursion
  4065. (org-back-to-heading t)
  4066. (outline-end-of-heading)
  4067. (outline-flag-region (point)
  4068. (progn (org-end-of-subtree t) (point))
  4069. flag)))
  4070. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4071. ;; Declare Column View Code
  4072. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  4073. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4074. (declare-function org-columns-compute "org-colview" (property))
  4075. ;; Declare ID code
  4076. (declare-function org-id-store-link "org-id")
  4077. (declare-function org-id-locations-load "org-id")
  4078. (declare-function org-id-locations-save "org-id")
  4079. (defvar org-id-track-globally)
  4080. ;;; Variables for pre-computed regular expressions, all buffer local
  4081. (defvar org-todo-regexp nil
  4082. "Matches any of the TODO state keywords.")
  4083. (make-variable-buffer-local 'org-todo-regexp)
  4084. (defvar org-not-done-regexp nil
  4085. "Matches any of the TODO state keywords except the last one.")
  4086. (make-variable-buffer-local 'org-not-done-regexp)
  4087. (defvar org-not-done-heading-regexp nil
  4088. "Matches a TODO headline that is not done.")
  4089. (make-variable-buffer-local 'org-not-done-regexp)
  4090. (defvar org-todo-line-regexp nil
  4091. "Matches a headline and puts TODO state into group 2 if present.")
  4092. (make-variable-buffer-local 'org-todo-line-regexp)
  4093. (defvar org-complex-heading-regexp nil
  4094. "Matches a headline and puts everything into groups:
  4095. group 1: the stars
  4096. group 2: The todo keyword, maybe
  4097. group 3: Priority cookie
  4098. group 4: True headline
  4099. group 5: Tags")
  4100. (make-variable-buffer-local 'org-complex-heading-regexp)
  4101. (defvar org-complex-heading-regexp-format nil
  4102. "Printf format to make regexp to match an exact headline.
  4103. This regexp will match the headline of any node which has the
  4104. exact headline text that is put into the format, but may have any
  4105. TODO state, priority and tags.")
  4106. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4107. (defvar org-todo-line-tags-regexp nil
  4108. "Matches a headline and puts TODO state into group 2 if present.
  4109. Also put tags into group 4 if tags are present.")
  4110. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4111. (defvar org-ds-keyword-length 12
  4112. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4113. (make-variable-buffer-local 'org-ds-keyword-length)
  4114. (defvar org-deadline-regexp nil
  4115. "Matches the DEADLINE keyword.")
  4116. (make-variable-buffer-local 'org-deadline-regexp)
  4117. (defvar org-deadline-time-regexp nil
  4118. "Matches the DEADLINE keyword together with a time stamp.")
  4119. (make-variable-buffer-local 'org-deadline-time-regexp)
  4120. (defvar org-deadline-time-hour-regexp nil
  4121. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4122. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4123. (defvar org-deadline-line-regexp nil
  4124. "Matches the DEADLINE keyword and the rest of the line.")
  4125. (make-variable-buffer-local 'org-deadline-line-regexp)
  4126. (defvar org-scheduled-regexp nil
  4127. "Matches the SCHEDULED keyword.")
  4128. (make-variable-buffer-local 'org-scheduled-regexp)
  4129. (defvar org-scheduled-time-regexp nil
  4130. "Matches the SCHEDULED keyword together with a time stamp.")
  4131. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4132. (defvar org-scheduled-time-hour-regexp nil
  4133. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4134. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4135. (defvar org-closed-time-regexp nil
  4136. "Matches the CLOSED keyword together with a time stamp.")
  4137. (make-variable-buffer-local 'org-closed-time-regexp)
  4138. (defvar org-keyword-time-regexp nil
  4139. "Matches any of the 4 keywords, together with the time stamp.")
  4140. (make-variable-buffer-local 'org-keyword-time-regexp)
  4141. (defvar org-keyword-time-not-clock-regexp nil
  4142. "Matches any of the 3 keywords, together with the time stamp.")
  4143. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4144. (defvar org-maybe-keyword-time-regexp nil
  4145. "Matches a timestamp, possibly preceded by a keyword.")
  4146. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4147. (defvar org-all-time-keywords nil
  4148. "List of time keywords.")
  4149. (make-variable-buffer-local 'org-all-time-keywords)
  4150. (defconst org-plain-time-of-day-regexp
  4151. (concat
  4152. "\\(\\<[012]?[0-9]"
  4153. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4154. "\\(--?"
  4155. "\\(\\<[012]?[0-9]"
  4156. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4157. "\\)?")
  4158. "Regular expression to match a plain time or time range.
  4159. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4160. groups carry important information:
  4161. 0 the full match
  4162. 1 the first time, range or not
  4163. 8 the second time, if it is a range.")
  4164. (defconst org-plain-time-extension-regexp
  4165. (concat
  4166. "\\(\\<[012]?[0-9]"
  4167. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4168. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4169. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4170. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4171. groups carry important information:
  4172. 0 the full match
  4173. 7 hours of duration
  4174. 9 minutes of duration")
  4175. (defconst org-stamp-time-of-day-regexp
  4176. (concat
  4177. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4178. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4179. "\\(--?"
  4180. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4181. "Regular expression to match a timestamp time or time range.
  4182. After a match, the following groups carry important information:
  4183. 0 the full match
  4184. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4185. 2 the first time, range or not
  4186. 4 the second time, if it is a range.")
  4187. (defconst org-startup-options
  4188. '(("fold" org-startup-folded t)
  4189. ("overview" org-startup-folded t)
  4190. ("nofold" org-startup-folded nil)
  4191. ("showall" org-startup-folded nil)
  4192. ("showeverything" org-startup-folded showeverything)
  4193. ("content" org-startup-folded content)
  4194. ("indent" org-startup-indented t)
  4195. ("noindent" org-startup-indented nil)
  4196. ("hidestars" org-hide-leading-stars t)
  4197. ("showstars" org-hide-leading-stars nil)
  4198. ("odd" org-odd-levels-only t)
  4199. ("oddeven" org-odd-levels-only nil)
  4200. ("align" org-startup-align-all-tables t)
  4201. ("noalign" org-startup-align-all-tables nil)
  4202. ("inlineimages" org-startup-with-inline-images t)
  4203. ("noinlineimages" org-startup-with-inline-images nil)
  4204. ("latexpreview" org-startup-with-latex-preview t)
  4205. ("nolatexpreview" org-startup-with-latex-preview nil)
  4206. ("customtime" org-display-custom-times t)
  4207. ("logdone" org-log-done time)
  4208. ("lognotedone" org-log-done note)
  4209. ("nologdone" org-log-done nil)
  4210. ("lognoteclock-out" org-log-note-clock-out t)
  4211. ("nolognoteclock-out" org-log-note-clock-out nil)
  4212. ("logrepeat" org-log-repeat state)
  4213. ("lognoterepeat" org-log-repeat note)
  4214. ("logdrawer" org-log-into-drawer t)
  4215. ("nologdrawer" org-log-into-drawer nil)
  4216. ("logstatesreversed" org-log-states-order-reversed t)
  4217. ("nologstatesreversed" org-log-states-order-reversed nil)
  4218. ("nologrepeat" org-log-repeat nil)
  4219. ("logreschedule" org-log-reschedule time)
  4220. ("lognotereschedule" org-log-reschedule note)
  4221. ("nologreschedule" org-log-reschedule nil)
  4222. ("logredeadline" org-log-redeadline time)
  4223. ("lognoteredeadline" org-log-redeadline note)
  4224. ("nologredeadline" org-log-redeadline nil)
  4225. ("logrefile" org-log-refile time)
  4226. ("lognoterefile" org-log-refile note)
  4227. ("nologrefile" org-log-refile nil)
  4228. ("fninline" org-footnote-define-inline t)
  4229. ("nofninline" org-footnote-define-inline nil)
  4230. ("fnlocal" org-footnote-section nil)
  4231. ("fnauto" org-footnote-auto-label t)
  4232. ("fnprompt" org-footnote-auto-label nil)
  4233. ("fnconfirm" org-footnote-auto-label confirm)
  4234. ("fnplain" org-footnote-auto-label plain)
  4235. ("fnadjust" org-footnote-auto-adjust t)
  4236. ("nofnadjust" org-footnote-auto-adjust nil)
  4237. ("constcgs" constants-unit-system cgs)
  4238. ("constSI" constants-unit-system SI)
  4239. ("noptag" org-tag-persistent-alist nil)
  4240. ("hideblocks" org-hide-block-startup t)
  4241. ("nohideblocks" org-hide-block-startup nil)
  4242. ("beamer" org-startup-with-beamer-mode t)
  4243. ("entitiespretty" org-pretty-entities t)
  4244. ("entitiesplain" org-pretty-entities nil))
  4245. "Variable associated with STARTUP options for org-mode.
  4246. Each element is a list of three items: the startup options (as written
  4247. in the #+STARTUP line), the corresponding variable, and the value to set
  4248. this variable to if the option is found. An optional forth element PUSH
  4249. means to push this value onto the list in the variable.")
  4250. (defun org-update-property-plist (key val props)
  4251. "Update PROPS with KEY and VAL."
  4252. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4253. (key (if appending (substring key 0 (- (length key) 1)) key))
  4254. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4255. (previous (cdr (assoc key props))))
  4256. (if appending
  4257. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4258. (cons (cons key val) remainder))))
  4259. (defcustom org-group-tags t
  4260. "When non-nil (the default), use group tags.
  4261. This can be turned on/off through `org-toggle-tags-groups'."
  4262. :group 'org-tags
  4263. :group 'org-startup
  4264. :type 'boolean)
  4265. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4266. (defun org-toggle-tags-groups ()
  4267. "Toggle support for group tags.
  4268. Support for group tags is controlled by the option
  4269. `org-group-tags', which is non-nil by default."
  4270. (interactive)
  4271. (setq org-group-tags (not org-group-tags))
  4272. (cond ((and (derived-mode-p 'org-agenda-mode)
  4273. org-group-tags)
  4274. (org-agenda-redo))
  4275. ((derived-mode-p 'org-mode)
  4276. (let ((org-inhibit-startup t)) (org-mode))))
  4277. (message "Groups tags support has been turned %s"
  4278. (if org-group-tags "on" "off")))
  4279. (defun org-set-regexps-and-options-for-tags ()
  4280. "Precompute variables used for tags."
  4281. (when (derived-mode-p 'org-mode)
  4282. (org-set-local 'org-file-tags nil)
  4283. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4284. (splitre "[ \t]+")
  4285. (start 0)
  4286. tags ftags key value)
  4287. (save-excursion
  4288. (save-restriction
  4289. (widen)
  4290. (goto-char (point-min))
  4291. (while (re-search-forward re nil t)
  4292. (setq key (upcase (org-match-string-no-properties 1))
  4293. value (org-match-string-no-properties 2))
  4294. (if (stringp value) (setq value (org-trim value)))
  4295. (cond
  4296. ((equal key "TAGS")
  4297. (setq tags (append tags (if tags '("\\n") nil)
  4298. (org-split-string value splitre))))
  4299. ((equal key "FILETAGS")
  4300. (when (string-match "\\S-" value)
  4301. (setq ftags
  4302. (append
  4303. ftags
  4304. (apply 'append
  4305. (mapcar (lambda (x) (org-split-string x ":"))
  4306. (org-split-string value)))))))))))
  4307. ;; Process the file tags.
  4308. (and ftags (org-set-local 'org-file-tags
  4309. (mapcar 'org-add-prop-inherited ftags)))
  4310. (org-set-local 'org-tag-groups-alist nil)
  4311. ;; Process the tags.
  4312. (when (and (not tags) org-tag-alist)
  4313. (setq tags
  4314. (mapcar
  4315. (lambda (tg) (cond ((eq (car tg) :startgroup) "{")
  4316. ((eq (car tg) :endgroup) "}")
  4317. ((eq (car tg) :grouptags) ":")
  4318. ((eq (car tg) :newline) "\n")
  4319. (t (concat (car tg)
  4320. (if (characterp (cdr tg))
  4321. (format "(%s)" (char-to-string (cdr tg))) "")))))
  4322. org-tag-alist)))
  4323. (let (e tgs g)
  4324. (while (setq e (pop tags))
  4325. (cond
  4326. ((equal e "{")
  4327. (progn (push '(:startgroup) tgs)
  4328. (when (equal (nth 1 tags) ":")
  4329. (push (list (replace-regexp-in-string
  4330. "(.+)$" "" (nth 0 tags)))
  4331. org-tag-groups-alist)
  4332. (setq g 0))))
  4333. ((equal e ":") (push '(:grouptags) tgs))
  4334. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4335. ((equal e "\\n") (push '(:newline) tgs))
  4336. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4337. (push (cons (match-string 1 e)
  4338. (string-to-char (match-string 2 e))) tgs)
  4339. (if (and g (> g 0))
  4340. (setcar org-tag-groups-alist
  4341. (append (car org-tag-groups-alist)
  4342. (list (match-string 1 e)))))
  4343. (if g (setq g (1+ g))))
  4344. (t (push (list e) tgs)
  4345. (if (and g (> g 0))
  4346. (setcar org-tag-groups-alist
  4347. (append (car org-tag-groups-alist) (list e))))
  4348. (if g (setq g (1+ g))))))
  4349. (org-set-local 'org-tag-alist nil)
  4350. (while (setq e (pop tgs))
  4351. (or (and (stringp (car e))
  4352. (assoc (car e) org-tag-alist))
  4353. (push e org-tag-alist)))
  4354. ;; Return a list with tag variables
  4355. (list org-file-tags org-tag-alist org-tag-groups-alist)))))
  4356. (defvar org-ota nil)
  4357. (defun org-set-regexps-and-options ()
  4358. "Precompute regular expressions used in the current buffer."
  4359. (when (derived-mode-p 'org-mode)
  4360. (org-set-local 'org-todo-kwd-alist nil)
  4361. (org-set-local 'org-todo-key-alist nil)
  4362. (org-set-local 'org-todo-key-trigger nil)
  4363. (org-set-local 'org-todo-keywords-1 nil)
  4364. (org-set-local 'org-done-keywords nil)
  4365. (org-set-local 'org-todo-heads nil)
  4366. (org-set-local 'org-todo-sets nil)
  4367. (org-set-local 'org-todo-log-states nil)
  4368. (org-set-local 'org-file-properties nil)
  4369. (let ((re (org-make-options-regexp
  4370. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4371. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4372. "SETUPFILE" "OPTIONS")
  4373. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4374. (splitre "[ \t]+")
  4375. (scripts org-use-sub-superscripts)
  4376. kwds kws0 kwsa key log value cat arch const links hw dws
  4377. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4378. (start 0))
  4379. (save-excursion
  4380. (save-restriction
  4381. (widen)
  4382. (goto-char (point-min))
  4383. (while
  4384. (or (and
  4385. ext-setup-or-nil
  4386. (not org-ota)
  4387. (let (ret)
  4388. (with-temp-buffer
  4389. (insert ext-setup-or-nil)
  4390. (let ((major-mode 'org-mode) org-ota)
  4391. (setq ret (save-match-data
  4392. (org-set-regexps-and-options-for-tags)))))
  4393. ;; Append setupfile tags to existing tags
  4394. (setq org-ota t)
  4395. (setq org-file-tags
  4396. (delq nil (append org-file-tags (nth 0 ret)))
  4397. org-tag-alist
  4398. (delq nil (append org-tag-alist (nth 1 ret)))
  4399. org-tag-groups-alist
  4400. (delq nil (append org-tag-groups-alist (nth 2 ret))))))
  4401. (and ext-setup-or-nil
  4402. (string-match re ext-setup-or-nil start)
  4403. (setq start (match-end 0)))
  4404. (and (setq ext-setup-or-nil nil start 0)
  4405. (re-search-forward re nil t)))
  4406. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4407. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4408. (if (stringp value) (setq value (org-trim value)))
  4409. (cond
  4410. ((equal key "CATEGORY")
  4411. (setq cat value))
  4412. ((member key '("SEQ_TODO" "TODO"))
  4413. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4414. ((equal key "TYP_TODO")
  4415. (push (cons 'type (org-split-string value splitre)) kwds))
  4416. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4417. ;; general TODO-like setup
  4418. (push (cons (intern (downcase (match-string 1 key)))
  4419. (org-split-string value splitre)) kwds))
  4420. ((equal key "COLUMNS")
  4421. (org-set-local 'org-columns-default-format value))
  4422. ((equal key "LINK")
  4423. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4424. (push (cons (match-string 1 value)
  4425. (org-trim (match-string 2 value)))
  4426. links)))
  4427. ((equal key "PRIORITIES")
  4428. (setq prio (org-split-string value " +")))
  4429. ((equal key "PROPERTY")
  4430. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4431. (setq props (org-update-property-plist (match-string 1 value)
  4432. (match-string 2 value)
  4433. props))))
  4434. ((equal key "CONSTANTS")
  4435. (org-table-set-constants))
  4436. ((equal key "STARTUP")
  4437. (let ((opts (org-split-string value splitre))
  4438. l var val)
  4439. (while (setq l (pop opts))
  4440. (when (setq l (assoc l org-startup-options))
  4441. (setq var (nth 1 l) val (nth 2 l))
  4442. (if (not (nth 3 l))
  4443. (set (make-local-variable var) val)
  4444. (if (not (listp (symbol-value var)))
  4445. (set (make-local-variable var) nil))
  4446. (set (make-local-variable var) (symbol-value var))
  4447. (add-to-list var val))))))
  4448. ((equal key "ARCHIVE")
  4449. (setq arch value)
  4450. (remove-text-properties 0 (length arch)
  4451. '(face t fontified t) arch))
  4452. ((equal key "OPTIONS")
  4453. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4454. (setq scripts (read (match-string 2 value)))))
  4455. ((and (equal key "SETUPFILE")
  4456. ;; Prevent checking in Gnus messages
  4457. (not buffer-read-only))
  4458. (setq setup-contents (org-file-contents
  4459. (expand-file-name
  4460. (org-remove-double-quotes value))
  4461. 'noerror))
  4462. (if (not ext-setup-or-nil)
  4463. (setq ext-setup-or-nil setup-contents start 0)
  4464. (setq ext-setup-or-nil
  4465. (concat (substring ext-setup-or-nil 0 start)
  4466. "\n" setup-contents "\n"
  4467. (substring ext-setup-or-nil start)))))))
  4468. ;; search for property blocks
  4469. (goto-char (point-min))
  4470. (while (re-search-forward org-block-regexp nil t)
  4471. (when (equal "PROPERTY" (upcase (match-string 1)))
  4472. (setq value (replace-regexp-in-string
  4473. "[\n\r]" " " (match-string 4)))
  4474. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4475. (setq props (org-update-property-plist (match-string 1 value)
  4476. (match-string 2 value)
  4477. props)))))))
  4478. (org-set-local 'org-use-sub-superscripts scripts)
  4479. (when cat
  4480. (org-set-local 'org-category (intern cat))
  4481. (push (cons "CATEGORY" cat) props))
  4482. (when prio
  4483. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4484. (setq prio (mapcar 'string-to-char prio))
  4485. (org-set-local 'org-highest-priority (nth 0 prio))
  4486. (org-set-local 'org-lowest-priority (nth 1 prio))
  4487. (org-set-local 'org-default-priority (nth 2 prio)))
  4488. (and props (org-set-local 'org-file-properties (nreverse props)))
  4489. (and arch (org-set-local 'org-archive-location arch))
  4490. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4491. ;; Process the TODO keywords
  4492. (unless kwds
  4493. ;; Use the global values as if they had been given locally.
  4494. (setq kwds (default-value 'org-todo-keywords))
  4495. (if (stringp (car kwds))
  4496. (setq kwds (list (cons org-todo-interpretation
  4497. (default-value 'org-todo-keywords)))))
  4498. (setq kwds (reverse kwds)))
  4499. (setq kwds (nreverse kwds))
  4500. (let (inter kws kw)
  4501. (while (setq kws (pop kwds))
  4502. (let ((kws (or
  4503. (run-hook-with-args-until-success
  4504. 'org-todo-setup-filter-hook kws)
  4505. kws)))
  4506. (setq inter (pop kws) sep (member "|" kws)
  4507. kws0 (delete "|" (copy-sequence kws))
  4508. kwsa nil
  4509. kws1 (mapcar
  4510. (lambda (x)
  4511. ;; 1 2
  4512. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4513. (progn
  4514. (setq kw (match-string 1 x)
  4515. key (and (match-end 2) (match-string 2 x))
  4516. log (org-extract-log-state-settings x))
  4517. (push (cons kw (and key (string-to-char key))) kwsa)
  4518. (and log (push log org-todo-log-states))
  4519. kw)
  4520. (error "Invalid TODO keyword %s" x)))
  4521. kws0)
  4522. kwsa (if kwsa (append '((:startgroup))
  4523. (nreverse kwsa)
  4524. '((:endgroup))))
  4525. hw (car kws1)
  4526. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4527. tail (list inter hw (car dws) (org-last dws))))
  4528. (add-to-list 'org-todo-heads hw 'append)
  4529. (push kws1 org-todo-sets)
  4530. (setq org-done-keywords (append org-done-keywords dws nil))
  4531. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4532. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4533. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4534. (setq org-todo-sets (nreverse org-todo-sets)
  4535. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4536. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4537. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4538. ;; Compute the regular expressions and other local variables.
  4539. ;; Using `org-outline-regexp-bol' would complicate them much,
  4540. ;; because of the fixed white space at the end of that string.
  4541. (if (not org-done-keywords)
  4542. (setq org-done-keywords (and org-todo-keywords-1
  4543. (list (org-last org-todo-keywords-1)))))
  4544. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4545. (length org-scheduled-string)
  4546. (length org-clock-string)
  4547. (length org-closed-string)))
  4548. org-not-done-keywords
  4549. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4550. org-todo-regexp
  4551. (concat "\\("
  4552. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4553. "\\)")
  4554. org-not-done-regexp
  4555. (concat "\\("
  4556. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4557. "\\)")
  4558. org-not-done-heading-regexp
  4559. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4560. org-todo-line-regexp
  4561. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4562. org-complex-heading-regexp
  4563. (concat "^\\(\\*+\\)"
  4564. "\\(?: +" org-todo-regexp "\\)?"
  4565. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4566. "\\(?: +\\(.*?\\)\\)??"
  4567. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4568. "[ \t]*$")
  4569. org-complex-heading-regexp-format
  4570. (concat "^\\(\\*+\\)"
  4571. "\\(?: +" org-todo-regexp "\\)?"
  4572. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4573. "\\(?: +"
  4574. ;; Stats cookies can be stuck to body.
  4575. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4576. "\\(%s\\)"
  4577. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4578. "\\)"
  4579. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4580. "[ \t]*$")
  4581. org-todo-line-tags-regexp
  4582. (concat "^\\(\\*+\\)"
  4583. "\\(?: +" org-todo-regexp "\\)?"
  4584. "\\(?: +\\(.*?\\)\\)??"
  4585. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4586. "[ \t]*$")
  4587. org-deadline-regexp (concat "\\<" org-deadline-string)
  4588. org-deadline-time-regexp
  4589. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4590. org-deadline-time-hour-regexp
  4591. (concat "\\<" org-deadline-string
  4592. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4593. org-deadline-line-regexp
  4594. (concat "\\<\\(" org-deadline-string "\\).*")
  4595. org-scheduled-regexp
  4596. (concat "\\<" org-scheduled-string)
  4597. org-scheduled-time-regexp
  4598. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4599. org-scheduled-time-hour-regexp
  4600. (concat "\\<" org-scheduled-string
  4601. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4602. org-closed-time-regexp
  4603. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4604. org-keyword-time-regexp
  4605. (concat "\\<\\(" org-scheduled-string
  4606. "\\|" org-deadline-string
  4607. "\\|" org-closed-string
  4608. "\\|" org-clock-string "\\)"
  4609. " *[[<]\\([^]>]+\\)[]>]")
  4610. org-keyword-time-not-clock-regexp
  4611. (concat "\\<\\(" org-scheduled-string
  4612. "\\|" org-deadline-string
  4613. "\\|" org-closed-string
  4614. "\\)"
  4615. " *[[<]\\([^]>]+\\)[]>]")
  4616. org-maybe-keyword-time-regexp
  4617. (concat "\\(\\<\\(" org-scheduled-string
  4618. "\\|" org-deadline-string
  4619. "\\|" org-closed-string
  4620. "\\|" org-clock-string "\\)\\)?"
  4621. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4622. org-all-time-keywords
  4623. (mapcar (lambda (w) (substring w 0 -1))
  4624. (list org-scheduled-string org-deadline-string
  4625. org-clock-string org-closed-string)))
  4626. (setq org-ota nil)
  4627. (org-compute-latex-and-related-regexp))))
  4628. (defun org-file-contents (file &optional noerror)
  4629. "Return the contents of FILE, as a string."
  4630. (if (and file (file-readable-p file))
  4631. (with-temp-buffer
  4632. (insert-file-contents file)
  4633. (buffer-string))
  4634. (funcall (if noerror 'message 'error)
  4635. "Cannot read file \"%s\"%s"
  4636. file
  4637. (let ((from (buffer-file-name (buffer-base-buffer))))
  4638. (if from (concat " (referenced in file \"" from "\")") "")))))
  4639. (defun org-extract-log-state-settings (x)
  4640. "Extract the log state setting from a TODO keyword string.
  4641. This will extract info from a string like \"WAIT(w@/!)\"."
  4642. (let (kw key log1 log2)
  4643. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4644. (setq kw (match-string 1 x)
  4645. key (and (match-end 2) (match-string 2 x))
  4646. log1 (and (match-end 3) (match-string 3 x))
  4647. log2 (and (match-end 4) (match-string 4 x)))
  4648. (and (or log1 log2)
  4649. (list kw
  4650. (and log1 (if (equal log1 "!") 'time 'note))
  4651. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4652. (defun org-remove-keyword-keys (list)
  4653. "Remove a pair of parenthesis at the end of each string in LIST."
  4654. (mapcar (lambda (x)
  4655. (if (string-match "(.*)$" x)
  4656. (substring x 0 (match-beginning 0))
  4657. x))
  4658. list))
  4659. (defun org-assign-fast-keys (alist)
  4660. "Assign fast keys to a keyword-key alist.
  4661. Respect keys that are already there."
  4662. (let (new e (alt ?0))
  4663. (while (setq e (pop alist))
  4664. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4665. (cdr e)) ;; Key already assigned.
  4666. (push e new)
  4667. (let ((clist (string-to-list (downcase (car e))))
  4668. (used (append new alist)))
  4669. (when (= (car clist) ?@)
  4670. (pop clist))
  4671. (while (and clist (rassoc (car clist) used))
  4672. (pop clist))
  4673. (unless clist
  4674. (while (rassoc alt used)
  4675. (incf alt)))
  4676. (push (cons (car e) (or (car clist) alt)) new))))
  4677. (nreverse new)))
  4678. ;;; Some variables used in various places
  4679. (defvar org-window-configuration nil
  4680. "Used in various places to store a window configuration.")
  4681. (defvar org-selected-window nil
  4682. "Used in various places to store a window configuration.")
  4683. (defvar org-finish-function nil
  4684. "Function to be called when `C-c C-c' is used.
  4685. This is for getting out of special buffers like capture.")
  4686. ;; FIXME: Occasionally check by commenting these, to make sure
  4687. ;; no other functions uses these, forgetting to let-bind them.
  4688. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4689. (defvar org-last-state)
  4690. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4691. ;; Defined somewhere in this file, but used before definition.
  4692. (defvar org-entities) ;; defined in org-entities.el
  4693. (defvar org-struct-menu)
  4694. (defvar org-org-menu)
  4695. (defvar org-tbl-menu)
  4696. ;;;; Define the Org-mode
  4697. ;; We use a before-change function to check if a table might need
  4698. ;; an update.
  4699. (defvar org-table-may-need-update t
  4700. "Indicates that a table might need an update.
  4701. This variable is set by `org-before-change-function'.
  4702. `org-table-align' sets it back to nil.")
  4703. (defun org-before-change-function (beg end)
  4704. "Every change indicates that a table might need an update."
  4705. (setq org-table-may-need-update t))
  4706. (defvar org-mode-map)
  4707. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4708. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4709. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4710. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4711. (defvar org-table-buffer-is-an nil)
  4712. (defvar bidi-paragraph-direction)
  4713. (defvar buffer-face-mode-face)
  4714. (require 'outline)
  4715. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4716. (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"))
  4717. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4718. ;; Other stuff we need.
  4719. (require 'time-date)
  4720. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4721. (require 'easymenu)
  4722. (require 'overlay)
  4723. ;; (require 'org-macs) moved higher up in the file before it is first used
  4724. (require 'org-entities)
  4725. ;; (require 'org-compat) moved higher up in the file before it is first used
  4726. (require 'org-faces)
  4727. (require 'org-list)
  4728. (require 'org-pcomplete)
  4729. (require 'org-src)
  4730. (require 'org-footnote)
  4731. (require 'org-macro)
  4732. ;; babel
  4733. (require 'ob)
  4734. ;;;###autoload
  4735. (define-derived-mode org-mode outline-mode "Org"
  4736. "Outline-based notes management and organizer, alias
  4737. \"Carsten's outline-mode for keeping track of everything.\"
  4738. Org-mode develops organizational tasks around a NOTES file which
  4739. contains information about projects as plain text. Org-mode is
  4740. implemented on top of outline-mode, which is ideal to keep the content
  4741. of large files well structured. It supports ToDo items, deadlines and
  4742. time stamps, which magically appear in the diary listing of the Emacs
  4743. calendar. Tables are easily created with a built-in table editor.
  4744. Plain text URL-like links connect to websites, emails (VM), Usenet
  4745. messages (Gnus), BBDB entries, and any files related to the project.
  4746. For printing and sharing of notes, an Org-mode file (or a part of it)
  4747. can be exported as a structured ASCII or HTML file.
  4748. The following commands are available:
  4749. \\{org-mode-map}"
  4750. ;; Get rid of Outline menus, they are not needed
  4751. ;; Need to do this here because define-derived-mode sets up
  4752. ;; the keymap so late. Still, it is a waste to call this each time
  4753. ;; we switch another buffer into org-mode.
  4754. (if (featurep 'xemacs)
  4755. (when (boundp 'outline-mode-menu-heading)
  4756. ;; Assume this is Greg's port, it uses easymenu
  4757. (easy-menu-remove outline-mode-menu-heading)
  4758. (easy-menu-remove outline-mode-menu-show)
  4759. (easy-menu-remove outline-mode-menu-hide))
  4760. (define-key org-mode-map [menu-bar headings] 'undefined)
  4761. (define-key org-mode-map [menu-bar hide] 'undefined)
  4762. (define-key org-mode-map [menu-bar show] 'undefined))
  4763. (org-load-modules-maybe)
  4764. (easy-menu-add org-org-menu)
  4765. (easy-menu-add org-tbl-menu)
  4766. (org-install-agenda-files-menu)
  4767. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4768. (add-to-invisibility-spec '(org-cwidth))
  4769. (add-to-invisibility-spec '(org-hide-block . t))
  4770. (when (featurep 'xemacs)
  4771. (org-set-local 'line-move-ignore-invisible t))
  4772. (org-set-local 'outline-regexp org-outline-regexp)
  4773. (org-set-local 'outline-level 'org-outline-level)
  4774. (setq bidi-paragraph-direction 'left-to-right)
  4775. (when (and org-ellipsis
  4776. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4777. (fboundp 'make-glyph-code))
  4778. (unless org-display-table
  4779. (setq org-display-table (make-display-table)))
  4780. (set-display-table-slot
  4781. org-display-table 4
  4782. (vconcat (mapcar
  4783. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4784. org-ellipsis)))
  4785. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4786. (setq buffer-display-table org-display-table))
  4787. (org-set-regexps-and-options-for-tags)
  4788. (org-set-regexps-and-options)
  4789. (org-set-font-lock-defaults)
  4790. (when (and org-tag-faces (not org-tags-special-faces-re))
  4791. ;; tag faces set outside customize.... force initialization.
  4792. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4793. ;; Calc embedded
  4794. (org-set-local 'calc-embedded-open-mode "# ")
  4795. ;; Modify a few syntax entries
  4796. (modify-syntax-entry ?@ "w")
  4797. (modify-syntax-entry ?\" "\"")
  4798. (modify-syntax-entry ?\\ "_")
  4799. (modify-syntax-entry ?~ "_")
  4800. (if org-startup-truncated (setq truncate-lines t))
  4801. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4802. (org-set-local 'font-lock-unfontify-region-function
  4803. 'org-unfontify-region)
  4804. ;; Activate before-change-function
  4805. (org-set-local 'org-table-may-need-update t)
  4806. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4807. 'local)
  4808. ;; Check for running clock before killing a buffer
  4809. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4810. ;; Initialize macros templates.
  4811. (org-macro-initialize-templates)
  4812. ;; Initialize radio targets.
  4813. (org-update-radio-target-regexp)
  4814. ;; Indentation.
  4815. (org-set-local 'indent-line-function 'org-indent-line)
  4816. (org-set-local 'indent-region-function 'org-indent-region)
  4817. ;; Filling and auto-filling.
  4818. (org-setup-filling)
  4819. ;; Comments.
  4820. (org-setup-comments-handling)
  4821. ;; Initialize cache.
  4822. (org-element-cache-reset)
  4823. ;; Beginning/end of defun
  4824. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4825. (org-set-local 'end-of-defun-function
  4826. (lambda ()
  4827. (if (not (org-at-heading-p))
  4828. (org-forward-element)
  4829. (org-forward-element)
  4830. (forward-char -1))))
  4831. ;; Next error for sparse trees
  4832. (org-set-local 'next-error-function 'org-occur-next-match)
  4833. ;; Make sure dependence stuff works reliably, even for users who set it
  4834. ;; too late :-(
  4835. (if org-enforce-todo-dependencies
  4836. (add-hook 'org-blocker-hook
  4837. 'org-block-todo-from-children-or-siblings-or-parent)
  4838. (remove-hook 'org-blocker-hook
  4839. 'org-block-todo-from-children-or-siblings-or-parent))
  4840. (if org-enforce-todo-checkbox-dependencies
  4841. (add-hook 'org-blocker-hook
  4842. 'org-block-todo-from-checkboxes)
  4843. (remove-hook 'org-blocker-hook
  4844. 'org-block-todo-from-checkboxes))
  4845. ;; Align options lines
  4846. (org-set-local
  4847. 'align-mode-rules-list
  4848. '((org-in-buffer-settings
  4849. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4850. (modes . '(org-mode)))))
  4851. ;; Imenu
  4852. (org-set-local 'imenu-create-index-function
  4853. 'org-imenu-get-tree)
  4854. ;; Make isearch reveal context
  4855. (if (or (featurep 'xemacs)
  4856. (not (boundp 'outline-isearch-open-invisible-function)))
  4857. ;; Emacs 21 and XEmacs make use of the hook
  4858. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4859. ;; Emacs 22 deals with this through a special variable
  4860. (org-set-local 'outline-isearch-open-invisible-function
  4861. (lambda (&rest ignore) (org-show-context 'isearch))))
  4862. ;; Setup the pcomplete hooks
  4863. (set (make-local-variable 'pcomplete-command-completion-function)
  4864. 'org-pcomplete-initial)
  4865. (set (make-local-variable 'pcomplete-command-name-function)
  4866. 'org-command-at-point)
  4867. (set (make-local-variable 'pcomplete-default-completion-function)
  4868. 'ignore)
  4869. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4870. 'org-parse-arguments)
  4871. (set (make-local-variable 'pcomplete-termination-string) "")
  4872. (when (>= emacs-major-version 23)
  4873. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4874. ;; If empty file that did not turn on org-mode automatically, make it to.
  4875. (if (and org-insert-mode-line-in-empty-file
  4876. (org-called-interactively-p 'any)
  4877. (= (point-min) (point-max)))
  4878. (insert "# -*- mode: org -*-\n\n"))
  4879. (unless org-inhibit-startup
  4880. (org-unmodified
  4881. (and org-startup-with-beamer-mode (org-beamer-mode))
  4882. (when org-startup-align-all-tables
  4883. (org-table-map-tables 'org-table-align 'quietly))
  4884. (when org-startup-with-inline-images
  4885. (org-display-inline-images))
  4886. (when org-startup-with-latex-preview
  4887. (org-toggle-latex-fragment))
  4888. (unless org-inhibit-startup-visibility-stuff
  4889. (org-set-startup-visibility))
  4890. (org-refresh-effort-properties)))
  4891. ;; Try to set org-hide correctly
  4892. (let ((foreground (org-find-invisible-foreground)))
  4893. (if foreground
  4894. (set-face-foreground 'org-hide foreground))))
  4895. ;; Update `customize-package-emacs-version-alist'
  4896. (add-to-list 'customize-package-emacs-version-alist
  4897. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4898. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4899. ("8.2.6" . "24.4")))
  4900. (defvar org-mode-transpose-word-syntax-table
  4901. (let ((st (make-syntax-table)))
  4902. (mapc (lambda(c) (modify-syntax-entry
  4903. (string-to-char (car c)) "w p" st))
  4904. org-emphasis-alist)
  4905. st))
  4906. (when (fboundp 'abbrev-table-put)
  4907. (abbrev-table-put org-mode-abbrev-table
  4908. :parents (list text-mode-abbrev-table)))
  4909. (defun org-find-invisible-foreground ()
  4910. (let ((candidates (remove
  4911. "unspecified-bg"
  4912. (nconc
  4913. (list (face-background 'default)
  4914. (face-background 'org-default))
  4915. (mapcar
  4916. (lambda (alist)
  4917. (when (boundp alist)
  4918. (cdr (assoc 'background-color (symbol-value alist)))))
  4919. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4920. (list (face-foreground 'org-hide))))))
  4921. (car (remove nil candidates))))
  4922. (defun org-current-time (&optional rounding-minutes past)
  4923. "Current time, possibly rounded to ROUNDING-MINUTES.
  4924. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4925. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4926. the rounding returns a past time."
  4927. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4928. (car org-time-stamp-rounding-minutes)))
  4929. (time (decode-time)) res)
  4930. (if (< r 1)
  4931. (current-time)
  4932. (setq res
  4933. (apply 'encode-time
  4934. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4935. (nthcdr 2 time))))
  4936. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4937. (seconds-to-time (- (org-float-time res) (* r 60)))
  4938. res))))
  4939. (defun org-today ()
  4940. "Return today date, considering `org-extend-today-until'."
  4941. (time-to-days
  4942. (time-subtract (current-time)
  4943. (list 0 (* 3600 org-extend-today-until) 0))))
  4944. ;;;; Font-Lock stuff, including the activators
  4945. (defvar org-mouse-map (make-sparse-keymap))
  4946. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4947. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4948. (when org-mouse-1-follows-link
  4949. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4950. (when org-tab-follows-link
  4951. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4952. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4953. (require 'font-lock)
  4954. (defconst org-non-link-chars "]\t\n\r<>")
  4955. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4956. "file+sys" "news" "shell" "elisp" "doi" "message"
  4957. "help"))
  4958. (defvar org-link-types-re nil
  4959. "Matches a link that has a url-like prefix like \"http:\"")
  4960. (defvar org-link-re-with-space nil
  4961. "Matches a link with spaces, optional angular brackets around it.")
  4962. (defvar org-link-re-with-space2 nil
  4963. "Matches a link with spaces, optional angular brackets around it.")
  4964. (defvar org-link-re-with-space3 nil
  4965. "Matches a link with spaces, only for internal part in bracket links.")
  4966. (defvar org-angle-link-re nil
  4967. "Matches link with angular brackets, spaces are allowed.")
  4968. (defvar org-plain-link-re nil
  4969. "Matches plain link, without spaces.")
  4970. (defvar org-bracket-link-regexp nil
  4971. "Matches a link in double brackets.")
  4972. (defvar org-bracket-link-analytic-regexp nil
  4973. "Regular expression used to analyze links.
  4974. Here is what the match groups contain after a match:
  4975. 1: http:
  4976. 2: http
  4977. 3: path
  4978. 4: [desc]
  4979. 5: desc")
  4980. (defvar org-bracket-link-analytic-regexp++ nil
  4981. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4982. (defvar org-any-link-re nil
  4983. "Regular expression matching any link.")
  4984. (defconst org-match-sexp-depth 3
  4985. "Number of stacked braces for sub/superscript matching.")
  4986. (defun org-create-multibrace-regexp (left right n)
  4987. "Create a regular expression which will match a balanced sexp.
  4988. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4989. as single character strings.
  4990. The regexp returned will match the entire expression including the
  4991. delimiters. It will also define a single group which contains the
  4992. match except for the outermost delimiters. The maximum depth of
  4993. stacked delimiters is N. Escaping delimiters is not possible."
  4994. (let* ((nothing (concat "[^" left right "]*?"))
  4995. (or "\\|")
  4996. (re nothing)
  4997. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4998. (while (> n 1)
  4999. (setq n (1- n)
  5000. re (concat re or next)
  5001. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  5002. (concat left "\\(" re "\\)" right)))
  5003. (defconst org-match-substring-regexp
  5004. (concat
  5005. "\\(\\S-\\)\\([_^]\\)\\("
  5006. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  5007. "\\|"
  5008. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  5009. "\\|"
  5010. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  5011. "The regular expression matching a sub- or superscript.")
  5012. (defconst org-match-substring-with-braces-regexp
  5013. (concat
  5014. "\\(\\S-\\)\\([_^]\\)"
  5015. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  5016. "The regular expression matching a sub- or superscript, forcing braces.")
  5017. (defun org-make-link-regexps ()
  5018. "Update the link regular expressions.
  5019. This should be called after the variable `org-link-types' has changed."
  5020. (let ((types-re (regexp-opt org-link-types t)))
  5021. (setq org-link-types-re
  5022. (concat "\\`" types-re ":")
  5023. org-link-re-with-space
  5024. (concat "<?" types-re ":"
  5025. "\\([^" org-non-link-chars " ]"
  5026. "[^" org-non-link-chars "]*"
  5027. "[^" org-non-link-chars " ]\\)>?")
  5028. org-link-re-with-space2
  5029. (concat "<?" types-re ":"
  5030. "\\([^" org-non-link-chars " ]"
  5031. "[^\t\n\r]*"
  5032. "[^" org-non-link-chars " ]\\)>?")
  5033. org-link-re-with-space3
  5034. (concat "<?" types-re ":"
  5035. "\\([^" org-non-link-chars " ]"
  5036. "[^\t\n\r]*\\)")
  5037. org-angle-link-re
  5038. (concat "<" types-re ":"
  5039. "\\([^" org-non-link-chars " ]"
  5040. "[^" org-non-link-chars "]*"
  5041. "\\)>")
  5042. org-plain-link-re
  5043. (concat
  5044. "\\<" types-re ":"
  5045. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  5046. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5047. org-bracket-link-regexp
  5048. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5049. org-bracket-link-analytic-regexp
  5050. (concat
  5051. "\\[\\["
  5052. "\\(" types-re ":\\)?"
  5053. "\\([^]]+\\)"
  5054. "\\]"
  5055. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5056. "\\]")
  5057. org-bracket-link-analytic-regexp++
  5058. (concat
  5059. "\\[\\["
  5060. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5061. "\\([^]]+\\)"
  5062. "\\]"
  5063. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5064. "\\]")
  5065. org-any-link-re
  5066. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5067. org-angle-link-re "\\)\\|\\("
  5068. org-plain-link-re "\\)"))))
  5069. (org-make-link-regexps)
  5070. (defvar org-emph-face nil)
  5071. (defun org-do-emphasis-faces (limit)
  5072. "Run through the buffer and emphasize strings."
  5073. (let (rtn a)
  5074. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5075. (let* ((border (char-after (match-beginning 3)))
  5076. (bre (regexp-quote (char-to-string border))))
  5077. (if (and (not (= border (char-after (match-beginning 4))))
  5078. (not (save-match-data
  5079. (string-match (concat bre ".*" bre)
  5080. (replace-regexp-in-string
  5081. "\n" " "
  5082. (substring (match-string 2) 1 -1))))))
  5083. (progn
  5084. (setq rtn t)
  5085. (setq a (assoc (match-string 3) org-emphasis-alist))
  5086. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5087. 'face
  5088. (nth 1 a))
  5089. (and (nth 2 a)
  5090. (org-remove-flyspell-overlays-in
  5091. (match-beginning 0) (match-end 0)))
  5092. (add-text-properties (match-beginning 2) (match-end 2)
  5093. '(font-lock-multiline t org-emphasis t))
  5094. (when org-hide-emphasis-markers
  5095. (add-text-properties (match-end 4) (match-beginning 5)
  5096. '(invisible org-link))
  5097. (add-text-properties (match-beginning 3) (match-end 3)
  5098. '(invisible org-link))))))
  5099. (goto-char (1+ (match-beginning 0))))
  5100. rtn))
  5101. (defun org-emphasize (&optional char)
  5102. "Insert or change an emphasis, i.e. a font like bold or italic.
  5103. If there is an active region, change that region to a new emphasis.
  5104. If there is no region, just insert the marker characters and position
  5105. the cursor between them.
  5106. CHAR should be the marker character. If it is a space, it means to
  5107. remove the emphasis of the selected region.
  5108. If CHAR is not given (for example in an interactive call) it will be
  5109. prompted for."
  5110. (interactive)
  5111. (let ((erc org-emphasis-regexp-components)
  5112. (prompt "")
  5113. (string "") beg end move c s)
  5114. (if (org-region-active-p)
  5115. (setq beg (region-beginning) end (region-end)
  5116. string (buffer-substring beg end))
  5117. (setq move t))
  5118. (unless char
  5119. (message "Emphasis marker or tag: [%s]"
  5120. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5121. (setq char (read-char-exclusive)))
  5122. (if (equal char ?\ )
  5123. (setq s "" move nil)
  5124. (unless (assoc (char-to-string char) org-emphasis-alist)
  5125. (user-error "No such emphasis marker: \"%c\"" char))
  5126. (setq s (char-to-string char)))
  5127. (while (and (> (length string) 1)
  5128. (equal (substring string 0 1) (substring string -1))
  5129. (assoc (substring string 0 1) org-emphasis-alist))
  5130. (setq string (substring string 1 -1)))
  5131. (setq string (concat s string s))
  5132. (if beg (delete-region beg end))
  5133. (unless (or (bolp)
  5134. (string-match (concat "[" (nth 0 erc) "\n]")
  5135. (char-to-string (char-before (point)))))
  5136. (insert " "))
  5137. (unless (or (eobp)
  5138. (string-match (concat "[" (nth 1 erc) "\n]")
  5139. (char-to-string (char-after (point)))))
  5140. (insert " ") (backward-char 1))
  5141. (insert string)
  5142. (and move (backward-char 1))))
  5143. (defconst org-nonsticky-props
  5144. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5145. (defsubst org-rear-nonsticky-at (pos)
  5146. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5147. (defun org-activate-plain-links (limit)
  5148. "Add link properties for plain links."
  5149. (let (f hl)
  5150. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5151. (not (member 'org-tag
  5152. (get-text-property (1- (match-beginning 0)) 'face)))
  5153. (not (org-in-src-block-p)))
  5154. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5155. (setq f (get-text-property (match-beginning 0) 'face))
  5156. (setq hl (org-match-string-no-properties 0))
  5157. (if (or (eq f 'org-tag)
  5158. (and (listp f) (memq 'org-tag f)))
  5159. nil
  5160. (add-text-properties (match-beginning 0) (match-end 0)
  5161. (list 'mouse-face 'highlight
  5162. 'face 'org-link
  5163. 'htmlize-link `(:uri ,hl)
  5164. 'keymap org-mouse-map))
  5165. (org-rear-nonsticky-at (match-end 0)))
  5166. t)))
  5167. (defun org-activate-code (limit)
  5168. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5169. (progn
  5170. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5171. (remove-text-properties (match-beginning 0) (match-end 0)
  5172. '(display t invisible t intangible t))
  5173. t)))
  5174. (defcustom org-src-fontify-natively t
  5175. "When non-nil, fontify code in code blocks."
  5176. :type 'boolean
  5177. :version "24.4"
  5178. :package-version '(Org . "8.3")
  5179. :group 'org-appearance
  5180. :group 'org-babel)
  5181. (defcustom org-allow-promoting-top-level-subtree nil
  5182. "When non-nil, allow promoting a top level subtree.
  5183. The leading star of the top level headline will be replaced
  5184. by a #."
  5185. :type 'boolean
  5186. :version "24.1"
  5187. :group 'org-appearance)
  5188. (defun org-fontify-meta-lines-and-blocks (limit)
  5189. (condition-case nil
  5190. (org-fontify-meta-lines-and-blocks-1 limit)
  5191. (error (message "org-mode fontification error"))))
  5192. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5193. "Fontify #+ lines and blocks."
  5194. (let ((case-fold-search t))
  5195. (if (re-search-forward
  5196. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5197. limit t)
  5198. (let ((beg (match-beginning 0))
  5199. (block-start (match-end 0))
  5200. (block-end nil)
  5201. (lang (match-string 7))
  5202. (beg1 (line-beginning-position 2))
  5203. (dc1 (downcase (match-string 2)))
  5204. (dc3 (downcase (match-string 3)))
  5205. end end1 quoting block-type ovl)
  5206. (cond
  5207. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5208. ;; a single line of backend-specific content
  5209. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5210. (remove-text-properties (match-beginning 0) (match-end 0)
  5211. '(display t invisible t intangible t))
  5212. (add-text-properties (match-beginning 1) (match-end 3)
  5213. '(font-lock-fontified t face org-meta-line))
  5214. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5215. '(font-lock-fontified t face org-block))
  5216. ; for backend-specific code
  5217. t)
  5218. ((and (match-end 4) (equal dc3 "+begin"))
  5219. ;; Truly a block
  5220. (setq block-type (downcase (match-string 5))
  5221. quoting (member block-type org-protecting-blocks))
  5222. (when (re-search-forward
  5223. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5224. nil t) ;; on purpose, we look further than LIMIT
  5225. (setq end (min (point-max) (match-end 0))
  5226. end1 (min (point-max) (1- (match-beginning 0))))
  5227. (setq block-end (match-beginning 0))
  5228. (when quoting
  5229. (org-remove-flyspell-overlays-in beg1 end1)
  5230. (remove-text-properties beg end
  5231. '(display t invisible t intangible t)))
  5232. (add-text-properties
  5233. beg end '(font-lock-fontified t font-lock-multiline t))
  5234. (add-text-properties beg beg1 '(face org-meta-line))
  5235. (org-remove-flyspell-overlays-in beg beg1)
  5236. (add-text-properties ; For end_src
  5237. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5238. (org-remove-flyspell-overlays-in end1 end)
  5239. (cond
  5240. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5241. (org-src-font-lock-fontify-block lang block-start block-end)
  5242. (add-text-properties beg1 block-end '(src-block t)))
  5243. (quoting
  5244. (add-text-properties beg1 (min (point-max) (1+ end1))
  5245. '(face org-block))) ; end of source block
  5246. ((not org-fontify-quote-and-verse-blocks))
  5247. ((string= block-type "quote")
  5248. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5249. ((string= block-type "verse")
  5250. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5251. (add-text-properties beg beg1 '(face org-block-begin-line))
  5252. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5253. '(face org-block-end-line))
  5254. t))
  5255. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5256. (org-remove-flyspell-overlays-in
  5257. (match-beginning 0)
  5258. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5259. (add-text-properties
  5260. beg (match-end 3)
  5261. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5262. '(font-lock-fontified t invisible t)
  5263. '(font-lock-fontified t face org-document-info-keyword)))
  5264. (add-text-properties
  5265. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5266. (if (string-equal dc1 "+title:")
  5267. '(font-lock-fontified t face org-document-title)
  5268. '(font-lock-fontified t face org-document-info))))
  5269. ((or (equal dc1 "+results")
  5270. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5271. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5272. "+call:" "+header:" "+headers:" "+name:"))
  5273. (and (match-end 4) (equal dc3 "+attr")))
  5274. (org-remove-flyspell-overlays-in
  5275. (match-beginning 0)
  5276. (if (equal "+caption:" dc1) (match-end 2) (match-end 0)))
  5277. (add-text-properties
  5278. beg (match-end 0)
  5279. '(font-lock-fontified t face org-meta-line))
  5280. t)
  5281. ((member dc3 '(" " ""))
  5282. (org-remove-flyspell-overlays-in beg (match-end 0))
  5283. (add-text-properties
  5284. beg (match-end 0)
  5285. '(font-lock-fontified t face font-lock-comment-face)))
  5286. (t ;; just any other in-buffer setting, but not indented
  5287. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5288. (add-text-properties
  5289. beg (match-end 0)
  5290. '(font-lock-fontified t face org-meta-line))
  5291. t))))))
  5292. (defun org-fontify-drawers (limit)
  5293. "Fontify drawers."
  5294. (when (re-search-forward org-drawer-regexp limit t)
  5295. (add-text-properties
  5296. (match-beginning 0) (match-end 0)
  5297. '(font-lock-fontified t face org-special-keyword))
  5298. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5299. t))
  5300. (defun org-fontify-macros (limit)
  5301. "Fontify macros."
  5302. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5303. (add-text-properties
  5304. (match-beginning 0) (match-end 0)
  5305. '(font-lock-fontified t face org-macro))
  5306. (when org-hide-macro-markers
  5307. (add-text-properties (match-end 2) (match-beginning 2)
  5308. '(invisible t))
  5309. (add-text-properties (match-beginning 1) (match-end 1)
  5310. '(invisible t)))
  5311. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5312. t))
  5313. (defun org-activate-angle-links (limit)
  5314. "Add text properties for angle links."
  5315. (if (and (re-search-forward org-angle-link-re limit t)
  5316. (not (org-in-src-block-p)))
  5317. (progn
  5318. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5319. (add-text-properties (match-beginning 0) (match-end 0)
  5320. (list 'mouse-face 'highlight
  5321. 'keymap org-mouse-map))
  5322. (org-rear-nonsticky-at (match-end 0))
  5323. t)))
  5324. (defun org-activate-footnote-links (limit)
  5325. "Add text properties for footnotes."
  5326. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5327. (when fn
  5328. (let* ((beg (nth 1 fn))
  5329. (end (nth 2 fn))
  5330. (label (car fn))
  5331. (referencep (/= (line-beginning-position) beg)))
  5332. (when (and referencep (nth 3 fn))
  5333. (save-excursion
  5334. (goto-char beg)
  5335. (search-forward (or label "fn:"))
  5336. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5337. (add-text-properties beg end
  5338. (list 'mouse-face 'highlight
  5339. 'keymap org-mouse-map
  5340. 'help-echo
  5341. (if referencep "Footnote reference"
  5342. "Footnote definition")
  5343. 'font-lock-fontified t
  5344. 'font-lock-multiline t
  5345. 'face 'org-footnote))))))
  5346. (defun org-activate-bracket-links (limit)
  5347. "Add text properties for bracketed links."
  5348. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5349. (not (org-in-src-block-p)))
  5350. (let* ((hl (org-match-string-no-properties 1))
  5351. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5352. (ip (org-maybe-intangible
  5353. (list 'invisible 'org-link
  5354. 'keymap org-mouse-map 'mouse-face 'highlight
  5355. 'font-lock-multiline t 'help-echo help
  5356. 'htmlize-link `(:uri ,hl))))
  5357. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5358. 'font-lock-multiline t 'help-echo help
  5359. 'htmlize-link `(:uri ,hl))))
  5360. ;; We need to remove the invisible property here. Table narrowing
  5361. ;; may have made some of this invisible.
  5362. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5363. (remove-text-properties (match-beginning 0) (match-end 0)
  5364. '(invisible nil))
  5365. (if (match-end 3)
  5366. (progn
  5367. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5368. (org-rear-nonsticky-at (match-beginning 3))
  5369. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5370. (org-rear-nonsticky-at (match-end 3))
  5371. (add-text-properties (match-end 3) (match-end 0) ip)
  5372. (org-rear-nonsticky-at (match-end 0)))
  5373. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5374. (org-rear-nonsticky-at (match-beginning 1))
  5375. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5376. (org-rear-nonsticky-at (match-end 1))
  5377. (add-text-properties (match-end 1) (match-end 0) ip)
  5378. (org-rear-nonsticky-at (match-end 0)))
  5379. t)))
  5380. (defun org-activate-dates (limit)
  5381. "Add text properties for dates."
  5382. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5383. (not (equal (char-before (match-beginning 0)) 91)))
  5384. (progn
  5385. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5386. (add-text-properties (match-beginning 0) (match-end 0)
  5387. (list 'mouse-face 'highlight
  5388. 'keymap org-mouse-map))
  5389. (org-rear-nonsticky-at (match-end 0))
  5390. (when org-display-custom-times
  5391. (if (match-end 3)
  5392. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5393. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5394. t)))
  5395. (defvar org-target-link-regexp nil
  5396. "Regular expression matching radio targets in plain text.")
  5397. (make-variable-buffer-local 'org-target-link-regexp)
  5398. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5399. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5400. border border border))
  5401. "Regular expression matching a link target.")
  5402. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5403. "Regular expression matching a radio target.")
  5404. (defconst org-any-target-regexp
  5405. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5406. "Regular expression matching any target.")
  5407. (defun org-activate-target-links (limit)
  5408. "Add text properties for target matches."
  5409. (when org-target-link-regexp
  5410. (let ((case-fold-search t))
  5411. (if (re-search-forward org-target-link-regexp limit t)
  5412. (progn
  5413. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5414. (add-text-properties (match-beginning 1) (match-end 1)
  5415. (list 'mouse-face 'highlight
  5416. 'keymap org-mouse-map
  5417. 'help-echo "Radio target link"
  5418. 'org-linked-text t))
  5419. (org-rear-nonsticky-at (match-end 1))
  5420. t)))))
  5421. (defun org-update-radio-target-regexp ()
  5422. "Find all radio targets in this file and update the regular expression.
  5423. Also refresh fontification if needed."
  5424. (interactive)
  5425. (let ((old-regexp org-target-link-regexp)
  5426. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5427. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5428. (targets
  5429. (org-with-wide-buffer
  5430. (goto-char (point-min))
  5431. (let (rtn)
  5432. (while (re-search-forward org-radio-target-regexp nil t)
  5433. ;; Make sure point is really within the object.
  5434. (backward-char)
  5435. (let ((obj (org-element-context)))
  5436. (when (eq (org-element-type obj) 'radio-target)
  5437. (add-to-list 'rtn (org-element-property :value obj)))))
  5438. rtn))))
  5439. (setq org-target-link-regexp
  5440. (and targets
  5441. (concat before-re
  5442. (mapconcat
  5443. (lambda (x)
  5444. (replace-regexp-in-string
  5445. " +" "\\s-+" (regexp-quote x) t t))
  5446. targets
  5447. "\\|")
  5448. after-re)))
  5449. (unless (equal old-regexp org-target-link-regexp)
  5450. ;; Clean-up cache.
  5451. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5452. ((not org-target-link-regexp) old-regexp)
  5453. (t
  5454. (concat before-re
  5455. (mapconcat
  5456. (lambda (re)
  5457. (substring re (length before-re)
  5458. (- (length after-re))))
  5459. (list old-regexp org-target-link-regexp)
  5460. "\\|")
  5461. after-re)))))
  5462. (org-with-wide-buffer
  5463. (goto-char (point-min))
  5464. (while (re-search-forward regexp nil t)
  5465. (org-element-cache-refresh (match-beginning 1)))))
  5466. ;; Re fontify buffer.
  5467. (when (memq 'radio org-highlight-links)
  5468. (org-restart-font-lock)))))
  5469. (defun org-hide-wide-columns (limit)
  5470. (let (s e)
  5471. (setq s (text-property-any (point) (or limit (point-max))
  5472. 'org-cwidth t))
  5473. (when s
  5474. (setq e (next-single-property-change s 'org-cwidth))
  5475. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5476. (goto-char e)
  5477. t)))
  5478. (defvar org-latex-and-related-regexp nil
  5479. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5480. (defun org-compute-latex-and-related-regexp ()
  5481. "Compute regular expression for LaTeX, entities and sub/superscript.
  5482. Result depends on variable `org-highlight-latex-and-related'."
  5483. (org-set-local
  5484. 'org-latex-and-related-regexp
  5485. (let* ((re-sub
  5486. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5487. ((eq org-use-sub-superscripts '{})
  5488. (list org-match-substring-with-braces-regexp))
  5489. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5490. (re-latex
  5491. (when (memq 'latex org-highlight-latex-and-related)
  5492. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5493. (delq nil
  5494. (mapcar (lambda (x)
  5495. (and (member (car x) matchers) (nth 1 x)))
  5496. org-latex-regexps)))))
  5497. (re-entities
  5498. (when (memq 'entities org-highlight-latex-and-related)
  5499. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5500. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5501. (defun org-do-latex-and-related (limit)
  5502. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5503. LIMIT bounds the search for syntax to highlight. Stop at first
  5504. highlighted object, if any. Return t if some highlighting was
  5505. done, nil otherwise."
  5506. (when (org-string-nw-p org-latex-and-related-regexp)
  5507. (catch 'found
  5508. (while (re-search-forward org-latex-and-related-regexp limit t)
  5509. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5510. 'face))
  5511. '(org-code org-verbatim underline))
  5512. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5513. '(?_ ?^))
  5514. 1
  5515. 0)))
  5516. (font-lock-prepend-text-property
  5517. (+ offset (match-beginning 0)) (match-end 0)
  5518. 'face 'org-latex-and-related)
  5519. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5520. '(font-lock-multiline t)))
  5521. (throw 'found t)))
  5522. nil)))
  5523. (defun org-restart-font-lock ()
  5524. "Restart `font-lock-mode', to force refontification."
  5525. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5526. (font-lock-mode -1)
  5527. (font-lock-mode 1)))
  5528. (defun org-activate-tags (limit)
  5529. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5530. (progn
  5531. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5532. (add-text-properties (match-beginning 1) (match-end 1)
  5533. (list 'mouse-face 'highlight
  5534. 'keymap org-mouse-map))
  5535. (org-rear-nonsticky-at (match-end 1))
  5536. t)))
  5537. (defun org-outline-level ()
  5538. "Compute the outline level of the heading at point.
  5539. If this is called at a normal headline, the level is the number of stars.
  5540. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5541. (save-excursion
  5542. (if (not (ignore-errors (org-back-to-heading t)))
  5543. 0
  5544. (looking-at org-outline-regexp)
  5545. (1- (- (match-end 0) (match-beginning 0))))))
  5546. (defvar org-font-lock-keywords nil)
  5547. (defsubst org-re-property (property &optional literal allow-null)
  5548. "Return a regexp matching a PROPERTY line.
  5549. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5550. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5551. non-nil, match properties even without a value.
  5552. Match group 3 is set to the value when it exists. If there is no
  5553. value and ALLOW-NULL is non-nil, it is set to the empty string."
  5554. (concat
  5555. "^\\(?4:[ \t]*\\)"
  5556. (format "\\(?1::\\(?2:%s\\):\\)"
  5557. (if literal property (regexp-quote property)))
  5558. (if allow-null
  5559. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$"
  5560. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$")))
  5561. (defconst org-property-re
  5562. (org-re-property ".*?" 'literal t)
  5563. "Regular expression matching a property line.
  5564. There are four matching groups:
  5565. 1: :PROPKEY: including the leading and trailing colon,
  5566. 2: PROPKEY without the leading and trailing colon,
  5567. 3: PROPVAL without leading or trailing spaces,
  5568. 4: the indentation of the current line,
  5569. 5: trailing whitespace.")
  5570. (defvar org-font-lock-hook nil
  5571. "Functions to be called for special font lock stuff.")
  5572. (defvar org-font-lock-set-keywords-hook nil
  5573. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5574. This is called after `org-font-lock-extra-keywords' is defined, but before
  5575. it is installed to be used by font lock. This can be useful if something
  5576. needs to be inserted at a specific position in the font-lock sequence.")
  5577. (defun org-font-lock-hook (limit)
  5578. "Run `org-font-lock-hook' within LIMIT."
  5579. (run-hook-with-args 'org-font-lock-hook limit))
  5580. (defun org-set-font-lock-defaults ()
  5581. "Set font lock defaults for the current buffer."
  5582. (let* ((em org-fontify-emphasized-text)
  5583. (lk org-highlight-links)
  5584. (org-font-lock-extra-keywords
  5585. (list
  5586. ;; Call the hook
  5587. '(org-font-lock-hook)
  5588. ;; Headlines
  5589. `(,(if org-fontify-whole-heading-line
  5590. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5591. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5592. (1 (org-get-level-face 1))
  5593. (2 (org-get-level-face 2))
  5594. (3 (org-get-level-face 3)))
  5595. ;; Table lines
  5596. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5597. (1 'org-table t))
  5598. ;; Table internals
  5599. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5600. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5601. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5602. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5603. ;; Drawers
  5604. '(org-fontify-drawers)
  5605. ;; Properties
  5606. (list org-property-re
  5607. '(1 'org-special-keyword t)
  5608. '(3 'org-property-value t))
  5609. ;; Links
  5610. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5611. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5612. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5613. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5614. (if (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5615. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5616. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5617. ;; Targets.
  5618. (list org-any-target-regexp '(0 'org-target t))
  5619. ;; Diary sexps.
  5620. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5621. ;; Macro
  5622. '(org-fontify-macros)
  5623. '(org-hide-wide-columns (0 nil append))
  5624. ;; TODO keyword
  5625. (list (format org-heading-keyword-regexp-format
  5626. org-todo-regexp)
  5627. '(2 (org-get-todo-face 2) t))
  5628. ;; DONE
  5629. (if org-fontify-done-headline
  5630. (list (format org-heading-keyword-regexp-format
  5631. (concat
  5632. "\\(?:"
  5633. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5634. "\\)"))
  5635. '(2 'org-headline-done t))
  5636. nil)
  5637. ;; Priorities
  5638. '(org-font-lock-add-priority-faces)
  5639. ;; Tags
  5640. '(org-font-lock-add-tag-faces)
  5641. ;; Tags groups
  5642. (if (and org-group-tags org-tag-groups-alist)
  5643. (list (concat org-outline-regexp-bol ".+\\(:"
  5644. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5645. ":\\).*$")
  5646. '(1 'org-tag-group prepend)))
  5647. ;; Special keywords
  5648. (list (concat "\\<" org-comment-string) '(0 'org-special-keyword t))
  5649. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5650. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5651. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5652. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5653. ;; Emphasis
  5654. (if em
  5655. (if (featurep 'xemacs)
  5656. '(org-do-emphasis-faces (0 nil append))
  5657. '(org-do-emphasis-faces)))
  5658. ;; Checkboxes
  5659. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5660. 1 'org-checkbox prepend)
  5661. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5662. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5663. (0 (org-get-checkbox-statistics-face) t)))
  5664. ;; Description list items
  5665. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5666. 1 'org-list-dt prepend)
  5667. ;; ARCHIVEd headings
  5668. (list (concat
  5669. org-outline-regexp-bol
  5670. "\\(.*:" org-archive-tag ":.*\\)")
  5671. '(1 'org-archived prepend))
  5672. ;; Specials
  5673. '(org-do-latex-and-related)
  5674. '(org-fontify-entities)
  5675. '(org-raise-scripts)
  5676. ;; Code
  5677. '(org-activate-code (1 'org-code t))
  5678. ;; COMMENT
  5679. (list (format
  5680. "^\\*\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5681. org-todo-regexp
  5682. org-comment-string)
  5683. '(9 'org-special-keyword t))
  5684. ;; Blocks and meta lines
  5685. '(org-fontify-meta-lines-and-blocks))))
  5686. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5687. (run-hooks 'org-font-lock-set-keywords-hook)
  5688. ;; Now set the full font-lock-keywords
  5689. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5690. (org-set-local 'font-lock-defaults
  5691. '(org-font-lock-keywords t nil nil backward-paragraph))
  5692. (kill-local-variable 'font-lock-keywords) nil))
  5693. (defun org-toggle-pretty-entities ()
  5694. "Toggle the composition display of entities as UTF8 characters."
  5695. (interactive)
  5696. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5697. (org-restart-font-lock)
  5698. (if org-pretty-entities
  5699. (message "Entities are now displayed as UTF8 characters")
  5700. (save-restriction
  5701. (widen)
  5702. (org-decompose-region (point-min) (point-max))
  5703. (message "Entities are now displayed as plain text"))))
  5704. (defvar org-custom-properties-overlays nil
  5705. "List of overlays used for custom properties.")
  5706. (make-variable-buffer-local 'org-custom-properties-overlays)
  5707. (defun org-toggle-custom-properties-visibility ()
  5708. "Display or hide properties in `org-custom-properties'."
  5709. (interactive)
  5710. (if org-custom-properties-overlays
  5711. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5712. (setq org-custom-properties-overlays nil))
  5713. (unless (not org-custom-properties)
  5714. (save-excursion
  5715. (save-restriction
  5716. (widen)
  5717. (goto-char (point-min))
  5718. (while (re-search-forward org-property-re nil t)
  5719. (mapc (lambda(p)
  5720. (when (equal p (substring (match-string 1) 1 -1))
  5721. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5722. (overlay-put o 'invisible t)
  5723. (overlay-put o 'org-custom-property t)
  5724. (push o org-custom-properties-overlays))))
  5725. org-custom-properties)))))))
  5726. (defun org-fontify-entities (limit)
  5727. "Find an entity to fontify."
  5728. (let (ee)
  5729. (when org-pretty-entities
  5730. (catch 'match
  5731. (while (re-search-forward
  5732. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5733. limit t)
  5734. (if (and (not (org-at-comment-p))
  5735. (setq ee (org-entity-get (match-string 1)))
  5736. (= (length (nth 6 ee)) 1))
  5737. (let*
  5738. ((end (if (equal (match-string 2) "{}")
  5739. (match-end 2)
  5740. (match-end 1))))
  5741. (add-text-properties
  5742. (match-beginning 0) end
  5743. (list 'font-lock-fontified t))
  5744. (compose-region (match-beginning 0) end
  5745. (nth 6 ee) nil)
  5746. (backward-char 1)
  5747. (throw 'match t))))
  5748. nil))))
  5749. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5750. "Fontify string S like in Org-mode."
  5751. (with-temp-buffer
  5752. (insert s)
  5753. (let ((org-odd-levels-only odd-levels))
  5754. (org-mode)
  5755. (font-lock-ensure)
  5756. (buffer-string))))
  5757. (defvar org-m nil)
  5758. (defvar org-l nil)
  5759. (defvar org-f nil)
  5760. (defun org-get-level-face (n)
  5761. "Get the right face for match N in font-lock matching of headlines."
  5762. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5763. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5764. (if org-cycle-level-faces
  5765. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5766. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5767. (cond
  5768. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5769. ((eq n 2) org-f)
  5770. (t (if org-level-color-stars-only nil org-f))))
  5771. (defun org-get-todo-face (kwd)
  5772. "Get the right face for a TODO keyword KWD.
  5773. If KWD is a number, get the corresponding match group."
  5774. (if (numberp kwd) (setq kwd (match-string kwd)))
  5775. (or (org-face-from-face-or-color
  5776. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5777. (and (member kwd org-done-keywords) 'org-done)
  5778. 'org-todo))
  5779. (defun org-face-from-face-or-color (context inherit face-or-color)
  5780. "Create a face list that inherits INHERIT, but sets the foreground color.
  5781. When FACE-OR-COLOR is not a string, just return it."
  5782. (if (stringp face-or-color)
  5783. (list :inherit inherit
  5784. (cdr (assoc context org-faces-easy-properties))
  5785. face-or-color)
  5786. face-or-color))
  5787. (defun org-font-lock-add-tag-faces (limit)
  5788. "Add the special tag faces."
  5789. (when (and org-tag-faces org-tags-special-faces-re)
  5790. (while (re-search-forward org-tags-special-faces-re limit t)
  5791. (add-text-properties (match-beginning 1) (match-end 1)
  5792. (list 'face (org-get-tag-face 1)
  5793. 'font-lock-fontified t))
  5794. (backward-char 1))))
  5795. (defun org-font-lock-add-priority-faces (limit)
  5796. "Add the special priority faces."
  5797. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5798. (when (save-match-data (org-at-heading-p))
  5799. (add-text-properties
  5800. (match-beginning 0) (match-end 0)
  5801. (list 'face (or (org-face-from-face-or-color
  5802. 'priority 'org-priority
  5803. (cdr (assoc (char-after (match-beginning 1))
  5804. org-priority-faces)))
  5805. 'org-priority)
  5806. 'font-lock-fontified t)))))
  5807. (defun org-get-tag-face (kwd)
  5808. "Get the right face for a TODO keyword KWD.
  5809. If KWD is a number, get the corresponding match group."
  5810. (if (numberp kwd) (setq kwd (match-string kwd)))
  5811. (or (org-face-from-face-or-color
  5812. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5813. 'org-tag))
  5814. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5815. "Remove fontification and activation overlays from links."
  5816. (font-lock-default-unfontify-region beg end)
  5817. (let* ((buffer-undo-list t)
  5818. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5819. (inhibit-modification-hooks t)
  5820. deactivate-mark buffer-file-name buffer-file-truename)
  5821. (org-decompose-region beg end)
  5822. (remove-text-properties beg end
  5823. '(mouse-face t keymap t org-linked-text t
  5824. invisible t intangible t
  5825. org-emphasis t))
  5826. (org-remove-font-lock-display-properties beg end)))
  5827. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5828. ((raise 0.3) (height 0.7))
  5829. ((raise -0.5))
  5830. ((raise 0.5)))
  5831. "Display properties for showing superscripts and subscripts.")
  5832. (defun org-remove-font-lock-display-properties (beg end)
  5833. "Remove specific display properties that have been added by font lock.
  5834. The will remove the raise properties that are used to show superscripts
  5835. and subscripts."
  5836. (let (next prop)
  5837. (while (< beg end)
  5838. (setq next (next-single-property-change beg 'display nil end)
  5839. prop (get-text-property beg 'display))
  5840. (if (member prop org-script-display)
  5841. (put-text-property beg next 'display nil))
  5842. (setq beg next))))
  5843. (defun org-raise-scripts (limit)
  5844. "Add raise properties to sub/superscripts."
  5845. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5846. (if (re-search-forward
  5847. (if (eq org-use-sub-superscripts t)
  5848. org-match-substring-regexp
  5849. org-match-substring-with-braces-regexp)
  5850. limit t)
  5851. (let* ((pos (point)) table-p comment-p
  5852. (mpos (match-beginning 3))
  5853. (emph-p (get-text-property mpos 'org-emphasis))
  5854. (link-p (get-text-property mpos 'mouse-face))
  5855. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5856. (goto-char (point-at-bol))
  5857. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5858. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5859. (goto-char pos)
  5860. ;; Handle a_b^c
  5861. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5862. (if (or comment-p emph-p link-p keyw-p)
  5863. t
  5864. (put-text-property (match-beginning 3) (match-end 0)
  5865. 'display
  5866. (if (equal (char-after (match-beginning 2)) ?^)
  5867. (nth (if table-p 3 1) org-script-display)
  5868. (nth (if table-p 2 0) org-script-display)))
  5869. (add-text-properties (match-beginning 2) (match-end 2)
  5870. (list 'invisible t
  5871. 'org-dwidth t 'org-dwidth-n 1))
  5872. (if (and (eq (char-after (match-beginning 3)) ?{)
  5873. (eq (char-before (match-end 3)) ?}))
  5874. (progn
  5875. (add-text-properties
  5876. (match-beginning 3) (1+ (match-beginning 3))
  5877. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5878. (add-text-properties
  5879. (1- (match-end 3)) (match-end 3)
  5880. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5881. t)))))
  5882. ;;;; Visibility cycling, including org-goto and indirect buffer
  5883. ;;; Cycling
  5884. (defvar org-cycle-global-status nil)
  5885. (make-variable-buffer-local 'org-cycle-global-status)
  5886. (put 'org-cycle-global-status 'org-state t)
  5887. (defvar org-cycle-subtree-status nil)
  5888. (make-variable-buffer-local 'org-cycle-subtree-status)
  5889. (put 'org-cycle-subtree-status 'org-state t)
  5890. (defvar org-inlinetask-min-level)
  5891. (defun org-unlogged-message (&rest args)
  5892. "Display a message, but avoid logging it in the *Messages* buffer."
  5893. (let ((message-log-max nil))
  5894. (apply 'message args)))
  5895. ;;;###autoload
  5896. (defun org-cycle (&optional arg)
  5897. "TAB-action and visibility cycling for Org-mode.
  5898. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5899. is outline visibility cycling, but it also invokes other actions
  5900. in special contexts.
  5901. - When this function is called with a prefix argument, rotate the entire
  5902. buffer through 3 states (global cycling)
  5903. 1. OVERVIEW: Show only top-level headlines.
  5904. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5905. 3. SHOW ALL: Show everything.
  5906. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5907. determined by the variable `org-startup-folded', and by any VISIBILITY
  5908. properties in the buffer.
  5909. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5910. including any drawers.
  5911. - When inside a table, re-align the table and move to the next field.
  5912. - When point is at the beginning of a headline, rotate the subtree started
  5913. by this line through 3 different states (local cycling)
  5914. 1. FOLDED: Only the main headline is shown.
  5915. 2. CHILDREN: The main headline and the direct children are shown.
  5916. From this state, you can move to one of the children
  5917. and zoom in further.
  5918. 3. SUBTREE: Show the entire subtree, including body text.
  5919. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5920. - When point is at the beginning of an empty headline and the variable
  5921. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5922. of the headline by demoting and promoting it to likely levels. This
  5923. speeds up creation document structure by pressing TAB once or several
  5924. times right after creating a new headline.
  5925. - When there is a numeric prefix, go up to a heading with level ARG, do
  5926. a `show-subtree' and return to the previous cursor position. If ARG
  5927. is negative, go up that many levels.
  5928. - When point is not at the beginning of a headline, execute the global
  5929. binding for TAB, which is re-indenting the line. See the option
  5930. `org-cycle-emulate-tab' for details.
  5931. - Special case: if point is at the beginning of the buffer and there is
  5932. no headline in line 1, this function will act as if called with prefix arg
  5933. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5934. But only if also the variable `org-cycle-global-at-bob' is t."
  5935. (interactive "P")
  5936. (org-load-modules-maybe)
  5937. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5938. (and org-cycle-level-after-item/entry-creation
  5939. (or (org-cycle-level)
  5940. (org-cycle-item-indentation))))
  5941. (let* ((limit-level
  5942. (or org-cycle-max-level
  5943. (and (boundp 'org-inlinetask-min-level)
  5944. org-inlinetask-min-level
  5945. (1- org-inlinetask-min-level))))
  5946. (nstars (and limit-level
  5947. (if org-odd-levels-only
  5948. (and limit-level (1- (* limit-level 2)))
  5949. limit-level)))
  5950. (org-outline-regexp
  5951. (if (not (derived-mode-p 'org-mode))
  5952. outline-regexp
  5953. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5954. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5955. (not (looking-at org-outline-regexp))))
  5956. (org-cycle-hook
  5957. (if bob-special
  5958. (delq 'org-optimize-window-after-visibility-change
  5959. (copy-sequence org-cycle-hook))
  5960. org-cycle-hook))
  5961. (pos (point)))
  5962. (if (or bob-special (equal arg '(4)))
  5963. ;; special case: use global cycling
  5964. (setq arg t))
  5965. (cond
  5966. ((equal arg '(16))
  5967. (setq last-command 'dummy)
  5968. (org-set-startup-visibility)
  5969. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5970. ((equal arg '(64))
  5971. (show-all)
  5972. (org-unlogged-message "Entire buffer visible, including drawers"))
  5973. ;; Try cdlatex TAB completion
  5974. ((org-try-cdlatex-tab))
  5975. ;; Table: enter it or move to the next field.
  5976. ((org-at-table-p 'any)
  5977. (if (org-at-table.el-p)
  5978. (message "Use C-c ' to edit table.el tables")
  5979. (if arg (org-table-edit-field t)
  5980. (org-table-justify-field-maybe)
  5981. (call-interactively 'org-table-next-field))))
  5982. ((run-hook-with-args-until-success
  5983. 'org-tab-after-check-for-table-hook))
  5984. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5985. ((eq arg t) (org-cycle-internal-global))
  5986. ;; Drawers: delegate to `org-flag-drawer'.
  5987. ((save-excursion
  5988. (beginning-of-line 1)
  5989. (looking-at org-drawer-regexp))
  5990. (org-flag-drawer ; toggle block visibility
  5991. (not (get-char-property (match-end 0) 'invisible))))
  5992. ;; Show-subtree, ARG levels up from here.
  5993. ((integerp arg)
  5994. (save-excursion
  5995. (org-back-to-heading)
  5996. (outline-up-heading (if (< arg 0) (- arg)
  5997. (- (funcall outline-level) arg)))
  5998. (org-show-subtree)))
  5999. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  6000. ((and (featurep 'org-inlinetask)
  6001. (org-inlinetask-at-task-p)
  6002. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  6003. (org-inlinetask-toggle-visibility))
  6004. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  6005. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  6006. (save-excursion (beginning-of-line 1)
  6007. (looking-at org-outline-regexp)))
  6008. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  6009. (org-cycle-internal-local))
  6010. ;; From there: TAB emulation and template completion.
  6011. (buffer-read-only (org-back-to-heading))
  6012. ((run-hook-with-args-until-success
  6013. 'org-tab-after-check-for-cycling-hook))
  6014. ((org-try-structure-completion))
  6015. ((run-hook-with-args-until-success
  6016. 'org-tab-before-tab-emulation-hook))
  6017. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  6018. (or (not (bolp))
  6019. (not (looking-at org-outline-regexp))))
  6020. (call-interactively (global-key-binding "\t")))
  6021. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  6022. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  6023. (or (and (eq org-cycle-emulate-tab 'white)
  6024. (= (match-end 0) (point-at-eol)))
  6025. (and (eq org-cycle-emulate-tab 'whitestart)
  6026. (>= (match-end 0) pos))))
  6027. t
  6028. (eq org-cycle-emulate-tab t))
  6029. (call-interactively (global-key-binding "\t")))
  6030. (t (save-excursion
  6031. (org-back-to-heading)
  6032. (org-cycle)))))))
  6033. (defun org-cycle-internal-global ()
  6034. "Do the global cycling action."
  6035. ;; Hack to avoid display of messages for .org attachments in Gnus
  6036. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6037. (cond
  6038. ((and (eq last-command this-command)
  6039. (eq org-cycle-global-status 'overview))
  6040. ;; We just created the overview - now do table of contents
  6041. ;; This can be slow in very large buffers, so indicate action
  6042. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6043. (unless ga (org-unlogged-message "CONTENTS..."))
  6044. (org-content)
  6045. (unless ga (org-unlogged-message "CONTENTS...done"))
  6046. (setq org-cycle-global-status 'contents)
  6047. (run-hook-with-args 'org-cycle-hook 'contents))
  6048. ((and (eq last-command this-command)
  6049. (eq org-cycle-global-status 'contents))
  6050. ;; We just showed the table of contents - now show everything
  6051. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6052. (show-all)
  6053. (unless ga (org-unlogged-message "SHOW ALL"))
  6054. (setq org-cycle-global-status 'all)
  6055. (run-hook-with-args 'org-cycle-hook 'all))
  6056. (t
  6057. ;; Default action: go to overview
  6058. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6059. (org-overview)
  6060. (unless ga (org-unlogged-message "OVERVIEW"))
  6061. (setq org-cycle-global-status 'overview)
  6062. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6063. (defvar org-called-with-limited-levels nil
  6064. "Non-nil when `org-with-limited-levels' is currently active.")
  6065. (defun org-cycle-internal-local ()
  6066. "Do the local cycling action."
  6067. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6068. ;; First, determine end of headline (EOH), end of subtree or item
  6069. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6070. (save-excursion
  6071. (if (org-at-item-p)
  6072. (progn
  6073. (beginning-of-line)
  6074. (setq struct (org-list-struct))
  6075. (setq eoh (point-at-eol))
  6076. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6077. (setq has-children (org-list-has-child-p (point) struct)))
  6078. (org-back-to-heading)
  6079. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6080. (setq eos (save-excursion (org-end-of-subtree t t)
  6081. (when (bolp) (backward-char)) (point)))
  6082. (setq has-children
  6083. (or (save-excursion
  6084. (let ((level (funcall outline-level)))
  6085. (outline-next-heading)
  6086. (and (org-at-heading-p t)
  6087. (> (funcall outline-level) level))))
  6088. (save-excursion
  6089. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6090. ;; Determine end invisible part of buffer (EOL)
  6091. (beginning-of-line 2)
  6092. ;; XEmacs doesn't have `next-single-char-property-change'
  6093. (if (featurep 'xemacs)
  6094. (while (and (not (eobp)) ;; this is like `next-line'
  6095. (get-char-property (1- (point)) 'invisible))
  6096. (beginning-of-line 2))
  6097. (while (and (not (eobp)) ;; this is like `next-line'
  6098. (get-char-property (1- (point)) 'invisible))
  6099. (goto-char (next-single-char-property-change (point) 'invisible))
  6100. (and (eolp) (beginning-of-line 2))))
  6101. (setq eol (point)))
  6102. ;; Find out what to do next and set `this-command'
  6103. (cond
  6104. ((= eos eoh)
  6105. ;; Nothing is hidden behind this heading
  6106. (unless (org-before-first-heading-p)
  6107. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6108. (org-unlogged-message "EMPTY ENTRY")
  6109. (setq org-cycle-subtree-status nil)
  6110. (save-excursion
  6111. (goto-char eos)
  6112. (outline-next-heading)
  6113. (if (outline-invisible-p) (org-flag-heading nil))))
  6114. ((and (or (>= eol eos)
  6115. (not (string-match "\\S-" (buffer-substring eol eos))))
  6116. (or has-children
  6117. (not (setq children-skipped
  6118. org-cycle-skip-children-state-if-no-children))))
  6119. ;; Entire subtree is hidden in one line: children view
  6120. (unless (org-before-first-heading-p)
  6121. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6122. (if (org-at-item-p)
  6123. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6124. (org-show-entry)
  6125. (org-with-limited-levels (show-children))
  6126. ;; FIXME: This slows down the func way too much.
  6127. ;; How keep drawers hidden in subtree anyway?
  6128. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6129. ;; (org-cycle-hide-drawers 'subtree))
  6130. ;; Fold every list in subtree to top-level items.
  6131. (when (eq org-cycle-include-plain-lists 'integrate)
  6132. (save-excursion
  6133. (org-back-to-heading)
  6134. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6135. (beginning-of-line 1)
  6136. (let* ((struct (org-list-struct))
  6137. (prevs (org-list-prevs-alist struct))
  6138. (end (org-list-get-bottom-point struct)))
  6139. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6140. (org-list-get-all-items (point) struct prevs))
  6141. (goto-char (if (< end eos) end eos)))))))
  6142. (org-unlogged-message "CHILDREN")
  6143. (save-excursion
  6144. (goto-char eos)
  6145. (outline-next-heading)
  6146. (if (outline-invisible-p) (org-flag-heading nil)))
  6147. (setq org-cycle-subtree-status 'children)
  6148. (unless (org-before-first-heading-p)
  6149. (run-hook-with-args 'org-cycle-hook 'children)))
  6150. ((or children-skipped
  6151. (and (eq last-command this-command)
  6152. (eq org-cycle-subtree-status 'children)))
  6153. ;; We just showed the children, or no children are there,
  6154. ;; now show everything.
  6155. (unless (org-before-first-heading-p)
  6156. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6157. (outline-flag-region eoh eos nil)
  6158. (org-unlogged-message
  6159. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6160. (setq org-cycle-subtree-status 'subtree)
  6161. (unless (org-before-first-heading-p)
  6162. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6163. (t
  6164. ;; Default action: hide the subtree.
  6165. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6166. (outline-flag-region eoh eos t)
  6167. (org-unlogged-message "FOLDED")
  6168. (setq org-cycle-subtree-status 'folded)
  6169. (unless (org-before-first-heading-p)
  6170. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6171. ;;;###autoload
  6172. (defun org-global-cycle (&optional arg)
  6173. "Cycle the global visibility. For details see `org-cycle'.
  6174. With \\[universal-argument] prefix arg, switch to startup visibility.
  6175. With a numeric prefix, show all headlines up to that level."
  6176. (interactive "P")
  6177. (let ((org-cycle-include-plain-lists
  6178. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6179. (cond
  6180. ((integerp arg)
  6181. (show-all)
  6182. (hide-sublevels arg)
  6183. (setq org-cycle-global-status 'contents))
  6184. ((equal arg '(4))
  6185. (org-set-startup-visibility)
  6186. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6187. (t
  6188. (org-cycle '(4))))))
  6189. (defun org-set-startup-visibility ()
  6190. "Set the visibility required by startup options and properties."
  6191. (cond
  6192. ((eq org-startup-folded t)
  6193. (org-overview))
  6194. ((eq org-startup-folded 'content)
  6195. (org-content))
  6196. ((or (eq org-startup-folded 'showeverything)
  6197. (eq org-startup-folded nil))
  6198. (show-all)))
  6199. (unless (eq org-startup-folded 'showeverything)
  6200. (if org-hide-block-startup (org-hide-block-all))
  6201. (org-set-visibility-according-to-property 'no-cleanup)
  6202. (org-cycle-hide-archived-subtrees 'all)
  6203. (org-cycle-hide-drawers 'all)
  6204. (org-cycle-show-empty-lines t)))
  6205. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6206. "Switch subtree visibilities according to :VISIBILITY: property."
  6207. (interactive)
  6208. (let (org-show-entry-below state)
  6209. (save-excursion
  6210. (goto-char (point-min))
  6211. (while (re-search-forward
  6212. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6213. nil t)
  6214. (setq state (match-string 1))
  6215. (save-excursion
  6216. (org-back-to-heading t)
  6217. (hide-subtree)
  6218. (org-reveal)
  6219. (cond
  6220. ((equal state '("fold" "folded"))
  6221. (hide-subtree))
  6222. ((equal state "children")
  6223. (org-show-hidden-entry)
  6224. (show-children))
  6225. ((equal state "content")
  6226. (save-excursion
  6227. (save-restriction
  6228. (org-narrow-to-subtree)
  6229. (org-content))))
  6230. ((member state '("all" "showall"))
  6231. (show-subtree)))))
  6232. (unless no-cleanup
  6233. (org-cycle-hide-archived-subtrees 'all)
  6234. (org-cycle-hide-drawers 'all)
  6235. (org-cycle-show-empty-lines 'all)))))
  6236. ;; This function uses outline-regexp instead of the more fundamental
  6237. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6238. ;; buffers, where outline-regexp is needed.
  6239. (defun org-overview ()
  6240. "Switch to overview mode, showing only top-level headlines.
  6241. This shows all headlines with a level equal or greater than the level
  6242. of the first headline in the buffer. This is important, because if the
  6243. first headline is not level one, then (hide-sublevels 1) gives confusing
  6244. results."
  6245. (interactive)
  6246. (save-excursion
  6247. (let ((level
  6248. (save-excursion
  6249. (goto-char (point-min))
  6250. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6251. (progn
  6252. (goto-char (match-beginning 0))
  6253. (funcall outline-level))))))
  6254. (and level (hide-sublevels level)))))
  6255. (defun org-content (&optional arg)
  6256. "Show all headlines in the buffer, like a table of contents.
  6257. With numerical argument N, show content up to level N."
  6258. (interactive "P")
  6259. (org-overview)
  6260. (save-excursion
  6261. ;; Visit all headings and show their offspring
  6262. (and (integerp arg) (org-overview))
  6263. (goto-char (point-max))
  6264. (catch 'exit
  6265. (while (and (progn (condition-case nil
  6266. (outline-previous-visible-heading 1)
  6267. (error (goto-char (point-min))))
  6268. t)
  6269. (looking-at org-outline-regexp))
  6270. (if (integerp arg)
  6271. (show-children (1- arg))
  6272. (show-branches))
  6273. (if (bobp) (throw 'exit nil))))))
  6274. (defun org-optimize-window-after-visibility-change (state)
  6275. "Adjust the window after a change in outline visibility.
  6276. This function is the default value of the hook `org-cycle-hook'."
  6277. (when (get-buffer-window (current-buffer))
  6278. (cond
  6279. ((eq state 'content) nil)
  6280. ((eq state 'all) nil)
  6281. ((eq state 'folded) nil)
  6282. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6283. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6284. (defun org-remove-empty-overlays-at (pos)
  6285. "Remove outline overlays that do not contain non-white stuff."
  6286. (mapc
  6287. (lambda (o)
  6288. (and (eq 'outline (overlay-get o 'invisible))
  6289. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6290. (overlay-end o))))
  6291. (delete-overlay o)))
  6292. (overlays-at pos)))
  6293. (defun org-clean-visibility-after-subtree-move ()
  6294. "Fix visibility issues after moving a subtree."
  6295. ;; First, find a reasonable region to look at:
  6296. ;; Start two siblings above, end three below
  6297. (let* ((beg (save-excursion
  6298. (and (org-get-last-sibling)
  6299. (org-get-last-sibling))
  6300. (point)))
  6301. (end (save-excursion
  6302. (and (org-get-next-sibling)
  6303. (org-get-next-sibling)
  6304. (org-get-next-sibling))
  6305. (if (org-at-heading-p)
  6306. (point-at-eol)
  6307. (point))))
  6308. (level (looking-at "\\*+"))
  6309. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6310. (save-excursion
  6311. (save-restriction
  6312. (narrow-to-region beg end)
  6313. (when re
  6314. ;; Properly fold already folded siblings
  6315. (goto-char (point-min))
  6316. (while (re-search-forward re nil t)
  6317. (if (and (not (outline-invisible-p))
  6318. (save-excursion
  6319. (goto-char (point-at-eol)) (outline-invisible-p)))
  6320. (hide-entry))))
  6321. (org-cycle-show-empty-lines 'overview)
  6322. (org-cycle-hide-drawers 'overview)))))
  6323. (defun org-cycle-show-empty-lines (state)
  6324. "Show empty lines above all visible headlines.
  6325. The region to be covered depends on STATE when called through
  6326. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6327. entire buffer covered. Note that an empty line is only shown if there
  6328. are at least `org-cycle-separator-lines' empty lines before the headline."
  6329. (when (not (= org-cycle-separator-lines 0))
  6330. (save-excursion
  6331. (let* ((n (abs org-cycle-separator-lines))
  6332. (re (cond
  6333. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6334. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6335. (t (let ((ns (number-to-string (- n 2))))
  6336. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6337. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6338. beg end b e)
  6339. (cond
  6340. ((memq state '(overview contents t))
  6341. (setq beg (point-min) end (point-max)))
  6342. ((memq state '(children folded))
  6343. (setq beg (point) end (progn (org-end-of-subtree t t)
  6344. (beginning-of-line 2)
  6345. (point)))))
  6346. (when beg
  6347. (goto-char beg)
  6348. (while (re-search-forward re end t)
  6349. (unless (get-char-property (match-end 1) 'invisible)
  6350. (setq e (match-end 1))
  6351. (if (< org-cycle-separator-lines 0)
  6352. (setq b (save-excursion
  6353. (goto-char (match-beginning 0))
  6354. (org-back-over-empty-lines)
  6355. (if (save-excursion
  6356. (goto-char (max (point-min) (1- (point))))
  6357. (org-at-heading-p))
  6358. (1- (point))
  6359. (point))))
  6360. (setq b (match-beginning 1)))
  6361. (outline-flag-region b e nil)))))))
  6362. ;; Never hide empty lines at the end of the file.
  6363. (save-excursion
  6364. (goto-char (point-max))
  6365. (outline-previous-heading)
  6366. (outline-end-of-heading)
  6367. (if (and (looking-at "[ \t\n]+")
  6368. (= (match-end 0) (point-max)))
  6369. (outline-flag-region (point) (match-end 0) nil))))
  6370. (defun org-show-empty-lines-in-parent ()
  6371. "Move to the parent and re-show empty lines before visible headlines."
  6372. (save-excursion
  6373. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6374. (org-cycle-show-empty-lines context))))
  6375. (defun org-files-list ()
  6376. "Return `org-agenda-files' list, plus all open org-mode files.
  6377. This is useful for operations that need to scan all of a user's
  6378. open and agenda-wise Org files."
  6379. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6380. (dolist (buf (buffer-list))
  6381. (with-current-buffer buf
  6382. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6383. (let ((file (expand-file-name (buffer-file-name))))
  6384. (unless (member file files)
  6385. (push file files))))))
  6386. files))
  6387. (defsubst org-entry-beginning-position ()
  6388. "Return the beginning position of the current entry."
  6389. (save-excursion (outline-back-to-heading t) (point)))
  6390. (defsubst org-entry-end-position ()
  6391. "Return the end position of the current entry."
  6392. (save-excursion (outline-next-heading) (point)))
  6393. (defun org-cycle-hide-drawers (state &optional exceptions)
  6394. "Re-hide all drawers after a visibility state change.
  6395. When non-nil, optional argument EXCEPTIONS is a list of strings
  6396. specifying which drawers should not be hidden."
  6397. (when (and (derived-mode-p 'org-mode)
  6398. (not (memq state '(overview folded contents))))
  6399. (save-excursion
  6400. (let* ((globalp (memq state '(contents all)))
  6401. (beg (if globalp (point-min) (point)))
  6402. (end (if globalp (point-max)
  6403. (if (eq state 'children)
  6404. (save-excursion (outline-next-heading) (point))
  6405. (org-end-of-subtree t)))))
  6406. (goto-char beg)
  6407. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6408. (unless (member-ignore-case (match-string 1) exceptions)
  6409. (let ((drawer (org-element-at-point)))
  6410. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6411. (org-flag-drawer t drawer)
  6412. ;; Make sure to skip drawer entirely or we might flag
  6413. ;; it another time when matching its ending line with
  6414. ;; `org-drawer-regexp'.
  6415. (goto-char (org-element-property :end drawer))))))))))
  6416. (defun org-cycle-hide-inline-tasks (state)
  6417. "Re-hide inline tasks when switching to 'contents or 'children
  6418. visibility state."
  6419. (case state
  6420. (contents
  6421. (when (org-bound-and-true-p org-inlinetask-min-level)
  6422. (hide-sublevels (1- org-inlinetask-min-level))))
  6423. (children
  6424. (when (featurep 'org-inlinetask)
  6425. (save-excursion
  6426. (while (and (outline-next-heading)
  6427. (org-inlinetask-at-task-p))
  6428. (org-inlinetask-toggle-visibility)
  6429. (org-inlinetask-goto-end)))))))
  6430. (defun org-flag-drawer (flag &optional element)
  6431. "When FLAG is non-nil, hide the drawer we are at.
  6432. Otherwise make it visible. When optional argument ELEMENT is
  6433. a parsed drawer, as returned by `org-element-at-point', hide or
  6434. show that drawer instead."
  6435. (when (save-excursion
  6436. (beginning-of-line)
  6437. (org-looking-at-p org-drawer-regexp))
  6438. (let ((drawer (or element (org-element-at-point))))
  6439. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6440. (let ((post (org-element-property :post-affiliated drawer)))
  6441. (save-excursion
  6442. (outline-flag-region
  6443. (progn (goto-char post) (line-end-position))
  6444. (progn (goto-char (org-element-property :end drawer))
  6445. (skip-chars-backward " \r\t\n")
  6446. (line-end-position))
  6447. flag))
  6448. ;; When the drawer is hidden away, make sure point lies in
  6449. ;; a visible part of the buffer.
  6450. (when (and flag (> (line-beginning-position) post))
  6451. (goto-char post)))))))
  6452. (defun org-subtree-end-visible-p ()
  6453. "Is the end of the current subtree visible?"
  6454. (pos-visible-in-window-p
  6455. (save-excursion (org-end-of-subtree t) (point))))
  6456. (defun org-first-headline-recenter ()
  6457. "Move cursor to the first headline and recenter the headline."
  6458. (goto-char (point-min))
  6459. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6460. (set-window-start (selected-window) (point-at-bol))))
  6461. ;;; Saving and restoring visibility
  6462. (defun org-outline-overlay-data (&optional use-markers)
  6463. "Return a list of the locations of all outline overlays.
  6464. These are overlays with the `invisible' property value `outline'.
  6465. The return value is a list of cons cells, with start and stop
  6466. positions for each overlay.
  6467. If USE-MARKERS is set, return the positions as markers."
  6468. (let (beg end)
  6469. (save-excursion
  6470. (save-restriction
  6471. (widen)
  6472. (delq nil
  6473. (mapcar (lambda (o)
  6474. (when (eq (overlay-get o 'invisible) 'outline)
  6475. (setq beg (overlay-start o)
  6476. end (overlay-end o))
  6477. (and beg end (> end beg)
  6478. (if use-markers
  6479. (cons (copy-marker beg)
  6480. (copy-marker end t))
  6481. (cons beg end)))))
  6482. (overlays-in (point-min) (point-max))))))))
  6483. (defun org-set-outline-overlay-data (data)
  6484. "Create visibility overlays for all positions in DATA.
  6485. DATA should have been made by `org-outline-overlay-data'."
  6486. (let (o)
  6487. (save-excursion
  6488. (save-restriction
  6489. (widen)
  6490. (show-all)
  6491. (mapc (lambda (c)
  6492. (outline-flag-region (car c) (cdr c) t))
  6493. data)))))
  6494. ;;; Folding of blocks
  6495. (defvar org-hide-block-overlays nil
  6496. "Overlays hiding blocks.")
  6497. (make-variable-buffer-local 'org-hide-block-overlays)
  6498. (defun org-block-map (function &optional start end)
  6499. "Call FUNCTION at the head of all source blocks in the current buffer.
  6500. Optional arguments START and END can be used to limit the range."
  6501. (let ((start (or start (point-min)))
  6502. (end (or end (point-max))))
  6503. (save-excursion
  6504. (goto-char start)
  6505. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6506. (save-excursion
  6507. (save-match-data
  6508. (goto-char (match-beginning 0))
  6509. (funcall function)))))))
  6510. (defun org-hide-block-toggle-all ()
  6511. "Toggle the visibility of all blocks in the current buffer."
  6512. (org-block-map 'org-hide-block-toggle))
  6513. (defun org-hide-block-all ()
  6514. "Fold all blocks in the current buffer."
  6515. (interactive)
  6516. (org-show-block-all)
  6517. (org-block-map 'org-hide-block-toggle-maybe))
  6518. (defun org-show-block-all ()
  6519. "Unfold all blocks in the current buffer."
  6520. (interactive)
  6521. (mapc 'delete-overlay org-hide-block-overlays)
  6522. (setq org-hide-block-overlays nil))
  6523. (defun org-hide-block-toggle-maybe ()
  6524. "Toggle visibility of block at point.
  6525. Unlike to `org-hide-block-toggle', this function does not throw
  6526. an error. Return a non-nil value when toggling is successful."
  6527. (interactive)
  6528. (ignore-errors (org-hide-block-toggle)))
  6529. (defun org-hide-block-toggle (&optional force)
  6530. "Toggle the visibility of the current block.
  6531. When optional argument FORCE is `off', make block visible. If it
  6532. is non-nil, hide it unconditionally. Throw an error when not at
  6533. a block. Return a non-nil value when toggling is successful."
  6534. (interactive)
  6535. (let ((element (org-element-at-point)))
  6536. (unless (memq (org-element-type element)
  6537. '(center-block comment-block dynamic-block example-block
  6538. export-block quote-block special-block
  6539. src-block verse-block))
  6540. (user-error "Not at a block"))
  6541. (let* ((start (save-excursion
  6542. (goto-char (org-element-property :post-affiliated element))
  6543. (line-end-position)))
  6544. (end (save-excursion
  6545. (goto-char (org-element-property :end element))
  6546. (skip-chars-backward " \r\t\n")
  6547. (line-end-position)))
  6548. (overlays (overlays-at start)))
  6549. (cond
  6550. ;; Do nothing when not before or at the block opening line or
  6551. ;; at the block closing line.
  6552. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6553. ((and (not (eq force 'off))
  6554. (not (memq t (mapcar
  6555. (lambda (o)
  6556. (eq (overlay-get o 'invisible) 'org-hide-block))
  6557. overlays))))
  6558. (let ((ov (make-overlay start end)))
  6559. (overlay-put ov 'invisible 'org-hide-block)
  6560. ;; Make the block accessible to `isearch'.
  6561. (overlay-put
  6562. ov 'isearch-open-invisible
  6563. (lambda (ov)
  6564. (when (memq ov org-hide-block-overlays)
  6565. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6566. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6567. (delete-overlay ov))))
  6568. (push ov org-hide-block-overlays)
  6569. ;; When the block is hidden away, make sure point is left in
  6570. ;; a visible part of the buffer.
  6571. (when (> (line-beginning-position) start)
  6572. (goto-char start)
  6573. (beginning-of-line))
  6574. ;; Signal successful toggling.
  6575. t))
  6576. ((or (not force) (eq force 'off))
  6577. (dolist (ov overlays t)
  6578. (when (memq ov org-hide-block-overlays)
  6579. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6580. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6581. (delete-overlay ov))))))))
  6582. ;; org-tab-after-check-for-cycling-hook
  6583. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6584. ;; Remove overlays when changing major mode
  6585. (add-hook 'org-mode-hook
  6586. (lambda () (org-add-hook 'change-major-mode-hook
  6587. 'org-show-block-all 'append 'local)))
  6588. ;;; Org-goto
  6589. (defvar org-goto-window-configuration nil)
  6590. (defvar org-goto-marker nil)
  6591. (defvar org-goto-map)
  6592. (defun org-goto-map ()
  6593. "Set the keymap `org-goto'."
  6594. (setq org-goto-map
  6595. (let ((map (make-sparse-keymap)))
  6596. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6597. mouse-drag-region universal-argument org-occur))
  6598. cmd)
  6599. (while (setq cmd (pop cmds))
  6600. (substitute-key-definition cmd cmd map global-map)))
  6601. (suppress-keymap map)
  6602. (org-defkey map "\C-m" 'org-goto-ret)
  6603. (org-defkey map [(return)] 'org-goto-ret)
  6604. (org-defkey map [(left)] 'org-goto-left)
  6605. (org-defkey map [(right)] 'org-goto-right)
  6606. (org-defkey map [(control ?g)] 'org-goto-quit)
  6607. (org-defkey map "\C-i" 'org-cycle)
  6608. (org-defkey map [(tab)] 'org-cycle)
  6609. (org-defkey map [(down)] 'outline-next-visible-heading)
  6610. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6611. (if org-goto-auto-isearch
  6612. (if (fboundp 'define-key-after)
  6613. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6614. nil)
  6615. (org-defkey map "q" 'org-goto-quit)
  6616. (org-defkey map "n" 'outline-next-visible-heading)
  6617. (org-defkey map "p" 'outline-previous-visible-heading)
  6618. (org-defkey map "f" 'outline-forward-same-level)
  6619. (org-defkey map "b" 'outline-backward-same-level)
  6620. (org-defkey map "u" 'outline-up-heading))
  6621. (org-defkey map "/" 'org-occur)
  6622. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6623. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6624. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6625. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6626. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6627. map)))
  6628. (defconst org-goto-help
  6629. "Browse buffer copy, to find location or copy text.%s
  6630. RET=jump to location C-g=quit and return to previous location
  6631. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6632. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6633. (defun org-goto (&optional alternative-interface)
  6634. "Look up a different location in the current file, keeping current visibility.
  6635. When you want look-up or go to a different location in a
  6636. document, the fastest way is often to fold the entire buffer and
  6637. then dive into the tree. This method has the disadvantage, that
  6638. the previous location will be folded, which may not be what you
  6639. want.
  6640. This command works around this by showing a copy of the current
  6641. buffer in an indirect buffer, in overview mode. You can dive
  6642. into the tree in that copy, use org-occur and incremental search
  6643. to find a location. When pressing RET or `Q', the command
  6644. returns to the original buffer in which the visibility is still
  6645. unchanged. After RET it will also jump to the location selected
  6646. in the indirect buffer and expose the headline hierarchy above.
  6647. With a prefix argument, use the alternative interface: e.g. if
  6648. `org-goto-interface' is 'outline use 'outline-path-completion."
  6649. (interactive "P")
  6650. (org-goto-map)
  6651. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6652. (org-refile-use-outline-path t)
  6653. (org-refile-target-verify-function nil)
  6654. (interface
  6655. (if (not alternative-interface)
  6656. org-goto-interface
  6657. (if (eq org-goto-interface 'outline)
  6658. 'outline-path-completion
  6659. 'outline)))
  6660. (org-goto-start-pos (point))
  6661. (selected-point
  6662. (if (eq interface 'outline)
  6663. (car (org-get-location (current-buffer) org-goto-help))
  6664. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6665. (org-refile-check-position pa)
  6666. (nth 3 pa)))))
  6667. (if selected-point
  6668. (progn
  6669. (org-mark-ring-push org-goto-start-pos)
  6670. (goto-char selected-point)
  6671. (if (or (outline-invisible-p) (org-invisible-p2))
  6672. (org-show-context 'org-goto)))
  6673. (message "Quit"))))
  6674. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6675. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6676. (defvar org-goto-local-auto-isearch-map) ; defined below
  6677. (defun org-get-location (buf help)
  6678. "Let the user select a location in the Org-mode buffer BUF.
  6679. This function uses a recursive edit. It returns the selected position
  6680. or nil."
  6681. (org-no-popups
  6682. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6683. (isearch-hide-immediately nil)
  6684. (isearch-search-fun-function
  6685. (lambda () 'org-goto-local-search-headings))
  6686. (org-goto-selected-point org-goto-exit-command))
  6687. (save-excursion
  6688. (save-window-excursion
  6689. (delete-other-windows)
  6690. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6691. (org-pop-to-buffer-same-window
  6692. (condition-case nil
  6693. (make-indirect-buffer (current-buffer) "*org-goto*")
  6694. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6695. (with-output-to-temp-buffer "*Org Help*"
  6696. (princ (format help (if org-goto-auto-isearch
  6697. " Just type for auto-isearch."
  6698. " n/p/f/b/u to navigate, q to quit."))))
  6699. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6700. (setq buffer-read-only nil)
  6701. (let ((org-startup-truncated t)
  6702. (org-startup-folded nil)
  6703. (org-startup-align-all-tables nil))
  6704. (org-mode)
  6705. (org-overview))
  6706. (setq buffer-read-only t)
  6707. (if (and (boundp 'org-goto-start-pos)
  6708. (integer-or-marker-p org-goto-start-pos))
  6709. (let ((org-show-hierarchy-above t)
  6710. (org-show-siblings t)
  6711. (org-show-following-heading t))
  6712. (goto-char org-goto-start-pos)
  6713. (and (outline-invisible-p) (org-show-context)))
  6714. (goto-char (point-min)))
  6715. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6716. (message "Select location and press RET")
  6717. (use-local-map org-goto-map)
  6718. (recursive-edit)))
  6719. (kill-buffer "*org-goto*")
  6720. (cons org-goto-selected-point org-goto-exit-command))))
  6721. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6722. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6723. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6724. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6725. (defun org-goto-local-search-headings (string bound noerror)
  6726. "Search and make sure that any matches are in headlines."
  6727. (catch 'return
  6728. (while (if isearch-forward
  6729. (search-forward string bound noerror)
  6730. (search-backward string bound noerror))
  6731. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6732. (and (member :headline context)
  6733. (not (member :tags context))))
  6734. (throw 'return (point))))))
  6735. (defun org-goto-local-auto-isearch ()
  6736. "Start isearch."
  6737. (interactive)
  6738. (goto-char (point-min))
  6739. (let ((keys (this-command-keys)))
  6740. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6741. (isearch-mode t)
  6742. (isearch-process-search-char (string-to-char keys)))))
  6743. (defun org-goto-ret (&optional arg)
  6744. "Finish `org-goto' by going to the new location."
  6745. (interactive "P")
  6746. (setq org-goto-selected-point (point)
  6747. org-goto-exit-command 'return)
  6748. (throw 'exit nil))
  6749. (defun org-goto-left ()
  6750. "Finish `org-goto' by going to the new location."
  6751. (interactive)
  6752. (if (org-at-heading-p)
  6753. (progn
  6754. (beginning-of-line 1)
  6755. (setq org-goto-selected-point (point)
  6756. org-goto-exit-command 'left)
  6757. (throw 'exit nil))
  6758. (user-error "Not on a heading")))
  6759. (defun org-goto-right ()
  6760. "Finish `org-goto' by going to the new location."
  6761. (interactive)
  6762. (if (org-at-heading-p)
  6763. (progn
  6764. (setq org-goto-selected-point (point)
  6765. org-goto-exit-command 'right)
  6766. (throw 'exit nil))
  6767. (user-error "Not on a heading")))
  6768. (defun org-goto-quit ()
  6769. "Finish `org-goto' without cursor motion."
  6770. (interactive)
  6771. (setq org-goto-selected-point nil)
  6772. (setq org-goto-exit-command 'quit)
  6773. (throw 'exit nil))
  6774. ;;; Indirect buffer display of subtrees
  6775. (defvar org-indirect-dedicated-frame nil
  6776. "This is the frame being used for indirect tree display.")
  6777. (defvar org-last-indirect-buffer nil)
  6778. (defun org-tree-to-indirect-buffer (&optional arg)
  6779. "Create indirect buffer and narrow it to current subtree.
  6780. With a numerical prefix ARG, go up to this level and then take that tree.
  6781. If ARG is negative, go up that many levels.
  6782. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6783. indirect buffer previously made with this command, to avoid proliferation of
  6784. indirect buffers. However, when you call the command with a \
  6785. \\[universal-argument] prefix, or
  6786. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6787. is kept so that you can work with several indirect buffers at the same time.
  6788. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6789. \\[universal-argument] prefix also
  6790. requests that a new frame be made for the new buffer, so that the dedicated
  6791. frame is not changed."
  6792. (interactive "P")
  6793. (let ((cbuf (current-buffer))
  6794. (cwin (selected-window))
  6795. (pos (point))
  6796. beg end level heading ibuf)
  6797. (save-excursion
  6798. (org-back-to-heading t)
  6799. (when (numberp arg)
  6800. (setq level (org-outline-level))
  6801. (if (< arg 0) (setq arg (+ level arg)))
  6802. (while (> (setq level (org-outline-level)) arg)
  6803. (org-up-heading-safe)))
  6804. (setq beg (point)
  6805. heading (org-get-heading))
  6806. (org-end-of-subtree t t)
  6807. (if (org-at-heading-p) (backward-char 1))
  6808. (setq end (point)))
  6809. (if (and (buffer-live-p org-last-indirect-buffer)
  6810. (not (eq org-indirect-buffer-display 'new-frame))
  6811. (not arg))
  6812. (kill-buffer org-last-indirect-buffer))
  6813. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6814. org-last-indirect-buffer ibuf)
  6815. (cond
  6816. ((or (eq org-indirect-buffer-display 'new-frame)
  6817. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6818. (select-frame (make-frame))
  6819. (delete-other-windows)
  6820. (org-pop-to-buffer-same-window ibuf)
  6821. (org-set-frame-title heading))
  6822. ((eq org-indirect-buffer-display 'dedicated-frame)
  6823. (raise-frame
  6824. (select-frame (or (and org-indirect-dedicated-frame
  6825. (frame-live-p org-indirect-dedicated-frame)
  6826. org-indirect-dedicated-frame)
  6827. (setq org-indirect-dedicated-frame (make-frame)))))
  6828. (delete-other-windows)
  6829. (org-pop-to-buffer-same-window ibuf)
  6830. (org-set-frame-title (concat "Indirect: " heading)))
  6831. ((eq org-indirect-buffer-display 'current-window)
  6832. (org-pop-to-buffer-same-window ibuf))
  6833. ((eq org-indirect-buffer-display 'other-window)
  6834. (pop-to-buffer ibuf))
  6835. (t (error "Invalid value")))
  6836. (if (featurep 'xemacs)
  6837. (save-excursion (org-mode) (turn-on-font-lock)))
  6838. (narrow-to-region beg end)
  6839. (show-all)
  6840. (goto-char pos)
  6841. (run-hook-with-args 'org-cycle-hook 'all)
  6842. (and (window-live-p cwin) (select-window cwin))))
  6843. (defun org-get-indirect-buffer (&optional buffer heading)
  6844. (setq buffer (or buffer (current-buffer)))
  6845. (let ((n 1) (base (buffer-name buffer)) bname)
  6846. (while (buffer-live-p
  6847. (get-buffer
  6848. (setq bname
  6849. (concat base "-"
  6850. (if heading (concat heading "-" (number-to-string n))
  6851. (number-to-string n))))))
  6852. (setq n (1+ n)))
  6853. (condition-case nil
  6854. (make-indirect-buffer buffer bname 'clone)
  6855. (error (make-indirect-buffer buffer bname)))))
  6856. (defun org-set-frame-title (title)
  6857. "Set the title of the current frame to the string TITLE."
  6858. ;; FIXME: how to name a single frame in XEmacs???
  6859. (unless (featurep 'xemacs)
  6860. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6861. ;;;; Structure editing
  6862. ;;; Inserting headlines
  6863. (defun org-previous-line-empty-p (&optional next)
  6864. "Is the previous line a blank line?
  6865. When NEXT is non-nil, check the next line instead."
  6866. (save-excursion
  6867. (and (not (bobp))
  6868. (or (beginning-of-line (if next 2 0)) t)
  6869. (save-match-data
  6870. (looking-at "[ \t]*$")))))
  6871. (defun org-insert-heading (&optional arg invisible-ok)
  6872. "Insert a new heading or an item with the same depth at point.
  6873. If point is at the beginning of a heading or a list item, insert
  6874. a new heading or a new item above the current one. If point is
  6875. at the beginning of a normal line, turn the line into a heading.
  6876. If point is in the middle of a headline or a list item, split the
  6877. headline or the item and create a new headline/item with the text
  6878. in the current line after point \(see `org-M-RET-may-split-line'
  6879. on how to modify this behavior).
  6880. With one universal prefix argument, set the user option
  6881. `org-insert-heading-respect-content' to t for the duration of
  6882. the command. This modifies the behavior described above in this
  6883. ways: on list items and at the beginning of normal lines, force
  6884. the insertion of a heading after the current subtree.
  6885. With two universal prefix arguments, insert the heading at the
  6886. end of the grandparent subtree. For example, if point is within
  6887. a 2nd-level heading, then it will insert a 2nd-level heading at
  6888. the end of the 1st-level parent heading.
  6889. If point is at the beginning of a headline, insert a sibling
  6890. before the current headline. If point is not at the beginning,
  6891. split the line and create a new headline with the text in the
  6892. current line after point \(see `org-M-RET-may-split-line' on how
  6893. to modify this behavior).
  6894. If point is at the beginning of a normal line, turn this line
  6895. into a heading.
  6896. When INVISIBLE-OK is set, stop at invisible headlines when going
  6897. back. This is important for non-interactive uses of the
  6898. command."
  6899. (interactive "P")
  6900. (if (org-called-interactively-p 'any) (org-reveal))
  6901. (let ((itemp (org-in-item-p))
  6902. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6903. (respect-content (or org-insert-heading-respect-content
  6904. (equal arg '(4))))
  6905. (initial-content "")
  6906. (adjust-empty-lines t))
  6907. (cond
  6908. ((or (= (buffer-size) 0)
  6909. (and (not (save-excursion
  6910. (and (ignore-errors (org-back-to-heading invisible-ok))
  6911. (org-at-heading-p))))
  6912. (or arg (not itemp))))
  6913. ;; At beginning of buffer or so high up that only a heading
  6914. ;; makes sense.
  6915. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6916. ((not respect-content)
  6917. (unless may-split (end-of-line))
  6918. (insert "\n* "))
  6919. ((re-search-forward org-outline-regexp-bol nil t)
  6920. (beginning-of-line)
  6921. (insert "* \n")
  6922. (backward-char))
  6923. (t (goto-char (point-max))
  6924. (insert "\n* ")))
  6925. (run-hooks 'org-insert-heading-hook))
  6926. ((and itemp (not (member arg '((4) (16)))))
  6927. ;; Insert an item
  6928. (org-insert-item))
  6929. (t
  6930. ;; Maybe move at the end of the subtree
  6931. (when (equal arg '(16))
  6932. (org-up-heading-safe)
  6933. (org-end-of-subtree t))
  6934. ;; Insert a heading
  6935. (save-restriction
  6936. (widen)
  6937. (let* ((level nil)
  6938. (on-heading (org-at-heading-p))
  6939. (empty-line-p (if on-heading
  6940. (org-previous-line-empty-p)
  6941. ;; We will decide later
  6942. nil))
  6943. ;; Get a level string to fall back on
  6944. (fix-level
  6945. (if (org-before-first-heading-p) "*"
  6946. (save-excursion
  6947. (org-back-to-heading t)
  6948. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6949. (looking-at org-outline-regexp)
  6950. (make-string (1- (length (match-string 0))) ?*))))
  6951. (stars
  6952. (save-excursion
  6953. (condition-case nil
  6954. (progn
  6955. (org-back-to-heading invisible-ok)
  6956. (when (and (not on-heading)
  6957. (featurep 'org-inlinetask)
  6958. (integerp org-inlinetask-min-level)
  6959. (>= (length (match-string 0))
  6960. org-inlinetask-min-level))
  6961. ;; Find a heading level before the inline task
  6962. (while (and (setq level (org-up-heading-safe))
  6963. (>= level org-inlinetask-min-level)))
  6964. (if (org-at-heading-p)
  6965. (org-back-to-heading invisible-ok)
  6966. (error "This should not happen")))
  6967. (unless (and (save-excursion
  6968. (save-match-data
  6969. (org-backward-heading-same-level
  6970. 1 invisible-ok))
  6971. (= (point) (match-beginning 0)))
  6972. (not (org-previous-line-empty-p t)))
  6973. (setq empty-line-p (or empty-line-p
  6974. (org-previous-line-empty-p))))
  6975. (match-string 0))
  6976. (error (or fix-level "* ")))))
  6977. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6978. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6979. pos hide-previous previous-pos)
  6980. ;; If we insert after content, move there and clean up whitespace
  6981. (when (and respect-content
  6982. (not (org-looking-at-p org-outline-regexp-bol)))
  6983. (if (not (org-before-first-heading-p))
  6984. (org-end-of-subtree nil t)
  6985. (re-search-forward org-outline-regexp-bol)
  6986. (beginning-of-line 0))
  6987. (skip-chars-backward " \r\n")
  6988. (and (not (looking-back "^\\*+"))
  6989. (looking-at "[ \t]+") (replace-match ""))
  6990. (unless (eobp) (forward-char 1))
  6991. (when (looking-at "^\\*")
  6992. (unless (bobp) (backward-char 1))
  6993. (insert "\n")))
  6994. ;; If we are splitting, grab the text that should be moved to the new headline
  6995. (when may-split
  6996. (if (org-on-heading-p)
  6997. ;; This is a heading, we split intelligently (keeping tags)
  6998. (let ((pos (point)))
  6999. (goto-char (point-at-bol))
  7000. (unless (looking-at org-complex-heading-regexp)
  7001. (error "This should not happen"))
  7002. (when (and (match-beginning 4)
  7003. (> pos (match-beginning 4))
  7004. (< pos (match-end 4)))
  7005. (setq initial-content (buffer-substring pos (match-end 4)))
  7006. (goto-char pos)
  7007. (delete-region (point) (match-end 4))
  7008. (if (looking-at "[ \t]*$")
  7009. (replace-match "")
  7010. (insert (make-string (length initial-content) ?\ )))
  7011. (setq initial-content (org-trim initial-content)))
  7012. (goto-char pos))
  7013. ;; a normal line
  7014. (setq initial-content
  7015. (org-trim (buffer-substring (point) (point-at-eol))))
  7016. (delete-region (point) (point-at-eol))))
  7017. ;; If we are at the beginning of the line, insert before it. Else after
  7018. (cond
  7019. ((and (bolp) (looking-at "[ \t]*$")))
  7020. ((and (bolp) (not (looking-at "[ \t]*$")))
  7021. (open-line 1))
  7022. (t
  7023. (goto-char (point-at-eol))
  7024. (insert "\n")))
  7025. ;; Insert the new heading
  7026. (insert stars)
  7027. (just-one-space)
  7028. (insert initial-content)
  7029. (when adjust-empty-lines
  7030. (if (or (not blank)
  7031. (and blank (not (org-previous-line-empty-p))))
  7032. (org-N-empty-lines-before-current (if blank 1 0))))
  7033. (run-hooks 'org-insert-heading-hook)))))))
  7034. (defun org-N-empty-lines-before-current (N)
  7035. "Make the number of empty lines before current exactly N.
  7036. So this will delete or add empty lines."
  7037. (save-excursion
  7038. (beginning-of-line)
  7039. (let ((p (point)))
  7040. (skip-chars-backward " \r\t\n")
  7041. (unless (bolp) (forward-line))
  7042. (delete-region (point) p))
  7043. (when (> N 0) (insert (make-string N ?\n)))))
  7044. (defun org-get-heading (&optional no-tags no-todo)
  7045. "Return the heading of the current entry, without the stars.
  7046. When NO-TAGS is non-nil, don't include tags.
  7047. When NO-TODO is non-nil, don't include TODO keywords."
  7048. (save-excursion
  7049. (org-back-to-heading t)
  7050. (cond
  7051. ((and no-tags no-todo)
  7052. (looking-at org-complex-heading-regexp)
  7053. (match-string 4))
  7054. (no-tags
  7055. (looking-at (concat org-outline-regexp
  7056. "\\(.*?\\)"
  7057. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7058. (match-string 1))
  7059. (no-todo
  7060. (looking-at org-todo-line-regexp)
  7061. (match-string 3))
  7062. (t (looking-at org-heading-regexp)
  7063. (match-string 2)))))
  7064. (defvar orgstruct-mode) ; defined below
  7065. (defun org-heading-components ()
  7066. "Return the components of the current heading.
  7067. This is a list with the following elements:
  7068. - the level as an integer
  7069. - the reduced level, different if `org-odd-levels-only' is set.
  7070. - the TODO keyword, or nil
  7071. - the priority character, like ?A, or nil if no priority is given
  7072. - the headline text itself, or the tags string if no headline text
  7073. - the tags string, or nil."
  7074. (save-excursion
  7075. (org-back-to-heading t)
  7076. (if (let (case-fold-search)
  7077. (looking-at
  7078. (if orgstruct-mode
  7079. org-heading-regexp
  7080. org-complex-heading-regexp)))
  7081. (if orgstruct-mode
  7082. (list (length (match-string 1))
  7083. (org-reduced-level (length (match-string 1)))
  7084. nil
  7085. nil
  7086. (match-string 2)
  7087. nil)
  7088. (list (length (match-string 1))
  7089. (org-reduced-level (length (match-string 1)))
  7090. (org-match-string-no-properties 2)
  7091. (and (match-end 3) (aref (match-string 3) 2))
  7092. (org-match-string-no-properties 4)
  7093. (org-match-string-no-properties 5))))))
  7094. (defun org-get-entry ()
  7095. "Get the entry text, after heading, entire subtree."
  7096. (save-excursion
  7097. (org-back-to-heading t)
  7098. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7099. (defun org-insert-heading-after-current ()
  7100. "Insert a new heading with same level as current, after current subtree."
  7101. (interactive)
  7102. (org-back-to-heading)
  7103. (org-insert-heading)
  7104. (org-move-subtree-down)
  7105. (end-of-line 1))
  7106. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7107. "Insert heading with `org-insert-heading-respect-content' set to t."
  7108. (interactive)
  7109. (org-insert-heading '(4) invisible-ok))
  7110. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7111. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7112. (interactive)
  7113. (org-insert-todo-heading force-state '(4)))
  7114. (defun org-insert-todo-heading (arg &optional force-heading)
  7115. "Insert a new heading with the same level and TODO state as current heading.
  7116. If the heading has no TODO state, or if the state is DONE, use the first
  7117. state (TODO by default). Also with one prefix arg, force first state. With
  7118. two prefix args, force inserting at the end of the parent subtree."
  7119. (interactive "P")
  7120. (when (or force-heading (not (org-insert-item 'checkbox)))
  7121. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7122. force-heading))
  7123. (save-excursion
  7124. (org-back-to-heading)
  7125. (outline-previous-heading)
  7126. (looking-at org-todo-line-regexp))
  7127. (let*
  7128. ((new-mark-x
  7129. (if (or (equal arg '(4))
  7130. (not (match-beginning 2))
  7131. (member (match-string 2) org-done-keywords))
  7132. (car org-todo-keywords-1)
  7133. (match-string 2)))
  7134. (new-mark
  7135. (or
  7136. (run-hook-with-args-until-success
  7137. 'org-todo-get-default-hook new-mark-x nil)
  7138. new-mark-x)))
  7139. (beginning-of-line 1)
  7140. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7141. (if org-treat-insert-todo-heading-as-state-change
  7142. (org-todo new-mark)
  7143. (insert new-mark " "))))
  7144. (when org-provide-todo-statistics
  7145. (org-update-parent-todo-statistics))))
  7146. (defun org-insert-subheading (arg)
  7147. "Insert a new subheading and demote it.
  7148. Works for outline headings and for plain lists alike."
  7149. (interactive "P")
  7150. (org-insert-heading arg)
  7151. (cond
  7152. ((org-at-heading-p) (org-do-demote))
  7153. ((org-at-item-p) (org-indent-item))))
  7154. (defun org-insert-todo-subheading (arg)
  7155. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7156. Works for outline headings and for plain lists alike."
  7157. (interactive "P")
  7158. (org-insert-todo-heading arg)
  7159. (cond
  7160. ((org-at-heading-p) (org-do-demote))
  7161. ((org-at-item-p) (org-indent-item))))
  7162. ;;; Promotion and Demotion
  7163. (defvar org-after-demote-entry-hook nil
  7164. "Hook run after an entry has been demoted.
  7165. The cursor will be at the beginning of the entry.
  7166. When a subtree is being demoted, the hook will be called for each node.")
  7167. (defvar org-after-promote-entry-hook nil
  7168. "Hook run after an entry has been promoted.
  7169. The cursor will be at the beginning of the entry.
  7170. When a subtree is being promoted, the hook will be called for each node.")
  7171. (defun org-promote-subtree ()
  7172. "Promote the entire subtree.
  7173. See also `org-promote'."
  7174. (interactive)
  7175. (save-excursion
  7176. (org-with-limited-levels (org-map-tree 'org-promote)))
  7177. (org-fix-position-after-promote))
  7178. (defun org-demote-subtree ()
  7179. "Demote the entire subtree. See `org-demote'.
  7180. See also `org-promote'."
  7181. (interactive)
  7182. (save-excursion
  7183. (org-with-limited-levels (org-map-tree 'org-demote)))
  7184. (org-fix-position-after-promote))
  7185. (defun org-do-promote ()
  7186. "Promote the current heading higher up the tree.
  7187. If the region is active in `transient-mark-mode', promote all headings
  7188. in the region."
  7189. (interactive)
  7190. (save-excursion
  7191. (if (org-region-active-p)
  7192. (org-map-region 'org-promote (region-beginning) (region-end))
  7193. (org-promote)))
  7194. (org-fix-position-after-promote))
  7195. (defun org-do-demote ()
  7196. "Demote the current heading lower down the tree.
  7197. If the region is active in `transient-mark-mode', demote all headings
  7198. in the region."
  7199. (interactive)
  7200. (save-excursion
  7201. (if (org-region-active-p)
  7202. (org-map-region 'org-demote (region-beginning) (region-end))
  7203. (org-demote)))
  7204. (org-fix-position-after-promote))
  7205. (defun org-fix-position-after-promote ()
  7206. "Make sure that after pro/demotion cursor position is right."
  7207. (let ((pos (point)))
  7208. (when (save-excursion
  7209. (beginning-of-line 1)
  7210. (looking-at org-todo-line-regexp)
  7211. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7212. (cond ((eobp) (insert " "))
  7213. ((eolp) (insert " "))
  7214. ((equal (char-after) ?\ ) (forward-char 1))))))
  7215. (defun org-current-level ()
  7216. "Return the level of the current entry, or nil if before the first headline.
  7217. The level is the number of stars at the beginning of the headline."
  7218. (save-excursion
  7219. (org-with-limited-levels
  7220. (if (ignore-errors (org-back-to-heading t))
  7221. (funcall outline-level)))))
  7222. (defun org-get-previous-line-level ()
  7223. "Return the outline depth of the last headline before the current line.
  7224. Returns 0 for the first headline in the buffer, and nil if before the
  7225. first headline."
  7226. (and (org-current-level)
  7227. (or (and (/= (line-beginning-position) (point-min))
  7228. (save-excursion (beginning-of-line 0) (org-current-level)))
  7229. 0)))
  7230. (defun org-reduced-level (l)
  7231. "Compute the effective level of a heading.
  7232. This takes into account the setting of `org-odd-levels-only'."
  7233. (cond
  7234. ((zerop l) 0)
  7235. (org-odd-levels-only (1+ (floor (/ l 2))))
  7236. (t l)))
  7237. (defun org-level-increment ()
  7238. "Return the number of stars that will be added or removed at a
  7239. time to headlines when structure editing, based on the value of
  7240. `org-odd-levels-only'."
  7241. (if org-odd-levels-only 2 1))
  7242. (defun org-get-valid-level (level &optional change)
  7243. "Rectify a level change under the influence of `org-odd-levels-only'
  7244. LEVEL is a current level, CHANGE is by how much the level should be
  7245. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7246. even level numbers will become the next higher odd number."
  7247. (if org-odd-levels-only
  7248. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7249. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7250. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7251. (max 1 (+ level (or change 0)))))
  7252. (if (boundp 'define-obsolete-function-alias)
  7253. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7254. (define-obsolete-function-alias 'org-get-legal-level
  7255. 'org-get-valid-level)
  7256. (define-obsolete-function-alias 'org-get-legal-level
  7257. 'org-get-valid-level "23.1")))
  7258. (defun org-promote ()
  7259. "Promote the current heading higher up the tree.
  7260. If the region is active in `transient-mark-mode', promote all headings
  7261. in the region."
  7262. (org-back-to-heading t)
  7263. (let* ((level (save-match-data (funcall outline-level)))
  7264. (after-change-functions (remove 'flyspell-after-change-function
  7265. after-change-functions))
  7266. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7267. (diff (abs (- level (length up-head) -1))))
  7268. (cond ((and (= level 1) org-called-with-limited-levels
  7269. org-allow-promoting-top-level-subtree)
  7270. (replace-match "# " nil t))
  7271. ((= level 1)
  7272. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7273. (t (replace-match up-head nil t)))
  7274. ;; Fixup tag positioning
  7275. (unless (= level 1)
  7276. (and org-auto-align-tags (org-set-tags nil 'ignore-column))
  7277. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7278. (run-hooks 'org-after-promote-entry-hook)))
  7279. (defun org-demote ()
  7280. "Demote the current heading lower down the tree.
  7281. If the region is active in `transient-mark-mode', demote all headings
  7282. in the region."
  7283. (org-back-to-heading t)
  7284. (let* ((level (save-match-data (funcall outline-level)))
  7285. (after-change-functions (remove 'flyspell-after-change-function
  7286. after-change-functions))
  7287. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7288. (diff (abs (- level (length down-head) -1))))
  7289. (replace-match down-head nil t)
  7290. ;; Fixup tag positioning
  7291. (and org-auto-align-tags (org-set-tags nil 'ignore-column))
  7292. (if org-adapt-indentation (org-fixup-indentation diff))
  7293. (run-hooks 'org-after-demote-entry-hook)))
  7294. (defun org-cycle-level ()
  7295. "Cycle the level of an empty headline through possible states.
  7296. This goes first to child, then to parent, level, then up the hierarchy.
  7297. After top level, it switches back to sibling level."
  7298. (interactive)
  7299. (let ((org-adapt-indentation nil))
  7300. (when (org-point-at-end-of-empty-headline)
  7301. (setq this-command 'org-cycle-level) ; Only needed for caching
  7302. (let ((cur-level (org-current-level))
  7303. (prev-level (org-get-previous-line-level)))
  7304. (cond
  7305. ;; If first headline in file, promote to top-level.
  7306. ((= prev-level 0)
  7307. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7308. do (org-do-promote)))
  7309. ;; If same level as prev, demote one.
  7310. ((= prev-level cur-level)
  7311. (org-do-demote))
  7312. ;; If parent is top-level, promote to top level if not already.
  7313. ((= prev-level 1)
  7314. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7315. do (org-do-promote)))
  7316. ;; If top-level, return to prev-level.
  7317. ((= cur-level 1)
  7318. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7319. do (org-do-demote)))
  7320. ;; If less than prev-level, promote one.
  7321. ((< cur-level prev-level)
  7322. (org-do-promote))
  7323. ;; If deeper than prev-level, promote until higher than
  7324. ;; prev-level.
  7325. ((> cur-level prev-level)
  7326. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7327. do (org-do-promote))))
  7328. t))))
  7329. (defun org-map-tree (fun)
  7330. "Call FUN for every heading underneath the current one."
  7331. (org-back-to-heading)
  7332. (let ((level (funcall outline-level)))
  7333. (save-excursion
  7334. (funcall fun)
  7335. (while (and (progn
  7336. (outline-next-heading)
  7337. (> (funcall outline-level) level))
  7338. (not (eobp)))
  7339. (funcall fun)))))
  7340. (defun org-map-region (fun beg end)
  7341. "Call FUN for every heading between BEG and END."
  7342. (let ((org-ignore-region t))
  7343. (save-excursion
  7344. (setq end (copy-marker end))
  7345. (goto-char beg)
  7346. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7347. (< (point) end))
  7348. (funcall fun))
  7349. (while (and (progn
  7350. (outline-next-heading)
  7351. (< (point) end))
  7352. (not (eobp)))
  7353. (funcall fun)))))
  7354. (defvar org-property-end-re) ; silence byte-compiler
  7355. (defun org-fixup-indentation (diff)
  7356. "Change the indentation in the current entry by DIFF.
  7357. However, if any line in the current entry has no indentation, or if it
  7358. would end up with no indentation after the change, nothing at all is done."
  7359. (save-excursion
  7360. (let ((end (save-excursion (outline-next-heading)
  7361. (point-marker)))
  7362. (prohibit (if (> diff 0)
  7363. "^\\S-"
  7364. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7365. col)
  7366. (unless (save-excursion (end-of-line 1)
  7367. (re-search-forward prohibit end t))
  7368. (while (and (< (point) end)
  7369. (re-search-forward "^[ \t]+" end t))
  7370. (goto-char (match-end 0))
  7371. (setq col (current-column))
  7372. (if (< diff 0) (replace-match ""))
  7373. (org-indent-to-column (+ diff col))))
  7374. (move-marker end nil))))
  7375. (defun org-convert-to-odd-levels ()
  7376. "Convert an org-mode file with all levels allowed to one with odd levels.
  7377. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7378. level 5 etc."
  7379. (interactive)
  7380. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7381. (let ((outline-level 'org-outline-level)
  7382. (org-odd-levels-only nil) n)
  7383. (save-excursion
  7384. (goto-char (point-min))
  7385. (while (re-search-forward "^\\*\\*+ " nil t)
  7386. (setq n (- (length (match-string 0)) 2))
  7387. (while (>= (setq n (1- n)) 0)
  7388. (org-demote))
  7389. (end-of-line 1))))))
  7390. (defun org-convert-to-oddeven-levels ()
  7391. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7392. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7393. file contains a section with an even level, conversion would
  7394. destroy the structure of the file. An error is signaled in this
  7395. case."
  7396. (interactive)
  7397. (goto-char (point-min))
  7398. ;; First check if there are no even levels
  7399. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7400. (org-show-context t)
  7401. (error "Not all levels are odd in this file. Conversion not possible"))
  7402. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7403. (let ((outline-regexp org-outline-regexp)
  7404. (outline-level 'org-outline-level)
  7405. (org-odd-levels-only nil) n)
  7406. (save-excursion
  7407. (goto-char (point-min))
  7408. (while (re-search-forward "^\\*\\*+ " nil t)
  7409. (setq n (/ (1- (length (match-string 0))) 2))
  7410. (while (>= (setq n (1- n)) 0)
  7411. (org-promote))
  7412. (end-of-line 1))))))
  7413. (defun org-tr-level (n)
  7414. "Make N odd if required."
  7415. (if org-odd-levels-only (1+ (/ n 2)) n))
  7416. ;;; Vertical tree motion, cutting and pasting of subtrees
  7417. (defun org-move-subtree-up (&optional arg)
  7418. "Move the current subtree up past ARG headlines of the same level."
  7419. (interactive "p")
  7420. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7421. (defun org-move-subtree-down (&optional arg)
  7422. "Move the current subtree down past ARG headlines of the same level."
  7423. (interactive "p")
  7424. (setq arg (prefix-numeric-value arg))
  7425. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7426. 'org-get-last-sibling))
  7427. (ins-point (make-marker))
  7428. (cnt (abs arg))
  7429. (col (current-column))
  7430. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7431. ;; Select the tree
  7432. (org-back-to-heading)
  7433. (setq beg0 (point))
  7434. (save-excursion
  7435. (setq ne-beg (org-back-over-empty-lines))
  7436. (setq beg (point)))
  7437. (save-match-data
  7438. (save-excursion (outline-end-of-heading)
  7439. (setq folded (outline-invisible-p)))
  7440. (progn (org-end-of-subtree nil t)
  7441. (unless (eobp) (backward-char))))
  7442. (outline-next-heading)
  7443. (setq ne-end (org-back-over-empty-lines))
  7444. (setq end (point))
  7445. (goto-char beg0)
  7446. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7447. ;; include less whitespace
  7448. (save-excursion
  7449. (goto-char beg)
  7450. (forward-line (- ne-beg ne-end))
  7451. (setq beg (point))))
  7452. ;; Find insertion point, with error handling
  7453. (while (> cnt 0)
  7454. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7455. (progn (goto-char beg0)
  7456. (user-error "Cannot move past superior level or buffer limit")))
  7457. (setq cnt (1- cnt)))
  7458. (if (> arg 0)
  7459. ;; Moving forward - still need to move over subtree
  7460. (progn (org-end-of-subtree t t)
  7461. (save-excursion
  7462. (org-back-over-empty-lines)
  7463. (or (bolp) (newline)))))
  7464. (setq ne-ins (org-back-over-empty-lines))
  7465. (move-marker ins-point (point))
  7466. (setq txt (buffer-substring beg end))
  7467. (org-save-markers-in-region beg end)
  7468. (delete-region beg end)
  7469. (org-remove-empty-overlays-at beg)
  7470. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7471. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7472. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7473. (let ((bbb (point)))
  7474. (insert-before-markers txt)
  7475. (org-reinstall-markers-in-region bbb)
  7476. (move-marker ins-point bbb))
  7477. (or (bolp) (insert "\n"))
  7478. (setq ins-end (point))
  7479. (goto-char ins-point)
  7480. (org-skip-whitespace)
  7481. (when (and (< arg 0)
  7482. (org-first-sibling-p)
  7483. (> ne-ins ne-beg))
  7484. ;; Move whitespace back to beginning
  7485. (save-excursion
  7486. (goto-char ins-end)
  7487. (let ((kill-whole-line t))
  7488. (kill-line (- ne-ins ne-beg)) (point)))
  7489. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7490. (move-marker ins-point nil)
  7491. (if folded
  7492. (hide-subtree)
  7493. (org-show-entry)
  7494. (show-children)
  7495. (org-cycle-hide-drawers 'children))
  7496. (org-clean-visibility-after-subtree-move)
  7497. ;; move back to the initial column we were at
  7498. (move-to-column col)))
  7499. (defvar org-subtree-clip ""
  7500. "Clipboard for cut and paste of subtrees.
  7501. This is actually only a copy of the kill, because we use the normal kill
  7502. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7503. (defvar org-subtree-clip-folded nil
  7504. "Was the last copied subtree folded?
  7505. This is used to fold the tree back after pasting.")
  7506. (defun org-cut-subtree (&optional n)
  7507. "Cut the current subtree into the clipboard.
  7508. With prefix arg N, cut this many sequential subtrees.
  7509. This is a short-hand for marking the subtree and then cutting it."
  7510. (interactive "p")
  7511. (org-copy-subtree n 'cut))
  7512. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7513. "Copy the current subtree it in the clipboard.
  7514. With prefix arg N, copy this many sequential subtrees.
  7515. This is a short-hand for marking the subtree and then copying it.
  7516. If CUT is non-nil, actually cut the subtree.
  7517. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7518. of some markers in the region, even if CUT is non-nil. This is
  7519. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7520. (interactive "p")
  7521. (let (beg end folded (beg0 (point)))
  7522. (if (org-called-interactively-p 'any)
  7523. (org-back-to-heading nil) ; take what looks like a subtree
  7524. (org-back-to-heading t)) ; take what is really there
  7525. (setq beg (point))
  7526. (skip-chars-forward " \t\r\n")
  7527. (save-match-data
  7528. (if nosubtrees
  7529. (outline-next-heading)
  7530. (save-excursion (outline-end-of-heading)
  7531. (setq folded (outline-invisible-p)))
  7532. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7533. (org-end-of-subtree t t)))
  7534. (setq end (point))
  7535. (goto-char beg0)
  7536. (when (> end beg)
  7537. (setq org-subtree-clip-folded folded)
  7538. (when (or cut force-store-markers)
  7539. (org-save-markers-in-region beg end))
  7540. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7541. (setq org-subtree-clip (current-kill 0))
  7542. (message "%s: Subtree(s) with %d characters"
  7543. (if cut "Cut" "Copied")
  7544. (length org-subtree-clip)))))
  7545. (defun org-paste-subtree (&optional level tree for-yank remove)
  7546. "Paste the clipboard as a subtree, with modification of headline level.
  7547. The entire subtree is promoted or demoted in order to match a new headline
  7548. level.
  7549. If the cursor is at the beginning of a headline, the same level as
  7550. that headline is used to paste the tree.
  7551. If not, the new level is derived from the *visible* headings
  7552. before and after the insertion point, and taken to be the inferior headline
  7553. level of the two. So if the previous visible heading is level 3 and the
  7554. next is level 4 (or vice versa), level 4 will be used for insertion.
  7555. This makes sure that the subtree remains an independent subtree and does
  7556. not swallow low level entries.
  7557. You can also force a different level, either by using a numeric prefix
  7558. argument, or by inserting the heading marker by hand. For example, if the
  7559. cursor is after \"*****\", then the tree will be shifted to level 5.
  7560. If optional TREE is given, use this text instead of the kill ring.
  7561. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7562. move back over whitespace before inserting, and move point to the end of
  7563. the inserted text when done.
  7564. When REMOVE is non-nil, remove the subtree from the clipboard."
  7565. (interactive "P")
  7566. (setq tree (or tree (and kill-ring (current-kill 0))))
  7567. (unless (org-kill-is-subtree-p tree)
  7568. (user-error "%s"
  7569. (substitute-command-keys
  7570. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7571. (org-with-limited-levels
  7572. (let* ((visp (not (outline-invisible-p)))
  7573. (txt tree)
  7574. (^re_ "\\(\\*+\\)[ \t]*")
  7575. (old-level (if (string-match org-outline-regexp-bol txt)
  7576. (- (match-end 0) (match-beginning 0) 1)
  7577. -1))
  7578. (force-level (cond (level (prefix-numeric-value level))
  7579. ((and (looking-at "[ \t]*$")
  7580. (string-match
  7581. "^\\*+$" (buffer-substring
  7582. (point-at-bol) (point))))
  7583. (- (match-end 0) (match-beginning 0)))
  7584. ((and (bolp)
  7585. (looking-at org-outline-regexp))
  7586. (- (match-end 0) (point) 1))))
  7587. (previous-level (save-excursion
  7588. (condition-case nil
  7589. (progn
  7590. (outline-previous-visible-heading 1)
  7591. (if (looking-at ^re_)
  7592. (- (match-end 0) (match-beginning 0) 1)
  7593. 1))
  7594. (error 1))))
  7595. (next-level (save-excursion
  7596. (condition-case nil
  7597. (progn
  7598. (or (looking-at org-outline-regexp)
  7599. (outline-next-visible-heading 1))
  7600. (if (looking-at ^re_)
  7601. (- (match-end 0) (match-beginning 0) 1)
  7602. 1))
  7603. (error 1))))
  7604. (new-level (or force-level (max previous-level next-level)))
  7605. (shift (if (or (= old-level -1)
  7606. (= new-level -1)
  7607. (= old-level new-level))
  7608. 0
  7609. (- new-level old-level)))
  7610. (delta (if (> shift 0) -1 1))
  7611. (func (if (> shift 0) 'org-demote 'org-promote))
  7612. (org-odd-levels-only nil)
  7613. beg end newend)
  7614. ;; Remove the forced level indicator
  7615. (if force-level
  7616. (delete-region (point-at-bol) (point)))
  7617. ;; Paste
  7618. (beginning-of-line (if (bolp) 1 2))
  7619. (setq beg (point))
  7620. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7621. (insert-before-markers txt)
  7622. (unless (string-match "\n\\'" txt) (insert "\n"))
  7623. (setq newend (point))
  7624. (org-reinstall-markers-in-region beg)
  7625. (setq end (point))
  7626. (goto-char beg)
  7627. (skip-chars-forward " \t\n\r")
  7628. (setq beg (point))
  7629. (if (and (outline-invisible-p) visp)
  7630. (save-excursion (outline-show-heading)))
  7631. ;; Shift if necessary
  7632. (unless (= shift 0)
  7633. (save-restriction
  7634. (narrow-to-region beg end)
  7635. (while (not (= shift 0))
  7636. (org-map-region func (point-min) (point-max))
  7637. (setq shift (+ delta shift)))
  7638. (goto-char (point-min))
  7639. (setq newend (point-max))))
  7640. (when (or (org-called-interactively-p 'interactive) for-yank)
  7641. (message "Clipboard pasted as level %d subtree" new-level))
  7642. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7643. kill-ring
  7644. (eq org-subtree-clip (current-kill 0))
  7645. org-subtree-clip-folded)
  7646. ;; The tree was folded before it was killed/copied
  7647. (hide-subtree))
  7648. (and for-yank (goto-char newend))
  7649. (and remove (setq kill-ring (cdr kill-ring))))))
  7650. (defun org-kill-is-subtree-p (&optional txt)
  7651. "Check if the current kill is an outline subtree, or a set of trees.
  7652. Returns nil if kill does not start with a headline, or if the first
  7653. headline level is not the largest headline level in the tree.
  7654. So this will actually accept several entries of equal levels as well,
  7655. which is OK for `org-paste-subtree'.
  7656. If optional TXT is given, check this string instead of the current kill."
  7657. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7658. (re (org-get-limited-outline-regexp))
  7659. (^re (concat "^" re))
  7660. (start-level (and kill
  7661. (string-match
  7662. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7663. kill)
  7664. (- (match-end 2) (match-beginning 2) 1)))
  7665. (start (1+ (or (match-beginning 2) -1))))
  7666. (if (not start-level)
  7667. (progn
  7668. nil) ;; does not even start with a heading
  7669. (catch 'exit
  7670. (while (setq start (string-match ^re kill (1+ start)))
  7671. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7672. (throw 'exit nil)))
  7673. t))))
  7674. (defvar org-markers-to-move nil
  7675. "Markers that should be moved with a cut-and-paste operation.
  7676. Those markers are stored together with their positions relative to
  7677. the start of the region.")
  7678. (defun org-save-markers-in-region (beg end)
  7679. "Check markers in region.
  7680. If these markers are between BEG and END, record their position relative
  7681. to BEG, so that after moving the block of text, we can put the markers back
  7682. into place.
  7683. This function gets called just before an entry or tree gets cut from the
  7684. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7685. called immediately, to move the markers with the entries."
  7686. (setq org-markers-to-move nil)
  7687. (when (featurep 'org-clock)
  7688. (org-clock-save-markers-for-cut-and-paste beg end))
  7689. (when (featurep 'org-agenda)
  7690. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7691. (defun org-check-and-save-marker (marker beg end)
  7692. "Check if MARKER is between BEG and END.
  7693. If yes, remember the marker and the distance to BEG."
  7694. (when (and (marker-buffer marker)
  7695. (equal (marker-buffer marker) (current-buffer)))
  7696. (if (and (>= marker beg) (< marker end))
  7697. (push (cons marker (- marker beg)) org-markers-to-move))))
  7698. (defun org-reinstall-markers-in-region (beg)
  7699. "Move all remembered markers to their position relative to BEG."
  7700. (mapc (lambda (x)
  7701. (move-marker (car x) (+ beg (cdr x))))
  7702. org-markers-to-move)
  7703. (setq org-markers-to-move nil))
  7704. (defun org-narrow-to-subtree ()
  7705. "Narrow buffer to the current subtree."
  7706. (interactive)
  7707. (save-excursion
  7708. (save-match-data
  7709. (org-with-limited-levels
  7710. (narrow-to-region
  7711. (progn (org-back-to-heading t) (point))
  7712. (progn (org-end-of-subtree t t)
  7713. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7714. (point)))))))
  7715. (defun org-narrow-to-block ()
  7716. "Narrow buffer to the current block."
  7717. (interactive)
  7718. (let* ((case-fold-search t)
  7719. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7720. "^[ \t]*#\\+end_.*")))
  7721. (if blockp
  7722. (narrow-to-region (car blockp) (cdr blockp))
  7723. (user-error "Not in a block"))))
  7724. (eval-when-compile
  7725. (defvar org-property-drawer-re))
  7726. (defvar org-property-start-re) ;; defined below
  7727. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7728. "Clone the task (subtree) at point N times.
  7729. The clones will be inserted as siblings.
  7730. In interactive use, the user will be prompted for the number of
  7731. clones to be produced. If the entry has a timestamp, the user
  7732. will also be prompted for a time shift, which may be a repeater
  7733. as used in time stamps, for example `+3d'. To disable this,
  7734. you can call the function with a universal prefix argument.
  7735. When a valid repeater is given and the entry contains any time
  7736. stamps, the clones will become a sequence in time, with time
  7737. stamps in the subtree shifted for each clone produced. If SHIFT
  7738. is nil or the empty string, time stamps will be left alone. The
  7739. ID property of the original subtree is removed.
  7740. If the original subtree did contain time stamps with a repeater,
  7741. the following will happen:
  7742. - the repeater will be removed in each clone
  7743. - an additional clone will be produced, with the current, unshifted
  7744. date(s) in the entry.
  7745. - the original entry will be placed *after* all the clones, with
  7746. repeater intact.
  7747. - the start days in the repeater in the original entry will be shifted
  7748. to past the last clone.
  7749. In this way you can spell out a number of instances of a repeating task,
  7750. and still retain the repeater to cover future instances of the task."
  7751. (interactive "nNumber of clones to produce: ")
  7752. (let ((shift
  7753. (or shift
  7754. (if (and (not (equal current-prefix-arg '(4)))
  7755. (save-excursion
  7756. (re-search-forward org-ts-regexp-both
  7757. (save-excursion
  7758. (org-end-of-subtree t)
  7759. (point)) t)))
  7760. (read-from-minibuffer
  7761. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7762. ""))) ;; No time shift
  7763. (n-no-remove -1)
  7764. (drawer-re org-drawer-regexp)
  7765. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7766. beg end template task idprop
  7767. shift-n shift-what doshift nmin nmax)
  7768. (if (not (and (integerp n) (> n 0)))
  7769. (user-error "Invalid number of replications %s" n))
  7770. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7771. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7772. shift)))
  7773. (user-error "Invalid shift specification %s" shift))
  7774. (when doshift
  7775. (setq shift-n (string-to-number (match-string 1 shift))
  7776. shift-what (cdr (assoc (match-string 2 shift)
  7777. '(("d" . day) ("w" . week)
  7778. ("m" . month) ("y" . year))))))
  7779. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7780. (setq nmin 1 nmax n)
  7781. (org-back-to-heading t)
  7782. (setq beg (point))
  7783. (setq idprop (org-entry-get nil "ID"))
  7784. (org-end-of-subtree t t)
  7785. (or (bolp) (insert "\n"))
  7786. (setq end (point))
  7787. (setq template (buffer-substring beg end))
  7788. (when (and doshift
  7789. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7790. (delete-region beg end)
  7791. (setq end beg)
  7792. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7793. (goto-char end)
  7794. (loop for n from nmin to nmax do
  7795. ;; prepare clone
  7796. (with-temp-buffer
  7797. (insert template)
  7798. (org-mode)
  7799. (goto-char (point-min))
  7800. (org-show-subtree)
  7801. (and idprop (if org-clone-delete-id
  7802. (org-entry-delete nil "ID")
  7803. (org-id-get-create t)))
  7804. (unless (= n 0)
  7805. (while (re-search-forward org-clock-re nil t)
  7806. (kill-whole-line))
  7807. (goto-char (point-min))
  7808. (while (re-search-forward drawer-re nil t)
  7809. (org-remove-empty-drawer-at (point))))
  7810. (goto-char (point-min))
  7811. (when doshift
  7812. (while (re-search-forward org-ts-regexp-both nil t)
  7813. (org-timestamp-change (* n shift-n) shift-what))
  7814. (unless (= n n-no-remove)
  7815. (goto-char (point-min))
  7816. (while (re-search-forward org-ts-regexp nil t)
  7817. (save-excursion
  7818. (goto-char (match-beginning 0))
  7819. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7820. (delete-region (match-beginning 1) (match-end 1)))))))
  7821. (setq task (buffer-string)))
  7822. (insert task))
  7823. (goto-char beg)))
  7824. ;;; Outline Sorting
  7825. (defun org-sort (with-case)
  7826. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7827. Optional argument WITH-CASE means sort case-sensitively."
  7828. (interactive "P")
  7829. (cond
  7830. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7831. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7832. (t
  7833. (org-call-with-arg 'org-sort-entries with-case))))
  7834. (defun org-sort-remove-invisible (s)
  7835. "Remove invisible links from string S."
  7836. (remove-text-properties 0 (length s) org-rm-props s)
  7837. (while (string-match org-bracket-link-regexp s)
  7838. (setq s (replace-match (if (match-end 2)
  7839. (match-string 3 s)
  7840. (match-string 1 s)) t t s)))
  7841. (let ((st (format " %s " s)))
  7842. (while (string-match org-emph-re st)
  7843. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7844. (setq s (substring st 1 -1)))
  7845. s)
  7846. (defvar org-priority-regexp) ; defined later in the file
  7847. (defvar org-after-sorting-entries-or-items-hook nil
  7848. "Hook that is run after a bunch of entries or items have been sorted.
  7849. When children are sorted, the cursor is in the parent line when this
  7850. hook gets called. When a region or a plain list is sorted, the cursor
  7851. will be in the first entry of the sorted region/list.")
  7852. (defun org-sort-entries
  7853. (&optional with-case sorting-type getkey-func compare-func property)
  7854. "Sort entries on a certain level of an outline tree.
  7855. If there is an active region, the entries in the region are sorted.
  7856. Else, if the cursor is before the first entry, sort the top-level items.
  7857. Else, the children of the entry at point are sorted.
  7858. Sorting can be alphabetically, numerically, by date/time as given by
  7859. a time stamp, by a property, by priority order, or by a custom function.
  7860. The command prompts for the sorting type unless it has been given to the
  7861. function through the SORTING-TYPE argument, which needs to be a character,
  7862. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7863. the precise meaning of each character:
  7864. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7865. c By creation time, which is assumed to be the first inactive time stamp
  7866. at the beginning of a line.
  7867. d By deadline date/time.
  7868. k By clocking time.
  7869. n Numerically, by converting the beginning of the entry/item to a number.
  7870. o By order of TODO keywords.
  7871. p By priority according to the cookie.
  7872. r By the value of a property.
  7873. s By scheduled date/time.
  7874. t By date/time, either the first active time stamp in the entry, or, if
  7875. none exist, by the first inactive one.
  7876. Capital letters will reverse the sort order.
  7877. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7878. called with point at the beginning of the record. It must return either
  7879. a string or a number that should serve as the sorting key for that record.
  7880. Comparing entries ignores case by default. However, with an optional argument
  7881. WITH-CASE, the sorting considers case as well.
  7882. Sorting is done against the visible part of the headlines, it ignores hidden
  7883. links.
  7884. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7885. (interactive "P")
  7886. (let ((case-func (if with-case 'identity 'downcase))
  7887. (cmstr
  7888. ;; The clock marker is lost when using `sort-subr', let's
  7889. ;; store the clocking string.
  7890. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7891. (save-excursion
  7892. (goto-char org-clock-marker)
  7893. (looking-back "^.*") (match-string-no-properties 0))))
  7894. start beg end stars re re2
  7895. txt what tmp)
  7896. ;; Find beginning and end of region to sort
  7897. (cond
  7898. ((org-region-active-p)
  7899. ;; we will sort the region
  7900. (setq end (region-end)
  7901. what "region")
  7902. (goto-char (region-beginning))
  7903. (if (not (org-at-heading-p)) (outline-next-heading))
  7904. (setq start (point)))
  7905. ((or (org-at-heading-p)
  7906. (ignore-errors (progn (org-back-to-heading) t)))
  7907. ;; we will sort the children of the current headline
  7908. (org-back-to-heading)
  7909. (setq start (point)
  7910. end (progn (org-end-of-subtree t t)
  7911. (or (bolp) (insert "\n"))
  7912. (when (>= (org-back-over-empty-lines) 1)
  7913. (forward-line 1))
  7914. (point))
  7915. what "children")
  7916. (goto-char start)
  7917. (show-subtree)
  7918. (outline-next-heading))
  7919. (t
  7920. ;; we will sort the top-level entries in this file
  7921. (goto-char (point-min))
  7922. (or (org-at-heading-p) (outline-next-heading))
  7923. (setq start (point))
  7924. (goto-char (point-max))
  7925. (beginning-of-line 1)
  7926. (when (looking-at ".*?\\S-")
  7927. ;; File ends in a non-white line
  7928. (end-of-line 1)
  7929. (insert "\n"))
  7930. (setq end (point-max))
  7931. (setq what "top-level")
  7932. (goto-char start)
  7933. (show-all)))
  7934. (setq beg (point))
  7935. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7936. (looking-at "\\(\\*+\\)")
  7937. (setq stars (match-string 1)
  7938. re (concat "^" (regexp-quote stars) " +")
  7939. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7940. txt (buffer-substring beg end))
  7941. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7942. (if (and (not (equal stars "*")) (string-match re2 txt))
  7943. (user-error "Region to sort contains a level above the first entry"))
  7944. (unless sorting-type
  7945. (message
  7946. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7947. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  7948. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  7949. what)
  7950. (setq sorting-type (read-char-exclusive))
  7951. (unless getkey-func
  7952. (and (= (downcase sorting-type) ?f)
  7953. (setq getkey-func
  7954. (org-icompleting-read "Sort using function: "
  7955. obarray 'fboundp t nil nil))
  7956. (setq getkey-func (intern getkey-func))))
  7957. (and (= (downcase sorting-type) ?r)
  7958. (not property)
  7959. (setq property
  7960. (org-icompleting-read "Property: "
  7961. (mapcar 'list (org-buffer-property-keys t))
  7962. nil t))))
  7963. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  7964. (message "Sorting entries...")
  7965. (save-restriction
  7966. (narrow-to-region start end)
  7967. (let ((dcst (downcase sorting-type))
  7968. (case-fold-search nil)
  7969. (now (current-time)))
  7970. (sort-subr
  7971. (/= dcst sorting-type)
  7972. ;; This function moves to the beginning character of the "record" to
  7973. ;; be sorted.
  7974. (lambda nil
  7975. (if (re-search-forward re nil t)
  7976. (goto-char (match-beginning 0))
  7977. (goto-char (point-max))))
  7978. ;; This function moves to the last character of the "record" being
  7979. ;; sorted.
  7980. (lambda nil
  7981. (save-match-data
  7982. (condition-case nil
  7983. (outline-forward-same-level 1)
  7984. (error
  7985. (goto-char (point-max))))))
  7986. ;; This function returns the value that gets sorted against.
  7987. (lambda nil
  7988. (cond
  7989. ((= dcst ?n)
  7990. (if (looking-at org-complex-heading-regexp)
  7991. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7992. nil))
  7993. ((= dcst ?a)
  7994. (if (looking-at org-complex-heading-regexp)
  7995. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7996. nil))
  7997. ((= dcst ?k)
  7998. (or (get-text-property (point) :org-clock-minutes) 0))
  7999. ((= dcst ?t)
  8000. (let ((end (save-excursion (outline-next-heading) (point))))
  8001. (if (or (re-search-forward org-ts-regexp end t)
  8002. (re-search-forward org-ts-regexp-both end t))
  8003. (org-time-string-to-seconds (match-string 0))
  8004. (org-float-time now))))
  8005. ((= dcst ?c)
  8006. (let ((end (save-excursion (outline-next-heading) (point))))
  8007. (if (re-search-forward
  8008. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  8009. end t)
  8010. (org-time-string-to-seconds (match-string 0))
  8011. (org-float-time now))))
  8012. ((= dcst ?s)
  8013. (let ((end (save-excursion (outline-next-heading) (point))))
  8014. (if (re-search-forward org-scheduled-time-regexp end t)
  8015. (org-time-string-to-seconds (match-string 1))
  8016. (org-float-time now))))
  8017. ((= dcst ?d)
  8018. (let ((end (save-excursion (outline-next-heading) (point))))
  8019. (if (re-search-forward org-deadline-time-regexp end t)
  8020. (org-time-string-to-seconds (match-string 1))
  8021. (org-float-time now))))
  8022. ((= dcst ?p)
  8023. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8024. (string-to-char (match-string 2))
  8025. org-default-priority))
  8026. ((= dcst ?r)
  8027. (or (org-entry-get nil property) ""))
  8028. ((= dcst ?o)
  8029. (if (looking-at org-complex-heading-regexp)
  8030. (let* ((m (match-string 2))
  8031. (s (if (member m org-done-keywords) '- '+)))
  8032. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8033. ((= dcst ?f)
  8034. (if getkey-func
  8035. (progn
  8036. (setq tmp (funcall getkey-func))
  8037. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  8038. tmp)
  8039. (error "Invalid key function `%s'" getkey-func)))
  8040. (t (error "Invalid sorting type `%c'" sorting-type))))
  8041. nil
  8042. (cond
  8043. ((= dcst ?a) 'string<)
  8044. ((= dcst ?f) compare-func)
  8045. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8046. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8047. ;; Reset the clock marker if needed
  8048. (when cmstr
  8049. (save-excursion
  8050. (goto-char start)
  8051. (search-forward cmstr nil t)
  8052. (move-marker org-clock-marker (point))))
  8053. (message "Sorting entries...done")))
  8054. (defun org-do-sort (table what &optional with-case sorting-type)
  8055. "Sort TABLE of WHAT according to SORTING-TYPE.
  8056. The user will be prompted for the SORTING-TYPE if the call to this
  8057. function does not specify it.
  8058. WHAT is only for the prompt, to indicate what is being sorted.
  8059. The sorting key will be extracted from the car of the elements of
  8060. the table.
  8061. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  8062. (unless sorting-type
  8063. (message
  8064. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  8065. what)
  8066. (setq sorting-type (read-char-exclusive)))
  8067. (let ((dcst (downcase sorting-type))
  8068. extractfun comparefun)
  8069. ;; Define the appropriate functions
  8070. (cond
  8071. ((= dcst ?n)
  8072. (setq extractfun 'string-to-number
  8073. comparefun (if (= dcst sorting-type) '< '>)))
  8074. ((= dcst ?a)
  8075. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  8076. (lambda(x) (downcase (org-sort-remove-invisible x))))
  8077. comparefun (if (= dcst sorting-type)
  8078. 'string<
  8079. (lambda (a b) (and (not (string< a b))
  8080. (not (string= a b)))))))
  8081. ((= dcst ?t)
  8082. (setq extractfun
  8083. (lambda (x)
  8084. (cond ((or (string-match org-ts-regexp x)
  8085. (string-match org-ts-regexp-both x))
  8086. (org-float-time
  8087. (org-time-string-to-time (match-string 0 x))))
  8088. ((string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" x)
  8089. (org-hh:mm-string-to-minutes x))
  8090. (t 0)))
  8091. comparefun (if (= dcst sorting-type) '< '>)))
  8092. (t (error "Invalid sorting type `%c'" sorting-type)))
  8093. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  8094. table)
  8095. (lambda (a b) (funcall comparefun (car a) (car b))))))
  8096. ;;; The orgstruct minor mode
  8097. ;; Define a minor mode which can be used in other modes in order to
  8098. ;; integrate the org-mode structure editing commands.
  8099. ;; This is really a hack, because the org-mode structure commands use
  8100. ;; keys which normally belong to the major mode. Here is how it
  8101. ;; works: The minor mode defines all the keys necessary to operate the
  8102. ;; structure commands, but wraps the commands into a function which
  8103. ;; tests if the cursor is currently at a headline or a plain list
  8104. ;; item. If that is the case, the structure command is used,
  8105. ;; temporarily setting many Org-mode variables like regular
  8106. ;; expressions for filling etc. However, when any of those keys is
  8107. ;; used at a different location, function uses `key-binding' to look
  8108. ;; up if the key has an associated command in another currently active
  8109. ;; keymap (minor modes, major mode, global), and executes that
  8110. ;; command. There might be problems if any of the keys is otherwise
  8111. ;; used as a prefix key.
  8112. (defcustom orgstruct-heading-prefix-regexp ""
  8113. "Regexp that matches the custom prefix of Org headlines in
  8114. orgstruct(++)-mode."
  8115. :group 'org
  8116. :version "24.4"
  8117. :package-version '(Org . "8.3")
  8118. :type 'regexp)
  8119. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8120. (defcustom orgstruct-setup-hook nil
  8121. "Hook run after orgstruct-mode-map is filled."
  8122. :group 'org
  8123. :version "24.4"
  8124. :package-version '(Org . "8.0")
  8125. :type 'hook)
  8126. (defvar orgstruct-initialized nil)
  8127. (defvar org-local-vars nil
  8128. "List of local variables, for use by `orgstruct-mode'.")
  8129. ;;;###autoload
  8130. (define-minor-mode orgstruct-mode
  8131. "Toggle the minor mode `orgstruct-mode'.
  8132. This mode is for using Org-mode structure commands in other
  8133. modes. The following keys behave as if Org-mode were active, if
  8134. the cursor is on a headline, or on a plain list item (both as
  8135. defined by Org-mode)."
  8136. nil " OrgStruct" (make-sparse-keymap)
  8137. (funcall (if orgstruct-mode
  8138. 'add-to-invisibility-spec
  8139. 'remove-from-invisibility-spec)
  8140. '(outline . t))
  8141. (when orgstruct-mode
  8142. (org-load-modules-maybe)
  8143. (unless orgstruct-initialized
  8144. (orgstruct-setup)
  8145. (setq orgstruct-initialized t))))
  8146. ;;;###autoload
  8147. (defun turn-on-orgstruct ()
  8148. "Unconditionally turn on `orgstruct-mode'."
  8149. (orgstruct-mode 1))
  8150. (defvar org-fb-vars nil)
  8151. (make-variable-buffer-local 'org-fb-vars)
  8152. (defun orgstruct++-mode (&optional arg)
  8153. "Toggle `orgstruct-mode', the enhanced version of it.
  8154. In addition to setting orgstruct-mode, this also exports all
  8155. indentation and autofilling variables from org-mode into the
  8156. buffer. It will also recognize item context in multiline items."
  8157. (interactive "P")
  8158. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8159. (if (< arg 1)
  8160. (progn (orgstruct-mode -1)
  8161. (mapc (lambda(v)
  8162. (org-set-local (car v)
  8163. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8164. org-fb-vars))
  8165. (orgstruct-mode 1)
  8166. (setq org-fb-vars nil)
  8167. (unless org-local-vars
  8168. (setq org-local-vars (org-get-local-variables)))
  8169. (let (var val)
  8170. (mapc
  8171. (lambda (x)
  8172. (when (string-match
  8173. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8174. (symbol-name (car x)))
  8175. (setq var (car x) val (nth 1 x))
  8176. (push (list var `(quote ,(eval var))) org-fb-vars)
  8177. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8178. org-local-vars)
  8179. (org-set-local 'orgstruct-is-++ t))))
  8180. (defvar orgstruct-is-++ nil
  8181. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8182. (make-variable-buffer-local 'orgstruct-is-++)
  8183. ;;;###autoload
  8184. (defun turn-on-orgstruct++ ()
  8185. "Unconditionally turn on `orgstruct++-mode'."
  8186. (orgstruct++-mode 1))
  8187. (defun orgstruct-error ()
  8188. "Error when there is no default binding for a structure key."
  8189. (interactive)
  8190. (funcall (if (fboundp 'user-error)
  8191. 'user-error
  8192. 'error)
  8193. "This key has no function outside structure elements"))
  8194. (defun orgstruct-setup ()
  8195. "Setup orgstruct keymap."
  8196. (dolist (cell '((org-demote . t)
  8197. (org-metaleft . t)
  8198. (org-metaright . t)
  8199. (org-promote . t)
  8200. (org-shiftmetaleft . t)
  8201. (org-shiftmetaright . t)
  8202. org-backward-element
  8203. org-backward-heading-same-level
  8204. org-ctrl-c-ret
  8205. org-ctrl-c-minus
  8206. org-ctrl-c-star
  8207. org-cycle
  8208. org-forward-heading-same-level
  8209. org-insert-heading
  8210. org-insert-heading-respect-content
  8211. org-kill-note-or-show-branches
  8212. org-mark-subtree
  8213. org-meta-return
  8214. org-metadown
  8215. org-metaup
  8216. org-narrow-to-subtree
  8217. org-promote-subtree
  8218. org-reveal
  8219. org-shiftdown
  8220. org-shiftleft
  8221. org-shiftmetadown
  8222. org-shiftmetaup
  8223. org-shiftright
  8224. org-shifttab
  8225. org-shifttab
  8226. org-shiftup
  8227. org-show-subtree
  8228. org-sort
  8229. org-up-element
  8230. outline-demote
  8231. outline-next-visible-heading
  8232. outline-previous-visible-heading
  8233. outline-promote
  8234. outline-up-heading
  8235. show-children))
  8236. (let ((f (or (car-safe cell) cell))
  8237. (disable-when-heading-prefix (cdr-safe cell)))
  8238. (when (fboundp f)
  8239. (let ((new-bindings))
  8240. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8241. (where-is-internal f outline-mode-map)))
  8242. (push binding new-bindings)
  8243. ;; TODO use local-function-key-map
  8244. (dolist (rep '(("<tab>" . "TAB")
  8245. ("<return>" . "RET")
  8246. ("<escape>" . "ESC")
  8247. ("<delete>" . "DEL")))
  8248. (setq binding (read-kbd-macro
  8249. (let ((case-fold-search))
  8250. (replace-regexp-in-string
  8251. (regexp-quote (cdr rep))
  8252. (car rep)
  8253. (key-description binding)))))
  8254. (pushnew binding new-bindings :test 'equal)))
  8255. (dolist (binding new-bindings)
  8256. (let ((key (lookup-key orgstruct-mode-map binding)))
  8257. (when (or (not key) (numberp key))
  8258. (ignore-errors
  8259. (org-defkey orgstruct-mode-map
  8260. binding
  8261. (orgstruct-make-binding
  8262. f binding disable-when-heading-prefix))))))))))
  8263. (run-hooks 'orgstruct-setup-hook))
  8264. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8265. "Create a function for binding in the structure minor mode.
  8266. FUN is the command to call inside a table. KEY is the key that
  8267. should be checked in for a command to execute outside of tables.
  8268. Non-nil `disable-when-heading-prefix' means to disable the command
  8269. if `orgstruct-heading-prefix-regexp' is not empty."
  8270. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8271. (let ((nname name)
  8272. (i 0))
  8273. (while (fboundp (intern nname))
  8274. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8275. (setq name (intern nname)))
  8276. (eval
  8277. (let ((bindings '((org-heading-regexp
  8278. (concat "^"
  8279. orgstruct-heading-prefix-regexp
  8280. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8281. (org-outline-regexp
  8282. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8283. (org-outline-regexp-bol
  8284. (concat "^" org-outline-regexp))
  8285. (outline-regexp org-outline-regexp)
  8286. (outline-heading-end-regexp "\n")
  8287. (outline-level 'org-outline-level)
  8288. (outline-heading-alist))))
  8289. `(defun ,name (arg)
  8290. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8291. "Outside of structure, run the binding of `"
  8292. (key-description key) "'."
  8293. (when disable-when-heading-prefix
  8294. (concat
  8295. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8296. "back to the default binding due to limitations of Org's implementation of\n"
  8297. "`" (symbol-name fun) "'.")))
  8298. (interactive "p")
  8299. (let* ((disable
  8300. ,(and disable-when-heading-prefix
  8301. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8302. (fallback
  8303. (or disable
  8304. (not
  8305. (let* ,bindings
  8306. (org-context-p 'headline 'item
  8307. ,(when (memq fun
  8308. '(org-insert-heading
  8309. org-insert-heading-respect-content
  8310. org-meta-return))
  8311. '(when orgstruct-is-++
  8312. 'item-body))))))))
  8313. (if fallback
  8314. (let* ((orgstruct-mode)
  8315. (binding
  8316. (loop with key = ,key
  8317. for rep in
  8318. '(nil
  8319. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8320. ("<\\([^>]*\\)return>" . "\\1RET")
  8321. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8322. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8323. do
  8324. (when rep
  8325. (setq key (read-kbd-macro
  8326. (let ((case-fold-search))
  8327. (replace-regexp-in-string
  8328. (car rep)
  8329. (cdr rep)
  8330. (key-description key))))))
  8331. thereis (key-binding key))))
  8332. (if (keymapp binding)
  8333. (org-set-transient-map binding)
  8334. (let ((func (or binding
  8335. (unless disable
  8336. 'orgstruct-error))))
  8337. (when func
  8338. (call-interactively func)))))
  8339. (org-run-like-in-org-mode
  8340. (lambda ()
  8341. (interactive)
  8342. (let* ,bindings
  8343. (call-interactively ',fun)))))))))
  8344. name))
  8345. (defun org-contextualize-keys (alist contexts)
  8346. "Return valid elements in ALIST depending on CONTEXTS.
  8347. `org-agenda-custom-commands' or `org-capture-templates' are the
  8348. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8349. or `org-capture-templates-contexts' are the associated contexts
  8350. definitions."
  8351. (let ((contexts
  8352. ;; normalize contexts
  8353. (mapcar
  8354. (lambda(c) (cond ((listp (cadr c))
  8355. (list (car c) (car c) (cadr c)))
  8356. ((string= "" (cadr c))
  8357. (list (car c) (car c) (caddr c)))
  8358. (t c))) contexts))
  8359. (a alist) c r s)
  8360. ;; loop over all commands or templates
  8361. (while (setq c (pop a))
  8362. (let (vrules repl)
  8363. (cond
  8364. ((not (assoc (car c) contexts))
  8365. (push c r))
  8366. ((and (assoc (car c) contexts)
  8367. (setq vrules (org-contextualize-validate-key
  8368. (car c) contexts)))
  8369. (mapc (lambda (vr)
  8370. (when (not (equal (car vr) (cadr vr)))
  8371. (setq repl vr))) vrules)
  8372. (if (not repl) (push c r)
  8373. (push (cadr repl) s)
  8374. (push
  8375. (cons (car c)
  8376. (cdr (or (assoc (cadr repl) alist)
  8377. (error "Undefined key `%s' as contextual replacement for `%s'"
  8378. (cadr repl) (car c)))))
  8379. r))))))
  8380. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8381. (delq
  8382. nil
  8383. (delete-dups
  8384. (mapcar (lambda (x)
  8385. (let ((tpl (car x)))
  8386. (when (not (delq
  8387. nil
  8388. (mapcar (lambda(y)
  8389. (equal y tpl)) s))) x)))
  8390. (reverse r))))))
  8391. (defun org-contextualize-validate-key (key contexts)
  8392. "Check CONTEXTS for agenda or capture KEY."
  8393. (let (r rr res)
  8394. (while (setq r (pop contexts))
  8395. (mapc
  8396. (lambda (rr)
  8397. (when
  8398. (and (equal key (car r))
  8399. (if (functionp rr) (funcall rr)
  8400. (or (and (eq (car rr) 'in-file)
  8401. (buffer-file-name)
  8402. (string-match (cdr rr) (buffer-file-name)))
  8403. (and (eq (car rr) 'in-mode)
  8404. (string-match (cdr rr) (symbol-name major-mode)))
  8405. (and (eq (car rr) 'in-buffer)
  8406. (string-match (cdr rr) (buffer-name)))
  8407. (when (and (eq (car rr) 'not-in-file)
  8408. (buffer-file-name))
  8409. (not (string-match (cdr rr) (buffer-file-name))))
  8410. (when (eq (car rr) 'not-in-mode)
  8411. (not (string-match (cdr rr) (symbol-name major-mode))))
  8412. (when (eq (car rr) 'not-in-buffer)
  8413. (not (string-match (cdr rr) (buffer-name)))))))
  8414. (push r res)))
  8415. (car (last r))))
  8416. (delete-dups (delq nil res))))
  8417. (defun org-context-p (&rest contexts)
  8418. "Check if local context is any of CONTEXTS.
  8419. Possible values in the list of contexts are `table', `headline', and `item'."
  8420. (let ((pos (point)))
  8421. (goto-char (point-at-bol))
  8422. (prog1 (or (and (memq 'table contexts)
  8423. (looking-at "[ \t]*|"))
  8424. (and (memq 'headline contexts)
  8425. (looking-at org-outline-regexp))
  8426. (and (memq 'item contexts)
  8427. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8428. (and (memq 'item-body contexts)
  8429. (org-in-item-p)))
  8430. (goto-char pos))))
  8431. (defun org-get-local-variables ()
  8432. "Return a list of all local variables in an Org mode buffer."
  8433. (let (varlist)
  8434. (with-current-buffer (get-buffer-create "*Org tmp*")
  8435. (erase-buffer)
  8436. (org-mode)
  8437. (setq varlist (buffer-local-variables)))
  8438. (kill-buffer "*Org tmp*")
  8439. (delq nil
  8440. (mapcar
  8441. (lambda (x)
  8442. (setq x
  8443. (if (symbolp x)
  8444. (list x)
  8445. (list (car x) (cdr x))))
  8446. (if (and (not (get (car x) 'org-state))
  8447. (string-match
  8448. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8449. (symbol-name (car x))))
  8450. x nil))
  8451. varlist))))
  8452. (defun org-clone-local-variables (from-buffer &optional regexp)
  8453. "Clone local variables from FROM-BUFFER.
  8454. Optional argument REGEXP selects variables to clone."
  8455. (mapc
  8456. (lambda (pair)
  8457. (and (symbolp (car pair))
  8458. (or (null regexp)
  8459. (string-match regexp (symbol-name (car pair))))
  8460. (set (make-local-variable (car pair))
  8461. (cdr pair))))
  8462. (buffer-local-variables from-buffer)))
  8463. ;;;###autoload
  8464. (defun org-run-like-in-org-mode (cmd)
  8465. "Run a command, pretending that the current buffer is in Org-mode.
  8466. This will temporarily bind local variables that are typically bound in
  8467. Org-mode to the values they have in Org-mode, and then interactively
  8468. call CMD."
  8469. (org-load-modules-maybe)
  8470. (unless org-local-vars
  8471. (setq org-local-vars (org-get-local-variables)))
  8472. (let (binds)
  8473. (dolist (var org-local-vars)
  8474. (when (or (not (boundp (car var)))
  8475. (eq (symbol-value (car var))
  8476. (default-value (car var))))
  8477. (push (list (car var) `(quote ,(cadr var))) binds)))
  8478. (eval `(let ,binds
  8479. (call-interactively (quote ,cmd))))))
  8480. (defun org-get-category (&optional pos force-refresh)
  8481. "Get the category applying to position POS."
  8482. (save-match-data
  8483. (if force-refresh (org-refresh-category-properties))
  8484. (let ((pos (or pos (point))))
  8485. (or (get-text-property pos 'org-category)
  8486. (progn (org-refresh-category-properties)
  8487. (get-text-property pos 'org-category))))))
  8488. ;;; Refresh properties
  8489. (defun org-refresh-properties (dprop tprop)
  8490. "Refresh buffer text properties.
  8491. DPROP is the drawer property and TPROP is either the
  8492. corresponding text property to set, or an alist with each element
  8493. being a text property (as a symbol) and a function to apply to
  8494. the value of the drawer property."
  8495. (let ((case-fold-search t)
  8496. (inhibit-read-only t))
  8497. (org-with-silent-modifications
  8498. (save-excursion
  8499. (save-restriction
  8500. (widen)
  8501. (goto-char (point-min))
  8502. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8503. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8504. (defun org-refresh-property (tprop p)
  8505. "Refresh the buffer text property TPROP from the drawer property P.
  8506. The refresh happens only for the current tree (not subtree)."
  8507. (save-excursion
  8508. (org-back-to-heading t)
  8509. ;; tprop is a text property symbol
  8510. (if (symbolp tprop)
  8511. (put-text-property
  8512. (point) (or (outline-next-heading) (point-max)) tprop p)
  8513. ;; tprop is an alist with (properties . function) elements
  8514. (mapc (lambda(al)
  8515. (save-excursion
  8516. (put-text-property
  8517. (point-at-bol) (or (outline-next-heading) (point-max))
  8518. (car al)
  8519. (funcall (cdr al) p))))
  8520. tprop))))
  8521. (defun org-refresh-category-properties ()
  8522. "Refresh category text properties in the buffer."
  8523. (let ((case-fold-search t)
  8524. (inhibit-read-only t)
  8525. (def-cat (cond
  8526. ((null org-category)
  8527. (if buffer-file-name
  8528. (file-name-sans-extension
  8529. (file-name-nondirectory buffer-file-name))
  8530. "???"))
  8531. ((symbolp org-category) (symbol-name org-category))
  8532. (t org-category)))
  8533. beg end cat pos optionp)
  8534. (org-with-silent-modifications
  8535. (save-excursion
  8536. (save-restriction
  8537. (widen)
  8538. (goto-char (point-min))
  8539. (put-text-property (point) (point-max) 'org-category def-cat)
  8540. (while (re-search-forward
  8541. "^[ \t]*\\(\\(?:#\\+\\|:\\)CATEGORY:\\)\\(.*\\)" nil t)
  8542. (setq pos (match-end 0)
  8543. optionp (equal (char-after (match-beginning 0)) ?#)
  8544. cat (org-trim (match-string 2)))
  8545. (if optionp
  8546. (setq beg (point-at-bol) end (point-max))
  8547. (org-back-to-heading t)
  8548. (setq beg (point) end (org-end-of-subtree t t)))
  8549. (put-text-property beg end 'org-category cat)
  8550. (goto-char pos)))))))
  8551. (defun org-refresh-stats-properties ()
  8552. "Refresh stats text properties in the buffer."
  8553. (let (stats)
  8554. (org-with-silent-modifications
  8555. (save-excursion
  8556. (save-restriction
  8557. (widen)
  8558. (goto-char (point-min))
  8559. (while (re-search-forward
  8560. (concat org-outline-regexp-bol ".*"
  8561. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8562. nil t)
  8563. (setq stats (cond ((equal (match-string 3) "0") 0)
  8564. ((match-string 2)
  8565. (/ (* (string-to-number (match-string 2)) 100)
  8566. (string-to-number (match-string 3))))
  8567. (t (string-to-number (match-string 1)))))
  8568. (org-back-to-heading t)
  8569. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8570. 'org-stats stats)))))))
  8571. (defun org-refresh-effort-properties ()
  8572. "Refresh effort properties"
  8573. (org-refresh-properties
  8574. org-effort-property
  8575. '((effort . identity)
  8576. (effort-minutes . org-duration-string-to-minutes))))
  8577. ;;;; Link Stuff
  8578. ;;; Link abbreviations
  8579. (defun org-link-expand-abbrev (link)
  8580. "Apply replacements as defined in `org-link-abbrev-alist'."
  8581. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8582. (let* ((key (match-string 1 link))
  8583. (as (or (assoc key org-link-abbrev-alist-local)
  8584. (assoc key org-link-abbrev-alist)))
  8585. (tag (and (match-end 2) (match-string 3 link)))
  8586. rpl)
  8587. (if (not as)
  8588. link
  8589. (setq rpl (cdr as))
  8590. (cond
  8591. ((symbolp rpl) (funcall rpl tag))
  8592. ((string-match "%(\\([^)]+\\))" rpl)
  8593. (replace-match
  8594. (save-match-data
  8595. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8596. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8597. ((string-match "%h" rpl)
  8598. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8599. (t (concat rpl tag)))))
  8600. link))
  8601. ;;; Storing and inserting links
  8602. (defvar org-insert-link-history nil
  8603. "Minibuffer history for links inserted with `org-insert-link'.")
  8604. (defvar org-stored-links nil
  8605. "Contains the links stored with `org-store-link'.")
  8606. (defvar org-store-link-plist nil
  8607. "Plist with info about the most recently link created with `org-store-link'.")
  8608. (defvar org-link-protocols nil
  8609. "Link protocols added to Org-mode using `org-add-link-type'.")
  8610. (defvar org-store-link-functions nil
  8611. "List of functions that are called to create and store a link.
  8612. Each function will be called in turn until one returns a non-nil
  8613. value. Each function should check if it is responsible for creating
  8614. this link (for example by looking at the major mode).
  8615. If not, it must exit and return nil.
  8616. If yes, it should return a non-nil value after a calling
  8617. `org-store-link-props' with a list of properties and values.
  8618. Special properties are:
  8619. :type The link prefix, like \"http\". This must be given.
  8620. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8621. This is obligatory as well.
  8622. :description Optional default description for the second pair
  8623. of brackets in an Org-mode link. The user can still change
  8624. this when inserting this link into an Org-mode buffer.
  8625. In addition to these, any additional properties can be specified
  8626. and then used in capture templates.")
  8627. (defun org-add-link-type (type &optional follow export)
  8628. "Add TYPE to the list of `org-link-types'.
  8629. Re-compute all regular expressions depending on `org-link-types'
  8630. FOLLOW and EXPORT are two functions.
  8631. FOLLOW should take the link path as the single argument and do whatever
  8632. is necessary to follow the link, for example find a file or display
  8633. a mail message.
  8634. EXPORT should format the link path for export to one of the export formats.
  8635. It should be a function accepting three arguments:
  8636. path the path of the link, the text after the prefix (like \"http:\")
  8637. desc the description of the link, if any, or a description added by
  8638. org-export-normalize-links if there is none
  8639. format the export format, a symbol like `html' or `latex' or `ascii'..
  8640. The function may use the FORMAT information to return different values
  8641. depending on the format. The return value will be put literally into
  8642. the exported file. If the return value is nil, this means Org should
  8643. do what it normally does with links which do not have EXPORT defined.
  8644. Org-mode has a built-in default for exporting links. If you are happy with
  8645. this default, there is no need to define an export function for the link
  8646. type. For a simple example of an export function, see `org-bbdb.el'."
  8647. (add-to-list 'org-link-types type t)
  8648. (org-make-link-regexps)
  8649. (if (assoc type org-link-protocols)
  8650. (setcdr (assoc type org-link-protocols) (list follow export))
  8651. (push (list type follow export) org-link-protocols)))
  8652. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8653. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8654. ;;;###autoload
  8655. (defun org-store-link (arg)
  8656. "\\<org-mode-map>Store an org-link to the current location.
  8657. This link is added to `org-stored-links' and can later be inserted
  8658. into an org-buffer with \\[org-insert-link].
  8659. For some link types, a prefix arg is interpreted.
  8660. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8661. For file links, arg negates `org-context-in-file-links'.
  8662. A double prefix arg force skipping storing functions that are not
  8663. part of Org's core.
  8664. A triple prefix arg force storing a link for each line in the
  8665. active region."
  8666. (interactive "P")
  8667. (org-load-modules-maybe)
  8668. (if (and (equal arg '(64)) (org-region-active-p))
  8669. (save-excursion
  8670. (let ((end (region-end)))
  8671. (goto-char (region-beginning))
  8672. (set-mark (point))
  8673. (while (< (point-at-eol) end)
  8674. (move-end-of-line 1) (activate-mark)
  8675. (let (current-prefix-arg)
  8676. (call-interactively 'org-store-link))
  8677. (move-beginning-of-line 2)
  8678. (set-mark (point)))))
  8679. (org-with-limited-levels
  8680. (setq org-store-link-plist nil)
  8681. (let (link cpltxt desc description search
  8682. txt custom-id agenda-link sfuns sfunsn)
  8683. (cond
  8684. ;; Store a link using an external link type
  8685. ((and (not (equal arg '(16)))
  8686. (setq sfuns
  8687. (delq
  8688. nil (mapcar (lambda (f)
  8689. (let (fs) (if (funcall f) (push f fs))))
  8690. org-store-link-functions))
  8691. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8692. (or (and (cdr sfuns)
  8693. (funcall (intern
  8694. (completing-read
  8695. "Which function for creating the link? "
  8696. sfunsn nil t (car sfunsn)))))
  8697. (funcall (caar sfuns)))
  8698. (setq link (plist-get org-store-link-plist :link)
  8699. desc (or (plist-get org-store-link-plist
  8700. :description) link))))
  8701. ;; Store a link from a source code buffer
  8702. ((org-src-edit-buffer-p)
  8703. (let (label gc)
  8704. (while (or (not label)
  8705. (save-excursion
  8706. (save-restriction
  8707. (widen)
  8708. (goto-char (point-min))
  8709. (re-search-forward
  8710. (regexp-quote (format org-coderef-label-format label))
  8711. nil t))))
  8712. (when label (message "Label exists already") (sit-for 2))
  8713. (setq label (read-string "Code line label: " label)))
  8714. (end-of-line 1)
  8715. (setq link (format org-coderef-label-format label))
  8716. (setq gc (- 79 (length link)))
  8717. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8718. (insert link)
  8719. (setq link (concat "(" label ")") desc nil)))
  8720. ;; We are in the agenda, link to referenced location
  8721. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8722. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8723. (get-text-property (point) 'org-marker))))
  8724. (when m
  8725. (org-with-point-at m
  8726. (setq agenda-link
  8727. (if (org-called-interactively-p 'any)
  8728. (call-interactively 'org-store-link)
  8729. (org-store-link nil)))))))
  8730. ((eq major-mode 'calendar-mode)
  8731. (let ((cd (calendar-cursor-to-date)))
  8732. (setq link
  8733. (format-time-string
  8734. (car org-time-stamp-formats)
  8735. (apply 'encode-time
  8736. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8737. nil nil nil))))
  8738. (org-store-link-props :type "calendar" :date cd)))
  8739. ((eq major-mode 'help-mode)
  8740. (setq link (concat "help:" (save-excursion
  8741. (goto-char (point-min))
  8742. (looking-at "^[^ ]+")
  8743. (match-string 0))))
  8744. (org-store-link-props :type "help"))
  8745. ((eq major-mode 'w3-mode)
  8746. (setq cpltxt (if (and (buffer-name)
  8747. (not (string-match "Untitled" (buffer-name))))
  8748. (buffer-name)
  8749. (url-view-url t))
  8750. link (url-view-url t))
  8751. (org-store-link-props :type "w3" :url (url-view-url t)))
  8752. ((eq major-mode 'image-mode)
  8753. (setq cpltxt (concat "file:"
  8754. (abbreviate-file-name buffer-file-name))
  8755. link cpltxt)
  8756. (org-store-link-props :type "image" :file buffer-file-name))
  8757. ;; In dired, store a link to the file of the current line
  8758. ((derived-mode-p 'dired-mode)
  8759. (let ((file (dired-get-filename nil t)))
  8760. (setq file (if file
  8761. (abbreviate-file-name
  8762. (expand-file-name (dired-get-filename nil t)))
  8763. ;; otherwise, no file so use current directory.
  8764. default-directory))
  8765. (setq cpltxt (concat "file:" file)
  8766. link cpltxt)))
  8767. ((setq search (run-hook-with-args-until-success
  8768. 'org-create-file-search-functions))
  8769. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8770. "::" search))
  8771. (setq cpltxt (or description link)))
  8772. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8773. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8774. (cond
  8775. ;; Store a link using the target at point
  8776. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8777. (setq cpltxt
  8778. (concat "file:"
  8779. (abbreviate-file-name
  8780. (buffer-file-name (buffer-base-buffer)))
  8781. "::" (match-string 1))
  8782. link cpltxt))
  8783. ((and (featurep 'org-id)
  8784. (or (eq org-id-link-to-org-use-id t)
  8785. (and (org-called-interactively-p 'any)
  8786. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8787. (and (eq org-id-link-to-org-use-id
  8788. 'create-if-interactive-and-no-custom-id)
  8789. (not custom-id))))
  8790. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8791. ;; Store a link using the ID at point
  8792. (setq link (condition-case nil
  8793. (prog1 (org-id-store-link)
  8794. (setq desc (or (plist-get org-store-link-plist
  8795. :description)
  8796. "")))
  8797. (error
  8798. ;; Probably before first headline, link only to file
  8799. (concat "file:"
  8800. (abbreviate-file-name
  8801. (buffer-file-name (buffer-base-buffer))))))))
  8802. (t
  8803. ;; Just link to current headline
  8804. (setq cpltxt (concat "file:"
  8805. (abbreviate-file-name
  8806. (buffer-file-name (buffer-base-buffer)))))
  8807. ;; Add a context search string
  8808. (when (org-xor org-context-in-file-links arg)
  8809. (let* ((ee (org-element-at-point))
  8810. (et (org-element-type ee))
  8811. (ev (plist-get (cadr ee) :value))
  8812. (ek (plist-get (cadr ee) :key))
  8813. (eok (and (stringp ek) (string-match "name" ek))))
  8814. (setq txt (cond
  8815. ((org-at-heading-p) nil)
  8816. ((and (eq et 'keyword) eok) ev)
  8817. ((org-region-active-p)
  8818. (buffer-substring (region-beginning) (region-end)))))
  8819. (when (or (null txt) (string-match "\\S-" txt))
  8820. (setq cpltxt
  8821. (concat cpltxt "::"
  8822. (condition-case nil
  8823. (org-make-org-heading-search-string txt)
  8824. (error "")))
  8825. desc (or (and (eq et 'keyword) eok ev)
  8826. (nth 4 (ignore-errors (org-heading-components)))
  8827. "NONE")))))
  8828. (if (string-match "::\\'" cpltxt)
  8829. (setq cpltxt (substring cpltxt 0 -2)))
  8830. (setq link cpltxt))))
  8831. ((buffer-file-name (buffer-base-buffer))
  8832. ;; Just link to this file here.
  8833. (setq cpltxt (concat "file:"
  8834. (abbreviate-file-name
  8835. (buffer-file-name (buffer-base-buffer)))))
  8836. ;; Add a context string.
  8837. (when (org-xor org-context-in-file-links arg)
  8838. (setq txt (if (org-region-active-p)
  8839. (buffer-substring (region-beginning) (region-end))
  8840. (buffer-substring (point-at-bol) (point-at-eol))))
  8841. ;; Only use search option if there is some text.
  8842. (when (string-match "\\S-" txt)
  8843. (setq cpltxt
  8844. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8845. desc "NONE")))
  8846. (setq link cpltxt))
  8847. ((org-called-interactively-p 'interactive)
  8848. (user-error "No method for storing a link from this buffer"))
  8849. (t (setq link nil)))
  8850. ;; We're done setting link and desc, clean up
  8851. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8852. (setq link (or link cpltxt)
  8853. desc (or desc cpltxt))
  8854. (cond ((equal desc "NONE") (setq desc nil))
  8855. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8856. (let ((d0 (match-string 3 desc))
  8857. (p0 (match-string 5 desc)))
  8858. (setq desc
  8859. (replace-regexp-in-string
  8860. org-bracket-link-regexp
  8861. (concat (or p0 d0)
  8862. (if (equal (length (match-string 0 desc))
  8863. (length desc)) "*" "")) desc)))))
  8864. ;; Return the link
  8865. (if (not (and (or (org-called-interactively-p 'any)
  8866. executing-kbd-macro) link))
  8867. (or agenda-link (and link (org-make-link-string link desc)))
  8868. (push (list link desc) org-stored-links)
  8869. (message "Stored: %s" (or desc link))
  8870. (when custom-id
  8871. (setq link (concat "file:" (abbreviate-file-name
  8872. (buffer-file-name)) "::#" custom-id))
  8873. (push (list link desc) org-stored-links))
  8874. (car org-stored-links))))))
  8875. (defun org-store-link-props (&rest plist)
  8876. "Store link properties, extract names and addresses."
  8877. (let (x adr)
  8878. (when (setq x (plist-get plist :from))
  8879. (setq adr (mail-extract-address-components x))
  8880. (setq plist (plist-put plist :fromname (car adr)))
  8881. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8882. (when (setq x (plist-get plist :to))
  8883. (setq adr (mail-extract-address-components x))
  8884. (setq plist (plist-put plist :toname (car adr)))
  8885. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8886. (let ((from (plist-get plist :from))
  8887. (to (plist-get plist :to)))
  8888. (when (and from to org-from-is-user-regexp)
  8889. (setq plist
  8890. (plist-put plist :fromto
  8891. (if (string-match org-from-is-user-regexp from)
  8892. (concat "to %t")
  8893. (concat "from %f"))))))
  8894. (setq org-store-link-plist plist))
  8895. (defun org-add-link-props (&rest plist)
  8896. "Add these properties to the link property list."
  8897. (let (key value)
  8898. (while plist
  8899. (setq key (pop plist) value (pop plist))
  8900. (setq org-store-link-plist
  8901. (plist-put org-store-link-plist key value)))))
  8902. (defun org-email-link-description (&optional fmt)
  8903. "Return the description part of an email link.
  8904. This takes information from `org-store-link-plist' and formats it
  8905. according to FMT (default from `org-email-link-description-format')."
  8906. (setq fmt (or fmt org-email-link-description-format))
  8907. (let* ((p org-store-link-plist)
  8908. (to (plist-get p :toaddress))
  8909. (from (plist-get p :fromaddress))
  8910. (table
  8911. (list
  8912. (cons "%c" (plist-get p :fromto))
  8913. (cons "%F" (plist-get p :from))
  8914. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8915. (cons "%T" (plist-get p :to))
  8916. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8917. (cons "%s" (plist-get p :subject))
  8918. (cons "%d" (plist-get p :date))
  8919. (cons "%m" (plist-get p :message-id)))))
  8920. (when (string-match "%c" fmt)
  8921. ;; Check if the user wrote this message
  8922. (if (and org-from-is-user-regexp from to
  8923. (save-match-data (string-match org-from-is-user-regexp from)))
  8924. (setq fmt (replace-match "to %t" t t fmt))
  8925. (setq fmt (replace-match "from %f" t t fmt))))
  8926. (org-replace-escapes fmt table)))
  8927. (defun org-make-org-heading-search-string (&optional string)
  8928. "Make search string for the current headline or STRING."
  8929. (let ((s (or string
  8930. (and (derived-mode-p 'org-mode)
  8931. (save-excursion
  8932. (org-back-to-heading t)
  8933. (org-element-property :raw-value (org-element-at-point))))))
  8934. (lines org-context-in-file-links))
  8935. (or string (setq s (concat "*" s))) ; Add * for headlines
  8936. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8937. (when (and string (integerp lines) (> lines 0))
  8938. (let ((slines (org-split-string s "\n")))
  8939. (when (< lines (length slines))
  8940. (setq s (mapconcat
  8941. 'identity
  8942. (reverse (nthcdr (- (length slines) lines)
  8943. (reverse slines))) "\n")))))
  8944. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8945. (defun org-make-link-string (link &optional description)
  8946. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8947. (unless (string-match "\\S-" link)
  8948. (error "Empty link"))
  8949. (when (and description
  8950. (stringp description)
  8951. (not (string-match "\\S-" description)))
  8952. (setq description nil))
  8953. (when (stringp description)
  8954. ;; Remove brackets from the description, they are fatal.
  8955. (while (string-match "\\[" description)
  8956. (setq description (replace-match "{" t t description)))
  8957. (while (string-match "\\]" description)
  8958. (setq description (replace-match "}" t t description))))
  8959. (when (equal link description)
  8960. ;; No description needed, it is identical
  8961. (setq description nil))
  8962. (when (and (not description)
  8963. (not (string-match (org-image-file-name-regexp) link))
  8964. (not (equal link (org-link-escape link))))
  8965. (setq description (org-extract-attributes link)))
  8966. (setq link
  8967. (cond ((string-match (org-image-file-name-regexp) link) link)
  8968. ((string-match org-link-types-re link)
  8969. (concat (match-string 1 link)
  8970. (org-link-escape (substring link (match-end 1)))))
  8971. (t (org-link-escape link))))
  8972. (concat "[[" link "]"
  8973. (if description (concat "[" description "]") "")
  8974. "]"))
  8975. (defconst org-link-escape-chars
  8976. ;;%20 %5B %5D
  8977. '(?\ ?\[ ?\])
  8978. "List of characters that should be escaped in a link when stored to Org.
  8979. This is the list that is used for internal purposes.")
  8980. (defconst org-link-escape-chars-browser
  8981. ;;%20 %22
  8982. '(?\ ?\")
  8983. "List of characters to be escaped before handing over to the browser.
  8984. If you consider using this constant then you probably want to use
  8985. the function `org-link-escape-browser' instead. See there why
  8986. this constant is a candidate to be removed once Org drops support
  8987. for Emacs 24.1 and 24.2.")
  8988. (defun org-link-escape (text &optional table merge)
  8989. "Return percent escaped representation of TEXT.
  8990. TEXT is a string with the text to escape.
  8991. Optional argument TABLE is a list with characters that should be
  8992. escaped. When nil, `org-link-escape-chars' is used.
  8993. If optional argument MERGE is set, merge TABLE into
  8994. `org-link-escape-chars'."
  8995. ;; Don't escape chars in internal links
  8996. (if (string-match "^\\*[[:alnum:]]+" text)
  8997. text
  8998. (cond
  8999. ((and table merge)
  9000. (mapc (lambda (defchr)
  9001. (unless (member defchr table)
  9002. (setq table (cons defchr table))))
  9003. org-link-escape-chars))
  9004. ((null table)
  9005. (setq table org-link-escape-chars)))
  9006. (mapconcat
  9007. (lambda (char)
  9008. (if (or (member char table)
  9009. (and (or (< char 32) (= char ?\%) (> char 126))
  9010. org-url-hexify-p))
  9011. (mapconcat (lambda (sequence-element)
  9012. (format "%%%.2X" sequence-element))
  9013. (or (encode-coding-char char 'utf-8)
  9014. (error "Unable to percent escape character: %s"
  9015. (char-to-string char))) "")
  9016. (char-to-string char))) text "")))
  9017. (defun org-link-escape-browser (text)
  9018. "Escape some characters before handing over to the browser.
  9019. This function is a candidate to be removed together with the
  9020. constant `org-link-escape-chars-browser' once Org drops support
  9021. for Emacs 24.1 and 24.2. All calls to this function will have to
  9022. be replaced with `url-encode-url' which is available since Emacs
  9023. 24.3.1."
  9024. ;; Example with the Org link
  9025. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  9026. ;; to open the browser with +subject:"Release 8.2" filled into the
  9027. ;; query field: In this case the variable TEXT contains the
  9028. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  9029. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  9030. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9031. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9032. (if (fboundp 'url-encode-url)
  9033. (url-encode-url text)
  9034. (if (org-string-match-p
  9035. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9036. text)
  9037. (org-link-escape text org-link-escape-chars-browser)
  9038. text)))
  9039. (defun org-link-unescape (str)
  9040. "Unhex hexified Unicode strings as returned from the JavaScript function
  9041. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  9042. (unless (and (null str) (string= "" str))
  9043. (let ((pos 0) (case-fold-search t) unhexed)
  9044. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  9045. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  9046. (setq str (replace-match unhexed t t str))
  9047. (setq pos (+ pos (length unhexed))))))
  9048. str)
  9049. (defun org-link-unescape-compound (hex)
  9050. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9051. Note: this function also decodes single byte encodings like
  9052. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9053. (save-match-data
  9054. (let* ((bytes (cdr (split-string hex "%")))
  9055. (ret "")
  9056. (eat 0)
  9057. (sum 0))
  9058. (while bytes
  9059. (let* ((val (string-to-number (pop bytes) 16))
  9060. (shift-xor
  9061. (if (= 0 eat)
  9062. (cond
  9063. ((>= val 252) (cons 6 252))
  9064. ((>= val 248) (cons 5 248))
  9065. ((>= val 240) (cons 4 240))
  9066. ((>= val 224) (cons 3 224))
  9067. ((>= val 192) (cons 2 192))
  9068. (t (cons 0 0)))
  9069. (cons 6 128))))
  9070. (if (>= val 192) (setq eat (car shift-xor)))
  9071. (setq val (logxor val (cdr shift-xor)))
  9072. (setq sum (+ (lsh sum (car shift-xor)) val))
  9073. (if (> eat 0) (setq eat (- eat 1)))
  9074. (cond
  9075. ((= 0 eat) ;multi byte
  9076. (setq ret (concat ret (org-char-to-string sum)))
  9077. (setq sum 0))
  9078. ((not bytes) ; single byte(s)
  9079. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9080. ret)))
  9081. (defun org-link-unescape-single-byte-sequence (hex)
  9082. "Unhexify hex-encoded single byte character sequences."
  9083. (mapconcat (lambda (byte)
  9084. (char-to-string (string-to-number byte 16)))
  9085. (cdr (split-string hex "%")) ""))
  9086. (defun org-xor (a b)
  9087. "Exclusive or."
  9088. (if a (not b) b))
  9089. (defun org-fixup-message-id-for-http (s)
  9090. "Replace special characters in a message id, so it can be used in an http query."
  9091. (when (string-match "%" s)
  9092. (setq s (mapconcat (lambda (c)
  9093. (if (eq c ?%)
  9094. "%25"
  9095. (char-to-string c)))
  9096. s "")))
  9097. (while (string-match "<" s)
  9098. (setq s (replace-match "%3C" t t s)))
  9099. (while (string-match ">" s)
  9100. (setq s (replace-match "%3E" t t s)))
  9101. (while (string-match "@" s)
  9102. (setq s (replace-match "%40" t t s)))
  9103. s)
  9104. (defun org-link-prettify (link)
  9105. "Return a human-readable representation of LINK.
  9106. The car of LINK must be a raw link.
  9107. The cdr of LINK must be either a link description or nil."
  9108. (let ((desc (or (cadr link) "<no description>")))
  9109. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9110. "<" (car link) ">")))
  9111. ;;;###autoload
  9112. (defun org-insert-link-global ()
  9113. "Insert a link like Org-mode does.
  9114. This command can be called in any mode to insert a link in Org-mode syntax."
  9115. (interactive)
  9116. (org-load-modules-maybe)
  9117. (org-run-like-in-org-mode 'org-insert-link))
  9118. (defun org-insert-all-links (arg &optional pre post)
  9119. "Insert all links in `org-stored-links'.
  9120. When a universal prefix, do not delete the links from `org-stored-links'.
  9121. When `ARG' is a number, insert the last N link(s).
  9122. `PRE' and `POST' are optional arguments to define a string to
  9123. prepend or to append."
  9124. (interactive "P")
  9125. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9126. (links (copy-seq org-stored-links))
  9127. (pr (or pre "- "))
  9128. (po (or post "\n"))
  9129. (cnt 1) l)
  9130. (if (null org-stored-links)
  9131. (message "No link to insert")
  9132. (while (and (or (listp arg) (>= arg cnt))
  9133. (setq l (if (listp arg)
  9134. (pop links)
  9135. (pop org-stored-links))))
  9136. (setq cnt (1+ cnt))
  9137. (insert pr)
  9138. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9139. (insert po)))))
  9140. (defun org-insert-last-stored-link (arg)
  9141. "Insert the last link stored in `org-stored-links'."
  9142. (interactive "p")
  9143. (org-insert-all-links arg "" "\n"))
  9144. (defun org-link-fontify-links-to-this-file ()
  9145. "Fontify links to the current file in `org-stored-links'."
  9146. (let ((f (buffer-file-name)) a b)
  9147. (setq a (mapcar (lambda(l)
  9148. (let ((ll (car l)))
  9149. (when (and (string-match "^file:\\(.+\\)::" ll)
  9150. (equal f (expand-file-name (match-string 1 ll))))
  9151. ll)))
  9152. org-stored-links))
  9153. (when (featurep 'org-id)
  9154. (setq b (mapcar (lambda(l)
  9155. (let ((ll (car l)))
  9156. (when (and (string-match "^id:\\(.+\\)$" ll)
  9157. (equal f (expand-file-name
  9158. (or (org-id-find-id-file
  9159. (match-string 1 ll)) ""))))
  9160. ll)))
  9161. org-stored-links)))
  9162. (mapcar (lambda(l)
  9163. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9164. (delq nil (append a b)))))
  9165. (defvar org-link-links-in-this-file nil)
  9166. (defun org-insert-link (&optional complete-file link-location default-description)
  9167. "Insert a link. At the prompt, enter the link.
  9168. Completion can be used to insert any of the link protocol prefixes like
  9169. http or ftp in use.
  9170. The history can be used to select a link previously stored with
  9171. `org-store-link'. When the empty string is entered (i.e. if you just
  9172. press RET at the prompt), the link defaults to the most recently
  9173. stored link. As SPC triggers completion in the minibuffer, you need to
  9174. use M-SPC or C-q SPC to force the insertion of a space character.
  9175. You will also be prompted for a description, and if one is given, it will
  9176. be displayed in the buffer instead of the link.
  9177. If there is already a link at point, this command will allow you to edit link
  9178. and description parts.
  9179. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9180. be selected using completion. The path to the file will be relative to the
  9181. current directory if the file is in the current directory or a subdirectory.
  9182. Otherwise, the link will be the absolute path as completed in the minibuffer
  9183. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9184. option `org-link-file-path-type'.
  9185. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9186. the current directory or below.
  9187. With three \\[universal-argument] prefixes, negate the meaning of
  9188. `org-keep-stored-link-after-insertion'.
  9189. If `org-make-link-description-function' is non-nil, this function will be
  9190. called with the link target, and the result will be the default
  9191. link description.
  9192. If the LINK-LOCATION parameter is non-nil, this value will be
  9193. used as the link location instead of reading one interactively.
  9194. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9195. be used as the default description."
  9196. (interactive "P")
  9197. (let* ((wcf (current-window-configuration))
  9198. (origbuf (current-buffer))
  9199. (region (if (org-region-active-p)
  9200. (buffer-substring (region-beginning) (region-end))))
  9201. (remove (and region (list (region-beginning) (region-end))))
  9202. (desc region)
  9203. tmphist ; byte-compile incorrectly complains about this
  9204. (link link-location)
  9205. (abbrevs org-link-abbrev-alist-local)
  9206. entry file all-prefixes auto-desc)
  9207. (cond
  9208. (link-location) ; specified by arg, just use it.
  9209. ((org-in-regexp org-bracket-link-regexp 1)
  9210. ;; We do have a link at point, and we are going to edit it.
  9211. (setq remove (list (match-beginning 0) (match-end 0)))
  9212. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9213. (setq link (read-string "Link: "
  9214. (org-link-unescape
  9215. (org-match-string-no-properties 1)))))
  9216. ((or (org-in-regexp org-angle-link-re)
  9217. (org-in-regexp org-plain-link-re))
  9218. ;; Convert to bracket link
  9219. (setq remove (list (match-beginning 0) (match-end 0))
  9220. link (read-string "Link: "
  9221. (org-remove-angle-brackets (match-string 0)))))
  9222. ((member complete-file '((4) (16)))
  9223. ;; Completing read for file names.
  9224. (setq link (org-file-complete-link complete-file)))
  9225. (t
  9226. ;; Read link, with completion for stored links.
  9227. (org-link-fontify-links-to-this-file)
  9228. (org-switch-to-buffer-other-window "*Org Links*")
  9229. (with-current-buffer "*Org Links*"
  9230. (erase-buffer)
  9231. (insert "Insert a link.
  9232. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9233. (when org-stored-links
  9234. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9235. (insert (mapconcat 'org-link-prettify
  9236. (reverse org-stored-links) "\n")))
  9237. (goto-char (point-min)))
  9238. (let ((cw (selected-window)))
  9239. (select-window (get-buffer-window "*Org Links*" 'visible))
  9240. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9241. (unless (pos-visible-in-window-p (point-max))
  9242. (org-fit-window-to-buffer))
  9243. (and (window-live-p cw) (select-window cw)))
  9244. ;; Fake a link history, containing the stored links.
  9245. (setq tmphist (append (mapcar 'car org-stored-links)
  9246. org-insert-link-history))
  9247. (setq all-prefixes (append (mapcar 'car abbrevs)
  9248. (mapcar 'car org-link-abbrev-alist)
  9249. org-link-types))
  9250. (unwind-protect
  9251. (progn
  9252. (setq link
  9253. (org-completing-read
  9254. "Link: "
  9255. (append
  9256. (mapcar (lambda (x) (concat x ":"))
  9257. all-prefixes)
  9258. (mapcar 'car org-stored-links))
  9259. nil nil nil
  9260. 'tmphist
  9261. (caar org-stored-links)))
  9262. (if (not (string-match "\\S-" link))
  9263. (user-error "No link selected"))
  9264. (mapc (lambda(l)
  9265. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9266. org-stored-links)
  9267. (if (or (member link all-prefixes)
  9268. (and (equal ":" (substring link -1))
  9269. (member (substring link 0 -1) all-prefixes)
  9270. (setq link (substring link 0 -1))))
  9271. (setq link (with-current-buffer origbuf
  9272. (org-link-try-special-completion link)))))
  9273. (set-window-configuration wcf)
  9274. (kill-buffer "*Org Links*"))
  9275. (setq entry (assoc link org-stored-links))
  9276. (or entry (push link org-insert-link-history))
  9277. (setq desc (or desc (nth 1 entry)))))
  9278. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9279. (not org-keep-stored-link-after-insertion))
  9280. (setq org-stored-links (delq (assoc link org-stored-links)
  9281. org-stored-links)))
  9282. (if (and (string-match org-plain-link-re link)
  9283. (not (string-match org-ts-regexp link)))
  9284. ;; URL-like link, normalize the use of angular brackets.
  9285. (setq link (org-remove-angle-brackets link)))
  9286. ;; Check if we are linking to the current file with a search
  9287. ;; option If yes, simplify the link by using only the search
  9288. ;; option.
  9289. (when (and buffer-file-name
  9290. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9291. (let* ((path (match-string 1 link))
  9292. (case-fold-search nil)
  9293. (search (match-string 2 link)))
  9294. (save-match-data
  9295. (if (equal (file-truename buffer-file-name) (file-truename path))
  9296. ;; We are linking to this same file, with a search option
  9297. (setq link search)))))
  9298. ;; Check if we can/should use a relative path. If yes, simplify the link
  9299. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9300. (let* ((type (match-string 1 link))
  9301. (path (match-string 2 link))
  9302. (origpath path)
  9303. (case-fold-search nil))
  9304. (cond
  9305. ((or (eq org-link-file-path-type 'absolute)
  9306. (equal complete-file '(16)))
  9307. (setq path (abbreviate-file-name (expand-file-name path))))
  9308. ((eq org-link-file-path-type 'noabbrev)
  9309. (setq path (expand-file-name path)))
  9310. ((eq org-link-file-path-type 'relative)
  9311. (setq path (file-relative-name path)))
  9312. (t
  9313. (save-match-data
  9314. (if (string-match (concat "^" (regexp-quote
  9315. (expand-file-name
  9316. (file-name-as-directory
  9317. default-directory))))
  9318. (expand-file-name path))
  9319. ;; We are linking a file with relative path name.
  9320. (setq path (substring (expand-file-name path)
  9321. (match-end 0)))
  9322. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9323. (setq link (concat type path))
  9324. (if (equal desc origpath)
  9325. (setq desc path))))
  9326. (if org-make-link-description-function
  9327. (setq desc
  9328. (or (condition-case nil
  9329. (funcall org-make-link-description-function link desc)
  9330. (error (progn (message "Can't get link description from `%s'"
  9331. (symbol-name org-make-link-description-function))
  9332. (sit-for 2) nil)))
  9333. (read-string "Description: " default-description)))
  9334. (if default-description (setq desc default-description)
  9335. (setq desc (or (and auto-desc desc)
  9336. (read-string "Description: " desc)))))
  9337. (unless (string-match "\\S-" desc) (setq desc nil))
  9338. (if remove (apply 'delete-region remove))
  9339. (insert (org-make-link-string link desc))))
  9340. (defun org-link-try-special-completion (type)
  9341. "If there is completion support for link type TYPE, offer it."
  9342. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9343. (if (functionp fun)
  9344. (funcall fun)
  9345. (read-string "Link (no completion support): " (concat type ":")))))
  9346. (defun org-file-complete-link (&optional arg)
  9347. "Create a file link using completion."
  9348. (let (file link)
  9349. (setq file (org-iread-file-name "File: "))
  9350. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9351. (pwd1 (file-name-as-directory (abbreviate-file-name
  9352. (expand-file-name ".")))))
  9353. (cond
  9354. ((equal arg '(16))
  9355. (setq link (concat
  9356. "file:"
  9357. (abbreviate-file-name (expand-file-name file)))))
  9358. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9359. (setq link (concat "file:" (match-string 1 file))))
  9360. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9361. (expand-file-name file))
  9362. (setq link (concat
  9363. "file:" (match-string 1 (expand-file-name file)))))
  9364. (t (setq link (concat "file:" file)))))
  9365. link))
  9366. (defun org-iread-file-name (&rest args)
  9367. "Read-file-name using `ido-mode' speedup if available.
  9368. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9369. See `read-file-name' for a description of parameters."
  9370. (org-without-partial-completion
  9371. (if (and org-completion-use-ido
  9372. (fboundp 'ido-read-file-name)
  9373. (boundp 'ido-mode) ido-mode
  9374. (listp (second args)))
  9375. (let ((ido-enter-matching-directory nil))
  9376. (apply 'ido-read-file-name args))
  9377. (apply 'read-file-name args))))
  9378. (defun org-completing-read (&rest args)
  9379. "Completing-read with SPACE being a normal character."
  9380. (let ((enable-recursive-minibuffers t)
  9381. (minibuffer-local-completion-map
  9382. (copy-keymap minibuffer-local-completion-map)))
  9383. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9384. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9385. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9386. (apply 'org-icompleting-read args)))
  9387. (defun org-completing-read-no-i (&rest args)
  9388. (let (org-completion-use-ido org-completion-use-iswitchb)
  9389. (apply 'org-completing-read args)))
  9390. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9391. "Use iswitch as a completing-read replacement to choose from choices.
  9392. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9393. from."
  9394. (let* ((iswitchb-use-virtual-buffers nil)
  9395. (iswitchb-make-buflist-hook
  9396. (lambda ()
  9397. (setq iswitchb-temp-buflist choices))))
  9398. (iswitchb-read-buffer prompt)))
  9399. (defun org-icompleting-read (&rest args)
  9400. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9401. (org-without-partial-completion
  9402. (if (and org-completion-use-ido
  9403. (fboundp 'ido-completing-read)
  9404. (boundp 'ido-mode) ido-mode
  9405. (listp (second args)))
  9406. (let ((ido-enter-matching-directory nil))
  9407. (apply 'ido-completing-read (concat (car args))
  9408. (if (consp (car (nth 1 args)))
  9409. (mapcar 'car (nth 1 args))
  9410. (nth 1 args))
  9411. (cddr args)))
  9412. (if (and org-completion-use-iswitchb
  9413. (boundp 'iswitchb-mode) iswitchb-mode
  9414. (listp (second args)))
  9415. (apply 'org-iswitchb-completing-read (concat (car args))
  9416. (if (consp (car (nth 1 args)))
  9417. (mapcar 'car (nth 1 args))
  9418. (nth 1 args))
  9419. (cddr args))
  9420. (apply 'completing-read args)))))
  9421. (defun org-extract-attributes (s)
  9422. "Extract the attributes cookie from a string and set as text property."
  9423. (let (a attr (start 0) key value)
  9424. (save-match-data
  9425. (when (string-match "{{\\([^}]+\\)}}$" s)
  9426. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9427. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9428. (setq key (match-string 1 a) value (match-string 2 a)
  9429. start (match-end 0)
  9430. attr (plist-put attr (intern key) value))))
  9431. (org-add-props s nil 'org-attr attr))
  9432. s))
  9433. ;;; Opening/following a link
  9434. (defvar org-link-search-failed nil)
  9435. (defvar org-open-link-functions nil
  9436. "Hook for functions finding a plain text link.
  9437. These functions must take a single argument, the link content.
  9438. They will be called for links that look like [[link text][description]]
  9439. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9440. like a filename (e.g. \"./blue.png\").
  9441. These functions will be called *before* Org attempts to resolve the
  9442. link by doing text searches in the current buffer - so if you want a
  9443. link \"[[target]]\" to still find \"<<target>>\", your function should
  9444. handle this as a special case.
  9445. When the function does handle the link, it must return a non-nil value.
  9446. If it decides that it is not responsible for this link, it must return
  9447. nil to indicate that that Org-mode can continue with other options
  9448. like exact and fuzzy text search.")
  9449. (defun org-next-link (&optional search-backward)
  9450. "Move forward to the next link.
  9451. If the link is in hidden text, expose it."
  9452. (interactive "P")
  9453. (when (and org-link-search-failed (eq this-command last-command))
  9454. (goto-char (point-min))
  9455. (message "Link search wrapped back to beginning of buffer"))
  9456. (setq org-link-search-failed nil)
  9457. (let* ((pos (point))
  9458. (ct (org-context))
  9459. (a (assoc :link ct))
  9460. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9461. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9462. ((looking-at org-any-link-re)
  9463. ;; Don't stay stuck at link without an org-link face
  9464. (forward-char (if search-backward -1 1))))
  9465. (if (funcall srch-fun org-any-link-re nil t)
  9466. (progn
  9467. (goto-char (match-beginning 0))
  9468. (if (outline-invisible-p) (org-show-context)))
  9469. (goto-char pos)
  9470. (setq org-link-search-failed t)
  9471. (message "No further link found"))))
  9472. (defun org-previous-link ()
  9473. "Move backward to the previous link.
  9474. If the link is in hidden text, expose it."
  9475. (interactive)
  9476. (funcall 'org-next-link t))
  9477. (defun org-translate-link (s)
  9478. "Translate a link string if a translation function has been defined."
  9479. (if (and org-link-translation-function
  9480. (fboundp org-link-translation-function)
  9481. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9482. (progn
  9483. (setq s (funcall org-link-translation-function
  9484. (match-string 1 s) (match-string 2 s)))
  9485. (concat (car s) ":" (cdr s)))
  9486. s))
  9487. (defun org-translate-link-from-planner (type path)
  9488. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9489. This is still an experimental function, your mileage may vary."
  9490. (cond
  9491. ((member type '("http" "https" "news" "ftp"))
  9492. ;; standard Internet links are the same.
  9493. nil)
  9494. ((and (equal type "irc") (string-match "^//" path))
  9495. ;; Planner has two / at the beginning of an irc link, we have 1.
  9496. ;; We should have zero, actually....
  9497. (setq path (substring path 1)))
  9498. ((and (equal type "lisp") (string-match "^/" path))
  9499. ;; Planner has a slash, we do not.
  9500. (setq type "elisp" path (substring path 1)))
  9501. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9502. ;; A typical message link. Planner has the id after the final slash,
  9503. ;; we separate it with a hash mark
  9504. (setq path (concat (match-string 1 path) "#"
  9505. (org-remove-angle-brackets (match-string 2 path))))))
  9506. (cons type path))
  9507. (defun org-find-file-at-mouse (ev)
  9508. "Open file link or URL at mouse."
  9509. (interactive "e")
  9510. (mouse-set-point ev)
  9511. (org-open-at-point 'in-emacs))
  9512. (defun org-open-at-mouse (ev)
  9513. "Open file link or URL at mouse.
  9514. See the docstring of `org-open-file' for details."
  9515. (interactive "e")
  9516. (mouse-set-point ev)
  9517. (if (eq major-mode 'org-agenda-mode)
  9518. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9519. (org-open-at-point))
  9520. (defvar org-window-config-before-follow-link nil
  9521. "The window configuration before following a link.
  9522. This is saved in case the need arises to restore it.")
  9523. (defvar org-open-link-marker (make-marker)
  9524. "Marker pointing to the location where `org-open-at-point' was called.")
  9525. ;;;###autoload
  9526. (defun org-open-at-point-global ()
  9527. "Follow a link like Org-mode does.
  9528. This command can be called in any mode to follow a link that has
  9529. Org-mode syntax."
  9530. (interactive)
  9531. (org-run-like-in-org-mode 'org-open-at-point))
  9532. ;;;###autoload
  9533. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9534. "Open a link in the string S, as if it was in Org-mode."
  9535. (interactive "sLink: \nP")
  9536. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9537. (with-temp-buffer
  9538. (let ((org-inhibit-startup (not reference-buffer)))
  9539. (org-mode)
  9540. (insert s)
  9541. (goto-char (point-min))
  9542. (when reference-buffer
  9543. (setq org-link-abbrev-alist-local
  9544. (with-current-buffer reference-buffer
  9545. org-link-abbrev-alist-local)))
  9546. (org-open-at-point arg reference-buffer)))))
  9547. (defvar org-open-at-point-functions nil
  9548. "Hook that is run when following a link at point.
  9549. Functions in this hook must return t if they identify and follow
  9550. a link at point. If they don't find anything interesting at point,
  9551. they must return nil.")
  9552. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9553. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9554. (defun org-open-at-point (&optional arg reference-buffer)
  9555. "Open link, timestamp, footnote or tags at point.
  9556. When point is on a link, follow it. Normally, files will be
  9557. opened by an appropriate application. If the optional prefix
  9558. argument ARG is non-nil, Emacs will visit the file. With
  9559. a double prefix argument, try to open outside of Emacs, in the
  9560. application the system uses for this file type.
  9561. When point is on a timestamp, open the agenda at the day
  9562. specified.
  9563. When point is a footnote definition, move to the first reference
  9564. found. If it is on a reference, move to the associated
  9565. definition.
  9566. When point is on a headline, display a list of every link in the
  9567. entry, so it is possible to pick one, or all, of them. If point
  9568. is on a tag, call `org-tags-view' instead.
  9569. When optional argument REFERENCE-BUFFER is non-nil, it should
  9570. specify a buffer from where the link search should happen. This
  9571. is used internally by `org-open-link-from-string'.
  9572. On top of syntactically correct links, this function will open
  9573. the link at point in comments or comment blocks and the first
  9574. link in a property drawer line."
  9575. (interactive "P")
  9576. ;; On a code block, open block's results.
  9577. (unless (call-interactively 'org-babel-open-src-block-result)
  9578. (org-load-modules-maybe)
  9579. (move-marker org-open-link-marker (point))
  9580. (setq org-window-config-before-follow-link (current-window-configuration))
  9581. (org-remove-occur-highlights nil nil t)
  9582. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9583. (let* ((context (org-element-context)) type value)
  9584. ;; On an unsupported type, check if point is contained within
  9585. ;; a support one.
  9586. (while (and (not (memq (setq type (org-element-type context))
  9587. '(comment comment-block
  9588. headline inlinetask link
  9589. footnote-definition footnote-reference
  9590. node-property timestamp)))
  9591. (setq context (org-element-property :parent context))))
  9592. (setq value (org-element-property :value context))
  9593. (cond
  9594. ;; Blank lines at the beginning of buffer: bail out.
  9595. ((not context) (user-error "No link found"))
  9596. ;; Exception n°1: links in property drawers
  9597. ((eq type 'node-property)
  9598. (org-open-link-from-string
  9599. (and (string-match org-any-link-re value)
  9600. (match-string-no-properties 0 value))))
  9601. ;; Exception n°2: links in comments.
  9602. ((memq type '(comment comment-block))
  9603. (save-excursion
  9604. (skip-chars-forward "\\S-" (point-at-eol))
  9605. (let ((string-rear (replace-regexp-in-string
  9606. "^[ \t]*# [ \t]*" ""
  9607. (buffer-substring (point) (line-beginning-position))))
  9608. (string-front (buffer-substring (point) (line-end-position))))
  9609. (with-temp-buffer
  9610. (let ((org-inhibit-startup t)) (org-mode))
  9611. (insert value)
  9612. (goto-char (point-min))
  9613. (when (and (search-forward string-rear nil t)
  9614. (search-forward string-front (line-end-position) t))
  9615. (goto-char (match-beginning 0))
  9616. (org-open-at-point)
  9617. (when (string= string-rear "") (forward-char)))))))
  9618. ;; On a headline or an inlinetask, but not on a timestamp,
  9619. ;; a link, a footnote reference or on tags.
  9620. ((and (memq type '(headline inlinetask))
  9621. ;; Not on tags.
  9622. (progn (save-excursion (beginning-of-line)
  9623. (looking-at org-complex-heading-regexp))
  9624. (or (not (match-beginning 5))
  9625. (< (point) (match-beginning 5)))))
  9626. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9627. (links (car data))
  9628. (links-end (cdr data)))
  9629. (if links
  9630. (dolist (link (if (stringp links) (list links) links))
  9631. (search-forward link nil links-end)
  9632. (goto-char (match-beginning 0))
  9633. (org-open-at-point))
  9634. (require 'org-attach)
  9635. (org-attach-reveal 'if-exists))))
  9636. ;; Do nothing on white spaces after an object, unless point
  9637. ;; is right after it.
  9638. ((> (point)
  9639. (save-excursion
  9640. (goto-char (org-element-property :end context))
  9641. (skip-chars-backward " \t")
  9642. (point)))
  9643. (user-error "No link found"))
  9644. ((eq type 'timestamp) (org-follow-timestamp-link))
  9645. ;; On tags within a headline or an inlinetask.
  9646. ((and (memq type '(headline inlinetask))
  9647. (progn (save-excursion (beginning-of-line)
  9648. (looking-at org-complex-heading-regexp))
  9649. (and (match-beginning 5)
  9650. (>= (point) (match-beginning 5)))))
  9651. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9652. ((eq type 'link)
  9653. (let ((type (org-element-property :type context))
  9654. (path (org-link-unescape (org-element-property :path context))))
  9655. ;; Switch back to REFERENCE-BUFFER needed when called in
  9656. ;; a temporary buffer through `org-open-link-from-string'.
  9657. (with-current-buffer (or reference-buffer (current-buffer))
  9658. (cond
  9659. ((equal type "file")
  9660. (if (string-match "[*?{]" (file-name-nondirectory path))
  9661. (dired path)
  9662. ;; Look into `org-link-protocols' in order to find
  9663. ;; a DEDICATED-FUNCTION to open file. The function
  9664. ;; will be applied on raw link instead of parsed
  9665. ;; link due to the limitation in `org-add-link-type'
  9666. ;; ("open" function called with a single argument).
  9667. ;; If no such function is found, fallback to
  9668. ;; `org-open-file'.
  9669. ;;
  9670. ;; Note : "file+emacs" and "file+sys" types are
  9671. ;; hard-coded in order to escape the previous
  9672. ;; limitation.
  9673. (let* ((option (org-element-property :search-option context))
  9674. (app (org-element-property :application context))
  9675. (dedicated-function
  9676. (nth 1 (assoc app org-link-protocols))))
  9677. (if dedicated-function
  9678. (funcall dedicated-function
  9679. (concat path
  9680. (and option (concat "::" option))))
  9681. (apply 'org-open-file
  9682. path
  9683. (cond (arg)
  9684. ((equal app "emacs") 'emacs)
  9685. ((equal app "sys") 'system))
  9686. (cond ((not option) nil)
  9687. ((org-string-match-p "\\`[0-9]+\\'" option)
  9688. (list (string-to-number option)))
  9689. (t (list nil
  9690. (org-link-unescape option)))))))))
  9691. ((assoc type org-link-protocols)
  9692. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9693. ((equal type "help")
  9694. (let ((f-or-v (intern path)))
  9695. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9696. ((boundp f-or-v) (describe-variable f-or-v))
  9697. (t (error "Not a known function or variable")))))
  9698. ((member type '("http" "https" "ftp" "mailto" "news"))
  9699. (browse-url (org-link-escape-browser (concat type ":" path))))
  9700. ((equal type "doi")
  9701. (browse-url
  9702. (org-link-escape-browser (concat org-doi-server-url path))))
  9703. ((equal type "message") (browse-url (concat type ":" path)))
  9704. ((equal type "shell")
  9705. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9706. (cmd path))
  9707. (if (or (and (org-string-nw-p
  9708. org-confirm-shell-link-not-regexp)
  9709. (string-match
  9710. org-confirm-shell-link-not-regexp cmd))
  9711. (not org-confirm-shell-link-function)
  9712. (funcall org-confirm-shell-link-function
  9713. (format "Execute \"%s\" in shell? "
  9714. (org-add-props cmd nil
  9715. 'face 'org-warning))))
  9716. (progn
  9717. (message "Executing %s" cmd)
  9718. (shell-command cmd buf)
  9719. (when (featurep 'midnight)
  9720. (setq clean-buffer-list-kill-buffer-names
  9721. (cons buf
  9722. clean-buffer-list-kill-buffer-names))))
  9723. (user-error "Abort"))))
  9724. ((equal type "elisp")
  9725. (let ((cmd path))
  9726. (if (or (and (org-string-nw-p
  9727. org-confirm-elisp-link-not-regexp)
  9728. (org-string-match-p
  9729. org-confirm-elisp-link-not-regexp cmd))
  9730. (not org-confirm-elisp-link-function)
  9731. (funcall org-confirm-elisp-link-function
  9732. (format "Execute \"%s\" as elisp? "
  9733. (org-add-props cmd nil
  9734. 'face 'org-warning))))
  9735. (message "%s => %s" cmd
  9736. (if (eq (string-to-char cmd) ?\()
  9737. (eval (read cmd))
  9738. (call-interactively (read cmd))))
  9739. (user-error "Abort"))))
  9740. ((equal type "id")
  9741. (require 'ord-id)
  9742. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9743. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9744. (unless (run-hook-with-args-until-success
  9745. 'org-open-link-functions path)
  9746. (if (not arg) (org-mark-ring-push)
  9747. (switch-to-buffer-other-window
  9748. (org-get-buffer-for-internal-link (current-buffer))))
  9749. (let ((cmd `(org-link-search
  9750. ,(if (member type '("custom-id" "coderef"))
  9751. (org-element-property :raw-link context)
  9752. path)
  9753. ,(cond ((equal arg '(4)) 'occur)
  9754. ((equal arg '(16)) 'org-occur))
  9755. ,(org-element-property :begin context))))
  9756. (condition-case nil
  9757. (let ((org-link-search-inhibit-query t))
  9758. (eval cmd))
  9759. (error (progn (widen) (eval cmd)))))))
  9760. (t (browse-url-at-point))))))
  9761. ;; On a footnote reference or at a footnote definition's label.
  9762. ((or (eq type 'footnote-reference)
  9763. (and (eq type 'footnote-definition)
  9764. (save-excursion
  9765. ;; Do not validate action when point is on the
  9766. ;; spaces right after the footnote label, in
  9767. ;; order to be on par with behaviour on links.
  9768. (skip-chars-forward " \t")
  9769. (let ((begin
  9770. (org-element-property :contents-begin context)))
  9771. (if begin (< (point) begin)
  9772. (= (org-element-property :post-affiliated context)
  9773. (line-beginning-position)))))))
  9774. (org-footnote-action))
  9775. (t (user-error "No link found")))))
  9776. (move-marker org-open-link-marker nil)
  9777. (run-hook-with-args 'org-follow-link-hook)))
  9778. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9779. "Offer links in the current entry and return the selected link.
  9780. If there is only one link, return it.
  9781. If NTH is an integer, return the NTH link found.
  9782. If ZERO is a string, check also this string for a link, and if
  9783. there is one, return it."
  9784. (with-current-buffer buffer
  9785. (save-excursion
  9786. (save-restriction
  9787. (widen)
  9788. (goto-char marker)
  9789. (let ((cnt ?0)
  9790. (in-emacs (if (integerp nth) nil nth))
  9791. have-zero end links link c)
  9792. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9793. (push (match-string 0 zero) links)
  9794. (setq cnt (1- cnt) have-zero t))
  9795. (save-excursion
  9796. (org-back-to-heading t)
  9797. (setq end (save-excursion (outline-next-heading) (point)))
  9798. (while (re-search-forward org-any-link-re end t)
  9799. (push (match-string 0) links))
  9800. (setq links (org-uniquify (reverse links))))
  9801. (cond
  9802. ((null links)
  9803. (message "No links"))
  9804. ((equal (length links) 1)
  9805. (setq link (car links)))
  9806. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9807. (setq link (nth (if have-zero nth (1- nth)) links)))
  9808. (t ; we have to select a link
  9809. (save-excursion
  9810. (save-window-excursion
  9811. (delete-other-windows)
  9812. (with-output-to-temp-buffer "*Select Link*"
  9813. (mapc (lambda (l)
  9814. (if (not (string-match org-bracket-link-regexp l))
  9815. (princ (format "[%c] %s\n" (incf cnt)
  9816. (org-remove-angle-brackets l)))
  9817. (if (match-end 3)
  9818. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9819. (match-string 3 l) (match-string 1 l)))
  9820. (princ (format "[%c] %s\n" (incf cnt)
  9821. (match-string 1 l))))))
  9822. links))
  9823. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9824. (message "Select link to open, RET to open all:")
  9825. (setq c (read-char-exclusive))
  9826. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9827. (when (equal c ?q) (user-error "Abort"))
  9828. (if (equal c ?\C-m)
  9829. (setq link links)
  9830. (setq nth (- c ?0))
  9831. (if have-zero (setq nth (1+ nth)))
  9832. (unless (and (integerp nth) (>= (length links) nth))
  9833. (user-error "Invalid link selection"))
  9834. (setq link (nth (1- nth) links)))))
  9835. (cons link end))))))
  9836. ;; TODO: These functions are deprecated since `org-open-at-point'
  9837. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9838. (defun org-open-file-with-system (path)
  9839. "Open file at PATH using the system way of opening it."
  9840. (org-open-file path 'system))
  9841. (defun org-open-file-with-emacs (path)
  9842. "Open file at PATH in Emacs."
  9843. (org-open-file path 'emacs))
  9844. ;;; File search
  9845. (defvar org-create-file-search-functions nil
  9846. "List of functions to construct the right search string for a file link.
  9847. These functions are called in turn with point at the location to
  9848. which the link should point.
  9849. A function in the hook should first test if it would like to
  9850. handle this file type, for example by checking the `major-mode'
  9851. or the file extension. If it decides not to handle this file, it
  9852. should just return nil to give other functions a chance. If it
  9853. does handle the file, it must return the search string to be used
  9854. when following the link. The search string will be part of the
  9855. file link, given after a double colon, and `org-open-at-point'
  9856. will automatically search for it. If special measures must be
  9857. taken to make the search successful, another function should be
  9858. added to the companion hook `org-execute-file-search-functions',
  9859. which see.
  9860. A function in this hook may also use `setq' to set the variable
  9861. `description' to provide a suggestion for the descriptive text to
  9862. be used for this link when it gets inserted into an Org-mode
  9863. buffer with \\[org-insert-link].")
  9864. (defvar org-execute-file-search-functions nil
  9865. "List of functions to execute a file search triggered by a link.
  9866. Functions added to this hook must accept a single argument, the
  9867. search string that was part of the file link, the part after the
  9868. double colon. The function must first check if it would like to
  9869. handle this search, for example by checking the `major-mode' or
  9870. the file extension. If it decides not to handle this search, it
  9871. should just return nil to give other functions a chance. If it
  9872. does handle the search, it must return a non-nil value to keep
  9873. other functions from trying.
  9874. Each function can access the current prefix argument through the
  9875. variable `current-prefix-arg'. Note that a single prefix is used
  9876. to force opening a link in Emacs, so it may be good to only use a
  9877. numeric or double prefix to guide the search function.
  9878. In case this is needed, a function in this hook can also restore
  9879. the window configuration before `org-open-at-point' was called using:
  9880. (set-window-configuration org-window-config-before-follow-link)")
  9881. (defun org-link-search (s &optional type avoid-pos stealth)
  9882. "Search for a link search option.
  9883. If S is surrounded by forward slashes, it is interpreted as a
  9884. regular expression. In org-mode files, this will create an `org-occur'
  9885. sparse tree. In ordinary files, `occur' will be used to list matches.
  9886. If the current buffer is in `dired-mode', grep will be used to search
  9887. in all files. If AVOID-POS is given, ignore matches near that position.
  9888. When optional argument STEALTH is non-nil, do not modify
  9889. visibility around point, thus ignoring
  9890. `org-show-hierarchy-above', `org-show-following-heading' and
  9891. `org-show-siblings' variables."
  9892. (let ((case-fold-search t)
  9893. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9894. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9895. (append '(("") (" ") ("\t") ("\n"))
  9896. org-emphasis-alist)
  9897. "\\|") "\\)"))
  9898. (pos (point))
  9899. (pre nil) (post nil)
  9900. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9901. (cond
  9902. ;; First check if there are any special search functions
  9903. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9904. ;; Now try the builtin stuff
  9905. ((and (equal (string-to-char s0) ?#)
  9906. (> (length s0) 1)
  9907. (save-excursion
  9908. (goto-char (point-min))
  9909. (and
  9910. (re-search-forward
  9911. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9912. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9913. (setq type 'dedicated
  9914. pos (match-beginning 0))))
  9915. ;; There is an exact target for this
  9916. (goto-char pos)
  9917. (org-back-to-heading t)))
  9918. ((save-excursion
  9919. (goto-char (point-min))
  9920. (and
  9921. (re-search-forward
  9922. (concat "<<" (regexp-quote s0) ">>") nil t)
  9923. (setq type 'dedicated
  9924. pos (match-beginning 0))))
  9925. ;; There is an exact target for this
  9926. (goto-char pos))
  9927. ((save-excursion
  9928. (goto-char (point-min))
  9929. (and
  9930. (re-search-forward
  9931. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9932. (setq type 'dedicated pos (match-beginning 0))))
  9933. ;; Found an element with a matching #+name affiliated keyword.
  9934. (goto-char pos))
  9935. ((and (string-match "^(\\(.*\\))$" s0)
  9936. (save-excursion
  9937. (goto-char (point-min))
  9938. (and
  9939. (re-search-forward
  9940. (concat "[^[]" (regexp-quote
  9941. (format org-coderef-label-format
  9942. (match-string 1 s0))))
  9943. nil t)
  9944. (setq type 'dedicated
  9945. pos (1+ (match-beginning 0))))))
  9946. ;; There is a coderef target for this
  9947. (goto-char pos))
  9948. ((string-match "^/\\(.*\\)/$" s)
  9949. ;; A regular expression
  9950. (cond
  9951. ((derived-mode-p 'org-mode)
  9952. (org-occur (match-string 1 s)))
  9953. (t (org-do-occur (match-string 1 s)))))
  9954. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9955. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9956. (goto-char (point-min))
  9957. (cond
  9958. ((let (case-fold-search)
  9959. (re-search-forward (format org-complex-heading-regexp-format
  9960. (regexp-quote s))
  9961. nil t))
  9962. ;; OK, found a match
  9963. (setq type 'dedicated)
  9964. (goto-char (match-beginning 0)))
  9965. ((and (not org-link-search-inhibit-query)
  9966. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9967. (y-or-n-p "No match - create this as a new heading? "))
  9968. (goto-char (point-max))
  9969. (or (bolp) (newline))
  9970. (insert "* " s "\n")
  9971. (beginning-of-line 0))
  9972. (t
  9973. (goto-char pos)
  9974. (error "No match"))))
  9975. (t
  9976. ;; A normal search string
  9977. (when (equal (string-to-char s) ?*)
  9978. ;; Anchor on headlines, post may include tags.
  9979. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9980. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9981. s (substring s 1)))
  9982. (remove-text-properties
  9983. 0 (length s)
  9984. '(face nil mouse-face nil keymap nil fontified nil) s)
  9985. ;; Make a series of regular expressions to find a match
  9986. (setq words (org-split-string s "[ \n\r\t]+")
  9987. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9988. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9989. "\\)" markers)
  9990. re2a_ (concat "\\(" (mapconcat 'downcase words
  9991. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9992. re2a (concat "[ \t\r\n]" re2a_)
  9993. re4_ (concat "\\(" (mapconcat 'downcase words
  9994. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9995. re4 (concat "[^a-zA-Z_]" re4_)
  9996. re1 (concat pre re2 post)
  9997. re3 (concat pre (if pre re4_ re4) post)
  9998. re5 (concat pre ".*" re4)
  9999. re2 (concat pre re2)
  10000. re2a (concat pre (if pre re2a_ re2a))
  10001. re4 (concat pre (if pre re4_ re4))
  10002. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  10003. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  10004. re5 "\\)"))
  10005. (cond
  10006. ((eq type 'org-occur) (org-occur reall))
  10007. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  10008. (t (goto-char (point-min))
  10009. (setq type 'fuzzy)
  10010. (if (or (and (org-search-not-self 1 re0 nil t)
  10011. (setq type 'dedicated))
  10012. (org-search-not-self 1 re1 nil t)
  10013. (org-search-not-self 1 re2 nil t)
  10014. (org-search-not-self 1 re2a nil t)
  10015. (org-search-not-self 1 re3 nil t)
  10016. (org-search-not-self 1 re4 nil t)
  10017. (org-search-not-self 1 re5 nil t))
  10018. (goto-char (match-beginning 1))
  10019. (goto-char pos)
  10020. (error "No match"))))))
  10021. (and (derived-mode-p 'org-mode)
  10022. (not stealth)
  10023. (org-show-context 'link-search))
  10024. type))
  10025. (defun org-search-not-self (group &rest args)
  10026. "Execute `re-search-forward', but only accept matches that do not
  10027. enclose the position of `org-open-link-marker'."
  10028. (let ((m org-open-link-marker))
  10029. (catch 'exit
  10030. (while (apply 're-search-forward args)
  10031. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  10032. (goto-char (match-end group))
  10033. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  10034. (> (match-beginning 0) (marker-position m))
  10035. (< (match-end 0) (marker-position m)))
  10036. (save-match-data
  10037. (or (not (org-in-regexp
  10038. org-bracket-link-analytic-regexp 1))
  10039. (not (match-end 4)) ; no description
  10040. (and (<= (match-beginning 4) (point))
  10041. (>= (match-end 4) (point))))))
  10042. (throw 'exit (point))))))))
  10043. (defun org-get-buffer-for-internal-link (buffer)
  10044. "Return a buffer to be used for displaying the link target of internal links."
  10045. (cond
  10046. ((not org-display-internal-link-with-indirect-buffer)
  10047. buffer)
  10048. ((string-match "(Clone)$" (buffer-name buffer))
  10049. (message "Buffer is already a clone, not making another one")
  10050. ;; we also do not modify visibility in this case
  10051. buffer)
  10052. (t ; make a new indirect buffer for displaying the link
  10053. (let* ((bn (buffer-name buffer))
  10054. (ibn (concat bn "(Clone)"))
  10055. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10056. (with-current-buffer ib (org-overview))
  10057. ib))))
  10058. (defun org-do-occur (regexp &optional cleanup)
  10059. "Call the Emacs command `occur'.
  10060. If CLEANUP is non-nil, remove the printout of the regular expression
  10061. in the *Occur* buffer. This is useful if the regex is long and not useful
  10062. to read."
  10063. (occur regexp)
  10064. (when cleanup
  10065. (let ((cwin (selected-window)) win beg end)
  10066. (when (setq win (get-buffer-window "*Occur*"))
  10067. (select-window win))
  10068. (goto-char (point-min))
  10069. (when (re-search-forward "match[a-z]+" nil t)
  10070. (setq beg (match-end 0))
  10071. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10072. (setq end (1- (match-beginning 0)))))
  10073. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10074. (goto-char (point-min))
  10075. (select-window cwin))))
  10076. ;;; The mark ring for links jumps
  10077. (defvar org-mark-ring nil
  10078. "Mark ring for positions before jumps in Org-mode.")
  10079. (defvar org-mark-ring-last-goto nil
  10080. "Last position in the mark ring used to go back.")
  10081. ;; Fill and close the ring
  10082. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10083. (loop for i from 1 to org-mark-ring-length do
  10084. (push (make-marker) org-mark-ring))
  10085. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10086. org-mark-ring)
  10087. (defun org-mark-ring-push (&optional pos buffer)
  10088. "Put the current position or POS into the mark ring and rotate it."
  10089. (interactive)
  10090. (setq pos (or pos (point)))
  10091. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10092. (move-marker (car org-mark-ring)
  10093. (or pos (point))
  10094. (or buffer (current-buffer)))
  10095. (message "%s"
  10096. (substitute-command-keys
  10097. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10098. (defun org-mark-ring-goto (&optional n)
  10099. "Jump to the previous position in the mark ring.
  10100. With prefix arg N, jump back that many stored positions. When
  10101. called several times in succession, walk through the entire ring.
  10102. Org-mode commands jumping to a different position in the current file,
  10103. or to another Org-mode file, automatically push the old position
  10104. onto the ring."
  10105. (interactive "p")
  10106. (let (p m)
  10107. (if (eq last-command this-command)
  10108. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10109. (setq p org-mark-ring))
  10110. (setq org-mark-ring-last-goto p)
  10111. (setq m (car p))
  10112. (org-pop-to-buffer-same-window (marker-buffer m))
  10113. (goto-char m)
  10114. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10115. (defun org-remove-angle-brackets (s)
  10116. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10117. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10118. s)
  10119. (defun org-add-angle-brackets (s)
  10120. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10121. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10122. s)
  10123. (defun org-remove-double-quotes (s)
  10124. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10125. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10126. s)
  10127. ;;; Following specific links
  10128. (defun org-follow-timestamp-link ()
  10129. "Open an agenda view for the time-stamp date/range at point."
  10130. (cond
  10131. ((org-at-date-range-p t)
  10132. (let ((org-agenda-start-on-weekday)
  10133. (t1 (match-string 1))
  10134. (t2 (match-string 2)) tt1 tt2)
  10135. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10136. tt2 (time-to-days (org-time-string-to-time t2)))
  10137. (let ((org-agenda-buffer-tmp-name
  10138. (format "*Org Agenda(a:%s)"
  10139. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10140. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10141. ((org-at-timestamp-p t)
  10142. (let ((org-agenda-buffer-tmp-name
  10143. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10144. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10145. (substring (match-string 1) 0 10)))
  10146. 1)))
  10147. (t (error "This should not happen"))))
  10148. ;;; Following file links
  10149. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10150. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10151. (declare-function mailcap-mime-info
  10152. "mailcap" (string &optional request no-decode))
  10153. (defvar org-wait nil)
  10154. (defun org-open-file (path &optional in-emacs line search)
  10155. "Open the file at PATH.
  10156. First, this expands any special file name abbreviations. Then the
  10157. configuration variable `org-file-apps' is checked if it contains an
  10158. entry for this file type, and if yes, the corresponding command is launched.
  10159. If no application is found, Emacs simply visits the file.
  10160. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10161. With a double \\[universal-argument] \\[universal-argument] \
  10162. prefix arg, Org tries to avoid opening in Emacs
  10163. and to use an external application to visit the file.
  10164. Optional LINE specifies a line to go to, optional SEARCH a string
  10165. to search for. If LINE or SEARCH is given, the file will be
  10166. opened in Emacs, unless an entry from org-file-apps that makes
  10167. use of groups in a regexp matches.
  10168. If you want to change the way frames are used when following a
  10169. link, please customize `org-link-frame-setup'.
  10170. If the file does not exist, an error is thrown."
  10171. (let* ((file (if (equal path "")
  10172. buffer-file-name
  10173. (substitute-in-file-name (expand-file-name path))))
  10174. (file-apps (append org-file-apps (org-default-apps)))
  10175. (apps (org-remove-if
  10176. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10177. (apps-dlink (org-remove-if-not
  10178. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10179. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10180. (dirp (if remp nil (file-directory-p file)))
  10181. (file (if (and dirp org-open-directory-means-index-dot-org)
  10182. (concat (file-name-as-directory file) "index.org")
  10183. file))
  10184. (a-m-a-p (assq 'auto-mode apps))
  10185. (dfile (downcase file))
  10186. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10187. (link (cond ((and (eq line nil)
  10188. (eq search nil))
  10189. file)
  10190. (line
  10191. (concat file "::" (number-to-string line)))
  10192. (search
  10193. (concat file "::" search))))
  10194. (dlink (downcase link))
  10195. (old-buffer (current-buffer))
  10196. (old-pos (point))
  10197. (old-mode major-mode)
  10198. ext cmd link-match-data)
  10199. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10200. (setq ext (match-string 1 dfile))
  10201. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10202. (setq ext (match-string 1 dfile))))
  10203. (cond
  10204. ((member in-emacs '((16) system))
  10205. (setq cmd (cdr (assoc 'system apps))))
  10206. (in-emacs (setq cmd 'emacs))
  10207. (t
  10208. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10209. (and dirp (cdr (assoc 'directory apps)))
  10210. ; first, try matching against apps-dlink
  10211. ; if we get a match here, store the match data for later
  10212. (let ((match (assoc-default dlink apps-dlink
  10213. 'string-match)))
  10214. (if match
  10215. (progn (setq link-match-data (match-data))
  10216. match)
  10217. (progn (setq in-emacs (or in-emacs line search))
  10218. nil))) ; if we have no match in apps-dlink,
  10219. ; always open the file in emacs if line or search
  10220. ; is given (for backwards compatibility)
  10221. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10222. 'string-match)
  10223. (cdr (assoc ext apps))
  10224. (cdr (assoc t apps))))))
  10225. (when (eq cmd 'system)
  10226. (setq cmd (cdr (assoc 'system apps))))
  10227. (when (eq cmd 'default)
  10228. (setq cmd (cdr (assoc t apps))))
  10229. (when (eq cmd 'mailcap)
  10230. (require 'mailcap)
  10231. (mailcap-parse-mailcaps)
  10232. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10233. (command (mailcap-mime-info mime-type)))
  10234. (if (stringp command)
  10235. (setq cmd command)
  10236. (setq cmd 'emacs))))
  10237. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10238. (not (file-exists-p file))
  10239. (not org-open-non-existing-files))
  10240. (user-error "No such file: %s" file))
  10241. (cond
  10242. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10243. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10244. (while (string-match "['\"]%s['\"]" cmd)
  10245. (setq cmd (replace-match "%s" t t cmd)))
  10246. (while (string-match "%s" cmd)
  10247. (setq cmd (replace-match
  10248. (save-match-data
  10249. (shell-quote-argument
  10250. (convert-standard-filename file)))
  10251. t t cmd)))
  10252. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10253. (save-match-data
  10254. (let ((match-index 1)
  10255. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10256. (set-match-data link-match-data)
  10257. (while (<= match-index number-of-groups)
  10258. (let ((regex (concat "%" (number-to-string match-index)))
  10259. (replace-with (match-string match-index dlink)))
  10260. (while (string-match regex cmd)
  10261. (setq cmd (replace-match replace-with t t cmd))))
  10262. (setq match-index (+ match-index 1)))))
  10263. (save-window-excursion
  10264. (message "Running %s...done" cmd)
  10265. (start-process-shell-command cmd nil cmd)
  10266. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10267. ((or (stringp cmd)
  10268. (eq cmd 'emacs))
  10269. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10270. (widen)
  10271. (if line (progn (org-goto-line line)
  10272. (if (derived-mode-p 'org-mode)
  10273. (org-reveal)))
  10274. (if search (org-link-search search))))
  10275. ((consp cmd)
  10276. (let ((file (convert-standard-filename file)))
  10277. (save-match-data
  10278. (set-match-data link-match-data)
  10279. (eval cmd))))
  10280. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10281. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10282. (or (not (equal old-buffer (current-buffer)))
  10283. (not (equal old-pos (point))))
  10284. (org-mark-ring-push old-pos old-buffer))))
  10285. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10286. "This function returns non-nil if `entry' uses a regular
  10287. expression which should be matched against the whole link by
  10288. org-open-file.
  10289. It assumes that is the case when the entry uses a regular
  10290. expression which has at least one grouping construct and the
  10291. action is either a lisp form or a command string containing
  10292. '%1', i.e. using at least one subexpression match as a
  10293. parameter."
  10294. (let ((selector (car entry))
  10295. (action (cdr entry)))
  10296. (if (stringp selector)
  10297. (and (> (regexp-opt-depth selector) 0)
  10298. (or (and (stringp action)
  10299. (string-match "%[0-9]" action))
  10300. (consp action)))
  10301. nil)))
  10302. (defun org-default-apps ()
  10303. "Return the default applications for this operating system."
  10304. (cond
  10305. ((eq system-type 'darwin)
  10306. org-file-apps-defaults-macosx)
  10307. ((eq system-type 'windows-nt)
  10308. org-file-apps-defaults-windowsnt)
  10309. (t org-file-apps-defaults-gnu)))
  10310. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10311. "Convert extensions to regular expressions in the cars of LIST.
  10312. Also, weed out any non-string entries, because the return value is used
  10313. only for regexp matching.
  10314. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10315. point to the symbol `emacs', indicating that the file should
  10316. be opened in Emacs."
  10317. (append
  10318. (delq nil
  10319. (mapcar (lambda (x)
  10320. (if (not (stringp (car x)))
  10321. nil
  10322. (if (string-match "\\W" (car x))
  10323. x
  10324. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10325. list))
  10326. (if add-auto-mode
  10327. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10328. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10329. (defun org-file-remote-p (file)
  10330. "Test whether FILE specifies a location on a remote system.
  10331. Return non-nil if the location is indeed remote.
  10332. For example, the filename \"/user@host:/foo\" specifies a location
  10333. on the system \"/user@host:\"."
  10334. (cond ((fboundp 'file-remote-p)
  10335. (file-remote-p file))
  10336. ((fboundp 'tramp-handle-file-remote-p)
  10337. (tramp-handle-file-remote-p file))
  10338. ((and (boundp 'ange-ftp-name-format)
  10339. (string-match (car ange-ftp-name-format) file))
  10340. t)))
  10341. ;;;; Refiling
  10342. (defun org-get-org-file ()
  10343. "Read a filename, with default directory `org-directory'."
  10344. (let ((default (or org-default-notes-file remember-data-file)))
  10345. (read-file-name (format "File name [%s]: " default)
  10346. (file-name-as-directory org-directory)
  10347. default)))
  10348. (defun org-notes-order-reversed-p ()
  10349. "Check if the current file should receive notes in reversed order."
  10350. (cond
  10351. ((not org-reverse-note-order) nil)
  10352. ((eq t org-reverse-note-order) t)
  10353. ((not (listp org-reverse-note-order)) nil)
  10354. (t (catch 'exit
  10355. (let ((all org-reverse-note-order)
  10356. entry)
  10357. (while (setq entry (pop all))
  10358. (if (string-match (car entry) buffer-file-name)
  10359. (throw 'exit (cdr entry))))
  10360. nil)))))
  10361. (defvar org-refile-target-table nil
  10362. "The list of refile targets, created by `org-refile'.")
  10363. (defvar org-agenda-new-buffers nil
  10364. "Buffers created to visit agenda files.")
  10365. (defvar org-refile-cache nil
  10366. "Cache for refile targets.")
  10367. (defvar org-refile-markers nil
  10368. "All the markers used for caching refile locations.")
  10369. (defun org-refile-marker (pos)
  10370. "Get a new refile marker, but only if caching is in use."
  10371. (if (not org-refile-use-cache)
  10372. pos
  10373. (let ((m (make-marker)))
  10374. (move-marker m pos)
  10375. (push m org-refile-markers)
  10376. m)))
  10377. (defun org-refile-cache-clear ()
  10378. "Clear the refile cache and disable all the markers."
  10379. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10380. (setq org-refile-markers nil)
  10381. (setq org-refile-cache nil)
  10382. (message "Refile cache has been cleared"))
  10383. (defun org-refile-cache-check-set (set)
  10384. "Check if all the markers in the cache still have live buffers."
  10385. (let (marker)
  10386. (catch 'exit
  10387. (while (and set (setq marker (nth 3 (pop set))))
  10388. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10389. (when (and marker (null (marker-buffer marker)))
  10390. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10391. (sit-for 3)
  10392. (throw 'exit nil)))
  10393. t)))
  10394. (defun org-refile-cache-put (set &rest identifiers)
  10395. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10396. (let* ((key (sha1 (prin1-to-string identifiers)))
  10397. (entry (assoc key org-refile-cache)))
  10398. (if entry
  10399. (setcdr entry set)
  10400. (push (cons key set) org-refile-cache))))
  10401. (defun org-refile-cache-get (&rest identifiers)
  10402. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10403. (cond
  10404. ((not org-refile-cache) nil)
  10405. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10406. (t
  10407. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10408. org-refile-cache))))
  10409. (and set (org-refile-cache-check-set set) set)))))
  10410. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10411. "Produce a table with refile targets."
  10412. (let ((case-fold-search nil)
  10413. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10414. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10415. targets tgs txt re files f desc descre fast-path-p level pos0)
  10416. (message "Getting targets...")
  10417. (with-current-buffer (or default-buffer (current-buffer))
  10418. (while (setq entry (pop entries))
  10419. (setq files (car entry) desc (cdr entry))
  10420. (setq fast-path-p nil)
  10421. (cond
  10422. ((null files) (setq files (list (current-buffer))))
  10423. ((eq files 'org-agenda-files)
  10424. (setq files (org-agenda-files 'unrestricted)))
  10425. ((and (symbolp files) (fboundp files))
  10426. (setq files (funcall files)))
  10427. ((and (symbolp files) (boundp files))
  10428. (setq files (symbol-value files))))
  10429. (if (stringp files) (setq files (list files)))
  10430. (cond
  10431. ((eq (car desc) :tag)
  10432. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10433. ((eq (car desc) :todo)
  10434. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10435. ((eq (car desc) :regexp)
  10436. (setq descre (cdr desc)))
  10437. ((eq (car desc) :level)
  10438. (setq descre (concat "^\\*\\{" (number-to-string
  10439. (if org-odd-levels-only
  10440. (1- (* 2 (cdr desc)))
  10441. (cdr desc)))
  10442. "\\}[ \t]")))
  10443. ((eq (car desc) :maxlevel)
  10444. (setq fast-path-p t)
  10445. (setq descre (concat "^\\*\\{1," (number-to-string
  10446. (if org-odd-levels-only
  10447. (1- (* 2 (cdr desc)))
  10448. (cdr desc)))
  10449. "\\}[ \t]")))
  10450. (t (error "Bad refiling target description %s" desc)))
  10451. (while (setq f (pop files))
  10452. (with-current-buffer
  10453. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10454. (or
  10455. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10456. (progn
  10457. (if (bufferp f) (setq f (buffer-file-name
  10458. (buffer-base-buffer f))))
  10459. (setq f (and f (expand-file-name f)))
  10460. (if (eq org-refile-use-outline-path 'file)
  10461. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10462. (save-excursion
  10463. (save-restriction
  10464. (widen)
  10465. (goto-char (point-min))
  10466. (while (re-search-forward descre nil t)
  10467. (goto-char (setq pos0 (point-at-bol)))
  10468. (catch 'next
  10469. (when org-refile-target-verify-function
  10470. (save-match-data
  10471. (or (funcall org-refile-target-verify-function)
  10472. (throw 'next t))))
  10473. (when (and (looking-at org-complex-heading-regexp)
  10474. (not (member (match-string 4) excluded-entries))
  10475. (match-string 4))
  10476. (setq level (org-reduced-level
  10477. (- (match-end 1) (match-beginning 1)))
  10478. txt (org-link-display-format (match-string 4))
  10479. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10480. re (format org-complex-heading-regexp-format
  10481. (regexp-quote (match-string 4))))
  10482. (when org-refile-use-outline-path
  10483. (setq txt (mapconcat
  10484. 'org-protect-slash
  10485. (append
  10486. (if (eq org-refile-use-outline-path
  10487. 'file)
  10488. (list (file-name-nondirectory
  10489. (buffer-file-name
  10490. (buffer-base-buffer))))
  10491. (if (eq org-refile-use-outline-path
  10492. 'full-file-path)
  10493. (list (buffer-file-name
  10494. (buffer-base-buffer)))))
  10495. (org-get-outline-path fast-path-p
  10496. level txt)
  10497. (list txt))
  10498. "/")))
  10499. (push (list txt f re (org-refile-marker (point)))
  10500. tgs)))
  10501. (when (= (point) pos0)
  10502. ;; verification function has not moved point
  10503. (goto-char (point-at-eol))))))))
  10504. (when org-refile-use-cache
  10505. (org-refile-cache-put tgs (buffer-file-name) descre))
  10506. (setq targets (append tgs targets))))))
  10507. (message "Getting targets...done")
  10508. (nreverse targets)))
  10509. (defun org-protect-slash (s)
  10510. (while (string-match "/" s)
  10511. (setq s (replace-match "\\" t t s)))
  10512. s)
  10513. (defvar org-olpa (make-vector 20 nil))
  10514. (defun org-get-outline-path (&optional fastp level heading)
  10515. "Return the outline path to the current entry, as a list.
  10516. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10517. routine which makes outline path derivations for an entire file,
  10518. avoiding backtracing. Refile target collection makes use of that."
  10519. (if fastp
  10520. (progn
  10521. (if (> level 19)
  10522. (error "Outline path failure, more than 19 levels"))
  10523. (loop for i from level upto 19 do
  10524. (aset org-olpa i nil))
  10525. (prog1
  10526. (delq nil (append org-olpa nil))
  10527. (aset org-olpa level heading)))
  10528. (let (rtn case-fold-search)
  10529. (save-excursion
  10530. (save-restriction
  10531. (widen)
  10532. (while (org-up-heading-safe)
  10533. (when (looking-at org-complex-heading-regexp)
  10534. (push (org-trim
  10535. (replace-regexp-in-string
  10536. ;; Remove statistical/checkboxes cookies
  10537. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10538. (org-match-string-no-properties 4)))
  10539. rtn)))
  10540. rtn)))))
  10541. (defun org-format-outline-path (path &optional width prefix separator)
  10542. "Format the outline path PATH for display.
  10543. WIDTH is the maximum number of characters that is available.
  10544. PREFIX is a prefix to be included in the returned string,
  10545. such as the file name.
  10546. SEPARATOR is inserted between the different parts of the path,
  10547. the default is \"/\"."
  10548. (setq width (or width 79))
  10549. (if prefix (setq width (- width (length prefix))))
  10550. (if (not path)
  10551. (or prefix "")
  10552. (let* ((nsteps (length path))
  10553. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10554. (maxwidth (if (<= total-width width)
  10555. 10000 ;; everything fits
  10556. ;; we need to shorten the level headings
  10557. (/ (- width nsteps) nsteps)))
  10558. (org-odd-levels-only nil)
  10559. (n 0)
  10560. (total (1+ (length prefix))))
  10561. (setq maxwidth (max maxwidth 10))
  10562. (concat prefix
  10563. (if prefix (or separator "/"))
  10564. (mapconcat
  10565. (lambda (h)
  10566. (setq n (1+ n))
  10567. (if (and (= n nsteps) (< maxwidth 10000))
  10568. (setq maxwidth (- total-width total)))
  10569. (if (< (length h) maxwidth)
  10570. (progn (setq total (+ total (length h) 1)) h)
  10571. (setq h (substring h 0 (- maxwidth 2))
  10572. total (+ total maxwidth 1))
  10573. (if (string-match "[ \t]+\\'" h)
  10574. (setq h (substring h 0 (match-beginning 0))))
  10575. (setq h (concat h "..")))
  10576. (org-add-props h nil 'face
  10577. (nth (% (1- n) org-n-level-faces)
  10578. org-level-faces))
  10579. h)
  10580. path (or separator "/"))))))
  10581. (defun org-display-outline-path (&optional file current separator just-return-string)
  10582. "Display the current outline path in the echo area.
  10583. If FILE is non-nil, prepend the output with the file name.
  10584. If CURRENT is non-nil, append the current heading to the output.
  10585. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10586. the different parts of the path and defaults to \"/\".
  10587. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10588. (interactive "P")
  10589. (let* (case-fold-search
  10590. (bfn (buffer-file-name (buffer-base-buffer)))
  10591. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10592. res)
  10593. (if current (setq path (append path
  10594. (save-excursion
  10595. (org-back-to-heading t)
  10596. (if (looking-at org-complex-heading-regexp)
  10597. (list (match-string 4)))))))
  10598. (setq res
  10599. (org-format-outline-path
  10600. path
  10601. (1- (frame-width))
  10602. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10603. separator))
  10604. (if just-return-string
  10605. (org-no-properties res)
  10606. (org-unlogged-message "%s" res))))
  10607. (defvar org-refile-history nil
  10608. "History for refiling operations.")
  10609. (defvar org-after-refile-insert-hook nil
  10610. "Hook run after `org-refile' has inserted its stuff at the new location.
  10611. Note that this is still *before* the stuff will be removed from
  10612. the *old* location.")
  10613. (defvar org-capture-last-stored-marker)
  10614. (defvar org-refile-keep nil
  10615. "Non-nil means `org-refile' will copy instead of refile.")
  10616. (defun org-copy ()
  10617. "Like `org-refile', but copy."
  10618. (interactive)
  10619. (let ((org-refile-keep t))
  10620. (funcall 'org-refile nil nil nil "Copy")))
  10621. (defun org-refile (&optional arg default-buffer rfloc msg)
  10622. "Move the entry or entries at point to another heading.
  10623. The list of target headings is compiled using the information in
  10624. `org-refile-targets', which see.
  10625. At the target location, the entry is filed as a subitem of the
  10626. target heading. Depending on `org-reverse-note-order', the new
  10627. subitem will either be the first or the last subitem.
  10628. If there is an active region, all entries in that region will be
  10629. refiled. However, the region must fulfill the requirement that
  10630. the first heading sets the top-level of the moved text.
  10631. With prefix arg ARG, the command will only visit the target
  10632. location and not actually move anything.
  10633. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10634. refiling operation has put the subtree.
  10635. With a numeric prefix argument of `2', refile to the running clock.
  10636. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10637. being set to `t' and copy to the target location, don't move it.
  10638. Beware that keeping refiled entries may result in duplicated ID
  10639. properties.
  10640. RFLOC can be a refile location obtained in a different way.
  10641. MSG is a string to replace \"Refile\" in the default prompt with
  10642. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10643. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10644. If you are using target caching (see `org-refile-use-cache'), you
  10645. have to clear the target cache in order to find new targets.
  10646. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10647. prefix argument (`C-u C-u C-u C-c C-w')."
  10648. (interactive "P")
  10649. (if (member arg '(0 (64)))
  10650. (org-refile-cache-clear)
  10651. (let* ((actionmsg (cond (msg msg)
  10652. ((equal arg 3) "Refile (and keep)")
  10653. (t "Refile")))
  10654. (cbuf (current-buffer))
  10655. (regionp (org-region-active-p))
  10656. (region-start (and regionp (region-beginning)))
  10657. (region-end (and regionp (region-end)))
  10658. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10659. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10660. pos it nbuf file re level reversed)
  10661. (setq last-command nil)
  10662. (when regionp
  10663. (goto-char region-start)
  10664. (or (bolp) (goto-char (point-at-bol)))
  10665. (setq region-start (point))
  10666. (unless (or (org-kill-is-subtree-p
  10667. (buffer-substring region-start region-end))
  10668. (prog1 org-refile-active-region-within-subtree
  10669. (let ((s (point-at-eol)))
  10670. (org-toggle-heading)
  10671. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10672. (user-error "The region is not a (sequence of) subtree(s)")))
  10673. (if (equal arg '(16))
  10674. (org-refile-goto-last-stored)
  10675. (when (or
  10676. (and (equal arg 2)
  10677. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10678. (prog1
  10679. (setq it (list (or org-clock-heading "running clock")
  10680. (buffer-file-name
  10681. (marker-buffer org-clock-hd-marker))
  10682. ""
  10683. (marker-position org-clock-hd-marker)))
  10684. (setq arg nil)))
  10685. (setq it (or rfloc
  10686. (let (heading-text)
  10687. (save-excursion
  10688. (unless (and arg (listp arg))
  10689. (org-back-to-heading t)
  10690. (setq heading-text
  10691. (replace-regexp-in-string
  10692. org-bracket-link-regexp
  10693. "\\3"
  10694. (nth 4 (org-heading-components)))))
  10695. (org-refile-get-location
  10696. (cond ((and arg (listp arg)) "Goto")
  10697. (regionp (concat actionmsg " region to"))
  10698. (t (concat actionmsg " subtree \""
  10699. heading-text "\" to")))
  10700. default-buffer
  10701. (and (not (equal '(4) arg))
  10702. org-refile-allow-creating-parent-nodes)
  10703. arg))))))
  10704. (setq file (nth 1 it)
  10705. re (nth 2 it)
  10706. pos (nth 3 it))
  10707. (if (and (not arg)
  10708. pos
  10709. (equal (buffer-file-name) file)
  10710. (if regionp
  10711. (and (>= pos region-start)
  10712. (<= pos region-end))
  10713. (and (>= pos (point))
  10714. (< pos (save-excursion
  10715. (org-end-of-subtree t t))))))
  10716. (error "Cannot refile to position inside the tree or region"))
  10717. (setq nbuf (or (find-buffer-visiting file)
  10718. (find-file-noselect file)))
  10719. (if (and arg (not (equal arg 3)))
  10720. (progn
  10721. (org-pop-to-buffer-same-window nbuf)
  10722. (goto-char pos)
  10723. (org-show-context 'org-goto))
  10724. (if regionp
  10725. (progn
  10726. (org-kill-new (buffer-substring region-start region-end))
  10727. (org-save-markers-in-region region-start region-end))
  10728. (org-copy-subtree 1 nil t))
  10729. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10730. (find-file-noselect file)))
  10731. (setq reversed (org-notes-order-reversed-p))
  10732. (save-excursion
  10733. (save-restriction
  10734. (widen)
  10735. (if pos
  10736. (progn
  10737. (goto-char pos)
  10738. (looking-at org-outline-regexp)
  10739. (setq level (org-get-valid-level (funcall outline-level) 1))
  10740. (goto-char
  10741. (if reversed
  10742. (or (outline-next-heading) (point-max))
  10743. (or (save-excursion (org-get-next-sibling))
  10744. (org-end-of-subtree t t)
  10745. (point-max)))))
  10746. (setq level 1)
  10747. (if (not reversed)
  10748. (goto-char (point-max))
  10749. (goto-char (point-min))
  10750. (or (outline-next-heading) (goto-char (point-max)))))
  10751. (if (not (bolp)) (newline))
  10752. (org-paste-subtree level nil nil t)
  10753. (when org-log-refile
  10754. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10755. (unless (eq org-log-refile 'note)
  10756. (save-excursion (org-add-log-note))))
  10757. (and org-auto-align-tags
  10758. (let ((org-loop-over-headlines-in-active-region nil))
  10759. (org-set-tags nil t)))
  10760. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10761. :last-refile)))
  10762. (when bookmark-name
  10763. (with-demoted-errors
  10764. (bookmark-set bookmark-name))))
  10765. ;; If we are refiling for capture, make sure that the
  10766. ;; last-capture pointers point here
  10767. (when (org-bound-and-true-p org-refile-for-capture)
  10768. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10769. :last-capture-marker)))
  10770. (when bookmark-name
  10771. (with-demoted-errors
  10772. (bookmark-set bookmark-name))))
  10773. (move-marker org-capture-last-stored-marker (point)))
  10774. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10775. (run-hooks 'org-after-refile-insert-hook))))
  10776. (unless org-refile-keep
  10777. (if regionp
  10778. (delete-region (point) (+ (point) (- region-end region-start)))
  10779. (delete-region
  10780. (and (org-back-to-heading t) (point))
  10781. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10782. (when (featurep 'org-inlinetask)
  10783. (org-inlinetask-remove-END-maybe))
  10784. (setq org-markers-to-move nil)
  10785. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10786. (defun org-refile-goto-last-stored ()
  10787. "Go to the location where the last refile was stored."
  10788. (interactive)
  10789. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10790. (message "This is the location of the last refile"))
  10791. (defun org-refile--get-location (refloc tbl)
  10792. "When user refile to REFLOC, find the associated target in TBL.
  10793. Also check `org-refile-target-table'."
  10794. (car (delq
  10795. nil
  10796. (mapcar
  10797. (lambda (r) (or (assoc r tbl)
  10798. (assoc r org-refile-target-table)))
  10799. (list (replace-regexp-in-string "/$" "" refloc)
  10800. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10801. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10802. no-exclude)
  10803. "Prompt the user for a refile location, using PROMPT.
  10804. PROMPT should not be suffixed with a colon and a space, because
  10805. this function appends the default value from
  10806. `org-refile-history' automatically, if that is not empty.
  10807. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10808. this is used for the GOTO interface."
  10809. (let ((org-refile-targets org-refile-targets)
  10810. (org-refile-use-outline-path org-refile-use-outline-path)
  10811. excluded-entries)
  10812. (when (and (derived-mode-p 'org-mode)
  10813. (not org-refile-use-cache)
  10814. (not no-exclude))
  10815. (org-map-tree
  10816. (lambda()
  10817. (setq excluded-entries
  10818. (append excluded-entries (list (org-get-heading t t)))))))
  10819. (setq org-refile-target-table
  10820. (org-refile-get-targets default-buffer excluded-entries)))
  10821. (unless org-refile-target-table
  10822. (user-error "No refile targets"))
  10823. (let* ((cbuf (current-buffer))
  10824. (partial-completion-mode nil)
  10825. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10826. (cfunc (if (and org-refile-use-outline-path
  10827. org-outline-path-complete-in-steps)
  10828. 'org-olpath-completing-read
  10829. 'org-icompleting-read))
  10830. (extra (if org-refile-use-outline-path "/" ""))
  10831. (cbnex (concat (buffer-name) extra))
  10832. (filename (and cfn (expand-file-name cfn)))
  10833. (tbl (mapcar
  10834. (lambda (x)
  10835. (if (and (not (member org-refile-use-outline-path
  10836. '(file full-file-path)))
  10837. (not (equal filename (nth 1 x))))
  10838. (cons (concat (car x) extra " ("
  10839. (file-name-nondirectory (nth 1 x)) ")")
  10840. (cdr x))
  10841. (cons (concat (car x) extra) (cdr x))))
  10842. org-refile-target-table))
  10843. (completion-ignore-case t)
  10844. cdef
  10845. (prompt (concat prompt
  10846. (or (and (car org-refile-history)
  10847. (concat " (default " (car org-refile-history) ")"))
  10848. (and (assoc cbnex tbl) (setq cdef cbnex)
  10849. (concat " (default " cbnex ")"))) ": "))
  10850. pa answ parent-target child parent old-hist)
  10851. (setq old-hist org-refile-history)
  10852. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10853. nil 'org-refile-history (or cdef (car org-refile-history))))
  10854. (if (setq pa (org-refile--get-location answ tbl))
  10855. (progn
  10856. (org-refile-check-position pa)
  10857. (when (or (not org-refile-history)
  10858. (not (eq old-hist org-refile-history))
  10859. (not (equal (car pa) (car org-refile-history))))
  10860. (setq org-refile-history
  10861. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10862. org-refile-history
  10863. (cdr org-refile-history))))
  10864. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10865. (pop org-refile-history)))
  10866. pa)
  10867. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10868. (progn
  10869. (setq parent (match-string 1 answ)
  10870. child (match-string 2 answ))
  10871. (setq parent-target (org-refile--get-location parent tbl))
  10872. (when (and parent-target
  10873. (or (eq new-nodes t)
  10874. (and (eq new-nodes 'confirm)
  10875. (y-or-n-p (format "Create new node \"%s\"? "
  10876. child)))))
  10877. (org-refile-new-child parent-target child)))
  10878. (user-error "Invalid target location")))))
  10879. (declare-function org-string-nw-p "org-macs" (s))
  10880. (defun org-refile-check-position (refile-pointer)
  10881. "Check if the refile pointer matches the headline to which it points."
  10882. (let* ((file (nth 1 refile-pointer))
  10883. (re (nth 2 refile-pointer))
  10884. (pos (nth 3 refile-pointer))
  10885. buffer)
  10886. (if (and (not (markerp pos)) (not file))
  10887. (user-error "Please indicate a target file in the refile path")
  10888. (when (org-string-nw-p re)
  10889. (setq buffer (if (markerp pos)
  10890. (marker-buffer pos)
  10891. (or (find-buffer-visiting file)
  10892. (find-file-noselect file))))
  10893. (with-current-buffer buffer
  10894. (save-excursion
  10895. (save-restriction
  10896. (widen)
  10897. (goto-char pos)
  10898. (beginning-of-line 1)
  10899. (unless (org-looking-at-p re)
  10900. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10901. (defun org-refile-new-child (parent-target child)
  10902. "Use refile target PARENT-TARGET to add new CHILD below it."
  10903. (unless parent-target
  10904. (error "Cannot find parent for new node"))
  10905. (let ((file (nth 1 parent-target))
  10906. (pos (nth 3 parent-target))
  10907. level)
  10908. (with-current-buffer (or (find-buffer-visiting file)
  10909. (find-file-noselect file))
  10910. (save-excursion
  10911. (save-restriction
  10912. (widen)
  10913. (if pos
  10914. (goto-char pos)
  10915. (goto-char (point-max))
  10916. (if (not (bolp)) (newline)))
  10917. (when (looking-at org-outline-regexp)
  10918. (setq level (funcall outline-level))
  10919. (org-end-of-subtree t t))
  10920. (org-back-over-empty-lines)
  10921. (insert "\n" (make-string
  10922. (if pos (org-get-valid-level level 1) 1) ?*)
  10923. " " child "\n")
  10924. (beginning-of-line 0)
  10925. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10926. (defun org-olpath-completing-read (prompt collection &rest args)
  10927. "Read an outline path like a file name."
  10928. (let ((thetable collection)
  10929. (org-completion-use-ido nil) ; does not work with ido.
  10930. (org-completion-use-iswitchb nil)) ; or iswitchb
  10931. (apply
  10932. 'org-icompleting-read prompt
  10933. (lambda (string predicate &optional flag)
  10934. (let (rtn r f (l (length string)))
  10935. (cond
  10936. ((eq flag nil)
  10937. ;; try completion
  10938. (try-completion string thetable))
  10939. ((eq flag t)
  10940. ;; all-completions
  10941. (setq rtn (all-completions string thetable predicate))
  10942. (mapcar
  10943. (lambda (x)
  10944. (setq r (substring x l))
  10945. (if (string-match " ([^)]*)$" x)
  10946. (setq f (match-string 0 x))
  10947. (setq f ""))
  10948. (if (string-match "/" r)
  10949. (concat string (substring r 0 (match-end 0)) f)
  10950. x))
  10951. rtn))
  10952. ((eq flag 'lambda)
  10953. ;; exact match?
  10954. (assoc string thetable)))))
  10955. args)))
  10956. ;;;; Dynamic blocks
  10957. (defun org-find-dblock (name)
  10958. "Find the first dynamic block with name NAME in the buffer.
  10959. If not found, stay at current position and return nil."
  10960. (let ((case-fold-search t) pos)
  10961. (save-excursion
  10962. (goto-char (point-min))
  10963. (setq pos (and (re-search-forward
  10964. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10965. (match-beginning 0))))
  10966. (if pos (goto-char pos))
  10967. pos))
  10968. (defun org-create-dblock (plist)
  10969. "Create a dynamic block section, with parameters taken from PLIST.
  10970. PLIST must contain a :name entry which is used as the name of the block."
  10971. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10972. (end-of-line 1)
  10973. (newline))
  10974. (let ((col (current-column))
  10975. (name (plist-get plist :name)))
  10976. (insert "#+BEGIN: " name)
  10977. (while plist
  10978. (if (eq (car plist) :name)
  10979. (setq plist (cddr plist))
  10980. (insert " " (prin1-to-string (pop plist)))))
  10981. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10982. (beginning-of-line -2)))
  10983. (defun org-prepare-dblock ()
  10984. "Prepare dynamic block for refresh.
  10985. This empties the block, puts the cursor at the insert position and returns
  10986. the property list including an extra property :name with the block name."
  10987. (unless (looking-at org-dblock-start-re)
  10988. (user-error "Not at a dynamic block"))
  10989. (let* ((begdel (1+ (match-end 0)))
  10990. (name (org-no-properties (match-string 1)))
  10991. (params (append (list :name name)
  10992. (read (concat "(" (match-string 3) ")")))))
  10993. (save-excursion
  10994. (beginning-of-line 1)
  10995. (skip-chars-forward " \t")
  10996. (setq params (plist-put params :indentation-column (current-column))))
  10997. (unless (re-search-forward org-dblock-end-re nil t)
  10998. (error "Dynamic block not terminated"))
  10999. (setq params
  11000. (append params
  11001. (list :content (buffer-substring
  11002. begdel (match-beginning 0)))))
  11003. (delete-region begdel (match-beginning 0))
  11004. (goto-char begdel)
  11005. (open-line 1)
  11006. params))
  11007. (defun org-map-dblocks (&optional command)
  11008. "Apply COMMAND to all dynamic blocks in the current buffer.
  11009. If COMMAND is not given, use `org-update-dblock'."
  11010. (let ((cmd (or command 'org-update-dblock)))
  11011. (save-excursion
  11012. (goto-char (point-min))
  11013. (while (re-search-forward org-dblock-start-re nil t)
  11014. (goto-char (match-beginning 0))
  11015. (save-excursion
  11016. (condition-case nil
  11017. (funcall cmd)
  11018. (error (message "Error during update of dynamic block"))))
  11019. (unless (re-search-forward org-dblock-end-re nil t)
  11020. (error "Dynamic block not terminated"))))))
  11021. (defun org-dblock-update (&optional arg)
  11022. "User command for updating dynamic blocks.
  11023. Update the dynamic block at point. With prefix ARG, update all dynamic
  11024. blocks in the buffer."
  11025. (interactive "P")
  11026. (if arg
  11027. (org-update-all-dblocks)
  11028. (or (looking-at org-dblock-start-re)
  11029. (org-beginning-of-dblock))
  11030. (org-update-dblock)))
  11031. (defun org-update-dblock ()
  11032. "Update the dynamic block at point.
  11033. This means to empty the block, parse for parameters and then call
  11034. the correct writing function."
  11035. (interactive)
  11036. (save-excursion
  11037. (let* ((win (selected-window))
  11038. (pos (point))
  11039. (line (org-current-line))
  11040. (params (org-prepare-dblock))
  11041. (name (plist-get params :name))
  11042. (indent (plist-get params :indentation-column))
  11043. (cmd (intern (concat "org-dblock-write:" name))))
  11044. (message "Updating dynamic block `%s' at line %d..." name line)
  11045. (funcall cmd params)
  11046. (message "Updating dynamic block `%s' at line %d...done" name line)
  11047. (goto-char pos)
  11048. (when (and indent (> indent 0))
  11049. (setq indent (make-string indent ?\ ))
  11050. (save-excursion
  11051. (select-window win)
  11052. (org-beginning-of-dblock)
  11053. (forward-line 1)
  11054. (while (not (looking-at org-dblock-end-re))
  11055. (insert indent)
  11056. (beginning-of-line 2))
  11057. (when (looking-at org-dblock-end-re)
  11058. (and (looking-at "[ \t]+")
  11059. (replace-match ""))
  11060. (insert indent)))))))
  11061. (defun org-beginning-of-dblock ()
  11062. "Find the beginning of the dynamic block at point.
  11063. Error if there is no such block at point."
  11064. (let ((pos (point))
  11065. beg)
  11066. (end-of-line 1)
  11067. (if (and (re-search-backward org-dblock-start-re nil t)
  11068. (setq beg (match-beginning 0))
  11069. (re-search-forward org-dblock-end-re nil t)
  11070. (> (match-end 0) pos))
  11071. (goto-char beg)
  11072. (goto-char pos)
  11073. (error "Not in a dynamic block"))))
  11074. (defun org-update-all-dblocks ()
  11075. "Update all dynamic blocks in the buffer.
  11076. This function can be used in a hook."
  11077. (interactive)
  11078. (when (derived-mode-p 'org-mode)
  11079. (org-map-dblocks 'org-update-dblock)))
  11080. ;;;; Completion
  11081. (declare-function org-export-backend-name "org-export" (cl-x))
  11082. (declare-function org-export-backend-options "org-export" (cl-x))
  11083. (defun org-get-export-keywords ()
  11084. "Return a list of all currently understood export keywords.
  11085. Export keywords include options, block names, attributes and
  11086. keywords relative to each registered export back-end."
  11087. (let (keywords)
  11088. (dolist (backend
  11089. (org-bound-and-true-p org-export--registered-backends)
  11090. (delq nil keywords))
  11091. ;; Back-end name (for keywords, like #+LATEX:)
  11092. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11093. (dolist (option-entry (org-export-backend-options backend))
  11094. ;; Back-end options.
  11095. (push (nth 1 option-entry) keywords)))))
  11096. (defconst org-options-keywords
  11097. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11098. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11099. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11100. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11101. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11102. (defcustom org-structure-template-alist
  11103. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11104. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11105. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11106. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11107. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11108. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11109. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX")
  11110. ("L" "#+LaTeX: ")
  11111. ("h" "#+BEGIN_HTML\n?\n#+END_HTML")
  11112. ("H" "#+HTML: ")
  11113. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  11114. ("A" "#+ASCII: ")
  11115. ("i" "#+INDEX: ?")
  11116. ("I" "#+INCLUDE: %file ?"))
  11117. "Structure completion elements.
  11118. This is a list of abbreviation keys and values. The value gets inserted
  11119. if you type `<' followed by the key and then press the completion key,
  11120. usually `TAB'. %file will be replaced by a file name after prompting
  11121. for the file using completion. The cursor will be placed at the position
  11122. of the `?` in the template.
  11123. There are two templates for each key, the first uses the original Org syntax,
  11124. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11125. the default when the /org-mtags.el/ module has been loaded. See also the
  11126. variable `org-mtags-prefer-muse-templates'."
  11127. :group 'org-completion
  11128. :type '(repeat
  11129. (list
  11130. (string :tag "Key")
  11131. (string :tag "Template")))
  11132. :version 24.4
  11133. :package-version '(Org . "8.3"))
  11134. (defun org-try-structure-completion ()
  11135. "Try to complete a structure template before point.
  11136. This looks for strings like \"<e\" on an otherwise empty line and
  11137. expands them."
  11138. (let ((l (buffer-substring (point-at-bol) (point)))
  11139. a)
  11140. (when (and (looking-at "[ \t]*$")
  11141. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11142. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11143. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11144. (match-beginning 1)) a)
  11145. t)))
  11146. (defun org-complete-expand-structure-template (start cell)
  11147. "Expand a structure template."
  11148. (let ((rpl (nth 1 cell))
  11149. (ind ""))
  11150. (delete-region start (point))
  11151. (when (string-match "\\`[ \t]*#\\+" rpl)
  11152. (cond
  11153. ((bolp))
  11154. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11155. (setq ind (buffer-substring (point-at-bol) (point))))
  11156. (t (newline))))
  11157. (setq start (point))
  11158. (if (string-match "%file" rpl)
  11159. (setq rpl (replace-match
  11160. (concat
  11161. "\""
  11162. (save-match-data
  11163. (abbreviate-file-name (read-file-name "Include file: ")))
  11164. "\"")
  11165. t t rpl)))
  11166. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11167. (concat "\n" ind)))
  11168. (insert rpl)
  11169. (if (re-search-backward "\\?" start t) (delete-char 1))))
  11170. ;;;; TODO, DEADLINE, Comments
  11171. (defun org-toggle-comment ()
  11172. "Change the COMMENT state of an entry."
  11173. (interactive)
  11174. (save-excursion
  11175. (org-back-to-heading)
  11176. (looking-at org-complex-heading-regexp)
  11177. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11178. (skip-chars-forward " \t")
  11179. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11180. (if (org-in-commented-heading-p t)
  11181. (delete-region (point)
  11182. (progn (search-forward " " (line-end-position) 'move)
  11183. (skip-chars-forward " \t")
  11184. (point)))
  11185. (insert org-comment-string)
  11186. (unless (eolp) (insert " ")))))
  11187. (defvar org-last-todo-state-is-todo nil
  11188. "This is non-nil when the last TODO state change led to a TODO state.
  11189. If the last change removed the TODO tag or switched to DONE, then
  11190. this is nil.")
  11191. (defvar org-setting-tags nil) ; dynamically skipped
  11192. (defvar org-todo-setup-filter-hook nil
  11193. "Hook for functions that pre-filter todo specs.
  11194. Each function takes a todo spec and returns either nil or the spec
  11195. transformed into canonical form." )
  11196. (defvar org-todo-get-default-hook nil
  11197. "Hook for functions that get a default item for todo.
  11198. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11199. nil or a string to be used for the todo mark." )
  11200. (defvar org-agenda-headline-snapshot-before-repeat)
  11201. (defun org-current-effective-time ()
  11202. "Return current time adjusted for `org-extend-today-until' variable."
  11203. (let* ((ct (org-current-time))
  11204. (dct (decode-time ct))
  11205. (ct1
  11206. (cond
  11207. (org-use-last-clock-out-time-as-effective-time
  11208. (or (org-clock-get-last-clock-out-time) ct))
  11209. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11210. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11211. (t ct))))
  11212. ct1))
  11213. (defun org-todo-yesterday (&optional arg)
  11214. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11215. (interactive "P")
  11216. (if (eq major-mode 'org-agenda-mode)
  11217. (apply 'org-agenda-todo-yesterday arg)
  11218. (let* ((hour (third (decode-time
  11219. (org-current-time))))
  11220. (org-extend-today-until (1+ hour)))
  11221. (org-todo arg))))
  11222. (defvar org-block-entry-blocking ""
  11223. "First entry preventing the TODO state change.")
  11224. (defun org-cancel-repeater ()
  11225. "Cancel a repeater by setting its numeric value to zero."
  11226. (interactive)
  11227. (save-excursion
  11228. (org-back-to-heading t)
  11229. (let ((bound1 (point))
  11230. (bound0 (save-excursion (outline-next-heading) (point))))
  11231. (when (re-search-forward
  11232. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11233. org-deadline-time-regexp "\\)\\|\\("
  11234. org-ts-regexp "\\)")
  11235. bound0 t)
  11236. (if (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]" bound1 t)
  11237. (replace-match "0" t nil nil 1))))))
  11238. (defun org-todo (&optional arg)
  11239. "Change the TODO state of an item.
  11240. The state of an item is given by a keyword at the start of the heading,
  11241. like
  11242. *** TODO Write paper
  11243. *** DONE Call mom
  11244. The different keywords are specified in the variable `org-todo-keywords'.
  11245. By default the available states are \"TODO\" and \"DONE\".
  11246. So for this example: when the item starts with TODO, it is changed to DONE.
  11247. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11248. DONE are present, add TODO at the beginning of the heading.
  11249. With \\[universal-argument] prefix arg, use completion to determine the new \
  11250. state.
  11251. With numeric prefix arg, switch to that state.
  11252. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11253. keywords (nextset).
  11254. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11255. With a numeric prefix arg of 0, inhibit note taking for the change.
  11256. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11257. When called through ELisp, arg is also interpreted in the following way:
  11258. 'none -> empty state
  11259. \"\"(empty string) -> switch to empty state
  11260. 'done -> switch to DONE
  11261. 'nextset -> switch to the next set of keywords
  11262. 'previousset -> switch to the previous set of keywords
  11263. \"WAITING\" -> switch to the specified keyword, but only if it
  11264. really is a member of `org-todo-keywords'."
  11265. (interactive "P")
  11266. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11267. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11268. 'region-start-level 'region))
  11269. org-loop-over-headlines-in-active-region)
  11270. (org-map-entries
  11271. `(org-todo ,arg)
  11272. org-loop-over-headlines-in-active-region
  11273. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11274. (if (equal arg '(16)) (setq arg 'nextset))
  11275. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11276. (let ((org-blocker-hook org-blocker-hook)
  11277. commentp
  11278. case-fold-search)
  11279. (when (equal arg '(64))
  11280. (setq arg nil org-blocker-hook nil))
  11281. (when (and org-blocker-hook
  11282. (or org-inhibit-blocking
  11283. (org-entry-get nil "NOBLOCKING")))
  11284. (setq org-blocker-hook nil))
  11285. (save-excursion
  11286. (catch 'exit
  11287. (org-back-to-heading t)
  11288. (when (org-in-commented-heading-p t)
  11289. (org-toggle-comment)
  11290. (setq commentp t))
  11291. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11292. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11293. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11294. (let* ((match-data (match-data))
  11295. (startpos (point-at-bol))
  11296. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11297. (org-log-done org-log-done)
  11298. (org-log-repeat org-log-repeat)
  11299. (org-todo-log-states org-todo-log-states)
  11300. (org-inhibit-logging
  11301. (if (equal arg 0)
  11302. (progn (setq arg nil) 'note) org-inhibit-logging))
  11303. (this (match-string 1))
  11304. (hl-pos (match-beginning 0))
  11305. (head (org-get-todo-sequence-head this))
  11306. (ass (assoc head org-todo-kwd-alist))
  11307. (interpret (nth 1 ass))
  11308. (done-word (nth 3 ass))
  11309. (final-done-word (nth 4 ass))
  11310. (org-last-state (or this ""))
  11311. (completion-ignore-case t)
  11312. (member (member this org-todo-keywords-1))
  11313. (tail (cdr member))
  11314. (org-state (cond
  11315. ((and org-todo-key-trigger
  11316. (or (and (equal arg '(4))
  11317. (eq org-use-fast-todo-selection 'prefix))
  11318. (and (not arg) org-use-fast-todo-selection
  11319. (not (eq org-use-fast-todo-selection
  11320. 'prefix)))))
  11321. ;; Use fast selection
  11322. (org-fast-todo-selection))
  11323. ((and (equal arg '(4))
  11324. (or (not org-use-fast-todo-selection)
  11325. (not org-todo-key-trigger)))
  11326. ;; Read a state with completion
  11327. (org-icompleting-read
  11328. "State: " (mapcar 'list org-todo-keywords-1)
  11329. nil t))
  11330. ((eq arg 'right)
  11331. (if this
  11332. (if tail (car tail) nil)
  11333. (car org-todo-keywords-1)))
  11334. ((eq arg 'left)
  11335. (if (equal member org-todo-keywords-1)
  11336. nil
  11337. (if this
  11338. (nth (- (length org-todo-keywords-1)
  11339. (length tail) 2)
  11340. org-todo-keywords-1)
  11341. (org-last org-todo-keywords-1))))
  11342. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11343. (setq arg nil))) ; hack to fall back to cycling
  11344. (arg
  11345. ;; user or caller requests a specific state
  11346. (cond
  11347. ((equal arg "") nil)
  11348. ((eq arg 'none) nil)
  11349. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11350. ((eq arg 'nextset)
  11351. (or (car (cdr (member head org-todo-heads)))
  11352. (car org-todo-heads)))
  11353. ((eq arg 'previousset)
  11354. (let ((org-todo-heads (reverse org-todo-heads)))
  11355. (or (car (cdr (member head org-todo-heads)))
  11356. (car org-todo-heads))))
  11357. ((car (member arg org-todo-keywords-1)))
  11358. ((stringp arg)
  11359. (user-error "State `%s' not valid in this file" arg))
  11360. ((nth (1- (prefix-numeric-value arg))
  11361. org-todo-keywords-1))))
  11362. ((null member) (or head (car org-todo-keywords-1)))
  11363. ((equal this final-done-word) nil) ;; -> make empty
  11364. ((null tail) nil) ;; -> first entry
  11365. ((memq interpret '(type priority))
  11366. (if (eq this-command last-command)
  11367. (car tail)
  11368. (if (> (length tail) 0)
  11369. (or done-word (car org-done-keywords))
  11370. nil)))
  11371. (t
  11372. (car tail))))
  11373. (org-state (or
  11374. (run-hook-with-args-until-success
  11375. 'org-todo-get-default-hook org-state org-last-state)
  11376. org-state))
  11377. (next (if org-state (concat " " org-state " ") " "))
  11378. (change-plist (list :type 'todo-state-change :from this :to org-state
  11379. :position startpos))
  11380. dolog now-done-p)
  11381. (when org-blocker-hook
  11382. (setq org-last-todo-state-is-todo
  11383. (not (member this org-done-keywords)))
  11384. (unless (save-excursion
  11385. (save-match-data
  11386. (org-with-wide-buffer
  11387. (run-hook-with-args-until-failure
  11388. 'org-blocker-hook change-plist))))
  11389. (if (org-called-interactively-p 'interactive)
  11390. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11391. this org-state org-block-entry-blocking)
  11392. ;; fail silently
  11393. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11394. this org-state org-block-entry-blocking)
  11395. (throw 'exit nil))))
  11396. (store-match-data match-data)
  11397. (replace-match next t t)
  11398. (cond ((equal this org-state)
  11399. (message "TODO state was already %s" (org-trim next)))
  11400. ((pos-visible-in-window-p hl-pos)
  11401. (message "TODO state changed to %s" (org-trim next))))
  11402. (unless head
  11403. (setq head (org-get-todo-sequence-head org-state)
  11404. ass (assoc head org-todo-kwd-alist)
  11405. interpret (nth 1 ass)
  11406. done-word (nth 3 ass)
  11407. final-done-word (nth 4 ass)))
  11408. (when (memq arg '(nextset previousset))
  11409. (message "Keyword-Set %d/%d: %s"
  11410. (- (length org-todo-sets) -1
  11411. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11412. (length org-todo-sets)
  11413. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11414. (setq org-last-todo-state-is-todo
  11415. (not (member org-state org-done-keywords)))
  11416. (setq now-done-p (and (member org-state org-done-keywords)
  11417. (not (member this org-done-keywords))))
  11418. (and logging (org-local-logging logging))
  11419. (when (and (or org-todo-log-states org-log-done)
  11420. (not (eq org-inhibit-logging t))
  11421. (not (memq arg '(nextset previousset))))
  11422. ;; we need to look at recording a time and note
  11423. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11424. (nth 2 (assoc this org-todo-log-states))))
  11425. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11426. (setq dolog 'time))
  11427. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11428. (and org-state
  11429. (member org-state org-not-done-keywords)
  11430. (not (member this org-not-done-keywords))))
  11431. ;; This is now a todo state and was not one before
  11432. ;; If there was a CLOSED time stamp, get rid of it.
  11433. (org-add-planning-info nil nil 'closed))
  11434. (when (and now-done-p org-log-done)
  11435. ;; It is now done, and it was not done before
  11436. (org-add-planning-info 'closed (org-current-effective-time))
  11437. (if (and (not dolog) (eq 'note org-log-done))
  11438. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11439. (when (and org-state dolog)
  11440. ;; This is a non-nil state, and we need to log it
  11441. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11442. ;; Fixup tag positioning
  11443. (org-todo-trigger-tag-changes org-state)
  11444. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11445. (when org-provide-todo-statistics
  11446. (org-update-parent-todo-statistics))
  11447. (run-hooks 'org-after-todo-state-change-hook)
  11448. (if (and arg (not (member org-state org-done-keywords)))
  11449. (setq head (org-get-todo-sequence-head org-state)))
  11450. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11451. ;; Do we need to trigger a repeat?
  11452. (when now-done-p
  11453. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11454. ;; This is for the agenda, take a snapshot of the headline.
  11455. (save-match-data
  11456. (setq org-agenda-headline-snapshot-before-repeat
  11457. (org-get-heading))))
  11458. (org-auto-repeat-maybe org-state))
  11459. ;; Fixup cursor location if close to the keyword
  11460. (if (and (outline-on-heading-p)
  11461. (not (bolp))
  11462. (save-excursion (beginning-of-line 1)
  11463. (looking-at org-todo-line-regexp))
  11464. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11465. (progn
  11466. (goto-char (or (match-end 2) (match-end 1)))
  11467. (and (looking-at " ") (just-one-space))))
  11468. (when org-trigger-hook
  11469. (save-excursion
  11470. (run-hook-with-args 'org-trigger-hook change-plist)))
  11471. (when commentp (org-toggle-comment))))))))
  11472. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11473. "Block turning an entry into a TODO, using the hierarchy.
  11474. This checks whether the current task should be blocked from state
  11475. changes. Such blocking occurs when:
  11476. 1. The task has children which are not all in a completed state.
  11477. 2. A task has a parent with the property :ORDERED:, and there
  11478. are siblings prior to the current task with incomplete
  11479. status.
  11480. 3. The parent of the task is blocked because it has siblings that should
  11481. be done first, or is child of a block grandparent TODO entry."
  11482. (if (not org-enforce-todo-dependencies)
  11483. t ; if locally turned off don't block
  11484. (catch 'dont-block
  11485. ;; If this is not a todo state change, or if this entry is already DONE,
  11486. ;; do not block
  11487. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11488. (member (plist-get change-plist :from)
  11489. (cons 'done org-done-keywords))
  11490. (member (plist-get change-plist :to)
  11491. (cons 'todo org-not-done-keywords))
  11492. (not (plist-get change-plist :to)))
  11493. (throw 'dont-block t))
  11494. ;; If this task has children, and any are undone, it's blocked
  11495. (save-excursion
  11496. (org-back-to-heading t)
  11497. (let ((this-level (funcall outline-level)))
  11498. (outline-next-heading)
  11499. (let ((child-level (funcall outline-level)))
  11500. (while (and (not (eobp))
  11501. (> child-level this-level))
  11502. ;; this todo has children, check whether they are all
  11503. ;; completed
  11504. (if (and (not (org-entry-is-done-p))
  11505. (org-entry-is-todo-p))
  11506. (progn (setq org-block-entry-blocking (org-get-heading))
  11507. (throw 'dont-block nil)))
  11508. (outline-next-heading)
  11509. (setq child-level (funcall outline-level))))))
  11510. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11511. ;; any previous siblings are undone, it's blocked
  11512. (save-excursion
  11513. (org-back-to-heading t)
  11514. (let* ((pos (point))
  11515. (parent-pos (and (org-up-heading-safe) (point))))
  11516. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11517. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11518. (forward-line 1)
  11519. (re-search-forward org-not-done-heading-regexp pos t))
  11520. (setq org-block-entry-blocking (match-string 0))
  11521. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11522. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11523. (while t
  11524. (goto-char parent-pos)
  11525. (if (not (looking-at org-not-done-heading-regexp))
  11526. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11527. (setq pos (point))
  11528. (setq parent-pos (and (org-up-heading-safe) (point)))
  11529. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11530. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11531. (forward-line 1)
  11532. (re-search-forward org-not-done-heading-regexp pos t)
  11533. (setq org-block-entry-blocking (org-get-heading)))
  11534. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11535. (defcustom org-track-ordered-property-with-tag nil
  11536. "Should the ORDERED property also be shown as a tag?
  11537. The ORDERED property decides if an entry should require subtasks to be
  11538. completed in sequence. Since a property is not very visible, setting
  11539. this option means that toggling the ORDERED property with the command
  11540. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11541. not relevant for the behavior, but it makes things more visible.
  11542. Note that toggling the tag with tags commands will not change the property
  11543. and therefore not influence behavior!
  11544. This can be t, meaning the tag ORDERED should be used, It can also be a
  11545. string to select a different tag for this task."
  11546. :group 'org-todo
  11547. :type '(choice
  11548. (const :tag "No tracking" nil)
  11549. (const :tag "Track with ORDERED tag" t)
  11550. (string :tag "Use other tag")))
  11551. (defun org-toggle-ordered-property ()
  11552. "Toggle the ORDERED property of the current entry.
  11553. For better visibility, you can track the value of this property with a tag.
  11554. See variable `org-track-ordered-property-with-tag'."
  11555. (interactive)
  11556. (let* ((t1 org-track-ordered-property-with-tag)
  11557. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11558. (save-excursion
  11559. (org-back-to-heading)
  11560. (if (org-entry-get nil "ORDERED")
  11561. (progn
  11562. (org-delete-property "ORDERED")
  11563. (and tag (org-toggle-tag tag 'off))
  11564. (message "Subtasks can be completed in arbitrary order"))
  11565. (org-entry-put nil "ORDERED" "t")
  11566. (and tag (org-toggle-tag tag 'on))
  11567. (message "Subtasks must be completed in sequence")))))
  11568. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11569. (defun org-block-todo-from-checkboxes (change-plist)
  11570. "Block turning an entry into a TODO, using checkboxes.
  11571. This checks whether the current task should be blocked from state
  11572. changes because there are unchecked boxes in this entry."
  11573. (if (not org-enforce-todo-checkbox-dependencies)
  11574. t ; if locally turned off don't block
  11575. (catch 'dont-block
  11576. ;; If this is not a todo state change, or if this entry is already DONE,
  11577. ;; do not block
  11578. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11579. (member (plist-get change-plist :from)
  11580. (cons 'done org-done-keywords))
  11581. (member (plist-get change-plist :to)
  11582. (cons 'todo org-not-done-keywords))
  11583. (not (plist-get change-plist :to)))
  11584. (throw 'dont-block t))
  11585. ;; If this task has checkboxes that are not checked, it's blocked
  11586. (save-excursion
  11587. (org-back-to-heading t)
  11588. (let ((beg (point)) end)
  11589. (outline-next-heading)
  11590. (setq end (point))
  11591. (goto-char beg)
  11592. (if (org-list-search-forward
  11593. (concat (org-item-beginning-re)
  11594. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11595. "\\[[- ]\\]")
  11596. end t)
  11597. (progn
  11598. (if (boundp 'org-blocked-by-checkboxes)
  11599. (setq org-blocked-by-checkboxes t))
  11600. (throw 'dont-block nil)))))
  11601. t))) ; do not block
  11602. (defun org-entry-blocked-p ()
  11603. "Is the current entry blocked?"
  11604. (org-with-silent-modifications
  11605. (if (org-entry-get nil "NOBLOCKING")
  11606. nil ;; Never block this entry
  11607. (not (run-hook-with-args-until-failure
  11608. 'org-blocker-hook
  11609. (list :type 'todo-state-change
  11610. :position (point)
  11611. :from 'todo
  11612. :to 'done))))))
  11613. (defun org-update-statistics-cookies (all)
  11614. "Update the statistics cookie, either from TODO or from checkboxes.
  11615. This should be called with the cursor in a line with a statistics cookie."
  11616. (interactive "P")
  11617. (if all
  11618. (progn
  11619. (org-update-checkbox-count 'all)
  11620. (org-map-entries 'org-update-parent-todo-statistics))
  11621. (if (not (org-at-heading-p))
  11622. (org-update-checkbox-count)
  11623. (let ((pos (point-marker))
  11624. end l1 l2)
  11625. (ignore-errors (org-back-to-heading t))
  11626. (if (not (org-at-heading-p))
  11627. (org-update-checkbox-count)
  11628. (setq l1 (org-outline-level))
  11629. (setq end (save-excursion
  11630. (outline-next-heading)
  11631. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11632. (point)))
  11633. (if (and (save-excursion
  11634. (re-search-forward
  11635. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11636. (not (save-excursion (re-search-forward
  11637. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11638. (org-update-checkbox-count)
  11639. (if (and l2 (> l2 l1))
  11640. (progn
  11641. (goto-char end)
  11642. (org-update-parent-todo-statistics))
  11643. (goto-char pos)
  11644. (beginning-of-line 1)
  11645. (while (re-search-forward
  11646. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11647. (point-at-eol) t)
  11648. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11649. (goto-char pos)
  11650. (move-marker pos nil)))))
  11651. (defvar org-entry-property-inherited-from) ;; defined below
  11652. (defun org-update-parent-todo-statistics ()
  11653. "Update any statistics cookie in the parent of the current headline.
  11654. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11655. the entire subtree and this will travel up the hierarchy and update
  11656. statistics everywhere."
  11657. (let* ((prop (save-excursion (org-up-heading-safe)
  11658. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11659. (recursive (or (not org-hierarchical-todo-statistics)
  11660. (and prop (string-match "\\<recursive\\>" prop))))
  11661. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11662. 0))
  11663. (first t)
  11664. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11665. level ltoggle l1 new ndel
  11666. (cnt-all 0) (cnt-done 0) is-percent kwd
  11667. checkbox-beg ov ovs ove cookie-present)
  11668. (catch 'exit
  11669. (save-excursion
  11670. (beginning-of-line 1)
  11671. (setq ltoggle (funcall outline-level))
  11672. ;; Three situations are to consider:
  11673. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11674. ;; to the top-level ancestor on the headline;
  11675. ;; 2. If parent has "recursive" property, repeat up to the
  11676. ;; headline setting that property, taking inheritance into
  11677. ;; account;
  11678. ;; 3. Else, move up to direct parent and proceed only once.
  11679. (while (and (setq level (org-up-heading-safe))
  11680. (or recursive first)
  11681. (>= (point) lim))
  11682. (setq first nil cookie-present nil)
  11683. (unless (and level
  11684. (not (string-match
  11685. "\\<checkbox\\>"
  11686. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11687. "")))))
  11688. (throw 'exit nil))
  11689. (while (re-search-forward box-re (point-at-eol) t)
  11690. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11691. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11692. (save-match-data
  11693. (unless (outline-next-heading) (throw 'exit nil))
  11694. (while (and (looking-at org-complex-heading-regexp)
  11695. (> (setq l1 (length (match-string 1))) level))
  11696. (setq kwd (and (or recursive (= l1 ltoggle))
  11697. (match-string 2)))
  11698. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11699. (and (eq org-provide-todo-statistics t)
  11700. (or (member kwd org-done-keywords)))
  11701. (and (listp org-provide-todo-statistics)
  11702. (stringp (car org-provide-todo-statistics))
  11703. (or (member kwd org-provide-todo-statistics)
  11704. (member kwd org-done-keywords)))
  11705. (and (listp org-provide-todo-statistics)
  11706. (listp (car org-provide-todo-statistics))
  11707. (or (member kwd (car org-provide-todo-statistics))
  11708. (and (member kwd org-done-keywords)
  11709. (member kwd (cadr org-provide-todo-statistics))))))
  11710. (setq cnt-all (1+ cnt-all))
  11711. (if (eq org-provide-todo-statistics t)
  11712. (and kwd (setq cnt-all (1+ cnt-all)))))
  11713. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11714. (member kwd org-done-keywords))
  11715. (and (listp org-provide-todo-statistics)
  11716. (listp (car org-provide-todo-statistics))
  11717. (member kwd org-done-keywords)
  11718. (member kwd (cadr org-provide-todo-statistics)))
  11719. (and (listp org-provide-todo-statistics)
  11720. (stringp (car org-provide-todo-statistics))
  11721. (member kwd org-done-keywords)))
  11722. (setq cnt-done (1+ cnt-done)))
  11723. (outline-next-heading)))
  11724. (setq new
  11725. (if is-percent
  11726. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11727. (format "[%d/%d]" cnt-done cnt-all))
  11728. ndel (- (match-end 0) checkbox-beg))
  11729. ;; handle overlays when updating cookie from column view
  11730. (when (setq ov (car (overlays-at checkbox-beg)))
  11731. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11732. (delete-overlay ov))
  11733. (goto-char checkbox-beg)
  11734. (insert new)
  11735. (delete-region (point) (+ (point) ndel))
  11736. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11737. (when ov (move-overlay ov ovs ove)))
  11738. (when cookie-present
  11739. (run-hook-with-args 'org-after-todo-statistics-hook
  11740. cnt-done (- cnt-all cnt-done))))))
  11741. (run-hooks 'org-todo-statistics-hook)))
  11742. (defvar org-after-todo-statistics-hook nil
  11743. "Hook that is called after a TODO statistics cookie has been updated.
  11744. Each function is called with two arguments: the number of not-done entries
  11745. and the number of done entries.
  11746. For example, the following function, when added to this hook, will switch
  11747. an entry to DONE when all children are done, and back to TODO when new
  11748. entries are set to a TODO status. Note that this hook is only called
  11749. when there is a statistics cookie in the headline!
  11750. (defun org-summary-todo (n-done n-not-done)
  11751. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11752. (let (org-log-done org-log-states) ; turn off logging
  11753. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11754. ")
  11755. (defvar org-todo-statistics-hook nil
  11756. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11757. This hook runs even if there is no statistics cookie present, in which case
  11758. `org-after-todo-statistics-hook' would not run.")
  11759. (defun org-todo-trigger-tag-changes (state)
  11760. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11761. (let ((l org-todo-state-tags-triggers)
  11762. changes)
  11763. (when (or (not state) (equal state ""))
  11764. (setq changes (append changes (cdr (assoc "" l)))))
  11765. (when (and (stringp state) (> (length state) 0))
  11766. (setq changes (append changes (cdr (assoc state l)))))
  11767. (when (member state org-not-done-keywords)
  11768. (setq changes (append changes (cdr (assoc 'todo l)))))
  11769. (when (member state org-done-keywords)
  11770. (setq changes (append changes (cdr (assoc 'done l)))))
  11771. (dolist (c changes)
  11772. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11773. (defun org-local-logging (value)
  11774. "Get logging settings from a property VALUE."
  11775. (let* (words w a)
  11776. ;; directly set the variables, they are already local.
  11777. (setq org-log-done nil
  11778. org-log-repeat nil
  11779. org-todo-log-states nil)
  11780. (setq words (org-split-string value))
  11781. (while (setq w (pop words))
  11782. (cond
  11783. ((setq a (assoc w org-startup-options))
  11784. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11785. (set (nth 1 a) (nth 2 a))))
  11786. ((setq a (org-extract-log-state-settings w))
  11787. (and (member (car a) org-todo-keywords-1)
  11788. (push a org-todo-log-states)))))))
  11789. (defun org-get-todo-sequence-head (kwd)
  11790. "Return the head of the TODO sequence to which KWD belongs.
  11791. If KWD is not set, check if there is a text property remembering the
  11792. right sequence."
  11793. (let (p)
  11794. (cond
  11795. ((not kwd)
  11796. (or (get-text-property (point-at-bol) 'org-todo-head)
  11797. (progn
  11798. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11799. nil (point-at-eol)))
  11800. (get-text-property p 'org-todo-head))))
  11801. ((not (member kwd org-todo-keywords-1))
  11802. (car org-todo-keywords-1))
  11803. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11804. (defun org-fast-todo-selection ()
  11805. "Fast TODO keyword selection with single keys.
  11806. Returns the new TODO keyword, or nil if no state change should occur."
  11807. (let* ((fulltable org-todo-key-alist)
  11808. (done-keywords org-done-keywords) ;; needed for the faces.
  11809. (maxlen (apply 'max (mapcar
  11810. (lambda (x)
  11811. (if (stringp (car x)) (string-width (car x)) 0))
  11812. fulltable)))
  11813. (expert nil)
  11814. (fwidth (+ maxlen 3 1 3))
  11815. (ncol (/ (- (window-width) 4) fwidth))
  11816. tg cnt e c tbl
  11817. groups ingroup)
  11818. (save-excursion
  11819. (save-window-excursion
  11820. (if expert
  11821. (set-buffer (get-buffer-create " *Org todo*"))
  11822. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11823. (erase-buffer)
  11824. (org-set-local 'org-done-keywords done-keywords)
  11825. (setq tbl fulltable cnt 0)
  11826. (while (setq e (pop tbl))
  11827. (cond
  11828. ((equal e '(:startgroup))
  11829. (push '() groups) (setq ingroup t)
  11830. (when (not (= cnt 0))
  11831. (setq cnt 0)
  11832. (insert "\n"))
  11833. (insert "{ "))
  11834. ((equal e '(:endgroup))
  11835. (setq ingroup nil cnt 0)
  11836. (insert "}\n"))
  11837. ((equal e '(:newline))
  11838. (when (not (= cnt 0))
  11839. (setq cnt 0)
  11840. (insert "\n")
  11841. (setq e (car tbl))
  11842. (while (equal (car tbl) '(:newline))
  11843. (insert "\n")
  11844. (setq tbl (cdr tbl)))))
  11845. (t
  11846. (setq tg (car e) c (cdr e))
  11847. (if ingroup (push tg (car groups)))
  11848. (setq tg (org-add-props tg nil 'face
  11849. (org-get-todo-face tg)))
  11850. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11851. (insert "[" c "] " tg (make-string
  11852. (- fwidth 4 (length tg)) ?\ ))
  11853. (when (= (setq cnt (1+ cnt)) ncol)
  11854. (insert "\n")
  11855. (if ingroup (insert " "))
  11856. (setq cnt 0)))))
  11857. (insert "\n")
  11858. (goto-char (point-min))
  11859. (if (not expert) (org-fit-window-to-buffer))
  11860. (message "[a-z..]:Set [SPC]:clear")
  11861. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11862. (cond
  11863. ((or (= c ?\C-g)
  11864. (and (= c ?q) (not (rassoc c fulltable))))
  11865. (setq quit-flag t))
  11866. ((= c ?\ ) nil)
  11867. ((setq e (rassoc c fulltable) tg (car e))
  11868. tg)
  11869. (t (setq quit-flag t)))))))
  11870. (defun org-entry-is-todo-p ()
  11871. (member (org-get-todo-state) org-not-done-keywords))
  11872. (defun org-entry-is-done-p ()
  11873. (member (org-get-todo-state) org-done-keywords))
  11874. (defun org-get-todo-state ()
  11875. "Return the TODO keyword of the current subtree."
  11876. (save-excursion
  11877. (org-back-to-heading t)
  11878. (and (looking-at org-todo-line-regexp)
  11879. (match-end 2)
  11880. (match-string 2))))
  11881. (defun org-at-date-range-p (&optional inactive-ok)
  11882. "Is the cursor inside a date range?"
  11883. (interactive)
  11884. (save-excursion
  11885. (catch 'exit
  11886. (let ((pos (point)))
  11887. (skip-chars-backward "^[<\r\n")
  11888. (skip-chars-backward "<[")
  11889. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11890. (>= (match-end 0) pos)
  11891. (throw 'exit t))
  11892. (skip-chars-backward "^<[\r\n")
  11893. (skip-chars-backward "<[")
  11894. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11895. (>= (match-end 0) pos)
  11896. (throw 'exit t)))
  11897. nil)))
  11898. (defun org-get-repeat (&optional tagline)
  11899. "Check if there is a deadline/schedule with repeater in this entry."
  11900. (save-match-data
  11901. (save-excursion
  11902. (org-back-to-heading t)
  11903. (and (re-search-forward (if tagline
  11904. (concat tagline "\\s-*" org-repeat-re)
  11905. org-repeat-re)
  11906. (org-entry-end-position) t)
  11907. (match-string-no-properties 1)))))
  11908. (defvar org-last-changed-timestamp)
  11909. (defvar org-last-inserted-timestamp)
  11910. (defvar org-log-post-message)
  11911. (defvar org-log-note-purpose)
  11912. (defvar org-log-note-how nil)
  11913. (defvar org-log-note-extra)
  11914. (defun org-auto-repeat-maybe (done-word)
  11915. "Check if the current headline contains a repeated deadline/schedule.
  11916. If yes, set TODO state back to what it was and change the base date
  11917. of repeating deadline/scheduled time stamps to new date.
  11918. This function is run automatically after each state change to a DONE state."
  11919. ;; last-state is dynamically scoped into this function
  11920. (let* ((repeat (org-get-repeat))
  11921. (aa (assoc org-last-state org-todo-kwd-alist))
  11922. (interpret (nth 1 aa))
  11923. (head (nth 2 aa))
  11924. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11925. (msg "Entry repeats: ")
  11926. (org-log-done nil)
  11927. (org-todo-log-states nil)
  11928. re type n what ts time to-state)
  11929. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11930. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11931. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11932. org-todo-repeat-to-state))
  11933. (unless (and to-state (member to-state org-todo-keywords-1))
  11934. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11935. (org-todo to-state)
  11936. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11937. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11938. (org-time-stamp-format t t))))
  11939. (when org-log-repeat
  11940. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11941. (memq 'org-add-log-note post-command-hook))
  11942. ;; OK, we are already setup for some record
  11943. (if (eq org-log-repeat 'note)
  11944. ;; make sure we take a note, not only a time stamp
  11945. (setq org-log-note-how 'note))
  11946. ;; Set up for taking a record
  11947. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11948. org-last-state
  11949. 'findpos org-log-repeat)))
  11950. (org-back-to-heading t)
  11951. (org-add-planning-info nil nil 'closed)
  11952. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11953. org-deadline-time-regexp "\\)\\|\\("
  11954. org-ts-regexp "\\)"))
  11955. (while (re-search-forward
  11956. re (save-excursion (outline-next-heading) (point)) t)
  11957. (setq type (if (match-end 1) org-scheduled-string
  11958. (if (match-end 3) org-deadline-string "Plain:"))
  11959. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11960. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11961. (setq n (string-to-number (match-string 2 ts))
  11962. what (match-string 3 ts))
  11963. (if (equal what "w") (setq n (* n 7) what "d"))
  11964. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11965. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11966. ;; Preparation, see if we need to modify the start date for the change
  11967. (when (match-end 1)
  11968. (setq time (save-match-data (org-time-string-to-time ts)))
  11969. (cond
  11970. ((equal (match-string 1 ts) ".")
  11971. ;; Shift starting date to today
  11972. (org-timestamp-change
  11973. (- (org-today) (time-to-days time))
  11974. 'day))
  11975. ((equal (match-string 1 ts) "+")
  11976. (let ((nshiftmax 10) (nshift 0))
  11977. (while (or (= nshift 0)
  11978. (<= (time-to-days time)
  11979. (time-to-days (current-time))))
  11980. (when (= (incf nshift) nshiftmax)
  11981. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11982. (user-error "Abort")))
  11983. (org-timestamp-change n (cdr (assoc what whata)))
  11984. (org-at-timestamp-p t)
  11985. (setq ts (match-string 1))
  11986. (setq time (save-match-data (org-time-string-to-time ts)))))
  11987. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11988. ;; rematch, so that we have everything in place for the real shift
  11989. (org-at-timestamp-p t)
  11990. (setq ts (match-string 1))
  11991. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11992. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11993. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11994. (setq org-log-post-message msg)
  11995. (message "%s" msg))))
  11996. (defun org-show-todo-tree (arg)
  11997. "Make a compact tree which shows all headlines marked with TODO.
  11998. The tree will show the lines where the regexp matches, and all higher
  11999. headlines above the match.
  12000. With a \\[universal-argument] prefix, prompt for a regexp to match.
  12001. With a numeric prefix N, construct a sparse tree for the Nth element
  12002. of `org-todo-keywords-1'."
  12003. (interactive "P")
  12004. (let ((case-fold-search nil)
  12005. (kwd-re
  12006. (cond ((null arg) org-not-done-regexp)
  12007. ((equal arg '(4))
  12008. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  12009. (mapcar 'list org-todo-keywords-1))))
  12010. (concat "\\("
  12011. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12012. "\\)\\>")))
  12013. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12014. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12015. org-todo-keywords-1)))
  12016. (t (user-error "Invalid prefix argument: %s" arg)))))
  12017. (message "%d TODO entries found"
  12018. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  12019. (defun org-deadline (arg &optional time)
  12020. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12021. With one universal prefix argument, remove any deadline from the item.
  12022. With two universal prefix arguments, prompt for a warning delay.
  12023. With argument TIME, set the deadline at the corresponding date. TIME
  12024. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12025. (interactive "P")
  12026. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12027. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12028. 'region-start-level 'region))
  12029. org-loop-over-headlines-in-active-region)
  12030. (org-map-entries
  12031. `(org-deadline ',arg ,time)
  12032. org-loop-over-headlines-in-active-region
  12033. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12034. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12035. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12036. (repeater (and old-date
  12037. (string-match
  12038. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12039. old-date)
  12040. (match-string 1 old-date))))
  12041. (cond
  12042. ((equal arg '(4))
  12043. (when (and old-date org-log-redeadline)
  12044. (org-add-log-setup 'deldeadline nil old-date 'findpos
  12045. org-log-redeadline))
  12046. (org-remove-timestamp-with-keyword org-deadline-string)
  12047. (message "Item no longer has a deadline."))
  12048. ((equal arg '(16))
  12049. (save-excursion
  12050. (org-back-to-heading t)
  12051. (if (re-search-forward
  12052. org-deadline-time-regexp
  12053. (save-excursion (outline-next-heading) (point)) t)
  12054. (let* ((rpl0 (match-string 1))
  12055. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12056. (replace-match
  12057. (concat org-deadline-string
  12058. " <" rpl
  12059. (format " -%dd"
  12060. (abs
  12061. (- (time-to-days
  12062. (save-match-data
  12063. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12064. (time-to-days old-date-time))))
  12065. ">") t t))
  12066. (user-error "No deadline information to update"))))
  12067. (t
  12068. (org-add-planning-info 'deadline time 'closed)
  12069. (when (and old-date org-log-redeadline
  12070. (not (equal old-date
  12071. (substring org-last-inserted-timestamp 1 -1))))
  12072. (org-add-log-setup 'redeadline nil old-date 'findpos
  12073. org-log-redeadline))
  12074. (when repeater
  12075. (save-excursion
  12076. (org-back-to-heading t)
  12077. (when (re-search-forward (concat org-deadline-string " "
  12078. org-last-inserted-timestamp)
  12079. (save-excursion
  12080. (outline-next-heading) (point)) t)
  12081. (goto-char (1- (match-end 0)))
  12082. (insert " " repeater)
  12083. (setq org-last-inserted-timestamp
  12084. (concat (substring org-last-inserted-timestamp 0 -1)
  12085. " " repeater
  12086. (substring org-last-inserted-timestamp -1))))))
  12087. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12088. (defun org-schedule (arg &optional time)
  12089. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12090. With one universal prefix argument, remove any scheduling date from the item.
  12091. With two universal prefix arguments, prompt for a delay cookie.
  12092. With argument TIME, scheduled at the corresponding date. TIME can
  12093. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12094. (interactive "P")
  12095. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12096. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12097. 'region-start-level 'region))
  12098. org-loop-over-headlines-in-active-region)
  12099. (org-map-entries
  12100. `(org-schedule ',arg ,time)
  12101. org-loop-over-headlines-in-active-region
  12102. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12103. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12104. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12105. (repeater (and old-date
  12106. (string-match
  12107. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12108. old-date)
  12109. (match-string 1 old-date))))
  12110. (cond
  12111. ((equal arg '(4))
  12112. (progn
  12113. (when (and old-date org-log-reschedule)
  12114. (org-add-log-setup 'delschedule nil old-date 'findpos
  12115. org-log-reschedule))
  12116. (org-remove-timestamp-with-keyword org-scheduled-string)
  12117. (message "Item is no longer scheduled.")))
  12118. ((equal arg '(16))
  12119. (save-excursion
  12120. (org-back-to-heading t)
  12121. (if (re-search-forward
  12122. org-scheduled-time-regexp
  12123. (save-excursion (outline-next-heading) (point)) t)
  12124. (let* ((rpl0 (match-string 1))
  12125. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12126. (replace-match
  12127. (concat org-scheduled-string
  12128. " <" rpl
  12129. (format " -%dd"
  12130. (abs
  12131. (- (time-to-days
  12132. (save-match-data
  12133. (org-read-date nil t nil "Delay until" old-date-time)))
  12134. (time-to-days old-date-time))))
  12135. ">") t t))
  12136. (user-error "No scheduled information to update"))))
  12137. (t
  12138. (org-add-planning-info 'scheduled time 'closed)
  12139. (when (and old-date org-log-reschedule
  12140. (not (equal old-date
  12141. (substring org-last-inserted-timestamp 1 -1))))
  12142. (org-add-log-setup 'reschedule nil old-date 'findpos
  12143. org-log-reschedule))
  12144. (when repeater
  12145. (save-excursion
  12146. (org-back-to-heading t)
  12147. (when (re-search-forward (concat org-scheduled-string " "
  12148. org-last-inserted-timestamp)
  12149. (save-excursion
  12150. (outline-next-heading) (point)) t)
  12151. (goto-char (1- (match-end 0)))
  12152. (insert " " repeater)
  12153. (setq org-last-inserted-timestamp
  12154. (concat (substring org-last-inserted-timestamp 0 -1)
  12155. " " repeater
  12156. (substring org-last-inserted-timestamp -1))))))
  12157. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12158. (defun org-get-scheduled-time (pom &optional inherit)
  12159. "Get the scheduled time as a time tuple, of a format suitable
  12160. for calling org-schedule with, or if there is no scheduling,
  12161. returns nil."
  12162. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12163. (when time
  12164. (apply 'encode-time (org-parse-time-string time)))))
  12165. (defun org-get-deadline-time (pom &optional inherit)
  12166. "Get the deadline as a time tuple, of a format suitable for
  12167. calling org-deadline with, or if there is no scheduling, returns
  12168. nil."
  12169. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12170. (when time
  12171. (apply 'encode-time (org-parse-time-string time)))))
  12172. (defun org-remove-timestamp-with-keyword (keyword)
  12173. "Remove all time stamps with KEYWORD in the current entry."
  12174. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12175. beg)
  12176. (save-excursion
  12177. (org-back-to-heading t)
  12178. (setq beg (point))
  12179. (outline-next-heading)
  12180. (while (re-search-backward re beg t)
  12181. (replace-match "")
  12182. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12183. (equal (char-before) ?\ ))
  12184. (backward-delete-char 1)
  12185. (if (string-match "^[ \t]*$" (buffer-substring
  12186. (point-at-bol) (point-at-eol)))
  12187. (delete-region (point-at-bol)
  12188. (min (point-max) (1+ (point-at-eol))))))))))
  12189. (defvar org-time-was-given) ; dynamically scoped parameter
  12190. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12191. (defun org-add-planning-info (what &optional time &rest remove)
  12192. "Insert new timestamp with keyword in the line directly after the headline.
  12193. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  12194. If non is given, the user is prompted for a date.
  12195. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  12196. be removed."
  12197. (interactive)
  12198. (let (org-time-was-given org-end-time-was-given ts
  12199. end default-time default-input)
  12200. (catch 'exit
  12201. (when (and (memq what '(scheduled deadline))
  12202. (or (not time)
  12203. (and (stringp time)
  12204. (string-match "^[-+]+[0-9]" time))))
  12205. ;; Try to get a default date/time from existing timestamp
  12206. (save-excursion
  12207. (org-back-to-heading t)
  12208. (setq end (save-excursion (outline-next-heading) (point)))
  12209. (when (re-search-forward (if (eq what 'scheduled)
  12210. org-scheduled-time-regexp
  12211. org-deadline-time-regexp)
  12212. end t)
  12213. (setq ts (match-string 1)
  12214. default-time
  12215. (apply 'encode-time (org-parse-time-string ts))
  12216. default-input (and ts (org-get-compact-tod ts))))))
  12217. (when what
  12218. (setq time
  12219. (if (stringp time)
  12220. ;; This is a string (relative or absolute), set proper date
  12221. (apply 'encode-time
  12222. (org-read-date-analyze
  12223. time default-time (decode-time default-time)))
  12224. ;; If necessary, get the time from the user
  12225. (or time (org-read-date nil 'to-time nil nil
  12226. default-time default-input)))))
  12227. (when (and org-insert-labeled-timestamps-at-point
  12228. (member what '(scheduled deadline)))
  12229. (insert
  12230. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  12231. (org-insert-time-stamp time org-time-was-given
  12232. nil nil nil (list org-end-time-was-given))
  12233. (setq what nil))
  12234. (save-excursion
  12235. (save-restriction
  12236. (let (col list elt ts buffer-invisibility-spec)
  12237. (org-back-to-heading t)
  12238. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  12239. (goto-char (match-end 1))
  12240. (setq col (current-column))
  12241. (goto-char (match-end 0))
  12242. (if (eobp) (insert "\n") (forward-char 1))
  12243. (when (and (not what)
  12244. (not (looking-at
  12245. (concat "[ \t]*"
  12246. org-keyword-time-not-clock-regexp))))
  12247. ;; Nothing to add, nothing to remove...... :-)
  12248. (throw 'exit nil))
  12249. (if (and (not (looking-at org-outline-regexp))
  12250. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  12251. "[^\r\n]*"))
  12252. (not (equal (match-string 1) org-clock-string)))
  12253. (narrow-to-region (match-beginning 0) (match-end 0))
  12254. (insert-before-markers "\n")
  12255. (backward-char 1)
  12256. (narrow-to-region (point) (point))
  12257. (and org-adapt-indentation (org-indent-to-column col)))
  12258. ;; Check if we have to remove something.
  12259. (setq list (cons what remove))
  12260. (while list
  12261. (setq elt (pop list))
  12262. (when (or (and (eq elt 'scheduled)
  12263. (re-search-forward org-scheduled-time-regexp nil t))
  12264. (and (eq elt 'deadline)
  12265. (re-search-forward org-deadline-time-regexp nil t))
  12266. (and (eq elt 'closed)
  12267. (re-search-forward org-closed-time-regexp nil t)))
  12268. (replace-match "")
  12269. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  12270. (and (looking-at "[ \t]+") (replace-match ""))
  12271. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  12272. (when what
  12273. (insert
  12274. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  12275. (cond ((eq what 'scheduled) org-scheduled-string)
  12276. ((eq what 'deadline) org-deadline-string)
  12277. ((eq what 'closed) org-closed-string))
  12278. " ")
  12279. (setq ts (org-insert-time-stamp
  12280. time
  12281. (or org-time-was-given
  12282. (and (eq what 'closed) org-log-done-with-time))
  12283. (eq what 'closed)
  12284. nil nil (list org-end-time-was-given)))
  12285. (insert
  12286. (if (not (or (bolp) (eq (char-before) ?\ )
  12287. (memq (char-after) '(32 10))
  12288. (eobp))) " " ""))
  12289. (end-of-line 1))
  12290. (goto-char (point-min))
  12291. (widen)
  12292. (if (and (looking-at "[ \t]*\n")
  12293. (equal (char-before) ?\n))
  12294. (delete-region (1- (point)) (point-at-eol)))
  12295. ts))))))
  12296. (defvar org-log-note-marker (make-marker))
  12297. (defvar org-log-note-purpose nil)
  12298. (defvar org-log-note-state nil)
  12299. (defvar org-log-note-previous-state nil)
  12300. (defvar org-log-note-extra nil)
  12301. (defvar org-log-note-window-configuration nil)
  12302. (defvar org-log-note-return-to (make-marker))
  12303. (defvar org-log-note-effective-time nil
  12304. "Remembered current time so that dynamically scoped
  12305. `org-extend-today-until' affects tha timestamps in state change
  12306. log")
  12307. (defvar org-log-post-message nil
  12308. "Message to be displayed after a log note has been stored.
  12309. The auto-repeater uses this.")
  12310. (defun org-add-note ()
  12311. "Add a note to the current entry.
  12312. This is done in the same way as adding a state change note."
  12313. (interactive)
  12314. (org-add-log-setup 'note nil nil 'findpos nil))
  12315. (defvar org-property-end-re)
  12316. (defun org-add-log-setup (&optional purpose state prev-state
  12317. findpos how extra)
  12318. "Set up the post command hook to take a note.
  12319. If this is about to TODO state change, the new state is expected in STATE.
  12320. When FINDPOS is non-nil, find the correct position for the note in
  12321. the current entry. If not, assume that it can be inserted at point.
  12322. HOW is an indicator what kind of note should be created.
  12323. EXTRA is additional text that will be inserted into the notes buffer."
  12324. (let* ((org-log-into-drawer (org-log-into-drawer))
  12325. (drawer (cond ((stringp org-log-into-drawer)
  12326. org-log-into-drawer)
  12327. (org-log-into-drawer "LOGBOOK"))))
  12328. (save-restriction
  12329. (save-excursion
  12330. (when findpos
  12331. (org-back-to-heading t)
  12332. (narrow-to-region (point) (save-excursion
  12333. (outline-next-heading) (point)))
  12334. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12335. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12336. "[^\r\n]*\\)?"))
  12337. (goto-char (match-end 0))
  12338. (cond
  12339. (drawer
  12340. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12341. nil t)
  12342. (progn
  12343. (goto-char (match-end 0))
  12344. (or org-log-states-order-reversed
  12345. (and (re-search-forward org-property-end-re nil t)
  12346. (goto-char (1- (match-beginning 0))))))
  12347. (insert "\n:" drawer ":\n:END:")
  12348. (beginning-of-line 0)
  12349. (org-indent-line)
  12350. (beginning-of-line 2)
  12351. (org-indent-line)
  12352. (end-of-line 0)))
  12353. ((and org-log-state-notes-insert-after-drawers
  12354. (save-excursion
  12355. (forward-line) (looking-at org-drawer-regexp)))
  12356. (forward-line)
  12357. (while (looking-at org-drawer-regexp)
  12358. (goto-char (match-end 0))
  12359. (re-search-forward org-property-end-re (point-max) t)
  12360. (forward-line))
  12361. (forward-line -1)))
  12362. (unless org-log-states-order-reversed
  12363. (and (= (char-after) ?\n) (forward-char 1))
  12364. (org-skip-over-state-notes)
  12365. (skip-chars-backward " \t\n\r")))
  12366. (move-marker org-log-note-marker (point))
  12367. (setq org-log-note-purpose purpose
  12368. org-log-note-state state
  12369. org-log-note-previous-state prev-state
  12370. org-log-note-how how
  12371. org-log-note-extra extra
  12372. org-log-note-effective-time (org-current-effective-time))
  12373. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12374. (defun org-skip-over-state-notes ()
  12375. "Skip past the list of State notes in an entry."
  12376. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12377. (when (ignore-errors (goto-char (org-in-item-p)))
  12378. (let* ((struct (org-list-struct))
  12379. (prevs (org-list-prevs-alist struct)))
  12380. (while (looking-at "[ \t]*- State")
  12381. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12382. (org-list-get-item-end (point) struct)))))))
  12383. (defun org-add-log-note (&optional purpose)
  12384. "Pop up a window for taking a note, and add this note later at point."
  12385. (remove-hook 'post-command-hook 'org-add-log-note)
  12386. (setq org-log-note-window-configuration (current-window-configuration))
  12387. (delete-other-windows)
  12388. (move-marker org-log-note-return-to (point))
  12389. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12390. (goto-char org-log-note-marker)
  12391. (org-switch-to-buffer-other-window "*Org Note*")
  12392. (erase-buffer)
  12393. (if (memq org-log-note-how '(time state))
  12394. (let (current-prefix-arg) (org-store-log-note))
  12395. (let ((org-inhibit-startup t)) (org-mode))
  12396. (insert (format "# Insert note for %s.
  12397. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12398. (cond
  12399. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12400. ((eq org-log-note-purpose 'done) "closed todo item")
  12401. ((eq org-log-note-purpose 'state)
  12402. (format "state change from \"%s\" to \"%s\""
  12403. (or org-log-note-previous-state "")
  12404. (or org-log-note-state "")))
  12405. ((eq org-log-note-purpose 'reschedule)
  12406. "rescheduling")
  12407. ((eq org-log-note-purpose 'delschedule)
  12408. "no longer scheduled")
  12409. ((eq org-log-note-purpose 'redeadline)
  12410. "changing deadline")
  12411. ((eq org-log-note-purpose 'deldeadline)
  12412. "removing deadline")
  12413. ((eq org-log-note-purpose 'refile)
  12414. "refiling")
  12415. ((eq org-log-note-purpose 'note)
  12416. "this entry")
  12417. (t (error "This should not happen")))))
  12418. (if org-log-note-extra (insert org-log-note-extra))
  12419. (org-set-local 'org-finish-function 'org-store-log-note)
  12420. (run-hooks 'org-log-buffer-setup-hook)))
  12421. (defvar org-note-abort nil) ; dynamically scoped
  12422. (defun org-store-log-note ()
  12423. "Finish taking a log note, and insert it to where it belongs."
  12424. (let ((txt (buffer-string)))
  12425. (kill-buffer (current-buffer))
  12426. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12427. lines ind bul)
  12428. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12429. (setq txt (replace-match "" t t txt)))
  12430. (if (string-match "\\s-+\\'" txt)
  12431. (setq txt (replace-match "" t t txt)))
  12432. (setq lines (org-split-string txt "\n"))
  12433. (when (and note (string-match "\\S-" note))
  12434. (setq note
  12435. (org-replace-escapes
  12436. note
  12437. (list (cons "%u" (user-login-name))
  12438. (cons "%U" user-full-name)
  12439. (cons "%t" (format-time-string
  12440. (org-time-stamp-format 'long 'inactive)
  12441. org-log-note-effective-time))
  12442. (cons "%T" (format-time-string
  12443. (org-time-stamp-format 'long nil)
  12444. org-log-note-effective-time))
  12445. (cons "%d" (format-time-string
  12446. (org-time-stamp-format nil 'inactive)
  12447. org-log-note-effective-time))
  12448. (cons "%D" (format-time-string
  12449. (org-time-stamp-format nil nil)
  12450. org-log-note-effective-time))
  12451. (cons "%s" (if org-log-note-state
  12452. (concat "\"" org-log-note-state "\"")
  12453. ""))
  12454. (cons "%S" (if org-log-note-previous-state
  12455. (concat "\"" org-log-note-previous-state "\"")
  12456. "\"\"")))))
  12457. (if lines (setq note (concat note " \\\\")))
  12458. (push note lines))
  12459. (when (or current-prefix-arg org-note-abort)
  12460. (when org-log-into-drawer
  12461. (org-remove-empty-drawer-at org-log-note-marker))
  12462. (setq lines nil))
  12463. (when lines
  12464. (with-current-buffer (marker-buffer org-log-note-marker)
  12465. (save-excursion
  12466. (goto-char org-log-note-marker)
  12467. (move-marker org-log-note-marker nil)
  12468. (end-of-line 1)
  12469. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12470. (setq ind (save-excursion
  12471. (if (ignore-errors (goto-char (org-in-item-p)))
  12472. (let ((struct (org-list-struct)))
  12473. (org-list-get-ind
  12474. (org-list-get-top-point struct) struct))
  12475. (skip-chars-backward " \r\t\n")
  12476. (cond
  12477. ((and (org-at-heading-p)
  12478. org-adapt-indentation)
  12479. (1+ (org-current-level)))
  12480. ((org-at-heading-p) 0)
  12481. (t (org-get-indentation))))))
  12482. (setq bul (org-list-bullet-string "-"))
  12483. (org-indent-line-to ind)
  12484. (insert bul (pop lines))
  12485. (let ((ind-body (+ (length bul) ind)))
  12486. (while lines
  12487. (insert "\n")
  12488. (org-indent-line-to ind-body)
  12489. (insert (pop lines))))
  12490. (message "Note stored")
  12491. (org-back-to-heading t)
  12492. (org-cycle-hide-drawers 'children))
  12493. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12494. ;; from within `org-add-log-note' because `buffer-undo-list'
  12495. ;; is then modified outside of `org-with-remote-undo'.
  12496. (when (eq this-command 'org-agenda-todo)
  12497. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12498. ;; Don't add undo information when called from `org-agenda-todo'
  12499. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12500. (set-window-configuration org-log-note-window-configuration)
  12501. (with-current-buffer (marker-buffer org-log-note-return-to)
  12502. (goto-char org-log-note-return-to))
  12503. (move-marker org-log-note-return-to nil)
  12504. (and org-log-post-message (message "%s" org-log-post-message))))
  12505. (defun org-remove-empty-drawer-at (pos)
  12506. "Remove an empty drawer at position POS.
  12507. POS may also be a marker."
  12508. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12509. (org-with-wide-buffer
  12510. (goto-char pos)
  12511. (let ((drawer (org-element-at-point)))
  12512. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12513. (not (org-element-property :contents-begin drawer)))
  12514. (delete-region (org-element-property :begin drawer)
  12515. (progn (goto-char (org-element-property :end drawer))
  12516. (skip-chars-backward " \r\t\n")
  12517. (forward-line)
  12518. (point))))))))
  12519. (defvar org-ts-type nil)
  12520. (defun org-sparse-tree (&optional arg type)
  12521. "Create a sparse tree, prompt for the details.
  12522. This command can create sparse trees. You first need to select the type
  12523. of match used to create the tree:
  12524. t Show all TODO entries.
  12525. T Show entries with a specific TODO keyword.
  12526. m Show entries selected by a tags/property match.
  12527. p Enter a property name and its value (both with completion on existing
  12528. names/values) and show entries with that property.
  12529. r Show entries matching a regular expression (`/' can be used as well).
  12530. b Show deadlines and scheduled items before a date.
  12531. a Show deadlines and scheduled items after a date.
  12532. d Show deadlines due within `org-deadline-warning-days'.
  12533. D Show deadlines and scheduled items between a date range."
  12534. (interactive "P")
  12535. (setq type (or type org-sparse-tree-default-date-type))
  12536. (setq org-ts-type type)
  12537. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12538. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12539. [c]ycle through date types: %s"
  12540. (case type
  12541. (all "all timestamps")
  12542. (scheduled "only scheduled")
  12543. (deadline "only deadline")
  12544. (active "only active timestamps")
  12545. (inactive "only inactive timestamps")
  12546. (scheduled-or-deadline "scheduled/deadline")
  12547. (closed "with a closed time-stamp")
  12548. (otherwise "scheduled/deadline")))
  12549. (let ((answer (read-char-exclusive)))
  12550. (case answer
  12551. (?c
  12552. (org-sparse-tree
  12553. arg
  12554. (cadr (memq type '(scheduled-or-deadline all scheduled deadline active
  12555. inactive closed)))))
  12556. (?d (call-interactively 'org-check-deadlines))
  12557. (?b (call-interactively 'org-check-before-date))
  12558. (?a (call-interactively 'org-check-after-date))
  12559. (?D (call-interactively 'org-check-dates-range))
  12560. (?t (call-interactively 'org-show-todo-tree))
  12561. (?T (org-show-todo-tree '(4)))
  12562. (?m (call-interactively 'org-match-sparse-tree))
  12563. ((?p ?P)
  12564. (let* ((kwd (org-icompleting-read
  12565. "Property: " (mapcar 'list (org-buffer-property-keys))))
  12566. (value (org-icompleting-read
  12567. "Value: " (mapcar 'list (org-property-values kwd)))))
  12568. (unless (string-match "\\`{.*}\\'" value)
  12569. (setq value (concat "\"" value "\"")))
  12570. (org-match-sparse-tree arg (concat kwd "=" value))))
  12571. ((?r ?R ?/) (call-interactively 'org-occur))
  12572. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12573. (defvar org-occur-highlights nil
  12574. "List of overlays used for occur matches.")
  12575. (make-variable-buffer-local 'org-occur-highlights)
  12576. (defvar org-occur-parameters nil
  12577. "Parameters of the active org-occur calls.
  12578. This is a list, each call to org-occur pushes as cons cell,
  12579. containing the regular expression and the callback, onto the list.
  12580. The list can contain several entries if `org-occur' has been called
  12581. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12582. will only contain one set of parameters. When the highlights are
  12583. removed (for example with `C-c C-c', or with the next edit (depending
  12584. on `org-remove-highlights-with-change'), this variable is emptied
  12585. as well.")
  12586. (make-variable-buffer-local 'org-occur-parameters)
  12587. (defun org-occur (regexp &optional keep-previous callback)
  12588. "Make a compact tree which shows all matches of REGEXP.
  12589. The tree will show the lines where the regexp matches, and all higher
  12590. headlines above the match. It will also show the heading after the match,
  12591. to make sure editing the matching entry is easy.
  12592. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12593. call to `org-occur' will be kept, to allow stacking of calls to this
  12594. command.
  12595. If CALLBACK is non-nil, it is a function which is called to confirm
  12596. that the match should indeed be shown."
  12597. (interactive "sRegexp: \nP")
  12598. (when (equal regexp "")
  12599. (user-error "Regexp cannot be empty"))
  12600. (unless keep-previous
  12601. (org-remove-occur-highlights nil nil t))
  12602. (push (cons regexp callback) org-occur-parameters)
  12603. (let ((cnt 0))
  12604. (save-excursion
  12605. (goto-char (point-min))
  12606. (if (or (not keep-previous) ; do not want to keep
  12607. (not org-occur-highlights)) ; no previous matches
  12608. ;; hide everything
  12609. (org-overview))
  12610. (while (re-search-forward regexp nil t)
  12611. (when (or (not callback)
  12612. (save-match-data (funcall callback)))
  12613. (setq cnt (1+ cnt))
  12614. (when org-highlight-sparse-tree-matches
  12615. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12616. (org-show-context 'occur-tree))))
  12617. (when org-remove-highlights-with-change
  12618. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12619. nil 'local))
  12620. (unless org-sparse-tree-open-archived-trees
  12621. (org-hide-archived-subtrees (point-min) (point-max)))
  12622. (run-hooks 'org-occur-hook)
  12623. (if (org-called-interactively-p 'interactive)
  12624. (message "%d match(es) for regexp %s" cnt regexp))
  12625. cnt))
  12626. (defun org-occur-next-match (&optional n reset)
  12627. "Function for `next-error-function' to find sparse tree matches.
  12628. N is the number of matches to move, when negative move backwards.
  12629. RESET is entirely ignored - this function always goes back to the
  12630. starting point when no match is found."
  12631. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12632. (search-func (if (< n 0)
  12633. 'previous-single-char-property-change
  12634. 'next-single-char-property-change))
  12635. (n (abs n))
  12636. (pos (point))
  12637. p1)
  12638. (catch 'exit
  12639. (while (setq p1 (funcall search-func (point) 'org-type))
  12640. (when (equal p1 limit)
  12641. (goto-char pos)
  12642. (user-error "No more matches"))
  12643. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12644. (setq n (1- n))
  12645. (when (= n 0)
  12646. (goto-char p1)
  12647. (throw 'exit (point))))
  12648. (goto-char p1))
  12649. (goto-char p1)
  12650. (user-error "No more matches"))))
  12651. (defun org-show-context (&optional key)
  12652. "Make sure point and context are visible.
  12653. How much context is shown depends upon the variables
  12654. `org-show-hierarchy-above', `org-show-following-heading',
  12655. `org-show-entry-below' and `org-show-siblings'."
  12656. (let ((heading-p (org-at-heading-p t))
  12657. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12658. (following-p (org-get-alist-option org-show-following-heading key))
  12659. (entry-p (org-get-alist-option org-show-entry-below key))
  12660. (siblings-p (org-get-alist-option org-show-siblings key)))
  12661. ;; Show heading or entry text
  12662. (if (and heading-p (not entry-p))
  12663. (org-flag-heading nil) ; only show the heading
  12664. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12665. (org-show-hidden-entry))) ; show entire entry
  12666. (when following-p
  12667. ;; Show next sibling, or heading below text
  12668. (save-excursion
  12669. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12670. (org-flag-heading nil))))
  12671. (when siblings-p (org-show-siblings))
  12672. (when hierarchy-p
  12673. ;; show all higher headings, possibly with siblings
  12674. (save-excursion
  12675. (while (and (ignore-errors (progn (org-up-heading-all 1) t))
  12676. (not (bobp)))
  12677. (org-flag-heading nil)
  12678. (when siblings-p (org-show-siblings)))))))
  12679. (defvar org-reveal-start-hook nil
  12680. "Hook run before revealing a location.")
  12681. (defun org-reveal (&optional siblings)
  12682. "Show current entry, hierarchy above it, and the following headline.
  12683. This can be used to show a consistent set of context around locations
  12684. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12685. not t for the search context.
  12686. With optional argument SIBLINGS, on each level of the hierarchy all
  12687. siblings are shown. This repairs the tree structure to what it would
  12688. look like when opened with hierarchical calls to `org-cycle'.
  12689. With double optional argument \\[universal-argument] \\[universal-argument], \
  12690. go to the parent and show the
  12691. entire tree."
  12692. (interactive "P")
  12693. (run-hooks 'org-reveal-start-hook)
  12694. (let ((org-show-hierarchy-above t)
  12695. (org-show-following-heading t)
  12696. (org-show-siblings (if siblings t org-show-siblings)))
  12697. (org-show-context nil))
  12698. (when (equal siblings '(16))
  12699. (save-excursion
  12700. (when (org-up-heading-safe)
  12701. (org-show-subtree)
  12702. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12703. (defun org-highlight-new-match (beg end)
  12704. "Highlight from BEG to END and mark the highlight is an occur headline."
  12705. (let ((ov (make-overlay beg end)))
  12706. (overlay-put ov 'face 'secondary-selection)
  12707. (overlay-put ov 'org-type 'org-occur)
  12708. (push ov org-occur-highlights)))
  12709. (defun org-remove-occur-highlights (&optional beg end noremove)
  12710. "Remove the occur highlights from the buffer.
  12711. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12712. from the `before-change-functions' in the current buffer."
  12713. (interactive)
  12714. (unless org-inhibit-highlight-removal
  12715. (mapc 'delete-overlay org-occur-highlights)
  12716. (setq org-occur-highlights nil)
  12717. (setq org-occur-parameters nil)
  12718. (unless noremove
  12719. (remove-hook 'before-change-functions
  12720. 'org-remove-occur-highlights 'local))))
  12721. ;;;; Priorities
  12722. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12723. "Regular expression matching the priority indicator.")
  12724. (defvar org-remove-priority-next-time nil)
  12725. (defun org-priority-up ()
  12726. "Increase the priority of the current item."
  12727. (interactive)
  12728. (org-priority 'up))
  12729. (defun org-priority-down ()
  12730. "Decrease the priority of the current item."
  12731. (interactive)
  12732. (org-priority 'down))
  12733. (defun org-priority (&optional action show)
  12734. "Change the priority of an item.
  12735. ACTION can be `set', `up', `down', or a character."
  12736. (interactive "P")
  12737. (if (equal action '(4))
  12738. (org-show-priority)
  12739. (unless org-enable-priority-commands
  12740. (user-error "Priority commands are disabled"))
  12741. (setq action (or action 'set))
  12742. (let (current new news have remove)
  12743. (save-excursion
  12744. (org-back-to-heading t)
  12745. (if (looking-at org-priority-regexp)
  12746. (setq current (string-to-char (match-string 2))
  12747. have t))
  12748. (cond
  12749. ((eq action 'remove)
  12750. (setq remove t new ?\ ))
  12751. ((or (eq action 'set)
  12752. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12753. (if (not (eq action 'set))
  12754. (setq new action)
  12755. (message "Priority %c-%c, SPC to remove: "
  12756. org-highest-priority org-lowest-priority)
  12757. (save-match-data
  12758. (setq new (read-char-exclusive))))
  12759. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12760. (= (upcase org-lowest-priority) org-lowest-priority))
  12761. (setq new (upcase new)))
  12762. (cond ((equal new ?\ ) (setq remove t))
  12763. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12764. (user-error "Priority must be between `%c' and `%c'"
  12765. org-highest-priority org-lowest-priority))))
  12766. ((eq action 'up)
  12767. (setq new (if have
  12768. (1- current) ; normal cycling
  12769. ;; last priority was empty
  12770. (if (eq last-command this-command)
  12771. org-lowest-priority ; wrap around empty to lowest
  12772. ;; default
  12773. (if org-priority-start-cycle-with-default
  12774. org-default-priority
  12775. (1- org-default-priority))))))
  12776. ((eq action 'down)
  12777. (setq new (if have
  12778. (1+ current) ; normal cycling
  12779. ;; last priority was empty
  12780. (if (eq last-command this-command)
  12781. org-highest-priority ; wrap around empty to highest
  12782. ;; default
  12783. (if org-priority-start-cycle-with-default
  12784. org-default-priority
  12785. (1+ org-default-priority))))))
  12786. (t (user-error "Invalid action")))
  12787. (if (or (< (upcase new) org-highest-priority)
  12788. (> (upcase new) org-lowest-priority))
  12789. (if (and (memq action '(up down))
  12790. (not have) (not (eq last-command this-command)))
  12791. ;; `new' is from default priority
  12792. (error
  12793. "The default can not be set, see `org-default-priority' why")
  12794. ;; normal cycling: `new' is beyond highest/lowest priority
  12795. ;; and is wrapped around to the empty priority
  12796. (setq remove t)))
  12797. (setq news (format "%c" new))
  12798. (if have
  12799. (if remove
  12800. (replace-match "" t t nil 1)
  12801. (replace-match news t t nil 2))
  12802. (if remove
  12803. (user-error "No priority cookie found in line")
  12804. (let ((case-fold-search nil))
  12805. (looking-at org-todo-line-regexp))
  12806. (if (match-end 2)
  12807. (progn
  12808. (goto-char (match-end 2))
  12809. (insert " [#" news "]"))
  12810. (goto-char (match-beginning 3))
  12811. (insert "[#" news "] "))))
  12812. (org-set-tags nil 'align))
  12813. (if remove
  12814. (message "Priority removed")
  12815. (message "Priority of current item set to %s" news)))))
  12816. (defun org-show-priority ()
  12817. "Show the priority of the current item.
  12818. This priority is composed of the main priority given with the [#A] cookies,
  12819. and by additional input from the age of a schedules or deadline entry."
  12820. (interactive)
  12821. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12822. (org-get-at-bol 'priority)
  12823. (save-excursion
  12824. (save-match-data
  12825. (beginning-of-line)
  12826. (and (looking-at org-heading-regexp)
  12827. (org-get-priority (match-string 0))))))))
  12828. (message "Priority is %d" (if pri pri -1000))))
  12829. (defun org-get-priority (s)
  12830. "Find priority cookie and return priority."
  12831. (save-match-data
  12832. (if (functionp org-get-priority-function)
  12833. (funcall org-get-priority-function)
  12834. (if (not (string-match org-priority-regexp s))
  12835. (* 1000 (- org-lowest-priority org-default-priority))
  12836. (* 1000 (- org-lowest-priority
  12837. (string-to-char (match-string 2 s))))))))
  12838. ;;;; Tags
  12839. (defvar org-agenda-archives-mode)
  12840. (defvar org-map-continue-from nil
  12841. "Position from where mapping should continue.
  12842. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12843. (defvar org-scanner-tags nil
  12844. "The current tag list while the tags scanner is running.")
  12845. (defvar org-trust-scanner-tags nil
  12846. "Should `org-get-tags-at' use the tags for the scanner.
  12847. This is for internal dynamical scoping only.
  12848. When this is non-nil, the function `org-get-tags-at' will return the value
  12849. of `org-scanner-tags' instead of building the list by itself. This
  12850. can lead to large speed-ups when the tags scanner is used in a file with
  12851. many entries, and when the list of tags is retrieved, for example to
  12852. obtain a list of properties. Building the tags list for each entry in such
  12853. a file becomes an N^2 operation - but with this variable set, it scales
  12854. as N.")
  12855. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12856. "Scan headline tags with inheritance and produce output ACTION.
  12857. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12858. or `agenda' to produce an entry list for an agenda view. It can also be
  12859. a Lisp form or a function that should be called at each matched headline, in
  12860. this case the return value is a list of all return values from these calls.
  12861. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12862. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12863. only lines with a not-done TODO keyword are included in the output.
  12864. This should be the same variable that was scoped into
  12865. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12866. START-LEVEL can be a string with asterisks, reducing the scope to
  12867. headlines matching this string."
  12868. (require 'org-agenda)
  12869. (let* ((re (concat "^"
  12870. (if start-level
  12871. ;; Get the correct level to match
  12872. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12873. org-outline-regexp)
  12874. " *\\(\\<\\("
  12875. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12876. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12877. (props (list 'face 'default
  12878. 'done-face 'org-agenda-done
  12879. 'undone-face 'default
  12880. 'mouse-face 'highlight
  12881. 'org-not-done-regexp org-not-done-regexp
  12882. 'org-todo-regexp org-todo-regexp
  12883. 'org-complex-heading-regexp org-complex-heading-regexp
  12884. 'help-echo
  12885. (format "mouse-2 or RET jump to org file %s"
  12886. (abbreviate-file-name
  12887. (or (buffer-file-name (buffer-base-buffer))
  12888. (buffer-name (buffer-base-buffer)))))))
  12889. (org-map-continue-from nil)
  12890. lspos tags tags-list
  12891. (tags-alist (list (cons 0 org-file-tags)))
  12892. (llast 0) rtn rtn1 level category i txt
  12893. todo marker entry priority)
  12894. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12895. (setq action (list 'lambda nil action)))
  12896. (save-excursion
  12897. (goto-char (point-min))
  12898. (when (eq action 'sparse-tree)
  12899. (org-overview)
  12900. (org-remove-occur-highlights))
  12901. (while (let (case-fold-search)
  12902. (re-search-forward re nil t))
  12903. (setq org-map-continue-from nil)
  12904. (catch :skip
  12905. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12906. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12907. (goto-char (setq lspos (match-beginning 0)))
  12908. (setq level (org-reduced-level (org-outline-level))
  12909. category (org-get-category))
  12910. (setq i llast llast level)
  12911. ;; remove tag lists from same and sublevels
  12912. (while (>= i level)
  12913. (when (setq entry (assoc i tags-alist))
  12914. (setq tags-alist (delete entry tags-alist)))
  12915. (setq i (1- i)))
  12916. ;; add the next tags
  12917. (when tags
  12918. (setq tags (org-split-string tags ":")
  12919. tags-alist
  12920. (cons (cons level tags) tags-alist)))
  12921. ;; compile tags for current headline
  12922. (setq tags-list
  12923. (if org-use-tag-inheritance
  12924. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12925. tags)
  12926. org-scanner-tags tags-list)
  12927. (when org-use-tag-inheritance
  12928. (setcdr (car tags-alist)
  12929. (mapcar (lambda (x)
  12930. (setq x (copy-sequence x))
  12931. (org-add-prop-inherited x))
  12932. (cdar tags-alist))))
  12933. (when (and tags org-use-tag-inheritance
  12934. (or (not (eq t org-use-tag-inheritance))
  12935. org-tags-exclude-from-inheritance))
  12936. ;; selective inheritance, remove uninherited ones
  12937. (setcdr (car tags-alist)
  12938. (org-remove-uninherited-tags (cdar tags-alist))))
  12939. (when (and
  12940. ;; eval matcher only when the todo condition is OK
  12941. (and (or (not todo-only) (member todo org-not-done-keywords))
  12942. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12943. (eval matcher)))
  12944. ;; Call the skipper, but return t if it does not skip,
  12945. ;; so that the `and' form continues evaluating
  12946. (progn
  12947. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12948. t)
  12949. ;; Check if timestamps are deselecting this entry
  12950. (or (not todo-only)
  12951. (and (member todo org-not-done-keywords)
  12952. (or (not org-agenda-tags-todo-honor-ignore-options)
  12953. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12954. ;; select this headline
  12955. (cond
  12956. ((eq action 'sparse-tree)
  12957. (and org-highlight-sparse-tree-matches
  12958. (org-get-heading) (match-end 0)
  12959. (org-highlight-new-match
  12960. (match-beginning 1) (match-end 1)))
  12961. (org-show-context 'tags-tree))
  12962. ((eq action 'agenda)
  12963. (setq txt (org-agenda-format-item
  12964. ""
  12965. (concat
  12966. (if (eq org-tags-match-list-sublevels 'indented)
  12967. (make-string (1- level) ?.) "")
  12968. (org-get-heading))
  12969. level category
  12970. tags-list)
  12971. priority (org-get-priority txt))
  12972. (goto-char lspos)
  12973. (setq marker (org-agenda-new-marker))
  12974. (org-add-props txt props
  12975. 'org-marker marker 'org-hd-marker marker 'org-category category
  12976. 'todo-state todo
  12977. 'priority priority 'type "tagsmatch")
  12978. (push txt rtn))
  12979. ((functionp action)
  12980. (setq org-map-continue-from nil)
  12981. (save-excursion
  12982. (setq rtn1 (funcall action))
  12983. (push rtn1 rtn)))
  12984. (t (user-error "Invalid action")))
  12985. ;; if we are to skip sublevels, jump to end of subtree
  12986. (unless org-tags-match-list-sublevels
  12987. (org-end-of-subtree t)
  12988. (backward-char 1))))
  12989. ;; Get the correct position from where to continue
  12990. (if org-map-continue-from
  12991. (goto-char org-map-continue-from)
  12992. (and (= (point) lspos) (end-of-line 1)))))
  12993. (when (and (eq action 'sparse-tree)
  12994. (not org-sparse-tree-open-archived-trees))
  12995. (org-hide-archived-subtrees (point-min) (point-max)))
  12996. (nreverse rtn)))
  12997. (defun org-remove-uninherited-tags (tags)
  12998. "Remove all tags that are not inherited from the list TAGS."
  12999. (cond
  13000. ((eq org-use-tag-inheritance t)
  13001. (if org-tags-exclude-from-inheritance
  13002. (org-delete-all org-tags-exclude-from-inheritance tags)
  13003. tags))
  13004. ((not org-use-tag-inheritance) nil)
  13005. ((stringp org-use-tag-inheritance)
  13006. (delq nil (mapcar
  13007. (lambda (x)
  13008. (if (and (string-match org-use-tag-inheritance x)
  13009. (not (member x org-tags-exclude-from-inheritance)))
  13010. x nil))
  13011. tags)))
  13012. ((listp org-use-tag-inheritance)
  13013. (delq nil (mapcar
  13014. (lambda (x)
  13015. (if (member x org-use-tag-inheritance) x nil))
  13016. tags)))))
  13017. (defun org-match-sparse-tree (&optional todo-only match)
  13018. "Create a sparse tree according to tags string MATCH.
  13019. MATCH can contain positive and negative selection of tags, like
  13020. \"+WORK+URGENT-WITHBOSS\".
  13021. If optional argument TODO-ONLY is non-nil, only select lines that are
  13022. also TODO lines."
  13023. (interactive "P")
  13024. (org-agenda-prepare-buffers (list (current-buffer)))
  13025. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13026. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13027. (defvar org-cached-props nil)
  13028. (defun org-cached-entry-get (pom property)
  13029. (if (or (eq t org-use-property-inheritance)
  13030. (and (stringp org-use-property-inheritance)
  13031. (string-match org-use-property-inheritance property))
  13032. (and (listp org-use-property-inheritance)
  13033. (member property org-use-property-inheritance)))
  13034. ;; Caching is not possible, check it directly
  13035. (org-entry-get pom property 'inherit)
  13036. ;; Get all properties, so that we can do complicated checks easily
  13037. (cdr (assoc property (or org-cached-props
  13038. (setq org-cached-props
  13039. (org-entry-properties pom)))))))
  13040. (defun org-global-tags-completion-table (&optional files)
  13041. "Return the list of all tags in all agenda buffer/files.
  13042. Optional FILES argument is a list of files which can be used
  13043. instead of the agenda files."
  13044. (save-excursion
  13045. (org-uniquify
  13046. (delq nil
  13047. (apply 'append
  13048. (mapcar
  13049. (lambda (file)
  13050. (set-buffer (find-file-noselect file))
  13051. (append (org-get-buffer-tags)
  13052. (mapcar (lambda (x) (if (stringp (car-safe x))
  13053. (list (car-safe x)) nil))
  13054. org-tag-alist)))
  13055. (if (and files (car files))
  13056. files
  13057. (org-agenda-files))))))))
  13058. (defun org-make-tags-matcher (match)
  13059. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13060. The variable `todo-only' is scoped dynamically into this function.
  13061. It will be set to t if the matcher restricts matching to TODO entries,
  13062. otherwise will not be touched.
  13063. Returns a cons of the selection string MATCH and the constructed
  13064. lisp form implementing the matcher. The matcher is to be evaluated
  13065. at an Org entry, with point on the headline, and returns t if the
  13066. entry matches the selection string MATCH. The returned lisp form
  13067. references two variables with information about the entry, which
  13068. must be bound around the form's evaluation: todo, the TODO keyword
  13069. at the entry (or nil of none); and tags-list, the list of all tags
  13070. at the entry including inherited ones. Additionally, the category
  13071. of the entry (if any) must be specified as the text property
  13072. 'org-category on the headline.
  13073. See also `org-scan-tags'.
  13074. "
  13075. (declare (special todo-only))
  13076. (unless (boundp 'todo-only)
  13077. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  13078. (unless match
  13079. ;; Get a new match request, with completion against the global
  13080. ;; tags table and the local tags in current buffer
  13081. (let ((org-last-tags-completion-table
  13082. (org-uniquify
  13083. (delq nil (append (org-get-buffer-tags)
  13084. (org-global-tags-completion-table))))))
  13085. (setq match (org-completing-read-no-i
  13086. "Match: " 'org-tags-completion-function nil nil nil
  13087. 'org-tags-history))))
  13088. ;; Parse the string and create a lisp form
  13089. (let ((match0 match)
  13090. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13091. minus tag mm
  13092. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13093. orterms term orlist re-p str-p level-p level-op time-p
  13094. prop-p pn pv po gv rest (start 0) (ss 0))
  13095. ;; Expand group tags
  13096. (setq match (org-tags-expand match))
  13097. ;; Check if there is a TODO part of this match, which would be the
  13098. ;; part after a "/". TO make sure that this slash is not part of
  13099. ;; a property value to be matched against, we also check that there
  13100. ;; is no " after that slash.
  13101. ;; First, find the last slash
  13102. (while (string-match "/+" match ss)
  13103. (setq start (match-beginning 0) ss (match-end 0)))
  13104. (if (and (string-match "/+" match start)
  13105. (not (save-match-data (string-match "\"" match start))))
  13106. ;; match contains also a todo-matching request
  13107. (progn
  13108. (setq tagsmatch (substring match 0 (match-beginning 0))
  13109. todomatch (substring match (match-end 0)))
  13110. (if (string-match "^!" todomatch)
  13111. (setq todo-only t todomatch (substring todomatch 1)))
  13112. (if (string-match "^\\s-*$" todomatch)
  13113. (setq todomatch nil)))
  13114. ;; only matching tags
  13115. (setq tagsmatch match todomatch nil))
  13116. ;; Make the tags matcher
  13117. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13118. (setq tagsmatcher t)
  13119. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13120. (while (setq term (pop orterms))
  13121. (while (and (equal (substring term -1) "\\") orterms)
  13122. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13123. (while (string-match re term)
  13124. (setq rest (substring term (match-end 0))
  13125. minus (and (match-end 1)
  13126. (equal (match-string 1 term) "-"))
  13127. tag (save-match-data (replace-regexp-in-string
  13128. "\\\\-" "-"
  13129. (match-string 2 term)))
  13130. re-p (equal (string-to-char tag) ?{)
  13131. level-p (match-end 4)
  13132. prop-p (match-end 5)
  13133. mm (cond
  13134. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13135. (level-p
  13136. (setq level-op (org-op-to-function (match-string 3 term)))
  13137. `(,level-op level ,(string-to-number
  13138. (match-string 4 term))))
  13139. (prop-p
  13140. (setq pn (match-string 5 term)
  13141. po (match-string 6 term)
  13142. pv (match-string 7 term)
  13143. re-p (equal (string-to-char pv) ?{)
  13144. str-p (equal (string-to-char pv) ?\")
  13145. time-p (save-match-data
  13146. (string-match "^\"[[<].*[]>]\"$" pv))
  13147. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  13148. (if time-p (setq pv (org-matcher-time pv)))
  13149. (setq po (org-op-to-function po (if time-p 'time str-p)))
  13150. (cond
  13151. ((equal pn "CATEGORY")
  13152. (setq gv '(get-text-property (point) 'org-category)))
  13153. ((equal pn "TODO")
  13154. (setq gv 'todo))
  13155. (t
  13156. (setq gv `(org-cached-entry-get nil ,pn))))
  13157. (if re-p
  13158. (if (eq po 'org<>)
  13159. `(not (string-match ,pv (or ,gv "")))
  13160. `(string-match ,pv (or ,gv "")))
  13161. (if str-p
  13162. `(,po (or ,gv "") ,pv)
  13163. `(,po (string-to-number (or ,gv ""))
  13164. ,(string-to-number pv) ))))
  13165. (t `(member ,tag tags-list)))
  13166. mm (if minus (list 'not mm) mm)
  13167. term rest)
  13168. (push mm tagsmatcher))
  13169. (push (if (> (length tagsmatcher) 1)
  13170. (cons 'and tagsmatcher)
  13171. (car tagsmatcher))
  13172. orlist)
  13173. (setq tagsmatcher nil))
  13174. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13175. (setq tagsmatcher
  13176. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13177. ;; Make the todo matcher
  13178. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13179. (setq todomatcher t)
  13180. (setq orterms (org-split-string todomatch "|") orlist nil)
  13181. (while (setq term (pop orterms))
  13182. (while (string-match re term)
  13183. (setq minus (and (match-end 1)
  13184. (equal (match-string 1 term) "-"))
  13185. kwd (match-string 2 term)
  13186. re-p (equal (string-to-char kwd) ?{)
  13187. term (substring term (match-end 0))
  13188. mm (if re-p
  13189. `(string-match ,(substring kwd 1 -1) todo)
  13190. (list 'equal 'todo kwd))
  13191. mm (if minus (list 'not mm) mm))
  13192. (push mm todomatcher))
  13193. (push (if (> (length todomatcher) 1)
  13194. (cons 'and todomatcher)
  13195. (car todomatcher))
  13196. orlist)
  13197. (setq todomatcher nil))
  13198. (setq todomatcher (if (> (length orlist) 1)
  13199. (cons 'or orlist) (car orlist))))
  13200. ;; Return the string and lisp forms of the matcher
  13201. (setq matcher (if todomatcher
  13202. (list 'and tagsmatcher todomatcher)
  13203. tagsmatcher))
  13204. (when todo-only
  13205. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13206. matcher)))
  13207. (cons match0 matcher)))
  13208. (defun org-tags-expand (match &optional single-as-list downcased)
  13209. "Expand group tags in MATCH.
  13210. This replaces every group tag in MATCH with a regexp tag search.
  13211. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13212. will be replaced like this:
  13213. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  13214. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  13215. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  13216. Replacing by a regexp preserves the structure of the match.
  13217. E.g., this expansion
  13218. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13219. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13220. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13221. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13222. assumed to be a single group tag, and the function will return
  13223. the list of tags in this group.
  13224. When DOWNCASE is non-nil, expand downcased TAGS."
  13225. (if org-group-tags
  13226. (let* ((case-fold-search t)
  13227. (stable org-mode-syntax-table)
  13228. (tal (or org-tag-groups-alist-for-agenda
  13229. org-tag-groups-alist))
  13230. (tal (if downcased
  13231. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  13232. (tml (mapcar 'car tal))
  13233. (rtnmatch match) rpl)
  13234. ;; @ and _ are allowed as word-components in tags
  13235. (modify-syntax-entry ?@ "w" stable)
  13236. (modify-syntax-entry ?_ "w" stable)
  13237. (while (and tml
  13238. (with-syntax-table stable
  13239. (string-match
  13240. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13241. (regexp-opt tml) "\\>\\)") rtnmatch)))
  13242. (let* ((dir (match-string 1 rtnmatch))
  13243. (tag (match-string 2 rtnmatch))
  13244. (tag (if downcased (downcase tag) tag)))
  13245. (setq tml (delete tag tml))
  13246. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  13247. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  13248. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  13249. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  13250. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  13251. (if single-as-list
  13252. (or (reverse rpl) (list rtnmatch))
  13253. rtnmatch))
  13254. (if single-as-list (list (if downcased (downcase match) match))
  13255. match)))
  13256. (defun org-op-to-function (op &optional stringp)
  13257. "Turn an operator into the appropriate function."
  13258. (setq op
  13259. (cond
  13260. ((equal op "<" ) '(< string< org-time<))
  13261. ((equal op ">" ) '(> org-string> org-time>))
  13262. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13263. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13264. ((member op '("=" "==")) '(= string= org-time=))
  13265. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13266. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13267. (defun org<> (a b) (not (= a b)))
  13268. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13269. (defun org-string>= (a b) (not (string< a b)))
  13270. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13271. (defun org-string<> (a b) (not (string= a b)))
  13272. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13273. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13274. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13275. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13276. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13277. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13278. (defun org-2ft (s)
  13279. "Convert S to a floating point time.
  13280. If S is already a number, just return it. If it is a string, parse
  13281. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13282. (cond
  13283. ((numberp s) s)
  13284. ((stringp s)
  13285. (condition-case nil
  13286. (float-time (apply 'encode-time (org-parse-time-string s)))
  13287. (error 0.)))
  13288. (t 0.)))
  13289. (defun org-time-today ()
  13290. "Time in seconds today at 0:00.
  13291. Returns the float number of seconds since the beginning of the
  13292. epoch to the beginning of today (00:00)."
  13293. (float-time (apply 'encode-time
  13294. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13295. (defun org-matcher-time (s)
  13296. "Interpret a time comparison value."
  13297. (save-match-data
  13298. (cond
  13299. ((string= s "<now>") (float-time))
  13300. ((string= s "<today>") (org-time-today))
  13301. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13302. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13303. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13304. (+ (org-time-today)
  13305. (* (string-to-number (match-string 1 s))
  13306. (cdr (assoc (match-string 2 s)
  13307. '(("d" . 86400.0) ("w" . 604800.0)
  13308. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13309. (t (org-2ft s)))))
  13310. (defun org-match-any-p (re list)
  13311. "Does re match any element of list?"
  13312. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13313. (delq nil list))
  13314. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13315. (defvar org-tags-overlay (make-overlay 1 1))
  13316. (org-detach-overlay org-tags-overlay)
  13317. (defun org-get-local-tags-at (&optional pos)
  13318. "Get a list of tags defined in the current headline."
  13319. (org-get-tags-at pos 'local))
  13320. (defun org-get-local-tags ()
  13321. "Get a list of tags defined in the current headline."
  13322. (org-get-tags-at nil 'local))
  13323. (defun org-get-tags-at (&optional pos local)
  13324. "Get a list of all headline tags applicable at POS.
  13325. POS defaults to point. If tags are inherited, the list contains
  13326. the targets in the same sequence as the headlines appear, i.e.
  13327. the tags of the current headline come last.
  13328. When LOCAL is non-nil, only return tags from the current headline,
  13329. ignore inherited ones."
  13330. (interactive)
  13331. (if (and org-trust-scanner-tags
  13332. (or (not pos) (equal pos (point)))
  13333. (not local))
  13334. org-scanner-tags
  13335. (let (tags ltags lastpos parent)
  13336. (save-excursion
  13337. (save-restriction
  13338. (widen)
  13339. (goto-char (or pos (point)))
  13340. (save-match-data
  13341. (catch 'done
  13342. (condition-case nil
  13343. (progn
  13344. (org-back-to-heading t)
  13345. (while (not (equal lastpos (point)))
  13346. (setq lastpos (point))
  13347. (when (looking-at
  13348. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13349. (setq ltags (org-split-string
  13350. (org-match-string-no-properties 1) ":"))
  13351. (when parent
  13352. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13353. (setq tags (append
  13354. (if parent
  13355. (org-remove-uninherited-tags ltags)
  13356. ltags)
  13357. tags)))
  13358. (or org-use-tag-inheritance (throw 'done t))
  13359. (if local (throw 'done t))
  13360. (or (org-up-heading-safe) (error nil))
  13361. (setq parent t)))
  13362. (error nil)))))
  13363. (if local
  13364. tags
  13365. (reverse (delete-dups
  13366. (reverse (append
  13367. (org-remove-uninherited-tags
  13368. org-file-tags) tags)))))))))
  13369. (defun org-add-prop-inherited (s)
  13370. (add-text-properties 0 (length s) '(inherited t) s)
  13371. s)
  13372. (defun org-toggle-tag (tag &optional onoff)
  13373. "Toggle the tag TAG for the current line.
  13374. If ONOFF is `on' or `off', don't toggle but set to this state."
  13375. (let (res current)
  13376. (save-excursion
  13377. (org-back-to-heading t)
  13378. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13379. (point-at-eol) t)
  13380. (progn
  13381. (setq current (match-string 1))
  13382. (replace-match ""))
  13383. (setq current ""))
  13384. (setq current (nreverse (org-split-string current ":")))
  13385. (cond
  13386. ((eq onoff 'on)
  13387. (setq res t)
  13388. (or (member tag current) (push tag current)))
  13389. ((eq onoff 'off)
  13390. (or (not (member tag current)) (setq current (delete tag current))))
  13391. (t (if (member tag current)
  13392. (setq current (delete tag current))
  13393. (setq res t)
  13394. (push tag current))))
  13395. (end-of-line 1)
  13396. (if current
  13397. (progn
  13398. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13399. (org-set-tags nil t))
  13400. (delete-horizontal-space))
  13401. (run-hooks 'org-after-tags-change-hook))
  13402. res))
  13403. (defun org-align-tags-here (to-col)
  13404. ;; Assumes that this is a headline
  13405. "Align tags on the current headline to TO-COL."
  13406. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13407. (beginning-of-line 1)
  13408. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13409. (< pos (match-beginning 2)))
  13410. (progn
  13411. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13412. (goto-char (match-beginning 1))
  13413. (insert " ")
  13414. (delete-region (point) (1+ (match-beginning 2)))
  13415. (setq ncol (max (current-column)
  13416. (1+ col)
  13417. (if (> to-col 0)
  13418. to-col
  13419. (- (abs to-col) tags-l))))
  13420. (setq p (point))
  13421. (insert (make-string (- ncol (current-column)) ?\ ))
  13422. (setq ncol (current-column))
  13423. (when indent-tabs-mode (tabify p (point-at-eol)))
  13424. (org-move-to-column (min ncol col)))
  13425. (goto-char pos))))
  13426. (defun org-set-tags-command (&optional arg just-align)
  13427. "Call the set-tags command for the current entry."
  13428. (interactive "P")
  13429. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13430. (org-set-tags arg just-align)
  13431. (save-excursion
  13432. (unless (and (org-region-active-p)
  13433. org-loop-over-headlines-in-active-region)
  13434. (org-back-to-heading t))
  13435. (org-set-tags arg just-align))))
  13436. (defun org-set-tags-to (data)
  13437. "Set the tags of the current entry to DATA, replacing the current tags.
  13438. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13439. If DATA is nil or the empty string, any tags will be removed."
  13440. (interactive "sTags: ")
  13441. (setq data
  13442. (cond
  13443. ((eq data nil) "")
  13444. ((equal data "") "")
  13445. ((stringp data)
  13446. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13447. ":"))
  13448. ((listp data)
  13449. (concat ":" (mapconcat 'identity data ":") ":"))))
  13450. (when data
  13451. (save-excursion
  13452. (org-back-to-heading t)
  13453. (when (looking-at org-complex-heading-regexp)
  13454. (if (match-end 5)
  13455. (progn
  13456. (goto-char (match-beginning 5))
  13457. (insert data)
  13458. (delete-region (point) (point-at-eol))
  13459. (org-set-tags nil 'align))
  13460. (goto-char (point-at-eol))
  13461. (insert " " data)
  13462. (org-set-tags nil 'align)))
  13463. (beginning-of-line 1)
  13464. (if (looking-at ".*?\\([ \t]+\\)$")
  13465. (delete-region (match-beginning 1) (match-end 1))))))
  13466. (defun org-align-all-tags ()
  13467. "Align the tags i all headings."
  13468. (interactive)
  13469. (save-excursion
  13470. (or (ignore-errors (org-back-to-heading t))
  13471. (outline-next-heading))
  13472. (if (org-at-heading-p)
  13473. (org-set-tags t)
  13474. (message "No headings"))))
  13475. (defvar org-indent-indentation-per-level)
  13476. (defun org-set-tags (&optional arg just-align)
  13477. "Set the tags for the current headline.
  13478. With prefix ARG, realign all tags in headings in the current buffer.
  13479. When JUST-ALIGN is non-nil, only align tags."
  13480. (interactive "P")
  13481. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13482. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13483. 'region-start-level 'region))
  13484. org-loop-over-headlines-in-active-region)
  13485. (org-map-entries
  13486. ;; We don't use ARG and JUST-ALIGN here because these args
  13487. ;; are not useful when looping over headlines.
  13488. `(org-set-tags)
  13489. org-loop-over-headlines-in-active-region
  13490. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13491. (let* ((re org-outline-regexp-bol)
  13492. (current (unless arg (org-get-tags-string)))
  13493. (col (current-column))
  13494. (org-setting-tags t)
  13495. table current-tags inherited-tags ; computed below when needed
  13496. tags p0 c0 c1 rpl di tc level)
  13497. (if arg
  13498. (save-excursion
  13499. (goto-char (point-min))
  13500. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13501. (while (re-search-forward re nil t)
  13502. (org-set-tags nil t)
  13503. (end-of-line 1)))
  13504. (message "All tags realigned to column %d" org-tags-column))
  13505. (if just-align
  13506. (setq tags current)
  13507. ;; Get a new set of tags from the user
  13508. (save-excursion
  13509. (setq table (append org-tag-persistent-alist
  13510. (or org-tag-alist (org-get-buffer-tags))
  13511. (and
  13512. org-complete-tags-always-offer-all-agenda-tags
  13513. (org-global-tags-completion-table
  13514. (org-agenda-files))))
  13515. org-last-tags-completion-table table
  13516. current-tags (org-split-string current ":")
  13517. inherited-tags (nreverse
  13518. (nthcdr (length current-tags)
  13519. (nreverse (org-get-tags-at))))
  13520. tags
  13521. (if (or (eq t org-use-fast-tag-selection)
  13522. (and org-use-fast-tag-selection
  13523. (delq nil (mapcar 'cdr table))))
  13524. (org-fast-tag-selection
  13525. current-tags inherited-tags table
  13526. (if org-fast-tag-selection-include-todo
  13527. org-todo-key-alist))
  13528. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13529. (org-trim
  13530. (org-icompleting-read "Tags: "
  13531. 'org-tags-completion-function
  13532. nil nil current 'org-tags-history))))))
  13533. (while (string-match "[-+&]+" tags)
  13534. ;; No boolean logic, just a list
  13535. (setq tags (replace-match ":" t t tags))))
  13536. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13537. (if org-tags-sort-function
  13538. (setq tags (mapconcat 'identity
  13539. (sort (org-split-string
  13540. tags (org-re "[^[:alnum:]_@#%]+"))
  13541. org-tags-sort-function) ":")))
  13542. (if (string-match "\\`[\t ]*\\'" tags)
  13543. (setq tags "")
  13544. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13545. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13546. ;; Insert new tags at the correct column
  13547. (beginning-of-line 1)
  13548. (setq level (or (and (looking-at org-outline-regexp)
  13549. (- (match-end 0) (point) 1))
  13550. 1))
  13551. (cond
  13552. ((and (equal current "") (equal tags "")))
  13553. ((re-search-forward
  13554. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13555. (point-at-eol) t)
  13556. (if (equal tags "")
  13557. (setq rpl "")
  13558. (goto-char (match-beginning 0))
  13559. (setq c0 (current-column)
  13560. ;; compute offset for the case of org-indent-mode active
  13561. di (if (org-bound-and-true-p org-indent-mode)
  13562. (* (1- org-indent-indentation-per-level) (1- level))
  13563. 0)
  13564. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13565. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13566. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (string-width tags))))
  13567. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13568. (replace-match rpl t t)
  13569. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13570. tags)
  13571. (t (error "Tags alignment failed")))
  13572. (org-move-to-column col)
  13573. (unless just-align
  13574. (run-hooks 'org-after-tags-change-hook))))))
  13575. (defun org-change-tag-in-region (beg end tag off)
  13576. "Add or remove TAG for each entry in the region.
  13577. This works in the agenda, and also in an org-mode buffer."
  13578. (interactive
  13579. (list (region-beginning) (region-end)
  13580. (let ((org-last-tags-completion-table
  13581. (if (derived-mode-p 'org-mode)
  13582. (org-uniquify
  13583. (delq nil (append (org-get-buffer-tags)
  13584. (org-global-tags-completion-table))))
  13585. (org-global-tags-completion-table))))
  13586. (org-icompleting-read
  13587. "Tag: " 'org-tags-completion-function nil nil nil
  13588. 'org-tags-history))
  13589. (progn
  13590. (message "[s]et or [r]emove? ")
  13591. (equal (read-char-exclusive) ?r))))
  13592. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13593. (let ((agendap (equal major-mode 'org-agenda-mode))
  13594. l1 l2 m buf pos newhead (cnt 0))
  13595. (goto-char end)
  13596. (setq l2 (1- (org-current-line)))
  13597. (goto-char beg)
  13598. (setq l1 (org-current-line))
  13599. (loop for l from l1 to l2 do
  13600. (org-goto-line l)
  13601. (setq m (get-text-property (point) 'org-hd-marker))
  13602. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13603. (and agendap m))
  13604. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13605. pos (if agendap m (point)))
  13606. (with-current-buffer buf
  13607. (save-excursion
  13608. (save-restriction
  13609. (goto-char pos)
  13610. (setq cnt (1+ cnt))
  13611. (org-toggle-tag tag (if off 'off 'on))
  13612. (setq newhead (org-get-heading)))))
  13613. (and agendap (org-agenda-change-all-lines newhead m))))
  13614. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13615. (defun org-tags-completion-function (string predicate &optional flag)
  13616. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13617. (confirm (lambda (x) (stringp (car x)))))
  13618. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13619. (setq s1 (match-string 1 string)
  13620. s2 (match-string 2 string))
  13621. (setq s1 "" s2 string))
  13622. (cond
  13623. ((eq flag nil)
  13624. ;; try completion
  13625. (setq rtn (try-completion s2 ctable confirm))
  13626. (if (stringp rtn)
  13627. (setq rtn
  13628. (concat s1 s2 (substring rtn (length s2))
  13629. (if (and org-add-colon-after-tag-completion
  13630. (assoc rtn ctable))
  13631. ":" ""))))
  13632. rtn)
  13633. ((eq flag t)
  13634. ;; all-completions
  13635. (all-completions s2 ctable confirm))
  13636. ((eq flag 'lambda)
  13637. ;; exact match?
  13638. (assoc s2 ctable)))))
  13639. (defun org-fast-tag-insert (kwd tags face &optional end)
  13640. "Insert KDW, and the TAGS, the latter with face FACE.
  13641. Also insert END."
  13642. (insert (format "%-12s" (concat kwd ":"))
  13643. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13644. (or end "")))
  13645. (defun org-fast-tag-show-exit (flag)
  13646. (save-excursion
  13647. (org-goto-line 3)
  13648. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13649. (replace-match ""))
  13650. (when flag
  13651. (end-of-line 1)
  13652. (org-move-to-column (- (window-width) 19) t)
  13653. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13654. (defun org-set-current-tags-overlay (current prefix)
  13655. "Add an overlay to CURRENT tag with PREFIX."
  13656. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13657. (if (featurep 'xemacs)
  13658. (org-overlay-display org-tags-overlay (concat prefix s)
  13659. 'secondary-selection)
  13660. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13661. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13662. (defvar org-last-tag-selection-key nil)
  13663. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13664. "Fast tag selection with single keys.
  13665. CURRENT is the current list of tags in the headline, INHERITED is the
  13666. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13667. possibly with grouping information. TODO-TABLE is a similar table with
  13668. TODO keywords, should these have keys assigned to them.
  13669. If the keys are nil, a-z are automatically assigned.
  13670. Returns the new tags string, or nil to not change the current settings."
  13671. (let* ((fulltable (append table todo-table))
  13672. (maxlen (apply 'max (mapcar
  13673. (lambda (x)
  13674. (if (stringp (car x)) (string-width (car x)) 0))
  13675. fulltable)))
  13676. (buf (current-buffer))
  13677. (expert (eq org-fast-tag-selection-single-key 'expert))
  13678. (buffer-tags nil)
  13679. (fwidth (+ maxlen 3 1 3))
  13680. (ncol (/ (- (window-width) 4) fwidth))
  13681. (i-face 'org-done)
  13682. (c-face 'org-todo)
  13683. tg cnt e c char c1 c2 ntable tbl rtn
  13684. ov-start ov-end ov-prefix
  13685. (exit-after-next org-fast-tag-selection-single-key)
  13686. (done-keywords org-done-keywords)
  13687. groups ingroup)
  13688. (save-excursion
  13689. (beginning-of-line 1)
  13690. (if (looking-at
  13691. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13692. (setq ov-start (match-beginning 1)
  13693. ov-end (match-end 1)
  13694. ov-prefix "")
  13695. (setq ov-start (1- (point-at-eol))
  13696. ov-end (1+ ov-start))
  13697. (skip-chars-forward "^\n\r")
  13698. (setq ov-prefix
  13699. (concat
  13700. (buffer-substring (1- (point)) (point))
  13701. (if (> (current-column) org-tags-column)
  13702. " "
  13703. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13704. (move-overlay org-tags-overlay ov-start ov-end)
  13705. (save-window-excursion
  13706. (if expert
  13707. (set-buffer (get-buffer-create " *Org tags*"))
  13708. (delete-other-windows)
  13709. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13710. (org-switch-to-buffer-other-window " *Org tags*"))
  13711. (erase-buffer)
  13712. (org-set-local 'org-done-keywords done-keywords)
  13713. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13714. (org-fast-tag-insert "Current" current c-face "\n\n")
  13715. (org-fast-tag-show-exit exit-after-next)
  13716. (org-set-current-tags-overlay current ov-prefix)
  13717. (setq tbl fulltable char ?a cnt 0)
  13718. (while (setq e (pop tbl))
  13719. (cond
  13720. ((equal (car e) :startgroup)
  13721. (push '() groups) (setq ingroup t)
  13722. (when (not (= cnt 0))
  13723. (setq cnt 0)
  13724. (insert "\n"))
  13725. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13726. ((equal (car e) :endgroup)
  13727. (setq ingroup nil cnt 0)
  13728. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13729. ((equal e '(:newline))
  13730. (when (not (= cnt 0))
  13731. (setq cnt 0)
  13732. (insert "\n")
  13733. (setq e (car tbl))
  13734. (while (equal (car tbl) '(:newline))
  13735. (insert "\n")
  13736. (setq tbl (cdr tbl)))))
  13737. ((equal e '(:grouptags)) nil)
  13738. (t
  13739. (setq tg (copy-sequence (car e)) c2 nil)
  13740. (if (cdr e)
  13741. (setq c (cdr e))
  13742. ;; automatically assign a character.
  13743. (setq c1 (string-to-char
  13744. (downcase (substring
  13745. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13746. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13747. (while (or (rassoc char ntable) (rassoc char table))
  13748. (setq char (1+ char)))
  13749. (setq c2 c1))
  13750. (setq c (or c2 char)))
  13751. (if ingroup (push tg (car groups)))
  13752. (setq tg (org-add-props tg nil 'face
  13753. (cond
  13754. ((not (assoc tg table))
  13755. (org-get-todo-face tg))
  13756. ((member tg current) c-face)
  13757. ((member tg inherited) i-face))))
  13758. (if (equal (caar tbl) :grouptags)
  13759. (org-add-props tg nil 'face 'org-tag-group))
  13760. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13761. (insert "[" c "] " tg (make-string
  13762. (- fwidth 4 (length tg)) ?\ ))
  13763. (push (cons tg c) ntable)
  13764. (when (= (setq cnt (1+ cnt)) ncol)
  13765. (insert "\n")
  13766. (if ingroup (insert " "))
  13767. (setq cnt 0)))))
  13768. (setq ntable (nreverse ntable))
  13769. (insert "\n")
  13770. (goto-char (point-min))
  13771. (if (not expert) (org-fit-window-to-buffer))
  13772. (setq rtn
  13773. (catch 'exit
  13774. (while t
  13775. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13776. (if (not groups) "no " "")
  13777. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13778. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13779. (setq org-last-tag-selection-key c)
  13780. (cond
  13781. ((= c ?\r) (throw 'exit t))
  13782. ((= c ?!)
  13783. (setq groups (not groups))
  13784. (goto-char (point-min))
  13785. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13786. ((= c ?\C-c)
  13787. (if (not expert)
  13788. (org-fast-tag-show-exit
  13789. (setq exit-after-next (not exit-after-next)))
  13790. (setq expert nil)
  13791. (delete-other-windows)
  13792. (set-window-buffer (split-window-vertically) " *Org tags*")
  13793. (org-switch-to-buffer-other-window " *Org tags*")
  13794. (org-fit-window-to-buffer)))
  13795. ((or (= c ?\C-g)
  13796. (and (= c ?q) (not (rassoc c ntable))))
  13797. (org-detach-overlay org-tags-overlay)
  13798. (setq quit-flag t))
  13799. ((= c ?\ )
  13800. (setq current nil)
  13801. (if exit-after-next (setq exit-after-next 'now)))
  13802. ((= c ?\t)
  13803. (condition-case nil
  13804. (setq tg (org-icompleting-read
  13805. "Tag: "
  13806. (or buffer-tags
  13807. (with-current-buffer buf
  13808. (org-get-buffer-tags)))))
  13809. (quit (setq tg "")))
  13810. (when (string-match "\\S-" tg)
  13811. (add-to-list 'buffer-tags (list tg))
  13812. (if (member tg current)
  13813. (setq current (delete tg current))
  13814. (push tg current)))
  13815. (if exit-after-next (setq exit-after-next 'now)))
  13816. ((setq e (rassoc c todo-table) tg (car e))
  13817. (with-current-buffer buf
  13818. (save-excursion (org-todo tg)))
  13819. (if exit-after-next (setq exit-after-next 'now)))
  13820. ((setq e (rassoc c ntable) tg (car e))
  13821. (if (member tg current)
  13822. (setq current (delete tg current))
  13823. (loop for g in groups do
  13824. (if (member tg g)
  13825. (mapc (lambda (x)
  13826. (setq current (delete x current)))
  13827. g)))
  13828. (push tg current))
  13829. (if exit-after-next (setq exit-after-next 'now))))
  13830. ;; Create a sorted list
  13831. (setq current
  13832. (sort current
  13833. (lambda (a b)
  13834. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13835. (if (eq exit-after-next 'now) (throw 'exit t))
  13836. (goto-char (point-min))
  13837. (beginning-of-line 2)
  13838. (delete-region (point) (point-at-eol))
  13839. (org-fast-tag-insert "Current" current c-face)
  13840. (org-set-current-tags-overlay current ov-prefix)
  13841. (while (re-search-forward
  13842. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13843. (setq tg (match-string 1))
  13844. (add-text-properties
  13845. (match-beginning 1) (match-end 1)
  13846. (list 'face
  13847. (cond
  13848. ((member tg current) c-face)
  13849. ((member tg inherited) i-face)
  13850. (t (get-text-property (match-beginning 1) 'face))))))
  13851. (goto-char (point-min)))))
  13852. (org-detach-overlay org-tags-overlay)
  13853. (if rtn
  13854. (mapconcat 'identity current ":")
  13855. nil))))
  13856. (defun org-get-tags-string ()
  13857. "Get the TAGS string in the current headline."
  13858. (unless (org-at-heading-p t)
  13859. (user-error "Not on a heading"))
  13860. (save-excursion
  13861. (beginning-of-line 1)
  13862. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13863. (org-match-string-no-properties 1)
  13864. "")))
  13865. (defun org-get-tags ()
  13866. "Get the list of tags specified in the current headline."
  13867. (org-split-string (org-get-tags-string) ":"))
  13868. (defun org-get-buffer-tags ()
  13869. "Get a table of all tags used in the buffer, for completion."
  13870. (let (tags)
  13871. (save-excursion
  13872. (goto-char (point-min))
  13873. (while (re-search-forward
  13874. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13875. (when (equal (char-after (point-at-bol 0)) ?*)
  13876. (mapc (lambda (x) (add-to-list 'tags x))
  13877. (org-split-string (org-match-string-no-properties 1) ":")))))
  13878. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13879. (mapcar 'list tags)))
  13880. ;;;; The mapping API
  13881. (defun org-map-entries (func &optional match scope &rest skip)
  13882. "Call FUNC at each headline selected by MATCH in SCOPE.
  13883. FUNC is a function or a lisp form. The function will be called without
  13884. arguments, with the cursor positioned at the beginning of the headline.
  13885. The return values of all calls to the function will be collected and
  13886. returned as a list.
  13887. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13888. does not need to preserve point. After evaluation, the cursor will be
  13889. moved to the end of the line (presumably of the headline of the
  13890. processed entry) and search continues from there. Under some
  13891. circumstances, this may not produce the wanted results. For example,
  13892. if you have removed (e.g. archived) the current (sub)tree it could
  13893. mean that the next entry will be skipped entirely. In such cases, you
  13894. can specify the position from where search should continue by making
  13895. FUNC set the variable `org-map-continue-from' to the desired buffer
  13896. position.
  13897. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13898. Only headlines that are matched by this query will be considered during
  13899. the iteration. When MATCH is nil or t, all headlines will be
  13900. visited by the iteration.
  13901. SCOPE determines the scope of this command. It can be any of:
  13902. nil The current buffer, respecting the restriction if any
  13903. tree The subtree started with the entry at point
  13904. region The entries within the active region, if any
  13905. region-start-level
  13906. The entries within the active region, but only those at
  13907. the same level than the first one.
  13908. file The current buffer, without restriction
  13909. file-with-archives
  13910. The current buffer, and any archives associated with it
  13911. agenda All agenda files
  13912. agenda-with-archives
  13913. All agenda files with any archive files associated with them
  13914. \(file1 file2 ...)
  13915. If this is a list, all files in the list will be scanned
  13916. The remaining args are treated as settings for the skipping facilities of
  13917. the scanner. The following items can be given here:
  13918. archive skip trees with the archive tag
  13919. comment skip trees with the COMMENT keyword
  13920. function or Emacs Lisp form:
  13921. will be used as value for `org-agenda-skip-function', so
  13922. whenever the function returns a position, FUNC will not be
  13923. called for that entry and search will continue from the
  13924. position returned
  13925. If your function needs to retrieve the tags including inherited tags
  13926. at the *current* entry, you can use the value of the variable
  13927. `org-scanner-tags' which will be much faster than getting the value
  13928. with `org-get-tags-at'. If your function gets properties with
  13929. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13930. to t around the call to `org-entry-properties' to get the same speedup.
  13931. Note that if your function moves around to retrieve tags and properties at
  13932. a *different* entry, you cannot use these techniques."
  13933. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13934. (not (org-region-active-p)))
  13935. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13936. (org-agenda-skip-archived-trees (memq 'archive skip))
  13937. (org-agenda-skip-comment-trees (memq 'comment skip))
  13938. (org-agenda-skip-function
  13939. (car (org-delete-all '(comment archive) skip)))
  13940. (org-tags-match-list-sublevels t)
  13941. (start-level (eq scope 'region-start-level))
  13942. matcher file res
  13943. org-todo-keywords-for-agenda
  13944. org-done-keywords-for-agenda
  13945. org-todo-keyword-alist-for-agenda
  13946. org-tag-alist-for-agenda
  13947. todo-only)
  13948. (cond
  13949. ((eq match t) (setq matcher t))
  13950. ((eq match nil) (setq matcher t))
  13951. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13952. (save-excursion
  13953. (save-restriction
  13954. (cond ((eq scope 'tree)
  13955. (org-back-to-heading t)
  13956. (org-narrow-to-subtree)
  13957. (setq scope nil))
  13958. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13959. (org-region-active-p))
  13960. ;; If needed, set start-level to a string like "2"
  13961. (when start-level
  13962. (save-excursion
  13963. (goto-char (region-beginning))
  13964. (unless (org-at-heading-p) (outline-next-heading))
  13965. (setq start-level (org-current-level))))
  13966. (narrow-to-region (region-beginning)
  13967. (save-excursion
  13968. (goto-char (region-end))
  13969. (unless (and (bolp) (org-at-heading-p))
  13970. (outline-next-heading))
  13971. (point)))
  13972. (setq scope nil)))
  13973. (if (not scope)
  13974. (progn
  13975. (org-agenda-prepare-buffers
  13976. (list (buffer-file-name (current-buffer))))
  13977. (setq res (org-scan-tags func matcher todo-only start-level)))
  13978. ;; Get the right scope
  13979. (cond
  13980. ((and scope (listp scope) (symbolp (car scope)))
  13981. (setq scope (eval scope)))
  13982. ((eq scope 'agenda)
  13983. (setq scope (org-agenda-files t)))
  13984. ((eq scope 'agenda-with-archives)
  13985. (setq scope (org-agenda-files t))
  13986. (setq scope (org-add-archive-files scope)))
  13987. ((eq scope 'file)
  13988. (setq scope (list (buffer-file-name))))
  13989. ((eq scope 'file-with-archives)
  13990. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13991. (org-agenda-prepare-buffers scope)
  13992. (while (setq file (pop scope))
  13993. (with-current-buffer (org-find-base-buffer-visiting file)
  13994. (save-excursion
  13995. (save-restriction
  13996. (widen)
  13997. (goto-char (point-min))
  13998. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13999. res)))
  14000. ;;;; Properties
  14001. ;;; Setting and retrieving properties
  14002. (defconst org-special-properties
  14003. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  14004. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  14005. "The special properties valid in Org-mode.
  14006. These are properties that are not defined in the property drawer,
  14007. but in some other way.")
  14008. (defconst org-default-properties
  14009. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14010. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14011. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14012. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14013. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  14014. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14015. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14016. "Some properties that are used by Org-mode for various purposes.
  14017. Being in this list makes sure that they are offered for completion.")
  14018. (defun org-property-action ()
  14019. "Do an action on properties."
  14020. (interactive)
  14021. (let (c)
  14022. (org-at-property-p)
  14023. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14024. (setq c (read-char-exclusive))
  14025. (cond
  14026. ((equal c ?s)
  14027. (call-interactively 'org-set-property))
  14028. ((equal c ?d)
  14029. (call-interactively 'org-delete-property))
  14030. ((equal c ?D)
  14031. (call-interactively 'org-delete-property-globally))
  14032. ((equal c ?c)
  14033. (call-interactively 'org-compute-property-at-point))
  14034. (t (user-error "No such property action %c" c)))))
  14035. (defun org-inc-effort ()
  14036. "Increment the value of the effort property in the current entry."
  14037. (interactive)
  14038. (org-set-effort nil t))
  14039. (defvar org-clock-effort) ;; Defined in org-clock.el
  14040. (defvar org-clock-current-task) ;; Defined in org-clock.el
  14041. (defun org-set-effort (&optional value increment)
  14042. "Set the effort property of the current entry.
  14043. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14044. 10th allowed value.
  14045. When INCREMENT is non-nil, set the property to the next allowed value."
  14046. (interactive "P")
  14047. (if (equal value 0) (setq value 10))
  14048. (let* ((completion-ignore-case t)
  14049. (prop org-effort-property)
  14050. (cur (org-entry-get nil prop))
  14051. (allowed (org-property-get-allowed-values nil prop 'table))
  14052. (existing (mapcar 'list (org-property-values prop)))
  14053. (heading (nth 4 (org-heading-components)))
  14054. rpl
  14055. (val (cond
  14056. ((stringp value) value)
  14057. ((and allowed (integerp value))
  14058. (or (car (nth (1- value) allowed))
  14059. (car (org-last allowed))))
  14060. ((and allowed increment)
  14061. (or (caadr (member (list cur) allowed))
  14062. (user-error "Allowed effort values are not set")))
  14063. (allowed
  14064. (message "Select 1-9,0, [RET%s]: %s"
  14065. (if cur (concat "=" cur) "")
  14066. (mapconcat 'car allowed " "))
  14067. (setq rpl (read-char-exclusive))
  14068. (if (equal rpl ?\r)
  14069. cur
  14070. (setq rpl (- rpl ?0))
  14071. (if (equal rpl 0) (setq rpl 10))
  14072. (if (and (> rpl 0) (<= rpl (length allowed)))
  14073. (car (nth (1- rpl) allowed))
  14074. (org-completing-read "Effort: " allowed nil))))
  14075. (t
  14076. (let (org-completion-use-ido org-completion-use-iswitchb)
  14077. (org-completing-read
  14078. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14079. (concat "[" cur "]") "")
  14080. ": ")
  14081. existing nil nil "" nil cur))))))
  14082. (unless (equal (org-entry-get nil prop) val)
  14083. (org-entry-put nil prop val))
  14084. (org-refresh-property
  14085. '((effort . identity)
  14086. (effort-minutes . org-duration-string-to-minutes))
  14087. val)
  14088. (when (string= heading org-clock-current-task)
  14089. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14090. (org-clock-update-mode-line))
  14091. (message "%s is now %s" prop val)))
  14092. (defun org-at-property-p ()
  14093. "Non-nil when point is inside a property drawer.
  14094. See `org-property-re' for match data, if applicable."
  14095. (when (eq (org-element-type (org-element-at-point)) 'node-property)
  14096. (save-excursion
  14097. (beginning-of-line)
  14098. (looking-at org-property-re))))
  14099. (defun org-get-property-block (&optional beg end force)
  14100. "Return the (beg . end) range of the body of the property drawer.
  14101. BEG and END are the beginning and end of the current subtree, or of
  14102. the part before the first headline. If they are not given, they will
  14103. be found. If the drawer does not exist and FORCE is non-nil, create
  14104. the drawer."
  14105. (catch 'exit
  14106. (save-excursion
  14107. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  14108. (progn (org-back-to-heading t) (point))))
  14109. (end (or end (and (not (outline-next-heading)) (point-max))
  14110. (point))))
  14111. (goto-char beg)
  14112. (if (re-search-forward org-property-start-re end t)
  14113. (setq beg (1+ (match-end 0)))
  14114. (if force
  14115. (save-excursion
  14116. (org-insert-property-drawer)
  14117. (setq end (progn (outline-next-heading) (point))))
  14118. (throw 'exit nil))
  14119. (goto-char beg)
  14120. (if (re-search-forward org-property-start-re end t)
  14121. (setq beg (1+ (match-end 0)))))
  14122. (if (re-search-forward org-property-end-re end t)
  14123. (setq end (match-beginning 0))
  14124. (or force (throw 'exit nil))
  14125. (goto-char beg)
  14126. (setq end beg)
  14127. (org-indent-line)
  14128. (insert ":END:\n"))
  14129. (cons beg end)))))
  14130. (defun org-entry-properties (&optional pom which specific)
  14131. "Get all properties of the entry at point-or-marker POM.
  14132. This includes the TODO keyword, the tags, time strings for deadline,
  14133. scheduled, and clocking, and any additional properties defined in the
  14134. entry. The return value is an alist, keys may occur multiple times
  14135. if the property key was used several times.
  14136. POM may also be nil, in which case the current entry is used.
  14137. If WHICH is nil or `all', get all properties. If WHICH is
  14138. `special' or `standard', only get that subclass. If WHICH
  14139. is a string only get exactly this property. SPECIFIC can be a string, the
  14140. specific property we are interested in. Specifying it can speed
  14141. things up because then unnecessary parsing is avoided."
  14142. (setq which (or which 'all))
  14143. (org-with-wide-buffer
  14144. (org-with-point-at pom
  14145. (let ((clockstr (substring org-clock-string 0 -1))
  14146. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  14147. (case-fold-search nil)
  14148. beg end range props sum-props key key1 value string clocksum clocksumt)
  14149. (when (and (derived-mode-p 'org-mode)
  14150. (ignore-errors (org-back-to-heading t)))
  14151. (setq beg (point))
  14152. (setq sum-props (get-text-property (point) 'org-summaries))
  14153. (setq clocksum (get-text-property (point) :org-clock-minutes)
  14154. clocksumt (get-text-property (point) :org-clock-minutes-today))
  14155. (outline-next-heading)
  14156. (setq end (point))
  14157. (when (memq which '(all special))
  14158. ;; Get the special properties, like TODO and tags
  14159. (goto-char beg)
  14160. (when (and (or (not specific) (string= specific "TODO"))
  14161. (looking-at org-todo-line-regexp) (match-end 2))
  14162. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14163. (when (and (or (not specific) (string= specific "PRIORITY"))
  14164. (looking-at org-priority-regexp))
  14165. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14166. (when (or (not specific) (string= specific "FILE"))
  14167. (push (cons "FILE" buffer-file-name) props))
  14168. (when (and (or (not specific) (string= specific "TAGS"))
  14169. (setq value (org-get-tags-string))
  14170. (string-match "\\S-" value))
  14171. (push (cons "TAGS" value) props))
  14172. (when (and (or (not specific) (string= specific "ALLTAGS"))
  14173. (setq value (org-get-tags-at)))
  14174. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  14175. ":"))
  14176. props))
  14177. (when (or (not specific) (string= specific "BLOCKED"))
  14178. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  14179. (when (or (not specific)
  14180. (member specific
  14181. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  14182. "TIMESTAMP" "TIMESTAMP_IA")))
  14183. (catch 'match
  14184. (while (and (re-search-forward org-maybe-keyword-time-regexp end t)
  14185. (not (text-property-any 0 (length (match-string 0))
  14186. 'face 'font-lock-comment-face
  14187. (match-string 0))))
  14188. (setq key (if (match-end 1)
  14189. (substring (org-match-string-no-properties 1)
  14190. 0 -1))
  14191. string (if (equal key clockstr)
  14192. (org-trim
  14193. (buffer-substring-no-properties
  14194. (match-beginning 3) (goto-char
  14195. (point-at-eol))))
  14196. (substring (org-match-string-no-properties 3)
  14197. 1 -1)))
  14198. ;; Get the correct property name from the key. This is
  14199. ;; necessary if the user has configured time keywords.
  14200. (setq key1 (concat key ":"))
  14201. (cond
  14202. ((not key)
  14203. (setq key
  14204. (if (= (char-after (match-beginning 3)) ?\[)
  14205. "TIMESTAMP_IA" "TIMESTAMP")))
  14206. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  14207. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  14208. ((equal key1 org-closed-string) (setq key "CLOSED"))
  14209. ((equal key1 org-clock-string) (setq key "CLOCK")))
  14210. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  14211. (progn
  14212. (push (cons key string) props)
  14213. ;; no need to search further if match is found
  14214. (throw 'match t))
  14215. (when (or (equal key "CLOCK") (not (assoc key props)))
  14216. (push (cons key string) props)))))))
  14217. (when (memq which '(all standard))
  14218. ;; Get the standard properties, like :PROP: ...
  14219. (setq range (org-get-property-block beg end))
  14220. (when range
  14221. (goto-char (car range))
  14222. (while (re-search-forward org-property-re
  14223. (cdr range) t)
  14224. (setq key (org-match-string-no-properties 2)
  14225. value (org-trim (or (org-match-string-no-properties 3) "")))
  14226. (unless (member key excluded)
  14227. (push (cons key (or value "")) props)))))
  14228. (if clocksum
  14229. (push (cons "CLOCKSUM"
  14230. (org-columns-number-to-string (/ (float clocksum) 60.)
  14231. 'add_times))
  14232. props))
  14233. (if clocksumt
  14234. (push (cons "CLOCKSUM_T"
  14235. (org-columns-number-to-string (/ (float clocksumt) 60.)
  14236. 'add_times))
  14237. props))
  14238. (unless (assoc "CATEGORY" props)
  14239. (push (cons "CATEGORY" (org-get-category)) props))
  14240. (append sum-props (nreverse props)))))))
  14241. (defun org-entry-get (pom property &optional inherit literal-nil)
  14242. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14243. If INHERIT is non-nil and the entry does not have the property,
  14244. then also check higher levels of the hierarchy.
  14245. If INHERIT is the symbol `selective', use inheritance only if the setting
  14246. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  14247. If the property is present but empty, the return value is the empty string.
  14248. If the property is not present at all, nil is returned.
  14249. Return the value as a string.
  14250. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  14251. do not interpret it as the list atom nil. This is used for inheritance
  14252. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  14253. (org-with-point-at pom
  14254. (if (and inherit (if (eq inherit 'selective)
  14255. (org-property-inherit-p property)
  14256. t))
  14257. (org-entry-get-with-inheritance property literal-nil)
  14258. (if (member property org-special-properties)
  14259. ;; We need a special property. Use `org-entry-properties'
  14260. ;; to retrieve it, but specify the wanted property
  14261. (cdr (assoc property (org-entry-properties nil 'special property)))
  14262. (org-with-wide-buffer
  14263. (let ((range (org-get-property-block)))
  14264. (when (and range (not (eq (car range) (cdr range)))
  14265. (save-excursion
  14266. (goto-char (car range))
  14267. (re-search-forward
  14268. (concat (org-re-property property) "\\|"
  14269. (org-re-property (concat property "+")))
  14270. (cdr range) t)))
  14271. (let* ((props
  14272. (list (or (assoc property org-file-properties)
  14273. (assoc property org-global-properties)
  14274. (assoc property org-global-properties-fixed))))
  14275. (ap (lambda (key)
  14276. (when (re-search-forward
  14277. (org-re-property key) (cdr range) t)
  14278. (setq props
  14279. (org-update-property-plist
  14280. key
  14281. (if (match-end 3)
  14282. (org-match-string-no-properties 3) "")
  14283. props)))))
  14284. val)
  14285. (goto-char (car range))
  14286. (funcall ap property)
  14287. (goto-char (car range))
  14288. (while (funcall ap (concat property "+")))
  14289. (setq val (cdr (assoc property props)))
  14290. (when val (if literal-nil val (org-not-nil val)))))))))))
  14291. (defun org-property-or-variable-value (var &optional inherit)
  14292. "Check if there is a property fixing the value of VAR.
  14293. If yes, return this value. If not, return the current value of the variable."
  14294. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14295. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14296. (read prop)
  14297. (symbol-value var))))
  14298. (defun org-entry-delete (pom property)
  14299. "Delete the property PROPERTY from entry at point-or-marker POM."
  14300. (unless (member property org-special-properties)
  14301. (org-with-point-at pom
  14302. (let ((range (org-get-property-block)))
  14303. (if (and range
  14304. (goto-char (car range))
  14305. (re-search-forward
  14306. (org-re-property property nil t)
  14307. (cdr range) t))
  14308. (progn
  14309. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14310. (org-remove-empty-drawer-at (car range))
  14311. t)
  14312. nil)))))
  14313. ;; Multi-values properties are properties that contain multiple values
  14314. ;; These values are assumed to be single words, separated by whitespace.
  14315. (defun org-entry-add-to-multivalued-property (pom property value)
  14316. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14317. (let* ((old (org-entry-get pom property))
  14318. (values (and old (org-split-string old "[ \t]"))))
  14319. (setq value (org-entry-protect-space value))
  14320. (unless (member value values)
  14321. (setq values (append values (list value)))
  14322. (org-entry-put pom property
  14323. (mapconcat 'identity values " ")))))
  14324. (defun org-entry-remove-from-multivalued-property (pom property value)
  14325. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14326. (let* ((old (org-entry-get pom property))
  14327. (values (and old (org-split-string old "[ \t]"))))
  14328. (setq value (org-entry-protect-space value))
  14329. (when (member value values)
  14330. (setq values (delete value values))
  14331. (org-entry-put pom property
  14332. (mapconcat 'identity values " ")))))
  14333. (defun org-entry-member-in-multivalued-property (pom property value)
  14334. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14335. (let* ((old (org-entry-get pom property))
  14336. (values (and old (org-split-string old "[ \t]"))))
  14337. (setq value (org-entry-protect-space value))
  14338. (member value values)))
  14339. (defun org-entry-get-multivalued-property (pom property)
  14340. "Return a list of values in a multivalued property."
  14341. (let* ((value (org-entry-get pom property))
  14342. (values (and value (org-split-string value "[ \t]"))))
  14343. (mapcar 'org-entry-restore-space values)))
  14344. (defun org-entry-put-multivalued-property (pom property &rest values)
  14345. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14346. VALUES should be a list of strings. Spaces will be protected."
  14347. (org-entry-put pom property
  14348. (mapconcat 'org-entry-protect-space values " "))
  14349. (let* ((value (org-entry-get pom property))
  14350. (values (and value (org-split-string value "[ \t]"))))
  14351. (mapcar 'org-entry-restore-space values)))
  14352. (defun org-entry-protect-space (s)
  14353. "Protect spaces and newline in string S."
  14354. (while (string-match " " s)
  14355. (setq s (replace-match "%20" t t s)))
  14356. (while (string-match "\n" s)
  14357. (setq s (replace-match "%0A" t t s)))
  14358. s)
  14359. (defun org-entry-restore-space (s)
  14360. "Restore spaces and newline in string S."
  14361. (while (string-match "%20" s)
  14362. (setq s (replace-match " " t t s)))
  14363. (while (string-match "%0A" s)
  14364. (setq s (replace-match "\n" t t s)))
  14365. s)
  14366. (defvar org-entry-property-inherited-from (make-marker)
  14367. "Marker pointing to the entry from where a property was inherited.
  14368. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14369. location of the entry where the inheritance search matched. If there was
  14370. no match, the marker will point nowhere.
  14371. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14372. is set.")
  14373. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14374. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14375. The search will stop at the first ancestor which has the property defined.
  14376. If the value found is \"nil\", return nil to show that the property
  14377. should be considered as undefined (this is the meaning of nil here).
  14378. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14379. (move-marker org-entry-property-inherited-from nil)
  14380. (let (tmp)
  14381. (save-excursion
  14382. (save-restriction
  14383. (widen)
  14384. (catch 'ex
  14385. (while t
  14386. (when (setq tmp (org-entry-get nil property nil literal-nil))
  14387. (or (ignore-errors (org-back-to-heading t))
  14388. (goto-char (point-min)))
  14389. (move-marker org-entry-property-inherited-from (point))
  14390. (throw 'ex tmp))
  14391. (or (ignore-errors (org-up-heading-safe))
  14392. (throw 'ex nil))))))
  14393. (setq tmp (or tmp
  14394. (cdr (assoc property org-file-properties))
  14395. (cdr (assoc property org-global-properties))
  14396. (cdr (assoc property org-global-properties-fixed))))
  14397. (if literal-nil tmp (org-not-nil tmp))))
  14398. (defvar org-property-changed-functions nil
  14399. "Hook called when the value of a property has changed.
  14400. Each hook function should accept two arguments, the name of the property
  14401. and the new value.")
  14402. (defun org-entry-put (pom property value)
  14403. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14404. If the value is `nil', it is converted to the empty string.
  14405. If it is not a string, an error is raised."
  14406. (cond ((null value) (setq value ""))
  14407. ((not (stringp value))
  14408. (error "Properties values should be strings.")))
  14409. (org-with-point-at pom
  14410. (org-back-to-heading t)
  14411. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14412. range)
  14413. (cond
  14414. ((equal property "TODO")
  14415. (when (and (string-match "\\S-" value)
  14416. (not (member value org-todo-keywords-1)))
  14417. (user-error "\"%s\" is not a valid TODO state" value))
  14418. (if (or (not value)
  14419. (not (string-match "\\S-" value)))
  14420. (setq value 'none))
  14421. (org-todo value)
  14422. (org-set-tags nil 'align))
  14423. ((equal property "PRIORITY")
  14424. (org-priority (if (and value (string-match "\\S-" value))
  14425. (string-to-char value) ?\ ))
  14426. (org-set-tags nil 'align))
  14427. ((equal property "CLOCKSUM")
  14428. (if (not (re-search-forward
  14429. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14430. (error "Cannot find a clock log")
  14431. (goto-char (- (match-end 1) 2))
  14432. (cond
  14433. ((eq value 'earlier) (org-timestamp-down))
  14434. ((eq value 'later) (org-timestamp-up)))
  14435. (org-clock-sum-current-item)))
  14436. ((equal property "SCHEDULED")
  14437. (if (re-search-forward org-scheduled-time-regexp end t)
  14438. (cond
  14439. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14440. ((eq value 'later) (org-timestamp-change 1 'day))
  14441. (t (call-interactively 'org-schedule)))
  14442. (call-interactively 'org-schedule)))
  14443. ((equal property "DEADLINE")
  14444. (if (re-search-forward org-deadline-time-regexp end t)
  14445. (cond
  14446. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14447. ((eq value 'later) (org-timestamp-change 1 'day))
  14448. (t (call-interactively 'org-deadline)))
  14449. (call-interactively 'org-deadline)))
  14450. ((member property org-special-properties)
  14451. (error "The %s property can not yet be set with `org-entry-put'"
  14452. property))
  14453. (t ; a non-special property
  14454. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14455. (setq range (org-get-property-block beg end 'force))
  14456. (goto-char (car range))
  14457. (if (re-search-forward
  14458. (org-re-property property nil t) (cdr range) t)
  14459. (progn
  14460. (delete-region (match-beginning 0) (match-end 0))
  14461. (goto-char (match-beginning 0)))
  14462. (goto-char (cdr range))
  14463. (insert "\n")
  14464. (backward-char 1)
  14465. (org-indent-line))
  14466. (insert ":" property ":")
  14467. (and value (insert " " value))
  14468. (org-indent-line)))))
  14469. (run-hook-with-args 'org-property-changed-functions property value)))
  14470. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14471. "Get all property keys in the current buffer.
  14472. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14473. like tags and TODO state.
  14474. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14475. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14476. and others.
  14477. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14478. formats in the current buffer."
  14479. (let (rtn range cfmt s p)
  14480. (save-excursion
  14481. (save-restriction
  14482. (widen)
  14483. (goto-char (point-min))
  14484. (while (re-search-forward org-property-start-re nil t)
  14485. (catch 'cont
  14486. (setq range (or (org-get-property-block)
  14487. (if (y-or-n-p
  14488. (format "Malformed drawer at %d, repair?" (point)))
  14489. (org-get-property-block nil nil t)
  14490. (throw 'cont nil))))
  14491. (goto-char (car range))
  14492. (while (re-search-forward org-property-re
  14493. (cdr range) t)
  14494. (add-to-list 'rtn (org-match-string-no-properties 2)))
  14495. (outline-next-heading)))))
  14496. (when include-specials
  14497. (setq rtn (append org-special-properties rtn)))
  14498. (when include-defaults
  14499. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14500. (add-to-list 'rtn org-effort-property))
  14501. (when include-columns
  14502. (save-excursion
  14503. (save-restriction
  14504. (widen)
  14505. (goto-char (point-min))
  14506. (while (re-search-forward
  14507. "^[ \t]*\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14508. nil t)
  14509. (setq cfmt (match-string 2) s 0)
  14510. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14511. cfmt s)
  14512. (setq s (match-end 0)
  14513. p (match-string 1 cfmt))
  14514. (unless (or (equal p "ITEM")
  14515. (member p org-special-properties))
  14516. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14517. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14518. (defun org-property-values (key)
  14519. "Return a list of all values of property KEY in the current buffer."
  14520. (save-excursion
  14521. (save-restriction
  14522. (widen)
  14523. (goto-char (point-min))
  14524. (let ((re (org-re-property key))
  14525. values)
  14526. (while (re-search-forward re nil t)
  14527. (add-to-list 'values (org-trim (match-string 3))))
  14528. (delete "" values)))))
  14529. (defun org-insert-property-drawer ()
  14530. "Insert a property drawer into the current entry."
  14531. (org-back-to-heading t)
  14532. (looking-at org-outline-regexp)
  14533. (let ((indent (if org-adapt-indentation
  14534. (- (match-end 0) (match-beginning 0))
  14535. 0))
  14536. (beg (point))
  14537. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14538. (org-clock-re (format "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|%s\\|:END:\\)"
  14539. org-clock-string))
  14540. (org-skip-line-list (list org-clock-string ":END:"))
  14541. end hiddenp)
  14542. (outline-next-heading)
  14543. (setq end (point))
  14544. (goto-char beg)
  14545. (while (re-search-forward re end t))
  14546. (setq hiddenp (outline-invisible-p))
  14547. (end-of-line 1)
  14548. (and (equal (char-after) ?\n) (forward-char 1))
  14549. (while (looking-at org-clock-re)
  14550. (if (member (match-string 1) org-skip-line-list)
  14551. ;; just skip this line
  14552. (beginning-of-line 2)
  14553. ;; Drawer start, find the end
  14554. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14555. (beginning-of-line 1)))
  14556. (org-skip-over-state-notes)
  14557. (skip-chars-backward " \t\n\r")
  14558. (if (and (eq (char-before) ?*) (not (eq (char-after) ?\n)))
  14559. (forward-char 1))
  14560. (goto-char (point-at-eol))
  14561. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14562. (beginning-of-line 0)
  14563. (org-indent-to-column indent)
  14564. (beginning-of-line 2)
  14565. (org-indent-to-column indent)
  14566. (beginning-of-line 0)
  14567. (if hiddenp
  14568. (save-excursion
  14569. (org-back-to-heading t)
  14570. (hide-entry))
  14571. (org-flag-drawer t))))
  14572. (defun org-insert-drawer (&optional arg drawer)
  14573. "Insert a drawer at point.
  14574. Optional argument DRAWER, when non-nil, is a string representing
  14575. drawer's name. Otherwise, the user is prompted for a name.
  14576. If a region is active, insert the drawer around that region
  14577. instead.
  14578. Point is left between drawer's boundaries."
  14579. (interactive "P")
  14580. (let* ((drawer (if arg "PROPERTIES"
  14581. (or drawer (read-from-minibuffer "Drawer: ")))))
  14582. (cond
  14583. ;; With C-u, fall back on `org-insert-property-drawer'
  14584. (arg (org-insert-property-drawer))
  14585. ;;
  14586. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14587. (user-error "Invalid drawer name"))
  14588. ;; With an active region, insert a drawer at point.
  14589. ((not (org-region-active-p))
  14590. (progn
  14591. (unless (bolp) (insert "\n"))
  14592. (insert (format ":%s:\n\n:END:\n" drawer))
  14593. (forward-line -2)))
  14594. ;; Otherwise, insert the drawer at point
  14595. (t
  14596. (let ((rbeg (region-beginning))
  14597. (rend (copy-marker (region-end))))
  14598. (unwind-protect
  14599. (progn
  14600. (goto-char rbeg)
  14601. (beginning-of-line)
  14602. (when (save-excursion
  14603. (re-search-forward org-outline-regexp-bol rend t))
  14604. (user-error "Drawers cannot contain headlines"))
  14605. ;; Position point at the beginning of the first
  14606. ;; non-blank line in region. Insert drawer's opening
  14607. ;; there, then indent it.
  14608. (org-skip-whitespace)
  14609. (beginning-of-line)
  14610. (insert ":" drawer ":\n")
  14611. (forward-line -1)
  14612. (indent-for-tab-command)
  14613. ;; Move point to the beginning of the first blank line
  14614. ;; after the last non-blank line in region. Insert
  14615. ;; drawer's closing, then indent it.
  14616. (goto-char rend)
  14617. (skip-chars-backward " \r\t\n")
  14618. (insert "\n:END:")
  14619. (deactivate-mark t)
  14620. (indent-for-tab-command)
  14621. (unless (eolp) (insert "\n")))
  14622. ;; Clear marker, whatever the outcome of insertion is.
  14623. (set-marker rend nil)))))))
  14624. (defvar org-property-set-functions-alist nil
  14625. "Property set function alist.
  14626. Each entry should have the following format:
  14627. (PROPERTY . READ-FUNCTION)
  14628. The read function will be called with the same argument as
  14629. `org-completing-read'.")
  14630. (defun org-set-property-function (property)
  14631. "Get the function that should be used to set PROPERTY.
  14632. This is computed according to `org-property-set-functions-alist'."
  14633. (or (cdr (assoc property org-property-set-functions-alist))
  14634. 'org-completing-read))
  14635. (defun org-read-property-value (property)
  14636. "Read PROPERTY value from user."
  14637. (let* ((completion-ignore-case t)
  14638. (allowed (org-property-get-allowed-values nil property 'table))
  14639. (cur (org-entry-get nil property))
  14640. (prompt (concat property " value"
  14641. (if (and cur (string-match "\\S-" cur))
  14642. (concat " [" cur "]") "") ": "))
  14643. (set-function (org-set-property-function property))
  14644. (val (if allowed
  14645. (funcall set-function prompt allowed nil
  14646. (not (get-text-property 0 'org-unrestricted
  14647. (caar allowed))))
  14648. (let (org-completion-use-ido org-completion-use-iswitchb)
  14649. (funcall set-function prompt
  14650. (mapcar 'list (org-property-values property))
  14651. nil nil "" nil cur)))))
  14652. (org-trim val)))
  14653. (defvar org-last-set-property nil)
  14654. (defvar org-last-set-property-value nil)
  14655. (defun org-read-property-name ()
  14656. "Read a property name."
  14657. (let* ((completion-ignore-case t)
  14658. (keys (org-buffer-property-keys nil t t))
  14659. (default-prop (or (save-excursion
  14660. (save-match-data
  14661. (beginning-of-line)
  14662. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14663. (null (string= (match-string 1) "END"))
  14664. (match-string 1))))
  14665. org-last-set-property))
  14666. (property (org-icompleting-read
  14667. (concat "Property"
  14668. (if default-prop (concat " [" default-prop "]") "")
  14669. ": ")
  14670. (mapcar 'list keys)
  14671. nil nil nil nil
  14672. default-prop)))
  14673. (if (member property keys)
  14674. property
  14675. (or (cdr (assoc (downcase property)
  14676. (mapcar (lambda (x) (cons (downcase x) x))
  14677. keys)))
  14678. property))))
  14679. (defun org-set-property-and-value (use-last)
  14680. "Allow to set [PROPERTY]: [value] direction from prompt.
  14681. When use-default, don't even ask, just use the last
  14682. \"[PROPERTY]: [value]\" string from the history."
  14683. (interactive "P")
  14684. (let* ((completion-ignore-case t)
  14685. (pv (or (and use-last org-last-set-property-value)
  14686. (org-completing-read
  14687. "Enter a \"[Property]: [value]\" pair: "
  14688. nil nil nil nil nil
  14689. org-last-set-property-value)))
  14690. prop val)
  14691. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14692. (setq prop (match-string 1 pv)
  14693. val (match-string 2 pv))
  14694. (org-set-property prop val))))
  14695. (defun org-set-property (property value)
  14696. "In the current entry, set PROPERTY to VALUE.
  14697. When called interactively, this will prompt for a property name, offering
  14698. completion on existing and default properties. And then it will prompt
  14699. for a value, offering completion either on allowed values (via an inherited
  14700. xxx_ALL property) or on existing values in other instances of this property
  14701. in the current file."
  14702. (interactive (list nil nil))
  14703. (let* ((property (or property (org-read-property-name)))
  14704. (value (or value (org-read-property-value property)))
  14705. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14706. (setq org-last-set-property property)
  14707. (setq org-last-set-property-value (concat property ": " value))
  14708. ;; Possibly postprocess the inserted value:
  14709. (when fn (setq value (funcall fn value)))
  14710. (unless (equal (org-entry-get nil property) value)
  14711. (org-entry-put nil property value))))
  14712. (defun org-delete-property (property)
  14713. "In the current entry, delete PROPERTY."
  14714. (interactive
  14715. (let* ((completion-ignore-case t)
  14716. (cat (org-entry-get (point) "CATEGORY"))
  14717. (props0 (org-entry-properties nil 'standard))
  14718. (props (if cat props0
  14719. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14720. (prop (if (< 1 (length props))
  14721. (org-icompleting-read "Property: " props nil t)
  14722. (caar props))))
  14723. (list prop)))
  14724. (if (not property)
  14725. (message "No property to delete in this entry")
  14726. (org-entry-delete nil property)
  14727. (message "Property \"%s\" deleted" property)))
  14728. (defun org-delete-property-globally (property)
  14729. "Remove PROPERTY globally, from all entries."
  14730. (interactive
  14731. (let* ((completion-ignore-case t)
  14732. (prop (org-icompleting-read
  14733. "Globally remove property: "
  14734. (mapcar 'list (org-buffer-property-keys)))))
  14735. (list prop)))
  14736. (save-excursion
  14737. (save-restriction
  14738. (widen)
  14739. (goto-char (point-min))
  14740. (let ((cnt 0))
  14741. (while (re-search-forward
  14742. (org-re-property property)
  14743. nil t)
  14744. (setq cnt (1+ cnt))
  14745. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14746. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14747. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14748. (defun org-compute-property-at-point ()
  14749. "Compute the property at point.
  14750. This looks for an enclosing column format, extracts the operator and
  14751. then applies it to the property in the column format's scope."
  14752. (interactive)
  14753. (unless (org-at-property-p)
  14754. (user-error "Not at a property"))
  14755. (let ((prop (org-match-string-no-properties 2)))
  14756. (org-columns-get-format-and-top-level)
  14757. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14758. (user-error "No operator defined for property %s" prop))
  14759. (org-columns-compute prop)))
  14760. (defvar org-property-allowed-value-functions nil
  14761. "Hook for functions supplying allowed values for a specific property.
  14762. The functions must take a single argument, the name of the property, and
  14763. return a flat list of allowed values. If \":ETC\" is one of
  14764. the values, this means that these values are intended as defaults for
  14765. completion, but that other values should be allowed too.
  14766. The functions must return nil if they are not responsible for this
  14767. property.")
  14768. (defun org-property-get-allowed-values (pom property &optional table)
  14769. "Get allowed values for the property PROPERTY.
  14770. When TABLE is non-nil, return an alist that can directly be used for
  14771. completion."
  14772. (let (vals)
  14773. (cond
  14774. ((equal property "TODO")
  14775. (setq vals (org-with-point-at pom
  14776. (append org-todo-keywords-1 '("")))))
  14777. ((equal property "PRIORITY")
  14778. (let ((n org-lowest-priority))
  14779. (while (>= n org-highest-priority)
  14780. (push (char-to-string n) vals)
  14781. (setq n (1- n)))))
  14782. ((member property org-special-properties))
  14783. ((setq vals (run-hook-with-args-until-success
  14784. 'org-property-allowed-value-functions property)))
  14785. (t
  14786. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14787. (when (and vals (string-match "\\S-" vals))
  14788. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14789. (setq vals (mapcar (lambda (x)
  14790. (cond ((stringp x) x)
  14791. ((numberp x) (number-to-string x))
  14792. ((symbolp x) (symbol-name x))
  14793. (t "???")))
  14794. vals)))))
  14795. (when (member ":ETC" vals)
  14796. (setq vals (remove ":ETC" vals))
  14797. (org-add-props (car vals) '(org-unrestricted t)))
  14798. (if table (mapcar 'list vals) vals)))
  14799. (defun org-property-previous-allowed-value (&optional previous)
  14800. "Switch to the next allowed value for this property."
  14801. (interactive)
  14802. (org-property-next-allowed-value t))
  14803. (defun org-property-next-allowed-value (&optional previous)
  14804. "Switch to the next allowed value for this property."
  14805. (interactive)
  14806. (unless (org-at-property-p)
  14807. (user-error "Not at a property"))
  14808. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14809. (key (match-string 2))
  14810. (value (match-string 3))
  14811. (allowed (or (org-property-get-allowed-values (point) key)
  14812. (and (member value '("[ ]" "[-]" "[X]"))
  14813. '("[ ]" "[X]"))))
  14814. (heading (save-match-data (nth 4 (org-heading-components))))
  14815. nval)
  14816. (unless allowed
  14817. (user-error "Allowed values for this property have not been defined"))
  14818. (if previous (setq allowed (reverse allowed)))
  14819. (if (member value allowed)
  14820. (setq nval (car (cdr (member value allowed)))))
  14821. (setq nval (or nval (car allowed)))
  14822. (if (equal nval value)
  14823. (user-error "Only one allowed value for this property"))
  14824. (org-at-property-p)
  14825. (replace-match (concat " :" key ": " nval) t t)
  14826. (org-indent-line)
  14827. (beginning-of-line 1)
  14828. (skip-chars-forward " \t")
  14829. (when (equal prop org-effort-property)
  14830. (org-refresh-property
  14831. '((effort . identity)
  14832. (effort-minutes . org-duration-string-to-minutes))
  14833. nval)
  14834. (when (string= org-clock-current-task heading)
  14835. (setq org-clock-effort nval)
  14836. (org-clock-update-mode-line)))
  14837. (run-hook-with-args 'org-property-changed-functions key nval)))
  14838. (defun org-find-olp (path &optional this-buffer)
  14839. "Return a marker pointing to the entry at outline path OLP.
  14840. If anything goes wrong, throw an error.
  14841. You can wrap this call to catch the error like this:
  14842. (condition-case msg
  14843. (org-mobile-locate-entry (match-string 4))
  14844. (error (nth 1 msg)))
  14845. The return value will then be either a string with the error message,
  14846. or a marker if everything is OK.
  14847. If THIS-BUFFER is set, the outline path does not contain a file,
  14848. only headings."
  14849. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14850. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14851. (level 1)
  14852. (lmin 1)
  14853. (lmax 1)
  14854. limit re end found pos heading cnt flevel)
  14855. (unless buffer (error "File not found :%s" file))
  14856. (with-current-buffer buffer
  14857. (save-excursion
  14858. (save-restriction
  14859. (widen)
  14860. (setq limit (point-max))
  14861. (goto-char (point-min))
  14862. (while (setq heading (pop path))
  14863. (setq re (format org-complex-heading-regexp-format
  14864. (regexp-quote heading)))
  14865. (setq cnt 0 pos (point))
  14866. (while (re-search-forward re end t)
  14867. (setq level (- (match-end 1) (match-beginning 1)))
  14868. (if (and (>= level lmin) (<= level lmax))
  14869. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14870. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14871. lmax heading))
  14872. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14873. lmax heading))
  14874. (goto-char found)
  14875. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14876. (setq end (save-excursion (org-end-of-subtree t t))))
  14877. (when (org-at-heading-p)
  14878. (point-marker)))))))
  14879. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14880. "Find node HEADING in BUFFER.
  14881. Return a marker to the heading if it was found, or nil if not.
  14882. If POS-ONLY is set, return just the position instead of a marker.
  14883. The heading text must match exact, but it may have a TODO keyword,
  14884. a priority cookie and tags in the standard locations."
  14885. (with-current-buffer (or buffer (current-buffer))
  14886. (save-excursion
  14887. (save-restriction
  14888. (widen)
  14889. (goto-char (point-min))
  14890. (let (case-fold-search)
  14891. (if (re-search-forward
  14892. (format org-complex-heading-regexp-format
  14893. (regexp-quote heading)) nil t)
  14894. (if pos-only
  14895. (match-beginning 0)
  14896. (move-marker (make-marker) (match-beginning 0)))))))))
  14897. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14898. "Find Org node headline HEADING in all .org files in directory DIR.
  14899. When the target headline is found, return a marker to this location."
  14900. (let ((files (directory-files (or dir default-directory)
  14901. t "\\`[^.#].*\\.org\\'"))
  14902. file visiting m buffer)
  14903. (catch 'found
  14904. (while (setq file (pop files))
  14905. (message "trying %s" file)
  14906. (setq visiting (org-find-base-buffer-visiting file))
  14907. (setq buffer (or visiting (find-file-noselect file)))
  14908. (setq m (org-find-exact-headline-in-buffer
  14909. heading buffer))
  14910. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14911. (and m (throw 'found m))))))
  14912. (defun org-find-entry-with-id (ident)
  14913. "Locate the entry that contains the ID property with exact value IDENT.
  14914. IDENT can be a string, a symbol or a number, this function will search for
  14915. the string representation of it.
  14916. Return the position where this entry starts, or nil if there is no such entry."
  14917. (interactive "sID: ")
  14918. (let ((id (cond
  14919. ((stringp ident) ident)
  14920. ((symbol-name ident) (symbol-name ident))
  14921. ((numberp ident) (number-to-string ident))
  14922. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14923. (case-fold-search nil))
  14924. (save-excursion
  14925. (save-restriction
  14926. (widen)
  14927. (goto-char (point-min))
  14928. (when (re-search-forward
  14929. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14930. nil t)
  14931. (org-back-to-heading t)
  14932. (point))))))
  14933. ;;;; Timestamps
  14934. (defvar org-last-changed-timestamp nil)
  14935. (defvar org-last-inserted-timestamp nil
  14936. "The last time stamp inserted with `org-insert-time-stamp'.")
  14937. (defvar org-ts-what) ; dynamically scoped parameter
  14938. (defun org-time-stamp (arg &optional inactive)
  14939. "Prompt for a date/time and insert a time stamp.
  14940. If the user specifies a time like HH:MM or if this command is
  14941. called with at least one prefix argument, the time stamp contains
  14942. the date and the time. Otherwise, only the date is be included.
  14943. All parts of a date not specified by the user is filled in from
  14944. the current date/time. So if you just press return without
  14945. typing anything, the time stamp will represent the current
  14946. date/time.
  14947. If there is already a timestamp at the cursor, it will be
  14948. modified.
  14949. With two universal prefix arguments, insert an active timestamp
  14950. with the current time without prompting the user.
  14951. When called from lisp, the timestamp is inactive if INACTIVE is
  14952. non-nil."
  14953. (interactive "P")
  14954. (let* ((ts nil)
  14955. (default-time
  14956. ;; Default time is either today, or, when entering a range,
  14957. ;; the range start.
  14958. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14959. (save-excursion
  14960. (re-search-backward
  14961. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14962. (- (point) 20) t)))
  14963. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14964. (current-time)))
  14965. (default-input (and ts (org-get-compact-tod ts)))
  14966. (repeater (save-excursion
  14967. (save-match-data
  14968. (beginning-of-line)
  14969. (when (re-search-forward
  14970. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14971. (save-excursion (progn (end-of-line) (point))) t)
  14972. (match-string 0)))))
  14973. org-time-was-given org-end-time-was-given time)
  14974. (cond
  14975. ((and (org-at-timestamp-p t)
  14976. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14977. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14978. (insert "--")
  14979. (setq time (let ((this-command this-command))
  14980. (org-read-date arg 'totime nil nil
  14981. default-time default-input inactive)))
  14982. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14983. ((org-at-timestamp-p t)
  14984. (setq time (let ((this-command this-command))
  14985. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14986. (when (org-at-timestamp-p t) ; just to get the match data
  14987. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14988. (replace-match "")
  14989. (setq org-last-changed-timestamp
  14990. (org-insert-time-stamp
  14991. time (or org-time-was-given arg)
  14992. inactive nil nil (list org-end-time-was-given)))
  14993. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14994. (setq org-last-changed-timestamp
  14995. (concat (substring org-last-inserted-timestamp 0 -1)
  14996. " " repeater ">"))))
  14997. (message "Timestamp updated"))
  14998. ((equal arg '(16))
  14999. (org-insert-time-stamp (current-time) t inactive))
  15000. (t
  15001. (setq time (let ((this-command this-command))
  15002. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  15003. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  15004. nil nil (list org-end-time-was-given))))))
  15005. ;; FIXME: can we use this for something else, like computing time differences?
  15006. (defun org-get-compact-tod (s)
  15007. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15008. (let* ((t1 (match-string 1 s))
  15009. (h1 (string-to-number (match-string 2 s)))
  15010. (m1 (string-to-number (match-string 3 s)))
  15011. (t2 (and (match-end 4) (match-string 5 s)))
  15012. (h2 (and t2 (string-to-number (match-string 6 s))))
  15013. (m2 (and t2 (string-to-number (match-string 7 s))))
  15014. dh dm)
  15015. (if (not t2)
  15016. t1
  15017. (setq dh (- h2 h1) dm (- m2 m1))
  15018. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15019. (concat t1 "+" (number-to-string dh)
  15020. (and (/= 0 dm) (format ":%02d" dm)))))))
  15021. (defun org-time-stamp-inactive (&optional arg)
  15022. "Insert an inactive time stamp.
  15023. An inactive time stamp is enclosed in square brackets instead of angle
  15024. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15025. does not link to the calendar and cannot be changed with the S-cursor keys.
  15026. So these are more for recording a certain time/date."
  15027. (interactive "P")
  15028. (org-time-stamp arg 'inactive))
  15029. (defvar org-date-ovl (make-overlay 1 1))
  15030. (overlay-put org-date-ovl 'face 'org-date-selected)
  15031. (org-detach-overlay org-date-ovl)
  15032. (defvar org-ans1) ; dynamically scoped parameter
  15033. (defvar org-ans2) ; dynamically scoped parameter
  15034. (defvar org-plain-time-of-day-regexp) ; defined below
  15035. (defvar org-overriding-default-time nil) ; dynamically scoped
  15036. (defvar org-read-date-overlay nil)
  15037. (defvar org-dcst nil) ; dynamically scoped
  15038. (defvar org-read-date-history nil)
  15039. (defvar org-read-date-final-answer nil)
  15040. (defvar org-read-date-analyze-futurep nil)
  15041. (defvar org-read-date-analyze-forced-year nil)
  15042. (defvar org-read-date-inactive)
  15043. (defvar org-read-date-minibuffer-local-map
  15044. (let* ((org-replace-disputed-keys nil)
  15045. (map (make-sparse-keymap)))
  15046. (set-keymap-parent map minibuffer-local-map)
  15047. (org-defkey map (kbd ".")
  15048. (lambda () (interactive)
  15049. ;; Are we at the beginning of the prompt?
  15050. (if (looking-back "^[^:]+: ")
  15051. (org-eval-in-calendar '(calendar-goto-today))
  15052. (insert "."))))
  15053. (org-defkey map (kbd "C-.")
  15054. (lambda () (interactive)
  15055. (org-eval-in-calendar '(calendar-goto-today))))
  15056. (org-defkey map [(meta shift left)]
  15057. (lambda () (interactive)
  15058. (org-eval-in-calendar '(calendar-backward-month 1))))
  15059. (org-defkey map [(meta shift right)]
  15060. (lambda () (interactive)
  15061. (org-eval-in-calendar '(calendar-forward-month 1))))
  15062. (org-defkey map [(meta shift up)]
  15063. (lambda () (interactive)
  15064. (org-eval-in-calendar '(calendar-backward-year 1))))
  15065. (org-defkey map [(meta shift down)]
  15066. (lambda () (interactive)
  15067. (org-eval-in-calendar '(calendar-forward-year 1))))
  15068. (org-defkey map [?\e (shift left)]
  15069. (lambda () (interactive)
  15070. (org-eval-in-calendar '(calendar-backward-month 1))))
  15071. (org-defkey map [?\e (shift right)]
  15072. (lambda () (interactive)
  15073. (org-eval-in-calendar '(calendar-forward-month 1))))
  15074. (org-defkey map [?\e (shift up)]
  15075. (lambda () (interactive)
  15076. (org-eval-in-calendar '(calendar-backward-year 1))))
  15077. (org-defkey map [?\e (shift down)]
  15078. (lambda () (interactive)
  15079. (org-eval-in-calendar '(calendar-forward-year 1))))
  15080. (org-defkey map [(shift up)]
  15081. (lambda () (interactive)
  15082. (org-eval-in-calendar '(calendar-backward-week 1))))
  15083. (org-defkey map [(shift down)]
  15084. (lambda () (interactive)
  15085. (org-eval-in-calendar '(calendar-forward-week 1))))
  15086. (org-defkey map [(shift left)]
  15087. (lambda () (interactive)
  15088. (org-eval-in-calendar '(calendar-backward-day 1))))
  15089. (org-defkey map [(shift right)]
  15090. (lambda () (interactive)
  15091. (org-eval-in-calendar '(calendar-forward-day 1))))
  15092. (org-defkey map "!"
  15093. (lambda () (interactive)
  15094. (org-eval-in-calendar '(diary-view-entries))
  15095. (message "")))
  15096. (org-defkey map ">"
  15097. (lambda () (interactive)
  15098. (org-eval-in-calendar '(scroll-calendar-left 1))))
  15099. (org-defkey map "<"
  15100. (lambda () (interactive)
  15101. (org-eval-in-calendar '(scroll-calendar-right 1))))
  15102. (org-defkey map "\C-v"
  15103. (lambda () (interactive)
  15104. (org-eval-in-calendar
  15105. '(calendar-scroll-left-three-months 1))))
  15106. (org-defkey map "\M-v"
  15107. (lambda () (interactive)
  15108. (org-eval-in-calendar
  15109. '(calendar-scroll-right-three-months 1))))
  15110. map)
  15111. "Keymap for minibuffer commands when using `org-read-date'.")
  15112. (defvar org-def)
  15113. (defvar org-defdecode)
  15114. (defvar org-with-time)
  15115. (defun org-read-date (&optional org-with-time to-time from-string prompt
  15116. default-time default-input inactive)
  15117. "Read a date, possibly a time, and make things smooth for the user.
  15118. The prompt will suggest to enter an ISO date, but you can also enter anything
  15119. which will at least partially be understood by `parse-time-string'.
  15120. Unrecognized parts of the date will default to the current day, month, year,
  15121. hour and minute. If this command is called to replace a timestamp at point,
  15122. or to enter the second timestamp of a range, the default time is taken
  15123. from the existing stamp. Furthermore, the command prefers the future,
  15124. so if you are giving a date where the year is not given, and the day-month
  15125. combination is already past in the current year, it will assume you
  15126. mean next year. For details, see the manual. A few examples:
  15127. 3-2-5 --> 2003-02-05
  15128. feb 15 --> currentyear-02-15
  15129. 2/15 --> currentyear-02-15
  15130. sep 12 9 --> 2009-09-12
  15131. 12:45 --> today 12:45
  15132. 22 sept 0:34 --> currentyear-09-22 0:34
  15133. 12 --> currentyear-currentmonth-12
  15134. Fri --> nearest Friday after today
  15135. -Tue --> last Tuesday
  15136. etc.
  15137. Furthermore you can specify a relative date by giving, as the *first* thing
  15138. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15139. change in days weeks, months, years.
  15140. With a single plus or minus, the date is relative to today. With a double
  15141. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15142. +4d --> four days from today
  15143. +4 --> same as above
  15144. +2w --> two weeks from today
  15145. ++5 --> five days from default date
  15146. The function understands only English month and weekday abbreviations.
  15147. While prompting, a calendar is popped up - you can also select the
  15148. date with the mouse (button 1). The calendar shows a period of three
  15149. months. To scroll it to other months, use the keys `>' and `<'.
  15150. If you don't like the calendar, turn it off with
  15151. \(setq org-read-date-popup-calendar nil)
  15152. With optional argument TO-TIME, the date will immediately be converted
  15153. to an internal time.
  15154. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  15155. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  15156. still enter a time, and this function will inform the calling routine
  15157. about this change. The calling routine may then choose to change the
  15158. format used to insert the time stamp into the buffer to include the time.
  15159. With optional argument FROM-STRING, read from this string instead from
  15160. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15161. the time/date that is used for everything that is not specified by the
  15162. user."
  15163. (require 'parse-time)
  15164. (let* ((org-time-stamp-rounding-minutes
  15165. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  15166. (org-dcst org-display-custom-times)
  15167. (ct (org-current-time))
  15168. (org-def (or org-overriding-default-time default-time ct))
  15169. (org-defdecode (decode-time org-def))
  15170. (dummy (progn
  15171. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15172. (setcar (nthcdr 2 org-defdecode) -1)
  15173. (setcar (nthcdr 1 org-defdecode) 59)
  15174. (setq org-def (apply 'encode-time org-defdecode)
  15175. org-defdecode (decode-time org-def)))))
  15176. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15177. (calendar-frame-setup nil)
  15178. (calendar-setup nil)
  15179. (calendar-move-hook nil)
  15180. (calendar-view-diary-initially-flag nil)
  15181. (calendar-view-holidays-initially-flag nil)
  15182. (timestr (format-time-string
  15183. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15184. (prompt (concat (if prompt (concat prompt " ") "")
  15185. (format "Date+time [%s]: " timestr)))
  15186. ans (org-ans0 "") org-ans1 org-ans2 final)
  15187. (cond
  15188. (from-string (setq ans from-string))
  15189. (org-read-date-popup-calendar
  15190. (save-excursion
  15191. (save-window-excursion
  15192. (calendar)
  15193. (org-eval-in-calendar '(setq cursor-type nil) t)
  15194. (unwind-protect
  15195. (progn
  15196. (calendar-forward-day (- (time-to-days org-def)
  15197. (calendar-absolute-from-gregorian
  15198. (calendar-current-date))))
  15199. (org-eval-in-calendar nil t)
  15200. (let* ((old-map (current-local-map))
  15201. (map (copy-keymap calendar-mode-map))
  15202. (minibuffer-local-map
  15203. (copy-keymap org-read-date-minibuffer-local-map)))
  15204. (org-defkey map (kbd "RET") 'org-calendar-select)
  15205. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15206. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15207. (unwind-protect
  15208. (progn
  15209. (use-local-map map)
  15210. (setq org-read-date-inactive inactive)
  15211. (add-hook 'post-command-hook 'org-read-date-display)
  15212. (setq org-ans0 (read-string prompt default-input
  15213. 'org-read-date-history nil))
  15214. ;; org-ans0: from prompt
  15215. ;; org-ans1: from mouse click
  15216. ;; org-ans2: from calendar motion
  15217. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15218. (remove-hook 'post-command-hook 'org-read-date-display)
  15219. (use-local-map old-map)
  15220. (when org-read-date-overlay
  15221. (delete-overlay org-read-date-overlay)
  15222. (setq org-read-date-overlay nil)))))
  15223. (bury-buffer "*Calendar*")))))
  15224. (t ; Naked prompt only
  15225. (unwind-protect
  15226. (setq ans (read-string prompt default-input
  15227. 'org-read-date-history timestr))
  15228. (when org-read-date-overlay
  15229. (delete-overlay org-read-date-overlay)
  15230. (setq org-read-date-overlay nil)))))
  15231. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15232. (when org-read-date-analyze-forced-year
  15233. (message "Year was forced into %s"
  15234. (if org-read-date-force-compatible-dates
  15235. "compatible range (1970-2037)"
  15236. "range representable on this machine"))
  15237. (ding))
  15238. ;; One round trip to get rid of 34th of August and stuff like that....
  15239. (setq final (decode-time (apply 'encode-time final)))
  15240. (setq org-read-date-final-answer ans)
  15241. (if to-time
  15242. (apply 'encode-time final)
  15243. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15244. (format "%04d-%02d-%02d %02d:%02d"
  15245. (nth 5 final) (nth 4 final) (nth 3 final)
  15246. (nth 2 final) (nth 1 final))
  15247. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15248. (defun org-read-date-display ()
  15249. "Display the current date prompt interpretation in the minibuffer."
  15250. (when org-read-date-display-live
  15251. (when org-read-date-overlay
  15252. (delete-overlay org-read-date-overlay))
  15253. (when (minibufferp (current-buffer))
  15254. (save-excursion
  15255. (end-of-line 1)
  15256. (while (not (equal (buffer-substring
  15257. (max (point-min) (- (point) 4)) (point))
  15258. " "))
  15259. (insert " ")))
  15260. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15261. " " (or org-ans1 org-ans2)))
  15262. (org-end-time-was-given nil)
  15263. (f (org-read-date-analyze ans org-def org-defdecode))
  15264. (fmts (if org-dcst
  15265. org-time-stamp-custom-formats
  15266. org-time-stamp-formats))
  15267. (fmt (if (or org-with-time
  15268. (and (boundp 'org-time-was-given) org-time-was-given))
  15269. (cdr fmts)
  15270. (car fmts)))
  15271. (txt (format-time-string fmt (apply 'encode-time f)))
  15272. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15273. (txt (concat "=> " txt)))
  15274. (when (and org-end-time-was-given
  15275. (string-match org-plain-time-of-day-regexp txt))
  15276. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15277. org-end-time-was-given
  15278. (substring txt (match-end 0)))))
  15279. (when org-read-date-analyze-futurep
  15280. (setq txt (concat txt " (=>F)")))
  15281. (setq org-read-date-overlay
  15282. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15283. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15284. (defun org-read-date-analyze (ans org-def org-defdecode)
  15285. "Analyze the combined answer of the date prompt."
  15286. ;; FIXME: cleanup and comment
  15287. (let ((nowdecode (decode-time (current-time)))
  15288. delta deltan deltaw deltadef year month day
  15289. hour minute second wday pm h2 m2 tl wday1
  15290. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15291. (setq org-read-date-analyze-futurep nil
  15292. org-read-date-analyze-forced-year nil)
  15293. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15294. (setq ans "+0"))
  15295. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15296. (setq ans (replace-match "" t t ans)
  15297. deltan (car delta)
  15298. deltaw (nth 1 delta)
  15299. deltadef (nth 2 delta)))
  15300. ;; Check if there is an iso week date in there. If yes, store the
  15301. ;; info and postpone interpreting it until the rest of the parsing
  15302. ;; is done.
  15303. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15304. (setq iso-year (if (match-end 1)
  15305. (org-small-year-to-year
  15306. (string-to-number (match-string 1 ans))))
  15307. iso-weekday (if (match-end 3)
  15308. (string-to-number (match-string 3 ans)))
  15309. iso-week (string-to-number (match-string 2 ans)))
  15310. (setq ans (replace-match "" t t ans)))
  15311. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15312. (when (string-match
  15313. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15314. (setq year (if (match-end 2)
  15315. (string-to-number (match-string 2 ans))
  15316. (progn (setq kill-year t)
  15317. (string-to-number (format-time-string "%Y"))))
  15318. month (string-to-number (match-string 3 ans))
  15319. day (string-to-number (match-string 4 ans)))
  15320. (if (< year 100) (setq year (+ 2000 year)))
  15321. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15322. t nil ans)))
  15323. ;; Help matching dotted european dates
  15324. (when (string-match
  15325. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15326. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15327. (setq kill-year t)
  15328. (string-to-number (format-time-string "%Y")))
  15329. day (string-to-number (match-string 1 ans))
  15330. month (string-to-number (match-string 2 ans))
  15331. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15332. t nil ans)))
  15333. ;; Help matching american dates, like 5/30 or 5/30/7
  15334. (when (string-match
  15335. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15336. (setq year (if (match-end 4)
  15337. (string-to-number (match-string 4 ans))
  15338. (progn (setq kill-year t)
  15339. (string-to-number (format-time-string "%Y"))))
  15340. month (string-to-number (match-string 1 ans))
  15341. day (string-to-number (match-string 2 ans)))
  15342. (if (< year 100) (setq year (+ 2000 year)))
  15343. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15344. t nil ans)))
  15345. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15346. ;; If there is a time with am/pm, and *no* time without it, we convert
  15347. ;; so that matching will be successful.
  15348. (loop for i from 1 to 2 do ; twice, for end time as well
  15349. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15350. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15351. (setq hour (string-to-number (match-string 1 ans))
  15352. minute (if (match-end 3)
  15353. (string-to-number (match-string 3 ans))
  15354. 0)
  15355. pm (equal ?p
  15356. (string-to-char (downcase (match-string 4 ans)))))
  15357. (if (and (= hour 12) (not pm))
  15358. (setq hour 0)
  15359. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15360. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15361. t t ans))))
  15362. ;; Check if a time range is given as a duration
  15363. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15364. (setq hour (string-to-number (match-string 1 ans))
  15365. h2 (+ hour (string-to-number (match-string 3 ans)))
  15366. minute (string-to-number (match-string 2 ans))
  15367. m2 (+ minute (if (match-end 5) (string-to-number
  15368. (match-string 5 ans))0)))
  15369. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15370. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15371. t t ans)))
  15372. ;; Check if there is a time range
  15373. (when (boundp 'org-end-time-was-given)
  15374. (setq org-time-was-given nil)
  15375. (when (and (string-match org-plain-time-of-day-regexp ans)
  15376. (match-end 8))
  15377. (setq org-end-time-was-given (match-string 8 ans))
  15378. (setq ans (concat (substring ans 0 (match-beginning 7))
  15379. (substring ans (match-end 7))))))
  15380. (setq tl (parse-time-string ans)
  15381. day (or (nth 3 tl) (nth 3 org-defdecode))
  15382. month (or (nth 4 tl)
  15383. (if (and org-read-date-prefer-future
  15384. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15385. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15386. (nth 4 org-defdecode)))
  15387. year (or (and (not kill-year) (nth 5 tl))
  15388. (if (and org-read-date-prefer-future
  15389. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15390. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15391. (nth 5 org-defdecode)))
  15392. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15393. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15394. second (or (nth 0 tl) 0)
  15395. wday (nth 6 tl))
  15396. (when (and (eq org-read-date-prefer-future 'time)
  15397. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15398. (equal day (nth 3 nowdecode))
  15399. (equal month (nth 4 nowdecode))
  15400. (equal year (nth 5 nowdecode))
  15401. (nth 2 tl)
  15402. (or (< (nth 2 tl) (nth 2 nowdecode))
  15403. (and (= (nth 2 tl) (nth 2 nowdecode))
  15404. (nth 1 tl)
  15405. (< (nth 1 tl) (nth 1 nowdecode)))))
  15406. (setq day (1+ day)
  15407. futurep t))
  15408. ;; Special date definitions below
  15409. (cond
  15410. (iso-week
  15411. ;; There was an iso week
  15412. (require 'cal-iso)
  15413. (setq futurep nil)
  15414. (setq year (or iso-year year)
  15415. day (or iso-weekday wday 1)
  15416. wday nil ; to make sure that the trigger below does not match
  15417. iso-date (calendar-gregorian-from-absolute
  15418. (calendar-absolute-from-iso
  15419. (list iso-week day year))))
  15420. ; FIXME: Should we also push ISO weeks into the future?
  15421. ; (when (and org-read-date-prefer-future
  15422. ; (not iso-year)
  15423. ; (< (calendar-absolute-from-gregorian iso-date)
  15424. ; (time-to-days (current-time))))
  15425. ; (setq year (1+ year)
  15426. ; iso-date (calendar-gregorian-from-absolute
  15427. ; (calendar-absolute-from-iso
  15428. ; (list iso-week day year)))))
  15429. (setq month (car iso-date)
  15430. year (nth 2 iso-date)
  15431. day (nth 1 iso-date)))
  15432. (deltan
  15433. (setq futurep nil)
  15434. (unless deltadef
  15435. (let ((now (decode-time (current-time))))
  15436. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15437. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15438. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15439. ((equal deltaw "m") (setq month (+ month deltan)))
  15440. ((equal deltaw "y") (setq year (+ year deltan)))))
  15441. ((and wday (not (nth 3 tl)))
  15442. ;; Weekday was given, but no day, so pick that day in the week
  15443. ;; on or after the derived date.
  15444. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15445. (unless (equal wday wday1)
  15446. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15447. (if (and (boundp 'org-time-was-given)
  15448. (nth 2 tl))
  15449. (setq org-time-was-given t))
  15450. (if (< year 100) (setq year (+ 2000 year)))
  15451. ;; Check of the date is representable
  15452. (if org-read-date-force-compatible-dates
  15453. (progn
  15454. (if (< year 1970)
  15455. (setq year 1970 org-read-date-analyze-forced-year t))
  15456. (if (> year 2037)
  15457. (setq year 2037 org-read-date-analyze-forced-year t)))
  15458. (condition-case nil
  15459. (ignore (encode-time second minute hour day month year))
  15460. (error
  15461. (setq year (nth 5 org-defdecode))
  15462. (setq org-read-date-analyze-forced-year t))))
  15463. (setq org-read-date-analyze-futurep futurep)
  15464. (list second minute hour day month year)))
  15465. (defvar parse-time-weekdays)
  15466. (defun org-read-date-get-relative (s today default)
  15467. "Check string S for special relative date string.
  15468. TODAY and DEFAULT are internal times, for today and for a default.
  15469. Return shift list (N what def-flag)
  15470. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15471. N is the number of WHATs to shift.
  15472. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15473. the DEFAULT date rather than TODAY."
  15474. (require 'parse-time)
  15475. (when (and
  15476. (string-match
  15477. (concat
  15478. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15479. "\\([0-9]+\\)?"
  15480. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15481. "\\([ \t]\\|$\\)") s)
  15482. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15483. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15484. (string-to-char (substring (match-string 1 s) -1))
  15485. ?+))
  15486. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15487. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15488. (what (if (match-end 3) (match-string 3 s) "d"))
  15489. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15490. (date (if rel default today))
  15491. (wday (nth 6 (decode-time date)))
  15492. delta)
  15493. (if wday1
  15494. (progn
  15495. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15496. (if (= delta 0) (setq delta 7))
  15497. (if (= dir ?-)
  15498. (progn
  15499. (setq delta (- delta 7))
  15500. (if (= delta 0) (setq delta -7))))
  15501. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15502. (list delta "d" rel))
  15503. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15504. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15505. "Turn a user-specified date into the internal representation.
  15506. The internal representation needed by the calendar is (month day year).
  15507. This is a wrapper to handle the brain-dead convention in calendar that
  15508. user function argument order change dependent on argument order."
  15509. (if (boundp 'calendar-date-style)
  15510. (cond
  15511. ((eq calendar-date-style 'american)
  15512. (list arg1 arg2 arg3))
  15513. ((eq calendar-date-style 'european)
  15514. (list arg2 arg1 arg3))
  15515. ((eq calendar-date-style 'iso)
  15516. (list arg2 arg3 arg1)))
  15517. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15518. (if (org-bound-and-true-p european-calendar-style)
  15519. (list arg2 arg1 arg3)
  15520. (list arg1 arg2 arg3)))))
  15521. (defun org-eval-in-calendar (form &optional keepdate)
  15522. "Eval FORM in the calendar window and return to current window.
  15523. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15524. otherwise stick to the current value of `org-ans2'."
  15525. (let ((sf (selected-frame))
  15526. (sw (selected-window)))
  15527. (select-window (get-buffer-window "*Calendar*" t))
  15528. (eval form)
  15529. (when (and (not keepdate) (calendar-cursor-to-date))
  15530. (let* ((date (calendar-cursor-to-date))
  15531. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15532. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15533. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15534. (select-window sw)
  15535. (org-select-frame-set-input-focus sf)))
  15536. (defun org-calendar-select ()
  15537. "Return to `org-read-date' with the date currently selected.
  15538. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15539. (interactive)
  15540. (when (calendar-cursor-to-date)
  15541. (let* ((date (calendar-cursor-to-date))
  15542. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15543. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15544. (if (active-minibuffer-window) (exit-minibuffer))))
  15545. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15546. "Insert a date stamp for the date given by the internal TIME.
  15547. WITH-HM means use the stamp format that includes the time of the day.
  15548. INACTIVE means use square brackets instead of angular ones, so that the
  15549. stamp will not contribute to the agenda.
  15550. PRE and POST are optional strings to be inserted before and after the
  15551. stamp.
  15552. The command returns the inserted time stamp."
  15553. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15554. stamp)
  15555. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15556. (insert-before-markers (or pre ""))
  15557. (when (listp extra)
  15558. (setq extra (car extra))
  15559. (if (and (stringp extra)
  15560. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15561. (setq extra (format "-%02d:%02d"
  15562. (string-to-number (match-string 1 extra))
  15563. (string-to-number (match-string 2 extra))))
  15564. (setq extra nil)))
  15565. (when extra
  15566. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15567. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15568. (insert-before-markers (or post ""))
  15569. (setq org-last-inserted-timestamp stamp)))
  15570. (defun org-toggle-time-stamp-overlays ()
  15571. "Toggle the use of custom time stamp formats."
  15572. (interactive)
  15573. (setq org-display-custom-times (not org-display-custom-times))
  15574. (unless org-display-custom-times
  15575. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15576. (while (setq p (next-single-property-change p 'display))
  15577. (if (and (get-text-property p 'display)
  15578. (eq (get-text-property p 'face) 'org-date))
  15579. (remove-text-properties
  15580. p (setq p (next-single-property-change p 'display))
  15581. '(display t))))
  15582. (set-buffer-modified-p bmp)))
  15583. (if (featurep 'xemacs)
  15584. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15585. (org-restart-font-lock)
  15586. (setq org-table-may-need-update t)
  15587. (if org-display-custom-times
  15588. (message "Time stamps are overlaid with custom format")
  15589. (message "Time stamp overlays removed")))
  15590. (defun org-display-custom-time (beg end)
  15591. "Overlay modified time stamp format over timestamp between BEG and END."
  15592. (let* ((ts (buffer-substring beg end))
  15593. t1 w1 with-hm tf time str w2 (off 0))
  15594. (save-match-data
  15595. (setq t1 (org-parse-time-string ts t))
  15596. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15597. (setq off (- (match-end 0) (match-beginning 0)))))
  15598. (setq end (- end off))
  15599. (setq w1 (- end beg)
  15600. with-hm (and (nth 1 t1) (nth 2 t1))
  15601. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15602. time (org-fix-decoded-time t1)
  15603. str (org-add-props
  15604. (format-time-string
  15605. (substring tf 1 -1) (apply 'encode-time time))
  15606. nil 'mouse-face 'highlight)
  15607. w2 (length str))
  15608. (if (not (= w2 w1))
  15609. (add-text-properties (1+ beg) (+ 2 beg)
  15610. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15611. (if (featurep 'xemacs)
  15612. (progn
  15613. (put-text-property beg end 'invisible t)
  15614. (put-text-property beg end 'end-glyph (make-glyph str)))
  15615. (put-text-property beg end 'display str))))
  15616. (defun org-translate-time (string)
  15617. "Translate all timestamps in STRING to custom format.
  15618. But do this only if the variable `org-display-custom-times' is set."
  15619. (when org-display-custom-times
  15620. (save-match-data
  15621. (let* ((start 0)
  15622. (re org-ts-regexp-both)
  15623. t1 with-hm inactive tf time str beg end)
  15624. (while (setq start (string-match re string start))
  15625. (setq beg (match-beginning 0)
  15626. end (match-end 0)
  15627. t1 (save-match-data
  15628. (org-parse-time-string (substring string beg end) t))
  15629. with-hm (and (nth 1 t1) (nth 2 t1))
  15630. inactive (equal (substring string beg (1+ beg)) "[")
  15631. tf (funcall (if with-hm 'cdr 'car)
  15632. org-time-stamp-custom-formats)
  15633. time (org-fix-decoded-time t1)
  15634. str (format-time-string
  15635. (concat
  15636. (if inactive "[" "<") (substring tf 1 -1)
  15637. (if inactive "]" ">"))
  15638. (apply 'encode-time time))
  15639. string (replace-match str t t string)
  15640. start (+ start (length str)))))))
  15641. string)
  15642. (defun org-fix-decoded-time (time)
  15643. "Set 0 instead of nil for the first 6 elements of time.
  15644. Don't touch the rest."
  15645. (let ((n 0))
  15646. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15647. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15648. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15649. "Difference between TIMESTAMP-STRING and now in days.
  15650. If SECONDS is non-nil, return the difference in seconds."
  15651. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15652. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15653. (funcall fdiff (current-time)))))
  15654. (defun org-deadline-close (timestamp-string &optional ndays)
  15655. "Is the time in TIMESTAMP-STRING close to the current date?"
  15656. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15657. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15658. (not (org-entry-is-done-p))))
  15659. (defun org-get-wdays (ts &optional delay zero-delay)
  15660. "Get the deadline lead time appropriate for timestring TS.
  15661. When DELAY is non-nil, get the delay time for scheduled items
  15662. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15663. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15664. don't try to find the delay cookie in the scheduled timestamp."
  15665. (let ((tv (if delay org-scheduled-delay-days
  15666. org-deadline-warning-days)))
  15667. (cond
  15668. ((or (and delay (< tv 0))
  15669. (and delay zero-delay (<= tv 0))
  15670. (and (not delay) (<= tv 0)))
  15671. ;; Enforce this value no matter what
  15672. (- tv))
  15673. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15674. ;; lead time is specified.
  15675. (floor (* (string-to-number (match-string 1 ts))
  15676. (cdr (assoc (match-string 2 ts)
  15677. '(("d" . 1) ("w" . 7)
  15678. ("m" . 30.4) ("y" . 365.25)
  15679. ("h" . 0.041667)))))))
  15680. ;; go for the default.
  15681. (t tv))))
  15682. (defun org-calendar-select-mouse (ev)
  15683. "Return to `org-read-date' with the date currently selected.
  15684. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15685. (interactive "e")
  15686. (mouse-set-point ev)
  15687. (when (calendar-cursor-to-date)
  15688. (let* ((date (calendar-cursor-to-date))
  15689. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15690. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15691. (if (active-minibuffer-window) (exit-minibuffer))))
  15692. (defun org-check-deadlines (ndays)
  15693. "Check if there are any deadlines due or past due.
  15694. A deadline is considered due if it happens within `org-deadline-warning-days'
  15695. days from today's date. If the deadline appears in an entry marked DONE,
  15696. it is not shown. The prefix arg NDAYS can be used to test that many
  15697. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15698. (interactive "P")
  15699. (let* ((org-warn-days
  15700. (cond
  15701. ((equal ndays '(4)) 100000)
  15702. (ndays (prefix-numeric-value ndays))
  15703. (t (abs org-deadline-warning-days))))
  15704. (case-fold-search nil)
  15705. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15706. (callback
  15707. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15708. (message "%d deadlines past-due or due within %d days"
  15709. (org-occur regexp nil callback)
  15710. org-warn-days)))
  15711. (defsubst org-re-timestamp (type)
  15712. "Return a regexp for timestamp TYPE.
  15713. Allowed values for TYPE are:
  15714. all: all timestamps
  15715. active: only active timestamps (<...>)
  15716. inactive: only inactive timestamps ([...])
  15717. scheduled: only scheduled timestamps
  15718. deadline: only deadline timestamps
  15719. closed: only closed time-stamps
  15720. When TYPE is nil, fall back on returning a regexp that matches
  15721. both scheduled and deadline timestamps."
  15722. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15723. ((eq type 'active) org-ts-regexp)
  15724. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15725. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15726. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15727. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15728. ((eq type 'scheduled-or-deadline)
  15729. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15730. (defun org-check-before-date (date)
  15731. "Check if there are deadlines or scheduled entries before DATE."
  15732. (interactive (list (org-read-date)))
  15733. (let ((case-fold-search nil)
  15734. (regexp (org-re-timestamp org-ts-type))
  15735. (callback
  15736. (lambda () (time-less-p
  15737. (org-time-string-to-time (match-string 1))
  15738. (org-time-string-to-time date)))))
  15739. (message "%d entries before %s"
  15740. (org-occur regexp nil callback) date)))
  15741. (defun org-check-after-date (date)
  15742. "Check if there are deadlines or scheduled entries after DATE."
  15743. (interactive (list (org-read-date)))
  15744. (let ((case-fold-search nil)
  15745. (regexp (org-re-timestamp org-ts-type))
  15746. (callback
  15747. (lambda () (not
  15748. (time-less-p
  15749. (org-time-string-to-time (match-string 1))
  15750. (org-time-string-to-time date))))))
  15751. (message "%d entries after %s"
  15752. (org-occur regexp nil callback) date)))
  15753. (defun org-check-dates-range (start-date end-date)
  15754. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15755. (interactive (list (org-read-date nil nil nil "Range starts")
  15756. (org-read-date nil nil nil "Range end")))
  15757. (let ((case-fold-search nil)
  15758. (regexp (org-re-timestamp org-ts-type))
  15759. (callback
  15760. (lambda ()
  15761. (let ((match (match-string 1)))
  15762. (and
  15763. (not (time-less-p
  15764. (org-time-string-to-time match)
  15765. (org-time-string-to-time start-date)))
  15766. (time-less-p
  15767. (org-time-string-to-time match)
  15768. (org-time-string-to-time end-date)))))))
  15769. (message "%d entries between %s and %s"
  15770. (org-occur regexp nil callback) start-date end-date)))
  15771. (defun org-evaluate-time-range (&optional to-buffer)
  15772. "Evaluate a time range by computing the difference between start and end.
  15773. Normally the result is just printed in the echo area, but with prefix arg
  15774. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15775. If the time range is actually in a table, the result is inserted into the
  15776. next column.
  15777. For time difference computation, a year is assumed to be exactly 365
  15778. days in order to avoid rounding problems."
  15779. (interactive "P")
  15780. (or
  15781. (org-clock-update-time-maybe)
  15782. (save-excursion
  15783. (unless (org-at-date-range-p t)
  15784. (goto-char (point-at-bol))
  15785. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15786. (if (not (org-at-date-range-p t))
  15787. (user-error "Not at a time-stamp range, and none found in current line")))
  15788. (let* ((ts1 (match-string 1))
  15789. (ts2 (match-string 2))
  15790. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15791. (match-end (match-end 0))
  15792. (time1 (org-time-string-to-time ts1))
  15793. (time2 (org-time-string-to-time ts2))
  15794. (t1 (org-float-time time1))
  15795. (t2 (org-float-time time2))
  15796. (diff (abs (- t2 t1)))
  15797. (negative (< (- t2 t1) 0))
  15798. ;; (ys (floor (* 365 24 60 60)))
  15799. (ds (* 24 60 60))
  15800. (hs (* 60 60))
  15801. (fy "%dy %dd %02d:%02d")
  15802. (fy1 "%dy %dd")
  15803. (fd "%dd %02d:%02d")
  15804. (fd1 "%dd")
  15805. (fh "%02d:%02d")
  15806. y d h m align)
  15807. (if havetime
  15808. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15809. y 0
  15810. d (floor (/ diff ds)) diff (mod diff ds)
  15811. h (floor (/ diff hs)) diff (mod diff hs)
  15812. m (floor (/ diff 60)))
  15813. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15814. y 0
  15815. d (floor (+ (/ diff ds) 0.5))
  15816. h 0 m 0))
  15817. (if (not to-buffer)
  15818. (message "%s" (org-make-tdiff-string y d h m))
  15819. (if (org-at-table-p)
  15820. (progn
  15821. (goto-char match-end)
  15822. (setq align t)
  15823. (and (looking-at " *|") (goto-char (match-end 0))))
  15824. (goto-char match-end))
  15825. (if (looking-at
  15826. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15827. (replace-match ""))
  15828. (if negative (insert " -"))
  15829. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15830. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15831. (insert " " (format fh h m))))
  15832. (if align (org-table-align))
  15833. (message "Time difference inserted")))))
  15834. (defun org-make-tdiff-string (y d h m)
  15835. (let ((fmt "")
  15836. (l nil))
  15837. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15838. l (push y l)))
  15839. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15840. l (push d l)))
  15841. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15842. l (push h l)))
  15843. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15844. l (push m l)))
  15845. (apply 'format fmt (nreverse l))))
  15846. (defun org-time-string-to-time (s &optional buffer pos)
  15847. "Convert a timestamp string into internal time."
  15848. (condition-case errdata
  15849. (apply 'encode-time (org-parse-time-string s))
  15850. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15851. s (if (not (and buffer pos))
  15852. ""
  15853. (format " at %d in buffer `%s'" pos buffer))
  15854. (cdr errdata)))))
  15855. (defun org-time-string-to-seconds (s)
  15856. "Convert a timestamp string to a number of seconds."
  15857. (org-float-time (org-time-string-to-time s)))
  15858. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15859. "Convert a time stamp to an absolute day number.
  15860. If there is a specifier for a cyclic time stamp, get the closest
  15861. date to DAYNR.
  15862. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15863. The variable `date' is bound by the calendar when this is called."
  15864. (cond
  15865. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15866. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15867. daynr
  15868. (+ daynr 1000)))
  15869. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15870. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15871. (time-to-days (current-time))) (match-string 0 s)
  15872. prefer show-all))
  15873. (t (time-to-days
  15874. (condition-case errdata
  15875. (apply 'encode-time (org-parse-time-string s))
  15876. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15877. s (if (not (and buffer pos))
  15878. ""
  15879. (format " at %d in buffer `%s'" pos buffer))
  15880. (cdr errdata))))))))
  15881. (defun org-days-to-iso-week (days)
  15882. "Return the iso week number."
  15883. (require 'cal-iso)
  15884. (car (calendar-iso-from-absolute days)))
  15885. (defun org-small-year-to-year (year)
  15886. "Convert 2-digit years into 4-digit years.
  15887. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15888. The year 2000 cannot be abbreviated. Any year larger than 99
  15889. is returned unchanged."
  15890. (if (< year 38)
  15891. (setq year (+ 2000 year))
  15892. (if (< year 100)
  15893. (setq year (+ 1900 year))))
  15894. year)
  15895. (defun org-time-from-absolute (d)
  15896. "Return the time corresponding to date D.
  15897. D may be an absolute day number, or a calendar-type list (month day year)."
  15898. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15899. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15900. (defun org-calendar-holiday ()
  15901. "List of holidays, for Diary display in Org-mode."
  15902. (require 'holidays)
  15903. (let ((hl (funcall
  15904. (if (fboundp 'calendar-check-holidays)
  15905. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15906. (if hl (mapconcat 'identity hl "; "))))
  15907. (defun org-diary-sexp-entry (sexp entry date)
  15908. "Process a SEXP diary ENTRY for DATE."
  15909. (require 'diary-lib)
  15910. (let ((result (if calendar-debug-sexp
  15911. (let ((stack-trace-on-error t))
  15912. (eval (car (read-from-string sexp))))
  15913. (condition-case nil
  15914. (eval (car (read-from-string sexp)))
  15915. (error
  15916. (beep)
  15917. (message "Bad sexp at line %d in %s: %s"
  15918. (org-current-line)
  15919. (buffer-file-name) sexp)
  15920. (sleep-for 2))))))
  15921. (cond ((stringp result) (split-string result "; "))
  15922. ((and (consp result)
  15923. (not (consp (cdr result)))
  15924. (stringp (cdr result))) (cdr result))
  15925. ((and (consp result)
  15926. (stringp (car result))) result)
  15927. (result entry))))
  15928. (defun org-diary-to-ical-string (frombuf)
  15929. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15930. This uses the icalendar.el library."
  15931. (let* ((tmpdir (if (featurep 'xemacs)
  15932. (temp-directory)
  15933. temporary-file-directory))
  15934. (tmpfile (make-temp-name
  15935. (expand-file-name "orgics" tmpdir)))
  15936. buf rtn b e)
  15937. (with-current-buffer frombuf
  15938. (icalendar-export-region (point-min) (point-max) tmpfile)
  15939. (setq buf (find-buffer-visiting tmpfile))
  15940. (set-buffer buf)
  15941. (goto-char (point-min))
  15942. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15943. (setq b (match-beginning 0)))
  15944. (goto-char (point-max))
  15945. (if (re-search-backward "^END:VEVENT" nil t)
  15946. (setq e (match-end 0)))
  15947. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15948. (kill-buffer buf)
  15949. (delete-file tmpfile)
  15950. rtn))
  15951. (defun org-closest-date (start current change prefer show-all)
  15952. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15953. When PREFER is `past', return a date that is either CURRENT or past.
  15954. When PREFER is `future', return a date that is either CURRENT or future.
  15955. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15956. ;; Make the proper lists from the dates
  15957. (catch 'exit
  15958. (let ((a1 '(("h" . hour)
  15959. ("d" . day)
  15960. ("w" . week)
  15961. ("m" . month)
  15962. ("y" . year)))
  15963. (shour (nth 2 (org-parse-time-string start)))
  15964. dn dw sday cday n1 n2 n0
  15965. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15966. (setq start (org-date-to-gregorian start)
  15967. current (org-date-to-gregorian
  15968. (if show-all
  15969. current
  15970. (time-to-days (current-time))))
  15971. sday (calendar-absolute-from-gregorian start)
  15972. cday (calendar-absolute-from-gregorian current))
  15973. (if (<= cday sday) (throw 'exit sday))
  15974. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15975. (setq dn (string-to-number (match-string 1 change))
  15976. dw (cdr (assoc (match-string 2 change) a1)))
  15977. (user-error "Invalid change specifier: %s" change))
  15978. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15979. (cond
  15980. ((eq dw 'hour)
  15981. (let ((missing-hours
  15982. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15983. dn)))
  15984. (setq n1 (if (zerop missing-hours) cday
  15985. (- cday (1+ (floor (/ missing-hours 24)))))
  15986. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15987. ((eq dw 'day)
  15988. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15989. n2 (+ n1 dn)))
  15990. ((eq dw 'year)
  15991. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15992. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15993. (setq date1 (list m d y1)
  15994. n1 (calendar-absolute-from-gregorian date1)
  15995. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15996. n2 (calendar-absolute-from-gregorian date2)))
  15997. ((eq dw 'month)
  15998. ;; approx number of month between the two dates
  15999. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16000. ;; How often does dn fit in there?
  16001. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16002. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16003. m (+ m nm)
  16004. ny (floor (/ m 12))
  16005. y (+ y ny)
  16006. m (- m (* ny 12)))
  16007. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16008. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16009. (setq m2 (+ m dn) y2 y)
  16010. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16011. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16012. (while (<= n2 cday)
  16013. (setq n1 n2 m m2 y y2)
  16014. (setq m2 (+ m dn) y2 y)
  16015. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16016. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16017. ;; Make sure n1 is the earlier date
  16018. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  16019. (if show-all
  16020. (cond
  16021. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16022. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16023. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16024. (cond
  16025. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16026. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16027. (t (if (= cday n1) n1 n2)))))))
  16028. (defun org-date-to-gregorian (date)
  16029. "Turn any specification of DATE into a Gregorian date for the calendar."
  16030. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16031. ((and (listp date) (= (length date) 3)) date)
  16032. ((stringp date)
  16033. (setq date (org-parse-time-string date))
  16034. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16035. ((listp date)
  16036. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16037. (defun org-parse-time-string (s &optional nodefault)
  16038. "Parse the standard Org-mode time string.
  16039. This should be a lot faster than the normal `parse-time-string'.
  16040. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16041. hour and minute fields will be nil if not given."
  16042. (cond ((string-match org-ts-regexp0 s)
  16043. (list 0
  16044. (if (or (match-beginning 8) (not nodefault))
  16045. (string-to-number (or (match-string 8 s) "0")))
  16046. (if (or (match-beginning 7) (not nodefault))
  16047. (string-to-number (or (match-string 7 s) "0")))
  16048. (string-to-number (match-string 4 s))
  16049. (string-to-number (match-string 3 s))
  16050. (string-to-number (match-string 2 s))
  16051. nil nil nil))
  16052. ((string-match "^<[^>]+>$" s)
  16053. (decode-time (seconds-to-time (org-matcher-time s))))
  16054. (t (error "Not a standard Org-mode time string: %s" s))))
  16055. (defun org-timestamp-up (&optional arg)
  16056. "Increase the date item at the cursor by one.
  16057. If the cursor is on the year, change the year. If it is on the month,
  16058. the day or the time, change that.
  16059. With prefix ARG, change by that many units."
  16060. (interactive "p")
  16061. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16062. (defun org-timestamp-down (&optional arg)
  16063. "Decrease the date item at the cursor by one.
  16064. If the cursor is on the year, change the year. If it is on the month,
  16065. the day or the time, change that.
  16066. With prefix ARG, change by that many units."
  16067. (interactive "p")
  16068. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16069. (defun org-timestamp-up-day (&optional arg)
  16070. "Increase the date in the time stamp by one day.
  16071. With prefix ARG, change that many days."
  16072. (interactive "p")
  16073. (if (and (not (org-at-timestamp-p t))
  16074. (org-at-heading-p))
  16075. (org-todo 'up)
  16076. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16077. (defun org-timestamp-down-day (&optional arg)
  16078. "Decrease the date in the time stamp by one day.
  16079. With prefix ARG, change that many days."
  16080. (interactive "p")
  16081. (if (and (not (org-at-timestamp-p t))
  16082. (org-at-heading-p))
  16083. (org-todo 'down)
  16084. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16085. (defun org-at-timestamp-p (&optional inactive-ok)
  16086. "Determine if the cursor is in or at a timestamp."
  16087. (interactive)
  16088. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16089. (pos (point))
  16090. (ans (or (looking-at tsr)
  16091. (save-excursion
  16092. (skip-chars-backward "^[<\n\r\t")
  16093. (if (> (point) (point-min)) (backward-char 1))
  16094. (and (looking-at tsr)
  16095. (> (- (match-end 0) pos) -1))))))
  16096. (and ans
  16097. (boundp 'org-ts-what)
  16098. (setq org-ts-what
  16099. (cond
  16100. ((= pos (match-beginning 0)) 'bracket)
  16101. ;; Point is considered to be "on the bracket" whether
  16102. ;; it's really on it or right after it.
  16103. ((= pos (1- (match-end 0))) 'bracket)
  16104. ((= pos (match-end 0)) 'after)
  16105. ((org-pos-in-match-range pos 2) 'year)
  16106. ((org-pos-in-match-range pos 3) 'month)
  16107. ((org-pos-in-match-range pos 7) 'hour)
  16108. ((org-pos-in-match-range pos 8) 'minute)
  16109. ((or (org-pos-in-match-range pos 4)
  16110. (org-pos-in-match-range pos 5)) 'day)
  16111. ((and (> pos (or (match-end 8) (match-end 5)))
  16112. (< pos (match-end 0)))
  16113. (- pos (or (match-end 8) (match-end 5))))
  16114. (t 'day))))
  16115. ans))
  16116. (defun org-toggle-timestamp-type ()
  16117. "Toggle the type (<active> or [inactive]) of a time stamp."
  16118. (interactive)
  16119. (when (org-at-timestamp-p t)
  16120. (let ((beg (match-beginning 0)) (end (match-end 0))
  16121. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16122. (save-excursion
  16123. (goto-char beg)
  16124. (while (re-search-forward "[][<>]" end t)
  16125. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16126. t t)))
  16127. (message "Timestamp is now %sactive"
  16128. (if (equal (char-after beg) ?<) "" "in")))))
  16129. (defun org-at-clock-log-p nil
  16130. "Is the cursor on the clock log line?"
  16131. (save-excursion
  16132. (move-beginning-of-line 1)
  16133. (looking-at org-clock-line-re)))
  16134. (defvar org-clock-history) ; defined in org-clock.el
  16135. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16136. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16137. "Change the date in the time stamp at point.
  16138. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16139. `year', `minute', `second'. If WHAT is not given, the cursor position
  16140. in the timestamp determines what will be changed.
  16141. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16142. (let ((origin (point)) origin-cat
  16143. with-hm inactive
  16144. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16145. org-ts-what
  16146. extra rem
  16147. ts time time0 fixnext clrgx)
  16148. (if (not (org-at-timestamp-p t))
  16149. (user-error "Not at a timestamp"))
  16150. (if (and (not what) (eq org-ts-what 'bracket))
  16151. (org-toggle-timestamp-type)
  16152. ;; Point isn't on brackets. Remember the part of the time-stamp
  16153. ;; the point was in. Indeed, size of time-stamps may change,
  16154. ;; but point must be kept in the same category nonetheless.
  16155. (setq origin-cat org-ts-what)
  16156. (if (and (not what) (not (eq org-ts-what 'day))
  16157. org-display-custom-times
  16158. (get-text-property (point) 'display)
  16159. (not (get-text-property (1- (point)) 'display)))
  16160. (setq org-ts-what 'day))
  16161. (setq org-ts-what (or what org-ts-what)
  16162. inactive (= (char-after (match-beginning 0)) ?\[)
  16163. ts (match-string 0))
  16164. (replace-match "")
  16165. (when (string-match
  16166. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16167. ts)
  16168. (setq extra (match-string 1 ts))
  16169. (if suppress-tmp-delay
  16170. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16171. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16172. (setq with-hm t))
  16173. (setq time0 (org-parse-time-string ts))
  16174. (when (and updown
  16175. (eq org-ts-what 'minute)
  16176. (not current-prefix-arg))
  16177. ;; This looks like s-up and s-down. Change by one rounding step.
  16178. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16179. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  16180. (setcar (cdr time0) (+ (nth 1 time0)
  16181. (if (> n 0) (- rem) (- dm rem))))))
  16182. (setq time
  16183. (encode-time (or (car time0) 0)
  16184. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16185. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16186. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16187. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16188. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16189. (nthcdr 6 time0)))
  16190. (when (and (member org-ts-what '(hour minute))
  16191. extra
  16192. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16193. (setq extra (org-modify-ts-extra
  16194. extra
  16195. (if (eq org-ts-what 'hour) 2 5)
  16196. n dm)))
  16197. (when (integerp org-ts-what)
  16198. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16199. (if (eq what 'calendar)
  16200. (let ((cal-date (org-get-date-from-calendar)))
  16201. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16202. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16203. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16204. (setcar time0 (or (car time0) 0))
  16205. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16206. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16207. (setq time (apply 'encode-time time0))))
  16208. ;; Insert the new time-stamp, and ensure point stays in the same
  16209. ;; category as before (i.e. not after the last position in that
  16210. ;; category).
  16211. (let ((pos (point)))
  16212. ;; Stay before inserted string. `save-excursion' is of no use.
  16213. (setq org-last-changed-timestamp
  16214. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16215. (goto-char pos))
  16216. (save-match-data
  16217. (looking-at org-ts-regexp3)
  16218. (goto-char (cond
  16219. ;; `day' category ends before `hour' if any, or at
  16220. ;; the end of the day name.
  16221. ((eq origin-cat 'day)
  16222. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16223. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16224. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16225. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16226. ;; `year' and `month' have both fixed size: point
  16227. ;; couldn't have moved into another part.
  16228. (t origin))))
  16229. ;; Update clock if on a CLOCK line.
  16230. (org-clock-update-time-maybe)
  16231. ;; Maybe adjust the closest clock in `org-clock-history'
  16232. (when org-clock-adjust-closest
  16233. (if (not (and (org-at-clock-log-p)
  16234. (< 1 (length (delq nil (mapcar 'marker-position
  16235. org-clock-history))))))
  16236. (message "No clock to adjust")
  16237. (cond ((save-excursion ; fix previous clock?
  16238. (re-search-backward org-ts-regexp0 nil t)
  16239. (org-looking-back (concat org-clock-string " \\[")))
  16240. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16241. ((save-excursion ; fix next clock?
  16242. (re-search-backward org-ts-regexp0 nil t)
  16243. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16244. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16245. (save-window-excursion
  16246. ;; Find closest clock to point, adjust the previous/next one in history
  16247. (let* ((p (save-excursion (org-back-to-heading t)))
  16248. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16249. (clfixnth
  16250. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16251. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16252. (if (not clfixpos)
  16253. (message "No clock to adjust")
  16254. (save-excursion
  16255. (org-goto-marker-or-bmk clfixpos)
  16256. (org-show-subtree)
  16257. (when (re-search-forward clrgx nil t)
  16258. (goto-char (match-beginning 1))
  16259. (let (org-clock-adjust-closest)
  16260. (org-timestamp-change n org-ts-what updown))
  16261. (message "Clock adjusted in %s for heading: %s"
  16262. (file-name-nondirectory (buffer-file-name))
  16263. (org-get-heading t t)))))))))
  16264. ;; Try to recenter the calendar window, if any.
  16265. (if (and org-calendar-follow-timestamp-change
  16266. (get-buffer-window "*Calendar*" t)
  16267. (memq org-ts-what '(day month year)))
  16268. (org-recenter-calendar (time-to-days time))))))
  16269. (defun org-modify-ts-extra (s pos n dm)
  16270. "Change the different parts of the lead-time and repeat fields in timestamp."
  16271. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16272. ng h m new rem)
  16273. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16274. (cond
  16275. ((or (org-pos-in-match-range pos 2)
  16276. (org-pos-in-match-range pos 3))
  16277. (setq m (string-to-number (match-string 3 s))
  16278. h (string-to-number (match-string 2 s)))
  16279. (if (org-pos-in-match-range pos 2)
  16280. (setq h (+ h n))
  16281. (setq n (* dm (org-no-warnings (signum n))))
  16282. (when (not (= 0 (setq rem (% m dm))))
  16283. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16284. (setq m (+ m n)))
  16285. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16286. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16287. (setq h (min 24 (max 0 h)))
  16288. (setq ng 1 new (format "-%02d:%02d" h m)))
  16289. ((org-pos-in-match-range pos 6)
  16290. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16291. ((org-pos-in-match-range pos 5)
  16292. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16293. ((org-pos-in-match-range pos 9)
  16294. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16295. ((org-pos-in-match-range pos 8)
  16296. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16297. (when ng
  16298. (setq s (concat
  16299. (substring s 0 (match-beginning ng))
  16300. new
  16301. (substring s (match-end ng))))))
  16302. s))
  16303. (defun org-recenter-calendar (date)
  16304. "If the calendar is visible, recenter it to DATE."
  16305. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16306. (when cwin
  16307. (let ((calendar-move-hook nil))
  16308. (with-selected-window cwin
  16309. (calendar-goto-date (if (listp date) date
  16310. (calendar-gregorian-from-absolute date))))))))
  16311. (defun org-goto-calendar (&optional arg)
  16312. "Go to the Emacs calendar at the current date.
  16313. If there is a time stamp in the current line, go to that date.
  16314. A prefix ARG can be used to force the current date."
  16315. (interactive "P")
  16316. (let ((tsr org-ts-regexp) diff
  16317. (calendar-move-hook nil)
  16318. (calendar-view-holidays-initially-flag nil)
  16319. (calendar-view-diary-initially-flag nil))
  16320. (if (or (org-at-timestamp-p)
  16321. (save-excursion
  16322. (beginning-of-line 1)
  16323. (looking-at (concat ".*" tsr))))
  16324. (let ((d1 (time-to-days (current-time)))
  16325. (d2 (time-to-days
  16326. (org-time-string-to-time (match-string 1)))))
  16327. (setq diff (- d2 d1))))
  16328. (calendar)
  16329. (calendar-goto-today)
  16330. (if (and diff (not arg)) (calendar-forward-day diff))))
  16331. (defun org-get-date-from-calendar ()
  16332. "Return a list (month day year) of date at point in calendar."
  16333. (with-current-buffer "*Calendar*"
  16334. (save-match-data
  16335. (calendar-cursor-to-date))))
  16336. (defun org-date-from-calendar ()
  16337. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16338. If there is already a time stamp at the cursor position, update it."
  16339. (interactive)
  16340. (if (org-at-timestamp-p t)
  16341. (org-timestamp-change 0 'calendar)
  16342. (let ((cal-date (org-get-date-from-calendar)))
  16343. (org-insert-time-stamp
  16344. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16345. (defcustom org-effort-durations
  16346. `(("h" . 60)
  16347. ("d" . ,(* 60 8))
  16348. ("w" . ,(* 60 8 5))
  16349. ("m" . ,(* 60 8 5 4))
  16350. ("y" . ,(* 60 8 5 40)))
  16351. "Conversion factor to minutes for an effort modifier.
  16352. Each entry has the form (MODIFIER . MINUTES).
  16353. In an effort string, a number followed by MODIFIER is multiplied
  16354. by the specified number of MINUTES to obtain an effort in
  16355. minutes.
  16356. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16357. effort string \"2hours\" is equivalent to 120 minutes."
  16358. :group 'org-agenda
  16359. :version "24.1"
  16360. :type '(alist :key-type (string :tag "Modifier")
  16361. :value-type (number :tag "Minutes")))
  16362. (defun org-minutes-to-clocksum-string (m)
  16363. "Format number of minutes as a clocksum string.
  16364. The format is determined by `org-time-clocksum-format',
  16365. `org-time-clocksum-use-fractional' and
  16366. `org-time-clocksum-fractional-format' and
  16367. `org-time-clocksum-use-effort-durations'."
  16368. (let ((clocksum "")
  16369. (m (round m)) ; Don't allow fractions of minutes
  16370. h d w mo y fmt n)
  16371. (setq h (if org-time-clocksum-use-effort-durations
  16372. (cdr (assoc "h" org-effort-durations)) 60)
  16373. d (if org-time-clocksum-use-effort-durations
  16374. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16375. w (if org-time-clocksum-use-effort-durations
  16376. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16377. mo (if org-time-clocksum-use-effort-durations
  16378. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16379. y (if org-time-clocksum-use-effort-durations
  16380. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16381. ;; fractional format
  16382. (if org-time-clocksum-use-fractional
  16383. (cond
  16384. ;; single format string
  16385. ((stringp org-time-clocksum-fractional-format)
  16386. (format org-time-clocksum-fractional-format (/ m (float h))))
  16387. ;; choice of fractional formats for different time units
  16388. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16389. (> (/ (truncate m) (* y d h)) 0))
  16390. (format fmt (/ m (* y d (float h)))))
  16391. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16392. (> (/ (truncate m) (* mo d h)) 0))
  16393. (format fmt (/ m (* mo d (float h)))))
  16394. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16395. (> (/ (truncate m) (* w d h)) 0))
  16396. (format fmt (/ m (* w d (float h)))))
  16397. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16398. (> (/ (truncate m) (* d h)) 0))
  16399. (format fmt (/ m (* d (float h)))))
  16400. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16401. (> (/ (truncate m) h) 0))
  16402. (format fmt (/ m (float h))))
  16403. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16404. (format fmt m))
  16405. ;; fall back to smallest time unit with a format
  16406. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16407. (format fmt (/ m (float h))))
  16408. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16409. (format fmt (/ m (* d (float h)))))
  16410. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16411. (format fmt (/ m (* w d (float h)))))
  16412. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16413. (format fmt (/ m (* mo d (float h)))))
  16414. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16415. (format fmt (/ m (* y d (float h))))))
  16416. ;; standard (non-fractional) format, with single format string
  16417. (if (stringp org-time-clocksum-format)
  16418. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16419. ;; separate formats components
  16420. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16421. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16422. (plist-get org-time-clocksum-format :require-years))
  16423. (setq clocksum (concat clocksum (format fmt n))
  16424. m (- m (* n y d h))))
  16425. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16426. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16427. (plist-get org-time-clocksum-format :require-months))
  16428. (setq clocksum (concat clocksum (format fmt n))
  16429. m (- m (* n mo d h))))
  16430. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16431. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16432. (plist-get org-time-clocksum-format :require-weeks))
  16433. (setq clocksum (concat clocksum (format fmt n))
  16434. m (- m (* n w d h))))
  16435. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16436. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16437. (plist-get org-time-clocksum-format :require-days))
  16438. (setq clocksum (concat clocksum (format fmt n))
  16439. m (- m (* n d h))))
  16440. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16441. (or (> (setq n (/ (truncate m) h)) 0)
  16442. (plist-get org-time-clocksum-format :require-hours))
  16443. (setq clocksum (concat clocksum (format fmt n))
  16444. m (- m (* n h))))
  16445. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16446. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16447. (setq clocksum (concat clocksum (format fmt m))))
  16448. ;; return formatted time duration
  16449. clocksum))))
  16450. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16451. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16452. "Org mode version 8.0")
  16453. (defun org-hours-to-clocksum-string (n)
  16454. (org-minutes-to-clocksum-string (* n 60)))
  16455. (defun org-hh:mm-string-to-minutes (s)
  16456. "Convert a string H:MM to a number of minutes.
  16457. If the string is just a number, interpret it as minutes.
  16458. In fact, the first hh:mm or number in the string will be taken,
  16459. there can be extra stuff in the string.
  16460. If no number is found, the return value is 0."
  16461. (cond
  16462. ((integerp s) s)
  16463. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16464. (+ (* (string-to-number (match-string 1 s)) 60)
  16465. (string-to-number (match-string 2 s))))
  16466. ((string-match "\\([0-9]+\\)" s)
  16467. (string-to-number (match-string 1 s)))
  16468. (t 0)))
  16469. (defcustom org-image-actual-width t
  16470. "Should we use the actual width of images when inlining them?
  16471. When set to `t', always use the image width.
  16472. When set to a number, use imagemagick (when available) to set
  16473. the image's width to this value.
  16474. When set to a number in a list, try to get the width from any
  16475. #+ATTR.* keyword if it matches a width specification like
  16476. #+ATTR_HTML: :width 300px
  16477. and fall back on that number if none is found.
  16478. When set to nil, try to get the width from an #+ATTR.* keyword
  16479. and fall back on the original width if none is found.
  16480. This requires Emacs >= 24.1, build with imagemagick support."
  16481. :group 'org-appearance
  16482. :version "24.4"
  16483. :package-version '(Org . "8.0")
  16484. :type '(choice
  16485. (const :tag "Use the image width" t)
  16486. (integer :tag "Use a number of pixels")
  16487. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16488. (const :tag "Use #+ATTR* or don't resize" nil)))
  16489. (defcustom org-agenda-inhibit-startup nil
  16490. "Inhibit startup when preparing agenda buffers.
  16491. When this variable is `t', the initialization of the Org agenda
  16492. buffers is inhibited: e.g. the visibility state is not set, the
  16493. tables are not re-aligned, etc."
  16494. :type 'boolean
  16495. :version "24.3"
  16496. :group 'org-agenda)
  16497. (define-obsolete-variable-alias
  16498. 'org-agenda-ignore-drawer-properties
  16499. 'org-agenda-ignore-properties "24.5")
  16500. (defcustom org-agenda-ignore-properties nil
  16501. "Avoid updating text properties when building the agenda.
  16502. Properties are used to prepare buffers for effort estimates,
  16503. appointments, statistics and subtree-local categories.
  16504. If you don't use these in the agenda, you can add them to this
  16505. list and agenda building will be a bit faster.
  16506. The value is a list, with zero or more of the symbols `effort', `appt',
  16507. `stats' or `category'."
  16508. :type '(set :greedy t
  16509. (const effort)
  16510. (const appt)
  16511. (const stats)
  16512. (const category))
  16513. :version "24.5"
  16514. :package-version '(Org . "8.3")
  16515. :group 'org-agenda)
  16516. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16517. "Convert a duration string S to minutes.
  16518. A bare number is interpreted as minutes, modifiers can be set by
  16519. customizing `org-effort-durations' (which see).
  16520. Entries containing a colon are interpreted as H:MM by
  16521. `org-hh:mm-string-to-minutes'."
  16522. (let ((result 0)
  16523. (re (concat "\\([0-9.]+\\) *\\("
  16524. (regexp-opt (mapcar 'car org-effort-durations))
  16525. "\\)")))
  16526. (while (string-match re s)
  16527. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16528. (string-to-number (match-string 1 s))))
  16529. (setq s (replace-match "" nil t s)))
  16530. (setq result (floor result))
  16531. (incf result (org-hh:mm-string-to-minutes s))
  16532. (if output-to-string (number-to-string result) result)))
  16533. ;;;; Files
  16534. (defun org-save-all-org-buffers ()
  16535. "Save all Org-mode buffers without user confirmation."
  16536. (interactive)
  16537. (message "Saving all Org-mode buffers...")
  16538. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16539. (when (featurep 'org-id) (org-id-locations-save))
  16540. (message "Saving all Org-mode buffers... done"))
  16541. (defun org-revert-all-org-buffers ()
  16542. "Revert all Org-mode buffers.
  16543. Prompt for confirmation when there are unsaved changes.
  16544. Be sure you know what you are doing before letting this function
  16545. overwrite your changes.
  16546. This function is useful in a setup where one tracks org files
  16547. with a version control system, to revert on one machine after pulling
  16548. changes from another. I believe the procedure must be like this:
  16549. 1. M-x org-save-all-org-buffers
  16550. 2. Pull changes from the other machine, resolve conflicts
  16551. 3. M-x org-revert-all-org-buffers"
  16552. (interactive)
  16553. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16554. (user-error "Abort"))
  16555. (save-excursion
  16556. (save-window-excursion
  16557. (mapc
  16558. (lambda (b)
  16559. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16560. (with-current-buffer b buffer-file-name))
  16561. (org-pop-to-buffer-same-window b)
  16562. (revert-buffer t 'no-confirm)))
  16563. (buffer-list))
  16564. (when (and (featurep 'org-id) org-id-track-globally)
  16565. (org-id-locations-load)))))
  16566. ;;;; Agenda files
  16567. ;;;###autoload
  16568. (defun org-switchb (&optional arg)
  16569. "Switch between Org buffers.
  16570. With one prefix argument, restrict available buffers to files.
  16571. With two prefix arguments, restrict available buffers to agenda files.
  16572. Defaults to `iswitchb' for buffer name completion.
  16573. Set `org-completion-use-ido' to make it use ido instead."
  16574. (interactive "P")
  16575. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16576. ((equal arg '(16)) (org-buffer-list 'agenda))
  16577. (t (org-buffer-list))))
  16578. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16579. (org-completion-use-ido org-completion-use-ido))
  16580. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16581. (setq org-completion-use-iswitchb t))
  16582. (org-pop-to-buffer-same-window
  16583. (org-icompleting-read "Org buffer: "
  16584. (mapcar 'list (mapcar 'buffer-name blist))
  16585. nil t))))
  16586. ;;; Define some older names previously used for this functionality
  16587. ;;;###autoload
  16588. (defalias 'org-ido-switchb 'org-switchb)
  16589. ;;;###autoload
  16590. (defalias 'org-iswitchb 'org-switchb)
  16591. (defun org-buffer-list (&optional predicate exclude-tmp)
  16592. "Return a list of Org buffers.
  16593. PREDICATE can be `export', `files' or `agenda'.
  16594. export restrict the list to Export buffers.
  16595. files restrict the list to buffers visiting Org files.
  16596. agenda restrict the list to buffers visiting agenda files.
  16597. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16598. (let* ((bfn nil)
  16599. (agenda-files (and (eq predicate 'agenda)
  16600. (mapcar 'file-truename (org-agenda-files t))))
  16601. (filter
  16602. (cond
  16603. ((eq predicate 'files)
  16604. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16605. ((eq predicate 'export)
  16606. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16607. ((eq predicate 'agenda)
  16608. (lambda (b)
  16609. (with-current-buffer b
  16610. (and (derived-mode-p 'org-mode)
  16611. (setq bfn (buffer-file-name b))
  16612. (member (file-truename bfn) agenda-files)))))
  16613. (t (lambda (b) (with-current-buffer b
  16614. (or (derived-mode-p 'org-mode)
  16615. (string-match "\*Org .*Export"
  16616. (buffer-name b)))))))))
  16617. (delq nil
  16618. (mapcar
  16619. (lambda(b)
  16620. (if (and (funcall filter b)
  16621. (or (not exclude-tmp)
  16622. (not (string-match "tmp" (buffer-name b)))))
  16623. b
  16624. nil))
  16625. (buffer-list)))))
  16626. (defun org-agenda-files (&optional unrestricted archives)
  16627. "Get the list of agenda files.
  16628. Optional UNRESTRICTED means return the full list even if a restriction
  16629. is currently in place.
  16630. When ARCHIVES is t, include all archive files that are really being
  16631. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16632. `org-agenda-archives-mode' is t."
  16633. (let ((files
  16634. (cond
  16635. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16636. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16637. ((listp org-agenda-files) org-agenda-files)
  16638. (t (error "Invalid value of `org-agenda-files'")))))
  16639. (setq files (apply 'append
  16640. (mapcar (lambda (f)
  16641. (if (file-directory-p f)
  16642. (directory-files
  16643. f t org-agenda-file-regexp)
  16644. (list f)))
  16645. files)))
  16646. (when org-agenda-skip-unavailable-files
  16647. (setq files (delq nil
  16648. (mapcar (function
  16649. (lambda (file)
  16650. (and (file-readable-p file) file)))
  16651. files))))
  16652. (when (or (eq archives t)
  16653. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16654. (setq files (org-add-archive-files files)))
  16655. files))
  16656. (defun org-agenda-file-p (&optional file)
  16657. "Return non-nil, if FILE is an agenda file.
  16658. If FILE is omitted, use the file associated with the current
  16659. buffer."
  16660. (member (or file (buffer-file-name))
  16661. (org-agenda-files t)))
  16662. (defun org-edit-agenda-file-list ()
  16663. "Edit the list of agenda files.
  16664. Depending on setup, this either uses customize to edit the variable
  16665. `org-agenda-files', or it visits the file that is holding the list. In the
  16666. latter case, the buffer is set up in a way that saving it automatically kills
  16667. the buffer and restores the previous window configuration."
  16668. (interactive)
  16669. (if (stringp org-agenda-files)
  16670. (let ((cw (current-window-configuration)))
  16671. (find-file org-agenda-files)
  16672. (org-set-local 'org-window-configuration cw)
  16673. (org-add-hook 'after-save-hook
  16674. (lambda ()
  16675. (set-window-configuration
  16676. (prog1 org-window-configuration
  16677. (kill-buffer (current-buffer))))
  16678. (org-install-agenda-files-menu)
  16679. (message "New agenda file list installed"))
  16680. nil 'local)
  16681. (message "%s" (substitute-command-keys
  16682. "Edit list and finish with \\[save-buffer]")))
  16683. (customize-variable 'org-agenda-files)))
  16684. (defun org-store-new-agenda-file-list (list)
  16685. "Set new value for the agenda file list and save it correctly."
  16686. (if (stringp org-agenda-files)
  16687. (let ((fe (org-read-agenda-file-list t)) b u)
  16688. (while (setq b (find-buffer-visiting org-agenda-files))
  16689. (kill-buffer b))
  16690. (with-temp-file org-agenda-files
  16691. (insert
  16692. (mapconcat
  16693. (lambda (f) ;; Keep un-expanded entries.
  16694. (if (setq u (assoc f fe))
  16695. (cdr u)
  16696. f))
  16697. list "\n")
  16698. "\n")))
  16699. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16700. (org-insert-mode-line-in-empty-file nil))
  16701. (setq org-agenda-files list)
  16702. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16703. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16704. "Read the list of agenda files from a file.
  16705. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16706. filenames, used by `org-store-new-agenda-file-list' to write back
  16707. un-expanded file names."
  16708. (when (file-directory-p org-agenda-files)
  16709. (error "`org-agenda-files' cannot be a single directory"))
  16710. (when (stringp org-agenda-files)
  16711. (with-temp-buffer
  16712. (insert-file-contents org-agenda-files)
  16713. (mapcar
  16714. (lambda (f)
  16715. (let ((e (expand-file-name (substitute-in-file-name f)
  16716. org-directory)))
  16717. (if pair-with-expansion
  16718. (cons e f)
  16719. e)))
  16720. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16721. ;;;###autoload
  16722. (defun org-cycle-agenda-files ()
  16723. "Cycle through the files in `org-agenda-files'.
  16724. If the current buffer visits an agenda file, find the next one in the list.
  16725. If the current buffer does not, find the first agenda file."
  16726. (interactive)
  16727. (let* ((fs (org-agenda-files t))
  16728. (files (append fs (list (car fs))))
  16729. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16730. file)
  16731. (unless files (user-error "No agenda files"))
  16732. (catch 'exit
  16733. (while (setq file (pop files))
  16734. (if (equal (file-truename file) tcf)
  16735. (when (car files)
  16736. (find-file (car files))
  16737. (throw 'exit t))))
  16738. (find-file (car fs)))
  16739. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16740. (defun org-agenda-file-to-front (&optional to-end)
  16741. "Move/add the current file to the top of the agenda file list.
  16742. If the file is not present in the list, it is added to the front. If it is
  16743. present, it is moved there. With optional argument TO-END, add/move to the
  16744. end of the list."
  16745. (interactive "P")
  16746. (let ((org-agenda-skip-unavailable-files nil)
  16747. (file-alist (mapcar (lambda (x)
  16748. (cons (file-truename x) x))
  16749. (org-agenda-files t)))
  16750. (ctf (file-truename
  16751. (or buffer-file-name
  16752. (user-error "Please save the current buffer to a file"))))
  16753. x had)
  16754. (setq x (assoc ctf file-alist) had x)
  16755. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16756. (if to-end
  16757. (setq file-alist (append (delq x file-alist) (list x)))
  16758. (setq file-alist (cons x (delq x file-alist))))
  16759. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16760. (org-install-agenda-files-menu)
  16761. (message "File %s to %s of agenda file list"
  16762. (if had "moved" "added") (if to-end "end" "front"))))
  16763. (defun org-remove-file (&optional file)
  16764. "Remove current file from the list of files in variable `org-agenda-files'.
  16765. These are the files which are being checked for agenda entries.
  16766. Optional argument FILE means use this file instead of the current."
  16767. (interactive)
  16768. (let* ((org-agenda-skip-unavailable-files nil)
  16769. (file (or file buffer-file-name
  16770. (user-error "Current buffer does not visit a file")))
  16771. (true-file (file-truename file))
  16772. (afile (abbreviate-file-name file))
  16773. (files (delq nil (mapcar
  16774. (lambda (x)
  16775. (if (equal true-file
  16776. (file-truename x))
  16777. nil x))
  16778. (org-agenda-files t)))))
  16779. (if (not (= (length files) (length (org-agenda-files t))))
  16780. (progn
  16781. (org-store-new-agenda-file-list files)
  16782. (org-install-agenda-files-menu)
  16783. (message "Removed file: %s" afile))
  16784. (message "File was not in list: %s (not removed)" afile))))
  16785. (defun org-file-menu-entry (file)
  16786. (vector file (list 'find-file file) t))
  16787. (defun org-check-agenda-file (file)
  16788. "Make sure FILE exists. If not, ask user what to do."
  16789. (when (not (file-exists-p file))
  16790. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16791. (abbreviate-file-name file))
  16792. (let ((r (downcase (read-char-exclusive))))
  16793. (cond
  16794. ((equal r ?r)
  16795. (org-remove-file file)
  16796. (throw 'nextfile t))
  16797. (t (user-error "Abort"))))))
  16798. (defun org-get-agenda-file-buffer (file)
  16799. "Get an agenda buffer visiting FILE.
  16800. If the buffer needs to be created, add it to the list of buffers
  16801. which might be released later."
  16802. (let ((buf (org-find-base-buffer-visiting file)))
  16803. (if buf
  16804. buf ; just return it
  16805. ;; Make a new buffer and remember it
  16806. (setq buf (find-file-noselect file))
  16807. (if buf (push buf org-agenda-new-buffers))
  16808. buf)))
  16809. (defun org-release-buffers (blist)
  16810. "Release all buffers in list, asking the user for confirmation when needed.
  16811. When a buffer is unmodified, it is just killed. When modified, it is saved
  16812. \(if the user agrees) and then killed."
  16813. (let (buf file)
  16814. (while (setq buf (pop blist))
  16815. (setq file (buffer-file-name buf))
  16816. (when (and (buffer-modified-p buf)
  16817. file
  16818. (y-or-n-p (format "Save file %s? " file)))
  16819. (with-current-buffer buf (save-buffer)))
  16820. (kill-buffer buf))))
  16821. (defun org-agenda-prepare-buffers (files)
  16822. "Create buffers for all agenda files, protect archived trees and comments."
  16823. (interactive)
  16824. (let ((pa '(:org-archived t))
  16825. (pc '(:org-comment t))
  16826. (pall '(:org-archived t :org-comment t))
  16827. (inhibit-read-only t)
  16828. (org-inhibit-startup org-agenda-inhibit-startup)
  16829. (rea (concat ":" org-archive-tag ":"))
  16830. file re pos)
  16831. (setq org-tag-alist-for-agenda nil
  16832. org-tag-groups-alist-for-agenda nil)
  16833. (save-excursion
  16834. (save-restriction
  16835. (while (setq file (pop files))
  16836. (catch 'nextfile
  16837. (if (bufferp file)
  16838. (set-buffer file)
  16839. (org-check-agenda-file file)
  16840. (set-buffer (org-get-agenda-file-buffer file)))
  16841. (widen)
  16842. (org-set-regexps-and-options-for-tags)
  16843. (setq pos (point))
  16844. (goto-char (point-min))
  16845. (let ((case-fold-search t))
  16846. (when (search-forward "#+setupfile" nil t)
  16847. ;; Don't set all regexps and options systematically as
  16848. ;; this is only run for setting agenda tags from setup
  16849. ;; file
  16850. (org-set-regexps-and-options)))
  16851. (or (memq 'category org-agenda-ignore-properties)
  16852. (org-refresh-category-properties))
  16853. (or (memq 'stats org-agenda-ignore-properties)
  16854. (org-refresh-stats-properties))
  16855. (or (memq 'effort org-agenda-ignore-properties)
  16856. (org-refresh-effort-properties))
  16857. (or (memq 'appt org-agenda-ignore-properties)
  16858. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  16859. (setq org-todo-keywords-for-agenda
  16860. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16861. (setq org-done-keywords-for-agenda
  16862. (append org-done-keywords-for-agenda org-done-keywords))
  16863. (setq org-todo-keyword-alist-for-agenda
  16864. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16865. (setq org-tag-alist-for-agenda
  16866. (org-uniquify
  16867. (append org-tag-alist-for-agenda
  16868. org-tag-alist
  16869. org-tag-persistent-alist)))
  16870. (if org-group-tags
  16871. (setq org-tag-groups-alist-for-agenda
  16872. (org-uniquify-alist
  16873. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16874. (org-with-silent-modifications
  16875. (save-excursion
  16876. (remove-text-properties (point-min) (point-max) pall)
  16877. (when org-agenda-skip-archived-trees
  16878. (goto-char (point-min))
  16879. (while (re-search-forward rea nil t)
  16880. (if (org-at-heading-p t)
  16881. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16882. (goto-char (point-min))
  16883. (setq re (format "^\\* .*\\<%s\\>" org-comment-string))
  16884. (while (re-search-forward re nil t)
  16885. (when (save-match-data (org-in-commented-heading-p t))
  16886. (add-text-properties
  16887. (match-beginning 0) (org-end-of-subtree t) pc)))))
  16888. (goto-char pos)))))
  16889. (setq org-todo-keywords-for-agenda
  16890. (org-uniquify org-todo-keywords-for-agenda))
  16891. (setq org-todo-keyword-alist-for-agenda
  16892. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16893. ;;;; CDLaTeX minor mode
  16894. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16895. "Keymap for the minor `org-cdlatex-mode'.")
  16896. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16897. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16898. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16899. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16900. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  16901. (defvar org-cdlatex-texmathp-advice-is-done nil
  16902. "Flag remembering if we have applied the advice to texmathp already.")
  16903. (define-minor-mode org-cdlatex-mode
  16904. "Toggle the minor `org-cdlatex-mode'.
  16905. This mode supports entering LaTeX environment and math in LaTeX fragments
  16906. in Org-mode.
  16907. \\{org-cdlatex-mode-map}"
  16908. nil " OCDL" nil
  16909. (when org-cdlatex-mode
  16910. (require 'cdlatex)
  16911. (run-hooks 'cdlatex-mode-hook)
  16912. (cdlatex-compute-tables))
  16913. (unless org-cdlatex-texmathp-advice-is-done
  16914. (setq org-cdlatex-texmathp-advice-is-done t)
  16915. (defadvice texmathp (around org-math-always-on activate)
  16916. "Always return t in org-mode buffers.
  16917. This is because we want to insert math symbols without dollars even outside
  16918. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16919. an embedded LaTeX fragment, let texmathp do its job.
  16920. \\[org-cdlatex-mode-map]"
  16921. (interactive)
  16922. (let (p)
  16923. (cond
  16924. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16925. ((eq this-command 'cdlatex-math-symbol)
  16926. (setq ad-return-value t
  16927. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16928. (t
  16929. (let ((p (org-inside-LaTeX-fragment-p)))
  16930. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16931. (setq ad-return-value t
  16932. texmathp-why '("Org-mode embedded math" . 0))
  16933. (if p ad-do-it)))))))))
  16934. (defun turn-on-org-cdlatex ()
  16935. "Unconditionally turn on `org-cdlatex-mode'."
  16936. (org-cdlatex-mode 1))
  16937. (defun org-try-cdlatex-tab ()
  16938. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16939. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16940. - inside a LaTeX fragment, or
  16941. - after the first word in a line, where an abbreviation expansion could
  16942. insert a LaTeX environment."
  16943. (when org-cdlatex-mode
  16944. (cond
  16945. ;; Before any word on the line: No expansion possible.
  16946. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16947. ;; Just after first word on the line: Expand it. Make sure it
  16948. ;; cannot happen on headlines, though.
  16949. ((save-excursion
  16950. (skip-chars-backward "a-zA-Z0-9*")
  16951. (skip-chars-backward " \t")
  16952. (and (bolp) (not (org-at-heading-p))))
  16953. (cdlatex-tab) t)
  16954. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16955. (defun org-cdlatex-underscore-caret (&optional arg)
  16956. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16957. Revert to the normal definition outside of these fragments."
  16958. (interactive "P")
  16959. (if (org-inside-LaTeX-fragment-p)
  16960. (call-interactively 'cdlatex-sub-superscript)
  16961. (let (org-cdlatex-mode)
  16962. (call-interactively (key-binding (vector last-input-event))))))
  16963. (defun org-cdlatex-math-modify (&optional arg)
  16964. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16965. Revert to the normal definition outside of these fragments."
  16966. (interactive "P")
  16967. (if (org-inside-LaTeX-fragment-p)
  16968. (call-interactively 'cdlatex-math-modify)
  16969. (let (org-cdlatex-mode)
  16970. (call-interactively (key-binding (vector last-input-event))))))
  16971. (defun org-cdlatex-environment-indent (&optional environment item)
  16972. "Execute `cdlatex-environment' and indent the inserted environment."
  16973. (interactive)
  16974. (cdlatex-environment environment item)
  16975. (let ((element (org-element-at-point)))
  16976. (org-indent-region (org-element-property :begin element)
  16977. (org-element-property :end element))))
  16978. ;;;; LaTeX fragments
  16979. (defun org-inside-LaTeX-fragment-p ()
  16980. "Test if point is inside a LaTeX fragment.
  16981. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16982. sequence appearing also before point.
  16983. Even though the matchers for math are configurable, this function assumes
  16984. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16985. delimiters are skipped when they have been removed by customization.
  16986. The return value is nil, or a cons cell with the delimiter and the
  16987. position of this delimiter.
  16988. This function does a reasonably good job, but can locally be fooled by
  16989. for example currency specifications. For example it will assume being in
  16990. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16991. fragments that are properly closed, but during editing, we have to live
  16992. with the uncertainty caused by missing closing delimiters. This function
  16993. looks only before point, not after."
  16994. (catch 'exit
  16995. (let ((pos (point))
  16996. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16997. (lim (progn
  16998. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16999. (point)))
  17000. dd-on str (start 0) m re)
  17001. (goto-char pos)
  17002. (when dodollar
  17003. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17004. re (nth 1 (assoc "$" org-latex-regexps)))
  17005. (while (string-match re str start)
  17006. (cond
  17007. ((= (match-end 0) (length str))
  17008. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17009. ((= (match-end 0) (- (length str) 5))
  17010. (throw 'exit nil))
  17011. (t (setq start (match-end 0))))))
  17012. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17013. (goto-char pos)
  17014. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17015. (and (match-beginning 2) (throw 'exit nil))
  17016. ;; count $$
  17017. (while (re-search-backward "\\$\\$" lim t)
  17018. (setq dd-on (not dd-on)))
  17019. (goto-char pos)
  17020. (if dd-on (cons "$$" m))))))
  17021. (defun org-inside-latex-macro-p ()
  17022. "Is point inside a LaTeX macro or its arguments?"
  17023. (save-match-data
  17024. (org-in-regexp
  17025. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17026. (defvar org-latex-fragment-image-overlays nil
  17027. "List of overlays carrying the images of latex fragments.")
  17028. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  17029. (defun org-remove-latex-fragment-image-overlays ()
  17030. "Remove all overlays with LaTeX fragment images in current buffer."
  17031. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  17032. (setq org-latex-fragment-image-overlays nil))
  17033. (define-obsolete-function-alias
  17034. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17035. (defun org-toggle-latex-fragment (&optional subtree)
  17036. "Preview the LaTeX fragment at point, or all locally or globally.
  17037. If the cursor is in a LaTeX fragment, create the image and overlay
  17038. it over the source code. If there is no fragment at point, display
  17039. all fragments in the current text, from one headline to the next. With
  17040. prefix SUBTREE, display all fragments in the current subtree. With a
  17041. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  17042. the cursor is before the first headline,
  17043. display all fragments in the buffer.
  17044. The images can be removed again with \\[org-toggle-latex-fragment]."
  17045. (interactive "P")
  17046. (unless buffer-file-name
  17047. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  17048. (if org-latex-fragment-image-overlays
  17049. (progn (org-remove-latex-fragment-image-overlays)
  17050. (message "LaTeX fragments images removed"))
  17051. (when (display-graphic-p)
  17052. (org-remove-latex-fragment-image-overlays)
  17053. (save-excursion
  17054. (save-restriction
  17055. (let (beg end at msg)
  17056. (cond
  17057. ((or (equal subtree '(16))
  17058. (not (save-excursion
  17059. (re-search-backward org-outline-regexp-bol nil t))))
  17060. (setq beg (point-min) end (point-max)
  17061. msg "Creating images for buffer...%s"))
  17062. ((equal subtree '(4))
  17063. (org-back-to-heading)
  17064. (setq beg (point) end (org-end-of-subtree t)
  17065. msg "Creating images for subtree...%s"))
  17066. (t
  17067. (if (setq at (org-inside-LaTeX-fragment-p))
  17068. (goto-char (max (point-min) (- (cdr at) 2)))
  17069. (org-back-to-heading))
  17070. (setq beg (point) end (progn (outline-next-heading) (point))
  17071. msg (if at "Creating image...%s"
  17072. "Creating images for entry...%s"))))
  17073. (message msg "")
  17074. (narrow-to-region beg end)
  17075. (goto-char beg)
  17076. (org-format-latex
  17077. (concat org-latex-preview-ltxpng-directory
  17078. (file-name-sans-extension
  17079. (file-name-nondirectory buffer-file-name)))
  17080. default-directory 'overlays msg at 'forbuffer
  17081. org-latex-create-formula-image-program)
  17082. (message msg "done. Use `C-c C-x C-l' to remove images.")))))))
  17083. (defun org-format-latex (prefix &optional dir overlays msg at
  17084. forbuffer processing-type)
  17085. "Replace LaTeX fragments with links to an image, and produce images.
  17086. Some of the options can be changed using the variable
  17087. `org-format-latex-options'."
  17088. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17089. (let* ((prefixnodir (file-name-nondirectory prefix))
  17090. (absprefix (expand-file-name prefix dir))
  17091. (todir (file-name-directory absprefix))
  17092. (opt org-format-latex-options)
  17093. (optnew org-format-latex-options)
  17094. (matchers (plist-get opt :matchers))
  17095. (re-list org-latex-regexps)
  17096. (cnt 0) txt hash link beg end re e checkdir
  17097. string
  17098. m n block-type block linkfile movefile ov)
  17099. ;; Check the different regular expressions
  17100. (while (setq e (pop re-list))
  17101. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  17102. block (if block-type "\n\n" ""))
  17103. (when (member m matchers)
  17104. (goto-char (point-min))
  17105. (while (re-search-forward re nil t)
  17106. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  17107. (or (not overlays)
  17108. (not (eq (get-char-property (match-beginning n)
  17109. 'org-overlay-type)
  17110. 'org-latex-overlay))))
  17111. (cond
  17112. ((eq processing-type 'verbatim))
  17113. ((eq processing-type 'mathjax)
  17114. ;; Prepare for MathJax processing.
  17115. (setq string (match-string n))
  17116. (when (member m '("$" "$1"))
  17117. (save-excursion
  17118. (delete-region (match-beginning n) (match-end n))
  17119. (goto-char (match-beginning n))
  17120. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  17121. ((or (eq processing-type 'dvipng)
  17122. (eq processing-type 'imagemagick))
  17123. ;; Process to an image.
  17124. (setq txt (match-string n)
  17125. beg (match-beginning n) end (match-end n)
  17126. cnt (1+ cnt))
  17127. (let ((face (face-at-point))
  17128. (fg (plist-get opt :foreground))
  17129. (bg (plist-get opt :background))
  17130. ;; Ensure full list is printed.
  17131. print-length print-level)
  17132. (when forbuffer
  17133. ;; Get the colors from the face at point.
  17134. (goto-char beg)
  17135. (when (eq fg 'auto)
  17136. (setq fg (face-attribute face :foreground nil 'default)))
  17137. (when (eq bg 'auto)
  17138. (setq bg (face-attribute face :background nil 'default)))
  17139. (setq optnew (copy-sequence opt))
  17140. (plist-put optnew :foreground fg)
  17141. (plist-put optnew :background bg))
  17142. (setq hash (sha1 (prin1-to-string
  17143. (list org-format-latex-header
  17144. org-latex-default-packages-alist
  17145. org-latex-packages-alist
  17146. org-format-latex-options
  17147. forbuffer txt fg bg)))
  17148. linkfile (format "%s_%s.png" prefix hash)
  17149. movefile (format "%s_%s.png" absprefix hash)))
  17150. (setq link (concat block "[[file:" linkfile "]]" block))
  17151. (if msg (message msg cnt))
  17152. (goto-char beg)
  17153. (unless checkdir ; Ensure the directory exists.
  17154. (setq checkdir t)
  17155. (or (file-directory-p todir) (make-directory todir t)))
  17156. (unless (file-exists-p movefile)
  17157. (org-create-formula-image
  17158. txt movefile optnew forbuffer processing-type))
  17159. (if overlays
  17160. (progn
  17161. (mapc (lambda (o)
  17162. (if (eq (overlay-get o 'org-overlay-type)
  17163. 'org-latex-overlay)
  17164. (delete-overlay o)))
  17165. (overlays-in beg end))
  17166. (setq ov (make-overlay beg end))
  17167. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  17168. (if (featurep 'xemacs)
  17169. (progn
  17170. (overlay-put ov 'invisible t)
  17171. (overlay-put
  17172. ov 'end-glyph
  17173. (make-glyph (vector 'png :file movefile))))
  17174. (overlay-put
  17175. ov 'display
  17176. (list 'image :type 'png :file movefile :ascent 'center)))
  17177. (push ov org-latex-fragment-image-overlays)
  17178. (goto-char end))
  17179. (delete-region beg end)
  17180. (insert (org-add-props link
  17181. (list 'org-latex-src
  17182. (replace-regexp-in-string
  17183. "\"" "" txt)
  17184. 'org-latex-src-embed-type
  17185. (if block-type 'paragraph 'character))))))
  17186. ((eq processing-type 'mathml)
  17187. ;; Process to MathML
  17188. (unless (save-match-data (org-format-latex-mathml-available-p))
  17189. (user-error "LaTeX to MathML converter not configured"))
  17190. (setq txt (match-string n)
  17191. beg (match-beginning n) end (match-end n)
  17192. cnt (1+ cnt))
  17193. (if msg (message msg cnt))
  17194. (goto-char beg)
  17195. (delete-region beg end)
  17196. (insert (org-format-latex-as-mathml
  17197. txt block-type prefix dir)))
  17198. (t
  17199. (error "Unknown conversion type %s for LaTeX fragments"
  17200. processing-type)))))))))
  17201. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17202. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17203. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17204. sucessful, return the portion between \"<math...> </math>\"
  17205. elements otherwise return nil. When MATHML-FILE is specified,
  17206. write the results in to that file. When invoked as an
  17207. interactive command, prompt for LATEX-FRAG, with initial value
  17208. set to the current active region and echo the results for user
  17209. inspection."
  17210. (interactive (list (let ((frag (when (org-region-active-p)
  17211. (buffer-substring-no-properties
  17212. (region-beginning) (region-end)))))
  17213. (read-string "LaTeX Fragment: " frag nil frag))))
  17214. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17215. (let* ((tmp-in-file (file-relative-name
  17216. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17217. (ignore (write-region latex-frag nil tmp-in-file))
  17218. (tmp-out-file (file-relative-name
  17219. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17220. (cmd (format-spec
  17221. org-latex-to-mathml-convert-command
  17222. `((?j . ,(shell-quote-argument
  17223. (expand-file-name org-latex-to-mathml-jar-file)))
  17224. (?I . ,(shell-quote-argument tmp-in-file))
  17225. (?o . ,(shell-quote-argument tmp-out-file)))))
  17226. mathml shell-command-output)
  17227. (when (org-called-interactively-p 'any)
  17228. (unless (org-format-latex-mathml-available-p)
  17229. (user-error "LaTeX to MathML converter not configured")))
  17230. (message "Running %s" cmd)
  17231. (setq shell-command-output (shell-command-to-string cmd))
  17232. (setq mathml
  17233. (when (file-readable-p tmp-out-file)
  17234. (with-current-buffer (find-file-noselect tmp-out-file t)
  17235. (goto-char (point-min))
  17236. (when (re-search-forward
  17237. (concat
  17238. (regexp-quote
  17239. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  17240. "\\(.\\|\n\\)*"
  17241. (regexp-quote "</math>")) nil t)
  17242. (prog1 (match-string 0) (kill-buffer))))))
  17243. (cond
  17244. (mathml
  17245. (setq mathml
  17246. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17247. (when mathml-file
  17248. (write-region mathml nil mathml-file))
  17249. (when (org-called-interactively-p 'any)
  17250. (message mathml)))
  17251. ((message "LaTeX to MathML conversion failed")
  17252. (message shell-command-output)))
  17253. (delete-file tmp-in-file)
  17254. (when (file-exists-p tmp-out-file)
  17255. (delete-file tmp-out-file))
  17256. mathml))
  17257. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17258. prefix &optional dir)
  17259. "Use `org-create-math-formula' but check local cache first."
  17260. (let* ((absprefix (expand-file-name prefix dir))
  17261. (print-length nil) (print-level nil)
  17262. (formula-id (concat
  17263. "formula-"
  17264. (sha1
  17265. (prin1-to-string
  17266. (list latex-frag
  17267. org-latex-to-mathml-convert-command)))))
  17268. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17269. (formula-cache-dir (file-name-directory formula-cache)))
  17270. (unless (file-directory-p formula-cache-dir)
  17271. (make-directory formula-cache-dir t))
  17272. (unless (file-exists-p formula-cache)
  17273. (org-create-math-formula latex-frag formula-cache))
  17274. (if (file-exists-p formula-cache)
  17275. ;; Successful conversion. Return the link to MathML file.
  17276. (org-add-props
  17277. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17278. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17279. 'org-latex-src-embed-type (if latex-frag-type
  17280. 'paragraph 'character)))
  17281. ;; Failed conversion. Return the LaTeX fragment verbatim
  17282. latex-frag)))
  17283. (defun org-create-formula-image (string tofile options buffer &optional type)
  17284. "Create an image from LaTeX source using dvipng or convert.
  17285. This function calls either `org-create-formula-image-with-dvipng'
  17286. or `org-create-formula-image-with-imagemagick' depending on the
  17287. value of `org-latex-create-formula-image-program' or on the value
  17288. of the optional TYPE variable.
  17289. Note: ultimately these two function should be combined as they
  17290. share a good deal of logic."
  17291. (org-check-external-command
  17292. "latex" "needed to convert LaTeX fragments to images")
  17293. (funcall
  17294. (case (or type org-latex-create-formula-image-program)
  17295. ('dvipng
  17296. (org-check-external-command
  17297. "dvipng" "needed to convert LaTeX fragments to images")
  17298. 'org-create-formula-image-with-dvipng)
  17299. ('imagemagick
  17300. (org-check-external-command
  17301. "convert" "you need to install imagemagick")
  17302. 'org-create-formula-image-with-imagemagick)
  17303. (t (error
  17304. "Invalid value of `org-latex-create-formula-image-program'")))
  17305. string tofile options buffer))
  17306. (declare-function org-export-get-backend "ox" (name))
  17307. (declare-function org-export--get-global-options "ox" (&optional backend))
  17308. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17309. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17310. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17311. (defun org-create-formula--latex-header ()
  17312. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17313. (let ((info (org-combine-plists (org-export--get-global-options
  17314. (org-export-get-backend 'latex))
  17315. (org-export--get-inbuffer-options
  17316. (org-export-get-backend 'latex)))))
  17317. (org-latex-guess-babel-language
  17318. (org-latex-guess-inputenc
  17319. (org-splice-latex-header
  17320. org-format-latex-header
  17321. org-latex-default-packages-alist
  17322. org-latex-packages-alist t
  17323. (plist-get info :latex-header)))
  17324. info)))
  17325. ;; This function borrows from Ganesh Swami's latex2png.el
  17326. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17327. "This calls dvipng."
  17328. (require 'ox-latex)
  17329. (let* ((tmpdir (if (featurep 'xemacs)
  17330. (temp-directory)
  17331. temporary-file-directory))
  17332. (texfilebase (make-temp-name
  17333. (expand-file-name "orgtex" tmpdir)))
  17334. (texfile (concat texfilebase ".tex"))
  17335. (dvifile (concat texfilebase ".dvi"))
  17336. (pngfile (concat texfilebase ".png"))
  17337. (fnh (if (featurep 'xemacs)
  17338. (font-height (face-font 'default))
  17339. (face-attribute 'default :height nil)))
  17340. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17341. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17342. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17343. "Black"))
  17344. (bg (or (plist-get options (if buffer :background :html-background))
  17345. "Transparent")))
  17346. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17347. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17348. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17349. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17350. (let ((latex-header (org-create-formula--latex-header)))
  17351. (with-temp-file texfile
  17352. (insert latex-header)
  17353. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17354. (let ((dir default-directory))
  17355. (ignore-errors
  17356. (cd tmpdir)
  17357. (call-process "latex" nil nil nil texfile))
  17358. (cd dir))
  17359. (if (not (file-exists-p dvifile))
  17360. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17361. (ignore-errors
  17362. (if (featurep 'xemacs)
  17363. (call-process "dvipng" nil nil nil
  17364. "-fg" fg "-bg" bg
  17365. "-T" "tight"
  17366. "-o" pngfile
  17367. dvifile)
  17368. (call-process "dvipng" nil nil nil
  17369. "-fg" fg "-bg" bg
  17370. "-D" dpi
  17371. ;;"-x" scale "-y" scale
  17372. "-T" "tight"
  17373. "-o" pngfile
  17374. dvifile)))
  17375. (if (not (file-exists-p pngfile))
  17376. (if org-format-latex-signal-error
  17377. (error "Failed to create png file from %s" texfile)
  17378. (message "Failed to create png file from %s" texfile)
  17379. nil)
  17380. ;; Use the requested file name and clean up
  17381. (copy-file pngfile tofile 'replace)
  17382. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17383. (if (file-exists-p (concat texfilebase e))
  17384. (delete-file (concat texfilebase e))))
  17385. pngfile))))
  17386. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17387. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17388. "This calls convert, which is included into imagemagick."
  17389. (require 'ox-latex)
  17390. (let* ((tmpdir (if (featurep 'xemacs)
  17391. (temp-directory)
  17392. temporary-file-directory))
  17393. (texfilebase (make-temp-name
  17394. (expand-file-name "orgtex" tmpdir)))
  17395. (texfile (concat texfilebase ".tex"))
  17396. (pdffile (concat texfilebase ".pdf"))
  17397. (pngfile (concat texfilebase ".png"))
  17398. (fnh (if (featurep 'xemacs)
  17399. (font-height (face-font 'default))
  17400. (face-attribute 'default :height nil)))
  17401. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17402. (dpi (number-to-string (* scale (floor (if buffer fnh 120.)))))
  17403. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17404. "black"))
  17405. (bg (or (plist-get options (if buffer :background :html-background))
  17406. "white")))
  17407. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17408. (setq fg (org-latex-color-format fg)))
  17409. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17410. (setq bg (org-latex-color-format
  17411. (if (string= bg "Transparent") "white" bg))))
  17412. (let ((latex-header (org-create-formula--latex-header)))
  17413. (with-temp-file texfile
  17414. (insert latex-header)
  17415. (insert "\n\\begin{document}\n"
  17416. "\\definecolor{fg}{rgb}{" fg "}\n"
  17417. "\\definecolor{bg}{rgb}{" bg "}\n"
  17418. "\n\\pagecolor{bg}\n"
  17419. "\n{\\color{fg}\n"
  17420. string
  17421. "\n}\n"
  17422. "\n\\end{document}\n")))
  17423. (org-latex-compile texfile t)
  17424. (if (not (file-exists-p pdffile))
  17425. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17426. (ignore-errors
  17427. (if (featurep 'xemacs)
  17428. (call-process "convert" nil nil nil
  17429. "-density" "96"
  17430. "-trim"
  17431. "-antialias"
  17432. pdffile
  17433. "-quality" "100"
  17434. ;; "-sharpen" "0x1.0"
  17435. pngfile)
  17436. (call-process "convert" nil nil nil
  17437. "-density" dpi
  17438. "-trim"
  17439. "-antialias"
  17440. pdffile
  17441. "-quality" "100"
  17442. ;; "-sharpen" "0x1.0"
  17443. pngfile)))
  17444. (if (not (file-exists-p pngfile))
  17445. (if org-format-latex-signal-error
  17446. (error "Failed to create png file from %s" texfile)
  17447. (message "Failed to create png file from %s" texfile)
  17448. nil)
  17449. ;; Use the requested file name and clean up
  17450. (copy-file pngfile tofile 'replace)
  17451. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17452. (if (file-exists-p (concat texfilebase e))
  17453. (delete-file (concat texfilebase e))))
  17454. pngfile))))
  17455. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17456. "Fill a LaTeX header template TPL.
  17457. In the template, the following place holders will be recognized:
  17458. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17459. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17460. [PACKAGES] \\usepackage statements for PKG
  17461. [NO-PACKAGES] do not include PKG
  17462. [EXTRA] the string EXTRA
  17463. [NO-EXTRA] do not include EXTRA
  17464. For backward compatibility, if both the positive and the negative place
  17465. holder is missing, the positive one (without the \"NO-\") will be
  17466. assumed to be present at the end of the template.
  17467. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17468. EXTRA is a string.
  17469. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17470. (let (rpl (end ""))
  17471. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17472. (setq rpl (if (or (match-end 1) (not def-pkg))
  17473. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17474. tpl (replace-match rpl t t tpl))
  17475. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17476. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17477. (setq rpl (if (or (match-end 1) (not pkg))
  17478. "" (org-latex-packages-to-string pkg snippets-p t))
  17479. tpl (replace-match rpl t t tpl))
  17480. (if pkg (setq end
  17481. (concat end "\n"
  17482. (org-latex-packages-to-string pkg snippets-p)))))
  17483. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17484. (setq rpl (if (or (match-end 1) (not extra))
  17485. "" (concat extra "\n"))
  17486. tpl (replace-match rpl t t tpl))
  17487. (if (and extra (string-match "\\S-" extra))
  17488. (setq end (concat end "\n" extra))))
  17489. (if (string-match "\\S-" end)
  17490. (concat tpl "\n" end)
  17491. tpl)))
  17492. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17493. "Turn an alist of packages into a string with the \\usepackage macros."
  17494. (setq pkg (mapconcat (lambda(p)
  17495. (cond
  17496. ((stringp p) p)
  17497. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17498. (format "%% Package %s omitted" (cadr p)))
  17499. ((equal "" (car p))
  17500. (format "\\usepackage{%s}" (cadr p)))
  17501. (t
  17502. (format "\\usepackage[%s]{%s}"
  17503. (car p) (cadr p)))))
  17504. pkg
  17505. "\n"))
  17506. (if newline (concat pkg "\n") pkg))
  17507. (defun org-dvipng-color (attr)
  17508. "Return a RGB color specification for dvipng."
  17509. (apply 'format "rgb %s %s %s"
  17510. (mapcar 'org-normalize-color
  17511. (if (featurep 'xemacs)
  17512. (color-rgb-components
  17513. (face-property 'default
  17514. (cond ((eq attr :foreground) 'foreground)
  17515. ((eq attr :background) 'background))))
  17516. (color-values (face-attribute 'default attr nil))))))
  17517. (defun org-dvipng-color-format (color-name)
  17518. "Convert COLOR-NAME to a RGB color value for dvipng."
  17519. (apply 'format "rgb %s %s %s"
  17520. (mapcar 'org-normalize-color
  17521. (color-values color-name))))
  17522. (defun org-latex-color (attr)
  17523. "Return a RGB color for the LaTeX color package."
  17524. (apply 'format "%s,%s,%s"
  17525. (mapcar 'org-normalize-color
  17526. (if (featurep 'xemacs)
  17527. (color-rgb-components
  17528. (face-property 'default
  17529. (cond ((eq attr :foreground) 'foreground)
  17530. ((eq attr :background) 'background))))
  17531. (color-values (face-attribute 'default attr nil))))))
  17532. (defun org-latex-color-format (color-name)
  17533. "Convert COLOR-NAME to a RGB color value."
  17534. (apply 'format "%s,%s,%s"
  17535. (mapcar 'org-normalize-color
  17536. (color-values color-name))))
  17537. (defun org-normalize-color (value)
  17538. "Return string to be used as color value for an RGB component."
  17539. (format "%g" (/ value 65535.0)))
  17540. ;; Image display
  17541. (defvar org-inline-image-overlays nil)
  17542. (make-variable-buffer-local 'org-inline-image-overlays)
  17543. (defun org-toggle-inline-images (&optional include-linked)
  17544. "Toggle the display of inline images.
  17545. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17546. (interactive "P")
  17547. (if org-inline-image-overlays
  17548. (progn
  17549. (org-remove-inline-images)
  17550. (message "Inline image display turned off"))
  17551. (org-display-inline-images include-linked)
  17552. (if (and (org-called-interactively-p)
  17553. org-inline-image-overlays)
  17554. (message "%d images displayed inline"
  17555. (length org-inline-image-overlays))
  17556. (message "No images to display inline"))))
  17557. (defun org-redisplay-inline-images ()
  17558. "Refresh the display of inline images."
  17559. (interactive)
  17560. (if (not org-inline-image-overlays)
  17561. (org-toggle-inline-images)
  17562. (org-toggle-inline-images)
  17563. (org-toggle-inline-images)))
  17564. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17565. "Display inline images.
  17566. An inline image is a link which follows either of these
  17567. conventions:
  17568. 1. Its path is a file with an extension matching return value
  17569. from `image-file-name-regexp' and it has no contents.
  17570. 2. Its description consists in a single link of the previous
  17571. type.
  17572. When optional argument INCLUDE-LINKED is non-nil, also links with
  17573. a text description part will be inlined. This can be nice for
  17574. a quick look at those images, but it does not reflect what
  17575. exported files will look like.
  17576. When optional argument REFRESH is non-nil, refresh existing
  17577. images between BEG and END. This will create new image displays
  17578. only if necessary. BEG and END default to the buffer
  17579. boundaries."
  17580. (interactive "P")
  17581. (when (display-graphic-p)
  17582. (unless refresh
  17583. (org-remove-inline-images)
  17584. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17585. (org-with-wide-buffer
  17586. (goto-char (or beg (point-min)))
  17587. (let ((case-fold-search t)
  17588. (file-extension-re (org-image-file-name-regexp)))
  17589. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17590. (let ((link (save-match-data (org-element-context))))
  17591. ;; Check if we're at an inline image.
  17592. (when (and (equal (org-element-property :type link) "file")
  17593. (or include-linked
  17594. (not (org-element-property :contents-begin link)))
  17595. (let ((parent (org-element-property :parent link)))
  17596. (or (not (eq (org-element-type parent) 'link))
  17597. (not (cdr (org-element-contents parent)))))
  17598. (org-string-match-p file-extension-re
  17599. (org-element-property :path link)))
  17600. (let ((file (expand-file-name (org-element-property :path link))))
  17601. (when (file-exists-p file)
  17602. (let ((width
  17603. ;; Apply `org-image-actual-width' specifications.
  17604. (cond
  17605. ((not (image-type-available-p 'imagemagick)) nil)
  17606. ((eq org-image-actual-width t) nil)
  17607. ((listp org-image-actual-width)
  17608. (or
  17609. ;; First try to find a width among
  17610. ;; attributes associated to the paragraph
  17611. ;; containing link.
  17612. (let ((paragraph
  17613. (let ((e link))
  17614. (while (and (setq e (org-element-property
  17615. :parent e))
  17616. (not (eq (org-element-type e)
  17617. 'paragraph))))
  17618. e)))
  17619. (when paragraph
  17620. (save-excursion
  17621. (goto-char (org-element-property :begin paragraph))
  17622. (when
  17623. (re-search-forward
  17624. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  17625. (org-element-property
  17626. :post-affiliated paragraph)
  17627. t)
  17628. (string-to-number (match-string 1))))))
  17629. ;; Otherwise, fall-back to provided number.
  17630. (car org-image-actual-width)))
  17631. ((numberp org-image-actual-width)
  17632. org-image-actual-width)))
  17633. (old (get-char-property-and-overlay
  17634. (org-element-property :begin link)
  17635. 'org-image-overlay)))
  17636. (if (and (car-safe old) refresh)
  17637. (image-refresh (overlay-get (cdr old) 'display))
  17638. (let ((image (create-image file
  17639. (and width 'imagemagick)
  17640. nil
  17641. :width width)))
  17642. (when image
  17643. (let* ((link
  17644. ;; If inline image is the description
  17645. ;; of another link, be sure to
  17646. ;; consider the latter as the one to
  17647. ;; apply the overlay on.
  17648. (let ((parent
  17649. (org-element-property :parent link)))
  17650. (if (eq (org-element-type parent) 'link)
  17651. parent
  17652. link)))
  17653. (ov (make-overlay
  17654. (org-element-property :begin link)
  17655. (progn
  17656. (goto-char
  17657. (org-element-property :end link))
  17658. (skip-chars-backward " \t")
  17659. (point)))))
  17660. (overlay-put ov 'display image)
  17661. (overlay-put ov 'face 'default)
  17662. (overlay-put ov 'org-image-overlay t)
  17663. (overlay-put
  17664. ov 'modification-hooks
  17665. (list 'org-display-inline-remove-overlay))
  17666. (push ov org-inline-image-overlays)))))))))))))))
  17667. (define-obsolete-function-alias
  17668. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17669. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17670. "Remove inline-display overlay if a corresponding region is modified."
  17671. (let ((inhibit-modification-hooks t))
  17672. (when (and ov after)
  17673. (delete ov org-inline-image-overlays)
  17674. (delete-overlay ov))))
  17675. (defun org-remove-inline-images ()
  17676. "Remove inline display of images."
  17677. (interactive)
  17678. (mapc 'delete-overlay org-inline-image-overlays)
  17679. (setq org-inline-image-overlays nil))
  17680. ;;;; Key bindings
  17681. ;; Outline functions from `outline-mode-prefix-map'
  17682. ;; that can be remapped in Org:
  17683. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17684. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17685. (define-key org-mode-map [remap outline-forward-same-level]
  17686. 'org-forward-heading-same-level)
  17687. (define-key org-mode-map [remap outline-backward-same-level]
  17688. 'org-backward-heading-same-level)
  17689. (define-key org-mode-map [remap show-branches]
  17690. 'org-kill-note-or-show-branches)
  17691. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17692. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17693. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17694. ;; Outline functions from `outline-mode-prefix-map' that can not
  17695. ;; be remapped in Org:
  17696. ;;
  17697. ;; - the column "key binding" shows whether the Outline function is still
  17698. ;; available in Org mode on the same key that it has been bound to in
  17699. ;; Outline mode:
  17700. ;; - "overridden": key used for a different functionality in Org mode
  17701. ;; - else: key still bound to the same Outline function in Org mode
  17702. ;;
  17703. ;; | Outline function | key binding | Org replacement |
  17704. ;; |------------------------------------+-------------+-----------------------|
  17705. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17706. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17707. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17708. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17709. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17710. ;; | `show-entry' | overridden | no replacement |
  17711. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17712. ;; | `show-branches' | `C-c C-k' | still same function |
  17713. ;; | `show-subtree' | overridden | visibility cycling |
  17714. ;; | `show-all' | overridden | no replacement |
  17715. ;; | `hide-subtree' | overridden | visibility cycling |
  17716. ;; | `hide-body' | overridden | no replacement |
  17717. ;; | `hide-entry' | overridden | visibility cycling |
  17718. ;; | `hide-leaves' | overridden | no replacement |
  17719. ;; | `hide-sublevels' | overridden | no replacement |
  17720. ;; | `hide-other' | overridden | no replacement |
  17721. ;; Make `C-c C-x' a prefix key
  17722. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17723. ;; TAB key with modifiers
  17724. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17725. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17726. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17727. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17728. ;; The following line is necessary under Suse GNU/Linux
  17729. (unless (featurep 'xemacs)
  17730. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17731. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17732. (define-key org-mode-map [backtab] 'org-shifttab)
  17733. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17734. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17735. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17736. ;; Cursor keys with modifiers
  17737. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17738. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17739. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17740. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17741. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  17742. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  17743. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17744. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17745. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17746. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17747. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17748. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17749. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17750. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17751. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17752. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17753. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17754. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17755. ;; Babel keys
  17756. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17757. (mapc (lambda (pair)
  17758. (define-key org-babel-map (car pair) (cdr pair)))
  17759. org-babel-key-bindings)
  17760. ;;; Extra keys for tty access.
  17761. ;; We only set them when really needed because otherwise the
  17762. ;; menus don't show the simple keys
  17763. (when (or org-use-extra-keys
  17764. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17765. (not window-system))
  17766. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17767. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17768. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17769. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17770. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17771. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17772. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17773. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17774. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17775. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17776. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17777. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17778. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17779. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17780. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17781. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17782. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17783. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17784. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17785. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17786. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17787. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17788. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17789. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17790. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17791. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17792. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17793. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17794. ;; All the other keys
  17795. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17796. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17797. (if (boundp 'narrow-map)
  17798. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17799. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17800. (if (boundp 'narrow-map)
  17801. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17802. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17803. (if (boundp 'narrow-map)
  17804. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17805. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17806. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17807. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17808. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17809. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17810. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17811. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17812. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17813. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17814. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17815. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17816. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17817. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17818. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17819. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17820. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17821. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17822. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17823. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17824. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17825. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17826. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17827. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17828. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17829. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17830. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17831. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17832. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17833. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17834. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17835. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17836. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17837. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17838. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17839. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17840. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17841. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17842. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  17843. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17844. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17845. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17846. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17847. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17848. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17849. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17850. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17851. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17852. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17853. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17854. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17855. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17856. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17857. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17858. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17859. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17860. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17861. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17862. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17863. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17864. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17865. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17866. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17867. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  17868. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  17869. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  17870. (org-defkey org-mode-map "\C-m" 'org-return)
  17871. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17872. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17873. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17874. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17875. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17876. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17877. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17878. (org-defkey org-mode-map "\C-cp" 'orgtbl-ascii-plot)
  17879. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17880. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17881. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17882. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17883. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17884. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17885. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17886. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  17887. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17888. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17889. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17890. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17891. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17892. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17893. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17894. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17895. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17896. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17897. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17898. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17899. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17900. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17901. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17902. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17903. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17904. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17905. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17906. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17907. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17908. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  17909. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17910. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17911. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17912. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17913. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17914. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17915. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17916. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17917. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17918. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17919. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17920. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17921. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17922. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17923. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17924. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17925. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17926. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17927. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17928. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17929. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17930. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17931. (when (featurep 'xemacs)
  17932. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17933. (defconst org-speed-commands-default
  17934. '(
  17935. ("Outline Navigation")
  17936. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17937. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17938. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17939. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17940. ("F" . org-next-block)
  17941. ("B" . org-previous-block)
  17942. ("u" . (org-speed-move-safe 'outline-up-heading))
  17943. ("j" . org-goto)
  17944. ("g" . (org-refile t))
  17945. ("Outline Visibility")
  17946. ("c" . org-cycle)
  17947. ("C" . org-shifttab)
  17948. (" " . org-display-outline-path)
  17949. ("s" . org-narrow-to-subtree)
  17950. ("=" . org-columns)
  17951. ("Outline Structure Editing")
  17952. ("U" . org-metaup)
  17953. ("D" . org-metadown)
  17954. ("r" . org-metaright)
  17955. ("l" . org-metaleft)
  17956. ("R" . org-shiftmetaright)
  17957. ("L" . org-shiftmetaleft)
  17958. ("i" . (progn (forward-char 1) (call-interactively
  17959. 'org-insert-heading-respect-content)))
  17960. ("^" . org-sort)
  17961. ("w" . org-refile)
  17962. ("a" . org-archive-subtree-default-with-confirmation)
  17963. ("@" . org-mark-subtree)
  17964. ("#" . org-toggle-comment)
  17965. ("Clock Commands")
  17966. ("I" . org-clock-in)
  17967. ("O" . org-clock-out)
  17968. ("Meta Data Editing")
  17969. ("t" . org-todo)
  17970. ("," . (org-priority))
  17971. ("0" . (org-priority ?\ ))
  17972. ("1" . (org-priority ?A))
  17973. ("2" . (org-priority ?B))
  17974. ("3" . (org-priority ?C))
  17975. (":" . org-set-tags-command)
  17976. ("e" . org-set-effort)
  17977. ("E" . org-inc-effort)
  17978. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17979. (org-entry-put (point) "APPT_WARNTIME" m)))
  17980. ("Agenda Views etc")
  17981. ("v" . org-agenda)
  17982. ("/" . org-sparse-tree)
  17983. ("Misc")
  17984. ("o" . org-open-at-point)
  17985. ("?" . org-speed-command-help)
  17986. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17987. (">" . (org-agenda-remove-restriction-lock))
  17988. )
  17989. "The default speed commands.")
  17990. (defun org-print-speed-command (e)
  17991. (if (> (length (car e)) 1)
  17992. (progn
  17993. (princ "\n")
  17994. (princ (car e))
  17995. (princ "\n")
  17996. (princ (make-string (length (car e)) ?-))
  17997. (princ "\n"))
  17998. (princ (car e))
  17999. (princ " ")
  18000. (if (symbolp (cdr e))
  18001. (princ (symbol-name (cdr e)))
  18002. (prin1 (cdr e)))
  18003. (princ "\n")))
  18004. (defun org-speed-command-help ()
  18005. "Show the available speed commands."
  18006. (interactive)
  18007. (if (not org-use-speed-commands)
  18008. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18009. (with-output-to-temp-buffer "*Help*"
  18010. (princ "User-defined Speed commands\n===========================\n")
  18011. (mapc 'org-print-speed-command org-speed-commands-user)
  18012. (princ "\n")
  18013. (princ "Built-in Speed commands\n=======================\n")
  18014. (mapc 'org-print-speed-command org-speed-commands-default))
  18015. (with-current-buffer "*Help*"
  18016. (setq truncate-lines t))))
  18017. (defun org-speed-move-safe (cmd)
  18018. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18019. If not, return to the original position and throw an error."
  18020. (interactive)
  18021. (let ((pos (point)))
  18022. (call-interactively cmd)
  18023. (unless (and (bolp) (org-at-heading-p))
  18024. (goto-char pos)
  18025. (error "Boundary reached while executing %s" cmd))))
  18026. (defvar org-self-insert-command-undo-counter 0)
  18027. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18028. (defvar org-speed-command nil)
  18029. (define-obsolete-function-alias
  18030. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18031. (defun org-speed-command-activate (keys)
  18032. "Hook for activating single-letter speed commands.
  18033. `org-speed-commands-default' specifies a minimal command set.
  18034. Use `org-speed-commands-user' for further customization."
  18035. (when (or (and (bolp) (looking-at org-outline-regexp))
  18036. (and (functionp org-use-speed-commands)
  18037. (funcall org-use-speed-commands)))
  18038. (cdr (assoc keys (append org-speed-commands-user
  18039. org-speed-commands-default)))))
  18040. (define-obsolete-function-alias
  18041. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18042. (defun org-babel-speed-command-activate (keys)
  18043. "Hook for activating single-letter code block commands."
  18044. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18045. (cdr (assoc keys org-babel-key-bindings))))
  18046. (defcustom org-speed-command-hook
  18047. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18048. "Hook for activating speed commands at strategic locations.
  18049. Hook functions are called in sequence until a valid handler is
  18050. found.
  18051. Each hook takes a single argument, a user-pressed command key
  18052. which is also a `self-insert-command' from the global map.
  18053. Within the hook, examine the cursor position and the command key
  18054. and return nil or a valid handler as appropriate. Handler could
  18055. be one of an interactive command, a function, or a form.
  18056. Set `org-use-speed-commands' to non-nil value to enable this
  18057. hook. The default setting is `org-speed-command-activate'."
  18058. :group 'org-structure
  18059. :version "24.1"
  18060. :type 'hook)
  18061. (defun org-self-insert-command (N)
  18062. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18063. If the cursor is in a table looking at whitespace, the whitespace is
  18064. overwritten, and the table is not marked as requiring realignment."
  18065. (interactive "p")
  18066. (org-check-before-invisible-edit 'insert)
  18067. (cond
  18068. ((and org-use-speed-commands
  18069. (let ((kv (this-command-keys-vector)))
  18070. (setq org-speed-command
  18071. (run-hook-with-args-until-success
  18072. 'org-speed-command-hook
  18073. (make-string 1 (aref kv (1- (length kv))))))))
  18074. (cond
  18075. ((commandp org-speed-command)
  18076. (setq this-command org-speed-command)
  18077. (call-interactively org-speed-command))
  18078. ((functionp org-speed-command)
  18079. (funcall org-speed-command))
  18080. ((and org-speed-command (listp org-speed-command))
  18081. (eval org-speed-command))
  18082. (t (let (org-use-speed-commands)
  18083. (call-interactively 'org-self-insert-command)))))
  18084. ((and
  18085. (org-table-p)
  18086. (progn
  18087. ;; check if we blank the field, and if that triggers align
  18088. (and (featurep 'org-table) org-table-auto-blank-field
  18089. (member last-command
  18090. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas-expand))
  18091. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  18092. ;; got extra space, this field does not determine column width
  18093. (let (org-table-may-need-update) (org-table-blank-field))
  18094. ;; no extra space, this field may determine column width
  18095. (org-table-blank-field)))
  18096. t)
  18097. (eq N 1)
  18098. (looking-at "[^|\n]* |"))
  18099. (let (org-table-may-need-update)
  18100. (goto-char (1- (match-end 0)))
  18101. (backward-delete-char 1)
  18102. (goto-char (match-beginning 0))
  18103. (self-insert-command N)))
  18104. (t
  18105. (setq org-table-may-need-update t)
  18106. (self-insert-command N)
  18107. (org-fix-tags-on-the-fly)
  18108. (if org-self-insert-cluster-for-undo
  18109. (if (not (eq last-command 'org-self-insert-command))
  18110. (setq org-self-insert-command-undo-counter 1)
  18111. (if (>= org-self-insert-command-undo-counter 20)
  18112. (setq org-self-insert-command-undo-counter 1)
  18113. (and (> org-self-insert-command-undo-counter 0)
  18114. buffer-undo-list (listp buffer-undo-list)
  18115. (not (cadr buffer-undo-list)) ; remove nil entry
  18116. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18117. (setq org-self-insert-command-undo-counter
  18118. (1+ org-self-insert-command-undo-counter))))))))
  18119. (defun org-check-before-invisible-edit (kind)
  18120. "Check is editing if kind KIND would be dangerous with invisible text around.
  18121. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18122. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18123. (if (and org-catch-invisible-edits
  18124. (or (not (boundp 'visible-mode)) (not visible-mode))
  18125. (or (get-char-property (point) 'invisible)
  18126. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18127. ;; OK, we need to take a closer look
  18128. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18129. (invisible-before-point (if (bobp) nil (get-char-property
  18130. (1- (point)) 'invisible)))
  18131. (border-and-ok-direction
  18132. (or
  18133. ;; Check if we are acting predictably before invisible text
  18134. (and invisible-at-point (not invisible-before-point)
  18135. (memq kind '(insert delete-backward)))
  18136. ;; Check if we are acting predictably after invisible text
  18137. ;; This works not well, and I have turned it off. It seems
  18138. ;; better to always show and stop after invisible text.
  18139. ;; (and (not invisible-at-point) invisible-before-point
  18140. ;; (memq kind '(insert delete)))
  18141. )))
  18142. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18143. (memq invisible-before-point '(outline org-hide-block t)))
  18144. (if (eq org-catch-invisible-edits 'error)
  18145. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18146. (if (and org-custom-properties-overlays
  18147. (y-or-n-p "Display invisible properties in this buffer? "))
  18148. (org-toggle-custom-properties-visibility)
  18149. ;; Make the area visible
  18150. (save-excursion
  18151. (if invisible-before-point
  18152. (goto-char (previous-single-char-property-change
  18153. (point) 'invisible)))
  18154. (show-subtree))
  18155. (cond
  18156. ((eq org-catch-invisible-edits 'show)
  18157. ;; That's it, we do the edit after showing
  18158. (message
  18159. "Unfolding invisible region around point before editing")
  18160. (sit-for 1))
  18161. ((and (eq org-catch-invisible-edits 'smart)
  18162. border-and-ok-direction)
  18163. (message "Unfolding invisible region around point before editing"))
  18164. (t
  18165. ;; Don't do the edit, make the user repeat it in full visibility
  18166. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18167. (defun org-fix-tags-on-the-fly ()
  18168. (when (and (equal (char-after (point-at-bol)) ?*)
  18169. (org-at-heading-p))
  18170. (org-align-tags-here org-tags-column)))
  18171. (defun org-delete-backward-char (N)
  18172. "Like `delete-backward-char', insert whitespace at field end in tables.
  18173. When deleting backwards, in tables this function will insert whitespace in
  18174. front of the next \"|\" separator, to keep the table aligned. The table will
  18175. still be marked for re-alignment if the field did fill the entire column,
  18176. because, in this case the deletion might narrow the column."
  18177. (interactive "p")
  18178. (save-match-data
  18179. (org-check-before-invisible-edit 'delete-backward)
  18180. (if (and (org-table-p)
  18181. (eq N 1)
  18182. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18183. (looking-at ".*?|"))
  18184. (let ((pos (point))
  18185. (noalign (looking-at "[^|\n\r]* |"))
  18186. (c org-table-may-need-update))
  18187. (backward-delete-char N)
  18188. (if (not overwrite-mode)
  18189. (progn
  18190. (skip-chars-forward "^|")
  18191. (insert " ")
  18192. (goto-char (1- pos))))
  18193. ;; noalign: if there were two spaces at the end, this field
  18194. ;; does not determine the width of the column.
  18195. (if noalign (setq org-table-may-need-update c)))
  18196. (backward-delete-char N)
  18197. (org-fix-tags-on-the-fly))))
  18198. (defun org-delete-char (N)
  18199. "Like `delete-char', but insert whitespace at field end in tables.
  18200. When deleting characters, in tables this function will insert whitespace in
  18201. front of the next \"|\" separator, to keep the table aligned. The table will
  18202. still be marked for re-alignment if the field did fill the entire column,
  18203. because, in this case the deletion might narrow the column."
  18204. (interactive "p")
  18205. (save-match-data
  18206. (org-check-before-invisible-edit 'delete)
  18207. (if (and (org-table-p)
  18208. (not (bolp))
  18209. (not (= (char-after) ?|))
  18210. (eq N 1))
  18211. (if (looking-at ".*?|")
  18212. (let ((pos (point))
  18213. (noalign (looking-at "[^|\n\r]* |"))
  18214. (c org-table-may-need-update))
  18215. (replace-match
  18216. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18217. (goto-char pos)
  18218. ;; noalign: if there were two spaces at the end, this field
  18219. ;; does not determine the width of the column.
  18220. (if noalign (setq org-table-may-need-update c)))
  18221. (delete-char N))
  18222. (delete-char N)
  18223. (org-fix-tags-on-the-fly))))
  18224. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18225. (put 'org-self-insert-command 'delete-selection
  18226. (lambda ()
  18227. (not (run-hook-with-args-until-success
  18228. 'self-insert-uses-region-functions))))
  18229. (put 'orgtbl-self-insert-command 'delete-selection
  18230. (lambda ()
  18231. (not (run-hook-with-args-until-success
  18232. 'self-insert-uses-region-functions))))
  18233. (put 'org-delete-char 'delete-selection 'supersede)
  18234. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18235. (put 'org-yank 'delete-selection 'yank)
  18236. ;; Make `flyspell-mode' delay after some commands
  18237. (put 'org-self-insert-command 'flyspell-delayed t)
  18238. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18239. (put 'org-delete-char 'flyspell-delayed t)
  18240. (put 'org-delete-backward-char 'flyspell-delayed t)
  18241. ;; Make pabbrev-mode expand after org-mode commands
  18242. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18243. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18244. (defun org-remap (map &rest commands)
  18245. "In MAP, remap the functions given in COMMANDS.
  18246. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18247. (let (new old)
  18248. (while commands
  18249. (setq old (pop commands) new (pop commands))
  18250. (if (fboundp 'command-remapping)
  18251. (org-defkey map (vector 'remap old) new)
  18252. (substitute-key-definition old new map global-map)))))
  18253. (defun org-transpose-words ()
  18254. "Transpose words for Org.
  18255. This uses the `org-mode-transpose-word-syntax-table' syntax
  18256. table, which interprets characters in `org-emphasis-alist' as
  18257. word constituents."
  18258. (interactive)
  18259. (with-syntax-table org-mode-transpose-word-syntax-table
  18260. (call-interactively 'transpose-words)))
  18261. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18262. (when (eq org-enable-table-editor 'optimized)
  18263. ;; If the user wants maximum table support, we need to hijack
  18264. ;; some standard editing functions
  18265. (org-remap org-mode-map
  18266. 'self-insert-command 'org-self-insert-command
  18267. 'delete-char 'org-delete-char
  18268. 'delete-backward-char 'org-delete-backward-char)
  18269. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18270. (defvar org-ctrl-c-ctrl-c-hook nil
  18271. "Hook for functions attaching themselves to `C-c C-c'.
  18272. This can be used to add additional functionality to the C-c C-c
  18273. key which executes context-dependent commands. This hook is run
  18274. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18275. run after the last test.
  18276. Each function will be called with no arguments. The function
  18277. must check if the context is appropriate for it to act. If yes,
  18278. it should do its thing and then return a non-nil value. If the
  18279. context is wrong, just do nothing and return nil.")
  18280. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18281. "Hook for functions attaching themselves to `C-c C-c'.
  18282. This can be used to add additional functionality to the C-c C-c
  18283. key which executes context-dependent commands. This hook is run
  18284. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18285. before the first test.
  18286. Each function will be called with no arguments. The function
  18287. must check if the context is appropriate for it to act. If yes,
  18288. it should do its thing and then return a non-nil value. If the
  18289. context is wrong, just do nothing and return nil.")
  18290. (defvar org-tab-first-hook nil
  18291. "Hook for functions to attach themselves to TAB.
  18292. See `org-ctrl-c-ctrl-c-hook' for more information.
  18293. This hook runs as the first action when TAB is pressed, even before
  18294. `org-cycle' messes around with the `outline-regexp' to cater for
  18295. inline tasks and plain list item folding.
  18296. If any function in this hook returns t, any other actions that
  18297. would have been caused by TAB (such as table field motion or visibility
  18298. cycling) will not occur.")
  18299. (defvar org-tab-after-check-for-table-hook nil
  18300. "Hook for functions to attach themselves to TAB.
  18301. See `org-ctrl-c-ctrl-c-hook' for more information.
  18302. This hook runs after it has been established that the cursor is not in a
  18303. table, but before checking if the cursor is in a headline or if global cycling
  18304. should be done.
  18305. If any function in this hook returns t, not other actions like visibility
  18306. cycling will be done.")
  18307. (defvar org-tab-after-check-for-cycling-hook nil
  18308. "Hook for functions to attach themselves to TAB.
  18309. See `org-ctrl-c-ctrl-c-hook' for more information.
  18310. This hook runs after it has been established that not table field motion and
  18311. not visibility should be done because of current context. This is probably
  18312. the place where a package like yasnippets can hook in.")
  18313. (defvar org-tab-before-tab-emulation-hook nil
  18314. "Hook for functions to attach themselves to TAB.
  18315. See `org-ctrl-c-ctrl-c-hook' for more information.
  18316. This hook runs after every other options for TAB have been exhausted, but
  18317. before indentation and \t insertion takes place.")
  18318. (defvar org-metaleft-hook nil
  18319. "Hook for functions attaching themselves to `M-left'.
  18320. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18321. (defvar org-metaright-hook nil
  18322. "Hook for functions attaching themselves to `M-right'.
  18323. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18324. (defvar org-metaup-hook nil
  18325. "Hook for functions attaching themselves to `M-up'.
  18326. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18327. (defvar org-metadown-hook nil
  18328. "Hook for functions attaching themselves to `M-down'.
  18329. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18330. (defvar org-shiftmetaleft-hook nil
  18331. "Hook for functions attaching themselves to `M-S-left'.
  18332. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18333. (defvar org-shiftmetaright-hook nil
  18334. "Hook for functions attaching themselves to `M-S-right'.
  18335. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18336. (defvar org-shiftmetaup-hook nil
  18337. "Hook for functions attaching themselves to `M-S-up'.
  18338. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18339. (defvar org-shiftmetadown-hook nil
  18340. "Hook for functions attaching themselves to `M-S-down'.
  18341. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18342. (defvar org-metareturn-hook nil
  18343. "Hook for functions attaching themselves to `M-RET'.
  18344. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18345. (defvar org-shiftup-hook nil
  18346. "Hook for functions attaching themselves to `S-up'.
  18347. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18348. (defvar org-shiftup-final-hook nil
  18349. "Hook for functions attaching themselves to `S-up'.
  18350. This one runs after all other options except shift-select have been excluded.
  18351. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18352. (defvar org-shiftdown-hook nil
  18353. "Hook for functions attaching themselves to `S-down'.
  18354. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18355. (defvar org-shiftdown-final-hook nil
  18356. "Hook for functions attaching themselves to `S-down'.
  18357. This one runs after all other options except shift-select have been excluded.
  18358. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18359. (defvar org-shiftleft-hook nil
  18360. "Hook for functions attaching themselves to `S-left'.
  18361. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18362. (defvar org-shiftleft-final-hook nil
  18363. "Hook for functions attaching themselves to `S-left'.
  18364. This one runs after all other options except shift-select have been excluded.
  18365. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18366. (defvar org-shiftright-hook nil
  18367. "Hook for functions attaching themselves to `S-right'.
  18368. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18369. (defvar org-shiftright-final-hook nil
  18370. "Hook for functions attaching themselves to `S-right'.
  18371. This one runs after all other options except shift-select have been excluded.
  18372. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18373. (defun org-modifier-cursor-error ()
  18374. "Throw an error, a modified cursor command was applied in wrong context."
  18375. (user-error "This command is active in special context like tables, headlines or items"))
  18376. (defun org-shiftselect-error ()
  18377. "Throw an error because Shift-Cursor command was applied in wrong context."
  18378. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18379. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18380. (user-error "This command works only in special context like headlines or timestamps")))
  18381. (defun org-call-for-shift-select (cmd)
  18382. (let ((this-command-keys-shift-translated t))
  18383. (call-interactively cmd)))
  18384. (defun org-shifttab (&optional arg)
  18385. "Global visibility cycling or move to previous table field.
  18386. Call `org-table-previous-field' within a table.
  18387. When ARG is nil, cycle globally through visibility states.
  18388. When ARG is a numeric prefix, show contents of this level."
  18389. (interactive "P")
  18390. (cond
  18391. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18392. ((integerp arg)
  18393. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18394. (message "Content view to level: %d" arg)
  18395. (org-content (prefix-numeric-value arg2))
  18396. (org-cycle-show-empty-lines t)
  18397. (setq org-cycle-global-status 'overview)))
  18398. (t (call-interactively 'org-global-cycle))))
  18399. (defun org-shiftmetaleft ()
  18400. "Promote subtree or delete table column.
  18401. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18402. `org-table-delete-column', depending on context. See the
  18403. individual commands for more information."
  18404. (interactive)
  18405. (cond
  18406. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18407. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18408. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18409. ((if (not (org-region-active-p)) (org-at-item-p)
  18410. (save-excursion (goto-char (region-beginning))
  18411. (org-at-item-p)))
  18412. (call-interactively 'org-outdent-item-tree))
  18413. (t (org-modifier-cursor-error))))
  18414. (defun org-shiftmetaright ()
  18415. "Demote subtree or insert table column.
  18416. Calls `org-demote-subtree', `org-indent-item-tree', or
  18417. `org-table-insert-column', depending on context. See the
  18418. individual commands for more information."
  18419. (interactive)
  18420. (cond
  18421. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18422. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18423. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18424. ((if (not (org-region-active-p)) (org-at-item-p)
  18425. (save-excursion (goto-char (region-beginning))
  18426. (org-at-item-p)))
  18427. (call-interactively 'org-indent-item-tree))
  18428. (t (org-modifier-cursor-error))))
  18429. (defun org-shiftmetaup (&optional arg)
  18430. "Drag the line at point up.
  18431. In a table, kill the current row.
  18432. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18433. but also adjust the previous clocked item in the clock history.
  18434. Everywhere else, drag the line at point up."
  18435. (interactive "P")
  18436. (cond
  18437. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18438. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18439. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18440. (call-interactively 'org-timestamp-up)))
  18441. (t (call-interactively 'org-drag-line-backward))))
  18442. (defun org-shiftmetadown (&optional arg)
  18443. "Drag the line at point down.
  18444. In a table, insert an empty row at the current line.
  18445. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18446. but also adjust the previous clocked item in the clock history.
  18447. Everywhere else, drag the line at point down."
  18448. (interactive "P")
  18449. (cond
  18450. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18451. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18452. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18453. (call-interactively 'org-timestamp-down)))
  18454. (t (call-interactively 'org-drag-line-forward))))
  18455. (defsubst org-hidden-tree-error ()
  18456. (user-error
  18457. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18458. (defun org-metaleft (&optional arg)
  18459. "Promote heading or move table column to left.
  18460. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18461. With no specific context, calls the Emacs default `backward-word'.
  18462. See the individual commands for more information."
  18463. (interactive "P")
  18464. (cond
  18465. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18466. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18467. ((org-with-limited-levels
  18468. (or (org-at-heading-p)
  18469. (and (org-region-active-p)
  18470. (save-excursion
  18471. (goto-char (region-beginning))
  18472. (org-at-heading-p)))))
  18473. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18474. (call-interactively 'org-do-promote))
  18475. ;; At an inline task.
  18476. ((org-at-heading-p)
  18477. (call-interactively 'org-inlinetask-promote))
  18478. ((or (org-at-item-p)
  18479. (and (org-region-active-p)
  18480. (save-excursion
  18481. (goto-char (region-beginning))
  18482. (org-at-item-p))))
  18483. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18484. (call-interactively 'org-outdent-item))
  18485. (t (call-interactively 'backward-word))))
  18486. (defun org-metaright (&optional arg)
  18487. "Demote a subtree, a list item or move table column to right.
  18488. In front of a drawer or a block keyword, indent it correctly.
  18489. With no specific context, calls the Emacs default `forward-word'.
  18490. See the individual commands for more information."
  18491. (interactive "P")
  18492. (cond
  18493. ((run-hook-with-args-until-success 'org-metaright-hook))
  18494. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18495. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18496. ((org-at-block-p) (call-interactively 'org-indent-block))
  18497. ((org-with-limited-levels
  18498. (or (org-at-heading-p)
  18499. (and (org-region-active-p)
  18500. (save-excursion
  18501. (goto-char (region-beginning))
  18502. (org-at-heading-p)))))
  18503. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18504. (call-interactively 'org-do-demote))
  18505. ;; At an inline task.
  18506. ((org-at-heading-p)
  18507. (call-interactively 'org-inlinetask-demote))
  18508. ((or (org-at-item-p)
  18509. (and (org-region-active-p)
  18510. (save-excursion
  18511. (goto-char (region-beginning))
  18512. (org-at-item-p))))
  18513. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18514. (call-interactively 'org-indent-item))
  18515. (t (call-interactively 'forward-word))))
  18516. (defun org-check-for-hidden (what)
  18517. "Check if there are hidden headlines/items in the current visual line.
  18518. WHAT can be either `headlines' or `items'. If the current line is
  18519. an outline or item heading and it has a folded subtree below it,
  18520. this function returns t, nil otherwise."
  18521. (let ((re (cond
  18522. ((eq what 'headlines) org-outline-regexp-bol)
  18523. ((eq what 'items) (org-item-beginning-re))
  18524. (t (error "This should not happen"))))
  18525. beg end)
  18526. (save-excursion
  18527. (catch 'exit
  18528. (unless (org-region-active-p)
  18529. (setq beg (point-at-bol))
  18530. (beginning-of-line 2)
  18531. (while (and (not (eobp)) ;; this is like `next-line'
  18532. (get-char-property (1- (point)) 'invisible))
  18533. (beginning-of-line 2))
  18534. (setq end (point))
  18535. (goto-char beg)
  18536. (goto-char (point-at-eol))
  18537. (setq end (max end (point)))
  18538. (while (re-search-forward re end t)
  18539. (if (get-char-property (match-beginning 0) 'invisible)
  18540. (throw 'exit t))))
  18541. nil))))
  18542. (defun org-metaup (&optional arg)
  18543. "Move subtree up or move table row up.
  18544. Calls `org-move-subtree-up' or `org-table-move-row' or
  18545. `org-move-item-up', depending on context. See the individual commands
  18546. for more information."
  18547. (interactive "P")
  18548. (cond
  18549. ((run-hook-with-args-until-success 'org-metaup-hook))
  18550. ((org-region-active-p)
  18551. (let* ((a (min (region-beginning) (region-end)))
  18552. (b (1- (max (region-beginning) (region-end))))
  18553. (c (save-excursion (goto-char a)
  18554. (move-beginning-of-line 0)))
  18555. (d (save-excursion (goto-char a)
  18556. (move-end-of-line 0) (point))))
  18557. (transpose-regions a b c d)
  18558. (goto-char c)))
  18559. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18560. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18561. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18562. (t (org-drag-element-backward))))
  18563. (defun org-metadown (&optional arg)
  18564. "Move subtree down or move table row down.
  18565. Calls `org-move-subtree-down' or `org-table-move-row' or
  18566. `org-move-item-down', depending on context. See the individual
  18567. commands for more information."
  18568. (interactive "P")
  18569. (cond
  18570. ((run-hook-with-args-until-success 'org-metadown-hook))
  18571. ((org-region-active-p)
  18572. (let* ((a (min (region-beginning) (region-end)))
  18573. (b (max (region-beginning) (region-end)))
  18574. (c (save-excursion (goto-char b)
  18575. (move-beginning-of-line 1)))
  18576. (d (save-excursion (goto-char b)
  18577. (move-end-of-line 1) (1+ (point)))))
  18578. (transpose-regions a b c d)
  18579. (goto-char d)))
  18580. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18581. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18582. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18583. (t (org-drag-element-forward))))
  18584. (defun org-shiftup (&optional arg)
  18585. "Increase item in timestamp or increase priority of current headline.
  18586. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18587. depending on context. See the individual commands for more information."
  18588. (interactive "P")
  18589. (cond
  18590. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18591. ((and org-support-shift-select (org-region-active-p))
  18592. (org-call-for-shift-select 'previous-line))
  18593. ((org-at-timestamp-p t)
  18594. (call-interactively (if org-edit-timestamp-down-means-later
  18595. 'org-timestamp-down 'org-timestamp-up)))
  18596. ((and (not (eq org-support-shift-select 'always))
  18597. org-enable-priority-commands
  18598. (org-at-heading-p))
  18599. (call-interactively 'org-priority-up))
  18600. ((and (not org-support-shift-select) (org-at-item-p))
  18601. (call-interactively 'org-previous-item))
  18602. ((org-clocktable-try-shift 'up arg))
  18603. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18604. (org-support-shift-select
  18605. (org-call-for-shift-select 'previous-line))
  18606. (t (org-shiftselect-error))))
  18607. (defun org-shiftdown (&optional arg)
  18608. "Decrease item in timestamp or decrease priority of current headline.
  18609. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18610. depending on context. See the individual commands for more information."
  18611. (interactive "P")
  18612. (cond
  18613. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18614. ((and org-support-shift-select (org-region-active-p))
  18615. (org-call-for-shift-select 'next-line))
  18616. ((org-at-timestamp-p t)
  18617. (call-interactively (if org-edit-timestamp-down-means-later
  18618. 'org-timestamp-up 'org-timestamp-down)))
  18619. ((and (not (eq org-support-shift-select 'always))
  18620. org-enable-priority-commands
  18621. (org-at-heading-p))
  18622. (call-interactively 'org-priority-down))
  18623. ((and (not org-support-shift-select) (org-at-item-p))
  18624. (call-interactively 'org-next-item))
  18625. ((org-clocktable-try-shift 'down arg))
  18626. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18627. (org-support-shift-select
  18628. (org-call-for-shift-select 'next-line))
  18629. (t (org-shiftselect-error))))
  18630. (defun org-shiftright (&optional arg)
  18631. "Cycle the thing at point or in the current line, depending on context.
  18632. Depending on context, this does one of the following:
  18633. - switch a timestamp at point one day into the future
  18634. - on a headline, switch to the next TODO keyword.
  18635. - on an item, switch entire list to the next bullet type
  18636. - on a property line, switch to the next allowed value
  18637. - on a clocktable definition line, move time block into the future"
  18638. (interactive "P")
  18639. (cond
  18640. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18641. ((and org-support-shift-select (org-region-active-p))
  18642. (org-call-for-shift-select 'forward-char))
  18643. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18644. ((and (not (eq org-support-shift-select 'always))
  18645. (org-at-heading-p))
  18646. (let ((org-inhibit-logging
  18647. (not org-treat-S-cursor-todo-selection-as-state-change))
  18648. (org-inhibit-blocking
  18649. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18650. (org-call-with-arg 'org-todo 'right)))
  18651. ((or (and org-support-shift-select
  18652. (not (eq org-support-shift-select 'always))
  18653. (org-at-item-bullet-p))
  18654. (and (not org-support-shift-select) (org-at-item-p)))
  18655. (org-call-with-arg 'org-cycle-list-bullet nil))
  18656. ((and (not (eq org-support-shift-select 'always))
  18657. (org-at-property-p))
  18658. (call-interactively 'org-property-next-allowed-value))
  18659. ((org-clocktable-try-shift 'right arg))
  18660. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18661. (org-support-shift-select
  18662. (org-call-for-shift-select 'forward-char))
  18663. (t (org-shiftselect-error))))
  18664. (defun org-shiftleft (&optional arg)
  18665. "Cycle the thing at point or in the current line, depending on context.
  18666. Depending on context, this does one of the following:
  18667. - switch a timestamp at point one day into the past
  18668. - on a headline, switch to the previous TODO keyword.
  18669. - on an item, switch entire list to the previous bullet type
  18670. - on a property line, switch to the previous allowed value
  18671. - on a clocktable definition line, move time block into the past"
  18672. (interactive "P")
  18673. (cond
  18674. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18675. ((and org-support-shift-select (org-region-active-p))
  18676. (org-call-for-shift-select 'backward-char))
  18677. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18678. ((and (not (eq org-support-shift-select 'always))
  18679. (org-at-heading-p))
  18680. (let ((org-inhibit-logging
  18681. (not org-treat-S-cursor-todo-selection-as-state-change))
  18682. (org-inhibit-blocking
  18683. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18684. (org-call-with-arg 'org-todo 'left)))
  18685. ((or (and org-support-shift-select
  18686. (not (eq org-support-shift-select 'always))
  18687. (org-at-item-bullet-p))
  18688. (and (not org-support-shift-select) (org-at-item-p)))
  18689. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18690. ((and (not (eq org-support-shift-select 'always))
  18691. (org-at-property-p))
  18692. (call-interactively 'org-property-previous-allowed-value))
  18693. ((org-clocktable-try-shift 'left arg))
  18694. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18695. (org-support-shift-select
  18696. (org-call-for-shift-select 'backward-char))
  18697. (t (org-shiftselect-error))))
  18698. (defun org-shiftcontrolright ()
  18699. "Switch to next TODO set."
  18700. (interactive)
  18701. (cond
  18702. ((and org-support-shift-select (org-region-active-p))
  18703. (org-call-for-shift-select 'forward-word))
  18704. ((and (not (eq org-support-shift-select 'always))
  18705. (org-at-heading-p))
  18706. (org-call-with-arg 'org-todo 'nextset))
  18707. (org-support-shift-select
  18708. (org-call-for-shift-select 'forward-word))
  18709. (t (org-shiftselect-error))))
  18710. (defun org-shiftcontrolleft ()
  18711. "Switch to previous TODO set."
  18712. (interactive)
  18713. (cond
  18714. ((and org-support-shift-select (org-region-active-p))
  18715. (org-call-for-shift-select 'backward-word))
  18716. ((and (not (eq org-support-shift-select 'always))
  18717. (org-at-heading-p))
  18718. (org-call-with-arg 'org-todo 'previousset))
  18719. (org-support-shift-select
  18720. (org-call-for-shift-select 'backward-word))
  18721. (t (org-shiftselect-error))))
  18722. (defun org-shiftcontrolup (&optional n)
  18723. "Change timestamps synchronously up in CLOCK log lines.
  18724. Optional argument N tells to change by that many units."
  18725. (interactive "P")
  18726. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18727. (let (org-support-shift-select)
  18728. (org-clock-timestamps-up n))
  18729. (user-error "Not at a clock log")))
  18730. (defun org-shiftcontroldown (&optional n)
  18731. "Change timestamps synchronously down in CLOCK log lines.
  18732. Optional argument N tells to change by that many units."
  18733. (interactive "P")
  18734. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18735. (let (org-support-shift-select)
  18736. (org-clock-timestamps-down n))
  18737. (user-error "Not at a clock log")))
  18738. (defun org-increase-number-at-point (&optional inc)
  18739. "Increment the number at point.
  18740. With an optional prefix numeric argument INC, increment using
  18741. this numeric value."
  18742. (interactive "p")
  18743. (if (not (number-at-point))
  18744. (user-error "Not on a number")
  18745. (unless inc (setq inc 1))
  18746. (let ((pos (point))
  18747. (beg (skip-chars-backward "-+^/*0-9eE."))
  18748. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  18749. (setq nap (buffer-substring-no-properties
  18750. (+ pos beg) (+ pos beg end)))
  18751. (delete-region (+ pos beg) (+ pos beg end))
  18752. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  18753. (when (org-at-table-p)
  18754. (org-table-align)
  18755. (org-table-end-of-field 1))))
  18756. (defun org-decrease-number-at-point (&optional inc)
  18757. "Decrement the number at point.
  18758. With an optional prefix numeric argument INC, decrement using
  18759. this numeric value."
  18760. (interactive "p")
  18761. (org-increase-number-at-point (- inc)))
  18762. (defun org-ctrl-c-ret ()
  18763. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18764. (interactive)
  18765. (cond
  18766. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18767. (t (call-interactively 'org-insert-heading))))
  18768. (defun org-find-visible ()
  18769. (let ((s (point)))
  18770. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18771. (get-char-property s 'invisible)))
  18772. s))
  18773. (defun org-find-invisible ()
  18774. (let ((s (point)))
  18775. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18776. (not (get-char-property s 'invisible))))
  18777. s))
  18778. (defun org-copy-visible (beg end)
  18779. "Copy the visible parts of the region."
  18780. (interactive "r")
  18781. (let (snippets s)
  18782. (save-excursion
  18783. (save-restriction
  18784. (narrow-to-region beg end)
  18785. (setq s (goto-char (point-min)))
  18786. (while (not (= (point) (point-max)))
  18787. (goto-char (org-find-invisible))
  18788. (push (buffer-substring s (point)) snippets)
  18789. (setq s (goto-char (org-find-visible))))))
  18790. (kill-new (apply 'concat (nreverse snippets)))))
  18791. (defun org-copy-special ()
  18792. "Copy region in table or copy current subtree.
  18793. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18794. See the individual commands for more information."
  18795. (interactive)
  18796. (call-interactively
  18797. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18798. (defun org-cut-special ()
  18799. "Cut region in table or cut current subtree.
  18800. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18801. See the individual commands for more information."
  18802. (interactive)
  18803. (call-interactively
  18804. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18805. (defun org-paste-special (arg)
  18806. "Paste rectangular region into table, or past subtree relative to level.
  18807. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18808. See the individual commands for more information."
  18809. (interactive "P")
  18810. (if (org-at-table-p)
  18811. (org-table-paste-rectangle)
  18812. (org-paste-subtree arg)))
  18813. (defsubst org-in-fixed-width-region-p ()
  18814. "Is point in a fixed-width region?"
  18815. (save-match-data
  18816. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18817. (defun org-edit-special (&optional arg)
  18818. "Call a special editor for the element at point.
  18819. When at a table, call the formula editor with `org-table-edit-formulas'.
  18820. When in a source code block, call `org-edit-src-code'.
  18821. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18822. When at an #+INCLUDE keyword, visit the included file.
  18823. On a link, call `ffap' to visit the link at point.
  18824. Otherwise, return a user error."
  18825. (interactive "P")
  18826. (let ((element (org-element-at-point)))
  18827. (assert (not buffer-read-only) nil
  18828. "Buffer is read-only: %s" (buffer-name))
  18829. (case (org-element-type element)
  18830. (src-block
  18831. (if (not arg) (org-edit-src-code)
  18832. (let* ((info (org-babel-get-src-block-info))
  18833. (lang (nth 0 info))
  18834. (params (nth 2 info))
  18835. (session (cdr (assq :session params))))
  18836. (if (not session) (org-edit-src-code)
  18837. ;; At a src-block with a session and function called with
  18838. ;; an ARG: switch to the buffer related to the inferior
  18839. ;; process.
  18840. (switch-to-buffer
  18841. (funcall (intern (concat "org-babel-prep-session:" lang))
  18842. session params))))))
  18843. (keyword
  18844. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18845. (find-file-other-window
  18846. (org-remove-double-quotes
  18847. (car (org-split-string (org-element-property :value element)))))
  18848. (user-error "No special environment to edit here")))
  18849. (table
  18850. (if (eq (org-element-property :type element) 'table.el)
  18851. (org-edit-src-code)
  18852. (call-interactively 'org-table-edit-formulas)))
  18853. ;; Only Org tables contain `table-row' type elements.
  18854. (table-row (call-interactively 'org-table-edit-formulas))
  18855. ((example-block export-block) (org-edit-src-code))
  18856. (fixed-width (org-edit-fixed-width-region))
  18857. (otherwise
  18858. ;; No notable element at point. Though, we may be at a link,
  18859. ;; which is an object. Thus, scan deeper.
  18860. (if (eq (org-element-type (org-element-context element)) 'link)
  18861. (call-interactively 'ffap)
  18862. (user-error "No special environment to edit here"))))))
  18863. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18864. (defun org-ctrl-c-ctrl-c (&optional arg)
  18865. "Set tags in headline, or update according to changed information at point.
  18866. This command does many different things, depending on context:
  18867. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18868. this is what we do.
  18869. - If the cursor is on a statistics cookie, update it.
  18870. - If the cursor is in a headline, prompt for tags and insert them
  18871. into the current line, aligned to `org-tags-column'. When called
  18872. with prefix arg, realign all tags in the current buffer.
  18873. - If the cursor is in one of the special #+KEYWORD lines, this
  18874. triggers scanning the buffer for these lines and updating the
  18875. information.
  18876. - If the cursor is inside a table, realign the table. This command
  18877. works even if the automatic table editor has been turned off.
  18878. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18879. the entire table.
  18880. - If the cursor is at a footnote reference or definition, jump to
  18881. the corresponding definition or references, respectively.
  18882. - If the cursor is a the beginning of a dynamic block, update it.
  18883. - If the current buffer is a capture buffer, close note and file it.
  18884. - If the cursor is on a <<<target>>>, update radio targets and
  18885. corresponding links in this buffer.
  18886. - If the cursor is on a numbered item in a plain list, renumber the
  18887. ordered list.
  18888. - If the cursor is on a checkbox, toggle it.
  18889. - If the cursor is on a code block, evaluate it. The variable
  18890. `org-confirm-babel-evaluate' can be used to control prompting
  18891. before code block evaluation, by default every code block
  18892. evaluation requires confirmation. Code block evaluation can be
  18893. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18894. (interactive "P")
  18895. (cond
  18896. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18897. org-occur-highlights)
  18898. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18899. (org-remove-occur-highlights)
  18900. (message "Temporary highlights/overlays removed from current buffer"))
  18901. ((and (local-variable-p 'org-finish-function (current-buffer))
  18902. (fboundp org-finish-function))
  18903. (funcall org-finish-function))
  18904. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18905. (t
  18906. (let* ((context (org-element-context)) (type (org-element-type context)))
  18907. ;; Test if point is within a blank line.
  18908. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18909. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18910. (user-error "C-c C-c can do nothing useful at this location"))
  18911. (case type
  18912. ;; When at a link, act according to the parent instead.
  18913. (link (setq context (org-element-property :parent context))
  18914. (setq type (org-element-type context)))
  18915. ;; Unsupported object types: refer to the first supported
  18916. ;; element or object containing it.
  18917. ((bold code entity export-snippet inline-babel-call inline-src-block
  18918. italic latex-fragment line-break macro strike-through subscript
  18919. superscript underline verbatim)
  18920. (while (and (setq context (org-element-property :parent context))
  18921. (not (memq (setq type (org-element-type context))
  18922. '(radio-target paragraph verse-block
  18923. table-cell)))))))
  18924. ;; For convenience: at the first line of a paragraph on the
  18925. ;; same line as an item, apply function on that item instead.
  18926. (when (eq type 'paragraph)
  18927. (let ((parent (org-element-property :parent context)))
  18928. (when (and (eq (org-element-type parent) 'item)
  18929. (= (point-at-bol) (org-element-property :begin parent)))
  18930. (setq context parent type 'item))))
  18931. ;; Act according to type of element or object at point.
  18932. (case type
  18933. (clock (org-clock-update-time-maybe))
  18934. (dynamic-block
  18935. (save-excursion
  18936. (goto-char (org-element-property :post-affiliated context))
  18937. (org-update-dblock)))
  18938. (footnote-definition
  18939. (goto-char (org-element-property :post-affiliated context))
  18940. (call-interactively 'org-footnote-action))
  18941. (footnote-reference (call-interactively 'org-footnote-action))
  18942. ((headline inlinetask)
  18943. (save-excursion (goto-char (org-element-property :begin context))
  18944. (call-interactively 'org-set-tags)))
  18945. (item
  18946. ;; At an item: a double C-u set checkbox to "[-]"
  18947. ;; unconditionally, whereas a single one will toggle its
  18948. ;; presence. Without an universal argument, if the item
  18949. ;; has a checkbox, toggle it. Otherwise repair the list.
  18950. (let* ((box (org-element-property :checkbox context))
  18951. (struct (org-element-property :structure context))
  18952. (old-struct (copy-tree struct))
  18953. (parents (org-list-parents-alist struct))
  18954. (prevs (org-list-prevs-alist struct))
  18955. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18956. (org-list-set-checkbox
  18957. (org-element-property :begin context) struct
  18958. (cond ((equal arg '(16)) "[-]")
  18959. ((and (not box) (equal arg '(4))) "[ ]")
  18960. ((or (not box) (equal arg '(4))) nil)
  18961. ((eq box 'on) "[ ]")
  18962. (t "[X]")))
  18963. ;; Mimic `org-list-write-struct' but with grabbing
  18964. ;; a return value from `org-list-struct-fix-box'.
  18965. (org-list-struct-fix-ind struct parents 2)
  18966. (org-list-struct-fix-item-end struct)
  18967. (org-list-struct-fix-bul struct prevs)
  18968. (org-list-struct-fix-ind struct parents)
  18969. (let ((block-item
  18970. (org-list-struct-fix-box struct parents prevs orderedp)))
  18971. (if (and box (equal struct old-struct))
  18972. (if (equal arg '(16))
  18973. (message "Checkboxes already reset")
  18974. (user-error "Cannot toggle this checkbox: %s"
  18975. (if (eq box 'on)
  18976. "all subitems checked"
  18977. "unchecked subitems")))
  18978. (org-list-struct-apply-struct struct old-struct)
  18979. (org-update-checkbox-count-maybe))
  18980. (when block-item
  18981. (message "Checkboxes were removed due to empty box at line %d"
  18982. (org-current-line block-item))))))
  18983. (keyword
  18984. (let ((org-inhibit-startup-visibility-stuff t)
  18985. (org-startup-align-all-tables nil))
  18986. (when (boundp 'org-table-coordinate-overlays)
  18987. (mapc 'delete-overlay org-table-coordinate-overlays)
  18988. (setq org-table-coordinate-overlays nil))
  18989. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18990. (message "Local setup has been refreshed"))
  18991. (plain-list
  18992. ;; At a plain list, with a double C-u argument, set
  18993. ;; checkboxes of each item to "[-]", whereas a single one
  18994. ;; will toggle their presence according to the state of the
  18995. ;; first item in the list. Without an argument, repair the
  18996. ;; list.
  18997. (let* ((begin (org-element-property :contents-begin context))
  18998. (beginm (move-marker (make-marker) begin))
  18999. (struct (org-element-property :structure context))
  19000. (old-struct (copy-tree struct))
  19001. (first-box (save-excursion
  19002. (goto-char begin)
  19003. (looking-at org-list-full-item-re)
  19004. (match-string-no-properties 3)))
  19005. (new-box (cond ((equal arg '(16)) "[-]")
  19006. ((equal arg '(4)) (unless first-box "[ ]"))
  19007. ((equal first-box "[X]") "[ ]")
  19008. (t "[X]"))))
  19009. (cond
  19010. (arg
  19011. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  19012. (org-list-get-all-items
  19013. begin struct (org-list-prevs-alist struct))))
  19014. ((and first-box (eq (point) begin))
  19015. ;; For convenience, when point is at bol on the first
  19016. ;; item of the list and no argument is provided, simply
  19017. ;; toggle checkbox of that item, if any.
  19018. (org-list-set-checkbox begin struct new-box)))
  19019. (org-list-write-struct
  19020. struct (org-list-parents-alist struct) old-struct)
  19021. (org-update-checkbox-count-maybe)
  19022. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19023. ((property-drawer node-property)
  19024. (call-interactively 'org-property-action))
  19025. ((radio-target target)
  19026. (call-interactively 'org-update-radio-target-regexp))
  19027. (statistics-cookie
  19028. (call-interactively 'org-update-statistics-cookies))
  19029. ((table table-cell table-row)
  19030. ;; At a table, recalculate every field and align it. Also
  19031. ;; send the table if necessary. If the table has
  19032. ;; a `table.el' type, just give up. At a table row or
  19033. ;; cell, maybe recalculate line but always align table.
  19034. (if (eq (org-element-property :type context) 'table.el)
  19035. (message "Use C-c ' to edit table.el tables")
  19036. (let ((org-enable-table-editor t))
  19037. (if (or (eq type 'table)
  19038. ;; Check if point is at a TBLFM line.
  19039. (and (eq type 'table-row)
  19040. (= (point) (org-element-property :end context))))
  19041. (save-excursion
  19042. (if (org-at-TBLFM-p)
  19043. (progn (require 'org-table)
  19044. (org-table-calc-current-TBLFM))
  19045. (goto-char (org-element-property :contents-begin context))
  19046. (org-call-with-arg 'org-table-recalculate (or arg t))
  19047. (orgtbl-send-table 'maybe)))
  19048. (org-table-maybe-eval-formula)
  19049. (cond (arg (call-interactively 'org-table-recalculate))
  19050. ((org-table-maybe-recalculate-line))
  19051. (t (org-table-align)))))))
  19052. (timestamp (org-timestamp-change 0 'day))
  19053. (otherwise
  19054. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19055. (user-error
  19056. "C-c C-c can do nothing useful at this location")))))))))
  19057. (defun org-mode-restart ()
  19058. (interactive)
  19059. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19060. (funcall major-mode)
  19061. (hack-local-variables)
  19062. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19063. (org-indent-mode -1)))
  19064. (message "%s restarted" major-mode))
  19065. (defun org-kill-note-or-show-branches ()
  19066. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19067. (interactive)
  19068. (if (not org-finish-function)
  19069. (progn
  19070. (hide-subtree)
  19071. (call-interactively 'show-branches))
  19072. (let ((org-note-abort t))
  19073. (funcall org-finish-function))))
  19074. (defun org-open-line (n)
  19075. "Insert a new row in tables, call `open-line' elsewhere.
  19076. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19077. (interactive "*p")
  19078. (cond
  19079. ((not org-special-ctrl-o)
  19080. (open-line n))
  19081. ((org-at-table-p)
  19082. (org-table-insert-row))
  19083. (t
  19084. (open-line n))))
  19085. (defun org-return (&optional indent)
  19086. "Goto next table row or insert a newline.
  19087. Calls `org-table-next-row' or `newline', depending on context.
  19088. See the individual commands for more information."
  19089. (interactive)
  19090. (let (org-ts-what)
  19091. (cond
  19092. ((or (bobp) (org-in-src-block-p))
  19093. (if indent (newline-and-indent) (newline)))
  19094. ((org-at-table-p)
  19095. (org-table-justify-field-maybe)
  19096. (call-interactively 'org-table-next-row))
  19097. ;; when `newline-and-indent' is called within a list, make sure
  19098. ;; text moved stays inside the item.
  19099. ((and (org-in-item-p) indent)
  19100. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  19101. (progn
  19102. (save-match-data (newline))
  19103. (org-indent-line-to (length (match-string 0))))
  19104. (let ((ind (org-get-indentation)))
  19105. (newline)
  19106. (if (org-looking-back org-list-end-re)
  19107. (org-indent-line)
  19108. (org-indent-line-to ind)))))
  19109. ((and org-return-follows-link
  19110. (org-at-timestamp-p t)
  19111. (not (eq org-ts-what 'after)))
  19112. (org-follow-timestamp-link))
  19113. ((and org-return-follows-link
  19114. (let ((tprop (get-text-property (point) 'face)))
  19115. (or (eq tprop 'org-link)
  19116. (and (listp tprop) (memq 'org-link tprop)))))
  19117. (call-interactively 'org-open-at-point))
  19118. ((and (org-at-heading-p)
  19119. (looking-at
  19120. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  19121. (org-show-entry)
  19122. (end-of-line 1)
  19123. (newline))
  19124. (t (if indent (newline-and-indent) (newline))))))
  19125. (defun org-return-indent ()
  19126. "Goto next table row or insert a newline and indent.
  19127. Calls `org-table-next-row' or `newline-and-indent', depending on
  19128. context. See the individual commands for more information."
  19129. (interactive)
  19130. (org-return t))
  19131. (defun org-ctrl-c-star ()
  19132. "Compute table, or change heading status of lines.
  19133. Calls `org-table-recalculate' or `org-toggle-heading',
  19134. depending on context."
  19135. (interactive)
  19136. (cond
  19137. ((org-at-table-p)
  19138. (call-interactively 'org-table-recalculate))
  19139. (t
  19140. ;; Convert all lines in region to list items
  19141. (call-interactively 'org-toggle-heading))))
  19142. (defun org-ctrl-c-minus ()
  19143. "Insert separator line in table or modify bullet status of line.
  19144. Also turns a plain line or a region of lines into list items.
  19145. Calls `org-table-insert-hline', `org-toggle-item', or
  19146. `org-cycle-list-bullet', depending on context."
  19147. (interactive)
  19148. (cond
  19149. ((org-at-table-p)
  19150. (call-interactively 'org-table-insert-hline))
  19151. ((org-region-active-p)
  19152. (call-interactively 'org-toggle-item))
  19153. ((org-in-item-p)
  19154. (call-interactively 'org-cycle-list-bullet))
  19155. (t
  19156. (call-interactively 'org-toggle-item))))
  19157. (defun org-toggle-item (arg)
  19158. "Convert headings or normal lines to items, items to normal lines.
  19159. If there is no active region, only the current line is considered.
  19160. If the first non blank line in the region is a headline, convert
  19161. all headlines to items, shifting text accordingly.
  19162. If it is an item, convert all items to normal lines.
  19163. If it is normal text, change region into a list of items.
  19164. With a prefix argument ARG, change the region in a single item."
  19165. (interactive "P")
  19166. (let ((shift-text
  19167. (function
  19168. ;; Shift text in current section to IND, from point to END.
  19169. ;; The function leaves point to END line.
  19170. (lambda (ind end)
  19171. (let ((min-i 1000) (end (copy-marker end)))
  19172. ;; First determine the minimum indentation (MIN-I) of
  19173. ;; the text.
  19174. (save-excursion
  19175. (catch 'exit
  19176. (while (< (point) end)
  19177. (let ((i (org-get-indentation)))
  19178. (cond
  19179. ;; Skip blank lines and inline tasks.
  19180. ((looking-at "^[ \t]*$"))
  19181. ((looking-at org-outline-regexp-bol))
  19182. ;; We can't find less than 0 indentation.
  19183. ((zerop i) (throw 'exit (setq min-i 0)))
  19184. ((< i min-i) (setq min-i i))))
  19185. (forward-line))))
  19186. ;; Then indent each line so that a line indented to
  19187. ;; MIN-I becomes indented to IND. Ignore blank lines
  19188. ;; and inline tasks in the process.
  19189. (let ((delta (- ind min-i)))
  19190. (while (< (point) end)
  19191. (unless (or (looking-at "^[ \t]*$")
  19192. (looking-at org-outline-regexp-bol))
  19193. (org-indent-line-to (+ (org-get-indentation) delta)))
  19194. (forward-line)))))))
  19195. (skip-blanks
  19196. (function
  19197. ;; Return beginning of first non-blank line, starting from
  19198. ;; line at POS.
  19199. (lambda (pos)
  19200. (save-excursion
  19201. (goto-char pos)
  19202. (skip-chars-forward " \r\t\n")
  19203. (point-at-bol)))))
  19204. beg end)
  19205. ;; Determine boundaries of changes.
  19206. (if (org-region-active-p)
  19207. (setq beg (funcall skip-blanks (region-beginning))
  19208. end (copy-marker (region-end)))
  19209. (setq beg (funcall skip-blanks (point-at-bol))
  19210. end (copy-marker (point-at-eol))))
  19211. ;; Depending on the starting line, choose an action on the text
  19212. ;; between BEG and END.
  19213. (org-with-limited-levels
  19214. (save-excursion
  19215. (goto-char beg)
  19216. (cond
  19217. ;; Case 1. Start at an item: de-itemize. Note that it only
  19218. ;; happens when a region is active: `org-ctrl-c-minus'
  19219. ;; would call `org-cycle-list-bullet' otherwise.
  19220. ((org-at-item-p)
  19221. (while (< (point) end)
  19222. (when (org-at-item-p)
  19223. (skip-chars-forward " \t")
  19224. (delete-region (point) (match-end 0)))
  19225. (forward-line)))
  19226. ;; Case 2. Start at an heading: convert to items.
  19227. ((org-at-heading-p)
  19228. (let* ((bul (org-list-bullet-string "-"))
  19229. (bul-len (length bul))
  19230. (done (org-entry-is-done-p))
  19231. (todo (org-entry-is-todo-p))
  19232. ;; Indentation of the first heading. It should be
  19233. ;; relative to the indentation of its parent, if any.
  19234. (start-ind (save-excursion
  19235. (cond
  19236. ((not org-adapt-indentation) 0)
  19237. ((not (outline-previous-heading)) 0)
  19238. (t (length (match-string 0))))))
  19239. ;; Level of first heading. Further headings will be
  19240. ;; compared to it to determine hierarchy in the list.
  19241. (ref-level (org-reduced-level (org-outline-level))))
  19242. (when (or done todo) (org-todo ""))
  19243. (while (< (point) end)
  19244. (let* ((level (org-reduced-level (org-outline-level)))
  19245. (delta (max 0 (- level ref-level))))
  19246. ;; If current headline is less indented than the first
  19247. ;; one, set it as reference, in order to preserve
  19248. ;; subtrees.
  19249. (when (< level ref-level) (setq ref-level level))
  19250. (replace-match bul t t)
  19251. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19252. (when (or done todo)
  19253. (let* ((struct (org-list-struct))
  19254. (old (copy-tree struct)))
  19255. (org-list-set-checkbox (line-beginning-position)
  19256. struct
  19257. (if done "[X]" "[ ]"))
  19258. (org-list-write-struct struct
  19259. (org-list-parents-alist struct)
  19260. old)))
  19261. ;; Ensure all text down to END (or SECTION-END) belongs
  19262. ;; to the newly created item.
  19263. (let ((section-end (save-excursion
  19264. (or (outline-next-heading) (point)))))
  19265. (forward-line)
  19266. (funcall shift-text
  19267. (+ start-ind (* (1+ delta) bul-len))
  19268. (min end section-end)))))))
  19269. ;; Case 3. Normal line with ARG: make the first line of region
  19270. ;; an item, and shift indentation of others lines to
  19271. ;; set them as item's body.
  19272. (arg (let* ((bul (org-list-bullet-string "-"))
  19273. (bul-len (length bul))
  19274. (ref-ind (org-get-indentation)))
  19275. (skip-chars-forward " \t")
  19276. (insert bul)
  19277. (forward-line)
  19278. (while (< (point) end)
  19279. ;; Ensure that lines less indented than first one
  19280. ;; still get included in item body.
  19281. (funcall shift-text
  19282. (+ ref-ind bul-len)
  19283. (min end (save-excursion (or (outline-next-heading)
  19284. (point)))))
  19285. (forward-line))))
  19286. ;; Case 4. Normal line without ARG: turn each non-item line
  19287. ;; into an item.
  19288. (t
  19289. (while (< (point) end)
  19290. (unless (or (org-at-heading-p) (org-at-item-p))
  19291. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19292. (replace-match
  19293. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19294. (forward-line))))))))
  19295. (defun org-toggle-heading (&optional nstars)
  19296. "Convert headings to normal text, or items or text to headings.
  19297. If there is no active region, only convert the current line.
  19298. With a \\[universal-argument] prefix, convert the whole list at
  19299. point into heading.
  19300. In a region:
  19301. - If the first non blank line is a headline, remove the stars
  19302. from all headlines in the region.
  19303. - If it is a normal line, turn each and every normal line (i.e.,
  19304. not an heading or an item) in the region into headings. If you
  19305. want to convert only the first line of this region, use one
  19306. universal prefix argument.
  19307. - If it is a plain list item, turn all plain list items into headings.
  19308. When converting a line into a heading, the number of stars is chosen
  19309. such that the lines become children of the current entry. However,
  19310. when a numeric prefix argument is given, its value determines the
  19311. number of stars to add."
  19312. (interactive "P")
  19313. (let ((skip-blanks
  19314. (function
  19315. ;; Return beginning of first non-blank line, starting from
  19316. ;; line at POS.
  19317. (lambda (pos)
  19318. (save-excursion
  19319. (goto-char pos)
  19320. (while (org-at-comment-p) (forward-line))
  19321. (skip-chars-forward " \r\t\n")
  19322. (point-at-bol)))))
  19323. beg end toggled)
  19324. ;; Determine boundaries of changes. If a universal prefix has
  19325. ;; been given, put the list in a region. If region ends at a bol,
  19326. ;; do not consider the last line to be in the region.
  19327. (when (and current-prefix-arg (org-at-item-p))
  19328. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19329. (org-mark-element))
  19330. (if (org-region-active-p)
  19331. (setq beg (funcall skip-blanks (region-beginning))
  19332. end (copy-marker (save-excursion
  19333. (goto-char (region-end))
  19334. (if (bolp) (point) (point-at-eol)))))
  19335. (setq beg (funcall skip-blanks (point-at-bol))
  19336. end (copy-marker (point-at-eol))))
  19337. ;; Ensure inline tasks don't count as headings.
  19338. (org-with-limited-levels
  19339. (save-excursion
  19340. (goto-char beg)
  19341. (cond
  19342. ;; Case 1. Started at an heading: de-star headings.
  19343. ((org-at-heading-p)
  19344. (while (< (point) end)
  19345. (when (org-at-heading-p t)
  19346. (looking-at org-outline-regexp) (replace-match "")
  19347. (setq toggled t))
  19348. (forward-line)))
  19349. ;; Case 2. Started at an item: change items into headlines.
  19350. ;; One star will be added by `org-list-to-subtree'.
  19351. ((org-at-item-p)
  19352. (let* ((stars (make-string
  19353. ;; subtract the star that will be added again by
  19354. ;; `org-list-to-subtree'
  19355. (if (numberp nstars) (1- nstars)
  19356. (or (org-current-level) 0))
  19357. ?*))
  19358. (add-stars
  19359. (cond (nstars "") ; stars from prefix only
  19360. ((equal stars "") "") ; before first heading
  19361. (org-odd-levels-only "*") ; inside heading, odd
  19362. (t "")))) ; inside heading, oddeven
  19363. (while (< (point) end)
  19364. (when (org-at-item-p)
  19365. ;; Pay attention to cases when region ends before list.
  19366. (let* ((struct (org-list-struct))
  19367. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19368. (save-restriction
  19369. (narrow-to-region (point) list-end)
  19370. (insert
  19371. (org-list-to-subtree
  19372. (org-list-parse-list t)
  19373. '(:istart (concat stars add-stars (funcall get-stars depth))
  19374. :icount (concat stars add-stars (funcall get-stars depth)))))))
  19375. (setq toggled t))
  19376. (forward-line))))
  19377. ;; Case 3. Started at normal text: make every line an heading,
  19378. ;; skipping headlines and items.
  19379. (t (let* ((stars
  19380. (make-string
  19381. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19382. (add-stars
  19383. (cond (nstars "") ; stars from prefix only
  19384. ((equal stars "") "*") ; before first heading
  19385. (org-odd-levels-only "**") ; inside heading, odd
  19386. (t "*"))) ; inside heading, oddeven
  19387. (rpl (concat stars add-stars " "))
  19388. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19389. (while (< (point) (if (equal nstars '(4)) lend end))
  19390. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19391. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19392. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19393. (forward-line)))))))
  19394. (unless toggled (message "Cannot toggle heading from here"))))
  19395. (defun org-meta-return (&optional arg)
  19396. "Insert a new heading or wrap a region in a table.
  19397. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19398. on context. See the individual commands for more information."
  19399. (interactive "P")
  19400. (org-check-before-invisible-edit 'insert)
  19401. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19402. (let* ((element (org-element-at-point))
  19403. (type (org-element-type element)))
  19404. (when (eq type 'table-row)
  19405. (setq element (org-element-property :parent element))
  19406. (setq type 'table))
  19407. (if (and (eq type 'table)
  19408. (eq (org-element-property :type element) 'org)
  19409. (>= (point) (org-element-property :contents-begin element))
  19410. (< (point) (org-element-property :contents-end element)))
  19411. (call-interactively 'org-table-wrap-region)
  19412. (call-interactively 'org-insert-heading)))))
  19413. ;;; Menu entries
  19414. (defsubst org-in-subtree-not-table-p ()
  19415. "Are we in a subtree and not in a table?"
  19416. (and (not (org-before-first-heading-p))
  19417. (not (org-at-table-p))))
  19418. ;; Define the Org-mode menus
  19419. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19420. '("Tbl"
  19421. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19422. ["Next Field" org-cycle (org-at-table-p)]
  19423. ["Previous Field" org-shifttab (org-at-table-p)]
  19424. ["Next Row" org-return (org-at-table-p)]
  19425. "--"
  19426. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19427. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19428. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19429. "--"
  19430. ("Column"
  19431. ["Move Column Left" org-metaleft (org-at-table-p)]
  19432. ["Move Column Right" org-metaright (org-at-table-p)]
  19433. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19434. ["Insert Column" org-shiftmetaright (org-at-table-p)]
  19435. ["Ascii plot" orgtbl-ascii-plot (org-at-table-p)])
  19436. ("Row"
  19437. ["Move Row Up" org-metaup (org-at-table-p)]
  19438. ["Move Row Down" org-metadown (org-at-table-p)]
  19439. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19440. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19441. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19442. "--"
  19443. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19444. ("Rectangle"
  19445. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19446. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19447. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19448. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19449. "--"
  19450. ("Calculate"
  19451. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19452. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19453. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19454. "--"
  19455. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19456. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19457. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19458. "--"
  19459. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19460. "--"
  19461. ["Sum Column/Rectangle" org-table-sum
  19462. (or (org-at-table-p) (org-region-active-p))]
  19463. ["Which Column?" org-table-current-column (org-at-table-p)])
  19464. ["Debug Formulas"
  19465. org-table-toggle-formula-debugger
  19466. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19467. ["Show Col/Row Numbers"
  19468. org-table-toggle-coordinate-overlays
  19469. :style toggle
  19470. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19471. "--"
  19472. ["Create" org-table-create (and (not (org-at-table-p))
  19473. org-enable-table-editor)]
  19474. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19475. ["Import from File" org-table-import (not (org-at-table-p))]
  19476. ["Export to File" org-table-export (org-at-table-p)]
  19477. "--"
  19478. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  19479. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19480. '("Org"
  19481. ("Show/Hide"
  19482. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19483. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19484. ["Sparse Tree..." org-sparse-tree t]
  19485. ["Reveal Context" org-reveal t]
  19486. ["Show All" show-all t]
  19487. "--"
  19488. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19489. "--"
  19490. ["New Heading" org-insert-heading t]
  19491. ("Navigate Headings"
  19492. ["Up" outline-up-heading t]
  19493. ["Next" outline-next-visible-heading t]
  19494. ["Previous" outline-previous-visible-heading t]
  19495. ["Next Same Level" outline-forward-same-level t]
  19496. ["Previous Same Level" outline-backward-same-level t]
  19497. "--"
  19498. ["Jump" org-goto t])
  19499. ("Edit Structure"
  19500. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19501. "--"
  19502. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19503. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19504. "--"
  19505. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19506. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19507. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19508. "--"
  19509. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19510. "--"
  19511. ["Copy visible text" org-copy-visible t]
  19512. "--"
  19513. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19514. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19515. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19516. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19517. "--"
  19518. ["Sort Region/Children" org-sort t]
  19519. "--"
  19520. ["Convert to odd levels" org-convert-to-odd-levels t]
  19521. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19522. ("Editing"
  19523. ["Emphasis..." org-emphasize t]
  19524. ["Edit Source Example" org-edit-special t]
  19525. "--"
  19526. ["Footnote new/jump" org-footnote-action t]
  19527. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19528. ("Archive"
  19529. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19530. "--"
  19531. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19532. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19533. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19534. )
  19535. "--"
  19536. ("Hyperlinks"
  19537. ["Store Link (Global)" org-store-link t]
  19538. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19539. ["Insert Link" org-insert-link t]
  19540. ["Follow Link" org-open-at-point t]
  19541. "--"
  19542. ["Next link" org-next-link t]
  19543. ["Previous link" org-previous-link t]
  19544. "--"
  19545. ["Descriptive Links"
  19546. org-toggle-link-display
  19547. :style radio
  19548. :selected org-descriptive-links
  19549. ]
  19550. ["Literal Links"
  19551. org-toggle-link-display
  19552. :style radio
  19553. :selected (not org-descriptive-links)])
  19554. "--"
  19555. ("TODO Lists"
  19556. ["TODO/DONE/-" org-todo t]
  19557. ("Select keyword"
  19558. ["Next keyword" org-shiftright (org-at-heading-p)]
  19559. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19560. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19561. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19562. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19563. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19564. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19565. "--"
  19566. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19567. :selected org-enforce-todo-dependencies :style toggle :active t]
  19568. "Settings for tree at point"
  19569. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19570. :selected (org-entry-get nil "ORDERED")
  19571. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19572. ["Do Children parallel" org-toggle-ordered-property :style radio
  19573. :selected (not (org-entry-get nil "ORDERED"))
  19574. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19575. "--"
  19576. ["Set Priority" org-priority t]
  19577. ["Priority Up" org-shiftup t]
  19578. ["Priority Down" org-shiftdown t]
  19579. "--"
  19580. ["Get news from all feeds" org-feed-update-all t]
  19581. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19582. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19583. ("TAGS and Properties"
  19584. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19585. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19586. "--"
  19587. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19588. ["Column view of properties" org-columns t]
  19589. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19590. ("Dates and Scheduling"
  19591. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19592. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19593. ("Change Date"
  19594. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19595. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19596. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19597. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19598. ["Compute Time Range" org-evaluate-time-range t]
  19599. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19600. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19601. "--"
  19602. ["Custom time format" org-toggle-time-stamp-overlays
  19603. :style radio :selected org-display-custom-times]
  19604. "--"
  19605. ["Goto Calendar" org-goto-calendar t]
  19606. ["Date from Calendar" org-date-from-calendar t]
  19607. "--"
  19608. ["Start/Restart Timer" org-timer-start t]
  19609. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19610. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19611. ["Insert Timer String" org-timer t]
  19612. ["Insert Timer Item" org-timer-item t])
  19613. ("Logging work"
  19614. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19615. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19616. ["Clock out" org-clock-out t]
  19617. ["Clock cancel" org-clock-cancel t]
  19618. "--"
  19619. ["Mark as default task" org-clock-mark-default-task t]
  19620. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19621. ["Goto running clock" org-clock-goto t]
  19622. "--"
  19623. ["Display times" org-clock-display t]
  19624. ["Create clock table" org-clock-report t]
  19625. "--"
  19626. ["Record DONE time"
  19627. (progn (setq org-log-done (not org-log-done))
  19628. (message "Switching to %s will %s record a timestamp"
  19629. (car org-done-keywords)
  19630. (if org-log-done "automatically" "not")))
  19631. :style toggle :selected org-log-done])
  19632. "--"
  19633. ["Agenda Command..." org-agenda t]
  19634. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19635. ("File List for Agenda")
  19636. ("Special views current file"
  19637. ["TODO Tree" org-show-todo-tree t]
  19638. ["Check Deadlines" org-check-deadlines t]
  19639. ["Timeline" org-timeline t]
  19640. ["Tags/Property tree" org-match-sparse-tree t])
  19641. "--"
  19642. ["Export/Publish..." org-export-dispatch t]
  19643. ("LaTeX"
  19644. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19645. :selected org-cdlatex-mode]
  19646. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19647. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19648. ["Modify math symbol" org-cdlatex-math-modify
  19649. (org-inside-LaTeX-fragment-p)]
  19650. ["Insert citation" org-reftex-citation t]
  19651. "--"
  19652. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19653. "--"
  19654. ("MobileOrg"
  19655. ["Push Files and Views" org-mobile-push t]
  19656. ["Get Captured and Flagged" org-mobile-pull t]
  19657. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19658. "--"
  19659. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19660. "--"
  19661. ("Documentation"
  19662. ["Show Version" org-version t]
  19663. ["Info Documentation" org-info t])
  19664. ("Customize"
  19665. ["Browse Org Group" org-customize t]
  19666. "--"
  19667. ["Expand This Menu" org-create-customize-menu
  19668. (fboundp 'customize-menu-create)])
  19669. ["Send bug report" org-submit-bug-report t]
  19670. "--"
  19671. ("Refresh/Reload"
  19672. ["Refresh setup current buffer" org-mode-restart t]
  19673. ["Reload Org (after update)" org-reload t]
  19674. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19675. ))
  19676. (defun org-info (&optional node)
  19677. "Read documentation for Org-mode in the info system.
  19678. With optional NODE, go directly to that node."
  19679. (interactive)
  19680. (info (format "(org)%s" (or node ""))))
  19681. ;;;###autoload
  19682. (defun org-submit-bug-report ()
  19683. "Submit a bug report on Org-mode via mail.
  19684. Don't hesitate to report any problems or inaccurate documentation.
  19685. If you don't have setup sending mail from (X)Emacs, please copy the
  19686. output buffer into your mail program, as it gives us important
  19687. information about your Org-mode version and configuration."
  19688. (interactive)
  19689. (require 'reporter)
  19690. (org-load-modules-maybe)
  19691. (org-require-autoloaded-modules)
  19692. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19693. (reporter-submit-bug-report
  19694. "emacs-orgmode@gnu.org"
  19695. (org-version nil 'full)
  19696. (let (list)
  19697. (save-window-excursion
  19698. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19699. (delete-other-windows)
  19700. (erase-buffer)
  19701. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19702. We would like to add your full Org-mode and Outline configuration to the
  19703. bug report. This greatly simplifies the work of the maintainer and
  19704. other experts on the mailing list.
  19705. HOWEVER, some variables you have customized may contain private
  19706. information. The names of customers, colleagues, or friends, might
  19707. appear in the form of file names, tags, todo states, or search strings.
  19708. If you answer yes to the prompt, you might want to check and remove
  19709. such private information before sending the email.")
  19710. (add-text-properties (point-min) (point-max) '(face org-warning))
  19711. (when (yes-or-no-p "Include your Org-mode configuration ")
  19712. (mapatoms
  19713. (lambda (v)
  19714. (and (boundp v)
  19715. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19716. (or (and (symbol-value v)
  19717. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19718. (and
  19719. (get v 'custom-type) (get v 'standard-value)
  19720. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19721. (push v list)))))
  19722. (kill-buffer (get-buffer "*Warn about privacy*"))
  19723. list))
  19724. nil nil
  19725. "Remember to cover the basics, that is, what you expected to happen and
  19726. what in fact did happen. You don't know how to make a good report? See
  19727. http://orgmode.org/manual/Feedback.html#Feedback
  19728. Your bug report will be posted to the Org-mode mailing list.
  19729. ------------------------------------------------------------------------")
  19730. (save-excursion
  19731. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19732. (replace-match "\\1Bug: \\3 [\\2]")))))
  19733. (defun org-install-agenda-files-menu ()
  19734. (let ((bl (buffer-list)))
  19735. (save-excursion
  19736. (while bl
  19737. (set-buffer (pop bl))
  19738. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19739. (when (derived-mode-p 'org-mode)
  19740. (easy-menu-change
  19741. '("Org") "File List for Agenda"
  19742. (append
  19743. (list
  19744. ["Edit File List" (org-edit-agenda-file-list) t]
  19745. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19746. ["Remove Current File from List" org-remove-file t]
  19747. ["Cycle through agenda files" org-cycle-agenda-files t]
  19748. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19749. "--")
  19750. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19751. ;;;; Documentation
  19752. (defun org-require-autoloaded-modules ()
  19753. (interactive)
  19754. (mapc 'require
  19755. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19756. org-table org-timer)))
  19757. ;;;###autoload
  19758. (defun org-reload (&optional uncompiled)
  19759. "Reload all org lisp files.
  19760. With prefix arg UNCOMPILED, load the uncompiled versions."
  19761. (interactive "P")
  19762. (require 'loadhist)
  19763. (let* ((org-dir (org-find-library-dir "org"))
  19764. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19765. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19766. (remove-re (mapconcat 'identity
  19767. (mapcar (lambda (f) (concat "^" f "$"))
  19768. (list (if (featurep 'xemacs)
  19769. "org-colview"
  19770. "org-colview-xemacs")
  19771. "org" "org-loaddefs" "org-version"))
  19772. "\\|"))
  19773. (feats (delete-dups
  19774. (mapcar 'file-name-sans-extension
  19775. (mapcar 'file-name-nondirectory
  19776. (delq nil
  19777. (mapcar 'feature-file
  19778. features))))))
  19779. (lfeat (append
  19780. (sort
  19781. (setq feats
  19782. (delq nil (mapcar
  19783. (lambda (f)
  19784. (if (and (string-match feature-re f)
  19785. (not (string-match remove-re f)))
  19786. f nil))
  19787. feats)))
  19788. 'string-lessp)
  19789. (list "org-version" "org")))
  19790. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19791. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19792. load-uncore load-misses)
  19793. (setq load-misses
  19794. (delq 't
  19795. (mapcar (lambda (f)
  19796. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19797. (and (string= org-dir contrib-dir)
  19798. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19799. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19800. (add-to-list 'load-uncore f 'append)
  19801. 't)
  19802. f))
  19803. lfeat)))
  19804. (if load-uncore
  19805. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19806. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19807. (if load-misses
  19808. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19809. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19810. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19811. ;;;###autoload
  19812. (defun org-customize ()
  19813. "Call the customize function with org as argument."
  19814. (interactive)
  19815. (org-load-modules-maybe)
  19816. (org-require-autoloaded-modules)
  19817. (customize-browse 'org))
  19818. (defun org-create-customize-menu ()
  19819. "Create a full customization menu for Org-mode, insert it into the menu."
  19820. (interactive)
  19821. (org-load-modules-maybe)
  19822. (org-require-autoloaded-modules)
  19823. (if (fboundp 'customize-menu-create)
  19824. (progn
  19825. (easy-menu-change
  19826. '("Org") "Customize"
  19827. `(["Browse Org group" org-customize t]
  19828. "--"
  19829. ,(customize-menu-create 'org)
  19830. ["Set" Custom-set t]
  19831. ["Save" Custom-save t]
  19832. ["Reset to Current" Custom-reset-current t]
  19833. ["Reset to Saved" Custom-reset-saved t]
  19834. ["Reset to Standard Settings" Custom-reset-standard t]))
  19835. (message "\"Org\"-menu now contains full customization menu"))
  19836. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19837. ;;;; Miscellaneous stuff
  19838. ;;; Generally useful functions
  19839. (defsubst org-get-at-eol (property n)
  19840. "Get text property PROPERTY at the end of line less N characters."
  19841. (get-text-property (- (point-at-eol) n) property))
  19842. (defun org-find-text-property-in-string (prop s)
  19843. "Return the first non-nil value of property PROP in string S."
  19844. (or (get-text-property 0 prop s)
  19845. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19846. prop s)))
  19847. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19848. "Display the given MESSAGE as a warning."
  19849. (if (fboundp 'display-warning)
  19850. (display-warning 'org message
  19851. (if (featurep 'xemacs) 'warning :warning))
  19852. (let ((buf (get-buffer-create "*Org warnings*")))
  19853. (with-current-buffer buf
  19854. (goto-char (point-max))
  19855. (insert "Warning (Org): " message)
  19856. (unless (bolp)
  19857. (newline)))
  19858. (display-buffer buf)
  19859. (sit-for 0))))
  19860. (defun org-eval (form)
  19861. "Eval FORM and return result."
  19862. (condition-case error
  19863. (eval form)
  19864. (error (format "%%![Error: %s]" error))))
  19865. (defun org-in-clocktable-p ()
  19866. "Check if the cursor is in a clocktable."
  19867. (let ((pos (point)) start)
  19868. (save-excursion
  19869. (end-of-line 1)
  19870. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19871. (setq start (match-beginning 0))
  19872. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19873. (>= (match-end 0) pos)
  19874. start))))
  19875. (defun org-in-verbatim-emphasis ()
  19876. (save-match-data
  19877. (and (org-in-regexp org-emph-re 2)
  19878. (>= (point) (match-beginning 3))
  19879. (<= (point) (match-end 4))
  19880. (member (match-string 3) '("=" "~")))))
  19881. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19882. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19883. (if (and marker (marker-buffer marker)
  19884. (buffer-live-p (marker-buffer marker)))
  19885. (progn
  19886. (org-pop-to-buffer-same-window (marker-buffer marker))
  19887. (if (or (> marker (point-max)) (< marker (point-min)))
  19888. (widen))
  19889. (goto-char marker)
  19890. (org-show-context 'org-goto))
  19891. (if bookmark
  19892. (bookmark-jump bookmark)
  19893. (error "Cannot find location"))))
  19894. (defun org-quote-csv-field (s)
  19895. "Quote field for inclusion in CSV material."
  19896. (if (string-match "[\",]" s)
  19897. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19898. s))
  19899. (defun org-force-self-insert (N)
  19900. "Needed to enforce self-insert under remapping."
  19901. (interactive "p")
  19902. (self-insert-command N))
  19903. (defun org-string-width (s)
  19904. "Compute width of string, ignoring invisible characters.
  19905. This ignores character with invisibility property `org-link', and also
  19906. characters with property `org-cwidth', because these will become invisible
  19907. upon the next fontification round."
  19908. (let (b l)
  19909. (when (or (eq t buffer-invisibility-spec)
  19910. (assq 'org-link buffer-invisibility-spec))
  19911. (while (setq b (text-property-any 0 (length s)
  19912. 'invisible 'org-link s))
  19913. (setq s (concat (substring s 0 b)
  19914. (substring s (or (next-single-property-change
  19915. b 'invisible s) (length s)))))))
  19916. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19917. (setq s (concat (substring s 0 b)
  19918. (substring s (or (next-single-property-change
  19919. b 'org-cwidth s) (length s))))))
  19920. (setq l (string-width s) b -1)
  19921. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19922. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19923. l))
  19924. (defun org-shorten-string (s maxlength)
  19925. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19926. If the string is shorter or has length MAXLENGTH, just return the
  19927. original string. If it is longer, the functions finds a space in the
  19928. string, breaks this string off at that locations and adds three dots
  19929. as ellipsis. Including the ellipsis, the string will not be longer
  19930. than MAXLENGTH. If finding a good breaking point in the string does
  19931. not work, the string is just chopped off in the middle of a word
  19932. if necessary."
  19933. (if (<= (length s) maxlength)
  19934. s
  19935. (let* ((n (max (- maxlength 4) 1))
  19936. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19937. (if (string-match re s)
  19938. (concat (match-string 1 s) "...")
  19939. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19940. (defun org-get-indentation (&optional line)
  19941. "Get the indentation of the current line, interpreting tabs.
  19942. When LINE is given, assume it represents a line and compute its indentation."
  19943. (if line
  19944. (if (string-match "^ *" (org-remove-tabs line))
  19945. (match-end 0))
  19946. (save-excursion
  19947. (beginning-of-line 1)
  19948. (skip-chars-forward " \t")
  19949. (current-column))))
  19950. (defun org-get-string-indentation (s)
  19951. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19952. (let ((n -1) (i 0) (w tab-width) c)
  19953. (catch 'exit
  19954. (while (< (setq n (1+ n)) (length s))
  19955. (setq c (aref s n))
  19956. (cond ((= c ?\ ) (setq i (1+ i)))
  19957. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19958. (t (throw 'exit t)))))
  19959. i))
  19960. (defun org-remove-tabs (s &optional width)
  19961. "Replace tabulators in S with spaces.
  19962. Assumes that s is a single line, starting in column 0."
  19963. (setq width (or width tab-width))
  19964. (while (string-match "\t" s)
  19965. (setq s (replace-match
  19966. (make-string
  19967. (- (* width (/ (+ (match-beginning 0) width) width))
  19968. (match-beginning 0)) ?\ )
  19969. t t s)))
  19970. s)
  19971. (defun org-fix-indentation (line ind)
  19972. "Fix indentation in LINE.
  19973. IND is a cons cell with target and minimum indentation.
  19974. If the current indentation in LINE is smaller than the minimum,
  19975. leave it alone. If it is larger than ind, set it to the target."
  19976. (let* ((l (org-remove-tabs line))
  19977. (i (org-get-indentation l))
  19978. (i1 (car ind)) (i2 (cdr ind)))
  19979. (if (>= i i2) (setq l (substring line i2)))
  19980. (if (> i1 0)
  19981. (concat (make-string i1 ?\ ) l)
  19982. l)))
  19983. (defun org-remove-indentation (code &optional n)
  19984. "Remove the maximum common indentation from the lines in CODE.
  19985. N may optionally be the number of spaces to remove."
  19986. (with-temp-buffer
  19987. (insert code)
  19988. (org-do-remove-indentation n)
  19989. (buffer-string)))
  19990. (defun org-do-remove-indentation (&optional n)
  19991. "Remove the maximum common indentation from the buffer."
  19992. (untabify (point-min) (point-max))
  19993. (let ((min 10000) re)
  19994. (if n
  19995. (setq min n)
  19996. (goto-char (point-min))
  19997. (while (re-search-forward "^ *[^ \n]" nil t)
  19998. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19999. (unless (or (= min 0) (= min 10000))
  20000. (setq re (format "^ \\{%d\\}" min))
  20001. (goto-char (point-min))
  20002. (while (re-search-forward re nil t)
  20003. (replace-match "")
  20004. (end-of-line 1))
  20005. min)))
  20006. (defun org-fill-template (template alist)
  20007. "Find each %key of ALIST in TEMPLATE and replace it."
  20008. (let ((case-fold-search nil)
  20009. entry key value)
  20010. (setq alist (sort (copy-sequence alist)
  20011. (lambda (a b) (< (length (car a)) (length (car b))))))
  20012. (while (setq entry (pop alist))
  20013. (setq template
  20014. (replace-regexp-in-string
  20015. (concat "%" (regexp-quote (car entry)))
  20016. (or (cdr entry) "") template t t)))
  20017. template))
  20018. (defun org-base-buffer (buffer)
  20019. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20020. (if (not buffer)
  20021. buffer
  20022. (or (buffer-base-buffer buffer)
  20023. buffer)))
  20024. (defun org-wrap (string &optional width lines)
  20025. "Wrap string to either a number of lines, or a width in characters.
  20026. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20027. that costs. If there is a word longer than WIDTH, the text is actually
  20028. wrapped to the length of that word.
  20029. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20030. many lines, whatever width that takes.
  20031. The return value is a list of lines, without newlines at the end."
  20032. (let* ((words (org-split-string string "[ \t\n]+"))
  20033. (maxword (apply 'max (mapcar 'org-string-width words)))
  20034. w ll)
  20035. (cond (width
  20036. (org-do-wrap words (max maxword width)))
  20037. (lines
  20038. (setq w maxword)
  20039. (setq ll (org-do-wrap words maxword))
  20040. (if (<= (length ll) lines)
  20041. ll
  20042. (setq ll words)
  20043. (while (> (length ll) lines)
  20044. (setq w (1+ w))
  20045. (setq ll (org-do-wrap words w)))
  20046. ll))
  20047. (t (error "Cannot wrap this")))))
  20048. (defun org-do-wrap (words width)
  20049. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20050. (let (lines line)
  20051. (while words
  20052. (setq line (pop words))
  20053. (while (and words (< (+ (length line) (length (car words))) width))
  20054. (setq line (concat line " " (pop words))))
  20055. (setq lines (push line lines)))
  20056. (nreverse lines)))
  20057. (defun org-split-string (string &optional separators)
  20058. "Splits STRING into substrings at SEPARATORS.
  20059. No empty strings are returned if there are matches at the beginning
  20060. and end of string."
  20061. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  20062. (start 0)
  20063. notfirst
  20064. (list nil))
  20065. (while (and (string-match rexp string
  20066. (if (and notfirst
  20067. (= start (match-beginning 0))
  20068. (< start (length string)))
  20069. (1+ start) start))
  20070. (< (match-beginning 0) (length string)))
  20071. (setq notfirst t)
  20072. (or (eq (match-beginning 0) 0)
  20073. (and (eq (match-beginning 0) (match-end 0))
  20074. (eq (match-beginning 0) start))
  20075. (setq list
  20076. (cons (substring string start (match-beginning 0))
  20077. list)))
  20078. (setq start (match-end 0)))
  20079. (or (eq start (length string))
  20080. (setq list
  20081. (cons (substring string start)
  20082. list)))
  20083. (nreverse list)))
  20084. (defun org-quote-vert (s)
  20085. "Replace \"|\" with \"\\vert\"."
  20086. (while (string-match "|" s)
  20087. (setq s (replace-match "\\vert" t t s)))
  20088. s)
  20089. (defun org-uuidgen-p (s)
  20090. "Is S an ID created by UUIDGEN?"
  20091. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20092. (defun org-in-src-block-p (&optional inside)
  20093. "Whether point is in a code source block.
  20094. When INSIDE is non-nil, don't consider we are within a src block
  20095. when point is at #+BEGIN_SRC or #+END_SRC."
  20096. (let ((case-fold-search t) ov)
  20097. (or (and (eq (get-char-property (point) 'src-block) t))
  20098. (and (not inside)
  20099. (save-match-data
  20100. (save-excursion
  20101. (beginning-of-line)
  20102. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20103. (defun org-context ()
  20104. "Return a list of contexts of the current cursor position.
  20105. If several contexts apply, all are returned.
  20106. Each context entry is a list with a symbol naming the context, and
  20107. two positions indicating start and end of the context. Possible
  20108. contexts are:
  20109. :headline anywhere in a headline
  20110. :headline-stars on the leading stars in a headline
  20111. :todo-keyword on a TODO keyword (including DONE) in a headline
  20112. :tags on the TAGS in a headline
  20113. :priority on the priority cookie in a headline
  20114. :item on the first line of a plain list item
  20115. :item-bullet on the bullet/number of a plain list item
  20116. :checkbox on the checkbox in a plain list item
  20117. :table in an org-mode table
  20118. :table-special on a special filed in a table
  20119. :table-table in a table.el table
  20120. :clocktable in a clocktable
  20121. :src-block in a source block
  20122. :link on a hyperlink
  20123. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20124. :target on a <<target>>
  20125. :radio-target on a <<<radio-target>>>
  20126. :latex-fragment on a LaTeX fragment
  20127. :latex-preview on a LaTeX fragment with overlaid preview image
  20128. This function expects the position to be visible because it uses font-lock
  20129. faces as a help to recognize the following contexts: :table-special, :link,
  20130. and :keyword."
  20131. (let* ((f (get-text-property (point) 'face))
  20132. (faces (if (listp f) f (list f)))
  20133. (case-fold-search t)
  20134. (p (point)) clist o)
  20135. ;; First the large context
  20136. (cond
  20137. ((org-at-heading-p t)
  20138. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20139. (when (progn
  20140. (beginning-of-line 1)
  20141. (looking-at org-todo-line-tags-regexp))
  20142. (push (org-point-in-group p 1 :headline-stars) clist)
  20143. (push (org-point-in-group p 2 :todo-keyword) clist)
  20144. (push (org-point-in-group p 4 :tags) clist))
  20145. (goto-char p)
  20146. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20147. (if (looking-at "\\[#[A-Z0-9]\\]")
  20148. (push (org-point-in-group p 0 :priority) clist)))
  20149. ((org-at-item-p)
  20150. (push (org-point-in-group p 2 :item-bullet) clist)
  20151. (push (list :item (point-at-bol)
  20152. (save-excursion (org-end-of-item) (point)))
  20153. clist)
  20154. (and (org-at-item-checkbox-p)
  20155. (push (org-point-in-group p 0 :checkbox) clist)))
  20156. ((org-at-table-p)
  20157. (push (list :table (org-table-begin) (org-table-end)) clist)
  20158. (if (memq 'org-formula faces)
  20159. (push (list :table-special
  20160. (previous-single-property-change p 'face)
  20161. (next-single-property-change p 'face)) clist)))
  20162. ((org-at-table-p 'any)
  20163. (push (list :table-table) clist)))
  20164. (goto-char p)
  20165. (let ((case-fold-search t))
  20166. ;; New the "medium" contexts: clocktables, source blocks
  20167. (cond ((org-in-clocktable-p)
  20168. (push (list :clocktable
  20169. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20170. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20171. (match-beginning 1))
  20172. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20173. (match-end 0))) clist))
  20174. ((org-in-src-block-p)
  20175. (push (list :src-block
  20176. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20177. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20178. (match-beginning 1))
  20179. (and (search-forward "#+END_SRC" nil t)
  20180. (match-beginning 0))) clist))))
  20181. (goto-char p)
  20182. ;; Now the small context
  20183. (cond
  20184. ((org-at-timestamp-p)
  20185. (push (org-point-in-group p 0 :timestamp) clist))
  20186. ((memq 'org-link faces)
  20187. (push (list :link
  20188. (previous-single-property-change p 'face)
  20189. (next-single-property-change p 'face)) clist))
  20190. ((memq 'org-special-keyword faces)
  20191. (push (list :keyword
  20192. (previous-single-property-change p 'face)
  20193. (next-single-property-change p 'face)) clist))
  20194. ((org-at-target-p)
  20195. (push (org-point-in-group p 0 :target) clist)
  20196. (goto-char (1- (match-beginning 0)))
  20197. (if (looking-at org-radio-target-regexp)
  20198. (push (org-point-in-group p 0 :radio-target) clist))
  20199. (goto-char p))
  20200. ((setq o (car (delq nil
  20201. (mapcar
  20202. (lambda (x)
  20203. (if (memq x org-latex-fragment-image-overlays) x))
  20204. (overlays-at (point))))))
  20205. (push (list :latex-fragment
  20206. (overlay-start o) (overlay-end o)) clist)
  20207. (push (list :latex-preview
  20208. (overlay-start o) (overlay-end o)) clist))
  20209. ((org-inside-LaTeX-fragment-p)
  20210. ;; FIXME: positions wrong.
  20211. (push (list :latex-fragment (point) (point)) clist)))
  20212. (setq clist (nreverse (delq nil clist)))
  20213. clist))
  20214. ;; FIXME: Compare with at-regexp-p Do we need both?
  20215. (defun org-in-regexp (re &optional nlines visually)
  20216. "Check if point is inside a match of regexp.
  20217. Normally only the current line is checked, but you can include NLINES extra
  20218. lines both before and after point into the search.
  20219. If VISUALLY is set, require that the cursor is not after the match but
  20220. really on, so that the block visually is on the match."
  20221. (catch 'exit
  20222. (let ((pos (point))
  20223. (eol (point-at-eol (+ 1 (or nlines 0))))
  20224. (inc (if visually 1 0)))
  20225. (save-excursion
  20226. (beginning-of-line (- 1 (or nlines 0)))
  20227. (while (re-search-forward re eol t)
  20228. (if (and (<= (match-beginning 0) pos)
  20229. (>= (+ inc (match-end 0)) pos))
  20230. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  20231. (defun org-at-regexp-p (regexp)
  20232. "Is point inside a match of REGEXP in the current line?"
  20233. (catch 'exit
  20234. (save-excursion
  20235. (let ((pos (point)) (end (point-at-eol)))
  20236. (beginning-of-line 1)
  20237. (while (re-search-forward regexp end t)
  20238. (if (and (<= (match-beginning 0) pos)
  20239. (>= (match-end 0) pos))
  20240. (throw 'exit t)))
  20241. nil))))
  20242. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20243. "Non-nil when point is between matches of START-RE and END-RE.
  20244. Also return a non-nil value when point is on one of the matches.
  20245. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20246. buffer positions. Default values are the positions of headlines
  20247. surrounding the point.
  20248. The functions returns a cons cell whose car (resp. cdr) is the
  20249. position before START-RE (resp. after END-RE)."
  20250. (save-match-data
  20251. (let ((pos (point))
  20252. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20253. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20254. beg end)
  20255. (save-excursion
  20256. ;; Point is on a block when on START-RE or if START-RE can be
  20257. ;; found before it...
  20258. (and (or (org-at-regexp-p start-re)
  20259. (re-search-backward start-re limit-up t))
  20260. (setq beg (match-beginning 0))
  20261. ;; ... and END-RE after it...
  20262. (goto-char (match-end 0))
  20263. (re-search-forward end-re limit-down t)
  20264. (> (setq end (match-end 0)) pos)
  20265. ;; ... without another START-RE in-between.
  20266. (goto-char (match-beginning 0))
  20267. (not (re-search-backward start-re (1+ beg) t))
  20268. ;; Return value.
  20269. (cons beg end))))))
  20270. (defun org-in-block-p (names)
  20271. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20272. NAMES is a list of strings containing names of blocks.
  20273. Return first block name matched, or nil. Beware that in case of
  20274. nested blocks, the returned name may not belong to the closest
  20275. block from point."
  20276. (save-match-data
  20277. (catch 'exit
  20278. (let ((case-fold-search t)
  20279. (lim-up (save-excursion (outline-previous-heading)))
  20280. (lim-down (save-excursion (outline-next-heading))))
  20281. (mapc (lambda (name)
  20282. (let ((n (regexp-quote name)))
  20283. (when (org-between-regexps-p
  20284. (concat "^[ \t]*#\\+begin_" n)
  20285. (concat "^[ \t]*#\\+end_" n)
  20286. lim-up lim-down)
  20287. (throw 'exit n))))
  20288. names))
  20289. nil)))
  20290. (defun org-in-drawer-p ()
  20291. "Non-nil if point is within a drawer.
  20292. If point is within a drawer, return it, as parsed data."
  20293. (let ((element (save-match-data (org-element-at-point))))
  20294. (while (and element (not (memq (org-element-type element)
  20295. '(drawer property-drawer))))
  20296. (setq element (org-element-property :parent element)))
  20297. element))
  20298. (defun org-occur-in-agenda-files (regexp &optional nlines)
  20299. "Call `multi-occur' with buffers for all agenda files."
  20300. (interactive "sOrg-files matching: \np")
  20301. (let* ((files (org-agenda-files))
  20302. (tnames (mapcar 'file-truename files))
  20303. (extra org-agenda-text-search-extra-files)
  20304. f)
  20305. (when (eq (car extra) 'agenda-archives)
  20306. (setq extra (cdr extra))
  20307. (setq files (org-add-archive-files files)))
  20308. (while (setq f (pop extra))
  20309. (unless (member (file-truename f) tnames)
  20310. (add-to-list 'files f 'append)
  20311. (add-to-list 'tnames (file-truename f) 'append)))
  20312. (multi-occur
  20313. (mapcar (lambda (x)
  20314. (with-current-buffer
  20315. (or (get-file-buffer x) (find-file-noselect x))
  20316. (widen)
  20317. (current-buffer)))
  20318. files)
  20319. regexp)))
  20320. (if (boundp 'occur-mode-find-occurrence-hook)
  20321. ;; Emacs 23
  20322. (add-hook 'occur-mode-find-occurrence-hook
  20323. (lambda ()
  20324. (when (derived-mode-p 'org-mode)
  20325. (org-reveal))))
  20326. ;; Emacs 22
  20327. (defadvice occur-mode-goto-occurrence
  20328. (after org-occur-reveal activate)
  20329. (and (derived-mode-p 'org-mode) (org-reveal)))
  20330. (defadvice occur-mode-goto-occurrence-other-window
  20331. (after org-occur-reveal activate)
  20332. (and (derived-mode-p 'org-mode) (org-reveal)))
  20333. (defadvice occur-mode-display-occurrence
  20334. (after org-occur-reveal activate)
  20335. (when (derived-mode-p 'org-mode)
  20336. (let ((pos (occur-mode-find-occurrence)))
  20337. (with-current-buffer (marker-buffer pos)
  20338. (save-excursion
  20339. (goto-char pos)
  20340. (org-reveal)))))))
  20341. (defun org-occur-link-in-agenda-files ()
  20342. "Create a link and search for it in the agendas.
  20343. The link is not stored in `org-stored-links', it is just created
  20344. for the search purpose."
  20345. (interactive)
  20346. (let ((link (condition-case nil
  20347. (org-store-link nil)
  20348. (error "Unable to create a link to here"))))
  20349. (org-occur-in-agenda-files (regexp-quote link))))
  20350. (defun org-reverse-string (string)
  20351. "Return the reverse of STRING."
  20352. (apply 'string (reverse (string-to-list string))))
  20353. ;; defsubst org-uniquify must be defined before first use
  20354. (defun org-uniquify-alist (alist)
  20355. "Merge elements of ALIST with the same key.
  20356. For example, in this alist:
  20357. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20358. => '((a 1 3) (b 2))
  20359. merge (a 1) and (a 3) into (a 1 3).
  20360. The function returns the new ALIST."
  20361. (let (rtn)
  20362. (mapc
  20363. (lambda (e)
  20364. (let (n)
  20365. (if (not (assoc (car e) rtn))
  20366. (push e rtn)
  20367. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20368. (setq rtn (assq-delete-all (car e) rtn))
  20369. (push n rtn))))
  20370. alist)
  20371. rtn))
  20372. (defun org-delete-all (elts list)
  20373. "Remove all elements in ELTS from LIST."
  20374. (while elts
  20375. (setq list (delete (pop elts) list)))
  20376. list)
  20377. (defun org-count (cl-item cl-seq)
  20378. "Count the number of occurrences of ITEM in SEQ.
  20379. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20380. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20381. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20382. (while (< cl-start cl-end)
  20383. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20384. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20385. (setq cl-start (1+ cl-start)))
  20386. cl-count))
  20387. (defun org-remove-if (predicate seq)
  20388. "Remove everything from SEQ that fulfills PREDICATE."
  20389. (let (res e)
  20390. (while seq
  20391. (setq e (pop seq))
  20392. (if (not (funcall predicate e)) (push e res)))
  20393. (nreverse res)))
  20394. (defun org-remove-if-not (predicate seq)
  20395. "Remove everything from SEQ that does not fulfill PREDICATE."
  20396. (let (res e)
  20397. (while seq
  20398. (setq e (pop seq))
  20399. (if (funcall predicate e) (push e res)))
  20400. (nreverse res)))
  20401. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20402. "Reduce two-argument FUNCTION across SEQ.
  20403. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20404. \":initial-value\" removed."
  20405. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20406. (cadr (memq :initial-value cl-keys)))
  20407. (cl-seq (pop cl-seq))
  20408. (t (funcall cl-func)))))
  20409. (while cl-seq
  20410. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20411. cl-accum))
  20412. (defun org-every (pred seq)
  20413. "Return true if PREDICATE is true of every element of SEQ.
  20414. Adapted from `every' in cl.el."
  20415. (catch 'org-every
  20416. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20417. t))
  20418. (defun org-some (pred seq)
  20419. "Return true if PREDICATE is true of any element of SEQ.
  20420. Adapted from `some' in cl.el."
  20421. (catch 'org-some
  20422. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20423. nil))
  20424. (defun org-back-over-empty-lines ()
  20425. "Move backwards over whitespace, to the beginning of the first empty line.
  20426. Returns the number of empty lines passed."
  20427. (let ((pos (point)))
  20428. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20429. (skip-chars-backward " \t\n\r")
  20430. (unless (eobp)
  20431. (forward-line -1)))
  20432. (beginning-of-line 2)
  20433. (goto-char (min (point) pos))
  20434. (count-lines (point) pos)))
  20435. (defun org-skip-whitespace ()
  20436. (skip-chars-forward " \t\n\r"))
  20437. (defun org-point-in-group (point group &optional context)
  20438. "Check if POINT is in match-group GROUP.
  20439. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20440. match. If the match group does not exist or point is not inside it,
  20441. return nil."
  20442. (and (match-beginning group)
  20443. (>= point (match-beginning group))
  20444. (<= point (match-end group))
  20445. (if context
  20446. (list context (match-beginning group) (match-end group))
  20447. t)))
  20448. (defun org-switch-to-buffer-other-window (&rest args)
  20449. "Switch to buffer in a second window on the current frame.
  20450. In particular, do not allow pop-up frames.
  20451. Returns the newly created buffer."
  20452. (org-no-popups
  20453. (apply 'switch-to-buffer-other-window args)))
  20454. (defun org-combine-plists (&rest plists)
  20455. "Create a single property list from all plists in PLISTS.
  20456. The process starts by copying the first list, and then setting properties
  20457. from the other lists. Settings in the last list are the most significant
  20458. ones and overrule settings in the other lists."
  20459. (let ((rtn (copy-sequence (pop plists)))
  20460. p v ls)
  20461. (while plists
  20462. (setq ls (pop plists))
  20463. (while ls
  20464. (setq p (pop ls) v (pop ls))
  20465. (setq rtn (plist-put rtn p v))))
  20466. rtn))
  20467. (defun org-replace-escapes (string table)
  20468. "Replace %-escapes in STRING with values in TABLE.
  20469. TABLE is an association list with keys like \"%a\" and string values.
  20470. The sequences in STRING may contain normal field width and padding information,
  20471. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20472. so values can contain further %-escapes if they are define later in TABLE."
  20473. (let ((tbl (copy-alist table))
  20474. (case-fold-search nil)
  20475. (pchg 0)
  20476. e re rpl)
  20477. (while (setq e (pop tbl))
  20478. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20479. (when (and (cdr e) (string-match re (cdr e)))
  20480. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20481. (safe "SREF"))
  20482. (add-text-properties 0 3 (list 'sref sref) safe)
  20483. (setcdr e (replace-match safe t t (cdr e)))))
  20484. (while (string-match re string)
  20485. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20486. (cdr e)))
  20487. (setq string (replace-match rpl t t string))))
  20488. (while (setq pchg (next-property-change pchg string))
  20489. (let ((sref (get-text-property pchg 'sref string)))
  20490. (when (and sref (string-match "SREF" string pchg))
  20491. (setq string (replace-match sref t t string)))))
  20492. string))
  20493. (defun org-sublist (list start end)
  20494. "Return a section of LIST, from START to END.
  20495. Counting starts at 1."
  20496. (let (rtn (c start))
  20497. (setq list (nthcdr (1- start) list))
  20498. (while (and list (<= c end))
  20499. (push (pop list) rtn)
  20500. (setq c (1+ c)))
  20501. (nreverse rtn)))
  20502. (defun org-find-base-buffer-visiting (file)
  20503. "Like `find-buffer-visiting' but always return the base buffer and
  20504. not an indirect buffer."
  20505. (let ((buf (or (get-file-buffer file)
  20506. (find-buffer-visiting file))))
  20507. (if buf
  20508. (or (buffer-base-buffer buf) buf)
  20509. nil)))
  20510. (defun org-image-file-name-regexp (&optional extensions)
  20511. "Return regexp matching the file names of images.
  20512. If EXTENSIONS is given, only match these."
  20513. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20514. (image-file-name-regexp)
  20515. (let ((image-file-name-extensions
  20516. (or extensions
  20517. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20518. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20519. (concat "\\."
  20520. (regexp-opt (nconc (mapcar 'upcase
  20521. image-file-name-extensions)
  20522. image-file-name-extensions)
  20523. t)
  20524. "\\'"))))
  20525. (defun org-file-image-p (file &optional extensions)
  20526. "Return non-nil if FILE is an image."
  20527. (save-match-data
  20528. (string-match (org-image-file-name-regexp extensions) file)))
  20529. (defun org-get-cursor-date (&optional with-time)
  20530. "Return the date at cursor in as a time.
  20531. This works in the calendar and in the agenda, anywhere else it just
  20532. returns the current time.
  20533. If WITH-TIME is non-nil, returns the time of the event at point (in
  20534. the agenda) or the current time of the day."
  20535. (let (date day defd tp tm hod mod)
  20536. (when with-time
  20537. (setq tp (get-text-property (point) 'time))
  20538. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20539. (setq hod (string-to-number (match-string 1 tp))
  20540. mod (string-to-number (match-string 2 tp))))
  20541. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20542. mod (nth 1 (decode-time (current-time))))))
  20543. (cond
  20544. ((eq major-mode 'calendar-mode)
  20545. (setq date (calendar-cursor-to-date)
  20546. defd (encode-time 0 (or mod 0) (or hod 0)
  20547. (nth 1 date) (nth 0 date) (nth 2 date))))
  20548. ((eq major-mode 'org-agenda-mode)
  20549. (setq day (get-text-property (point) 'day))
  20550. (if day
  20551. (setq date (calendar-gregorian-from-absolute day)
  20552. defd (encode-time 0 (or mod 0) (or hod 0)
  20553. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20554. (or defd (current-time))))
  20555. (defun org-mark-subtree (&optional up)
  20556. "Mark the current subtree.
  20557. This puts point at the start of the current subtree, and mark at
  20558. the end. If a numeric prefix UP is given, move up into the
  20559. hierarchy of headlines by UP levels before marking the subtree."
  20560. (interactive "P")
  20561. (org-with-limited-levels
  20562. (cond ((org-at-heading-p) (beginning-of-line))
  20563. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20564. (t (outline-previous-visible-heading 1))))
  20565. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20566. (if (org-called-interactively-p 'any)
  20567. (call-interactively 'org-mark-element)
  20568. (org-mark-element)))
  20569. ;;; Indentation
  20570. (defun org--get-expected-indentation (element contentsp)
  20571. "Expected indentation column for current line, according to ELEMENT.
  20572. ELEMENT is an element containing point. CONTENTSP is non-nil
  20573. when indentation is to be computed according to contents of
  20574. ELEMENT."
  20575. (let ((type (org-element-type element))
  20576. (start (org-element-property :begin element)))
  20577. (org-with-wide-buffer
  20578. (cond
  20579. (contentsp
  20580. (case type
  20581. (footnote-definition 0)
  20582. ((headline inlinetask nil)
  20583. (if (not org-adapt-indentation) 0
  20584. (let ((level (org-current-level)))
  20585. (if level (1+ level) 0))))
  20586. ((item plain list)
  20587. (org-list-item-body-column
  20588. (org-element-property :post-affiliated element)))
  20589. (otherwise
  20590. (goto-char start)
  20591. (org-get-indentation))))
  20592. ((memq type '(headline inlinetask nil))
  20593. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  20594. (org--get-expected-indentation element t)
  20595. 0))
  20596. ((eq type 'footnote-definition) 0)
  20597. ;; First paragraph of a footnote definition or an item.
  20598. ;; Indent like parent.
  20599. ((< (line-beginning-position) start)
  20600. (org--get-expected-indentation
  20601. (org-element-property :parent element) t))
  20602. ;; At first line: indent according to previous sibling, if any,
  20603. ;; ignoring footnote definitions and inline tasks, or parent's
  20604. ;; contents.
  20605. ((= (line-beginning-position) start)
  20606. (catch 'exit
  20607. (while t
  20608. (if (= (point-min) start) (throw 'exit 0)
  20609. (goto-char (1- start))
  20610. (let ((previous (org-element-at-point)))
  20611. (while (let ((parent (org-element-property :parent previous)))
  20612. (and parent
  20613. (setq previous parent)
  20614. (<= (org-element-property :end parent) start))))
  20615. (cond ((or (not previous)
  20616. (> (org-element-property :end previous) start))
  20617. (throw 'exit (org--get-expected-indentation previous t)))
  20618. ((memq (org-element-type previous)
  20619. '(footnote-definition inlinetask))
  20620. (setq start (org-element-property :begin previous)))
  20621. (t (goto-char (org-element-property :begin previous))
  20622. (throw 'exit (org-get-indentation)))))))))
  20623. ;; Otherwise, move to the first non-blank line above.
  20624. (t
  20625. (beginning-of-line)
  20626. (let ((pos (point)))
  20627. (skip-chars-backward " \r\t\n")
  20628. (cond
  20629. ;; Two blank lines end a footnote definition or a plain
  20630. ;; list. When we indent an empty line after them, the
  20631. ;; containing list or footnote definition is over, so it
  20632. ;; qualifies as a previous sibling. Therefore, we indent
  20633. ;; like its first line.
  20634. ((and (memq type '(footnote-definition plain-list))
  20635. (> (count-lines (point) pos) 2))
  20636. (goto-char start)
  20637. (org-get-indentation))
  20638. ;; Line above is the first one of a paragraph at the
  20639. ;; beginning of an item or a footnote definition. Indent
  20640. ;; like parent.
  20641. ((< (line-beginning-position) start)
  20642. (org--get-expected-indentation
  20643. (org-element-property :parent element) t))
  20644. ;; POS is after contents in a greater element. Indent like
  20645. ;; the beginning of the element.
  20646. ;;
  20647. ;; As a special case, if point is at the end of a footnote
  20648. ;; definition or an item, indent like the very last element
  20649. ;; within.
  20650. ((let ((cend (org-element-property :contents-end element)))
  20651. (and cend (<= cend pos)))
  20652. (if (memq type '(footnote-definition item plain-list))
  20653. (org--get-expected-indentation (org-element-at-point) nil)
  20654. (goto-char start)
  20655. (org-get-indentation)))
  20656. ;; In any other case, indent like the current line.
  20657. (t (org-get-indentation)))))))))
  20658. (defun org--align-node-property ()
  20659. "Align node property at point.
  20660. Alignment is done according to `org-property-format', which see."
  20661. (when (save-excursion
  20662. (beginning-of-line)
  20663. (looking-at org-property-re))
  20664. (replace-match
  20665. (concat (match-string 4)
  20666. (org-trim
  20667. (format org-property-format (match-string 1) (match-string 3))))
  20668. t t)))
  20669. (defun org-indent-line ()
  20670. "Indent line depending on context.
  20671. Indentation is done according to the following rules:
  20672. - Footnote definitions, headlines and inline tasks have to
  20673. start at column 0.
  20674. - On the very first line of an element, consider, in order, the
  20675. next rules until one matches:
  20676. 1. If there's a sibling element before, ignoring footnote
  20677. definitions and inline tasks, indent like its first line.
  20678. 2. If element has a parent, indent like its contents. More
  20679. precisely, if parent is an item, indent after the
  20680. description part, if any, or the bullet (see
  20681. ``org-list-description-max-indent'). Else, indent like
  20682. parent's first line.
  20683. 3. Otherwise, indent relatively to current level, if
  20684. `org-adapt-indentation' is non-nil, or to left margin.
  20685. - On a blank line at the end of a plain list, an item, or
  20686. a footnote definition, indent like the very last element
  20687. within.
  20688. - In the code part of a source block, use language major mode
  20689. to indent current line if `org-src-tab-acts-natively' is
  20690. non-nil. If it is nil, do nothing.
  20691. - Otherwise, indent like the first non-blank line above.
  20692. The function doesn't indent an item as it could break the whole
  20693. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  20694. \\[org-shiftmetaright].
  20695. Also align node properties according to `org-property-format'."
  20696. (interactive)
  20697. (cond
  20698. (orgstruct-is-++
  20699. (let ((indent-line-function
  20700. (cadadr (assq 'indent-line-function org-fb-vars))))
  20701. (indent-according-to-mode)))
  20702. ((org-at-heading-p) 'noindent)
  20703. (t
  20704. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  20705. (type (org-element-type element)))
  20706. (cond ((and (memq type '(plain-list item))
  20707. (= (line-beginning-position)
  20708. (org-element-property :post-affiliated element)))
  20709. 'noindent)
  20710. ((and (eq type 'src-block)
  20711. org-src-tab-acts-natively
  20712. (> (line-beginning-position)
  20713. (org-element-property :post-affiliated element))
  20714. (< (line-beginning-position)
  20715. (org-with-wide-buffer
  20716. (goto-char (org-element-property :end element))
  20717. (skip-chars-backward " \r\t\n")
  20718. (line-beginning-position))))
  20719. (let ((org-src-strip-leading-and-trailing-blank-lines nil))
  20720. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB"))))
  20721. (t
  20722. (let ((column (org--get-expected-indentation element nil)))
  20723. ;; Preserve current column.
  20724. (if (<= (current-column) (current-indentation))
  20725. (org-indent-line-to column)
  20726. (save-excursion (org-indent-line-to column))))
  20727. ;; Align node property. Also preserve current column.
  20728. (when (eq type 'node-property)
  20729. (let ((column (current-column)))
  20730. (org--align-node-property)
  20731. (org-move-to-column column)))))))))
  20732. (defun org-indent-region (start end)
  20733. "Indent each non-blank line in the region.
  20734. Called from a program, START and END specify the region to
  20735. indent. The function will not indent contents of example blocks,
  20736. verse blocks and export blocks as leading white spaces are
  20737. assumed to be significant there."
  20738. (interactive "r")
  20739. (save-excursion
  20740. (goto-char start)
  20741. (beginning-of-line)
  20742. (let ((indent-to
  20743. (lambda (ind pos)
  20744. ;; Set IND as indentation for all lines between point and
  20745. ;; POS or END, whichever comes first. Blank lines are
  20746. ;; ignored. Leave point after POS once done.
  20747. (let ((limit (copy-marker (min end pos))))
  20748. (while (< (point) limit)
  20749. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  20750. (forward-line))
  20751. (set-marker limit nil))))
  20752. (end (copy-marker end)))
  20753. (while (< (point) end)
  20754. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  20755. (let* ((element (org-element-at-point))
  20756. (type (org-element-type element))
  20757. (element-end (copy-marker (org-element-property :end element)))
  20758. (ind (org--get-expected-indentation element nil)))
  20759. (if (or (memq type '(paragraph table table-row))
  20760. (not (or (org-element-property :contents-begin element)
  20761. (memq type
  20762. '(example-block export-block src-block)))))
  20763. ;; Elements here are indented as a single block. Also
  20764. ;; align node properties.
  20765. (progn
  20766. (when (eq type 'node-property)
  20767. (org--align-node-property)
  20768. (beginning-of-line))
  20769. (funcall indent-to ind element-end))
  20770. ;; Elements in this category consist of three parts:
  20771. ;; before the contents, the contents, and after the
  20772. ;; contents. The contents are treated specially,
  20773. ;; according to the element type, or not indented at
  20774. ;; all. Other parts are indented as a single block.
  20775. (let* ((post (copy-marker
  20776. (org-element-property :post-affiliated element)))
  20777. (cbeg
  20778. (copy-marker
  20779. (cond
  20780. ((not (org-element-property :contents-begin element))
  20781. ;; Fake contents for source blocks.
  20782. (org-with-wide-buffer
  20783. (goto-char post)
  20784. (forward-line)
  20785. (point)))
  20786. ((memq type '(footnote-definition item plain-list))
  20787. ;; Contents in these elements could start on
  20788. ;; the same line as the beginning of the
  20789. ;; element. Make sure we start indenting
  20790. ;; from the second line.
  20791. (org-with-wide-buffer
  20792. (goto-char post)
  20793. (forward-line)
  20794. (point)))
  20795. (t (org-element-property :contents-begin element)))))
  20796. (cend (copy-marker
  20797. (or (org-element-property :contents-end element)
  20798. ;; Fake contents for source blocks.
  20799. (org-with-wide-buffer
  20800. (goto-char element-end)
  20801. (skip-chars-backward " \r\t\n")
  20802. (line-beginning-position))))))
  20803. (funcall indent-to ind cbeg)
  20804. (when (< (point) end)
  20805. (case type
  20806. ((example-block export-block verse-block))
  20807. (src-block
  20808. ;; In a source block, indent source code according
  20809. ;; to language major mode, but only if
  20810. ;; `org-src-tab-acts-natively' is non-nil.
  20811. (when (and (< (point) end) org-src-tab-acts-natively)
  20812. (ignore-errors
  20813. (let (org-src-strip-leading-and-trailing-blank-lines
  20814. ;; Region boundaries in edit buffer.
  20815. (start (1+ (- (point) cbeg)))
  20816. (end (- (min cend end) cbeg)))
  20817. (org-babel-do-in-edit-buffer
  20818. (indent-region start end))))))
  20819. (t (org-indent-region (point) (min cend end))))
  20820. (goto-char (min cend end))
  20821. (when (< (point) end) (funcall indent-to ind element-end)))
  20822. (set-marker post nil)
  20823. (set-marker cbeg nil)
  20824. (set-marker cend nil)))
  20825. (set-marker element-end nil))))
  20826. (set-marker end nil))))
  20827. (defun org-indent-drawer ()
  20828. "Indent the drawer at point."
  20829. (interactive)
  20830. (unless (save-excursion
  20831. (beginning-of-line)
  20832. (org-looking-at-p org-drawer-regexp))
  20833. (user-error "Not at a drawer"))
  20834. (let ((element (org-element-at-point)))
  20835. (unless (memq (org-element-type element) '(drawer property-drawer))
  20836. (user-error "Not at a drawer"))
  20837. (org-with-wide-buffer
  20838. (org-indent-region (org-element-property :begin element)
  20839. (org-element-property :end element))))
  20840. (message "Drawer at point indented"))
  20841. (defun org-indent-block ()
  20842. "Indent the block at point."
  20843. (interactive)
  20844. (unless (save-excursion
  20845. (beginning-of-line)
  20846. (let ((case-fold-search t))
  20847. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  20848. (user-error "Not at a block"))
  20849. (let ((element (org-element-at-point)))
  20850. (unless (memq (org-element-type element)
  20851. '(comment-block center-block dynamic-block example-block
  20852. export-block quote-block special-block
  20853. src-block verse-block))
  20854. (user-error "Not at a block"))
  20855. (org-with-wide-buffer
  20856. (org-indent-region (org-element-property :begin element)
  20857. (org-element-property :end element))))
  20858. (message "Block at point indented"))
  20859. ;;; Filling
  20860. ;; We use our own fill-paragraph and auto-fill functions.
  20861. ;; `org-fill-paragraph' relies on adaptive filling and context
  20862. ;; checking. Appropriate `fill-prefix' is computed with
  20863. ;; `org-adaptive-fill-function'.
  20864. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20865. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20866. ;; `org-adaptive-fill-function' value. Internally,
  20867. ;; `org-comment-line-break-function' breaks the line.
  20868. ;; `org-setup-filling' installs filling and auto-filling related
  20869. ;; variables during `org-mode' initialization.
  20870. (defvar org-element-paragraph-separate) ; org-element.el
  20871. (defun org-setup-filling ()
  20872. (require 'org-element)
  20873. ;; Prevent auto-fill from inserting unwanted new items.
  20874. (when (boundp 'fill-nobreak-predicate)
  20875. (org-set-local
  20876. 'fill-nobreak-predicate
  20877. (org-uniquify
  20878. (append fill-nobreak-predicate
  20879. '(org-fill-line-break-nobreak-p
  20880. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20881. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  20882. (org-set-local 'paragraph-start paragraph-ending)
  20883. (org-set-local 'paragraph-separate paragraph-ending))
  20884. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20885. (org-set-local 'auto-fill-inhibit-regexp nil)
  20886. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20887. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20888. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20889. (defun org-fill-line-break-nobreak-p ()
  20890. "Non-nil when a new line at point would create an Org line break."
  20891. (save-excursion
  20892. (skip-chars-backward "[ \t]")
  20893. (skip-chars-backward "\\\\")
  20894. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20895. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20896. "Non-nil when a new line at point would split a timestamp."
  20897. (and (org-at-timestamp-p t)
  20898. (not (looking-at org-ts-regexp-both))))
  20899. (declare-function message-in-body-p "message" ())
  20900. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20901. (defun org-adaptive-fill-function ()
  20902. "Compute a fill prefix for the current line.
  20903. Return fill prefix, as a string, or nil if current line isn't
  20904. meant to be filled. For convenience, if `adaptive-fill-regexp'
  20905. matches in paragraphs or comments, use it."
  20906. (catch 'exit
  20907. (when (derived-mode-p 'message-mode)
  20908. (save-excursion
  20909. (beginning-of-line)
  20910. (cond ((or (not (message-in-body-p))
  20911. (looking-at orgtbl-line-start-regexp))
  20912. (throw 'exit nil))
  20913. ((looking-at message-cite-prefix-regexp)
  20914. (throw 'exit (match-string-no-properties 0)))
  20915. ((looking-at org-outline-regexp)
  20916. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  20917. (org-with-wide-buffer
  20918. (unless (org-at-heading-p)
  20919. (let* ((p (line-beginning-position))
  20920. (element (save-excursion
  20921. (beginning-of-line)
  20922. (org-element-at-point)))
  20923. (type (org-element-type element))
  20924. (post-affiliated (org-element-property :post-affiliated element)))
  20925. (unless (< p post-affiliated)
  20926. (case type
  20927. (comment
  20928. (save-excursion
  20929. (beginning-of-line)
  20930. (looking-at "[ \t]*")
  20931. (concat (match-string 0) "# ")))
  20932. (footnote-definition "")
  20933. ((item plain-list)
  20934. (make-string (org-list-item-body-column post-affiliated) ?\s))
  20935. (paragraph
  20936. ;; Fill prefix is usually the same as the current line,
  20937. ;; unless the paragraph is at the beginning of an item.
  20938. (let ((parent (org-element-property :parent element)))
  20939. (save-excursion
  20940. (beginning-of-line)
  20941. (cond ((eq (org-element-type parent) 'item)
  20942. (make-string (org-list-item-body-column
  20943. (org-element-property :begin parent))
  20944. ?\s))
  20945. ((and adaptive-fill-regexp
  20946. ;; Locally disable
  20947. ;; `adaptive-fill-function' to let
  20948. ;; `fill-context-prefix' handle
  20949. ;; `adaptive-fill-regexp' variable.
  20950. (let (adaptive-fill-function)
  20951. (fill-context-prefix
  20952. post-affiliated
  20953. (org-element-property :end element)))))
  20954. ((looking-at "[ \t]+") (match-string 0))
  20955. (t "")))))
  20956. (comment-block
  20957. ;; Only fill contents if P is within block boundaries.
  20958. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20959. (forward-line)
  20960. (point)))
  20961. (cend (save-excursion
  20962. (goto-char (org-element-property :end element))
  20963. (skip-chars-backward " \r\t\n")
  20964. (line-beginning-position))))
  20965. (when (and (>= p cbeg) (< p cend))
  20966. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20967. (match-string 0)
  20968. "")))))))))))
  20969. (declare-function message-goto-body "message" ())
  20970. (defvar message-cite-prefix-regexp) ; From message.el
  20971. (defun org-fill-paragraph (&optional justify)
  20972. "Fill element at point, when applicable.
  20973. This function only applies to comment blocks, comments, example
  20974. blocks and paragraphs. Also, as a special case, re-align table
  20975. when point is at one.
  20976. If JUSTIFY is non-nil (interactively, with prefix argument),
  20977. justify as well. If `sentence-end-double-space' is non-nil, then
  20978. period followed by one space does not end a sentence, so don't
  20979. break a line there. The variable `fill-column' controls the
  20980. width for filling.
  20981. For convenience, when point is at a plain list, an item or
  20982. a footnote definition, try to fill the first paragraph within."
  20983. (interactive)
  20984. (if (and (derived-mode-p 'message-mode)
  20985. (or (not (message-in-body-p))
  20986. (save-excursion (move-beginning-of-line 1)
  20987. (looking-at message-cite-prefix-regexp))))
  20988. ;; First ensure filling is correct in message-mode.
  20989. (let ((fill-paragraph-function
  20990. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20991. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20992. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20993. (paragraph-separate
  20994. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20995. (fill-paragraph nil))
  20996. (with-syntax-table org-mode-transpose-word-syntax-table
  20997. ;; Move to end of line in order to get the first paragraph
  20998. ;; within a plain list or a footnote definition.
  20999. (let ((element (save-excursion
  21000. (end-of-line)
  21001. (or (ignore-errors (org-element-at-point))
  21002. (user-error "An element cannot be parsed line %d"
  21003. (line-number-at-pos (point)))))))
  21004. ;; First check if point is in a blank line at the beginning of
  21005. ;; the buffer. In that case, ignore filling.
  21006. (case (org-element-type element)
  21007. ;; Use major mode filling function is src blocks.
  21008. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21009. ;; Align Org tables, leave table.el tables as-is.
  21010. (table-row (org-table-align) t)
  21011. (table
  21012. (when (eq (org-element-property :type element) 'org)
  21013. (save-excursion
  21014. (goto-char (org-element-property :post-affiliated element))
  21015. (org-table-align)))
  21016. t)
  21017. (paragraph
  21018. ;; Paragraphs may contain `line-break' type objects.
  21019. (let ((beg (max (point-min)
  21020. (org-element-property :contents-begin element)))
  21021. (end (min (point-max)
  21022. (org-element-property :contents-end element))))
  21023. ;; Do nothing if point is at an affiliated keyword.
  21024. (if (< (line-end-position) beg) t
  21025. (when (derived-mode-p 'message-mode)
  21026. ;; In `message-mode', do not fill following citation
  21027. ;; in current paragraph nor text before message body.
  21028. (let ((body-start (save-excursion (message-goto-body))))
  21029. (when body-start (setq beg (max body-start beg))))
  21030. (when (save-excursion
  21031. (re-search-forward
  21032. (concat "^" message-cite-prefix-regexp) end t))
  21033. (setq end (match-beginning 0))))
  21034. ;; Fill paragraph, taking line breaks into account.
  21035. (save-excursion
  21036. (goto-char beg)
  21037. (let ((cuts (list beg)))
  21038. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21039. (when (eq 'line-break
  21040. (org-element-type
  21041. (save-excursion (backward-char)
  21042. (org-element-context))))
  21043. (push (point) cuts)))
  21044. (dolist (c (delq end cuts))
  21045. (fill-region-as-paragraph c end justify)
  21046. (setq end c))))
  21047. t)))
  21048. ;; Contents of `comment-block' type elements should be
  21049. ;; filled as plain text, but only if point is within block
  21050. ;; markers.
  21051. (comment-block
  21052. (let* ((case-fold-search t)
  21053. (beg (save-excursion
  21054. (goto-char (org-element-property :begin element))
  21055. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21056. (forward-line)
  21057. (point)))
  21058. (end (save-excursion
  21059. (goto-char (org-element-property :end element))
  21060. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21061. (line-beginning-position))))
  21062. (if (or (< (point) beg) (> (point) end)) t
  21063. (fill-region-as-paragraph
  21064. (save-excursion (end-of-line)
  21065. (re-search-backward "^[ \t]*$" beg 'move)
  21066. (line-beginning-position))
  21067. (save-excursion (beginning-of-line)
  21068. (re-search-forward "^[ \t]*$" end 'move)
  21069. (line-beginning-position))
  21070. justify))))
  21071. ;; Fill comments.
  21072. (comment
  21073. (let ((begin (org-element-property :post-affiliated element))
  21074. (end (org-element-property :end element)))
  21075. (when (and (>= (point) begin) (<= (point) end))
  21076. (let ((begin (save-excursion
  21077. (end-of-line)
  21078. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21079. (progn (forward-line) (point))
  21080. begin)))
  21081. (end (save-excursion
  21082. (end-of-line)
  21083. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21084. (1- (line-beginning-position))
  21085. (skip-chars-backward " \r\t\n")
  21086. (line-end-position)))))
  21087. ;; Do not fill comments when at a blank line.
  21088. (when (> end begin)
  21089. (let ((fill-prefix
  21090. (save-excursion
  21091. (beginning-of-line)
  21092. (looking-at "[ \t]*#")
  21093. (let ((comment-prefix (match-string 0)))
  21094. (goto-char (match-end 0))
  21095. (if (looking-at adaptive-fill-regexp)
  21096. (concat comment-prefix (match-string 0))
  21097. (concat comment-prefix " "))))))
  21098. (save-excursion
  21099. (fill-region-as-paragraph begin end justify))))))
  21100. t))
  21101. ;; Ignore every other element.
  21102. (otherwise t))))))
  21103. (defun org-auto-fill-function ()
  21104. "Auto-fill function."
  21105. ;; Check if auto-filling is meaningful.
  21106. (let ((fc (current-fill-column)))
  21107. (when (and fc (> (current-column) fc))
  21108. (let* ((fill-prefix (org-adaptive-fill-function))
  21109. ;; Enforce empty fill prefix, if required. Otherwise, it
  21110. ;; will be computed again.
  21111. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21112. (when fill-prefix (do-auto-fill))))))
  21113. (defun org-comment-line-break-function (&optional soft)
  21114. "Break line at point and indent, continuing comment if within one.
  21115. The inserted newline is marked hard if variable
  21116. `use-hard-newlines' is true, unless optional argument SOFT is
  21117. non-nil."
  21118. (if soft (insert-and-inherit ?\n) (newline 1))
  21119. (save-excursion (forward-char -1) (delete-horizontal-space))
  21120. (delete-horizontal-space)
  21121. (indent-to-left-margin)
  21122. (insert-before-markers-and-inherit fill-prefix))
  21123. ;;; Fixed Width Areas
  21124. (defun org-toggle-fixed-width ()
  21125. "Toggle fixed-width markup.
  21126. Add or remove fixed-width markup on current line, whenever it
  21127. makes sense. Return an error otherwise.
  21128. If a region is active and if it contains only fixed-width areas
  21129. or blank lines, remove all fixed-width markup in it. If the
  21130. region contains anything else, convert all non-fixed-width lines
  21131. to fixed-width ones.
  21132. Blank lines at the end of the region are ignored unless the
  21133. region only contains such lines."
  21134. (interactive)
  21135. (if (not (org-region-active-p))
  21136. ;; No region:
  21137. ;;
  21138. ;; Remove fixed width marker only in a fixed-with element.
  21139. ;;
  21140. ;; Add fixed width maker in paragraphs, in blank lines after
  21141. ;; elements or at the beginning of a headline or an inlinetask,
  21142. ;; and before any one-line elements (e.g., a clock).
  21143. (progn
  21144. (beginning-of-line)
  21145. (let* ((element (org-element-at-point))
  21146. (type (org-element-type element)))
  21147. (cond
  21148. ((and (eq type 'fixed-width)
  21149. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21150. (replace-match
  21151. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21152. ((and (memq type '(babel-call clock comment diary-sexp headline
  21153. horizontal-rule keyword paragraph
  21154. planning))
  21155. (<= (org-element-property :post-affiliated element) (point)))
  21156. (skip-chars-forward " \t")
  21157. (insert ": "))
  21158. ((and (org-looking-at-p "[ \t]*$")
  21159. (or (eq type 'inlinetask)
  21160. (save-excursion
  21161. (skip-chars-forward " \r\t\n")
  21162. (<= (org-element-property :end element) (point)))))
  21163. (delete-region (point) (line-end-position))
  21164. (org-indent-line)
  21165. (insert ": "))
  21166. (t (user-error "Cannot insert a fixed-width line here")))))
  21167. ;; Region active.
  21168. (let* ((begin (save-excursion
  21169. (goto-char (region-beginning))
  21170. (line-beginning-position)))
  21171. (end (copy-marker
  21172. (save-excursion
  21173. (goto-char (region-end))
  21174. (unless (eolp) (beginning-of-line))
  21175. (if (save-excursion (re-search-backward "\\S-" begin t))
  21176. (progn (skip-chars-backward " \r\t\n") (point))
  21177. (point)))))
  21178. (all-fixed-width-p
  21179. (catch 'not-all-p
  21180. (save-excursion
  21181. (goto-char begin)
  21182. (skip-chars-forward " \r\t\n")
  21183. (when (eobp) (throw 'not-all-p nil))
  21184. (while (< (point) end)
  21185. (let ((element (org-element-at-point)))
  21186. (if (eq (org-element-type element) 'fixed-width)
  21187. (goto-char (org-element-property :end element))
  21188. (throw 'not-all-p nil))))
  21189. t))))
  21190. (if all-fixed-width-p
  21191. (save-excursion
  21192. (goto-char begin)
  21193. (while (< (point) end)
  21194. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21195. (replace-match
  21196. "" nil nil nil
  21197. (if (= (line-end-position) (match-end 0)) 0 1)))
  21198. (forward-line)))
  21199. (let ((min-ind (point-max)))
  21200. ;; Find minimum indentation across all lines.
  21201. (save-excursion
  21202. (goto-char begin)
  21203. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21204. (setq min-ind 0)
  21205. (catch 'zerop
  21206. (while (< (point) end)
  21207. (unless (org-looking-at-p "[ \t]*$")
  21208. (let ((ind (org-get-indentation)))
  21209. (setq min-ind (min min-ind ind))
  21210. (when (zerop ind) (throw 'zerop t))))
  21211. (forward-line)))))
  21212. ;; Loop over all lines and add fixed-width markup everywhere
  21213. ;; but in fixed-width lines.
  21214. (save-excursion
  21215. (goto-char begin)
  21216. (while (< (point) end)
  21217. (cond
  21218. ((org-at-heading-p)
  21219. (insert ": ")
  21220. (forward-line)
  21221. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21222. (insert ":")
  21223. (forward-line)))
  21224. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21225. (let* ((element (org-element-at-point))
  21226. (element-end (org-element-property :end element)))
  21227. (if (eq (org-element-type element) 'fixed-width)
  21228. (progn (goto-char element-end)
  21229. (skip-chars-backward " \r\t\n")
  21230. (forward-line))
  21231. (let ((limit (min end element-end)))
  21232. (while (< (point) limit)
  21233. (org-move-to-column min-ind t)
  21234. (insert ": ")
  21235. (forward-line))))))
  21236. (t
  21237. (org-move-to-column min-ind t)
  21238. (insert ": ")
  21239. (forward-line)))))))
  21240. (set-marker end nil))))
  21241. ;;; Comments
  21242. ;; Org comments syntax is quite complex. It requires the entire line
  21243. ;; to be just a comment. Also, even with the right syntax at the
  21244. ;; beginning of line, some some elements (i.e. verse-block or
  21245. ;; example-block) don't accept comments. Usual Emacs comment commands
  21246. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21247. ;; Org still relies on `comment-dwim', but cannot trust
  21248. ;; `comment-only-p'. So, `comment-region-function' and
  21249. ;; `uncomment-region-function' both point
  21250. ;; to`org-comment-or-uncomment-region'. Eventually,
  21251. ;; `org-insert-comment' takes care of insertion of comments at the
  21252. ;; beginning of line.
  21253. ;; `org-setup-comments-handling' install comments related variables
  21254. ;; during `org-mode' initialization.
  21255. (defun org-setup-comments-handling ()
  21256. (interactive)
  21257. (org-set-local 'comment-use-syntax nil)
  21258. (org-set-local 'comment-start "# ")
  21259. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21260. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  21261. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  21262. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  21263. (defun org-insert-comment ()
  21264. "Insert an empty comment above current line.
  21265. If the line is empty, insert comment at its beginning. When
  21266. point is within a source block, comment according to the related
  21267. major mode."
  21268. (if (let ((element (org-element-at-point)))
  21269. (and (eq (org-element-type element) 'src-block)
  21270. (< (save-excursion
  21271. (goto-char (org-element-property :post-affiliated element))
  21272. (line-end-position))
  21273. (point))
  21274. (> (save-excursion
  21275. (goto-char (org-element-property :end element))
  21276. (skip-chars-backward " \r\t\n")
  21277. (line-beginning-position))
  21278. (point))))
  21279. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21280. (beginning-of-line)
  21281. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21282. (open-line 1))
  21283. (org-indent-line)
  21284. (insert "# ")))
  21285. (defvar comment-empty-lines) ; From newcomment.el.
  21286. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  21287. "Comment or uncomment each non-blank line in the region.
  21288. Uncomment each non-blank line between BEG and END if it only
  21289. contains commented lines. Otherwise, comment them. If region is
  21290. strictly within a source block, use appropriate comment syntax."
  21291. (if (let ((element (org-element-at-point)))
  21292. (and (eq (org-element-type element) 'src-block)
  21293. (< (save-excursion
  21294. (goto-char (org-element-property :post-affiliated element))
  21295. (line-end-position))
  21296. beg)
  21297. (>= (save-excursion
  21298. (goto-char (org-element-property :end element))
  21299. (skip-chars-backward " \r\t\n")
  21300. (line-beginning-position))
  21301. end)))
  21302. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21303. (save-restriction
  21304. ;; Restrict region
  21305. (narrow-to-region (save-excursion (goto-char beg)
  21306. (skip-chars-forward " \r\t\n" end)
  21307. (line-beginning-position))
  21308. (save-excursion (goto-char end)
  21309. (skip-chars-backward " \r\t\n" beg)
  21310. (line-end-position)))
  21311. (let ((uncommentp
  21312. ;; UNCOMMENTP is non-nil when every non blank line between
  21313. ;; BEG and END is a comment.
  21314. (save-excursion
  21315. (goto-char (point-min))
  21316. (while (and (not (eobp))
  21317. (let ((element (org-element-at-point)))
  21318. (and (eq (org-element-type element) 'comment)
  21319. (goto-char (min (point-max)
  21320. (org-element-property
  21321. :end element)))))))
  21322. (eobp))))
  21323. (if uncommentp
  21324. ;; Only blank lines and comments in region: uncomment it.
  21325. (save-excursion
  21326. (goto-char (point-min))
  21327. (while (not (eobp))
  21328. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21329. (replace-match "" nil nil nil 1))
  21330. (forward-line)))
  21331. ;; Comment each line in region.
  21332. (let ((min-indent (point-max)))
  21333. ;; First find the minimum indentation across all lines.
  21334. (save-excursion
  21335. (goto-char (point-min))
  21336. (while (and (not (eobp)) (not (zerop min-indent)))
  21337. (unless (looking-at "[ \t]*$")
  21338. (setq min-indent (min min-indent (current-indentation))))
  21339. (forward-line)))
  21340. ;; Then loop over all lines.
  21341. (save-excursion
  21342. (goto-char (point-min))
  21343. (while (not (eobp))
  21344. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21345. ;; Don't get fooled by invisible text (e.g. link path)
  21346. ;; when moving to column MIN-INDENT.
  21347. (let ((buffer-invisibility-spec nil))
  21348. (org-move-to-column min-indent t))
  21349. (insert comment-start))
  21350. (forward-line)))))))))
  21351. (defun org-comment-dwim (arg)
  21352. "Call `comment-dwim' within a source edit buffer if needed."
  21353. (interactive "*P")
  21354. (if (org-in-src-block-p)
  21355. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21356. (call-interactively 'comment-dwim)))
  21357. ;;; Planning
  21358. ;; This section contains tools to operate on timestamp objects, as
  21359. ;; returned by, e.g. `org-element-context'.
  21360. (defun org-timestamp-has-time-p (timestamp)
  21361. "Non-nil when TIMESTAMP has a time specified."
  21362. (org-element-property :hour-start timestamp))
  21363. (defun org-timestamp-format (timestamp format &optional end utc)
  21364. "Format a TIMESTAMP element into a string.
  21365. FORMAT is a format specifier to be passed to
  21366. `format-time-string'.
  21367. When optional argument END is non-nil, use end of date-range or
  21368. time-range, if possible.
  21369. When optional argument UTC is non-nil, time will be expressed as
  21370. Universal Time."
  21371. (format-time-string
  21372. format
  21373. (apply 'encode-time
  21374. (cons 0
  21375. (mapcar
  21376. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21377. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21378. '(:minute-start :hour-start :day-start :month-start
  21379. :year-start)))))
  21380. utc))
  21381. (defun org-timestamp-split-range (timestamp &optional end)
  21382. "Extract a timestamp object from a date or time range.
  21383. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  21384. the end of the range. Otherwise, extract its start.
  21385. Return a new timestamp object sharing the same parent as
  21386. TIMESTAMP."
  21387. (let ((type (org-element-property :type timestamp)))
  21388. (if (memq type '(active inactive diary)) timestamp
  21389. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  21390. ;; Set new type.
  21391. (org-element-put-property
  21392. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21393. ;; Copy start properties over end properties if END is
  21394. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21395. (let ((p-alist '((:minute-start . :minute-end)
  21396. (:hour-start . :hour-end)
  21397. (:day-start . :day-end)
  21398. (:month-start . :month-end)
  21399. (:year-start . :year-end))))
  21400. (dolist (p-cell p-alist)
  21401. (org-element-put-property
  21402. split-ts
  21403. (funcall (if end 'car 'cdr) p-cell)
  21404. (org-element-property
  21405. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  21406. ;; Eventually refresh `:raw-value'.
  21407. (org-element-put-property split-ts :raw-value nil)
  21408. (org-element-put-property
  21409. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21410. (defun org-timestamp-translate (timestamp &optional boundary)
  21411. "Apply `org-translate-time' on a TIMESTAMP object.
  21412. When optional argument BOUNDARY is non-nil, it is either the
  21413. symbol `start' or `end'. In this case, only translate the
  21414. starting or ending part of TIMESTAMP if it is a date or time
  21415. range. Otherwise, translate both parts."
  21416. (if (and (not boundary)
  21417. (memq (org-element-property :type timestamp)
  21418. '(active-range inactive-range)))
  21419. (concat
  21420. (org-translate-time
  21421. (org-element-property :raw-value
  21422. (org-timestamp-split-range timestamp)))
  21423. "--"
  21424. (org-translate-time
  21425. (org-element-property :raw-value
  21426. (org-timestamp-split-range timestamp t))))
  21427. (org-translate-time
  21428. (org-element-property
  21429. :raw-value
  21430. (if (not boundary) timestamp
  21431. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  21432. ;;; Other stuff.
  21433. (defun org-reftex-citation ()
  21434. "Use reftex-citation to insert a citation into the buffer.
  21435. This looks for a line like
  21436. #+BIBLIOGRAPHY: foo plain option:-d
  21437. and derives from it that foo.bib is the bibliography file relevant
  21438. for this document. It then installs the necessary environment for RefTeX
  21439. to work in this buffer and calls `reftex-citation' to insert a citation
  21440. into the buffer.
  21441. Export of such citations to both LaTeX and HTML is handled by the contributed
  21442. package ox-bibtex by Taru Karttunen."
  21443. (interactive)
  21444. (let ((reftex-docstruct-symbol 'rds)
  21445. (reftex-cite-format "\\cite{%l}")
  21446. rds bib)
  21447. (save-excursion
  21448. (save-restriction
  21449. (widen)
  21450. (let ((case-fold-search t)
  21451. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21452. (if (not (save-excursion
  21453. (or (re-search-forward re nil t)
  21454. (re-search-backward re nil t))))
  21455. (user-error "No bibliography defined in file")
  21456. (setq bib (concat (match-string 1) ".bib")
  21457. rds (list (list 'bib bib)))))))
  21458. (call-interactively 'reftex-citation)))
  21459. ;;;; Functions extending outline functionality
  21460. (defun org-beginning-of-line (&optional arg)
  21461. "Go to the beginning of the current line. If that is invisible, continue
  21462. to a visible line beginning. This makes the function of C-a more intuitive.
  21463. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21464. first attempt, and only move to after the tags when the cursor is already
  21465. beyond the end of the headline."
  21466. (interactive "P")
  21467. (let ((pos (point))
  21468. (special (if (consp org-special-ctrl-a/e)
  21469. (car org-special-ctrl-a/e)
  21470. org-special-ctrl-a/e))
  21471. deactivate-mark refpos)
  21472. (if (org-bound-and-true-p visual-line-mode)
  21473. (beginning-of-visual-line 1)
  21474. (beginning-of-line 1))
  21475. (if (and arg (fboundp 'move-beginning-of-line))
  21476. (call-interactively 'move-beginning-of-line)
  21477. (if (bobp)
  21478. nil
  21479. (backward-char 1)
  21480. (if (org-truely-invisible-p)
  21481. (while (and (not (bobp)) (org-truely-invisible-p))
  21482. (backward-char 1)
  21483. (beginning-of-line 1))
  21484. (forward-char 1))))
  21485. (when special
  21486. (cond
  21487. ((and (looking-at org-complex-heading-regexp)
  21488. (= (char-after (match-end 1)) ?\ ))
  21489. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21490. (point-at-eol)))
  21491. (goto-char
  21492. (if (eq special t)
  21493. (cond ((> pos refpos) refpos)
  21494. ((= pos (point)) refpos)
  21495. (t (point)))
  21496. (cond ((> pos (point)) (point))
  21497. ((not (eq last-command this-command)) (point))
  21498. (t refpos)))))
  21499. ((org-at-item-p)
  21500. ;; Being at an item and not looking at an the item means point
  21501. ;; was previously moved to beginning of a visual line, which
  21502. ;; doesn't contain the item. Therefore, do nothing special,
  21503. ;; just stay here.
  21504. (when (looking-at org-list-full-item-re)
  21505. ;; Set special position at first white space character after
  21506. ;; bullet, and check-box, if any.
  21507. (let ((after-bullet
  21508. (let ((box (match-end 3)))
  21509. (if (not box) (match-end 1)
  21510. (let ((after (char-after box)))
  21511. (if (and after (= after ? )) (1+ box) box))))))
  21512. ;; Special case: Move point to special position when
  21513. ;; currently after it or at beginning of line.
  21514. (if (eq special t)
  21515. (when (or (> pos after-bullet) (= (point) pos))
  21516. (goto-char after-bullet))
  21517. ;; Reversed case: Move point to special position when
  21518. ;; point was already at beginning of line and command is
  21519. ;; repeated.
  21520. (when (and (= (point) pos) (eq last-command this-command))
  21521. (goto-char after-bullet))))))))
  21522. (org-no-warnings
  21523. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21524. (setq disable-point-adjustment
  21525. (or (not (invisible-p (point)))
  21526. (not (invisible-p (max (point-min) (1- (point))))))))
  21527. (defun org-end-of-line (&optional arg)
  21528. "Go to the end of the line.
  21529. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21530. tags on the first attempt, and only move to after the tags when
  21531. the cursor is already beyond the end of the headline."
  21532. (interactive "P")
  21533. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21534. org-special-ctrl-a/e))
  21535. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21536. 'end-of-visual-line)
  21537. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21538. (t 'end-of-line)))
  21539. deactivate-mark)
  21540. (if (or (not special) arg) (call-interactively move-fun)
  21541. (let* ((element (save-excursion (beginning-of-line)
  21542. (org-element-at-point)))
  21543. (type (org-element-type element)))
  21544. (cond
  21545. ((memq type '(headline inlinetask))
  21546. (let ((pos (point)))
  21547. (beginning-of-line 1)
  21548. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21549. (if (eq special t)
  21550. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21551. (goto-char (match-beginning 1))
  21552. (goto-char (match-end 0)))
  21553. (if (or (< pos (match-end 0))
  21554. (not (eq this-command last-command)))
  21555. (goto-char (match-end 0))
  21556. (goto-char (match-beginning 1))))
  21557. (call-interactively move-fun))))
  21558. ((outline-invisible-p (line-end-position))
  21559. ;; If element is hidden, `move-end-of-line' would put point
  21560. ;; after it. Use `end-of-line' to stay on current line.
  21561. (call-interactively 'end-of-line))
  21562. (t (call-interactively move-fun)))))
  21563. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21564. (setq disable-point-adjustment
  21565. (or (not (invisible-p (point)))
  21566. (not (invisible-p (max (point-min) (1- (point))))))))
  21567. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21568. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21569. (defun org-backward-sentence (&optional arg)
  21570. "Go to beginning of sentence, or beginning of table field.
  21571. This will call `backward-sentence' or `org-table-beginning-of-field',
  21572. depending on context."
  21573. (interactive "P")
  21574. (cond
  21575. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  21576. (t (call-interactively 'backward-sentence))))
  21577. (defun org-forward-sentence (&optional arg)
  21578. "Go to end of sentence, or end of table field.
  21579. This will call `forward-sentence' or `org-table-end-of-field',
  21580. depending on context."
  21581. (interactive "P")
  21582. (cond
  21583. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  21584. (t (call-interactively 'forward-sentence))))
  21585. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21586. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21587. (defun org-kill-line (&optional arg)
  21588. "Kill line, to tags or end of line."
  21589. (interactive "P")
  21590. (cond
  21591. ((or (not org-special-ctrl-k)
  21592. (bolp)
  21593. (not (org-at-heading-p)))
  21594. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21595. org-ctrl-k-protect-subtree)
  21596. (if (or (eq org-ctrl-k-protect-subtree 'error)
  21597. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  21598. (user-error "C-k aborted as it would kill a hidden subtree")))
  21599. (call-interactively
  21600. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  21601. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  21602. (kill-region (point) (match-beginning 1))
  21603. (org-set-tags nil t))
  21604. (t (kill-region (point) (point-at-eol)))))
  21605. (define-key org-mode-map "\C-k" 'org-kill-line)
  21606. (defun org-yank (&optional arg)
  21607. "Yank. If the kill is a subtree, treat it specially.
  21608. This command will look at the current kill and check if is a single
  21609. subtree, or a series of subtrees[1]. If it passes the test, and if the
  21610. cursor is at the beginning of a line or after the stars of a currently
  21611. empty headline, then the yank is handled specially. How exactly depends
  21612. on the value of the following variables, both set by default.
  21613. `org-yank-folded-subtrees'
  21614. When set, the subtree(s) will be folded after insertion, but only
  21615. if doing so would now swallow text after the yanked text.
  21616. `org-yank-adjusted-subtrees'
  21617. When set, the subtree will be promoted or demoted in order to
  21618. fit into the local outline tree structure, which means that the
  21619. level will be adjusted so that it becomes the smaller one of the
  21620. two *visible* surrounding headings.
  21621. Any prefix to this command will cause `yank' to be called directly with
  21622. no special treatment. In particular, a simple \\[universal-argument] prefix \
  21623. will just
  21624. plainly yank the text as it is.
  21625. \[1] The test checks if the first non-white line is a heading
  21626. and if there are no other headings with fewer stars."
  21627. (interactive "P")
  21628. (org-yank-generic 'yank arg))
  21629. (defun org-yank-generic (command arg)
  21630. "Perform some yank-like command.
  21631. This function implements the behavior described in the `org-yank'
  21632. documentation. However, it has been generalized to work for any
  21633. interactive command with similar behavior."
  21634. ;; pretend to be command COMMAND
  21635. (setq this-command command)
  21636. (if arg
  21637. (call-interactively command)
  21638. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  21639. (and (org-kill-is-subtree-p)
  21640. (or (bolp)
  21641. (and (looking-at "[ \t]*$")
  21642. (string-match
  21643. "\\`\\*+\\'"
  21644. (buffer-substring (point-at-bol) (point)))))))
  21645. swallowp)
  21646. (cond
  21647. ((and subtreep org-yank-folded-subtrees)
  21648. (let ((beg (point))
  21649. end)
  21650. (if (and subtreep org-yank-adjusted-subtrees)
  21651. (org-paste-subtree nil nil 'for-yank)
  21652. (call-interactively command))
  21653. (setq end (point))
  21654. (goto-char beg)
  21655. (when (and (bolp) subtreep
  21656. (not (setq swallowp
  21657. (org-yank-folding-would-swallow-text beg end))))
  21658. (org-with-limited-levels
  21659. (or (looking-at org-outline-regexp)
  21660. (re-search-forward org-outline-regexp-bol end t))
  21661. (while (and (< (point) end) (looking-at org-outline-regexp))
  21662. (hide-subtree)
  21663. (org-cycle-show-empty-lines 'folded)
  21664. (condition-case nil
  21665. (outline-forward-same-level 1)
  21666. (error (goto-char end))))))
  21667. (when swallowp
  21668. (message
  21669. "Inserted text not folded because that would swallow text"))
  21670. (goto-char end)
  21671. (skip-chars-forward " \t\n\r")
  21672. (beginning-of-line 1)
  21673. (push-mark beg 'nomsg)))
  21674. ((and subtreep org-yank-adjusted-subtrees)
  21675. (let ((beg (point-at-bol)))
  21676. (org-paste-subtree nil nil 'for-yank)
  21677. (push-mark beg 'nomsg)))
  21678. (t
  21679. (call-interactively command))))))
  21680. (defun org-yank-folding-would-swallow-text (beg end)
  21681. "Would hide-subtree at BEG swallow any text after END?"
  21682. (let (level)
  21683. (org-with-limited-levels
  21684. (save-excursion
  21685. (goto-char beg)
  21686. (when (or (looking-at org-outline-regexp)
  21687. (re-search-forward org-outline-regexp-bol end t))
  21688. (setq level (org-outline-level)))
  21689. (goto-char end)
  21690. (skip-chars-forward " \t\r\n\v\f")
  21691. (if (or (eobp)
  21692. (and (bolp) (looking-at org-outline-regexp)
  21693. (<= (org-outline-level) level)))
  21694. nil ; Nothing would be swallowed
  21695. t))))) ; something would swallow
  21696. (define-key org-mode-map "\C-y" 'org-yank)
  21697. (defun org-truely-invisible-p ()
  21698. "Check if point is at a character currently not visible.
  21699. This version does not only check the character property, but also
  21700. `visible-mode'."
  21701. ;; Early versions of noutline don't have `outline-invisible-p'.
  21702. (if (org-bound-and-true-p visible-mode)
  21703. nil
  21704. (outline-invisible-p)))
  21705. (defun org-invisible-p2 ()
  21706. "Check if point is at a character currently not visible."
  21707. (save-excursion
  21708. (if (and (eolp) (not (bobp))) (backward-char 1))
  21709. ;; Early versions of noutline don't have `outline-invisible-p'.
  21710. (outline-invisible-p)))
  21711. (defun org-back-to-heading (&optional invisible-ok)
  21712. "Call `outline-back-to-heading', but provide a better error message."
  21713. (condition-case nil
  21714. (outline-back-to-heading invisible-ok)
  21715. (error (error "Before first headline at position %d in buffer %s"
  21716. (point) (current-buffer)))))
  21717. (defun org-before-first-heading-p ()
  21718. "Before first heading?"
  21719. (save-excursion
  21720. (end-of-line)
  21721. (null (re-search-backward org-outline-regexp-bol nil t))))
  21722. (defun org-at-heading-p (&optional ignored)
  21723. (outline-on-heading-p t))
  21724. ;; Compatibility alias with Org versions < 7.8.03
  21725. (defalias 'org-on-heading-p 'org-at-heading-p)
  21726. (defun org-in-commented-heading-p (&optional no-inheritance)
  21727. "Non-nil if point is under a commented heading.
  21728. This function also checks ancestors of the current headline,
  21729. unless optional argument NO-INHERITANCE is non-nil."
  21730. (cond
  21731. ((org-before-first-heading-p) nil)
  21732. ((let ((headline (nth 4 (org-heading-components))))
  21733. (and headline
  21734. (let ((case-fold-search nil))
  21735. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  21736. headline)))))
  21737. (no-inheritance nil)
  21738. (t
  21739. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  21740. (defun org-at-comment-p nil
  21741. "Is cursor in a commented line?"
  21742. (save-excursion
  21743. (save-match-data
  21744. (beginning-of-line)
  21745. (looking-at "^[ \t]*# "))))
  21746. (defun org-at-drawer-p nil
  21747. "Is cursor at a drawer keyword?"
  21748. (save-excursion
  21749. (move-beginning-of-line 1)
  21750. (looking-at org-drawer-regexp)))
  21751. (defun org-at-block-p nil
  21752. "Is cursor at a block keyword?"
  21753. (save-excursion
  21754. (move-beginning-of-line 1)
  21755. (looking-at org-block-regexp)))
  21756. (defun org-point-at-end-of-empty-headline ()
  21757. "If point is at the end of an empty headline, return t, else nil.
  21758. If the heading only contains a TODO keyword, it is still still considered
  21759. empty."
  21760. (and (looking-at "[ \t]*$")
  21761. (when org-todo-line-regexp
  21762. (save-excursion
  21763. (beginning-of-line 1)
  21764. (let ((case-fold-search nil))
  21765. (looking-at org-todo-line-regexp)
  21766. (string= (match-string 3) ""))))))
  21767. (defun org-at-heading-or-item-p ()
  21768. (or (org-at-heading-p) (org-at-item-p)))
  21769. (defun org-at-target-p ()
  21770. (or (org-in-regexp org-radio-target-regexp)
  21771. (org-in-regexp org-target-regexp)))
  21772. ;; Compatibility alias with Org versions < 7.8.03
  21773. (defalias 'org-on-target-p 'org-at-target-p)
  21774. (defun org-up-heading-all (arg)
  21775. "Move to the heading line of which the present line is a subheading.
  21776. This function considers both visible and invisible heading lines.
  21777. With argument, move up ARG levels."
  21778. (if (fboundp 'outline-up-heading-all)
  21779. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21780. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21781. (defun org-up-heading-safe ()
  21782. "Move to the heading line of which the present line is a subheading.
  21783. This version will not throw an error. It will return the level of the
  21784. headline found, or nil if no higher level is found.
  21785. Also, this function will be a lot faster than `outline-up-heading',
  21786. because it relies on stars being the outline starters. This can really
  21787. make a significant difference in outlines with very many siblings."
  21788. (when (ignore-errors (org-back-to-heading t))
  21789. (let ((level-up (1- (funcall outline-level))))
  21790. (and (> level-up 0)
  21791. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  21792. (funcall outline-level)))))
  21793. (defun org-first-sibling-p ()
  21794. "Is this heading the first child of its parents?"
  21795. (interactive)
  21796. (let ((re org-outline-regexp-bol)
  21797. level l)
  21798. (unless (org-at-heading-p t)
  21799. (user-error "Not at a heading"))
  21800. (setq level (funcall outline-level))
  21801. (save-excursion
  21802. (if (not (re-search-backward re nil t))
  21803. t
  21804. (setq l (funcall outline-level))
  21805. (< l level)))))
  21806. (defun org-goto-sibling (&optional previous)
  21807. "Goto the next sibling, even if it is invisible.
  21808. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21809. when a sibling was found. When none is found, return nil and don't
  21810. move point."
  21811. (let ((fun (if previous 're-search-backward 're-search-forward))
  21812. (pos (point))
  21813. (re org-outline-regexp-bol)
  21814. level l)
  21815. (when (ignore-errors (org-back-to-heading t))
  21816. (setq level (funcall outline-level))
  21817. (catch 'exit
  21818. (or previous (forward-char 1))
  21819. (while (funcall fun re nil t)
  21820. (setq l (funcall outline-level))
  21821. (when (< l level) (goto-char pos) (throw 'exit nil))
  21822. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21823. (goto-char pos)
  21824. nil))))
  21825. (defun org-show-siblings ()
  21826. "Show all siblings of the current headline."
  21827. (save-excursion
  21828. (while (org-goto-sibling) (org-flag-heading nil)))
  21829. (save-excursion
  21830. (while (org-goto-sibling 'previous)
  21831. (org-flag-heading nil))))
  21832. (defun org-goto-first-child ()
  21833. "Goto the first child, even if it is invisible.
  21834. Return t when a child was found. Otherwise don't move point and
  21835. return nil."
  21836. (let (level (pos (point)) (re org-outline-regexp-bol))
  21837. (when (ignore-errors (org-back-to-heading t))
  21838. (setq level (outline-level))
  21839. (forward-char 1)
  21840. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21841. (progn (goto-char (match-beginning 0)) t)
  21842. (goto-char pos) nil))))
  21843. (defun org-show-hidden-entry ()
  21844. "Show an entry where even the heading is hidden."
  21845. (save-excursion
  21846. (org-show-entry)))
  21847. (defun org-flag-heading (flag &optional entry)
  21848. "Flag the current heading. FLAG non-nil means make invisible.
  21849. When ENTRY is non-nil, show the entire entry."
  21850. (save-excursion
  21851. (org-back-to-heading t)
  21852. ;; Check if we should show the entire entry
  21853. (if entry
  21854. (progn
  21855. (org-show-entry)
  21856. (save-excursion
  21857. (and (outline-next-heading)
  21858. (org-flag-heading nil))))
  21859. (outline-flag-region (max (point-min) (1- (point)))
  21860. (save-excursion (outline-end-of-heading) (point))
  21861. flag))))
  21862. (defun org-get-next-sibling ()
  21863. "Move to next heading of the same level, and return point.
  21864. If there is no such heading, return nil.
  21865. This is like outline-next-sibling, but invisible headings are ok."
  21866. (let ((level (funcall outline-level)))
  21867. (outline-next-heading)
  21868. (while (and (not (eobp)) (> (funcall outline-level) level))
  21869. (outline-next-heading))
  21870. (if (or (eobp) (< (funcall outline-level) level))
  21871. nil
  21872. (point))))
  21873. (defun org-get-last-sibling ()
  21874. "Move to previous heading of the same level, and return point.
  21875. If there is no such heading, return nil."
  21876. (let ((opoint (point))
  21877. (level (funcall outline-level)))
  21878. (outline-previous-heading)
  21879. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21880. (while (and (> (funcall outline-level) level)
  21881. (not (bobp)))
  21882. (outline-previous-heading))
  21883. (if (< (funcall outline-level) level)
  21884. nil
  21885. (point)))))
  21886. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21887. "Goto to the end of a subtree."
  21888. ;; This contains an exact copy of the original function, but it uses
  21889. ;; `org-back-to-heading', to make it work also in invisible
  21890. ;; trees. And is uses an invisible-ok argument.
  21891. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21892. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21893. ;; with many children and grandchildren etc, this can be much faster
  21894. ;; than the outline version.
  21895. (org-back-to-heading invisible-ok)
  21896. (let ((first t)
  21897. (level (funcall outline-level)))
  21898. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21899. ;; A true heading (not a plain list item), in Org-mode
  21900. ;; This means we can easily find the end by looking
  21901. ;; only for the right number of stars. Using a regexp to do
  21902. ;; this is so much faster than using a Lisp loop.
  21903. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21904. (forward-char 1)
  21905. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21906. ;; something else, do it the slow way
  21907. (while (and (not (eobp))
  21908. (or first (> (funcall outline-level) level)))
  21909. (setq first nil)
  21910. (outline-next-heading)))
  21911. (unless to-heading
  21912. (if (memq (preceding-char) '(?\n ?\^M))
  21913. (progn
  21914. ;; Go to end of line before heading
  21915. (forward-char -1)
  21916. (if (memq (preceding-char) '(?\n ?\^M))
  21917. ;; leave blank line before heading
  21918. (forward-char -1))))))
  21919. (point))
  21920. (defun org-end-of-meta-data-and-drawers ()
  21921. "Jump to the first text after meta data and drawers in the current entry.
  21922. This will move over empty lines, lines with planning time stamps,
  21923. clocking lines, and drawers."
  21924. (org-back-to-heading t)
  21925. (let ((end (save-excursion (outline-next-heading) (point)))
  21926. (re (concat "\\(" org-drawer-regexp "\\)"
  21927. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21928. (forward-line 1)
  21929. (while (re-search-forward re end t)
  21930. (if (not (match-end 1))
  21931. ;; empty or planning line
  21932. (forward-line 1)
  21933. ;; a drawer, find the end
  21934. (re-search-forward "^[ \t]*:END:" end 'move)
  21935. (forward-line 1)))
  21936. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21937. (point)))
  21938. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21939. "Move forward to the ARG'th subheading at same level as this one.
  21940. Stop at the first and last subheadings of a superior heading.
  21941. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21942. non-nil it will also look at invisible ones."
  21943. (interactive "p")
  21944. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21945. (if (and arg (< arg 0))
  21946. (goto-char (point-min))
  21947. (outline-next-heading))
  21948. (org-at-heading-p)
  21949. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21950. (f (if (and arg (< arg 0))
  21951. 're-search-backward
  21952. 're-search-forward))
  21953. (count (if arg (abs arg) 1))
  21954. (result (point)))
  21955. (while (and (prog1 (> count 0)
  21956. (forward-char (if (and arg (< arg 0)) -1 1)))
  21957. (funcall f org-outline-regexp-bol nil 'move))
  21958. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21959. (cond ((< l level) (setq count 0))
  21960. ((and (= l level)
  21961. (or invisible-ok
  21962. (progn
  21963. (goto-char (line-beginning-position))
  21964. (not (outline-invisible-p)))))
  21965. (setq count (1- count))
  21966. (when (eq l level)
  21967. (setq result (point)))))))
  21968. (goto-char result))
  21969. (beginning-of-line 1)))
  21970. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21971. "Move backward to the ARG'th subheading at same level as this one.
  21972. Stop at the first and last subheadings of a superior heading."
  21973. (interactive "p")
  21974. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21975. (defun org-next-block (arg &optional backward block-regexp)
  21976. "Jump to the next block.
  21977. With a prefix argument ARG, jump forward ARG many source blocks.
  21978. When BACKWARD is non-nil, jump to the previous block.
  21979. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21980. (interactive "p")
  21981. (let ((re (or block-regexp org-block-regexp))
  21982. (re-search-fn (or (and backward 're-search-backward)
  21983. 're-search-forward)))
  21984. (if (looking-at re) (forward-char 1))
  21985. (condition-case nil
  21986. (funcall re-search-fn re nil nil arg)
  21987. (error (user-error "No %s code blocks"
  21988. (if backward "previous" "further" ))))
  21989. (goto-char (match-beginning 0)) (org-show-context)))
  21990. (defun org-previous-block (arg &optional block-regexp)
  21991. "Jump to the previous block.
  21992. With a prefix argument ARG, jump backward ARG many source blocks.
  21993. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21994. (interactive "p")
  21995. (org-next-block arg t block-regexp))
  21996. (defun org-forward-paragraph ()
  21997. "Move forward to beginning of next paragraph or equivalent.
  21998. The function moves point to the beginning of the next visible
  21999. structural element, which can be a paragraph, a table, a list
  22000. item, etc. It also provides some special moves for convenience:
  22001. - On an affiliated keyword, jump to the beginning of the
  22002. relative element.
  22003. - On an item or a footnote definition, move to the second
  22004. element inside, if any.
  22005. - On a table or a property drawer, jump after it.
  22006. - On a verse or source block, stop after blank lines."
  22007. (interactive)
  22008. (when (eobp) (user-error "Cannot move further down"))
  22009. (let* ((deactivate-mark nil)
  22010. (element (org-element-at-point))
  22011. (type (org-element-type element))
  22012. (post-affiliated (org-element-property :post-affiliated element))
  22013. (contents-begin (org-element-property :contents-begin element))
  22014. (contents-end (org-element-property :contents-end element))
  22015. (end (let ((end (org-element-property :end element)) (parent element))
  22016. (while (and (setq parent (org-element-property :parent parent))
  22017. (= (org-element-property :contents-end parent) end))
  22018. (setq end (org-element-property :end parent)))
  22019. end)))
  22020. (cond ((not element)
  22021. (skip-chars-forward " \r\t\n")
  22022. (or (eobp) (beginning-of-line)))
  22023. ;; On affiliated keywords, move to element's beginning.
  22024. ((< (point) post-affiliated)
  22025. (goto-char post-affiliated))
  22026. ;; At a table row, move to the end of the table. Similarly,
  22027. ;; at a node property, move to the end of the property
  22028. ;; drawer.
  22029. ((memq type '(node-property table-row))
  22030. (goto-char (org-element-property
  22031. :end (org-element-property :parent element))))
  22032. ((memq type '(property-drawer table)) (goto-char end))
  22033. ;; Consider blank lines as separators in verse and source
  22034. ;; blocks to ease editing.
  22035. ((memq type '(src-block verse-block))
  22036. (when (eq type 'src-block)
  22037. (setq contents-end
  22038. (save-excursion (goto-char end)
  22039. (skip-chars-backward " \r\t\n")
  22040. (line-beginning-position))))
  22041. (beginning-of-line)
  22042. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22043. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22044. (goto-char end)
  22045. (skip-chars-forward " \r\t\n")
  22046. (if (= (point) contents-end) (goto-char end)
  22047. (beginning-of-line))))
  22048. ;; With no contents, just skip element.
  22049. ((not contents-begin) (goto-char end))
  22050. ;; If contents are invisible, skip the element altogether.
  22051. ((outline-invisible-p (line-end-position))
  22052. (case type
  22053. (headline
  22054. (org-with-limited-levels (outline-next-visible-heading 1)))
  22055. ;; At a plain list, make sure we move to the next item
  22056. ;; instead of skipping the whole list.
  22057. (plain-list (forward-char)
  22058. (org-forward-paragraph))
  22059. (otherwise (goto-char end))))
  22060. ((>= (point) contents-end) (goto-char end))
  22061. ((>= (point) contents-begin)
  22062. ;; This can only happen on paragraphs and plain lists.
  22063. (case type
  22064. (paragraph (goto-char end))
  22065. ;; At a plain list, try to move to second element in
  22066. ;; first item, if possible.
  22067. (plain-list (end-of-line)
  22068. (org-forward-paragraph))))
  22069. ;; When contents start on the middle of a line (e.g. in
  22070. ;; items and footnote definitions), try to reach first
  22071. ;; element starting after current line.
  22072. ((> (line-end-position) contents-begin)
  22073. (end-of-line)
  22074. (org-forward-paragraph))
  22075. (t (goto-char contents-begin)))))
  22076. (defun org-backward-paragraph ()
  22077. "Move backward to start of previous paragraph or equivalent.
  22078. The function moves point to the beginning of the current
  22079. structural element, which can be a paragraph, a table, a list
  22080. item, etc., or to the beginning of the previous visible one if
  22081. point is already there. It also provides some special moves for
  22082. convenience:
  22083. - On an affiliated keyword, jump to the first one.
  22084. - On a table or a property drawer, move to its beginning.
  22085. - On a verse or source block, stop before blank lines."
  22086. (interactive)
  22087. (when (bobp) (user-error "Cannot move further up"))
  22088. (let* ((deactivate-mark nil)
  22089. (element (org-element-at-point))
  22090. (type (org-element-type element))
  22091. (contents-begin (org-element-property :contents-begin element))
  22092. (contents-end (org-element-property :contents-end element))
  22093. (post-affiliated (org-element-property :post-affiliated element))
  22094. (begin (org-element-property :begin element)))
  22095. (cond
  22096. ((not element) (goto-char (point-min)))
  22097. ((= (point) begin)
  22098. (backward-char)
  22099. (org-backward-paragraph))
  22100. ((<= (point) post-affiliated) (goto-char begin))
  22101. ((memq type '(node-property table-row))
  22102. (goto-char (org-element-property
  22103. :post-affiliated (org-element-property :parent element))))
  22104. ((memq type '(property-drawer table)) (goto-char begin))
  22105. ((memq type '(src-block verse-block))
  22106. (when (eq type 'src-block)
  22107. (setq contents-begin
  22108. (save-excursion (goto-char begin) (forward-line) (point))))
  22109. (if (= (point) contents-begin) (goto-char post-affiliated)
  22110. ;; Inside a verse block, see blank lines as paragraph
  22111. ;; separators.
  22112. (let ((origin (point)))
  22113. (skip-chars-backward " \r\t\n" contents-begin)
  22114. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22115. (skip-chars-forward " \r\t\n" origin)
  22116. (if (= (point) origin) (goto-char contents-begin)
  22117. (beginning-of-line))))))
  22118. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22119. ((eq type 'paragraph)
  22120. (goto-char contents-begin)
  22121. ;; When at first paragraph in an item or a footnote definition,
  22122. ;; move directly to beginning of line.
  22123. (let ((parent-contents
  22124. (org-element-property
  22125. :contents-begin (org-element-property :parent element))))
  22126. (when (and parent-contents (= parent-contents contents-begin))
  22127. (beginning-of-line))))
  22128. ;; At the end of a greater element, move to the beginning of the
  22129. ;; last element within.
  22130. ((>= (point) contents-end)
  22131. (goto-char (1- contents-end))
  22132. (org-backward-paragraph))
  22133. (t (goto-char (or post-affiliated begin))))
  22134. ;; Ensure we never leave point invisible.
  22135. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22136. (defun org-forward-element ()
  22137. "Move forward by one element.
  22138. Move to the next element at the same level, when possible."
  22139. (interactive)
  22140. (cond ((eobp) (user-error "Cannot move further down"))
  22141. ((org-with-limited-levels (org-at-heading-p))
  22142. (let ((origin (point)))
  22143. (goto-char (org-end-of-subtree nil t))
  22144. (unless (org-with-limited-levels (org-at-heading-p))
  22145. (goto-char origin)
  22146. (user-error "Cannot move further down"))))
  22147. (t
  22148. (let* ((elem (org-element-at-point))
  22149. (end (org-element-property :end elem))
  22150. (parent (org-element-property :parent elem)))
  22151. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22152. (goto-char (org-element-property :end parent)))
  22153. ((integer-or-marker-p end) (goto-char end))
  22154. (t (message "No element at point")))))))
  22155. (defun org-backward-element ()
  22156. "Move backward by one element.
  22157. Move to the previous element at the same level, when possible."
  22158. (interactive)
  22159. (cond ((bobp) (user-error "Cannot move further up"))
  22160. ((org-with-limited-levels (org-at-heading-p))
  22161. ;; At a headline, move to the previous one, if any, or stay
  22162. ;; here.
  22163. (let ((origin (point)))
  22164. (org-with-limited-levels (org-backward-heading-same-level 1))
  22165. ;; When current headline has no sibling above, move to its
  22166. ;; parent.
  22167. (when (= (point) origin)
  22168. (or (org-with-limited-levels (org-up-heading-safe))
  22169. (progn (goto-char origin)
  22170. (user-error "Cannot move further up"))))))
  22171. (t
  22172. (let* ((elem (org-element-at-point))
  22173. (beg (org-element-property :begin elem)))
  22174. (cond
  22175. ;; Move to beginning of current element if point isn't
  22176. ;; there already.
  22177. ((null beg) (message "No element at point"))
  22178. ((/= (point) beg) (goto-char beg))
  22179. (t (goto-char beg)
  22180. (skip-chars-backward " \r\t\n")
  22181. (unless (bobp)
  22182. (let ((prev (org-element-at-point)))
  22183. (goto-char (org-element-property :begin prev))
  22184. (while (and (setq prev (org-element-property :parent prev))
  22185. (<= (org-element-property :end prev) beg))
  22186. (goto-char (org-element-property :begin prev)))))))))))
  22187. (defun org-up-element ()
  22188. "Move to upper element."
  22189. (interactive)
  22190. (if (org-with-limited-levels (org-at-heading-p))
  22191. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22192. (let* ((elem (org-element-at-point))
  22193. (parent (org-element-property :parent elem)))
  22194. (if parent (goto-char (org-element-property :begin parent))
  22195. (if (org-with-limited-levels (org-before-first-heading-p))
  22196. (user-error "No surrounding element")
  22197. (org-with-limited-levels (org-back-to-heading)))))))
  22198. (defvar org-element-greater-elements)
  22199. (defun org-down-element ()
  22200. "Move to inner element."
  22201. (interactive)
  22202. (let ((element (org-element-at-point)))
  22203. (cond
  22204. ((memq (org-element-type element) '(plain-list table))
  22205. (goto-char (org-element-property :contents-begin element))
  22206. (forward-char))
  22207. ((memq (org-element-type element) org-element-greater-elements)
  22208. ;; If contents are hidden, first disclose them.
  22209. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22210. (goto-char (or (org-element-property :contents-begin element)
  22211. (user-error "No content for this element"))))
  22212. (t (user-error "No inner element")))))
  22213. (defun org-drag-element-backward ()
  22214. "Move backward element at point."
  22215. (interactive)
  22216. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22217. (let* ((elem (org-element-at-point))
  22218. (prev-elem
  22219. (save-excursion
  22220. (goto-char (org-element-property :begin elem))
  22221. (skip-chars-backward " \r\t\n")
  22222. (unless (bobp)
  22223. (let* ((beg (org-element-property :begin elem))
  22224. (prev (org-element-at-point))
  22225. (up prev))
  22226. (while (and (setq up (org-element-property :parent up))
  22227. (<= (org-element-property :end up) beg))
  22228. (setq prev up))
  22229. prev)))))
  22230. ;; Error out if no previous element or previous element is
  22231. ;; a parent of the current one.
  22232. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22233. (user-error "Cannot drag element backward")
  22234. (let ((pos (point)))
  22235. (org-element-swap-A-B prev-elem elem)
  22236. (goto-char (+ (org-element-property :begin prev-elem)
  22237. (- pos (org-element-property :begin elem)))))))))
  22238. (defun org-drag-element-forward ()
  22239. "Move forward element at point."
  22240. (interactive)
  22241. (let* ((pos (point))
  22242. (elem (org-element-at-point)))
  22243. (when (= (point-max) (org-element-property :end elem))
  22244. (user-error "Cannot drag element forward"))
  22245. (goto-char (org-element-property :end elem))
  22246. (let ((next-elem (org-element-at-point)))
  22247. (when (or (org-element-nested-p elem next-elem)
  22248. (and (eq (org-element-type next-elem) 'headline)
  22249. (not (eq (org-element-type elem) 'headline))))
  22250. (goto-char pos)
  22251. (user-error "Cannot drag element forward"))
  22252. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22253. ;; body's length (without final blanks) and by the length of
  22254. ;; blanks between ELEM and NEXT-ELEM.
  22255. (let ((size-next (- (save-excursion
  22256. (goto-char (org-element-property :end next-elem))
  22257. (skip-chars-backward " \r\t\n")
  22258. (forward-line)
  22259. ;; Small correction if buffer doesn't end
  22260. ;; with a newline character.
  22261. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22262. (org-element-property :begin next-elem)))
  22263. (size-blank (- (org-element-property :end elem)
  22264. (save-excursion
  22265. (goto-char (org-element-property :end elem))
  22266. (skip-chars-backward " \r\t\n")
  22267. (forward-line)
  22268. (point)))))
  22269. (org-element-swap-A-B elem next-elem)
  22270. (goto-char (+ pos size-next size-blank))))))
  22271. (defun org-drag-line-forward (arg)
  22272. "Drag the line at point ARG lines forward."
  22273. (interactive "p")
  22274. (dotimes (n (abs arg))
  22275. (let ((c (current-column)))
  22276. (if (< 0 arg)
  22277. (progn
  22278. (beginning-of-line 2)
  22279. (transpose-lines 1)
  22280. (beginning-of-line 0))
  22281. (transpose-lines 1)
  22282. (beginning-of-line -1))
  22283. (org-move-to-column c))))
  22284. (defun org-drag-line-backward (arg)
  22285. "Drag the line at point ARG lines backward."
  22286. (interactive "p")
  22287. (org-drag-line-forward (- arg)))
  22288. (defun org-mark-element ()
  22289. "Put point at beginning of this element, mark at end.
  22290. Interactively, if this command is repeated or (in Transient Mark
  22291. mode) if the mark is active, it marks the next element after the
  22292. ones already marked."
  22293. (interactive)
  22294. (let (deactivate-mark)
  22295. (if (and (org-called-interactively-p 'any)
  22296. (or (and (eq last-command this-command) (mark t))
  22297. (and transient-mark-mode mark-active)))
  22298. (set-mark
  22299. (save-excursion
  22300. (goto-char (mark))
  22301. (goto-char (org-element-property :end (org-element-at-point)))))
  22302. (let ((element (org-element-at-point)))
  22303. (end-of-line)
  22304. (push-mark (org-element-property :end element) t t)
  22305. (goto-char (org-element-property :begin element))))))
  22306. (defun org-narrow-to-element ()
  22307. "Narrow buffer to current element."
  22308. (interactive)
  22309. (let ((elem (org-element-at-point)))
  22310. (cond
  22311. ((eq (car elem) 'headline)
  22312. (narrow-to-region
  22313. (org-element-property :begin elem)
  22314. (org-element-property :end elem)))
  22315. ((memq (car elem) org-element-greater-elements)
  22316. (narrow-to-region
  22317. (org-element-property :contents-begin elem)
  22318. (org-element-property :contents-end elem)))
  22319. (t
  22320. (narrow-to-region
  22321. (org-element-property :begin elem)
  22322. (org-element-property :end elem))))))
  22323. (defun org-transpose-element ()
  22324. "Transpose current and previous elements, keeping blank lines between.
  22325. Point is moved after both elements."
  22326. (interactive)
  22327. (org-skip-whitespace)
  22328. (let ((end (org-element-property :end (org-element-at-point))))
  22329. (org-drag-element-backward)
  22330. (goto-char end)))
  22331. (defun org-unindent-buffer ()
  22332. "Un-indent the visible part of the buffer.
  22333. Relative indentation (between items, inside blocks, etc.) isn't
  22334. modified."
  22335. (interactive)
  22336. (unless (eq major-mode 'org-mode)
  22337. (user-error "Cannot un-indent a buffer not in Org mode"))
  22338. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  22339. unindent-tree ; For byte-compiler.
  22340. (unindent-tree
  22341. (function
  22342. (lambda (contents)
  22343. (mapc
  22344. (lambda (element)
  22345. (if (memq (org-element-type element) '(headline section))
  22346. (funcall unindent-tree (org-element-contents element))
  22347. (save-excursion
  22348. (save-restriction
  22349. (narrow-to-region
  22350. (org-element-property :begin element)
  22351. (org-element-property :end element))
  22352. (org-do-remove-indentation)))))
  22353. (reverse contents))))))
  22354. (funcall unindent-tree (org-element-contents parse-tree))))
  22355. (defun org-show-subtree ()
  22356. "Show everything after this heading at deeper levels."
  22357. (interactive)
  22358. (outline-flag-region
  22359. (point)
  22360. (save-excursion
  22361. (org-end-of-subtree t t))
  22362. nil))
  22363. (defun org-show-entry ()
  22364. "Show the body directly following this heading.
  22365. Show the heading too, if it is currently invisible."
  22366. (interactive)
  22367. (save-excursion
  22368. (ignore-errors
  22369. (org-back-to-heading t)
  22370. (outline-flag-region
  22371. (max (point-min) (1- (point)))
  22372. (save-excursion
  22373. (if (re-search-forward
  22374. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22375. (match-beginning 1)
  22376. (point-max)))
  22377. nil)
  22378. (org-cycle-hide-drawers 'children))))
  22379. (defun org-make-options-regexp (kwds &optional extra)
  22380. "Make a regular expression for keyword lines."
  22381. (concat
  22382. "^[ \t]*#\\+\\("
  22383. (mapconcat 'regexp-quote kwds "\\|")
  22384. (if extra (concat "\\|" extra))
  22385. "\\):[ \t]*\\(.*\\)"))
  22386. ;; Make isearch reveal the necessary context
  22387. (defun org-isearch-end ()
  22388. "Reveal context after isearch exits."
  22389. (when isearch-success ; only if search was successful
  22390. (if (featurep 'xemacs)
  22391. ;; Under XEmacs, the hook is run in the correct place,
  22392. ;; we directly show the context.
  22393. (org-show-context 'isearch)
  22394. ;; In Emacs the hook runs *before* restoring the overlays.
  22395. ;; So we have to use a one-time post-command-hook to do this.
  22396. ;; (Emacs 22 has a special variable, see function `org-mode')
  22397. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22398. isearch-mode-end-hook-quit)
  22399. ;; Only when the isearch was not quitted.
  22400. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22401. 'append 'local)))))
  22402. (defun org-isearch-post-command ()
  22403. "Remove self from hook, and show context."
  22404. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22405. (org-show-context 'isearch))
  22406. ;;;; Integration with and fixes for other packages
  22407. ;;; Imenu support
  22408. (defvar org-imenu-markers nil
  22409. "All markers currently used by Imenu.")
  22410. (make-variable-buffer-local 'org-imenu-markers)
  22411. (defun org-imenu-new-marker (&optional pos)
  22412. "Return a new marker for use by Imenu, and remember the marker."
  22413. (let ((m (make-marker)))
  22414. (move-marker m (or pos (point)))
  22415. (push m org-imenu-markers)
  22416. m))
  22417. (defun org-imenu-get-tree ()
  22418. "Produce the index for Imenu."
  22419. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  22420. (setq org-imenu-markers nil)
  22421. (let* ((n org-imenu-depth)
  22422. (re (concat "^" (org-get-limited-outline-regexp)))
  22423. (subs (make-vector (1+ n) nil))
  22424. (last-level 0)
  22425. m level head0 head)
  22426. (save-excursion
  22427. (save-restriction
  22428. (widen)
  22429. (goto-char (point-max))
  22430. (while (re-search-backward re nil t)
  22431. (setq level (org-reduced-level (funcall outline-level)))
  22432. (when (and (<= level n)
  22433. (looking-at org-complex-heading-regexp)
  22434. (setq head0 (org-match-string-no-properties 4)))
  22435. (setq head (org-link-display-format head0)
  22436. m (org-imenu-new-marker))
  22437. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22438. (if (>= level last-level)
  22439. (push (cons head m) (aref subs level))
  22440. (push (cons head (aref subs (1+ level))) (aref subs level))
  22441. (loop for i from (1+ level) to n do (aset subs i nil)))
  22442. (setq last-level level)))))
  22443. (aref subs 1)))
  22444. (eval-after-load "imenu"
  22445. '(progn
  22446. (add-hook 'imenu-after-jump-hook
  22447. (lambda ()
  22448. (if (derived-mode-p 'org-mode)
  22449. (org-show-context 'org-goto))))))
  22450. (defun org-link-display-format (link)
  22451. "Replace a link with its the description.
  22452. If there is no description, use the link target."
  22453. (save-match-data
  22454. (if (string-match org-bracket-link-analytic-regexp link)
  22455. (replace-match (if (match-end 5)
  22456. (match-string 5 link)
  22457. (concat (match-string 1 link)
  22458. (match-string 3 link)))
  22459. nil t link)
  22460. link)))
  22461. (defun org-toggle-link-display ()
  22462. "Toggle the literal or descriptive display of links."
  22463. (interactive)
  22464. (if org-descriptive-links
  22465. (progn (org-remove-from-invisibility-spec '(org-link))
  22466. (org-restart-font-lock)
  22467. (setq org-descriptive-links nil))
  22468. (progn (add-to-invisibility-spec '(org-link))
  22469. (org-restart-font-lock)
  22470. (setq org-descriptive-links t))))
  22471. ;; Speedbar support
  22472. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22473. "Overlay marking the agenda restriction line in speedbar.")
  22474. (overlay-put org-speedbar-restriction-lock-overlay
  22475. 'face 'org-agenda-restriction-lock)
  22476. (overlay-put org-speedbar-restriction-lock-overlay
  22477. 'help-echo "Agendas are currently limited to this item.")
  22478. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  22479. (defun org-speedbar-set-agenda-restriction ()
  22480. "Restrict future agenda commands to the location at point in speedbar.
  22481. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  22482. (interactive)
  22483. (require 'org-agenda)
  22484. (let (p m tp np dir txt)
  22485. (cond
  22486. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22487. 'org-imenu t))
  22488. (setq m (get-text-property p 'org-imenu-marker))
  22489. (with-current-buffer (marker-buffer m)
  22490. (goto-char m)
  22491. (org-agenda-set-restriction-lock 'subtree)))
  22492. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22493. 'speedbar-function 'speedbar-find-file))
  22494. (setq tp (previous-single-property-change
  22495. (1+ p) 'speedbar-function)
  22496. np (next-single-property-change
  22497. tp 'speedbar-function)
  22498. dir (speedbar-line-directory)
  22499. txt (buffer-substring-no-properties (or tp (point-min))
  22500. (or np (point-max))))
  22501. (with-current-buffer (find-file-noselect
  22502. (let ((default-directory dir))
  22503. (expand-file-name txt)))
  22504. (unless (derived-mode-p 'org-mode)
  22505. (user-error "Cannot restrict to non-Org-mode file"))
  22506. (org-agenda-set-restriction-lock 'file)))
  22507. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  22508. (move-overlay org-speedbar-restriction-lock-overlay
  22509. (point-at-bol) (point-at-eol))
  22510. (setq current-prefix-arg nil)
  22511. (org-agenda-maybe-redo)))
  22512. (defvar speedbar-file-key-map)
  22513. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  22514. (eval-after-load "speedbar"
  22515. '(progn
  22516. (speedbar-add-supported-extension ".org")
  22517. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  22518. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  22519. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  22520. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  22521. (add-hook 'speedbar-visiting-tag-hook
  22522. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  22523. ;;; Fixes and Hacks for problems with other packages
  22524. (defun org-mode-flyspell-verify ()
  22525. "Function used for `flyspell-generic-check-word-predicate'."
  22526. (if (org-at-heading-p)
  22527. ;; At a headline or an inlinetask, check title only. This is
  22528. ;; faster than relying on `org-element-at-point'.
  22529. (and (save-excursion (beginning-of-line)
  22530. (and (let ((case-fold-search t))
  22531. (not (looking-at "\\*+ END[ \t]*$")))
  22532. (looking-at org-complex-heading-regexp)))
  22533. (match-beginning 4)
  22534. (>= (point) (match-beginning 4))
  22535. (or (not (match-beginning 5))
  22536. (< (point) (match-beginning 5))))
  22537. (let* ((element (org-element-at-point))
  22538. (post-affiliated (org-element-property :post-affiliated element))
  22539. (object-check
  22540. (function
  22541. ;; Non-nil if checks can be done for object at point.
  22542. (lambda ()
  22543. (let ((object (save-excursion
  22544. (when (org-looking-at-p "\\>") (backward-char))
  22545. (org-element-context element))))
  22546. (case (org-element-type object)
  22547. ;; Prevent checks in links due to keybinding conflict
  22548. ;; with Flyspell.
  22549. ((code entity export-snippet inline-babel-call
  22550. inline-src-block line-break latex-fragment link macro
  22551. statistics-cookie target timestamp verbatim)
  22552. nil)
  22553. (footnote-reference
  22554. ;; Only in inline footnotes, within the definition.
  22555. (and (eq (org-element-property :type object) 'inline)
  22556. (< (save-excursion
  22557. (goto-char (org-element-property :begin object))
  22558. (search-forward ":" nil t 2))
  22559. (point))))
  22560. (otherwise t)))))))
  22561. (cond
  22562. ;; Ignore checks in all affiliated keywords but captions.
  22563. ((< (point) post-affiliated)
  22564. (and (save-excursion
  22565. (beginning-of-line)
  22566. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  22567. (> (point) (match-end 0))
  22568. (funcall object-check)))
  22569. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  22570. ((and org-log-into-drawer
  22571. (let ((log (or (org-string-nw-p org-log-into-drawer) "LOGBOOK"))
  22572. (parent element))
  22573. (while (and parent (not (eq (org-element-type parent) 'drawer)))
  22574. (setq parent (org-element-property :parent parent)))
  22575. (and parent
  22576. (eq (compare-strings
  22577. log nil nil
  22578. (org-element-property :drawer-name parent) nil nil t)
  22579. t))))
  22580. nil)
  22581. (t
  22582. (case (org-element-type element)
  22583. ((comment quote-section) t)
  22584. (comment-block
  22585. ;; Allow checks between block markers, not on them.
  22586. (and (> (line-beginning-position) post-affiliated)
  22587. (save-excursion
  22588. (end-of-line)
  22589. (skip-chars-forward " \r\t\n")
  22590. (< (point) (org-element-property :end element)))))
  22591. ;; Arbitrary list of keywords where checks are meaningful.
  22592. ;; Make sure point is on the value part of the element.
  22593. (keyword
  22594. (and (member (org-element-property :key element)
  22595. '("DESCRIPTION" "TITLE"))
  22596. (< (save-excursion
  22597. (beginning-of-line) (search-forward ":") (point))
  22598. (point))))
  22599. ;; Check is globally allowed in paragraphs verse blocks and
  22600. ;; table rows (after affiliated keywords) but some objects
  22601. ;; must not be affected.
  22602. ((paragraph table-row verse-block)
  22603. (and (>= (point) (org-element-property :contents-begin element))
  22604. (< (point) (org-element-property :contents-end element))
  22605. (funcall object-check)))))))))
  22606. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  22607. (defun org-remove-flyspell-overlays-in (beg end)
  22608. "Remove flyspell overlays in region."
  22609. (and (org-bound-and-true-p flyspell-mode)
  22610. (fboundp 'flyspell-delete-region-overlays)
  22611. (flyspell-delete-region-overlays beg end)))
  22612. (defvar flyspell-delayed-commands)
  22613. (eval-after-load "flyspell"
  22614. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  22615. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  22616. (eval-after-load "bookmark"
  22617. '(if (boundp 'bookmark-after-jump-hook)
  22618. ;; We can use the hook
  22619. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  22620. ;; Hook not available, use advice
  22621. (defadvice bookmark-jump (after org-make-visible activate)
  22622. "Make the position visible."
  22623. (org-bookmark-jump-unhide))))
  22624. ;; Make sure saveplace shows the location if it was hidden
  22625. (eval-after-load "saveplace"
  22626. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  22627. "Make the position visible."
  22628. (org-bookmark-jump-unhide)))
  22629. ;; Make sure ecb shows the location if it was hidden
  22630. (eval-after-load "ecb"
  22631. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  22632. "Make hierarchy visible when jumping into location from ECB tree buffer."
  22633. (if (derived-mode-p 'org-mode)
  22634. (org-show-context))))
  22635. (defun org-bookmark-jump-unhide ()
  22636. "Unhide the current position, to show the bookmark location."
  22637. (and (derived-mode-p 'org-mode)
  22638. (or (outline-invisible-p)
  22639. (save-excursion (goto-char (max (point-min) (1- (point))))
  22640. (outline-invisible-p)))
  22641. (org-show-context 'bookmark-jump)))
  22642. (defun org-mark-jump-unhide ()
  22643. "Make the point visible with `org-show-context' after jumping to the mark."
  22644. (when (and (derived-mode-p 'org-mode)
  22645. (outline-invisible-p))
  22646. (org-show-context 'mark-goto)))
  22647. (eval-after-load "simple"
  22648. '(defadvice pop-to-mark-command (after org-make-visible activate)
  22649. "Make the point visible with `org-show-context'."
  22650. (org-mark-jump-unhide)))
  22651. (eval-after-load "simple"
  22652. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  22653. "Make the point visible with `org-show-context'."
  22654. (org-mark-jump-unhide)))
  22655. (eval-after-load "simple"
  22656. '(defadvice pop-global-mark (after org-make-visible activate)
  22657. "Make the point visible with `org-show-context'."
  22658. (org-mark-jump-unhide)))
  22659. ;; Make session.el ignore our circular variable
  22660. (defvar session-globals-exclude)
  22661. (eval-after-load "session"
  22662. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  22663. ;;;; Finish up
  22664. (provide 'org)
  22665. (run-hooks 'org-load-hook)
  22666. ;;; org.el ends here