org.el 933 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643
  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-or-clock-line-re
  350. (concat "^[ \t]*"
  351. (regexp-opt
  352. (list org-clock-string org-closed-string org-deadline-string
  353. org-scheduled-string)
  354. t))
  355. "Matches a line with planning or clock info.
  356. Matched keyword is in group 1.")
  357. ;;;; Drawer
  358. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  359. "Matches first or last line of a hidden block.
  360. Group 1 contains drawer's name or \"END\".")
  361. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  362. "Regular expression matching the first line of a property drawer.")
  363. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  364. "Regular expression matching the last line of a property drawer.")
  365. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  366. "Regular expression matching the first line of a clock drawer.")
  367. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  368. "Regular expression matching the last line of a clock drawer.")
  369. (defconst org-property-drawer-re
  370. (concat "\\(" org-property-start-re "\\)[^\000]*?\\("
  371. org-property-end-re "\\)\n?")
  372. "Matches an entire property drawer.")
  373. (defconst org-clock-drawer-re
  374. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  375. org-clock-drawer-end-re "\\)\n?")
  376. "Matches an entire clock drawer.")
  377. ;;;; Headline
  378. (defconst org-heading-keyword-regexp-format
  379. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  380. "Printf format for a regexp matching a headline with some keyword.
  381. This regexp will match the headline of any node which has the
  382. exact keyword that is put into the format. The keyword isn't in
  383. any group by default, but the stars and the body are.")
  384. (defconst org-heading-keyword-maybe-regexp-format
  385. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  386. "Printf format for a regexp matching a headline, possibly with some keyword.
  387. This regexp can match any headline with the specified keyword, or
  388. without a keyword. The keyword isn't in any group by default,
  389. but the stars and the body are.")
  390. (defconst org-archive-tag "ARCHIVE"
  391. "The tag that marks a subtree as archived.
  392. An archived subtree does not open during visibility cycling, and does
  393. not contribute to the agenda listings.")
  394. (defconst org-comment-string "COMMENT"
  395. "Entries starting with this keyword will never be exported.
  396. An entry can be toggled between COMMENT and normal with
  397. \\[org-toggle-comment].")
  398. ;;;; LaTeX Environments and Fragments
  399. (defconst org-latex-regexps
  400. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  401. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  402. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  403. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  404. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  405. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  406. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  407. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  408. "Regular expressions for matching embedded LaTeX.")
  409. ;;;; Node Property
  410. (defconst org-effort-property "Effort"
  411. "The property that is being used to keep track of effort estimates.
  412. Effort estimates given in this property need to have the format H:MM.")
  413. ;;;; Table
  414. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  415. "Detect an org-type or table-type table.")
  416. (defconst org-table-line-regexp "^[ \t]*|"
  417. "Detect an org-type table line.")
  418. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  419. "Detect an org-type table line.")
  420. (defconst org-table-hline-regexp "^[ \t]*|-"
  421. "Detect an org-type table hline.")
  422. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  423. "Detect a table-type table hline.")
  424. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  425. "Detect the first line outside a table when searching from within it.
  426. This works for both table types.")
  427. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  428. "Detect a #+TBLFM line.")
  429. ;;;; Timestamp
  430. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  431. "Regular expression for fast time stamp matching.")
  432. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  433. "Regular expression for fast time stamp matching.")
  434. (defconst org-ts-regexp0
  435. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  436. "Regular expression matching time strings for analysis.
  437. This one does not require the space after the date, so it can be used
  438. on a string that terminates immediately after the date.")
  439. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  440. "Regular expression matching time strings for analysis.")
  441. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  442. "Regular expression matching time stamps, with groups.")
  443. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  444. "Regular expression matching time stamps (also [..]), with groups.")
  445. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  446. "Regular expression matching a time stamp range.")
  447. (defconst org-tr-regexp-both
  448. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  449. "Regular expression matching a time stamp range.")
  450. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  451. org-ts-regexp "\\)?")
  452. "Regular expression matching a time stamp or time stamp range.")
  453. (defconst org-tsr-regexp-both
  454. (concat org-ts-regexp-both "\\(--?-?"
  455. org-ts-regexp-both "\\)?")
  456. "Regular expression matching a time stamp or time stamp range.
  457. The time stamps may be either active or inactive.")
  458. (defconst org-repeat-re
  459. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  460. "Regular expression for specifying repeated events.
  461. After a match, group 1 contains the repeat expression.")
  462. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  463. "Formats for `format-time-string' which are used for time stamps.")
  464. ;;; The custom variables
  465. (defgroup org nil
  466. "Outline-based notes management and organizer."
  467. :tag "Org"
  468. :group 'outlines
  469. :group 'calendar)
  470. (defcustom org-mode-hook nil
  471. "Mode hook for Org-mode, run after the mode was turned on."
  472. :group 'org
  473. :type 'hook)
  474. (defcustom org-load-hook nil
  475. "Hook that is run after org.el has been loaded."
  476. :group 'org
  477. :type 'hook)
  478. (defcustom org-log-buffer-setup-hook nil
  479. "Hook that is run after an Org log buffer is created."
  480. :group 'org
  481. :version "24.1"
  482. :type 'hook)
  483. (defvar org-modules) ; defined below
  484. (defvar org-modules-loaded nil
  485. "Have the modules been loaded already?")
  486. (defun org-load-modules-maybe (&optional force)
  487. "Load all extensions listed in `org-modules'."
  488. (when (or force (not org-modules-loaded))
  489. (mapc (lambda (ext)
  490. (condition-case nil (require ext)
  491. (error (message "Problems while trying to load feature `%s'" ext))))
  492. org-modules)
  493. (setq org-modules-loaded t)))
  494. (defun org-set-modules (var value)
  495. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  496. (set var value)
  497. (when (featurep 'org)
  498. (org-load-modules-maybe 'force)
  499. (org-element-cache-reset 'all)))
  500. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  501. "Modules that should always be loaded together with org.el.
  502. If a description starts with <C>, the file is not part of Emacs
  503. and loading it will require that you have downloaded and properly
  504. installed the Org mode distribution.
  505. You can also use this system to load external packages (i.e. neither Org
  506. core modules, nor modules from the CONTRIB directory). Just add symbols
  507. to the end of the list. If the package is called org-xyz.el, then you need
  508. to add the symbol `xyz', and the package must have a call to:
  509. \(provide 'org-xyz)
  510. For export specific modules, see also `org-export-backends'."
  511. :group 'org
  512. :set 'org-set-modules
  513. :version "24.4"
  514. :package-version '(Org . "8.0")
  515. :type
  516. '(set :greedy t
  517. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  518. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  519. (const :tag " crypt: Encryption of subtrees" org-crypt)
  520. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  521. (const :tag " docview: Links to doc-view buffers" org-docview)
  522. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  523. (const :tag " habit: Track your consistency with habits" org-habit)
  524. (const :tag " id: Global IDs for identifying entries" org-id)
  525. (const :tag " info: Links to Info nodes" org-info)
  526. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  527. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  528. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  529. (const :tag " mouse: Additional mouse support" org-mouse)
  530. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  531. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  532. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  533. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  534. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  535. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  536. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  537. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  538. (const :tag "C collector: Collect properties into tables" org-collector)
  539. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  540. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  541. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  542. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  543. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  544. (const :tag "C eval: Include command output as text" org-eval)
  545. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  546. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  547. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  548. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  549. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  550. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  551. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  552. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  553. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  554. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  555. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  556. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  557. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  558. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  559. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  560. (const :tag "C registry: A registry for Org-mode links" org-registry)
  561. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  562. (const :tag "C secretary: Team management with org-mode" org-secretary)
  563. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  564. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  565. (const :tag "C track: Keep up with Org-mode development" org-track)
  566. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  567. (const :tag "C vm: Links to VM folders/messages" org-vm)
  568. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  569. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  570. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  571. (defvar org-export--registered-backends) ; From ox.el.
  572. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  573. (declare-function org-export-backend-name "ox" (backend))
  574. (defcustom org-export-backends '(ascii html icalendar latex)
  575. "List of export back-ends that should be always available.
  576. If a description starts with <C>, the file is not part of Emacs
  577. and loading it will require that you have downloaded and properly
  578. installed the Org mode distribution.
  579. Unlike to `org-modules', libraries in this list will not be
  580. loaded along with Org, but only once the export framework is
  581. needed.
  582. This variable needs to be set before org.el is loaded. If you
  583. need to make a change while Emacs is running, use the customize
  584. interface or run the following code, where VAL stands for the new
  585. value of the variable, after updating it:
  586. \(progn
  587. \(setq org-export--registered-backends
  588. \(org-remove-if-not
  589. \(lambda (backend)
  590. \(let ((name (org-export-backend-name backend)))
  591. \(or (memq name val)
  592. \(catch 'parentp
  593. \(dolist (b val)
  594. \(and (org-export-derived-backend-p b name)
  595. \(throw 'parentp t)))))))
  596. org-export--registered-backends))
  597. \(let ((new-list (mapcar 'org-export-backend-name
  598. org-export--registered-backends)))
  599. \(dolist (backend val)
  600. \(cond
  601. \((not (load (format \"ox-%s\" backend) t t))
  602. \(message \"Problems while trying to load export back-end `%s'\"
  603. backend))
  604. \((not (memq backend new-list)) (push backend new-list))))
  605. \(set-default 'org-export-backends new-list)))
  606. Adding a back-end to this list will also pull the back-end it
  607. depends on, if any."
  608. :group 'org
  609. :group 'org-export
  610. :version "24.4"
  611. :package-version '(Org . "8.0")
  612. :initialize 'custom-initialize-set
  613. :set (lambda (var val)
  614. (if (not (featurep 'ox)) (set-default var val)
  615. ;; Any back-end not required anymore (not present in VAL and not
  616. ;; a parent of any back-end in the new value) is removed from the
  617. ;; list of registered back-ends.
  618. (setq org-export--registered-backends
  619. (org-remove-if-not
  620. (lambda (backend)
  621. (let ((name (org-export-backend-name backend)))
  622. (or (memq name val)
  623. (catch 'parentp
  624. (dolist (b val)
  625. (and (org-export-derived-backend-p b name)
  626. (throw 'parentp t)))))))
  627. org-export--registered-backends))
  628. ;; Now build NEW-LIST of both new back-ends and required
  629. ;; parents.
  630. (let ((new-list (mapcar 'org-export-backend-name
  631. org-export--registered-backends)))
  632. (dolist (backend val)
  633. (cond
  634. ((not (load (format "ox-%s" backend) t t))
  635. (message "Problems while trying to load export back-end `%s'"
  636. backend))
  637. ((not (memq backend new-list)) (push backend new-list))))
  638. ;; Set VAR to that list with fixed dependencies.
  639. (set-default var new-list))))
  640. :type '(set :greedy t
  641. (const :tag " ascii Export buffer to ASCII format" ascii)
  642. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  643. (const :tag " html Export buffer to HTML format" html)
  644. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  645. (const :tag " latex Export buffer to LaTeX format" latex)
  646. (const :tag " man Export buffer to MAN format" man)
  647. (const :tag " md Export buffer to Markdown format" md)
  648. (const :tag " odt Export buffer to ODT format" odt)
  649. (const :tag " org Export buffer to Org format" org)
  650. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  651. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  652. (const :tag "C deck Export buffer to deck.js presentations" deck)
  653. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  654. (const :tag "C groff Export buffer to Groff format" groff)
  655. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  656. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  657. (const :tag "C s5 Export buffer to s5 presentations" s5)
  658. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  659. (eval-after-load 'ox
  660. '(mapc
  661. (lambda (backend)
  662. (condition-case nil (require (intern (format "ox-%s" backend)))
  663. (error (message "Problems while trying to load export back-end `%s'"
  664. backend))))
  665. org-export-backends))
  666. (defcustom org-support-shift-select nil
  667. "Non-nil means make shift-cursor commands select text when possible.
  668. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  669. start selecting a region, or enlarge regions started in this way.
  670. In Org-mode, in special contexts, these same keys are used for
  671. other purposes, important enough to compete with shift selection.
  672. Org tries to balance these needs by supporting `shift-select-mode'
  673. outside these special contexts, under control of this variable.
  674. The default of this variable is nil, to avoid confusing behavior. Shifted
  675. cursor keys will then execute Org commands in the following contexts:
  676. - on a headline, changing TODO state (left/right) and priority (up/down)
  677. - on a time stamp, changing the time
  678. - in a plain list item, changing the bullet type
  679. - in a property definition line, switching between allowed values
  680. - in the BEGIN line of a clock table (changing the time block).
  681. Outside these contexts, the commands will throw an error.
  682. When this variable is t and the cursor is not in a special
  683. context, Org-mode will support shift-selection for making and
  684. enlarging regions. To make this more effective, the bullet
  685. cycling will no longer happen anywhere in an item line, but only
  686. if the cursor is exactly on the bullet.
  687. If you set this variable to the symbol `always', then the keys
  688. will not be special in headlines, property lines, and item lines,
  689. to make shift selection work there as well. If this is what you
  690. want, you can use the following alternative commands: `C-c C-t'
  691. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  692. can be used to switch TODO sets, `C-c -' to cycle item bullet
  693. types, and properties can be edited by hand or in column view.
  694. However, when the cursor is on a timestamp, shift-cursor commands
  695. will still edit the time stamp - this is just too good to give up.
  696. XEmacs user should have this variable set to nil, because
  697. `shift-select-mode' is in Emacs 23 or later only."
  698. :group 'org
  699. :type '(choice
  700. (const :tag "Never" nil)
  701. (const :tag "When outside special context" t)
  702. (const :tag "Everywhere except timestamps" always)))
  703. (defcustom org-loop-over-headlines-in-active-region nil
  704. "Shall some commands act upon headlines in the active region?
  705. When set to `t', some commands will be performed in all headlines
  706. within the active region.
  707. When set to `start-level', some commands will be performed in all
  708. headlines within the active region, provided that these headlines
  709. are of the same level than the first one.
  710. When set to a string, those commands will be performed on the
  711. matching headlines within the active region. Such string must be
  712. a tags/property/todo match as it is used in the agenda tags view.
  713. The list of commands is: `org-schedule', `org-deadline',
  714. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  715. `org-archive-to-archive-sibling'. The archiving commands skip
  716. already archived entries."
  717. :type '(choice (const :tag "Don't loop" nil)
  718. (const :tag "All headlines in active region" t)
  719. (const :tag "In active region, headlines at the same level than the first one" start-level)
  720. (string :tag "Tags/Property/Todo matcher"))
  721. :version "24.1"
  722. :group 'org-todo
  723. :group 'org-archive)
  724. (defgroup org-startup nil
  725. "Options concerning startup of Org-mode."
  726. :tag "Org Startup"
  727. :group 'org)
  728. (defcustom org-startup-folded t
  729. "Non-nil means entering Org-mode will switch to OVERVIEW.
  730. This can also be configured on a per-file basis by adding one of
  731. the following lines anywhere in the buffer:
  732. #+STARTUP: fold (or `overview', this is equivalent)
  733. #+STARTUP: nofold (or `showall', this is equivalent)
  734. #+STARTUP: content
  735. #+STARTUP: showeverything
  736. By default, this option is ignored when Org opens agenda files
  737. for the first time. If you want the agenda to honor the startup
  738. option, set `org-agenda-inhibit-startup' to nil."
  739. :group 'org-startup
  740. :type '(choice
  741. (const :tag "nofold: show all" nil)
  742. (const :tag "fold: overview" t)
  743. (const :tag "content: all headlines" content)
  744. (const :tag "show everything, even drawers" showeverything)))
  745. (defcustom org-startup-truncated t
  746. "Non-nil means entering Org-mode will set `truncate-lines'.
  747. This is useful since some lines containing links can be very long and
  748. uninteresting. Also tables look terrible when wrapped."
  749. :group 'org-startup
  750. :type 'boolean)
  751. (defcustom org-startup-indented nil
  752. "Non-nil means turn on `org-indent-mode' on startup.
  753. This can also be configured on a per-file basis by adding one of
  754. the following lines anywhere in the buffer:
  755. #+STARTUP: indent
  756. #+STARTUP: noindent"
  757. :group 'org-structure
  758. :type '(choice
  759. (const :tag "Not" nil)
  760. (const :tag "Globally (slow on startup in large files)" t)))
  761. (defcustom org-use-sub-superscripts t
  762. "Non-nil means interpret \"_\" and \"^\" for display.
  763. If you want to control how Org exports those characters, see
  764. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  765. used to be an alias for `org-export-with-sub-superscripts' in
  766. Org <8.0, it is not anymore.
  767. When this option is turned on, you can use TeX-like syntax for
  768. sub- and superscripts within the buffer. Several characters after
  769. \"_\" or \"^\" will be considered as a single item - so grouping
  770. with {} is normally not needed. For example, the following things
  771. will be parsed as single sub- or superscripts:
  772. 10^24 or 10^tau several digits will be considered 1 item.
  773. 10^-12 or 10^-tau a leading sign with digits or a word
  774. x^2-y^3 will be read as x^2 - y^3, because items are
  775. terminated by almost any nonword/nondigit char.
  776. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  777. Still, ambiguity is possible. So when in doubt, use {} to enclose
  778. the sub/superscript. If you set this variable to the symbol `{}',
  779. the braces are *required* in order to trigger interpretations as
  780. sub/superscript. This can be helpful in documents that need \"_\"
  781. frequently in plain text."
  782. :group 'org-startup
  783. :version "24.4"
  784. :package-version '(Org . "8.0")
  785. :type '(choice
  786. (const :tag "Always interpret" t)
  787. (const :tag "Only with braces" {})
  788. (const :tag "Never interpret" nil)))
  789. (defcustom org-startup-with-beamer-mode nil
  790. "Non-nil means turn on `org-beamer-mode' on startup.
  791. This can also be configured on a per-file basis by adding one of
  792. the following lines anywhere in the buffer:
  793. #+STARTUP: beamer"
  794. :group 'org-startup
  795. :version "24.1"
  796. :type 'boolean)
  797. (defcustom org-startup-align-all-tables nil
  798. "Non-nil means align all tables when visiting a file.
  799. This is useful when the column width in tables is forced with <N> cookies
  800. in table fields. Such tables will look correct only after the first re-align.
  801. This can also be configured on a per-file basis by adding one of
  802. the following lines anywhere in the buffer:
  803. #+STARTUP: align
  804. #+STARTUP: noalign"
  805. :group 'org-startup
  806. :type 'boolean)
  807. (defcustom org-startup-with-inline-images nil
  808. "Non-nil means show inline images when loading a new Org file.
  809. This can also be configured on a per-file basis by adding one of
  810. the following lines anywhere in the buffer:
  811. #+STARTUP: inlineimages
  812. #+STARTUP: noinlineimages"
  813. :group 'org-startup
  814. :version "24.1"
  815. :type 'boolean)
  816. (defcustom org-startup-with-latex-preview nil
  817. "Non-nil means preview LaTeX fragments when loading a new Org file.
  818. This can also be configured on a per-file basis by adding one of
  819. the following lines anywhere in the buffer:
  820. #+STARTUP: latexpreview
  821. #+STARTUP: nolatexpreview"
  822. :group 'org-startup
  823. :version "24.4"
  824. :package-version '(Org . "8.0")
  825. :type 'boolean)
  826. (defcustom org-insert-mode-line-in-empty-file nil
  827. "Non-nil means insert the first line setting Org-mode in empty files.
  828. When the function `org-mode' is called interactively in an empty file, this
  829. normally means that the file name does not automatically trigger Org-mode.
  830. To ensure that the file will always be in Org-mode in the future, a
  831. line enforcing Org-mode will be inserted into the buffer, if this option
  832. has been set."
  833. :group 'org-startup
  834. :type 'boolean)
  835. (defcustom org-replace-disputed-keys nil
  836. "Non-nil means use alternative key bindings for some keys.
  837. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  838. These keys are also used by other packages like shift-selection-mode'
  839. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  840. If you want to use Org-mode together with one of these other modes,
  841. or more generally if you would like to move some Org-mode commands to
  842. other keys, set this variable and configure the keys with the variable
  843. `org-disputed-keys'.
  844. This option is only relevant at load-time of Org-mode, and must be set
  845. *before* org.el is loaded. Changing it requires a restart of Emacs to
  846. become effective."
  847. :group 'org-startup
  848. :type 'boolean)
  849. (defcustom org-use-extra-keys nil
  850. "Non-nil means use extra key sequence definitions for certain commands.
  851. This happens automatically if you run XEmacs or if `window-system'
  852. is nil. This variable lets you do the same manually. You must
  853. set it before loading org.
  854. Example: on Carbon Emacs 22 running graphically, with an external
  855. keyboard on a Powerbook, the default way of setting M-left might
  856. not work for either Alt or ESC. Setting this variable will make
  857. it work for ESC."
  858. :group 'org-startup
  859. :type 'boolean)
  860. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  861. (defcustom org-disputed-keys
  862. '(([(shift up)] . [(meta p)])
  863. ([(shift down)] . [(meta n)])
  864. ([(shift left)] . [(meta -)])
  865. ([(shift right)] . [(meta +)])
  866. ([(control shift right)] . [(meta shift +)])
  867. ([(control shift left)] . [(meta shift -)]))
  868. "Keys for which Org-mode and other modes compete.
  869. This is an alist, cars are the default keys, second element specifies
  870. the alternative to use when `org-replace-disputed-keys' is t.
  871. Keys can be specified in any syntax supported by `define-key'.
  872. The value of this option takes effect only at Org-mode's startup,
  873. therefore you'll have to restart Emacs to apply it after changing."
  874. :group 'org-startup
  875. :type 'alist)
  876. (defun org-key (key)
  877. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  878. Or return the original if not disputed.
  879. Also apply the translations defined in `org-xemacs-key-equivalents'."
  880. (when org-replace-disputed-keys
  881. (let* ((nkey (key-description key))
  882. (x (org-find-if (lambda (x)
  883. (equal (key-description (car x)) nkey))
  884. org-disputed-keys)))
  885. (setq key (if x (cdr x) key))))
  886. (when (featurep 'xemacs)
  887. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  888. key)
  889. (defun org-find-if (predicate seq)
  890. (catch 'exit
  891. (while seq
  892. (if (funcall predicate (car seq))
  893. (throw 'exit (car seq))
  894. (pop seq)))))
  895. (defun org-defkey (keymap key def)
  896. "Define a key, possibly translated, as returned by `org-key'."
  897. (define-key keymap (org-key key) def))
  898. (defcustom org-ellipsis nil
  899. "The ellipsis to use in the Org-mode outline.
  900. When nil, just use the standard three dots.
  901. When a string, use that string instead.
  902. When a face, use the standard 3 dots, but with the specified face.
  903. The change affects only Org-mode (which will then use its own display table).
  904. Changing this requires executing \\[org-mode] in a buffer to become
  905. effective."
  906. :group 'org-startup
  907. :type '(choice (const :tag "Default" nil)
  908. (face :tag "Face" :value org-warning)
  909. (string :tag "String" :value "...#")))
  910. (defvar org-display-table nil
  911. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  912. (defgroup org-keywords nil
  913. "Keywords in Org-mode."
  914. :tag "Org Keywords"
  915. :group 'org)
  916. (defcustom org-closed-keep-when-no-todo nil
  917. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  918. :group 'org-todo
  919. :group 'org-keywords
  920. :version "24.4"
  921. :package-version '(Org . "8.0")
  922. :type 'boolean)
  923. (defgroup org-structure nil
  924. "Options concerning the general structure of Org-mode files."
  925. :tag "Org Structure"
  926. :group 'org)
  927. (defgroup org-reveal-location nil
  928. "Options about how to make context of a location visible."
  929. :tag "Org Reveal Location"
  930. :group 'org-structure)
  931. (defconst org-context-choice
  932. '(choice
  933. (const :tag "Always" t)
  934. (const :tag "Never" nil)
  935. (repeat :greedy t :tag "Individual contexts"
  936. (cons
  937. (choice :tag "Context"
  938. (const agenda)
  939. (const org-goto)
  940. (const occur-tree)
  941. (const tags-tree)
  942. (const link-search)
  943. (const mark-goto)
  944. (const bookmark-jump)
  945. (const isearch)
  946. (const default))
  947. (boolean))))
  948. "Contexts for the reveal options.")
  949. (defcustom org-show-hierarchy-above '((default . t))
  950. "Non-nil means show full hierarchy when revealing a location.
  951. Org-mode often shows locations in an org-mode file which might have
  952. been invisible before. When this is set, the hierarchy of headings
  953. above the exposed location is shown.
  954. Turning this off for example for sparse trees makes them very compact.
  955. Instead of t, this can also be an alist specifying this option for different
  956. contexts. Valid contexts are
  957. agenda when exposing an entry from the agenda
  958. org-goto when using the command `org-goto' on key C-c C-j
  959. occur-tree when using the command `org-occur' on key C-c /
  960. tags-tree when constructing a sparse tree based on tags matches
  961. link-search when exposing search matches associated with a link
  962. mark-goto when exposing the jump goal of a mark
  963. bookmark-jump when exposing a bookmark location
  964. isearch when exiting from an incremental search
  965. default default for all contexts not set explicitly"
  966. :group 'org-reveal-location
  967. :type org-context-choice)
  968. (defcustom org-show-following-heading '((default . nil))
  969. "Non-nil means show following heading when revealing a location.
  970. Org-mode often shows locations in an org-mode file which might have
  971. been invisible before. When this is set, the heading following the
  972. match is shown.
  973. Turning this off for example for sparse trees makes them very compact,
  974. but makes it harder to edit the location of the match. In such a case,
  975. use the command \\[org-reveal] to show more context.
  976. Instead of t, this can also be an alist specifying this option for different
  977. contexts. See `org-show-hierarchy-above' for valid contexts."
  978. :group 'org-reveal-location
  979. :type org-context-choice)
  980. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  981. "Non-nil means show all sibling heading when revealing a location.
  982. Org-mode often shows locations in an org-mode file which might have
  983. been invisible before. When this is set, the sibling of the current entry
  984. heading are all made visible. If `org-show-hierarchy-above' is t,
  985. the same happens on each level of the hierarchy above the current entry.
  986. By default this is on for the isearch context, off for all other contexts.
  987. Turning this off for example for sparse trees makes them very compact,
  988. but makes it harder to edit the location of the match. In such a case,
  989. use the command \\[org-reveal] to show more context.
  990. Instead of t, this can also be an alist specifying this option for different
  991. contexts. See `org-show-hierarchy-above' for valid contexts."
  992. :group 'org-reveal-location
  993. :type org-context-choice
  994. :version "24.4"
  995. :package-version '(Org . "8.0"))
  996. (defcustom org-show-entry-below '((default . nil))
  997. "Non-nil means show the entry below a headline when revealing a location.
  998. Org-mode often shows locations in an org-mode file which might have
  999. been invisible before. When this is set, the text below the headline that is
  1000. exposed is also shown.
  1001. By default this is off for all contexts.
  1002. Instead of t, this can also be an alist specifying this option for different
  1003. contexts. See `org-show-hierarchy-above' for valid contexts."
  1004. :group 'org-reveal-location
  1005. :type org-context-choice)
  1006. (defcustom org-indirect-buffer-display 'other-window
  1007. "How should indirect tree buffers be displayed?
  1008. This applies to indirect buffers created with the commands
  1009. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1010. Valid values are:
  1011. current-window Display in the current window
  1012. other-window Just display in another window.
  1013. dedicated-frame Create one new frame, and re-use it each time.
  1014. new-frame Make a new frame each time. Note that in this case
  1015. previously-made indirect buffers are kept, and you need to
  1016. kill these buffers yourself."
  1017. :group 'org-structure
  1018. :group 'org-agenda-windows
  1019. :type '(choice
  1020. (const :tag "In current window" current-window)
  1021. (const :tag "In current frame, other window" other-window)
  1022. (const :tag "Each time a new frame" new-frame)
  1023. (const :tag "One dedicated frame" dedicated-frame)))
  1024. (defcustom org-use-speed-commands nil
  1025. "Non-nil means activate single letter commands at beginning of a headline.
  1026. This may also be a function to test for appropriate locations where speed
  1027. commands should be active.
  1028. For example, to activate speed commands when the point is on any
  1029. star at the beginning of the headline, you can do this:
  1030. (setq org-use-speed-commands
  1031. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1032. :group 'org-structure
  1033. :type '(choice
  1034. (const :tag "Never" nil)
  1035. (const :tag "At beginning of headline stars" t)
  1036. (function)))
  1037. (defcustom org-speed-commands-user nil
  1038. "Alist of additional speed commands.
  1039. This list will be checked before `org-speed-commands-default'
  1040. when the variable `org-use-speed-commands' is non-nil
  1041. and when the cursor is at the beginning of a headline.
  1042. The car if each entry is a string with a single letter, which must
  1043. be assigned to `self-insert-command' in the global map.
  1044. The cdr is either a command to be called interactively, a function
  1045. to be called, or a form to be evaluated.
  1046. An entry that is just a list with a single string will be interpreted
  1047. as a descriptive headline that will be added when listing the speed
  1048. commands in the Help buffer using the `?' speed command."
  1049. :group 'org-structure
  1050. :type '(repeat :value ("k" . ignore)
  1051. (choice :value ("k" . ignore)
  1052. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1053. (cons :tag "Letter and Command"
  1054. (string :tag "Command letter")
  1055. (choice
  1056. (function)
  1057. (sexp))))))
  1058. (defcustom org-bookmark-names-plist
  1059. '(:last-capture "org-capture-last-stored"
  1060. :last-refile "org-refile-last-stored"
  1061. :last-capture-marker "org-capture-last-stored-marker")
  1062. "Names for bookmarks automatically set by some Org commands.
  1063. This can provide strings as names for a number of bookmarks Org sets
  1064. automatically. The following keys are currently implemented:
  1065. :last-capture
  1066. :last-capture-marker
  1067. :last-refile
  1068. When a key does not show up in the property list, the corresponding bookmark
  1069. is not set."
  1070. :group 'org-structure
  1071. :type 'plist)
  1072. (defgroup org-cycle nil
  1073. "Options concerning visibility cycling in Org-mode."
  1074. :tag "Org Cycle"
  1075. :group 'org-structure)
  1076. (defcustom org-cycle-skip-children-state-if-no-children t
  1077. "Non-nil means skip CHILDREN state in entries that don't have any."
  1078. :group 'org-cycle
  1079. :type 'boolean)
  1080. (defcustom org-cycle-max-level nil
  1081. "Maximum level which should still be subject to visibility cycling.
  1082. Levels higher than this will, for cycling, be treated as text, not a headline.
  1083. When `org-odd-levels-only' is set, a value of N in this variable actually
  1084. means 2N-1 stars as the limiting headline.
  1085. When nil, cycle all levels.
  1086. Note that the limiting level of cycling is also influenced by
  1087. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1088. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1089. than its value."
  1090. :group 'org-cycle
  1091. :type '(choice
  1092. (const :tag "No limit" nil)
  1093. (integer :tag "Maximum level")))
  1094. (defcustom org-hide-block-startup nil
  1095. "Non-nil means entering Org-mode will fold all blocks.
  1096. This can also be set in on a per-file basis with
  1097. #+STARTUP: hideblocks
  1098. #+STARTUP: showblocks"
  1099. :group 'org-startup
  1100. :group 'org-cycle
  1101. :type 'boolean)
  1102. (defcustom org-cycle-global-at-bob nil
  1103. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1104. This makes it possible to do global cycling without having to use S-TAB or
  1105. \\[universal-argument] TAB. For this special case to work, the first line
  1106. of the buffer must not be a headline -- it may be empty or some other text.
  1107. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1108. sure the cursor stays at the beginning of the buffer. When this option is
  1109. nil, don't do anything special at the beginning of the buffer."
  1110. :group 'org-cycle
  1111. :type 'boolean)
  1112. (defcustom org-cycle-level-after-item/entry-creation t
  1113. "Non-nil means cycle entry level or item indentation in new empty entries.
  1114. When the cursor is at the end of an empty headline, i.e., with only stars
  1115. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1116. and then all possible ancestor states, before returning to the original state.
  1117. This makes data entry extremely fast: M-RET to create a new headline,
  1118. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1119. When the cursor is at the end of an empty plain list item, one TAB will
  1120. make it a subitem, two or more tabs will back up to make this an item
  1121. higher up in the item hierarchy."
  1122. :group 'org-cycle
  1123. :type 'boolean)
  1124. (defcustom org-cycle-emulate-tab t
  1125. "Where should `org-cycle' emulate TAB.
  1126. nil Never
  1127. white Only in completely white lines
  1128. whitestart Only at the beginning of lines, before the first non-white char
  1129. t Everywhere except in headlines
  1130. exc-hl-bol Everywhere except at the start of a headline
  1131. If TAB is used in a place where it does not emulate TAB, the current subtree
  1132. visibility is cycled."
  1133. :group 'org-cycle
  1134. :type '(choice (const :tag "Never" nil)
  1135. (const :tag "Only in completely white lines" white)
  1136. (const :tag "Before first char in a line" whitestart)
  1137. (const :tag "Everywhere except in headlines" t)
  1138. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1139. (defcustom org-cycle-separator-lines 2
  1140. "Number of empty lines needed to keep an empty line between collapsed trees.
  1141. If you leave an empty line between the end of a subtree and the following
  1142. headline, this empty line is hidden when the subtree is folded.
  1143. Org-mode will leave (exactly) one empty line visible if the number of
  1144. empty lines is equal or larger to the number given in this variable.
  1145. So the default 2 means at least 2 empty lines after the end of a subtree
  1146. are needed to produce free space between a collapsed subtree and the
  1147. following headline.
  1148. If the number is negative, and the number of empty lines is at least -N,
  1149. all empty lines are shown.
  1150. Special case: when 0, never leave empty lines in collapsed view."
  1151. :group 'org-cycle
  1152. :type 'integer)
  1153. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1154. (defcustom org-pre-cycle-hook nil
  1155. "Hook that is run before visibility cycling is happening.
  1156. The function(s) in this hook must accept a single argument which indicates
  1157. the new state that will be set right after running this hook. The
  1158. argument is a symbol. Before a global state change, it can have the values
  1159. `overview', `content', or `all'. Before a local state change, it can have
  1160. the values `folded', `children', or `subtree'."
  1161. :group 'org-cycle
  1162. :type 'hook)
  1163. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1164. org-cycle-hide-drawers
  1165. org-cycle-hide-inline-tasks
  1166. org-cycle-show-empty-lines
  1167. org-optimize-window-after-visibility-change)
  1168. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1169. The function(s) in this hook must accept a single argument which indicates
  1170. the new state that was set by the most recent `org-cycle' command. The
  1171. argument is a symbol. After a global state change, it can have the values
  1172. `overview', `contents', or `all'. After a local state change, it can have
  1173. the values `folded', `children', or `subtree'."
  1174. :group 'org-cycle
  1175. :type 'hook)
  1176. (defgroup org-edit-structure nil
  1177. "Options concerning structure editing in Org-mode."
  1178. :tag "Org Edit Structure"
  1179. :group 'org-structure)
  1180. (defcustom org-odd-levels-only nil
  1181. "Non-nil means skip even levels and only use odd levels for the outline.
  1182. This has the effect that two stars are being added/taken away in
  1183. promotion/demotion commands. It also influences how levels are
  1184. handled by the exporters.
  1185. Changing it requires restart of `font-lock-mode' to become effective
  1186. for fontification also in regions already fontified.
  1187. You may also set this on a per-file basis by adding one of the following
  1188. lines to the buffer:
  1189. #+STARTUP: odd
  1190. #+STARTUP: oddeven"
  1191. :group 'org-edit-structure
  1192. :group 'org-appearance
  1193. :type 'boolean)
  1194. (defcustom org-adapt-indentation t
  1195. "Non-nil means adapt indentation to outline node level.
  1196. When this variable is set, Org assumes that you write outlines by
  1197. indenting text in each node to align with the headline (after the stars).
  1198. The following issues are influenced by this variable:
  1199. - When this is set and the *entire* text in an entry is indented, the
  1200. indentation is increased by one space in a demotion command, and
  1201. decreased by one in a promotion command. If any line in the entry
  1202. body starts with text at column 0, indentation is not changed at all.
  1203. - Property drawers and planning information is inserted indented when
  1204. this variable s set. When nil, they will not be indented.
  1205. - TAB indents a line relative to context. The lines below a headline
  1206. will be indented when this variable is set.
  1207. Note that this is all about true indentation, by adding and removing
  1208. space characters. See also `org-indent.el' which does level-dependent
  1209. indentation in a virtual way, i.e. at display time in Emacs."
  1210. :group 'org-edit-structure
  1211. :type 'boolean)
  1212. (defcustom org-special-ctrl-a/e nil
  1213. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1214. When t, `C-a' will bring back the cursor to the beginning of the
  1215. headline text, i.e. after the stars and after a possible TODO
  1216. keyword. In an item, this will be the position after bullet and
  1217. check-box, if any. When the cursor is already at that position,
  1218. another `C-a' will bring it to the beginning of the line.
  1219. `C-e' will jump to the end of the headline, ignoring the presence
  1220. of tags in the headline. A second `C-e' will then jump to the
  1221. true end of the line, after any tags. This also means that, when
  1222. this variable is non-nil, `C-e' also will never jump beyond the
  1223. end of the heading of a folded section, i.e. not after the
  1224. ellipses.
  1225. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1226. normally, going to the true line boundary first. Only a directly
  1227. following, identical keypress will bring the cursor to the
  1228. special positions.
  1229. This may also be a cons cell where the behavior for `C-a' and
  1230. `C-e' is set separately."
  1231. :group 'org-edit-structure
  1232. :type '(choice
  1233. (const :tag "off" nil)
  1234. (const :tag "on: after stars/bullet and before tags first" t)
  1235. (const :tag "reversed: true line boundary first" reversed)
  1236. (cons :tag "Set C-a and C-e separately"
  1237. (choice :tag "Special C-a"
  1238. (const :tag "off" nil)
  1239. (const :tag "on: after stars/bullet first" t)
  1240. (const :tag "reversed: before stars/bullet first" reversed))
  1241. (choice :tag "Special C-e"
  1242. (const :tag "off" nil)
  1243. (const :tag "on: before tags first" t)
  1244. (const :tag "reversed: after tags first" reversed)))))
  1245. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1246. (defcustom org-special-ctrl-k nil
  1247. "Non-nil means `C-k' will behave specially in headlines.
  1248. When nil, `C-k' will call the default `kill-line' command.
  1249. When t, the following will happen while the cursor is in the headline:
  1250. - When the cursor is at the beginning of a headline, kill the entire
  1251. line and possible the folded subtree below the line.
  1252. - When in the middle of the headline text, kill the headline up to the tags.
  1253. - When after the headline text, kill the tags."
  1254. :group 'org-edit-structure
  1255. :type 'boolean)
  1256. (defcustom org-ctrl-k-protect-subtree nil
  1257. "Non-nil means, do not delete a hidden subtree with C-k.
  1258. When set to the symbol `error', simply throw an error when C-k is
  1259. used to kill (part-of) a headline that has hidden text behind it.
  1260. Any other non-nil value will result in a query to the user, if it is
  1261. OK to kill that hidden subtree. When nil, kill without remorse."
  1262. :group 'org-edit-structure
  1263. :version "24.1"
  1264. :type '(choice
  1265. (const :tag "Do not protect hidden subtrees" nil)
  1266. (const :tag "Protect hidden subtrees with a security query" t)
  1267. (const :tag "Never kill a hidden subtree with C-k" error)))
  1268. (defcustom org-special-ctrl-o t
  1269. "Non-nil means, make `C-o' insert a row in tables."
  1270. :group 'org-edit-structure
  1271. :type 'boolean)
  1272. (defcustom org-catch-invisible-edits nil
  1273. "Check if in invisible region before inserting or deleting a character.
  1274. Valid values are:
  1275. nil Do not check, so just do invisible edits.
  1276. error Throw an error and do nothing.
  1277. show Make point visible, and do the requested edit.
  1278. show-and-error Make point visible, then throw an error and abort the edit.
  1279. smart Make point visible, and do insertion/deletion if it is
  1280. adjacent to visible text and the change feels predictable.
  1281. Never delete a previously invisible character or add in the
  1282. middle or right after an invisible region. Basically, this
  1283. allows insertion and backward-delete right before ellipses.
  1284. FIXME: maybe in this case we should not even show?"
  1285. :group 'org-edit-structure
  1286. :version "24.1"
  1287. :type '(choice
  1288. (const :tag "Do not check" nil)
  1289. (const :tag "Throw error when trying to edit" error)
  1290. (const :tag "Unhide, but do not do the edit" show-and-error)
  1291. (const :tag "Show invisible part and do the edit" show)
  1292. (const :tag "Be smart and do the right thing" smart)))
  1293. (defcustom org-yank-folded-subtrees t
  1294. "Non-nil means when yanking subtrees, fold them.
  1295. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1296. it starts with a heading and all other headings in it are either children
  1297. or siblings, then fold all the subtrees. However, do this only if no
  1298. text after the yank would be swallowed into a folded tree by this action."
  1299. :group 'org-edit-structure
  1300. :type 'boolean)
  1301. (defcustom org-yank-adjusted-subtrees nil
  1302. "Non-nil means when yanking subtrees, adjust the level.
  1303. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1304. this function for details."
  1305. :group 'org-edit-structure
  1306. :type 'boolean)
  1307. (defcustom org-M-RET-may-split-line '((default . t))
  1308. "Non-nil means M-RET will split the line at the cursor position.
  1309. When nil, it will go to the end of the line before making a
  1310. new line.
  1311. You may also set this option in a different way for different
  1312. contexts. Valid contexts are:
  1313. headline when creating a new headline
  1314. item when creating a new item
  1315. table in a table field
  1316. default the value to be used for all contexts not explicitly
  1317. customized"
  1318. :group 'org-structure
  1319. :group 'org-table
  1320. :type '(choice
  1321. (const :tag "Always" t)
  1322. (const :tag "Never" nil)
  1323. (repeat :greedy t :tag "Individual contexts"
  1324. (cons
  1325. (choice :tag "Context"
  1326. (const headline)
  1327. (const item)
  1328. (const table)
  1329. (const default))
  1330. (boolean)))))
  1331. (defcustom org-insert-heading-respect-content nil
  1332. "Non-nil means insert new headings after the current subtree.
  1333. When nil, the new heading is created directly after the current line.
  1334. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1335. this variable on for the duration of the command."
  1336. :group 'org-structure
  1337. :type 'boolean)
  1338. (defcustom org-blank-before-new-entry '((heading . auto)
  1339. (plain-list-item . auto))
  1340. "Should `org-insert-heading' leave a blank line before new heading/item?
  1341. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1342. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1343. which case Org will look at the surrounding headings/items and try to
  1344. make an intelligent decision whether to insert a blank line or not.
  1345. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1346. the setting here is ignored and no empty line is inserted to avoid breaking
  1347. the list structure."
  1348. :group 'org-edit-structure
  1349. :type '(list
  1350. (cons (const heading)
  1351. (choice (const :tag "Never" nil)
  1352. (const :tag "Always" t)
  1353. (const :tag "Auto" auto)))
  1354. (cons (const plain-list-item)
  1355. (choice (const :tag "Never" nil)
  1356. (const :tag "Always" t)
  1357. (const :tag "Auto" auto)))))
  1358. (defcustom org-insert-heading-hook nil
  1359. "Hook being run after inserting a new heading."
  1360. :group 'org-edit-structure
  1361. :type 'hook)
  1362. (defcustom org-enable-fixed-width-editor t
  1363. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1364. This currently only means they are never auto-wrapped.
  1365. When nil, such lines will be treated like ordinary lines."
  1366. :group 'org-edit-structure
  1367. :type 'boolean)
  1368. (defcustom org-goto-auto-isearch t
  1369. "Non-nil means typing characters in `org-goto' starts incremental search.
  1370. When nil, you can use these keybindings to navigate the buffer:
  1371. q Quit the org-goto interface
  1372. n Go to the next visible heading
  1373. p Go to the previous visible heading
  1374. f Go one heading forward on same level
  1375. b Go one heading backward on same level
  1376. u Go one heading up"
  1377. :group 'org-edit-structure
  1378. :type 'boolean)
  1379. (defgroup org-sparse-trees nil
  1380. "Options concerning sparse trees in Org-mode."
  1381. :tag "Org Sparse Trees"
  1382. :group 'org-structure)
  1383. (defcustom org-highlight-sparse-tree-matches t
  1384. "Non-nil means highlight all matches that define a sparse tree.
  1385. The highlights will automatically disappear the next time the buffer is
  1386. changed by an edit command."
  1387. :group 'org-sparse-trees
  1388. :type 'boolean)
  1389. (defcustom org-remove-highlights-with-change t
  1390. "Non-nil means any change to the buffer will remove temporary highlights.
  1391. Such highlights are created by `org-occur' and `org-clock-display'.
  1392. When nil, `C-c C-c' needs to be used to get rid of the highlights.
  1393. The highlights created by `org-toggle-latex-fragment' always need
  1394. `C-c C-x C-l' to be removed."
  1395. :group 'org-sparse-trees
  1396. :group 'org-time
  1397. :type 'boolean)
  1398. (defcustom org-occur-hook '(org-first-headline-recenter)
  1399. "Hook that is run after `org-occur' has constructed a sparse tree.
  1400. This can be used to recenter the window to show as much of the structure
  1401. as possible."
  1402. :group 'org-sparse-trees
  1403. :type 'hook)
  1404. (defgroup org-imenu-and-speedbar nil
  1405. "Options concerning imenu and speedbar in Org-mode."
  1406. :tag "Org Imenu and Speedbar"
  1407. :group 'org-structure)
  1408. (defcustom org-imenu-depth 2
  1409. "The maximum level for Imenu access to Org-mode headlines.
  1410. This also applied for speedbar access."
  1411. :group 'org-imenu-and-speedbar
  1412. :type 'integer)
  1413. (defgroup org-table nil
  1414. "Options concerning tables in Org-mode."
  1415. :tag "Org Table"
  1416. :group 'org)
  1417. (defcustom org-enable-table-editor 'optimized
  1418. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1419. When nil, such lines will be treated like ordinary lines.
  1420. When equal to the symbol `optimized', the table editor will be optimized to
  1421. do the following:
  1422. - Automatic overwrite mode in front of whitespace in table fields.
  1423. This makes the structure of the table stay in tact as long as the edited
  1424. field does not exceed the column width.
  1425. - Minimize the number of realigns. Normally, the table is aligned each time
  1426. TAB or RET are pressed to move to another field. With optimization this
  1427. happens only if changes to a field might have changed the column width.
  1428. Optimization requires replacing the functions `self-insert-command',
  1429. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1430. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1431. very good at guessing when a re-align will be necessary, but you can always
  1432. force one with \\[org-ctrl-c-ctrl-c].
  1433. If you would like to use the optimized version in Org-mode, but the
  1434. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1435. This variable can be used to turn on and off the table editor during a session,
  1436. but in order to toggle optimization, a restart is required.
  1437. See also the variable `org-table-auto-blank-field'."
  1438. :group 'org-table
  1439. :type '(choice
  1440. (const :tag "off" nil)
  1441. (const :tag "on" t)
  1442. (const :tag "on, optimized" optimized)))
  1443. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1444. (version<= emacs-version "24.1"))
  1445. "Non-nil means cluster self-insert commands for undo when possible.
  1446. If this is set, then, like in the Emacs command loop, 20 consecutive
  1447. characters will be undone together.
  1448. This is configurable, because there is some impact on typing performance."
  1449. :group 'org-table
  1450. :type 'boolean)
  1451. (defcustom org-table-tab-recognizes-table.el t
  1452. "Non-nil means TAB will automatically notice a table.el table.
  1453. When it sees such a table, it moves point into it and - if necessary -
  1454. calls `table-recognize-table'."
  1455. :group 'org-table-editing
  1456. :type 'boolean)
  1457. (defgroup org-link nil
  1458. "Options concerning links in Org-mode."
  1459. :tag "Org Link"
  1460. :group 'org)
  1461. (defvar org-link-abbrev-alist-local nil
  1462. "Buffer-local version of `org-link-abbrev-alist', which see.
  1463. The value of this is taken from the #+LINK lines.")
  1464. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1465. (defcustom org-link-abbrev-alist nil
  1466. "Alist of link abbreviations.
  1467. The car of each element is a string, to be replaced at the start of a link.
  1468. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1469. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1470. [[linkkey:tag][description]]
  1471. The 'linkkey' must be a word word, starting with a letter, followed
  1472. by letters, numbers, '-' or '_'.
  1473. If REPLACE is a string, the tag will simply be appended to create the link.
  1474. If the string contains \"%s\", the tag will be inserted there. If the string
  1475. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1476. at that point (see the function `url-hexify-string'). If the string contains
  1477. the specifier \"%(my-function)\", then the custom function `my-function' will
  1478. be invoked: this function takes the tag as its only argument and must return
  1479. a string.
  1480. REPLACE may also be a function that will be called with the tag as the
  1481. only argument to create the link, which should be returned as a string.
  1482. See the manual for examples."
  1483. :group 'org-link
  1484. :type '(repeat
  1485. (cons
  1486. (string :tag "Protocol")
  1487. (choice
  1488. (string :tag "Format")
  1489. (function)))))
  1490. (defcustom org-descriptive-links t
  1491. "Non-nil means Org will display descriptive links.
  1492. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1493. \"Org Website\", hiding the link itself and just displaying its
  1494. description. When set to `nil', Org will display the full links
  1495. literally.
  1496. You can interactively set the value of this variable by calling
  1497. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1498. :group 'org-link
  1499. :type 'boolean)
  1500. (defcustom org-link-file-path-type 'adaptive
  1501. "How the path name in file links should be stored.
  1502. Valid values are:
  1503. relative Relative to the current directory, i.e. the directory of the file
  1504. into which the link is being inserted.
  1505. absolute Absolute path, if possible with ~ for home directory.
  1506. noabbrev Absolute path, no abbreviation of home directory.
  1507. adaptive Use relative path for files in the current directory and sub-
  1508. directories of it. For other files, use an absolute path."
  1509. :group 'org-link
  1510. :type '(choice
  1511. (const relative)
  1512. (const absolute)
  1513. (const noabbrev)
  1514. (const adaptive)))
  1515. (defvaralias 'org-activate-links 'org-highlight-links)
  1516. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1517. "Types of links that should be highlighted in Org-mode files.
  1518. This is a list of symbols, each one of them leading to the
  1519. highlighting of a certain link type.
  1520. You can still open links that are not highlighted.
  1521. In principle, it does not hurt to turn on highlighting for all
  1522. link types. There may be a small gain when turning off unused
  1523. link types. The types are:
  1524. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1525. angle Links in angular brackets that may contain whitespace like
  1526. <bbdb:Carsten Dominik>.
  1527. plain Plain links in normal text, no whitespace, like http://google.com.
  1528. radio Text that is matched by a radio target, see manual for details.
  1529. tag Tag settings in a headline (link to tag search).
  1530. date Time stamps (link to calendar).
  1531. footnote Footnote labels.
  1532. If you set this variable during an Emacs session, use `org-mode-restart'
  1533. in the Org buffer so that the change takes effect."
  1534. :group 'org-link
  1535. :group 'org-appearance
  1536. :type '(set :greedy t
  1537. (const :tag "Double bracket links" bracket)
  1538. (const :tag "Angular bracket links" angle)
  1539. (const :tag "Plain text links" plain)
  1540. (const :tag "Radio target matches" radio)
  1541. (const :tag "Tags" tag)
  1542. (const :tag "Timestamps" date)
  1543. (const :tag "Footnotes" footnote)))
  1544. (defcustom org-make-link-description-function nil
  1545. "Function to use for generating link descriptions from links.
  1546. When nil, the link location will be used. This function must take
  1547. two parameters: the first one is the link, the second one is the
  1548. description generated by `org-insert-link'. The function should
  1549. return the description to use."
  1550. :group 'org-link
  1551. :type '(choice (const nil) (function)))
  1552. (defgroup org-link-store nil
  1553. "Options concerning storing links in Org-mode."
  1554. :tag "Org Store Link"
  1555. :group 'org-link)
  1556. (defcustom org-url-hexify-p t
  1557. "When non-nil, hexify URL when creating a link."
  1558. :type 'boolean
  1559. :version "24.3"
  1560. :group 'org-link-store)
  1561. (defcustom org-email-link-description-format "Email %c: %.30s"
  1562. "Format of the description part of a link to an email or usenet message.
  1563. The following %-escapes will be replaced by corresponding information:
  1564. %F full \"From\" field
  1565. %f name, taken from \"From\" field, address if no name
  1566. %T full \"To\" field
  1567. %t first name in \"To\" field, address if no name
  1568. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1569. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1570. %s subject
  1571. %d date
  1572. %m message-id.
  1573. You may use normal field width specification between the % and the letter.
  1574. This is for example useful to limit the length of the subject.
  1575. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1576. :group 'org-link-store
  1577. :type 'string)
  1578. (defcustom org-from-is-user-regexp
  1579. (let (r1 r2)
  1580. (when (and user-mail-address (not (string= user-mail-address "")))
  1581. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1582. (when (and user-full-name (not (string= user-full-name "")))
  1583. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1584. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1585. "Regexp matched against the \"From:\" header of an email or usenet message.
  1586. It should match if the message is from the user him/herself."
  1587. :group 'org-link-store
  1588. :type 'regexp)
  1589. (defcustom org-context-in-file-links t
  1590. "Non-nil means file links from `org-store-link' contain context.
  1591. A search string will be added to the file name with :: as separator and
  1592. used to find the context when the link is activated by the command
  1593. `org-open-at-point'. When this option is t, the entire active region
  1594. will be placed in the search string of the file link. If set to a
  1595. positive integer, only the first n lines of context will be stored.
  1596. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1597. negates this setting for the duration of the command."
  1598. :group 'org-link-store
  1599. :type '(choice boolean integer))
  1600. (defcustom org-keep-stored-link-after-insertion nil
  1601. "Non-nil means keep link in list for entire session.
  1602. The command `org-store-link' adds a link pointing to the current
  1603. location to an internal list. These links accumulate during a session.
  1604. The command `org-insert-link' can be used to insert links into any
  1605. Org-mode file (offering completion for all stored links). When this
  1606. option is nil, every link which has been inserted once using \\[org-insert-link]
  1607. will be removed from the list, to make completing the unused links
  1608. more efficient."
  1609. :group 'org-link-store
  1610. :type 'boolean)
  1611. (defgroup org-link-follow nil
  1612. "Options concerning following links in Org-mode."
  1613. :tag "Org Follow Link"
  1614. :group 'org-link)
  1615. (defcustom org-link-translation-function nil
  1616. "Function to translate links with different syntax to Org syntax.
  1617. This can be used to translate links created for example by the Planner
  1618. or emacs-wiki packages to Org syntax.
  1619. The function must accept two parameters, a TYPE containing the link
  1620. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1621. which is everything after the link protocol. It should return a cons
  1622. with possibly modified values of type and path.
  1623. Org contains a function for this, so if you set this variable to
  1624. `org-translate-link-from-planner', you should be able follow many
  1625. links created by planner."
  1626. :group 'org-link-follow
  1627. :type '(choice (const nil) (function)))
  1628. (defcustom org-follow-link-hook nil
  1629. "Hook that is run after a link has been followed."
  1630. :group 'org-link-follow
  1631. :type 'hook)
  1632. (defcustom org-tab-follows-link nil
  1633. "Non-nil means on links TAB will follow the link.
  1634. Needs to be set before org.el is loaded.
  1635. This really should not be used, it does not make sense, and the
  1636. implementation is bad."
  1637. :group 'org-link-follow
  1638. :type 'boolean)
  1639. (defcustom org-return-follows-link nil
  1640. "Non-nil means on links RET will follow the link.
  1641. In tables, the special behavior of RET has precedence."
  1642. :group 'org-link-follow
  1643. :type 'boolean)
  1644. (defcustom org-mouse-1-follows-link
  1645. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1646. "Non-nil means mouse-1 on a link will follow the link.
  1647. A longer mouse click will still set point. Does not work on XEmacs.
  1648. Needs to be set before org.el is loaded."
  1649. :group 'org-link-follow
  1650. :version "24.4"
  1651. :package-version '(Org . "8.3")
  1652. :type '(choice
  1653. (const :tag "A double click follows the link" double)
  1654. (const :tag "Unconditionally follow the link with mouse-1" t)
  1655. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1656. (defcustom org-mark-ring-length 4
  1657. "Number of different positions to be recorded in the ring.
  1658. Changing this requires a restart of Emacs to work correctly."
  1659. :group 'org-link-follow
  1660. :type 'integer)
  1661. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1662. "Non-nil means internal links in Org files must exactly match a headline.
  1663. When nil, the link search tries to match a phrase with all words
  1664. in the search text."
  1665. :group 'org-link-follow
  1666. :version "24.1"
  1667. :type '(choice
  1668. (const :tag "Use fuzzy text search" nil)
  1669. (const :tag "Match only exact headline" t)
  1670. (const :tag "Match exact headline or query to create it"
  1671. query-to-create)))
  1672. (defcustom org-link-frame-setup
  1673. '((vm . vm-visit-folder-other-frame)
  1674. (vm-imap . vm-visit-imap-folder-other-frame)
  1675. (gnus . org-gnus-no-new-news)
  1676. (file . find-file-other-window)
  1677. (wl . wl-other-frame))
  1678. "Setup the frame configuration for following links.
  1679. When following a link with Emacs, it may often be useful to display
  1680. this link in another window or frame. This variable can be used to
  1681. set this up for the different types of links.
  1682. For VM, use any of
  1683. `vm-visit-folder'
  1684. `vm-visit-folder-other-window'
  1685. `vm-visit-folder-other-frame'
  1686. For Gnus, use any of
  1687. `gnus'
  1688. `gnus-other-frame'
  1689. `org-gnus-no-new-news'
  1690. For FILE, use any of
  1691. `find-file'
  1692. `find-file-other-window'
  1693. `find-file-other-frame'
  1694. For Wanderlust use any of
  1695. `wl'
  1696. `wl-other-frame'
  1697. For the calendar, use the variable `calendar-setup'.
  1698. For BBDB, it is currently only possible to display the matches in
  1699. another window."
  1700. :group 'org-link-follow
  1701. :type '(list
  1702. (cons (const vm)
  1703. (choice
  1704. (const vm-visit-folder)
  1705. (const vm-visit-folder-other-window)
  1706. (const vm-visit-folder-other-frame)))
  1707. (cons (const vm-imap)
  1708. (choice
  1709. (const vm-visit-imap-folder)
  1710. (const vm-visit-imap-folder-other-window)
  1711. (const vm-visit-imap-folder-other-frame)))
  1712. (cons (const gnus)
  1713. (choice
  1714. (const gnus)
  1715. (const gnus-other-frame)
  1716. (const org-gnus-no-new-news)))
  1717. (cons (const file)
  1718. (choice
  1719. (const find-file)
  1720. (const find-file-other-window)
  1721. (const find-file-other-frame)))
  1722. (cons (const wl)
  1723. (choice
  1724. (const wl)
  1725. (const wl-other-frame)))))
  1726. (defcustom org-display-internal-link-with-indirect-buffer nil
  1727. "Non-nil means use indirect buffer to display infile links.
  1728. Activating internal links (from one location in a file to another location
  1729. in the same file) normally just jumps to the location. When the link is
  1730. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1731. is displayed in
  1732. another window. When this option is set, the other window actually displays
  1733. an indirect buffer clone of the current buffer, to avoid any visibility
  1734. changes to the current buffer."
  1735. :group 'org-link-follow
  1736. :type 'boolean)
  1737. (defcustom org-open-non-existing-files nil
  1738. "Non-nil means `org-open-file' will open non-existing files.
  1739. When nil, an error will be generated.
  1740. This variable applies only to external applications because they
  1741. might choke on non-existing files. If the link is to a file that
  1742. will be opened in Emacs, the variable is ignored."
  1743. :group 'org-link-follow
  1744. :type 'boolean)
  1745. (defcustom org-open-directory-means-index-dot-org nil
  1746. "Non-nil means a link to a directory really means to index.org.
  1747. When nil, following a directory link will run dired or open a finder/explorer
  1748. window on that directory."
  1749. :group 'org-link-follow
  1750. :type 'boolean)
  1751. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1752. "Non-nil means ask for confirmation before executing shell links.
  1753. Shell links can be dangerous: just think about a link
  1754. [[shell:rm -rf ~/*][Google Search]]
  1755. This link would show up in your Org-mode document as \"Google Search\",
  1756. but really it would remove your entire home directory.
  1757. Therefore we advise against setting this variable to nil.
  1758. Just change it to `y-or-n-p' if you want to confirm with a
  1759. single keystroke rather than having to type \"yes\"."
  1760. :group 'org-link-follow
  1761. :type '(choice
  1762. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1763. (const :tag "with y-or-n (faster)" y-or-n-p)
  1764. (const :tag "no confirmation (dangerous)" nil)))
  1765. (put 'org-confirm-shell-link-function
  1766. 'safe-local-variable
  1767. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1768. (defcustom org-confirm-shell-link-not-regexp ""
  1769. "A regexp to skip confirmation for shell links."
  1770. :group 'org-link-follow
  1771. :version "24.1"
  1772. :type 'regexp)
  1773. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1774. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1775. Elisp links can be dangerous: just think about a link
  1776. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1777. This link would show up in your Org-mode document as \"Google Search\",
  1778. but really it would remove your entire home directory.
  1779. Therefore we advise against setting this variable to nil.
  1780. Just change it to `y-or-n-p' if you want to confirm with a
  1781. single keystroke rather than having to type \"yes\"."
  1782. :group 'org-link-follow
  1783. :type '(choice
  1784. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1785. (const :tag "with y-or-n (faster)" y-or-n-p)
  1786. (const :tag "no confirmation (dangerous)" nil)))
  1787. (put 'org-confirm-shell-link-function
  1788. 'safe-local-variable
  1789. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1790. (defcustom org-confirm-elisp-link-not-regexp ""
  1791. "A regexp to skip confirmation for Elisp links."
  1792. :group 'org-link-follow
  1793. :version "24.1"
  1794. :type 'regexp)
  1795. (defconst org-file-apps-defaults-gnu
  1796. '((remote . emacs)
  1797. (system . mailcap)
  1798. (t . mailcap))
  1799. "Default file applications on a UNIX or GNU/Linux system.
  1800. See `org-file-apps'.")
  1801. (defconst org-file-apps-defaults-macosx
  1802. '((remote . emacs)
  1803. (t . "open %s")
  1804. (system . "open %s")
  1805. ("ps.gz" . "gv %s")
  1806. ("eps.gz" . "gv %s")
  1807. ("dvi" . "xdvi %s")
  1808. ("fig" . "xfig %s"))
  1809. "Default file applications on a MacOS X system.
  1810. The system \"open\" is known as a default, but we use X11 applications
  1811. for some files for which the OS does not have a good default.
  1812. See `org-file-apps'.")
  1813. (defconst org-file-apps-defaults-windowsnt
  1814. (list
  1815. '(remote . emacs)
  1816. (cons t
  1817. (list (if (featurep 'xemacs)
  1818. 'mswindows-shell-execute
  1819. 'w32-shell-execute)
  1820. "open" 'file))
  1821. (cons 'system
  1822. (list (if (featurep 'xemacs)
  1823. 'mswindows-shell-execute
  1824. 'w32-shell-execute)
  1825. "open" 'file)))
  1826. "Default file applications on a Windows NT system.
  1827. The system \"open\" is used for most files.
  1828. See `org-file-apps'.")
  1829. (defcustom org-file-apps
  1830. '((auto-mode . emacs)
  1831. ("\\.mm\\'" . default)
  1832. ("\\.x?html?\\'" . default)
  1833. ("\\.pdf\\'" . default))
  1834. "External applications for opening `file:path' items in a document.
  1835. Org-mode uses system defaults for different file types, but
  1836. you can use this variable to set the application for a given file
  1837. extension. The entries in this list are cons cells where the car identifies
  1838. files and the cdr the corresponding command. Possible values for the
  1839. file identifier are
  1840. \"string\" A string as a file identifier can be interpreted in different
  1841. ways, depending on its contents:
  1842. - Alphanumeric characters only:
  1843. Match links with this file extension.
  1844. Example: (\"pdf\" . \"evince %s\")
  1845. to open PDFs with evince.
  1846. - Regular expression: Match links where the
  1847. filename matches the regexp. If you want to
  1848. use groups here, use shy groups.
  1849. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1850. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1851. to open *.html and *.xhtml with firefox.
  1852. - Regular expression which contains (non-shy) groups:
  1853. Match links where the whole link, including \"::\", and
  1854. anything after that, matches the regexp.
  1855. In a custom command string, %1, %2, etc. are replaced with
  1856. the parts of the link that were matched by the groups.
  1857. For backwards compatibility, if a command string is given
  1858. that does not use any of the group matches, this case is
  1859. handled identically to the second one (i.e. match against
  1860. file name only).
  1861. In a custom lisp form, you can access the group matches with
  1862. (match-string n link).
  1863. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1864. to open [[file:document.pdf::5]] with evince at page 5.
  1865. `directory' Matches a directory
  1866. `remote' Matches a remote file, accessible through tramp or efs.
  1867. Remote files most likely should be visited through Emacs
  1868. because external applications cannot handle such paths.
  1869. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1870. so all files Emacs knows how to handle. Using this with
  1871. command `emacs' will open most files in Emacs. Beware that this
  1872. will also open html files inside Emacs, unless you add
  1873. (\"html\" . default) to the list as well.
  1874. t Default for files not matched by any of the other options.
  1875. `system' The system command to open files, like `open' on Windows
  1876. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1877. that will be selected if you call `C-c C-o' with a double
  1878. \\[universal-argument] \\[universal-argument] prefix.
  1879. Possible values for the command are:
  1880. `emacs' The file will be visited by the current Emacs process.
  1881. `default' Use the default application for this file type, which is the
  1882. association for t in the list, most likely in the system-specific
  1883. part.
  1884. This can be used to overrule an unwanted setting in the
  1885. system-specific variable.
  1886. `system' Use the system command for opening files, like \"open\".
  1887. This command is specified by the entry whose car is `system'.
  1888. Most likely, the system-specific version of this variable
  1889. does define this command, but you can overrule/replace it
  1890. here.
  1891. string A command to be executed by a shell; %s will be replaced
  1892. by the path to the file.
  1893. sexp A Lisp form which will be evaluated. The file path will
  1894. be available in the Lisp variable `file'.
  1895. For more examples, see the system specific constants
  1896. `org-file-apps-defaults-macosx'
  1897. `org-file-apps-defaults-windowsnt'
  1898. `org-file-apps-defaults-gnu'."
  1899. :group 'org-link-follow
  1900. :type '(repeat
  1901. (cons (choice :value ""
  1902. (string :tag "Extension")
  1903. (const :tag "System command to open files" system)
  1904. (const :tag "Default for unrecognized files" t)
  1905. (const :tag "Remote file" remote)
  1906. (const :tag "Links to a directory" directory)
  1907. (const :tag "Any files that have Emacs modes"
  1908. auto-mode))
  1909. (choice :value ""
  1910. (const :tag "Visit with Emacs" emacs)
  1911. (const :tag "Use default" default)
  1912. (const :tag "Use the system command" system)
  1913. (string :tag "Command")
  1914. (sexp :tag "Lisp form")))))
  1915. (defcustom org-doi-server-url "http://dx.doi.org/"
  1916. "The URL of the DOI server."
  1917. :type 'string
  1918. :version "24.3"
  1919. :group 'org-link-follow)
  1920. (defgroup org-refile nil
  1921. "Options concerning refiling entries in Org-mode."
  1922. :tag "Org Refile"
  1923. :group 'org)
  1924. (defcustom org-directory "~/org"
  1925. "Directory with org files.
  1926. This is just a default location to look for Org files. There is no need
  1927. at all to put your files into this directory. It is only used in the
  1928. following situations:
  1929. 1. When a capture template specifies a target file that is not an
  1930. absolute path. The path will then be interpreted relative to
  1931. `org-directory'
  1932. 2. When a capture note is filed away in an interactive way (when exiting the
  1933. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1934. with `org-directory' as the default path."
  1935. :group 'org-refile
  1936. :group 'org-capture
  1937. :type 'directory)
  1938. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1939. "Default target for storing notes.
  1940. Used as a fall back file for org-capture.el, for templates that
  1941. do not specify a target file."
  1942. :group 'org-refile
  1943. :group 'org-capture
  1944. :type '(choice
  1945. (const :tag "Default from remember-data-file" nil)
  1946. file))
  1947. (defcustom org-goto-interface 'outline
  1948. "The default interface to be used for `org-goto'.
  1949. Allowed values are:
  1950. outline The interface shows an outline of the relevant file
  1951. and the correct heading is found by moving through
  1952. the outline or by searching with incremental search.
  1953. outline-path-completion Headlines in the current buffer are offered via
  1954. completion. This is the interface also used by
  1955. the refile command."
  1956. :group 'org-refile
  1957. :type '(choice
  1958. (const :tag "Outline" outline)
  1959. (const :tag "Outline-path-completion" outline-path-completion)))
  1960. (defcustom org-goto-max-level 5
  1961. "Maximum target level when running `org-goto' with refile interface."
  1962. :group 'org-refile
  1963. :type 'integer)
  1964. (defcustom org-reverse-note-order nil
  1965. "Non-nil means store new notes at the beginning of a file or entry.
  1966. When nil, new notes will be filed to the end of a file or entry.
  1967. This can also be a list with cons cells of regular expressions that
  1968. are matched against file names, and values."
  1969. :group 'org-capture
  1970. :group 'org-refile
  1971. :type '(choice
  1972. (const :tag "Reverse always" t)
  1973. (const :tag "Reverse never" nil)
  1974. (repeat :tag "By file name regexp"
  1975. (cons regexp boolean))))
  1976. (defcustom org-log-refile nil
  1977. "Information to record when a task is refiled.
  1978. Possible values are:
  1979. nil Don't add anything
  1980. time Add a time stamp to the task
  1981. note Prompt for a note and add it with template `org-log-note-headings'
  1982. This option can also be set with on a per-file-basis with
  1983. #+STARTUP: nologrefile
  1984. #+STARTUP: logrefile
  1985. #+STARTUP: lognoterefile
  1986. You can have local logging settings for a subtree by setting the LOGGING
  1987. property to one or more of these keywords.
  1988. When bulk-refiling from the agenda, the value `note' is forbidden and
  1989. will temporarily be changed to `time'."
  1990. :group 'org-refile
  1991. :group 'org-progress
  1992. :version "24.1"
  1993. :type '(choice
  1994. (const :tag "No logging" nil)
  1995. (const :tag "Record timestamp" time)
  1996. (const :tag "Record timestamp with note." note)))
  1997. (defcustom org-refile-targets nil
  1998. "Targets for refiling entries with \\[org-refile].
  1999. This is a list of cons cells. Each cell contains:
  2000. - a specification of the files to be considered, either a list of files,
  2001. or a symbol whose function or variable value will be used to retrieve
  2002. a file name or a list of file names. If you use `org-agenda-files' for
  2003. that, all agenda files will be scanned for targets. Nil means consider
  2004. headings in the current buffer.
  2005. - A specification of how to find candidate refile targets. This may be
  2006. any of:
  2007. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2008. This tag has to be present in all target headlines, inheritance will
  2009. not be considered.
  2010. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2011. todo keyword.
  2012. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2013. headlines that are refiling targets.
  2014. - a cons cell (:level . N). Any headline of level N is considered a target.
  2015. Note that, when `org-odd-levels-only' is set, level corresponds to
  2016. order in hierarchy, not to the number of stars.
  2017. - a cons cell (:maxlevel . N). Any headline with level <= N is 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. Each element of this list generates a set of possible targets.
  2021. The union of these sets is presented (with completion) to
  2022. the user by `org-refile'.
  2023. You can set the variable `org-refile-target-verify-function' to a function
  2024. to verify each headline found by the simple criteria above.
  2025. When this variable is nil, all top-level headlines in the current buffer
  2026. are used, equivalent to the value `((nil . (:level . 1))'."
  2027. :group 'org-refile
  2028. :type '(repeat
  2029. (cons
  2030. (choice :value org-agenda-files
  2031. (const :tag "All agenda files" org-agenda-files)
  2032. (const :tag "Current buffer" nil)
  2033. (function) (variable) (file))
  2034. (choice :tag "Identify target headline by"
  2035. (cons :tag "Specific tag" (const :value :tag) (string))
  2036. (cons :tag "TODO keyword" (const :value :todo) (string))
  2037. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2038. (cons :tag "Level number" (const :value :level) (integer))
  2039. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2040. (defcustom org-refile-target-verify-function nil
  2041. "Function to verify if the headline at point should be a refile target.
  2042. The function will be called without arguments, with point at the
  2043. beginning of the headline. It should return t and leave point
  2044. where it is if the headline is a valid target for refiling.
  2045. If the target should not be selected, the function must return nil.
  2046. In addition to this, it may move point to a place from where the search
  2047. should be continued. For example, the function may decide that the entire
  2048. subtree of the current entry should be excluded and move point to the end
  2049. of the subtree."
  2050. :group 'org-refile
  2051. :type '(choice
  2052. (const nil)
  2053. (function)))
  2054. (defcustom org-refile-use-cache nil
  2055. "Non-nil means cache refile targets to speed up the process.
  2056. The cache for a particular file will be updated automatically when
  2057. the buffer has been killed, or when any of the marker used for flagging
  2058. refile targets no longer points at a live buffer.
  2059. If you have added new entries to a buffer that might themselves be targets,
  2060. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  2061. find that easier, `C-u C-u C-u C-c C-w'."
  2062. :group 'org-refile
  2063. :version "24.1"
  2064. :type 'boolean)
  2065. (defcustom org-refile-use-outline-path nil
  2066. "Non-nil means provide refile targets as paths.
  2067. So a level 3 headline will be available as level1/level2/level3.
  2068. When the value is `file', also include the file name (without directory)
  2069. into the path. In this case, you can also stop the completion after
  2070. the file name, to get entries inserted as top level in the file.
  2071. When `full-file-path', include the full file path."
  2072. :group 'org-refile
  2073. :type '(choice
  2074. (const :tag "Not" nil)
  2075. (const :tag "Yes" t)
  2076. (const :tag "Start with file name" file)
  2077. (const :tag "Start with full file path" full-file-path)))
  2078. (defcustom org-outline-path-complete-in-steps t
  2079. "Non-nil means complete the outline path in hierarchical steps.
  2080. When Org-mode uses the refile interface to select an outline path
  2081. \(see variable `org-refile-use-outline-path'), the completion of
  2082. the path can be done is a single go, or if can be done in steps down
  2083. the headline hierarchy. Going in steps is probably the best if you
  2084. do not use a special completion package like `ido' or `icicles'.
  2085. However, when using these packages, going in one step can be very
  2086. fast, while still showing the whole path to the entry."
  2087. :group 'org-refile
  2088. :type 'boolean)
  2089. (defcustom org-refile-allow-creating-parent-nodes nil
  2090. "Non-nil means allow to create new nodes as refile targets.
  2091. New nodes are then created by adding \"/new node name\" to the completion
  2092. of an existing node. When the value of this variable is `confirm',
  2093. new node creation must be confirmed by the user (recommended).
  2094. When nil, the completion must match an existing entry.
  2095. Note that, if the new heading is not seen by the criteria
  2096. listed in `org-refile-targets', multiple instances of the same
  2097. heading would be created by trying again to file under the new
  2098. heading."
  2099. :group 'org-refile
  2100. :type '(choice
  2101. (const :tag "Never" nil)
  2102. (const :tag "Always" t)
  2103. (const :tag "Prompt for confirmation" confirm)))
  2104. (defcustom org-refile-active-region-within-subtree nil
  2105. "Non-nil means also refile active region within a subtree.
  2106. By default `org-refile' doesn't allow refiling regions if they
  2107. don't contain a set of subtrees, but it might be convenient to
  2108. do so sometimes: in that case, the first line of the region is
  2109. converted to a headline before refiling."
  2110. :group 'org-refile
  2111. :version "24.1"
  2112. :type 'boolean)
  2113. (defgroup org-todo nil
  2114. "Options concerning TODO items in Org-mode."
  2115. :tag "Org TODO"
  2116. :group 'org)
  2117. (defgroup org-progress nil
  2118. "Options concerning Progress logging in Org-mode."
  2119. :tag "Org Progress"
  2120. :group 'org-time)
  2121. (defvar org-todo-interpretation-widgets
  2122. '((:tag "Sequence (cycling hits every state)" sequence)
  2123. (:tag "Type (cycling directly to DONE)" type))
  2124. "The available interpretation symbols for customizing `org-todo-keywords'.
  2125. Interested libraries should add to this list.")
  2126. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2127. "List of TODO entry keyword sequences and their interpretation.
  2128. \\<org-mode-map>This is a list of sequences.
  2129. Each sequence starts with a symbol, either `sequence' or `type',
  2130. indicating if the keywords should be interpreted as a sequence of
  2131. action steps, or as different types of TODO items. The first
  2132. keywords are states requiring action - these states will select a headline
  2133. for inclusion into the global TODO list Org-mode produces. If one of
  2134. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2135. signify that no further action is necessary. If \"|\" is not found,
  2136. the last keyword is treated as the only DONE state of the sequence.
  2137. The command \\[org-todo] cycles an entry through these states, and one
  2138. additional state where no keyword is present. For details about this
  2139. cycling, see the manual.
  2140. TODO keywords and interpretation can also be set on a per-file basis with
  2141. the special #+SEQ_TODO and #+TYP_TODO lines.
  2142. Each keyword can optionally specify a character for fast state selection
  2143. \(in combination with the variable `org-use-fast-todo-selection')
  2144. and specifiers for state change logging, using the same syntax that
  2145. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2146. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2147. indicates to record a time stamp each time this state is selected.
  2148. Each keyword may also specify if a timestamp or a note should be
  2149. recorded when entering or leaving the state, by adding additional
  2150. characters in the parenthesis after the keyword. This looks like this:
  2151. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2152. record only the time of the state change. With X and Y being either
  2153. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2154. Y when leaving the state if and only if the *target* state does not
  2155. define X. You may omit any of the fast-selection key or X or /Y,
  2156. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2157. For backward compatibility, this variable may also be just a list
  2158. of keywords. In this case the interpretation (sequence or type) will be
  2159. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2160. :group 'org-todo
  2161. :group 'org-keywords
  2162. :type '(choice
  2163. (repeat :tag "Old syntax, just keywords"
  2164. (string :tag "Keyword"))
  2165. (repeat :tag "New syntax"
  2166. (cons
  2167. (choice
  2168. :tag "Interpretation"
  2169. ;;Quick and dirty way to see
  2170. ;;`org-todo-interpretations'. This takes the
  2171. ;;place of item arguments
  2172. :convert-widget
  2173. (lambda (widget)
  2174. (widget-put widget
  2175. :args (mapcar
  2176. (lambda (x)
  2177. (widget-convert
  2178. (cons 'const x)))
  2179. org-todo-interpretation-widgets))
  2180. widget))
  2181. (repeat
  2182. (string :tag "Keyword"))))))
  2183. (defvar org-todo-keywords-1 nil
  2184. "All TODO and DONE keywords active in a buffer.")
  2185. (make-variable-buffer-local 'org-todo-keywords-1)
  2186. (defvar org-todo-keywords-for-agenda nil)
  2187. (defvar org-done-keywords-for-agenda nil)
  2188. (defvar org-todo-keyword-alist-for-agenda nil)
  2189. (defvar org-tag-alist-for-agenda nil
  2190. "Alist of all tags from all agenda files.")
  2191. (defvar org-tag-groups-alist-for-agenda nil
  2192. "Alist of all groups tags from all current agenda files.")
  2193. (defvar org-tag-groups-alist nil)
  2194. (make-variable-buffer-local 'org-tag-groups-alist)
  2195. (defvar org-agenda-contributing-files nil)
  2196. (defvar org-not-done-keywords nil)
  2197. (make-variable-buffer-local 'org-not-done-keywords)
  2198. (defvar org-done-keywords nil)
  2199. (make-variable-buffer-local 'org-done-keywords)
  2200. (defvar org-todo-heads nil)
  2201. (make-variable-buffer-local 'org-todo-heads)
  2202. (defvar org-todo-sets nil)
  2203. (make-variable-buffer-local 'org-todo-sets)
  2204. (defvar org-todo-log-states nil)
  2205. (make-variable-buffer-local 'org-todo-log-states)
  2206. (defvar org-todo-kwd-alist nil)
  2207. (make-variable-buffer-local 'org-todo-kwd-alist)
  2208. (defvar org-todo-key-alist nil)
  2209. (make-variable-buffer-local 'org-todo-key-alist)
  2210. (defvar org-todo-key-trigger nil)
  2211. (make-variable-buffer-local 'org-todo-key-trigger)
  2212. (defcustom org-todo-interpretation 'sequence
  2213. "Controls how TODO keywords are interpreted.
  2214. This variable is in principle obsolete and is only used for
  2215. backward compatibility, if the interpretation of todo keywords is
  2216. not given already in `org-todo-keywords'. See that variable for
  2217. more information."
  2218. :group 'org-todo
  2219. :group 'org-keywords
  2220. :type '(choice (const sequence)
  2221. (const type)))
  2222. (defcustom org-use-fast-todo-selection t
  2223. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2224. This variable describes if and under what circumstances the cycling
  2225. mechanism for TODO keywords will be replaced by a single-key, direct
  2226. selection scheme.
  2227. When nil, fast selection is never used.
  2228. When the symbol `prefix', it will be used when `org-todo' is called
  2229. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2230. `C-u t' in an agenda buffer.
  2231. When t, fast selection is used by default. In this case, the prefix
  2232. argument forces cycling instead.
  2233. In all cases, the special interface is only used if access keys have
  2234. actually been assigned by the user, i.e. if keywords in the configuration
  2235. are followed by a letter in parenthesis, like TODO(t)."
  2236. :group 'org-todo
  2237. :type '(choice
  2238. (const :tag "Never" nil)
  2239. (const :tag "By default" t)
  2240. (const :tag "Only with C-u C-c C-t" prefix)))
  2241. (defcustom org-provide-todo-statistics t
  2242. "Non-nil means update todo statistics after insert and toggle.
  2243. ALL-HEADLINES means update todo statistics by including headlines
  2244. with no TODO keyword as well, counting them as not done.
  2245. A list of TODO keywords means the same, but skip keywords that are
  2246. not in this list.
  2247. When set to a list of two lists, the first list contains keywords
  2248. to consider as TODO keywords, the second list contains keywords
  2249. to consider as DONE keywords.
  2250. When this is set, todo statistics is updated in the parent of the
  2251. current entry each time a todo state is changed."
  2252. :group 'org-todo
  2253. :type '(choice
  2254. (const :tag "Yes, only for TODO entries" t)
  2255. (const :tag "Yes, including all entries" all-headlines)
  2256. (repeat :tag "Yes, for TODOs in this list"
  2257. (string :tag "TODO keyword"))
  2258. (list :tag "Yes, for TODOs and DONEs in these lists"
  2259. (repeat (string :tag "TODO keyword"))
  2260. (repeat (string :tag "DONE keyword")))
  2261. (other :tag "No TODO statistics" nil)))
  2262. (defcustom org-hierarchical-todo-statistics t
  2263. "Non-nil means TODO statistics covers just direct children.
  2264. When nil, all entries in the subtree are considered.
  2265. This has only an effect if `org-provide-todo-statistics' is set.
  2266. To set this to nil for only a single subtree, use a COOKIE_DATA
  2267. property and include the word \"recursive\" into the value."
  2268. :group 'org-todo
  2269. :type 'boolean)
  2270. (defcustom org-after-todo-state-change-hook nil
  2271. "Hook which is run after the state of a TODO item was changed.
  2272. The new state (a string with a TODO keyword, or nil) is available in the
  2273. Lisp variable `org-state'."
  2274. :group 'org-todo
  2275. :type 'hook)
  2276. (defvar org-blocker-hook nil
  2277. "Hook for functions that are allowed to block a state change.
  2278. Functions in this hook should not modify the buffer.
  2279. Each function gets as its single argument a property list,
  2280. see `org-trigger-hook' for more information about this list.
  2281. If any of the functions in this hook returns nil, the state change
  2282. is blocked.")
  2283. (defvar org-trigger-hook nil
  2284. "Hook for functions that are triggered by a state change.
  2285. Each function gets as its single argument a property list with at
  2286. least the following elements:
  2287. (:type type-of-change :position pos-at-entry-start
  2288. :from old-state :to new-state)
  2289. Depending on the type, more properties may be present.
  2290. This mechanism is currently implemented for:
  2291. TODO state changes
  2292. ------------------
  2293. :type todo-state-change
  2294. :from previous state (keyword as a string), or nil, or a symbol
  2295. 'todo' or 'done', to indicate the general type of state.
  2296. :to new state, like in :from")
  2297. (defcustom org-enforce-todo-dependencies nil
  2298. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2299. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2300. be blocked if any prior sibling is not yet done.
  2301. Finally, if the parent is blocked because of ordered siblings of its own,
  2302. the child will also be blocked."
  2303. :set (lambda (var val)
  2304. (set var val)
  2305. (if val
  2306. (add-hook 'org-blocker-hook
  2307. 'org-block-todo-from-children-or-siblings-or-parent)
  2308. (remove-hook 'org-blocker-hook
  2309. 'org-block-todo-from-children-or-siblings-or-parent)))
  2310. :group 'org-todo
  2311. :type 'boolean)
  2312. (defcustom org-enforce-todo-checkbox-dependencies nil
  2313. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2314. When this is nil, checkboxes have no influence on switching TODO states.
  2315. When non-nil, you first need to check off all check boxes before the TODO
  2316. entry can be switched to DONE.
  2317. This variable needs to be set before org.el is loaded, and you need to
  2318. restart Emacs after a change to make the change effective. The only way
  2319. to change is while Emacs is running is through the customize interface."
  2320. :set (lambda (var val)
  2321. (set var val)
  2322. (if val
  2323. (add-hook 'org-blocker-hook
  2324. 'org-block-todo-from-checkboxes)
  2325. (remove-hook 'org-blocker-hook
  2326. 'org-block-todo-from-checkboxes)))
  2327. :group 'org-todo
  2328. :type 'boolean)
  2329. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2330. "Non-nil means inserting a TODO heading is treated as state change.
  2331. So when the command \\[org-insert-todo-heading] is used, state change
  2332. logging will apply if appropriate. When nil, the new TODO item will
  2333. be inserted directly, and no logging will take place."
  2334. :group 'org-todo
  2335. :type 'boolean)
  2336. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2337. "Non-nil means switching TODO states with S-cursor counts as state change.
  2338. This is the default behavior. However, setting this to nil allows a
  2339. convenient way to select a TODO state and bypass any logging associated
  2340. with that."
  2341. :group 'org-todo
  2342. :type 'boolean)
  2343. (defcustom org-todo-state-tags-triggers nil
  2344. "Tag changes that should be triggered by TODO state changes.
  2345. This is a list. Each entry is
  2346. (state-change (tag . flag) .......)
  2347. State-change can be a string with a state, and empty string to indicate the
  2348. state that has no TODO keyword, or it can be one of the symbols `todo'
  2349. or `done', meaning any not-done or done state, respectively."
  2350. :group 'org-todo
  2351. :group 'org-tags
  2352. :type '(repeat
  2353. (cons (choice :tag "When changing to"
  2354. (const :tag "Not-done state" todo)
  2355. (const :tag "Done state" done)
  2356. (string :tag "State"))
  2357. (repeat
  2358. (cons :tag "Tag action"
  2359. (string :tag "Tag")
  2360. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2361. (defcustom org-log-done nil
  2362. "Information to record when a task moves to the DONE state.
  2363. Possible values are:
  2364. nil Don't add anything, just change the keyword
  2365. time Add a time stamp to the task
  2366. note Prompt for a note and add it with template `org-log-note-headings'
  2367. This option can also be set with on a per-file-basis with
  2368. #+STARTUP: nologdone
  2369. #+STARTUP: logdone
  2370. #+STARTUP: lognotedone
  2371. You can have local logging settings for a subtree by setting the LOGGING
  2372. property to one or more of these keywords."
  2373. :group 'org-todo
  2374. :group 'org-progress
  2375. :type '(choice
  2376. (const :tag "No logging" nil)
  2377. (const :tag "Record CLOSED timestamp" time)
  2378. (const :tag "Record CLOSED timestamp with note." note)))
  2379. ;; Normalize old uses of org-log-done.
  2380. (cond
  2381. ((eq org-log-done t) (setq org-log-done 'time))
  2382. ((and (listp org-log-done) (memq 'done org-log-done))
  2383. (setq org-log-done 'note)))
  2384. (defcustom org-log-reschedule nil
  2385. "Information to record when the scheduling date of a tasks is modified.
  2386. Possible values are:
  2387. nil Don't add anything, just change the date
  2388. time Add a time stamp to the task
  2389. note Prompt for a note and add it with template `org-log-note-headings'
  2390. This option can also be set with on a per-file-basis with
  2391. #+STARTUP: nologreschedule
  2392. #+STARTUP: logreschedule
  2393. #+STARTUP: lognotereschedule"
  2394. :group 'org-todo
  2395. :group 'org-progress
  2396. :type '(choice
  2397. (const :tag "No logging" nil)
  2398. (const :tag "Record timestamp" time)
  2399. (const :tag "Record timestamp with note." note)))
  2400. (defcustom org-log-redeadline nil
  2401. "Information to record when the deadline date of a tasks is modified.
  2402. Possible values are:
  2403. nil Don't add anything, just change the date
  2404. time Add a time stamp to the task
  2405. note Prompt for a note and add it with template `org-log-note-headings'
  2406. This option can also be set with on a per-file-basis with
  2407. #+STARTUP: nologredeadline
  2408. #+STARTUP: logredeadline
  2409. #+STARTUP: lognoteredeadline
  2410. You can have local logging settings for a subtree by setting the LOGGING
  2411. property to one or more of these keywords."
  2412. :group 'org-todo
  2413. :group 'org-progress
  2414. :type '(choice
  2415. (const :tag "No logging" nil)
  2416. (const :tag "Record timestamp" time)
  2417. (const :tag "Record timestamp with note." note)))
  2418. (defcustom org-log-note-clock-out nil
  2419. "Non-nil means record a note when clocking out of an item.
  2420. This can also be configured on a per-file basis by adding one of
  2421. the following lines anywhere in the buffer:
  2422. #+STARTUP: lognoteclock-out
  2423. #+STARTUP: nolognoteclock-out"
  2424. :group 'org-todo
  2425. :group 'org-progress
  2426. :type 'boolean)
  2427. (defcustom org-log-done-with-time t
  2428. "Non-nil means the CLOSED time stamp will contain date and time.
  2429. When nil, only the date will be recorded."
  2430. :group 'org-progress
  2431. :type 'boolean)
  2432. (defcustom org-log-note-headings
  2433. '((done . "CLOSING NOTE %t")
  2434. (state . "State %-12s from %-12S %t")
  2435. (note . "Note taken on %t")
  2436. (reschedule . "Rescheduled from %S on %t")
  2437. (delschedule . "Not scheduled, was %S on %t")
  2438. (redeadline . "New deadline from %S on %t")
  2439. (deldeadline . "Removed deadline, was %S on %t")
  2440. (refile . "Refiled on %t")
  2441. (clock-out . ""))
  2442. "Headings for notes added to entries.
  2443. The value is an alist, with the car being a symbol indicating the note
  2444. context, and the cdr is the heading to be used. The heading may also be the
  2445. empty string.
  2446. %t in the heading will be replaced by a time stamp.
  2447. %T will be an active time stamp instead the default inactive one
  2448. %d will be replaced by a short-format time stamp.
  2449. %D will be replaced by an active short-format time stamp.
  2450. %s will be replaced by the new TODO state, in double quotes.
  2451. %S will be replaced by the old TODO state, in double quotes.
  2452. %u will be replaced by the user name.
  2453. %U will be replaced by the full user name.
  2454. In fact, it is not a good idea to change the `state' entry, because
  2455. agenda log mode depends on the format of these entries."
  2456. :group 'org-todo
  2457. :group 'org-progress
  2458. :type '(list :greedy t
  2459. (cons (const :tag "Heading when closing an item" done) string)
  2460. (cons (const :tag
  2461. "Heading when changing todo state (todo sequence only)"
  2462. state) string)
  2463. (cons (const :tag "Heading when just taking a note" note) string)
  2464. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2465. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2466. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2467. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2468. (cons (const :tag "Heading when refiling" refile) string)
  2469. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2470. (unless (assq 'note org-log-note-headings)
  2471. (push '(note . "%t") org-log-note-headings))
  2472. (defcustom org-log-into-drawer nil
  2473. "Non-nil means insert state change notes and time stamps into a drawer.
  2474. When nil, state changes notes will be inserted after the headline and
  2475. any scheduling and clock lines, but not inside a drawer.
  2476. The value of this variable should be the name of the drawer to use.
  2477. LOGBOOK is proposed as the default drawer for this purpose, you can
  2478. also set this to a string to define the drawer of your choice.
  2479. A value of t is also allowed, representing \"LOGBOOK\".
  2480. A value of t or nil can also be set with on a per-file-basis with
  2481. #+STARTUP: logdrawer
  2482. #+STARTUP: nologdrawer
  2483. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2484. will be ignored.
  2485. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2486. a subtree."
  2487. :group 'org-todo
  2488. :group 'org-progress
  2489. :type '(choice
  2490. (const :tag "Not into a drawer" nil)
  2491. (const :tag "LOGBOOK" t)
  2492. (string :tag "Other")))
  2493. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2494. (defun org-log-into-drawer ()
  2495. "Return the value of `org-log-into-drawer', but let properties overrule.
  2496. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2497. used instead of the default value."
  2498. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2499. (cond
  2500. ((not p) org-log-into-drawer)
  2501. ((equal p "nil") nil)
  2502. ((equal p "t") "LOGBOOK")
  2503. (t p))))
  2504. (defcustom org-log-state-notes-insert-after-drawers nil
  2505. "Non-nil means insert state change notes after any drawers in entry.
  2506. Only the drawers that *immediately* follow the headline and the
  2507. deadline/scheduled line are skipped.
  2508. When nil, insert notes right after the heading and perhaps the line
  2509. with deadline/scheduling if present.
  2510. This variable will have no effect if `org-log-into-drawer' is
  2511. set."
  2512. :group 'org-todo
  2513. :group 'org-progress
  2514. :type 'boolean)
  2515. (defcustom org-log-states-order-reversed t
  2516. "Non-nil means the latest state note will be directly after heading.
  2517. When nil, the state change notes will be ordered according to time.
  2518. This option can also be set with on a per-file-basis with
  2519. #+STARTUP: logstatesreversed
  2520. #+STARTUP: nologstatesreversed"
  2521. :group 'org-todo
  2522. :group 'org-progress
  2523. :type 'boolean)
  2524. (defcustom org-todo-repeat-to-state nil
  2525. "The TODO state to which a repeater should return the repeating task.
  2526. By default this is the first task in a TODO sequence, or the previous state
  2527. in a TODO_TYP set. But you can specify another task here.
  2528. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2529. :group 'org-todo
  2530. :version "24.1"
  2531. :type '(choice (const :tag "Head of sequence" nil)
  2532. (string :tag "Specific state")))
  2533. (defcustom org-log-repeat 'time
  2534. "Non-nil means record moving through the DONE state when triggering repeat.
  2535. An auto-repeating task is immediately switched back to TODO when
  2536. marked DONE. If you are not logging state changes (by adding \"@\"
  2537. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2538. record a closing note, there will be no record of the task moving
  2539. through DONE. This variable forces taking a note anyway.
  2540. nil Don't force a record
  2541. time Record a time stamp
  2542. note Prompt for a note and add it with template `org-log-note-headings'
  2543. This option can also be set with on a per-file-basis with
  2544. #+STARTUP: nologrepeat
  2545. #+STARTUP: logrepeat
  2546. #+STARTUP: lognoterepeat
  2547. You can have local logging settings for a subtree by setting the LOGGING
  2548. property to one or more of these keywords."
  2549. :group 'org-todo
  2550. :group 'org-progress
  2551. :type '(choice
  2552. (const :tag "Don't force a record" nil)
  2553. (const :tag "Force recording the DONE state" time)
  2554. (const :tag "Force recording a note with the DONE state" note)))
  2555. (defgroup org-priorities nil
  2556. "Priorities in Org-mode."
  2557. :tag "Org Priorities"
  2558. :group 'org-todo)
  2559. (defcustom org-enable-priority-commands t
  2560. "Non-nil means priority commands are active.
  2561. When nil, these commands will be disabled, so that you never accidentally
  2562. set a priority."
  2563. :group 'org-priorities
  2564. :type 'boolean)
  2565. (defcustom org-highest-priority ?A
  2566. "The highest priority of TODO items. A character like ?A, ?B etc.
  2567. Must have a smaller ASCII number than `org-lowest-priority'."
  2568. :group 'org-priorities
  2569. :type 'character)
  2570. (defcustom org-lowest-priority ?C
  2571. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2572. Must have a larger ASCII number than `org-highest-priority'."
  2573. :group 'org-priorities
  2574. :type 'character)
  2575. (defcustom org-default-priority ?B
  2576. "The default priority of TODO items.
  2577. This is the priority an item gets if no explicit priority is given.
  2578. When starting to cycle on an empty priority the first step in the cycle
  2579. depends on `org-priority-start-cycle-with-default'. The resulting first
  2580. step priority must not exceed the range from `org-highest-priority' to
  2581. `org-lowest-priority' which means that `org-default-priority' has to be
  2582. in this range exclusive or inclusive the range boundaries. Else the
  2583. first step refuses to set the default and the second will fall back
  2584. to (depending on the command used) the highest or lowest priority."
  2585. :group 'org-priorities
  2586. :type 'character)
  2587. (defcustom org-priority-start-cycle-with-default t
  2588. "Non-nil means start with default priority when starting to cycle.
  2589. When this is nil, the first step in the cycle will be (depending on the
  2590. command used) one higher or lower than the default priority.
  2591. See also `org-default-priority'."
  2592. :group 'org-priorities
  2593. :type 'boolean)
  2594. (defcustom org-get-priority-function nil
  2595. "Function to extract the priority from a string.
  2596. The string is normally the headline. If this is nil Org computes the
  2597. priority from the priority cookie like [#A] in the headline. It returns
  2598. an integer, increasing by 1000 for each priority level.
  2599. The user can set a different function here, which should take a string
  2600. as an argument and return the numeric priority."
  2601. :group 'org-priorities
  2602. :version "24.1"
  2603. :type '(choice
  2604. (const nil)
  2605. (function)))
  2606. (defgroup org-time nil
  2607. "Options concerning time stamps and deadlines in Org-mode."
  2608. :tag "Org Time"
  2609. :group 'org)
  2610. (defcustom org-insert-labeled-timestamps-at-point nil
  2611. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2612. When nil, these labeled time stamps are forces into the second line of an
  2613. entry, just after the headline. When scheduling from the global TODO list,
  2614. the time stamp will always be forced into the second line."
  2615. :group 'org-time
  2616. :type 'boolean)
  2617. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2618. "Number of minutes to round time stamps to.
  2619. These are two values, the first applies when first creating a time stamp.
  2620. The second applies when changing it with the commands `S-up' and `S-down'.
  2621. When changing the time stamp, this means that it will change in steps
  2622. of N minutes, as given by the second value.
  2623. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2624. numbers should be factors of 60, so for example 5, 10, 15.
  2625. When this is larger than 1, you can still force an exact time stamp by using
  2626. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2627. and by using a prefix arg to `S-up/down' to specify the exact number
  2628. of minutes to shift."
  2629. :group 'org-time
  2630. :get (lambda (var) ; Make sure both elements are there
  2631. (if (integerp (default-value var))
  2632. (list (default-value var) 5)
  2633. (default-value var)))
  2634. :type '(list
  2635. (integer :tag "when inserting times")
  2636. (integer :tag "when modifying times")))
  2637. ;; Normalize old customizations of this variable.
  2638. (when (integerp org-time-stamp-rounding-minutes)
  2639. (setq org-time-stamp-rounding-minutes
  2640. (list org-time-stamp-rounding-minutes
  2641. org-time-stamp-rounding-minutes)))
  2642. (defcustom org-display-custom-times nil
  2643. "Non-nil means overlay custom formats over all time stamps.
  2644. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2645. To turn this on on a per-file basis, insert anywhere in the file:
  2646. #+STARTUP: customtime"
  2647. :group 'org-time
  2648. :set 'set-default
  2649. :type 'sexp)
  2650. (make-variable-buffer-local 'org-display-custom-times)
  2651. (defcustom org-time-stamp-custom-formats
  2652. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2653. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2654. These are overlaid over the default ISO format if the variable
  2655. `org-display-custom-times' is set. Time like %H:%M should be at the
  2656. end of the second format. The custom formats are also honored by export
  2657. commands, if custom time display is turned on at the time of export."
  2658. :group 'org-time
  2659. :type 'sexp)
  2660. (defun org-time-stamp-format (&optional long inactive)
  2661. "Get the right format for a time string."
  2662. (let ((f (if long (cdr org-time-stamp-formats)
  2663. (car org-time-stamp-formats))))
  2664. (if inactive
  2665. (concat "[" (substring f 1 -1) "]")
  2666. f)))
  2667. (defcustom org-time-clocksum-format
  2668. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2669. "The format string used when creating CLOCKSUM lines.
  2670. This is also used when Org mode generates a time duration.
  2671. The value can be a single format string containing two
  2672. %-sequences, which will be filled with the number of hours and
  2673. minutes in that order.
  2674. Alternatively, the value can be a plist associating any of the
  2675. keys :years, :months, :weeks, :days, :hours or :minutes with
  2676. format strings. The time duration is formatted using only the
  2677. time components that are needed and concatenating the results.
  2678. If a time unit in absent, it falls back to the next smallest
  2679. unit.
  2680. The keys :require-years, :require-months, :require-days,
  2681. :require-weeks, :require-hours, :require-minutes are also
  2682. meaningful. A non-nil value for these keys indicates that the
  2683. corresponding time component should always be included, even if
  2684. its value is 0.
  2685. For example,
  2686. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2687. :require-minutes t)
  2688. means durations longer than a day will be expressed in days,
  2689. hours and minutes, and durations less than a day will always be
  2690. expressed in hours and minutes (even for durations less than an
  2691. hour).
  2692. The value
  2693. \(:days \"%dd\" :minutes \"%dm\")
  2694. means durations longer than a day will be expressed in days and
  2695. minutes, and durations less than a day will be expressed entirely
  2696. in minutes (even for durations longer than an hour)."
  2697. :group 'org-time
  2698. :group 'org-clock
  2699. :version "24.4"
  2700. :package-version '(Org . "8.0")
  2701. :type '(choice (string :tag "Format string")
  2702. (set :tag "Plist"
  2703. (group :inline t (const :tag "Years" :years)
  2704. (string :tag "Format string"))
  2705. (group :inline t
  2706. (const :tag "Always show years" :require-years)
  2707. (const t))
  2708. (group :inline t (const :tag "Months" :months)
  2709. (string :tag "Format string"))
  2710. (group :inline t
  2711. (const :tag "Always show months" :require-months)
  2712. (const t))
  2713. (group :inline t (const :tag "Weeks" :weeks)
  2714. (string :tag "Format string"))
  2715. (group :inline t
  2716. (const :tag "Always show weeks" :require-weeks)
  2717. (const t))
  2718. (group :inline t (const :tag "Days" :days)
  2719. (string :tag "Format string"))
  2720. (group :inline t
  2721. (const :tag "Always show days" :require-days)
  2722. (const t))
  2723. (group :inline t (const :tag "Hours" :hours)
  2724. (string :tag "Format string"))
  2725. (group :inline t
  2726. (const :tag "Always show hours" :require-hours)
  2727. (const t))
  2728. (group :inline t (const :tag "Minutes" :minutes)
  2729. (string :tag "Format string"))
  2730. (group :inline t
  2731. (const :tag "Always show minutes" :require-minutes)
  2732. (const t)))))
  2733. (defcustom org-time-clocksum-use-fractional nil
  2734. "When non-nil, \\[org-clock-display] uses fractional times.
  2735. See `org-time-clocksum-format' for more on time clock formats."
  2736. :group 'org-time
  2737. :group 'org-clock
  2738. :version "24.3"
  2739. :type 'boolean)
  2740. (defcustom org-time-clocksum-use-effort-durations nil
  2741. "When non-nil, \\[org-clock-display] uses effort durations.
  2742. E.g. by default, one day is considered to be a 8 hours effort,
  2743. so a task that has been clocked for 16 hours will be displayed
  2744. as during 2 days in the clock display or in the clocktable.
  2745. See `org-effort-durations' on how to set effort durations
  2746. and `org-time-clocksum-format' for more on time clock formats."
  2747. :group 'org-time
  2748. :group 'org-clock
  2749. :version "24.4"
  2750. :package-version '(Org . "8.0")
  2751. :type 'boolean)
  2752. (defcustom org-time-clocksum-fractional-format "%.2f"
  2753. "The format string used when creating CLOCKSUM lines,
  2754. or when Org mode generates a time duration, if
  2755. `org-time-clocksum-use-fractional' is enabled.
  2756. The value can be a single format string containing one
  2757. %-sequence, which will be filled with the number of hours as
  2758. a float.
  2759. Alternatively, the value can be a plist associating any of the
  2760. keys :years, :months, :weeks, :days, :hours or :minutes with
  2761. a format string. The time duration is formatted using the
  2762. largest time unit which gives a non-zero integer part. If all
  2763. specified formats have zero integer part, the smallest time unit
  2764. is used."
  2765. :group 'org-time
  2766. :type '(choice (string :tag "Format string")
  2767. (set (group :inline t (const :tag "Years" :years)
  2768. (string :tag "Format string"))
  2769. (group :inline t (const :tag "Months" :months)
  2770. (string :tag "Format string"))
  2771. (group :inline t (const :tag "Weeks" :weeks)
  2772. (string :tag "Format string"))
  2773. (group :inline t (const :tag "Days" :days)
  2774. (string :tag "Format string"))
  2775. (group :inline t (const :tag "Hours" :hours)
  2776. (string :tag "Format string"))
  2777. (group :inline t (const :tag "Minutes" :minutes)
  2778. (string :tag "Format string")))))
  2779. (defcustom org-deadline-warning-days 14
  2780. "Number of days before expiration during which a deadline becomes active.
  2781. This variable governs the display in sparse trees and in the agenda.
  2782. When 0 or negative, it means use this number (the absolute value of it)
  2783. even if a deadline has a different individual lead time specified.
  2784. Custom commands can set this variable in the options section."
  2785. :group 'org-time
  2786. :group 'org-agenda-daily/weekly
  2787. :type 'integer)
  2788. (defcustom org-scheduled-delay-days 0
  2789. "Number of days before a scheduled item becomes active.
  2790. This variable governs the display in sparse trees and in the agenda.
  2791. The default value (i.e. 0) means: don't delay scheduled item.
  2792. When negative, it means use this number (the absolute value of it)
  2793. even if a scheduled item has a different individual delay time
  2794. specified.
  2795. Custom commands can set this variable in the options section."
  2796. :group 'org-time
  2797. :group 'org-agenda-daily/weekly
  2798. :version "24.4"
  2799. :package-version '(Org . "8.0")
  2800. :type 'integer)
  2801. (defcustom org-read-date-prefer-future t
  2802. "Non-nil means assume future for incomplete date input from user.
  2803. This affects the following situations:
  2804. 1. The user gives a month but not a year.
  2805. For example, if it is April and you enter \"feb 2\", this will be read
  2806. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2807. 2. The user gives a day, but no month.
  2808. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2809. read this as the third of *next* month. However, if you enter \"17\",
  2810. it will be considered as *this* month.
  2811. If you set this variable to the symbol `time', then also the following
  2812. will work:
  2813. 3. If the user gives a time.
  2814. If the time is before now, it will be interpreted as tomorrow.
  2815. Currently none of this works for ISO week specifications.
  2816. When this option is nil, the current day, month and year will always be
  2817. used as defaults.
  2818. See also `org-agenda-jump-prefer-future'."
  2819. :group 'org-time
  2820. :type '(choice
  2821. (const :tag "Never" nil)
  2822. (const :tag "Check month and day" t)
  2823. (const :tag "Check month, day, and time" time)))
  2824. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2825. "Should the agenda jump command prefer the future for incomplete dates?
  2826. The default is to do the same as configured in `org-read-date-prefer-future'.
  2827. But you can also set a deviating value here.
  2828. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2829. :group 'org-agenda
  2830. :group 'org-time
  2831. :version "24.1"
  2832. :type '(choice
  2833. (const :tag "Use org-read-date-prefer-future"
  2834. org-read-date-prefer-future)
  2835. (const :tag "Never" nil)
  2836. (const :tag "Always" t)))
  2837. (defcustom org-read-date-force-compatible-dates t
  2838. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2839. Depending on the system Emacs is running on, certain dates cannot
  2840. be represented with the type used internally to represent time.
  2841. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2842. correctly. Some systems allow for earlier dates, some for later,
  2843. some for both. One way to find out it to insert any date into an
  2844. Org buffer, putting the cursor on the year and hitting S-up and
  2845. S-down to test the range.
  2846. When this variable is set to t, the date/time prompt will not let
  2847. you specify dates outside the 1970-2037 range, so it is certain that
  2848. these dates will work in whatever version of Emacs you are
  2849. running, and also that you can move a file from one Emacs implementation
  2850. to another. WHenever Org is forcing the year for you, it will display
  2851. a message and beep.
  2852. When this variable is nil, Org will check if the date is
  2853. representable in the specific Emacs implementation you are using.
  2854. If not, it will force a year, usually the current year, and beep
  2855. to remind you. Currently this setting is not recommended because
  2856. the likelihood that you will open your Org files in an Emacs that
  2857. has limited date range is not negligible.
  2858. A workaround for this problem is to use diary sexp dates for time
  2859. stamps outside of this range."
  2860. :group 'org-time
  2861. :version "24.1"
  2862. :type 'boolean)
  2863. (defcustom org-read-date-display-live t
  2864. "Non-nil means display current interpretation of date prompt live.
  2865. This display will be in an overlay, in the minibuffer."
  2866. :group 'org-time
  2867. :type 'boolean)
  2868. (defcustom org-read-date-popup-calendar t
  2869. "Non-nil means pop up a calendar when prompting for a date.
  2870. In the calendar, the date can be selected with mouse-1. However, the
  2871. minibuffer will also be active, and you can simply enter the date as well.
  2872. When nil, only the minibuffer will be available."
  2873. :group 'org-time
  2874. :type 'boolean)
  2875. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2876. 'org-read-date-popup-calendar)
  2877. (make-obsolete-variable
  2878. 'org-read-date-minibuffer-setup-hook
  2879. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2880. (defcustom org-read-date-minibuffer-setup-hook nil
  2881. "Hook to be used to set up keys for the date/time interface.
  2882. Add key definitions to `minibuffer-local-map', which will be a
  2883. temporary copy.
  2884. WARNING: This option is obsolete, you should use
  2885. `org-read-date-minibuffer-local-map' to set up keys."
  2886. :group 'org-time
  2887. :type 'hook)
  2888. (defcustom org-extend-today-until 0
  2889. "The hour when your day really ends. Must be an integer.
  2890. This has influence for the following applications:
  2891. - When switching the agenda to \"today\". It it is still earlier than
  2892. the time given here, the day recognized as TODAY is actually yesterday.
  2893. - When a date is read from the user and it is still before the time given
  2894. here, the current date and time will be assumed to be yesterday, 23:59.
  2895. Also, timestamps inserted in capture templates follow this rule.
  2896. IMPORTANT: This is a feature whose implementation is and likely will
  2897. remain incomplete. Really, it is only here because past midnight seems to
  2898. be the favorite working time of John Wiegley :-)"
  2899. :group 'org-time
  2900. :type 'integer)
  2901. (defcustom org-use-effective-time nil
  2902. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2903. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2904. \"effective time\" of any timestamps between midnight and 8am will be
  2905. 23:59 of the previous day."
  2906. :group 'org-time
  2907. :version "24.1"
  2908. :type 'boolean)
  2909. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2910. "When non-nil, use the last clock out time for `org-todo'.
  2911. Note that this option has precedence over the combined use of
  2912. `org-use-effective-time' and `org-extend-today-until'."
  2913. :group 'org-time
  2914. :version "24.4"
  2915. :package-version '(Org . "8.0")
  2916. :type 'boolean)
  2917. (defcustom org-edit-timestamp-down-means-later nil
  2918. "Non-nil means S-down will increase the time in a time stamp.
  2919. When nil, S-up will increase."
  2920. :group 'org-time
  2921. :type 'boolean)
  2922. (defcustom org-calendar-follow-timestamp-change t
  2923. "Non-nil means make the calendar window follow timestamp changes.
  2924. When a timestamp is modified and the calendar window is visible, it will be
  2925. moved to the new date."
  2926. :group 'org-time
  2927. :type 'boolean)
  2928. (defgroup org-tags nil
  2929. "Options concerning tags in Org-mode."
  2930. :tag "Org Tags"
  2931. :group 'org)
  2932. (defcustom org-tag-alist nil
  2933. "List of tags allowed in Org-mode files.
  2934. When this list is nil, Org-mode will base TAG input on what is already in the
  2935. buffer.
  2936. The value of this variable is an alist, the car of each entry must be a
  2937. keyword as a string, the cdr may be a character that is used to select
  2938. that tag through the fast-tag-selection interface.
  2939. See the manual for details."
  2940. :group 'org-tags
  2941. :type '(repeat
  2942. (choice
  2943. (cons (string :tag "Tag name")
  2944. (character :tag "Access char"))
  2945. (list :tag "Start radio group"
  2946. (const :startgroup)
  2947. (option (string :tag "Group description")))
  2948. (list :tag "Group tags delimiter"
  2949. (const :grouptags))
  2950. (list :tag "End radio group"
  2951. (const :endgroup)
  2952. (option (string :tag "Group description")))
  2953. (const :tag "New line" (:newline)))))
  2954. (defcustom org-tag-persistent-alist nil
  2955. "List of tags that will always appear in all Org-mode files.
  2956. This is in addition to any in buffer settings or customizations
  2957. of `org-tag-alist'.
  2958. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2959. The value of this variable is an alist, the car of each entry must be a
  2960. keyword as a string, the cdr may be a character that is used to select
  2961. that tag through the fast-tag-selection interface.
  2962. See the manual for details.
  2963. To disable these tags on a per-file basis, insert anywhere in the file:
  2964. #+STARTUP: noptag"
  2965. :group 'org-tags
  2966. :type '(repeat
  2967. (choice
  2968. (cons (string :tag "Tag name")
  2969. (character :tag "Access char"))
  2970. (const :tag "Start radio group" (:startgroup))
  2971. (const :tag "Group tags delimiter" (:grouptags))
  2972. (const :tag "End radio group" (:endgroup))
  2973. (const :tag "New line" (:newline)))))
  2974. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2975. "If non-nil, always offer completion for all tags of all agenda files.
  2976. Instead of customizing this variable directly, you might want to
  2977. set it locally for capture buffers, because there no list of
  2978. tags in that file can be created dynamically (there are none).
  2979. (add-hook 'org-capture-mode-hook
  2980. (lambda ()
  2981. (set (make-local-variable
  2982. 'org-complete-tags-always-offer-all-agenda-tags)
  2983. t)))"
  2984. :group 'org-tags
  2985. :version "24.1"
  2986. :type 'boolean)
  2987. (defvar org-file-tags nil
  2988. "List of tags that can be inherited by all entries in the file.
  2989. The tags will be inherited if the variable `org-use-tag-inheritance'
  2990. says they should be.
  2991. This variable is populated from #+FILETAGS lines.")
  2992. (defcustom org-use-fast-tag-selection 'auto
  2993. "Non-nil means use fast tag selection scheme.
  2994. This is a special interface to select and deselect tags with single keys.
  2995. When nil, fast selection is never used.
  2996. When the symbol `auto', fast selection is used if and only if selection
  2997. characters for tags have been configured, either through the variable
  2998. `org-tag-alist' or through a #+TAGS line in the buffer.
  2999. When t, fast selection is always used and selection keys are assigned
  3000. automatically if necessary."
  3001. :group 'org-tags
  3002. :type '(choice
  3003. (const :tag "Always" t)
  3004. (const :tag "Never" nil)
  3005. (const :tag "When selection characters are configured" auto)))
  3006. (defcustom org-fast-tag-selection-single-key nil
  3007. "Non-nil means fast tag selection exits after first change.
  3008. When nil, you have to press RET to exit it.
  3009. During fast tag selection, you can toggle this flag with `C-c'.
  3010. This variable can also have the value `expert'. In this case, the window
  3011. displaying the tags menu is not even shown, until you press C-c again."
  3012. :group 'org-tags
  3013. :type '(choice
  3014. (const :tag "No" nil)
  3015. (const :tag "Yes" t)
  3016. (const :tag "Expert" expert)))
  3017. (defvar org-fast-tag-selection-include-todo nil
  3018. "Non-nil means fast tags selection interface will also offer TODO states.
  3019. This is an undocumented feature, you should not rely on it.")
  3020. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3021. "The column to which tags should be indented in a headline.
  3022. If this number is positive, it specifies the column. If it is negative,
  3023. it means that the tags should be flushright to that column. For example,
  3024. -80 works well for a normal 80 character screen.
  3025. When 0, place tags directly after headline text, with only one space in
  3026. between."
  3027. :group 'org-tags
  3028. :type 'integer)
  3029. (defcustom org-auto-align-tags t
  3030. "Non-nil keeps tags aligned when modifying headlines.
  3031. Some operations (i.e. demoting) change the length of a headline and
  3032. therefore shift the tags around. With this option turned on, after
  3033. each such operation the tags are again aligned to `org-tags-column'."
  3034. :group 'org-tags
  3035. :type 'boolean)
  3036. (defcustom org-use-tag-inheritance t
  3037. "Non-nil means tags in levels apply also for sublevels.
  3038. When nil, only the tags directly given in a specific line apply there.
  3039. This may also be a list of tags that should be inherited, or a regexp that
  3040. matches tags that should be inherited. Additional control is possible
  3041. with the variable `org-tags-exclude-from-inheritance' which gives an
  3042. explicit list of tags to be excluded from inheritance, even if the value of
  3043. `org-use-tag-inheritance' would select it for inheritance.
  3044. If this option is t, a match early-on in a tree can lead to a large
  3045. number of matches in the subtree when constructing the agenda or creating
  3046. a sparse tree. If you only want to see the first match in a tree during
  3047. a search, check out the variable `org-tags-match-list-sublevels'."
  3048. :group 'org-tags
  3049. :type '(choice
  3050. (const :tag "Not" nil)
  3051. (const :tag "Always" t)
  3052. (repeat :tag "Specific tags" (string :tag "Tag"))
  3053. (regexp :tag "Tags matched by regexp")))
  3054. (defcustom org-tags-exclude-from-inheritance nil
  3055. "List of tags that should never be inherited.
  3056. This is a way to exclude a few tags from inheritance. For way to do
  3057. the opposite, to actively allow inheritance for selected tags,
  3058. see the variable `org-use-tag-inheritance'."
  3059. :group 'org-tags
  3060. :type '(repeat (string :tag "Tag")))
  3061. (defun org-tag-inherit-p (tag)
  3062. "Check if TAG is one that should be inherited."
  3063. (cond
  3064. ((member tag org-tags-exclude-from-inheritance) nil)
  3065. ((eq org-use-tag-inheritance t) t)
  3066. ((not org-use-tag-inheritance) nil)
  3067. ((stringp org-use-tag-inheritance)
  3068. (string-match org-use-tag-inheritance tag))
  3069. ((listp org-use-tag-inheritance)
  3070. (member tag org-use-tag-inheritance))
  3071. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3072. (defcustom org-tags-match-list-sublevels t
  3073. "Non-nil means list also sublevels of headlines matching a search.
  3074. This variable applies to tags/property searches, and also to stuck
  3075. projects because this search is based on a tags match as well.
  3076. When set to the symbol `indented', sublevels are indented with
  3077. leading dots.
  3078. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3079. the sublevels of a headline matching a tag search often also match
  3080. the same search. Listing all of them can create very long lists.
  3081. Setting this variable to nil causes subtrees of a match to be skipped.
  3082. This variable is semi-obsolete and probably should always be true. It
  3083. is better to limit inheritance to certain tags using the variables
  3084. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3085. :group 'org-tags
  3086. :type '(choice
  3087. (const :tag "No, don't list them" nil)
  3088. (const :tag "Yes, do list them" t)
  3089. (const :tag "List them, indented with leading dots" indented)))
  3090. (defcustom org-tags-sort-function nil
  3091. "When set, tags are sorted using this function as a comparator."
  3092. :group 'org-tags
  3093. :type '(choice
  3094. (const :tag "No sorting" nil)
  3095. (const :tag "Alphabetical" string<)
  3096. (const :tag "Reverse alphabetical" string>)
  3097. (function :tag "Custom function" nil)))
  3098. (defvar org-tags-history nil
  3099. "History of minibuffer reads for tags.")
  3100. (defvar org-last-tags-completion-table nil
  3101. "The last used completion table for tags.")
  3102. (defvar org-after-tags-change-hook nil
  3103. "Hook that is run after the tags in a line have changed.")
  3104. (defgroup org-properties nil
  3105. "Options concerning properties in Org-mode."
  3106. :tag "Org Properties"
  3107. :group 'org)
  3108. (defcustom org-property-format "%-10s %s"
  3109. "How property key/value pairs should be formatted by `indent-line'.
  3110. When `indent-line' hits a property definition, it will format the line
  3111. according to this format, mainly to make sure that the values are
  3112. lined-up with respect to each other."
  3113. :group 'org-properties
  3114. :type 'string)
  3115. (defcustom org-properties-postprocess-alist nil
  3116. "Alist of properties and functions to adjust inserted values.
  3117. Elements of this alist must be of the form
  3118. ([string] [function])
  3119. where [string] must be a property name and [function] must be a
  3120. lambda expression: this lambda expression must take one argument,
  3121. the value to adjust, and return the new value as a string.
  3122. For example, this element will allow the property \"Remaining\"
  3123. to be updated wrt the relation between the \"Effort\" property
  3124. and the clock summary:
  3125. ((\"Remaining\" (lambda(value)
  3126. (let ((clocksum (org-clock-sum-current-item))
  3127. (effort (org-duration-string-to-minutes
  3128. (org-entry-get (point) \"Effort\"))))
  3129. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3130. :group 'org-properties
  3131. :version "24.1"
  3132. :type '(alist :key-type (string :tag "Property")
  3133. :value-type (function :tag "Function")))
  3134. (defcustom org-use-property-inheritance nil
  3135. "Non-nil means properties apply also for sublevels.
  3136. This setting is chiefly used during property searches. Turning it on can
  3137. cause significant overhead when doing a search, which is why it is not
  3138. on by default.
  3139. When nil, only the properties directly given in the current entry count.
  3140. When t, every property is inherited. The value may also be a list of
  3141. properties that should have inheritance, or a regular expression matching
  3142. properties that should be inherited.
  3143. However, note that some special properties use inheritance under special
  3144. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3145. and the properties ending in \"_ALL\" when they are used as descriptor
  3146. for valid values of a property.
  3147. Note for programmers:
  3148. When querying an entry with `org-entry-get', you can control if inheritance
  3149. should be used. By default, `org-entry-get' looks only at the local
  3150. properties. You can request inheritance by setting the inherit argument
  3151. to t (to force inheritance) or to `selective' (to respect the setting
  3152. in this variable)."
  3153. :group 'org-properties
  3154. :type '(choice
  3155. (const :tag "Not" nil)
  3156. (const :tag "Always" t)
  3157. (repeat :tag "Specific properties" (string :tag "Property"))
  3158. (regexp :tag "Properties matched by regexp")))
  3159. (defun org-property-inherit-p (property)
  3160. "Check if PROPERTY is one that should be inherited."
  3161. (cond
  3162. ((eq org-use-property-inheritance t) t)
  3163. ((not org-use-property-inheritance) nil)
  3164. ((stringp org-use-property-inheritance)
  3165. (string-match org-use-property-inheritance property))
  3166. ((listp org-use-property-inheritance)
  3167. (member property org-use-property-inheritance))
  3168. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3169. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3170. "The default column format, if no other format has been defined.
  3171. This variable can be set on the per-file basis by inserting a line
  3172. #+COLUMNS: %25ITEM ....."
  3173. :group 'org-properties
  3174. :type 'string)
  3175. (defcustom org-columns-ellipses ".."
  3176. "The ellipses to be used when a field in column view is truncated.
  3177. When this is the empty string, as many characters as possible are shown,
  3178. but then there will be no visual indication that the field has been truncated.
  3179. When this is a string of length N, the last N characters of a truncated
  3180. field are replaced by this string. If the column is narrower than the
  3181. ellipses string, only part of the ellipses string will be shown."
  3182. :group 'org-properties
  3183. :type 'string)
  3184. (defcustom org-columns-modify-value-for-display-function nil
  3185. "Function that modifies values for display in column view.
  3186. For example, it can be used to cut out a certain part from a time stamp.
  3187. The function must take 2 arguments:
  3188. column-title The title of the column (*not* the property name)
  3189. value The value that should be modified.
  3190. The function should return the value that should be displayed,
  3191. or nil if the normal value should be used."
  3192. :group 'org-properties
  3193. :type '(choice (const nil) (function)))
  3194. (defconst org-global-properties-fixed
  3195. '(("VISIBILITY_ALL" . "folded children content all")
  3196. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3197. "List of property/value pairs that can be inherited by any entry.
  3198. These are fixed values, for the preset properties. The user variable
  3199. that can be used to add to this list is `org-global-properties'.
  3200. The entries in this list are cons cells where the car is a property
  3201. name and cdr is a string with the value. If the value represents
  3202. multiple items like an \"_ALL\" property, separate the items by
  3203. spaces.")
  3204. (defcustom org-global-properties nil
  3205. "List of property/value pairs that can be inherited by any entry.
  3206. This list will be combined with the constant `org-global-properties-fixed'.
  3207. The entries in this list are cons cells where the car is a property
  3208. name and cdr is a string with the value.
  3209. You can set buffer-local values for the same purpose in the variable
  3210. `org-file-properties' this by adding lines like
  3211. #+PROPERTY: NAME VALUE"
  3212. :group 'org-properties
  3213. :type '(repeat
  3214. (cons (string :tag "Property")
  3215. (string :tag "Value"))))
  3216. (defvar org-file-properties nil
  3217. "List of property/value pairs that can be inherited by any entry.
  3218. Valid for the current buffer.
  3219. This variable is populated from #+PROPERTY lines.")
  3220. (make-variable-buffer-local 'org-file-properties)
  3221. (defgroup org-agenda nil
  3222. "Options concerning agenda views in Org-mode."
  3223. :tag "Org Agenda"
  3224. :group 'org)
  3225. (defvar org-category nil
  3226. "Variable used by org files to set a category for agenda display.
  3227. Such files should use a file variable to set it, for example
  3228. # -*- mode: org; org-category: \"ELisp\"
  3229. or contain a special line
  3230. #+CATEGORY: ELisp
  3231. If the file does not specify a category, then file's base name
  3232. is used instead.")
  3233. (make-variable-buffer-local 'org-category)
  3234. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3235. (defcustom org-agenda-files nil
  3236. "The files to be used for agenda display.
  3237. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3238. \\[org-remove-file]. You can also use customize to edit the list.
  3239. If an entry is a directory, all files in that directory that are matched by
  3240. `org-agenda-file-regexp' will be part of the file list.
  3241. If the value of the variable is not a list but a single file name, then
  3242. the list of agenda files is actually stored and maintained in that file, one
  3243. agenda file per line. In this file paths can be given relative to
  3244. `org-directory'. Tilde expansion and environment variable substitution
  3245. are also made."
  3246. :group 'org-agenda
  3247. :type '(choice
  3248. (repeat :tag "List of files and directories" file)
  3249. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3250. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3251. "Regular expression to match files for `org-agenda-files'.
  3252. If any element in the list in that variable contains a directory instead
  3253. of a normal file, all files in that directory that are matched by this
  3254. regular expression will be included."
  3255. :group 'org-agenda
  3256. :type 'regexp)
  3257. (defcustom org-agenda-text-search-extra-files nil
  3258. "List of extra files to be searched by text search commands.
  3259. These files will be searched in addition to the agenda files by the
  3260. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3261. Note that these files will only be searched for text search commands,
  3262. not for the other agenda views like todo lists, tag searches or the weekly
  3263. agenda. This variable is intended to list notes and possibly archive files
  3264. that should also be searched by these two commands.
  3265. In fact, if the first element in the list is the symbol `agenda-archives',
  3266. then all archive files of all agenda files will be added to the search
  3267. scope."
  3268. :group 'org-agenda
  3269. :type '(set :greedy t
  3270. (const :tag "Agenda Archives" agenda-archives)
  3271. (repeat :inline t (file))))
  3272. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3273. 'org-agenda-text-search-extra-files)
  3274. (defcustom org-agenda-skip-unavailable-files nil
  3275. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3276. A nil value means to remove them, after a query, from the list."
  3277. :group 'org-agenda
  3278. :type 'boolean)
  3279. (defcustom org-calendar-to-agenda-key [?c]
  3280. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3281. The command `org-calendar-goto-agenda' will be bound to this key. The
  3282. default is the character `c' because then `c' can be used to switch back and
  3283. forth between agenda and calendar."
  3284. :group 'org-agenda
  3285. :type 'sexp)
  3286. (defcustom org-calendar-insert-diary-entry-key [?i]
  3287. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3288. This option is irrelevant until `org-agenda-diary-file' has been configured
  3289. to point to an Org-mode file. When that is the case, the command
  3290. `org-agenda-diary-entry' will be bound to the key given here, by default
  3291. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3292. if you want to continue doing this, you need to change this to a different
  3293. key."
  3294. :group 'org-agenda
  3295. :type 'sexp)
  3296. (defcustom org-agenda-diary-file 'diary-file
  3297. "File to which to add new entries with the `i' key in agenda and calendar.
  3298. When this is the symbol `diary-file', the functionality in the Emacs
  3299. calendar will be used to add entries to the `diary-file'. But when this
  3300. points to a file, `org-agenda-diary-entry' will be used instead."
  3301. :group 'org-agenda
  3302. :type '(choice
  3303. (const :tag "The standard Emacs diary file" diary-file)
  3304. (file :tag "Special Org file diary entries")))
  3305. (eval-after-load "calendar"
  3306. '(progn
  3307. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3308. 'org-calendar-goto-agenda)
  3309. (add-hook 'calendar-mode-hook
  3310. (lambda ()
  3311. (unless (eq org-agenda-diary-file 'diary-file)
  3312. (define-key calendar-mode-map
  3313. org-calendar-insert-diary-entry-key
  3314. 'org-agenda-diary-entry))))))
  3315. (defgroup org-latex nil
  3316. "Options for embedding LaTeX code into Org-mode."
  3317. :tag "Org LaTeX"
  3318. :group 'org)
  3319. (defcustom org-format-latex-options
  3320. '(:foreground default :background default :scale 1.0
  3321. :html-foreground "Black" :html-background "Transparent"
  3322. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3323. "Options for creating images from LaTeX fragments.
  3324. This is a property list with the following properties:
  3325. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3326. `default' means use the foreground of the default face.
  3327. `auto' means use the foreground from the text face.
  3328. :background the background color, or \"Transparent\".
  3329. `default' means use the background of the default face.
  3330. `auto' means use the background from the text face.
  3331. :scale a scaling factor for the size of the images, to get more pixels
  3332. :html-foreground, :html-background, :html-scale
  3333. the same numbers for HTML export.
  3334. :matchers a list indicating which matchers should be used to
  3335. find LaTeX fragments. Valid members of this list are:
  3336. \"begin\" find environments
  3337. \"$1\" find single characters surrounded by $.$
  3338. \"$\" find math expressions surrounded by $...$
  3339. \"$$\" find math expressions surrounded by $$....$$
  3340. \"\\(\" find math expressions surrounded by \\(...\\)
  3341. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3342. :group 'org-latex
  3343. :type 'plist)
  3344. (defcustom org-format-latex-signal-error t
  3345. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3346. When nil, just push out a message."
  3347. :group 'org-latex
  3348. :version "24.1"
  3349. :type 'boolean)
  3350. (defcustom org-latex-to-mathml-jar-file nil
  3351. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3352. Use this to specify additional executable file say a jar file.
  3353. When using MathToWeb as the converter, specify the full-path to
  3354. your mathtoweb.jar file."
  3355. :group 'org-latex
  3356. :version "24.1"
  3357. :type '(choice
  3358. (const :tag "None" nil)
  3359. (file :tag "JAR file" :must-match t)))
  3360. (defcustom org-latex-to-mathml-convert-command nil
  3361. "Command to convert LaTeX fragments to MathML.
  3362. Replace format-specifiers in the command as noted below and use
  3363. `shell-command' to convert LaTeX to MathML.
  3364. %j: Executable file in fully expanded form as specified by
  3365. `org-latex-to-mathml-jar-file'.
  3366. %I: Input LaTeX file in fully expanded form
  3367. %o: Output MathML file
  3368. This command is used by `org-create-math-formula'.
  3369. When using MathToWeb as the converter, set this to
  3370. \"java -jar %j -unicode -force -df %o %I\"."
  3371. :group 'org-latex
  3372. :version "24.1"
  3373. :type '(choice
  3374. (const :tag "None" nil)
  3375. (string :tag "\nShell command")))
  3376. (defcustom org-latex-create-formula-image-program 'dvipng
  3377. "Program to convert LaTeX fragments with.
  3378. dvipng Process the LaTeX fragments to dvi file, then convert
  3379. dvi files to png files using dvipng.
  3380. This will also include processing of non-math environments.
  3381. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3382. to convert pdf files to png files"
  3383. :group 'org-latex
  3384. :version "24.1"
  3385. :type '(choice
  3386. (const :tag "dvipng" dvipng)
  3387. (const :tag "imagemagick" imagemagick)))
  3388. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3389. "Path to store latex preview images.
  3390. A relative path here creates many directories relative to the
  3391. processed org files paths. An absolute path puts all preview
  3392. images at the same place."
  3393. :group 'org-latex
  3394. :version "24.3"
  3395. :type 'string)
  3396. (defun org-format-latex-mathml-available-p ()
  3397. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3398. (save-match-data
  3399. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3400. org-latex-to-mathml-convert-command)
  3401. (let ((executable (car (split-string
  3402. org-latex-to-mathml-convert-command))))
  3403. (when (executable-find executable)
  3404. (if (string-match
  3405. "%j" org-latex-to-mathml-convert-command)
  3406. (file-readable-p org-latex-to-mathml-jar-file)
  3407. t))))))
  3408. (defcustom org-format-latex-header "\\documentclass{article}
  3409. \\usepackage[usenames]{color}
  3410. \[PACKAGES]
  3411. \[DEFAULT-PACKAGES]
  3412. \\pagestyle{empty} % do not remove
  3413. % The settings below are copied from fullpage.sty
  3414. \\setlength{\\textwidth}{\\paperwidth}
  3415. \\addtolength{\\textwidth}{-3cm}
  3416. \\setlength{\\oddsidemargin}{1.5cm}
  3417. \\addtolength{\\oddsidemargin}{-2.54cm}
  3418. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3419. \\setlength{\\textheight}{\\paperheight}
  3420. \\addtolength{\\textheight}{-\\headheight}
  3421. \\addtolength{\\textheight}{-\\headsep}
  3422. \\addtolength{\\textheight}{-\\footskip}
  3423. \\addtolength{\\textheight}{-3cm}
  3424. \\setlength{\\topmargin}{1.5cm}
  3425. \\addtolength{\\topmargin}{-2.54cm}"
  3426. "The document header used for processing LaTeX fragments.
  3427. It is imperative that this header make sure that no page number
  3428. appears on the page. The package defined in the variables
  3429. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3430. will either replace the placeholder \"[PACKAGES]\" in this
  3431. header, or they will be appended."
  3432. :group 'org-latex
  3433. :type 'string)
  3434. (defun org-set-packages-alist (var val)
  3435. "Set the packages alist and make sure it has 3 elements per entry."
  3436. (set var (mapcar (lambda (x)
  3437. (if (and (consp x) (= (length x) 2))
  3438. (list (car x) (nth 1 x) t)
  3439. x))
  3440. val)))
  3441. (defun org-get-packages-alist (var)
  3442. "Get the packages alist and make sure it has 3 elements per entry."
  3443. (mapcar (lambda (x)
  3444. (if (and (consp x) (= (length x) 2))
  3445. (list (car x) (nth 1 x) t)
  3446. x))
  3447. (default-value var)))
  3448. (defcustom org-latex-default-packages-alist
  3449. '(("AUTO" "inputenc" t)
  3450. ("T1" "fontenc" t)
  3451. ("" "fixltx2e" nil)
  3452. ("" "graphicx" t)
  3453. ("" "longtable" nil)
  3454. ("" "float" nil)
  3455. ("" "wrapfig" nil)
  3456. ("" "rotating" nil)
  3457. ("normalem" "ulem" t)
  3458. ("" "amsmath" t)
  3459. ("" "textcomp" t)
  3460. ("" "marvosym" t)
  3461. ("" "wasysym" t)
  3462. ("" "amssymb" t)
  3463. ("" "hyperref" nil)
  3464. "\\tolerance=1000")
  3465. "Alist of default packages to be inserted in the header.
  3466. Change this only if one of the packages here causes an
  3467. incompatibility with another package you are using.
  3468. The packages in this list are needed by one part or another of
  3469. Org mode to function properly:
  3470. - inputenc, fontenc: for basic font and character selection
  3471. - fixltx2e: Important patches of LaTeX itself
  3472. - graphicx: for including images
  3473. - longtable: For multipage tables
  3474. - float, wrapfig: for figure placement
  3475. - rotating: for sideways figures and tables
  3476. - ulem: for underline and strike-through
  3477. - amsmath: for subscript and superscript and math environments
  3478. - textcomp, marvosymb, wasysym, amssymb: for various symbols used
  3479. for interpreting the entities in `org-entities'. You can skip
  3480. some of these packages if you don't use any of their symbols.
  3481. - hyperref: for cross references
  3482. Therefore you should not modify this variable unless you know
  3483. what you are doing. The one reason to change it anyway is that
  3484. you might be loading some other package that conflicts with one
  3485. of the default packages. Each element is either a cell or
  3486. a string.
  3487. A cell is of the format:
  3488. \( \"options\" \"package\" SNIPPET-FLAG).
  3489. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3490. when compiling LaTeX snippets into images for inclusion into
  3491. non-LaTeX output.
  3492. A string will be inserted as-is in the header of the document."
  3493. :group 'org-latex
  3494. :group 'org-export-latex
  3495. :set 'org-set-packages-alist
  3496. :get 'org-get-packages-alist
  3497. :version "24.1"
  3498. :type '(repeat
  3499. (choice
  3500. (list :tag "options/package pair"
  3501. (string :tag "options")
  3502. (string :tag "package")
  3503. (boolean :tag "Snippet"))
  3504. (string :tag "A line of LaTeX"))))
  3505. (defcustom org-latex-packages-alist nil
  3506. "Alist of packages to be inserted in every LaTeX header.
  3507. These will be inserted after `org-latex-default-packages-alist'.
  3508. Each element is either a cell or a string.
  3509. A cell is of the format:
  3510. \(\"options\" \"package\" SNIPPET-FLAG)
  3511. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3512. needed when turning LaTeX snippets into images for inclusion into
  3513. non-LaTeX output.
  3514. A string will be inserted as-is in the header of the document.
  3515. Make sure that you only list packages here which:
  3516. - you want in every file;
  3517. - do not conflict with the setup in `org-format-latex-header';
  3518. - do not conflict with the default packages in
  3519. `org-latex-default-packages-alist'."
  3520. :group 'org-latex
  3521. :group 'org-export-latex
  3522. :set 'org-set-packages-alist
  3523. :get 'org-get-packages-alist
  3524. :type '(repeat
  3525. (choice
  3526. (list :tag "options/package pair"
  3527. (string :tag "options")
  3528. (string :tag "package")
  3529. (boolean :tag "Snippet"))
  3530. (string :tag "A line of LaTeX"))))
  3531. (defgroup org-appearance nil
  3532. "Settings for Org-mode appearance."
  3533. :tag "Org Appearance"
  3534. :group 'org)
  3535. (defcustom org-level-color-stars-only nil
  3536. "Non-nil means fontify only the stars in each headline.
  3537. When nil, the entire headline is fontified.
  3538. Changing it requires restart of `font-lock-mode' to become effective
  3539. also in regions already fontified."
  3540. :group 'org-appearance
  3541. :type 'boolean)
  3542. (defcustom org-hide-leading-stars nil
  3543. "Non-nil means hide the first N-1 stars in a headline.
  3544. This works by using the face `org-hide' for these stars. This
  3545. face is white for a light background, and black for a dark
  3546. background. You may have to customize the face `org-hide' to
  3547. make this work.
  3548. Changing it requires restart of `font-lock-mode' to become effective
  3549. also in regions already fontified.
  3550. You may also set this on a per-file basis by adding one of the following
  3551. lines to the buffer:
  3552. #+STARTUP: hidestars
  3553. #+STARTUP: showstars"
  3554. :group 'org-appearance
  3555. :type 'boolean)
  3556. (defcustom org-hidden-keywords nil
  3557. "List of symbols corresponding to keywords to be hidden the org buffer.
  3558. For example, a value '(title) for this list will make the document's title
  3559. appear in the buffer without the initial #+TITLE: keyword."
  3560. :group 'org-appearance
  3561. :version "24.1"
  3562. :type '(set (const :tag "#+AUTHOR" author)
  3563. (const :tag "#+DATE" date)
  3564. (const :tag "#+EMAIL" email)
  3565. (const :tag "#+TITLE" title)))
  3566. (defcustom org-custom-properties nil
  3567. "List of properties (as strings) with a special meaning.
  3568. The default use of these custom properties is to let the user
  3569. hide them with `org-toggle-custom-properties-visibility'."
  3570. :group 'org-properties
  3571. :group 'org-appearance
  3572. :version "24.3"
  3573. :type '(repeat (string :tag "Property Name")))
  3574. (defcustom org-fontify-done-headline nil
  3575. "Non-nil means change the face of a headline if it is marked DONE.
  3576. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3577. When this is non-nil, the headline after the keyword is set to the
  3578. `org-headline-done' as an additional indication."
  3579. :group 'org-appearance
  3580. :type 'boolean)
  3581. (defcustom org-fontify-emphasized-text t
  3582. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3583. Changing this variable requires a restart of Emacs to take effect."
  3584. :group 'org-appearance
  3585. :type 'boolean)
  3586. (defcustom org-fontify-whole-heading-line nil
  3587. "Non-nil means fontify the whole line for headings.
  3588. This is useful when setting a background color for the
  3589. org-level-* faces."
  3590. :group 'org-appearance
  3591. :type 'boolean)
  3592. (defcustom org-highlight-latex-and-related nil
  3593. "Non-nil means highlight LaTeX related syntax in the buffer.
  3594. When non nil, the value should be a list containing any of the
  3595. following symbols:
  3596. `latex' Highlight LaTeX snippets and environments.
  3597. `script' Highlight subscript and superscript.
  3598. `entities' Highlight entities."
  3599. :group 'org-appearance
  3600. :version "24.4"
  3601. :package-version '(Org . "8.0")
  3602. :type '(choice
  3603. (const :tag "No highlighting" nil)
  3604. (set :greedy t :tag "Highlight"
  3605. (const :tag "LaTeX snippets and environments" latex)
  3606. (const :tag "Subscript and superscript" script)
  3607. (const :tag "Entities" entities))))
  3608. (defcustom org-hide-emphasis-markers nil
  3609. "Non-nil mean font-lock should hide the emphasis marker characters."
  3610. :group 'org-appearance
  3611. :type 'boolean)
  3612. (defcustom org-hide-macro-markers nil
  3613. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3614. :group 'org-appearance
  3615. :type 'boolean)
  3616. (defcustom org-pretty-entities nil
  3617. "Non-nil means show entities as UTF8 characters.
  3618. When nil, the \\name form remains in the buffer."
  3619. :group 'org-appearance
  3620. :version "24.1"
  3621. :type 'boolean)
  3622. (defcustom org-pretty-entities-include-sub-superscripts t
  3623. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3624. :group 'org-appearance
  3625. :version "24.1"
  3626. :type 'boolean)
  3627. (defvar org-emph-re nil
  3628. "Regular expression for matching emphasis.
  3629. After a match, the match groups contain these elements:
  3630. 0 The match of the full regular expression, including the characters
  3631. before and after the proper match
  3632. 1 The character before the proper match, or empty at beginning of line
  3633. 2 The proper match, including the leading and trailing markers
  3634. 3 The leading marker like * or /, indicating the type of highlighting
  3635. 4 The text between the emphasis markers, not including the markers
  3636. 5 The character after the match, empty at the end of a line")
  3637. (defvar org-verbatim-re nil
  3638. "Regular expression for matching verbatim text.")
  3639. (defvar org-emphasis-regexp-components) ; defined just below
  3640. (defvar org-emphasis-alist) ; defined just below
  3641. (defun org-set-emph-re (var val)
  3642. "Set variable and compute the emphasis regular expression."
  3643. (set var val)
  3644. (when (and (boundp 'org-emphasis-alist)
  3645. (boundp 'org-emphasis-regexp-components)
  3646. org-emphasis-alist org-emphasis-regexp-components)
  3647. (let* ((e org-emphasis-regexp-components)
  3648. (pre (car e))
  3649. (post (nth 1 e))
  3650. (border (nth 2 e))
  3651. (body (nth 3 e))
  3652. (nl (nth 4 e))
  3653. (body1 (concat body "*?"))
  3654. (markers (mapconcat 'car org-emphasis-alist ""))
  3655. (vmarkers (mapconcat
  3656. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3657. org-emphasis-alist "")))
  3658. ;; make sure special characters appear at the right position in the class
  3659. (if (string-match "\\^" markers)
  3660. (setq markers (concat (replace-match "" t t markers) "^")))
  3661. (if (string-match "-" markers)
  3662. (setq markers (concat (replace-match "" t t markers) "-")))
  3663. (if (string-match "\\^" vmarkers)
  3664. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3665. (if (string-match "-" vmarkers)
  3666. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3667. (if (> nl 0)
  3668. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3669. (int-to-string nl) "\\}")))
  3670. ;; Make the regexp
  3671. (setq org-emph-re
  3672. (concat "\\([" pre "]\\|^\\)"
  3673. "\\("
  3674. "\\([" markers "]\\)"
  3675. "\\("
  3676. "[^" border "]\\|"
  3677. "[^" border "]"
  3678. body1
  3679. "[^" border "]"
  3680. "\\)"
  3681. "\\3\\)"
  3682. "\\([" post "]\\|$\\)"))
  3683. (setq org-verbatim-re
  3684. (concat "\\([" pre "]\\|^\\)"
  3685. "\\("
  3686. "\\([" vmarkers "]\\)"
  3687. "\\("
  3688. "[^" border "]\\|"
  3689. "[^" border "]"
  3690. body1
  3691. "[^" border "]"
  3692. "\\)"
  3693. "\\3\\)"
  3694. "\\([" post "]\\|$\\)")))))
  3695. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3696. ;; set this option proved cumbersome. See this message/thread:
  3697. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3698. (defvar org-emphasis-regexp-components
  3699. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n,\"'" "." 1)
  3700. "Components used to build the regular expression for emphasis.
  3701. This is a list with five entries. Terminology: In an emphasis string
  3702. like \" *strong word* \", we call the initial space PREMATCH, the final
  3703. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3704. and \"trong wor\" is the body. The different components in this variable
  3705. specify what is allowed/forbidden in each part:
  3706. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3707. post Chars allowed as postmatch. End of line will be allowed too.
  3708. border The chars *forbidden* as border characters.
  3709. body-regexp A regexp like \".\" to match a body character. Don't use
  3710. non-shy groups here, and don't allow newline here.
  3711. newline The maximum number of newlines allowed in an emphasis exp.
  3712. You need to reload Org or to restart Emacs after customizing this.")
  3713. (defcustom org-emphasis-alist
  3714. `(("*" bold)
  3715. ("/" italic)
  3716. ("_" underline)
  3717. ("=" org-verbatim verbatim)
  3718. ("~" org-code verbatim)
  3719. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3720. "Alist of characters and faces to emphasize text.
  3721. Text starting and ending with a special character will be emphasized,
  3722. for example *bold*, _underlined_ and /italic/. This variable sets the
  3723. marker characters and the face to be used by font-lock for highlighting
  3724. in Org-mode Emacs buffers.
  3725. You need to reload Org or to restart Emacs after customizing this."
  3726. :group 'org-appearance
  3727. :set 'org-set-emph-re
  3728. :version "24.4"
  3729. :package-version '(Org . "8.0")
  3730. :type '(repeat
  3731. (list
  3732. (string :tag "Marker character")
  3733. (choice
  3734. (face :tag "Font-lock-face")
  3735. (plist :tag "Face property list"))
  3736. (option (const verbatim)))))
  3737. (defvar org-protecting-blocks
  3738. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3739. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3740. This is needed for font-lock setup.")
  3741. ;;; Miscellaneous options
  3742. (defgroup org-completion nil
  3743. "Completion in Org-mode."
  3744. :tag "Org Completion"
  3745. :group 'org)
  3746. (defcustom org-completion-use-ido nil
  3747. "Non-nil means use ido completion wherever possible.
  3748. Note that `ido-mode' must be active for this variable to be relevant.
  3749. If you decide to turn this variable on, you might well want to turn off
  3750. `org-outline-path-complete-in-steps'.
  3751. See also `org-completion-use-iswitchb'."
  3752. :group 'org-completion
  3753. :type 'boolean)
  3754. (defcustom org-completion-use-iswitchb nil
  3755. "Non-nil means use iswitchb completion wherever possible.
  3756. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3757. If you decide to turn this variable on, you might well want to turn off
  3758. `org-outline-path-complete-in-steps'.
  3759. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3760. :group 'org-completion
  3761. :type 'boolean)
  3762. (defcustom org-completion-fallback-command 'hippie-expand
  3763. "The expansion command called by \\[pcomplete] in normal context.
  3764. Normal means, no org-mode-specific context."
  3765. :group 'org-completion
  3766. :type 'function)
  3767. ;;; Functions and variables from their packages
  3768. ;; Declared here to avoid compiler warnings
  3769. ;; XEmacs only
  3770. (defvar outline-mode-menu-heading)
  3771. (defvar outline-mode-menu-show)
  3772. (defvar outline-mode-menu-hide)
  3773. (defvar zmacs-regions) ; XEmacs regions
  3774. ;; Emacs only
  3775. (defvar mark-active)
  3776. ;; Various packages
  3777. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3778. (declare-function calendar-forward-day "cal-move" (arg))
  3779. (declare-function calendar-goto-date "cal-move" (date))
  3780. (declare-function calendar-goto-today "cal-move" ())
  3781. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3782. (defvar calc-embedded-close-formula)
  3783. (defvar calc-embedded-open-formula)
  3784. (declare-function cdlatex-tab "ext:cdlatex" ())
  3785. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3786. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3787. (defvar font-lock-unfontify-region-function)
  3788. (declare-function iswitchb-read-buffer "iswitchb"
  3789. (prompt &optional default require-match start matches-set))
  3790. (defvar iswitchb-temp-buflist)
  3791. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3792. (defvar org-agenda-tags-todo-honor-ignore-options)
  3793. (declare-function org-agenda-skip "org-agenda" ())
  3794. (declare-function
  3795. org-agenda-format-item "org-agenda"
  3796. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3797. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3798. (declare-function org-agenda-change-all-lines "org-agenda"
  3799. (newhead hdmarker &optional fixface just-this))
  3800. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3801. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3802. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3803. (beg end))
  3804. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3805. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3806. "org-agenda" (&optional end))
  3807. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3808. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3809. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3810. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3811. (declare-function org-indent-mode "org-indent" (&optional arg))
  3812. (declare-function parse-time-string "parse-time" (string))
  3813. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3814. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3815. (defvar remember-data-file)
  3816. (defvar texmathp-why)
  3817. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3818. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3819. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3820. ;;;###autoload
  3821. (defun turn-on-orgtbl ()
  3822. "Unconditionally turn on `orgtbl-mode'."
  3823. (require 'org-table)
  3824. (orgtbl-mode 1))
  3825. (defun org-at-table-p (&optional table-type)
  3826. "Return t if the cursor is inside an org-type table.
  3827. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3828. (if org-enable-table-editor
  3829. (save-excursion
  3830. (beginning-of-line 1)
  3831. (looking-at (if table-type org-table-any-line-regexp
  3832. org-table-line-regexp)))
  3833. nil))
  3834. (defsubst org-table-p () (org-at-table-p))
  3835. (defun org-at-table.el-p ()
  3836. "Return t if and only if we are at a table.el table."
  3837. (and (org-at-table-p 'any)
  3838. (save-excursion
  3839. (goto-char (org-table-begin 'any))
  3840. (looking-at org-table1-hline-regexp))))
  3841. (defun org-table-recognize-table.el ()
  3842. "If there is a table.el table nearby, recognize it and move into it."
  3843. (if org-table-tab-recognizes-table.el
  3844. (if (org-at-table.el-p)
  3845. (progn
  3846. (beginning-of-line 1)
  3847. (if (looking-at org-table-dataline-regexp)
  3848. nil
  3849. (if (looking-at org-table1-hline-regexp)
  3850. (progn
  3851. (beginning-of-line 2)
  3852. (if (looking-at org-table-any-border-regexp)
  3853. (beginning-of-line -1)))))
  3854. (if (re-search-forward "|" (org-table-end t) t)
  3855. (progn
  3856. (require 'table)
  3857. (if (table--at-cell-p (point))
  3858. t
  3859. (message "recognizing table.el table...")
  3860. (table-recognize-table)
  3861. (message "recognizing table.el table...done")))
  3862. (error "This should not happen"))
  3863. t)
  3864. nil)
  3865. nil))
  3866. (defun org-at-table-hline-p ()
  3867. "Return t if the cursor is inside a hline in a table."
  3868. (if org-enable-table-editor
  3869. (save-excursion
  3870. (beginning-of-line 1)
  3871. (looking-at org-table-hline-regexp))
  3872. nil))
  3873. (defun org-table-map-tables (function &optional quietly)
  3874. "Apply FUNCTION to the start of all tables in the buffer."
  3875. (save-excursion
  3876. (save-restriction
  3877. (widen)
  3878. (goto-char (point-min))
  3879. (while (re-search-forward org-table-any-line-regexp nil t)
  3880. (unless quietly
  3881. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3882. (beginning-of-line 1)
  3883. (when (and (looking-at org-table-line-regexp)
  3884. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3885. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3886. (save-excursion (funcall function))
  3887. (or (looking-at org-table-line-regexp)
  3888. (forward-char 1)))
  3889. (re-search-forward org-table-any-border-regexp nil 1))))
  3890. (unless quietly (message "Mapping tables: done")))
  3891. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3892. (declare-function org-clock-update-mode-line "org-clock" ())
  3893. (declare-function org-resolve-clocks "org-clock"
  3894. (&optional also-non-dangling-p prompt last-valid))
  3895. (defun org-at-TBLFM-p (&optional pos)
  3896. "Return t when point (or POS) is in #+TBLFM line."
  3897. (save-excursion
  3898. (let ((pos pos)))
  3899. (goto-char (or pos (point)))
  3900. (beginning-of-line 1)
  3901. (looking-at org-TBLFM-regexp)))
  3902. (defvar org-clock-start-time)
  3903. (defvar org-clock-marker (make-marker)
  3904. "Marker recording the last clock-in.")
  3905. (defvar org-clock-hd-marker (make-marker)
  3906. "Marker recording the last clock-in, but the headline position.")
  3907. (defvar org-clock-heading ""
  3908. "The heading of the current clock entry.")
  3909. (defun org-clock-is-active ()
  3910. "Return the buffer where the clock is currently running.
  3911. Return nil if no clock is running."
  3912. (marker-buffer org-clock-marker))
  3913. (defun org-check-running-clock ()
  3914. "Check if the current buffer contains the running clock.
  3915. If yes, offer to stop it and to save the buffer with the changes."
  3916. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3917. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3918. (buffer-name))))
  3919. (org-clock-out)
  3920. (when (y-or-n-p "Save changed buffer?")
  3921. (save-buffer))))
  3922. (defun org-clocktable-try-shift (dir n)
  3923. "Check if this line starts a clock table, if yes, shift the time block."
  3924. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3925. (org-clocktable-shift dir n)))
  3926. ;;;###autoload
  3927. (defun org-clock-persistence-insinuate ()
  3928. "Set up hooks for clock persistence."
  3929. (require 'org-clock)
  3930. (add-hook 'org-mode-hook 'org-clock-load)
  3931. (add-hook 'kill-emacs-hook 'org-clock-save))
  3932. (defgroup org-archive nil
  3933. "Options concerning archiving in Org-mode."
  3934. :tag "Org Archive"
  3935. :group 'org-structure)
  3936. (defcustom org-archive-location "%s_archive::"
  3937. "The location where subtrees should be archived.
  3938. The value of this variable is a string, consisting of two parts,
  3939. separated by a double-colon. The first part is a filename and
  3940. the second part is a headline.
  3941. When the filename is omitted, archiving happens in the same file.
  3942. %s in the filename will be replaced by the current file
  3943. name (without the directory part). Archiving to a different file
  3944. is useful to keep archived entries from contributing to the
  3945. Org-mode Agenda.
  3946. The archived entries will be filed as subtrees of the specified
  3947. headline. When the headline is omitted, the subtrees are simply
  3948. filed away at the end of the file, as top-level entries. Also in
  3949. the heading you can use %s to represent the file name, this can be
  3950. useful when using the same archive for a number of different files.
  3951. Here are a few examples:
  3952. \"%s_archive::\"
  3953. If the current file is Projects.org, archive in file
  3954. Projects.org_archive, as top-level trees. This is the default.
  3955. \"::* Archived Tasks\"
  3956. Archive in the current file, under the top-level headline
  3957. \"* Archived Tasks\".
  3958. \"~/org/archive.org::\"
  3959. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3960. \"~/org/archive.org::* From %s\"
  3961. Archive in file ~/org/archive.org (absolute path), under headlines
  3962. \"From FILENAME\" where file name is the current file name.
  3963. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3964. The \"datetree/\" string is special, signifying to archive
  3965. items to the datetree. Items are placed in either the CLOSED
  3966. date of the item, or the current date if there is no CLOSED date.
  3967. The heading will be a subentry to the current date. There doesn't
  3968. need to be a heading, but there always needs to be a slash after
  3969. datetree. For example, to store archived items directly in the
  3970. datetree, use \"~/org/datetree.org::datetree/\".
  3971. \"basement::** Finished Tasks\"
  3972. Archive in file ./basement (relative path), as level 3 trees
  3973. below the level 2 heading \"** Finished Tasks\".
  3974. You may set this option on a per-file basis by adding to the buffer a
  3975. line like
  3976. #+ARCHIVE: basement::** Finished Tasks
  3977. You may also define it locally for a subtree by setting an ARCHIVE property
  3978. in the entry. If such a property is found in an entry, or anywhere up
  3979. the hierarchy, it will be used."
  3980. :group 'org-archive
  3981. :type 'string)
  3982. (defcustom org-agenda-skip-archived-trees t
  3983. "Non-nil means the agenda will skip any items located in archived trees.
  3984. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3985. variable is no longer recommended, you should leave it at the value t.
  3986. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3987. :group 'org-archive
  3988. :group 'org-agenda-skip
  3989. :type 'boolean)
  3990. (defcustom org-columns-skip-archived-trees t
  3991. "Non-nil means ignore archived trees when creating column view."
  3992. :group 'org-archive
  3993. :group 'org-properties
  3994. :type 'boolean)
  3995. (defcustom org-cycle-open-archived-trees nil
  3996. "Non-nil means `org-cycle' will open archived trees.
  3997. An archived tree is a tree marked with the tag ARCHIVE.
  3998. When nil, archived trees will stay folded. You can still open them with
  3999. normal outline commands like `show-all', but not with the cycling commands."
  4000. :group 'org-archive
  4001. :group 'org-cycle
  4002. :type 'boolean)
  4003. (defcustom org-sparse-tree-open-archived-trees nil
  4004. "Non-nil means sparse tree construction shows matches in archived trees.
  4005. When nil, matches in these trees are highlighted, but the trees are kept in
  4006. collapsed state."
  4007. :group 'org-archive
  4008. :group 'org-sparse-trees
  4009. :type 'boolean)
  4010. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  4011. "The default date type when building a sparse tree.
  4012. When this is nil, a date is a scheduled or a deadline timestamp.
  4013. Otherwise, these types are allowed:
  4014. all: all timestamps
  4015. active: only active timestamps (<...>)
  4016. inactive: only inactive timestamps (<...)
  4017. scheduled: only scheduled timestamps
  4018. deadline: only deadline timestamps"
  4019. :type '(choice (const :tag "Scheduled or deadline" scheduled-or-deadline)
  4020. (const :tag "All timestamps" all)
  4021. (const :tag "Only active timestamps" active)
  4022. (const :tag "Only inactive timestamps" inactive)
  4023. (const :tag "Only scheduled timestamps" scheduled)
  4024. (const :tag "Only deadline timestamps" deadline)
  4025. (const :tag "Only closed timestamps" closed))
  4026. :version "24.3"
  4027. :group 'org-sparse-trees)
  4028. (defun org-cycle-hide-archived-subtrees (state)
  4029. "Re-hide all archived subtrees after a visibility state change."
  4030. (when (and (not org-cycle-open-archived-trees)
  4031. (not (memq state '(overview folded))))
  4032. (save-excursion
  4033. (let* ((globalp (memq state '(contents all)))
  4034. (beg (if globalp (point-min) (point)))
  4035. (end (if globalp (point-max) (org-end-of-subtree t))))
  4036. (org-hide-archived-subtrees beg end)
  4037. (goto-char beg)
  4038. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4039. (message "%s" (substitute-command-keys
  4040. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4041. (defun org-force-cycle-archived ()
  4042. "Cycle subtree even if it is archived."
  4043. (interactive)
  4044. (setq this-command 'org-cycle)
  4045. (let ((org-cycle-open-archived-trees t))
  4046. (call-interactively 'org-cycle)))
  4047. (defun org-hide-archived-subtrees (beg end)
  4048. "Re-hide all archived subtrees after a visibility state change."
  4049. (save-excursion
  4050. (let* ((re (concat ":" org-archive-tag ":")))
  4051. (goto-char beg)
  4052. (while (re-search-forward re end t)
  4053. (when (org-at-heading-p)
  4054. (org-flag-subtree t)
  4055. (org-end-of-subtree t))))))
  4056. (declare-function outline-end-of-heading "outline" ())
  4057. (declare-function outline-flag-region "outline" (from to flag))
  4058. (defun org-flag-subtree (flag)
  4059. (save-excursion
  4060. (org-back-to-heading t)
  4061. (outline-end-of-heading)
  4062. (outline-flag-region (point)
  4063. (progn (org-end-of-subtree t) (point))
  4064. flag)))
  4065. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4066. ;; Declare Column View Code
  4067. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  4068. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4069. (declare-function org-columns-compute "org-colview" (property))
  4070. ;; Declare ID code
  4071. (declare-function org-id-store-link "org-id")
  4072. (declare-function org-id-locations-load "org-id")
  4073. (declare-function org-id-locations-save "org-id")
  4074. (defvar org-id-track-globally)
  4075. ;;; Variables for pre-computed regular expressions, all buffer local
  4076. (defvar org-todo-regexp nil
  4077. "Matches any of the TODO state keywords.")
  4078. (make-variable-buffer-local 'org-todo-regexp)
  4079. (defvar org-not-done-regexp nil
  4080. "Matches any of the TODO state keywords except the last one.")
  4081. (make-variable-buffer-local 'org-not-done-regexp)
  4082. (defvar org-not-done-heading-regexp nil
  4083. "Matches a TODO headline that is not done.")
  4084. (make-variable-buffer-local 'org-not-done-regexp)
  4085. (defvar org-todo-line-regexp nil
  4086. "Matches a headline and puts TODO state into group 2 if present.")
  4087. (make-variable-buffer-local 'org-todo-line-regexp)
  4088. (defvar org-complex-heading-regexp nil
  4089. "Matches a headline and puts everything into groups:
  4090. group 1: the stars
  4091. group 2: The todo keyword, maybe
  4092. group 3: Priority cookie
  4093. group 4: True headline
  4094. group 5: Tags")
  4095. (make-variable-buffer-local 'org-complex-heading-regexp)
  4096. (defvar org-complex-heading-regexp-format nil
  4097. "Printf format to make regexp to match an exact headline.
  4098. This regexp will match the headline of any node which has the
  4099. exact headline text that is put into the format, but may have any
  4100. TODO state, priority and tags.")
  4101. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4102. (defvar org-todo-line-tags-regexp nil
  4103. "Matches a headline and puts TODO state into group 2 if present.
  4104. Also put tags into group 4 if tags are present.")
  4105. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4106. (defvar org-ds-keyword-length 12
  4107. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4108. (make-variable-buffer-local 'org-ds-keyword-length)
  4109. (defvar org-deadline-regexp nil
  4110. "Matches the DEADLINE keyword.")
  4111. (make-variable-buffer-local 'org-deadline-regexp)
  4112. (defvar org-deadline-time-regexp nil
  4113. "Matches the DEADLINE keyword together with a time stamp.")
  4114. (make-variable-buffer-local 'org-deadline-time-regexp)
  4115. (defvar org-deadline-time-hour-regexp nil
  4116. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4117. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4118. (defvar org-deadline-line-regexp nil
  4119. "Matches the DEADLINE keyword and the rest of the line.")
  4120. (make-variable-buffer-local 'org-deadline-line-regexp)
  4121. (defvar org-scheduled-regexp nil
  4122. "Matches the SCHEDULED keyword.")
  4123. (make-variable-buffer-local 'org-scheduled-regexp)
  4124. (defvar org-scheduled-time-regexp nil
  4125. "Matches the SCHEDULED keyword together with a time stamp.")
  4126. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4127. (defvar org-scheduled-time-hour-regexp nil
  4128. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4129. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4130. (defvar org-closed-time-regexp nil
  4131. "Matches the CLOSED keyword together with a time stamp.")
  4132. (make-variable-buffer-local 'org-closed-time-regexp)
  4133. (defvar org-keyword-time-regexp nil
  4134. "Matches any of the 4 keywords, together with the time stamp.")
  4135. (make-variable-buffer-local 'org-keyword-time-regexp)
  4136. (defvar org-keyword-time-not-clock-regexp nil
  4137. "Matches any of the 3 keywords, together with the time stamp.")
  4138. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4139. (defvar org-maybe-keyword-time-regexp nil
  4140. "Matches a timestamp, possibly preceded by a keyword.")
  4141. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4142. (defvar org-all-time-keywords nil
  4143. "List of time keywords.")
  4144. (make-variable-buffer-local 'org-all-time-keywords)
  4145. (defconst org-plain-time-of-day-regexp
  4146. (concat
  4147. "\\(\\<[012]?[0-9]"
  4148. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4149. "\\(--?"
  4150. "\\(\\<[012]?[0-9]"
  4151. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4152. "\\)?")
  4153. "Regular expression to match a plain time or time range.
  4154. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4155. groups carry important information:
  4156. 0 the full match
  4157. 1 the first time, range or not
  4158. 8 the second time, if it is a range.")
  4159. (defconst org-plain-time-extension-regexp
  4160. (concat
  4161. "\\(\\<[012]?[0-9]"
  4162. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4163. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4164. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4165. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4166. groups carry important information:
  4167. 0 the full match
  4168. 7 hours of duration
  4169. 9 minutes of duration")
  4170. (defconst org-stamp-time-of-day-regexp
  4171. (concat
  4172. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4173. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4174. "\\(--?"
  4175. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4176. "Regular expression to match a timestamp time or time range.
  4177. After a match, the following groups carry important information:
  4178. 0 the full match
  4179. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4180. 2 the first time, range or not
  4181. 4 the second time, if it is a range.")
  4182. (defconst org-startup-options
  4183. '(("fold" org-startup-folded t)
  4184. ("overview" org-startup-folded t)
  4185. ("nofold" org-startup-folded nil)
  4186. ("showall" org-startup-folded nil)
  4187. ("showeverything" org-startup-folded showeverything)
  4188. ("content" org-startup-folded content)
  4189. ("indent" org-startup-indented t)
  4190. ("noindent" org-startup-indented nil)
  4191. ("hidestars" org-hide-leading-stars t)
  4192. ("showstars" org-hide-leading-stars nil)
  4193. ("odd" org-odd-levels-only t)
  4194. ("oddeven" org-odd-levels-only nil)
  4195. ("align" org-startup-align-all-tables t)
  4196. ("noalign" org-startup-align-all-tables nil)
  4197. ("inlineimages" org-startup-with-inline-images t)
  4198. ("noinlineimages" org-startup-with-inline-images nil)
  4199. ("latexpreview" org-startup-with-latex-preview t)
  4200. ("nolatexpreview" org-startup-with-latex-preview nil)
  4201. ("customtime" org-display-custom-times t)
  4202. ("logdone" org-log-done time)
  4203. ("lognotedone" org-log-done note)
  4204. ("nologdone" org-log-done nil)
  4205. ("lognoteclock-out" org-log-note-clock-out t)
  4206. ("nolognoteclock-out" org-log-note-clock-out nil)
  4207. ("logrepeat" org-log-repeat state)
  4208. ("lognoterepeat" org-log-repeat note)
  4209. ("logdrawer" org-log-into-drawer t)
  4210. ("nologdrawer" org-log-into-drawer nil)
  4211. ("logstatesreversed" org-log-states-order-reversed t)
  4212. ("nologstatesreversed" org-log-states-order-reversed nil)
  4213. ("nologrepeat" org-log-repeat nil)
  4214. ("logreschedule" org-log-reschedule time)
  4215. ("lognotereschedule" org-log-reschedule note)
  4216. ("nologreschedule" org-log-reschedule nil)
  4217. ("logredeadline" org-log-redeadline time)
  4218. ("lognoteredeadline" org-log-redeadline note)
  4219. ("nologredeadline" org-log-redeadline nil)
  4220. ("logrefile" org-log-refile time)
  4221. ("lognoterefile" org-log-refile note)
  4222. ("nologrefile" org-log-refile nil)
  4223. ("fninline" org-footnote-define-inline t)
  4224. ("nofninline" org-footnote-define-inline nil)
  4225. ("fnlocal" org-footnote-section nil)
  4226. ("fnauto" org-footnote-auto-label t)
  4227. ("fnprompt" org-footnote-auto-label nil)
  4228. ("fnconfirm" org-footnote-auto-label confirm)
  4229. ("fnplain" org-footnote-auto-label plain)
  4230. ("fnadjust" org-footnote-auto-adjust t)
  4231. ("nofnadjust" org-footnote-auto-adjust nil)
  4232. ("constcgs" constants-unit-system cgs)
  4233. ("constSI" constants-unit-system SI)
  4234. ("noptag" org-tag-persistent-alist nil)
  4235. ("hideblocks" org-hide-block-startup t)
  4236. ("nohideblocks" org-hide-block-startup nil)
  4237. ("beamer" org-startup-with-beamer-mode t)
  4238. ("entitiespretty" org-pretty-entities t)
  4239. ("entitiesplain" org-pretty-entities nil))
  4240. "Variable associated with STARTUP options for org-mode.
  4241. Each element is a list of three items: the startup options (as written
  4242. in the #+STARTUP line), the corresponding variable, and the value to set
  4243. this variable to if the option is found. An optional forth element PUSH
  4244. means to push this value onto the list in the variable.")
  4245. (defun org-update-property-plist (key val props)
  4246. "Update PROPS with KEY and VAL."
  4247. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4248. (key (if appending (substring key 0 (- (length key) 1)) key))
  4249. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4250. (previous (cdr (assoc key props))))
  4251. (if appending
  4252. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4253. (cons (cons key val) remainder))))
  4254. (defcustom org-group-tags t
  4255. "When non-nil (the default), use group tags.
  4256. This can be turned on/off through `org-toggle-tags-groups'."
  4257. :group 'org-tags
  4258. :group 'org-startup
  4259. :type 'boolean)
  4260. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4261. (defun org-toggle-tags-groups ()
  4262. "Toggle support for group tags.
  4263. Support for group tags is controlled by the option
  4264. `org-group-tags', which is non-nil by default."
  4265. (interactive)
  4266. (setq org-group-tags (not org-group-tags))
  4267. (cond ((and (derived-mode-p 'org-agenda-mode)
  4268. org-group-tags)
  4269. (org-agenda-redo))
  4270. ((derived-mode-p 'org-mode)
  4271. (let ((org-inhibit-startup t)) (org-mode))))
  4272. (message "Groups tags support has been turned %s"
  4273. (if org-group-tags "on" "off")))
  4274. (defun org-set-regexps-and-options-for-tags ()
  4275. "Precompute variables used for tags."
  4276. (when (derived-mode-p 'org-mode)
  4277. (org-set-local 'org-file-tags nil)
  4278. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4279. (splitre "[ \t]+")
  4280. (start 0)
  4281. tags ftags key value)
  4282. (save-excursion
  4283. (save-restriction
  4284. (widen)
  4285. (goto-char (point-min))
  4286. (while (re-search-forward re nil t)
  4287. (setq key (upcase (org-match-string-no-properties 1))
  4288. value (org-match-string-no-properties 2))
  4289. (if (stringp value) (setq value (org-trim value)))
  4290. (cond
  4291. ((equal key "TAGS")
  4292. (setq tags (append tags (if tags '("\\n") nil)
  4293. (org-split-string value splitre))))
  4294. ((equal key "FILETAGS")
  4295. (when (string-match "\\S-" value)
  4296. (setq ftags
  4297. (append
  4298. ftags
  4299. (apply 'append
  4300. (mapcar (lambda (x) (org-split-string x ":"))
  4301. (org-split-string value)))))))))))
  4302. ;; Process the file tags.
  4303. (and ftags (org-set-local 'org-file-tags
  4304. (mapcar 'org-add-prop-inherited ftags)))
  4305. (org-set-local 'org-tag-groups-alist nil)
  4306. ;; Process the tags.
  4307. (when (and (not tags) org-tag-alist)
  4308. (setq tags
  4309. (mapcar
  4310. (lambda (tg) (cond ((eq (car tg) :startgroup) "{")
  4311. ((eq (car tg) :endgroup) "}")
  4312. ((eq (car tg) :grouptags) ":")
  4313. ((eq (car tg) :newline) "\n")
  4314. (t (concat (car tg)
  4315. (if (characterp (cdr tg))
  4316. (format "(%s)" (char-to-string (cdr tg))) "")))))
  4317. org-tag-alist)))
  4318. (let (e tgs g)
  4319. (while (setq e (pop tags))
  4320. (cond
  4321. ((equal e "{")
  4322. (progn (push '(:startgroup) tgs)
  4323. (when (equal (nth 1 tags) ":")
  4324. (push (list (replace-regexp-in-string
  4325. "(.+)$" "" (nth 0 tags)))
  4326. org-tag-groups-alist)
  4327. (setq g 0))))
  4328. ((equal e ":") (push '(:grouptags) tgs))
  4329. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4330. ((equal e "\\n") (push '(:newline) tgs))
  4331. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4332. (push (cons (match-string 1 e)
  4333. (string-to-char (match-string 2 e))) tgs)
  4334. (if (and g (> g 0))
  4335. (setcar org-tag-groups-alist
  4336. (append (car org-tag-groups-alist)
  4337. (list (match-string 1 e)))))
  4338. (if g (setq g (1+ g))))
  4339. (t (push (list e) tgs)
  4340. (if (and g (> g 0))
  4341. (setcar org-tag-groups-alist
  4342. (append (car org-tag-groups-alist) (list e))))
  4343. (if g (setq g (1+ g))))))
  4344. (org-set-local 'org-tag-alist nil)
  4345. (while (setq e (pop tgs))
  4346. (or (and (stringp (car e))
  4347. (assoc (car e) org-tag-alist))
  4348. (push e org-tag-alist)))
  4349. ;; Return a list with tag variables
  4350. (list org-file-tags org-tag-alist org-tag-groups-alist)))))
  4351. (defvar org-ota nil)
  4352. (defun org-set-regexps-and-options ()
  4353. "Precompute regular expressions used in the current buffer."
  4354. (when (derived-mode-p 'org-mode)
  4355. (org-set-local 'org-todo-kwd-alist nil)
  4356. (org-set-local 'org-todo-key-alist nil)
  4357. (org-set-local 'org-todo-key-trigger nil)
  4358. (org-set-local 'org-todo-keywords-1 nil)
  4359. (org-set-local 'org-done-keywords nil)
  4360. (org-set-local 'org-todo-heads nil)
  4361. (org-set-local 'org-todo-sets nil)
  4362. (org-set-local 'org-todo-log-states nil)
  4363. (org-set-local 'org-file-properties nil)
  4364. (let ((re (org-make-options-regexp
  4365. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4366. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4367. "SETUPFILE" "OPTIONS")
  4368. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4369. (splitre "[ \t]+")
  4370. (scripts org-use-sub-superscripts)
  4371. kwds kws0 kwsa key log value cat arch const links hw dws
  4372. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4373. (start 0))
  4374. (save-excursion
  4375. (save-restriction
  4376. (widen)
  4377. (goto-char (point-min))
  4378. (while
  4379. (or (and
  4380. ext-setup-or-nil
  4381. (not org-ota)
  4382. (let (ret)
  4383. (with-temp-buffer
  4384. (insert ext-setup-or-nil)
  4385. (let ((major-mode 'org-mode) org-ota)
  4386. (setq ret (save-match-data
  4387. (org-set-regexps-and-options-for-tags)))))
  4388. ;; Append setupfile tags to existing tags
  4389. (setq org-ota t)
  4390. (setq org-file-tags
  4391. (delq nil (append org-file-tags (nth 0 ret)))
  4392. org-tag-alist
  4393. (delq nil (append org-tag-alist (nth 1 ret)))
  4394. org-tag-groups-alist
  4395. (delq nil (append org-tag-groups-alist (nth 2 ret))))))
  4396. (and ext-setup-or-nil
  4397. (string-match re ext-setup-or-nil start)
  4398. (setq start (match-end 0)))
  4399. (and (setq ext-setup-or-nil nil start 0)
  4400. (re-search-forward re nil t)))
  4401. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4402. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4403. (if (stringp value) (setq value (org-trim value)))
  4404. (cond
  4405. ((equal key "CATEGORY")
  4406. (setq cat value))
  4407. ((member key '("SEQ_TODO" "TODO"))
  4408. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4409. ((equal key "TYP_TODO")
  4410. (push (cons 'type (org-split-string value splitre)) kwds))
  4411. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4412. ;; general TODO-like setup
  4413. (push (cons (intern (downcase (match-string 1 key)))
  4414. (org-split-string value splitre)) kwds))
  4415. ((equal key "COLUMNS")
  4416. (org-set-local 'org-columns-default-format value))
  4417. ((equal key "LINK")
  4418. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4419. (push (cons (match-string 1 value)
  4420. (org-trim (match-string 2 value)))
  4421. links)))
  4422. ((equal key "PRIORITIES")
  4423. (setq prio (org-split-string value " +")))
  4424. ((equal key "PROPERTY")
  4425. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4426. (setq props (org-update-property-plist (match-string 1 value)
  4427. (match-string 2 value)
  4428. props))))
  4429. ((equal key "CONSTANTS")
  4430. (org-table-set-constants))
  4431. ((equal key "STARTUP")
  4432. (let ((opts (org-split-string value splitre))
  4433. l var val)
  4434. (while (setq l (pop opts))
  4435. (when (setq l (assoc l org-startup-options))
  4436. (setq var (nth 1 l) val (nth 2 l))
  4437. (if (not (nth 3 l))
  4438. (set (make-local-variable var) val)
  4439. (if (not (listp (symbol-value var)))
  4440. (set (make-local-variable var) nil))
  4441. (set (make-local-variable var) (symbol-value var))
  4442. (add-to-list var val))))))
  4443. ((equal key "ARCHIVE")
  4444. (setq arch value)
  4445. (remove-text-properties 0 (length arch)
  4446. '(face t fontified t) arch))
  4447. ((equal key "OPTIONS")
  4448. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4449. (setq scripts (read (match-string 2 value)))))
  4450. ((and (equal key "SETUPFILE")
  4451. ;; Prevent checking in Gnus messages
  4452. (not buffer-read-only))
  4453. (setq setup-contents (org-file-contents
  4454. (expand-file-name
  4455. (org-remove-double-quotes value))
  4456. 'noerror))
  4457. (if (not ext-setup-or-nil)
  4458. (setq ext-setup-or-nil setup-contents start 0)
  4459. (setq ext-setup-or-nil
  4460. (concat (substring ext-setup-or-nil 0 start)
  4461. "\n" setup-contents "\n"
  4462. (substring ext-setup-or-nil start)))))))
  4463. ;; search for property blocks
  4464. (goto-char (point-min))
  4465. (while (re-search-forward org-block-regexp nil t)
  4466. (when (equal "PROPERTY" (upcase (match-string 1)))
  4467. (setq value (replace-regexp-in-string
  4468. "[\n\r]" " " (match-string 4)))
  4469. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4470. (setq props (org-update-property-plist (match-string 1 value)
  4471. (match-string 2 value)
  4472. props)))))))
  4473. (org-set-local 'org-use-sub-superscripts scripts)
  4474. (when cat
  4475. (org-set-local 'org-category (intern cat))
  4476. (push (cons "CATEGORY" cat) props))
  4477. (when prio
  4478. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4479. (setq prio (mapcar 'string-to-char prio))
  4480. (org-set-local 'org-highest-priority (nth 0 prio))
  4481. (org-set-local 'org-lowest-priority (nth 1 prio))
  4482. (org-set-local 'org-default-priority (nth 2 prio)))
  4483. (and props (org-set-local 'org-file-properties (nreverse props)))
  4484. (and arch (org-set-local 'org-archive-location arch))
  4485. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4486. ;; Process the TODO keywords
  4487. (unless kwds
  4488. ;; Use the global values as if they had been given locally.
  4489. (setq kwds (default-value 'org-todo-keywords))
  4490. (if (stringp (car kwds))
  4491. (setq kwds (list (cons org-todo-interpretation
  4492. (default-value 'org-todo-keywords)))))
  4493. (setq kwds (reverse kwds)))
  4494. (setq kwds (nreverse kwds))
  4495. (let (inter kws kw)
  4496. (while (setq kws (pop kwds))
  4497. (let ((kws (or
  4498. (run-hook-with-args-until-success
  4499. 'org-todo-setup-filter-hook kws)
  4500. kws)))
  4501. (setq inter (pop kws) sep (member "|" kws)
  4502. kws0 (delete "|" (copy-sequence kws))
  4503. kwsa nil
  4504. kws1 (mapcar
  4505. (lambda (x)
  4506. ;; 1 2
  4507. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4508. (progn
  4509. (setq kw (match-string 1 x)
  4510. key (and (match-end 2) (match-string 2 x))
  4511. log (org-extract-log-state-settings x))
  4512. (push (cons kw (and key (string-to-char key))) kwsa)
  4513. (and log (push log org-todo-log-states))
  4514. kw)
  4515. (error "Invalid TODO keyword %s" x)))
  4516. kws0)
  4517. kwsa (if kwsa (append '((:startgroup))
  4518. (nreverse kwsa)
  4519. '((:endgroup))))
  4520. hw (car kws1)
  4521. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4522. tail (list inter hw (car dws) (org-last dws))))
  4523. (add-to-list 'org-todo-heads hw 'append)
  4524. (push kws1 org-todo-sets)
  4525. (setq org-done-keywords (append org-done-keywords dws nil))
  4526. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4527. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4528. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4529. (setq org-todo-sets (nreverse org-todo-sets)
  4530. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4531. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4532. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4533. ;; Compute the regular expressions and other local variables.
  4534. ;; Using `org-outline-regexp-bol' would complicate them much,
  4535. ;; because of the fixed white space at the end of that string.
  4536. (if (not org-done-keywords)
  4537. (setq org-done-keywords (and org-todo-keywords-1
  4538. (list (org-last org-todo-keywords-1)))))
  4539. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4540. (length org-scheduled-string)
  4541. (length org-clock-string)
  4542. (length org-closed-string)))
  4543. org-not-done-keywords
  4544. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4545. org-todo-regexp
  4546. (concat "\\("
  4547. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4548. "\\)")
  4549. org-not-done-regexp
  4550. (concat "\\("
  4551. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4552. "\\)")
  4553. org-not-done-heading-regexp
  4554. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4555. org-todo-line-regexp
  4556. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4557. org-complex-heading-regexp
  4558. (concat "^\\(\\*+\\)"
  4559. "\\(?: +" org-todo-regexp "\\)?"
  4560. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4561. "\\(?: +\\(.*?\\)\\)??"
  4562. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4563. "[ \t]*$")
  4564. org-complex-heading-regexp-format
  4565. (concat "^\\(\\*+\\)"
  4566. "\\(?: +" org-todo-regexp "\\)?"
  4567. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4568. "\\(?: +"
  4569. ;; Stats cookies can be stuck to body.
  4570. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4571. "\\(%s\\)"
  4572. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4573. "\\)"
  4574. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4575. "[ \t]*$")
  4576. org-todo-line-tags-regexp
  4577. (concat "^\\(\\*+\\)"
  4578. "\\(?: +" org-todo-regexp "\\)?"
  4579. "\\(?: +\\(.*?\\)\\)??"
  4580. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4581. "[ \t]*$")
  4582. org-deadline-regexp (concat "\\<" org-deadline-string)
  4583. org-deadline-time-regexp
  4584. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4585. org-deadline-time-hour-regexp
  4586. (concat "\\<" org-deadline-string
  4587. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4588. org-deadline-line-regexp
  4589. (concat "\\<\\(" org-deadline-string "\\).*")
  4590. org-scheduled-regexp
  4591. (concat "\\<" org-scheduled-string)
  4592. org-scheduled-time-regexp
  4593. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4594. org-scheduled-time-hour-regexp
  4595. (concat "\\<" org-scheduled-string
  4596. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4597. org-closed-time-regexp
  4598. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4599. org-keyword-time-regexp
  4600. (concat "\\<\\(" org-scheduled-string
  4601. "\\|" org-deadline-string
  4602. "\\|" org-closed-string
  4603. "\\|" org-clock-string "\\)"
  4604. " *[[<]\\([^]>]+\\)[]>]")
  4605. org-keyword-time-not-clock-regexp
  4606. (concat "\\<\\(" org-scheduled-string
  4607. "\\|" org-deadline-string
  4608. "\\|" org-closed-string
  4609. "\\)"
  4610. " *[[<]\\([^]>]+\\)[]>]")
  4611. org-maybe-keyword-time-regexp
  4612. (concat "\\(\\<\\(" org-scheduled-string
  4613. "\\|" org-deadline-string
  4614. "\\|" org-closed-string
  4615. "\\|" org-clock-string "\\)\\)?"
  4616. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4617. org-all-time-keywords
  4618. (mapcar (lambda (w) (substring w 0 -1))
  4619. (list org-scheduled-string org-deadline-string
  4620. org-clock-string org-closed-string)))
  4621. (setq org-ota nil)
  4622. (org-compute-latex-and-related-regexp))))
  4623. (defun org-file-contents (file &optional noerror)
  4624. "Return the contents of FILE, as a string."
  4625. (if (and file (file-readable-p file))
  4626. (with-temp-buffer
  4627. (insert-file-contents file)
  4628. (buffer-string))
  4629. (funcall (if noerror 'message 'error)
  4630. "Cannot read file \"%s\"%s"
  4631. file
  4632. (let ((from (buffer-file-name (buffer-base-buffer))))
  4633. (if from (concat " (referenced in file \"" from "\")") "")))))
  4634. (defun org-extract-log-state-settings (x)
  4635. "Extract the log state setting from a TODO keyword string.
  4636. This will extract info from a string like \"WAIT(w@/!)\"."
  4637. (let (kw key log1 log2)
  4638. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4639. (setq kw (match-string 1 x)
  4640. key (and (match-end 2) (match-string 2 x))
  4641. log1 (and (match-end 3) (match-string 3 x))
  4642. log2 (and (match-end 4) (match-string 4 x)))
  4643. (and (or log1 log2)
  4644. (list kw
  4645. (and log1 (if (equal log1 "!") 'time 'note))
  4646. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4647. (defun org-remove-keyword-keys (list)
  4648. "Remove a pair of parenthesis at the end of each string in LIST."
  4649. (mapcar (lambda (x)
  4650. (if (string-match "(.*)$" x)
  4651. (substring x 0 (match-beginning 0))
  4652. x))
  4653. list))
  4654. (defun org-assign-fast-keys (alist)
  4655. "Assign fast keys to a keyword-key alist.
  4656. Respect keys that are already there."
  4657. (let (new e (alt ?0))
  4658. (while (setq e (pop alist))
  4659. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4660. (cdr e)) ;; Key already assigned.
  4661. (push e new)
  4662. (let ((clist (string-to-list (downcase (car e))))
  4663. (used (append new alist)))
  4664. (when (= (car clist) ?@)
  4665. (pop clist))
  4666. (while (and clist (rassoc (car clist) used))
  4667. (pop clist))
  4668. (unless clist
  4669. (while (rassoc alt used)
  4670. (incf alt)))
  4671. (push (cons (car e) (or (car clist) alt)) new))))
  4672. (nreverse new)))
  4673. ;;; Some variables used in various places
  4674. (defvar org-window-configuration nil
  4675. "Used in various places to store a window configuration.")
  4676. (defvar org-selected-window nil
  4677. "Used in various places to store a window configuration.")
  4678. (defvar org-finish-function nil
  4679. "Function to be called when `C-c C-c' is used.
  4680. This is for getting out of special buffers like capture.")
  4681. ;; FIXME: Occasionally check by commenting these, to make sure
  4682. ;; no other functions uses these, forgetting to let-bind them.
  4683. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4684. (defvar org-last-state)
  4685. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4686. ;; Defined somewhere in this file, but used before definition.
  4687. (defvar org-entities) ;; defined in org-entities.el
  4688. (defvar org-struct-menu)
  4689. (defvar org-org-menu)
  4690. (defvar org-tbl-menu)
  4691. ;;;; Define the Org-mode
  4692. ;; We use a before-change function to check if a table might need
  4693. ;; an update.
  4694. (defvar org-table-may-need-update t
  4695. "Indicates that a table might need an update.
  4696. This variable is set by `org-before-change-function'.
  4697. `org-table-align' sets it back to nil.")
  4698. (defun org-before-change-function (beg end)
  4699. "Every change indicates that a table might need an update."
  4700. (setq org-table-may-need-update t))
  4701. (defvar org-mode-map)
  4702. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4703. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4704. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4705. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4706. (defvar org-table-buffer-is-an nil)
  4707. (defvar bidi-paragraph-direction)
  4708. (defvar buffer-face-mode-face)
  4709. (require 'outline)
  4710. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4711. (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"))
  4712. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4713. ;; Other stuff we need.
  4714. (require 'time-date)
  4715. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4716. (require 'easymenu)
  4717. (require 'overlay)
  4718. ;; (require 'org-macs) moved higher up in the file before it is first used
  4719. (require 'org-entities)
  4720. ;; (require 'org-compat) moved higher up in the file before it is first used
  4721. (require 'org-faces)
  4722. (require 'org-list)
  4723. (require 'org-pcomplete)
  4724. (require 'org-src)
  4725. (require 'org-footnote)
  4726. (require 'org-macro)
  4727. ;; babel
  4728. (require 'ob)
  4729. ;;;###autoload
  4730. (define-derived-mode org-mode outline-mode "Org"
  4731. "Outline-based notes management and organizer, alias
  4732. \"Carsten's outline-mode for keeping track of everything.\"
  4733. Org-mode develops organizational tasks around a NOTES file which
  4734. contains information about projects as plain text. Org-mode is
  4735. implemented on top of outline-mode, which is ideal to keep the content
  4736. of large files well structured. It supports ToDo items, deadlines and
  4737. time stamps, which magically appear in the diary listing of the Emacs
  4738. calendar. Tables are easily created with a built-in table editor.
  4739. Plain text URL-like links connect to websites, emails (VM), Usenet
  4740. messages (Gnus), BBDB entries, and any files related to the project.
  4741. For printing and sharing of notes, an Org-mode file (or a part of it)
  4742. can be exported as a structured ASCII or HTML file.
  4743. The following commands are available:
  4744. \\{org-mode-map}"
  4745. ;; Get rid of Outline menus, they are not needed
  4746. ;; Need to do this here because define-derived-mode sets up
  4747. ;; the keymap so late. Still, it is a waste to call this each time
  4748. ;; we switch another buffer into org-mode.
  4749. (if (featurep 'xemacs)
  4750. (when (boundp 'outline-mode-menu-heading)
  4751. ;; Assume this is Greg's port, it uses easymenu
  4752. (easy-menu-remove outline-mode-menu-heading)
  4753. (easy-menu-remove outline-mode-menu-show)
  4754. (easy-menu-remove outline-mode-menu-hide))
  4755. (define-key org-mode-map [menu-bar headings] 'undefined)
  4756. (define-key org-mode-map [menu-bar hide] 'undefined)
  4757. (define-key org-mode-map [menu-bar show] 'undefined))
  4758. (org-load-modules-maybe)
  4759. (easy-menu-add org-org-menu)
  4760. (easy-menu-add org-tbl-menu)
  4761. (org-install-agenda-files-menu)
  4762. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4763. (add-to-invisibility-spec '(org-cwidth))
  4764. (add-to-invisibility-spec '(org-hide-block . t))
  4765. (when (featurep 'xemacs)
  4766. (org-set-local 'line-move-ignore-invisible t))
  4767. (org-set-local 'outline-regexp org-outline-regexp)
  4768. (org-set-local 'outline-level 'org-outline-level)
  4769. (setq bidi-paragraph-direction 'left-to-right)
  4770. (when (and org-ellipsis
  4771. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4772. (fboundp 'make-glyph-code))
  4773. (unless org-display-table
  4774. (setq org-display-table (make-display-table)))
  4775. (set-display-table-slot
  4776. org-display-table 4
  4777. (vconcat (mapcar
  4778. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4779. org-ellipsis)))
  4780. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4781. (setq buffer-display-table org-display-table))
  4782. (org-set-regexps-and-options-for-tags)
  4783. (org-set-regexps-and-options)
  4784. (org-set-font-lock-defaults)
  4785. (when (and org-tag-faces (not org-tags-special-faces-re))
  4786. ;; tag faces set outside customize.... force initialization.
  4787. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4788. ;; Calc embedded
  4789. (org-set-local 'calc-embedded-open-mode "# ")
  4790. ;; Modify a few syntax entries
  4791. (modify-syntax-entry ?@ "w")
  4792. (modify-syntax-entry ?\" "\"")
  4793. (modify-syntax-entry ?\\ "_")
  4794. (modify-syntax-entry ?~ "_")
  4795. (if org-startup-truncated (setq truncate-lines t))
  4796. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4797. (org-set-local 'font-lock-unfontify-region-function
  4798. 'org-unfontify-region)
  4799. ;; Activate before-change-function
  4800. (org-set-local 'org-table-may-need-update t)
  4801. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4802. 'local)
  4803. ;; Check for running clock before killing a buffer
  4804. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4805. ;; Initialize macros templates.
  4806. (org-macro-initialize-templates)
  4807. ;; Initialize radio targets.
  4808. (org-update-radio-target-regexp)
  4809. ;; Indentation.
  4810. (org-set-local 'indent-line-function 'org-indent-line)
  4811. (org-set-local 'indent-region-function 'org-indent-region)
  4812. ;; Filling and auto-filling.
  4813. (org-setup-filling)
  4814. ;; Comments.
  4815. (org-setup-comments-handling)
  4816. ;; Initialize cache.
  4817. (org-element-cache-reset)
  4818. ;; Beginning/end of defun
  4819. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4820. (org-set-local 'end-of-defun-function
  4821. (lambda ()
  4822. (if (not (org-at-heading-p))
  4823. (org-forward-element)
  4824. (org-forward-element)
  4825. (forward-char -1))))
  4826. ;; Next error for sparse trees
  4827. (org-set-local 'next-error-function 'org-occur-next-match)
  4828. ;; Make sure dependence stuff works reliably, even for users who set it
  4829. ;; too late :-(
  4830. (if org-enforce-todo-dependencies
  4831. (add-hook 'org-blocker-hook
  4832. 'org-block-todo-from-children-or-siblings-or-parent)
  4833. (remove-hook 'org-blocker-hook
  4834. 'org-block-todo-from-children-or-siblings-or-parent))
  4835. (if org-enforce-todo-checkbox-dependencies
  4836. (add-hook 'org-blocker-hook
  4837. 'org-block-todo-from-checkboxes)
  4838. (remove-hook 'org-blocker-hook
  4839. 'org-block-todo-from-checkboxes))
  4840. ;; Align options lines
  4841. (org-set-local
  4842. 'align-mode-rules-list
  4843. '((org-in-buffer-settings
  4844. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4845. (modes . '(org-mode)))))
  4846. ;; Imenu
  4847. (org-set-local 'imenu-create-index-function
  4848. 'org-imenu-get-tree)
  4849. ;; Make isearch reveal context
  4850. (if (or (featurep 'xemacs)
  4851. (not (boundp 'outline-isearch-open-invisible-function)))
  4852. ;; Emacs 21 and XEmacs make use of the hook
  4853. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4854. ;; Emacs 22 deals with this through a special variable
  4855. (org-set-local 'outline-isearch-open-invisible-function
  4856. (lambda (&rest ignore) (org-show-context 'isearch))))
  4857. ;; Setup the pcomplete hooks
  4858. (set (make-local-variable 'pcomplete-command-completion-function)
  4859. 'org-pcomplete-initial)
  4860. (set (make-local-variable 'pcomplete-command-name-function)
  4861. 'org-command-at-point)
  4862. (set (make-local-variable 'pcomplete-default-completion-function)
  4863. 'ignore)
  4864. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4865. 'org-parse-arguments)
  4866. (set (make-local-variable 'pcomplete-termination-string) "")
  4867. (when (>= emacs-major-version 23)
  4868. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4869. ;; If empty file that did not turn on org-mode automatically, make it to.
  4870. (if (and org-insert-mode-line-in-empty-file
  4871. (org-called-interactively-p 'any)
  4872. (= (point-min) (point-max)))
  4873. (insert "# -*- mode: org -*-\n\n"))
  4874. (unless org-inhibit-startup
  4875. (org-unmodified
  4876. (and org-startup-with-beamer-mode (org-beamer-mode))
  4877. (when org-startup-align-all-tables
  4878. (org-table-map-tables 'org-table-align 'quietly))
  4879. (when org-startup-with-inline-images
  4880. (org-display-inline-images))
  4881. (when org-startup-with-latex-preview
  4882. (org-toggle-latex-fragment))
  4883. (unless org-inhibit-startup-visibility-stuff
  4884. (org-set-startup-visibility))
  4885. (org-refresh-effort-properties)))
  4886. ;; Try to set org-hide correctly
  4887. (let ((foreground (org-find-invisible-foreground)))
  4888. (if foreground
  4889. (set-face-foreground 'org-hide foreground))))
  4890. ;; Update `customize-package-emacs-version-alist'
  4891. (add-to-list 'customize-package-emacs-version-alist
  4892. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4893. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4894. ("8.2.6" . "24.4")))
  4895. (defvar org-mode-transpose-word-syntax-table
  4896. (let ((st (make-syntax-table)))
  4897. (mapc (lambda(c) (modify-syntax-entry
  4898. (string-to-char (car c)) "w p" st))
  4899. org-emphasis-alist)
  4900. st))
  4901. (when (fboundp 'abbrev-table-put)
  4902. (abbrev-table-put org-mode-abbrev-table
  4903. :parents (list text-mode-abbrev-table)))
  4904. (defun org-find-invisible-foreground ()
  4905. (let ((candidates (remove
  4906. "unspecified-bg"
  4907. (nconc
  4908. (list (face-background 'default)
  4909. (face-background 'org-default))
  4910. (mapcar
  4911. (lambda (alist)
  4912. (when (boundp alist)
  4913. (cdr (assoc 'background-color (symbol-value alist)))))
  4914. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4915. (list (face-foreground 'org-hide))))))
  4916. (car (remove nil candidates))))
  4917. (defun org-current-time (&optional rounding-minutes past)
  4918. "Current time, possibly rounded to ROUNDING-MINUTES.
  4919. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4920. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4921. the rounding returns a past time."
  4922. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4923. (car org-time-stamp-rounding-minutes)))
  4924. (time (decode-time)) res)
  4925. (if (< r 1)
  4926. (current-time)
  4927. (setq res
  4928. (apply 'encode-time
  4929. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4930. (nthcdr 2 time))))
  4931. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4932. (seconds-to-time (- (org-float-time res) (* r 60)))
  4933. res))))
  4934. (defun org-today ()
  4935. "Return today date, considering `org-extend-today-until'."
  4936. (time-to-days
  4937. (time-subtract (current-time)
  4938. (list 0 (* 3600 org-extend-today-until) 0))))
  4939. ;;;; Font-Lock stuff, including the activators
  4940. (defvar org-mouse-map (make-sparse-keymap))
  4941. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4942. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4943. (when org-mouse-1-follows-link
  4944. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4945. (when org-tab-follows-link
  4946. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4947. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4948. (require 'font-lock)
  4949. (defconst org-non-link-chars "]\t\n\r<>")
  4950. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4951. "file+sys" "news" "shell" "elisp" "doi" "message"
  4952. "help"))
  4953. (defvar org-link-types-re nil
  4954. "Matches a link that has a url-like prefix like \"http:\"")
  4955. (defvar org-link-re-with-space nil
  4956. "Matches a link with spaces, optional angular brackets around it.")
  4957. (defvar org-link-re-with-space2 nil
  4958. "Matches a link with spaces, optional angular brackets around it.")
  4959. (defvar org-link-re-with-space3 nil
  4960. "Matches a link with spaces, only for internal part in bracket links.")
  4961. (defvar org-angle-link-re nil
  4962. "Matches link with angular brackets, spaces are allowed.")
  4963. (defvar org-plain-link-re nil
  4964. "Matches plain link, without spaces.")
  4965. (defvar org-bracket-link-regexp nil
  4966. "Matches a link in double brackets.")
  4967. (defvar org-bracket-link-analytic-regexp nil
  4968. "Regular expression used to analyze links.
  4969. Here is what the match groups contain after a match:
  4970. 1: http:
  4971. 2: http
  4972. 3: path
  4973. 4: [desc]
  4974. 5: desc")
  4975. (defvar org-bracket-link-analytic-regexp++ nil
  4976. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4977. (defvar org-any-link-re nil
  4978. "Regular expression matching any link.")
  4979. (defconst org-match-sexp-depth 3
  4980. "Number of stacked braces for sub/superscript matching.")
  4981. (defun org-create-multibrace-regexp (left right n)
  4982. "Create a regular expression which will match a balanced sexp.
  4983. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4984. as single character strings.
  4985. The regexp returned will match the entire expression including the
  4986. delimiters. It will also define a single group which contains the
  4987. match except for the outermost delimiters. The maximum depth of
  4988. stacked delimiters is N. Escaping delimiters is not possible."
  4989. (let* ((nothing (concat "[^" left right "]*?"))
  4990. (or "\\|")
  4991. (re nothing)
  4992. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4993. (while (> n 1)
  4994. (setq n (1- n)
  4995. re (concat re or next)
  4996. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4997. (concat left "\\(" re "\\)" right)))
  4998. (defconst org-match-substring-regexp
  4999. (concat
  5000. "\\(\\S-\\)\\([_^]\\)\\("
  5001. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  5002. "\\|"
  5003. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  5004. "\\|"
  5005. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  5006. "The regular expression matching a sub- or superscript.")
  5007. (defconst org-match-substring-with-braces-regexp
  5008. (concat
  5009. "\\(\\S-\\)\\([_^]\\)"
  5010. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  5011. "The regular expression matching a sub- or superscript, forcing braces.")
  5012. (defun org-make-link-regexps ()
  5013. "Update the link regular expressions.
  5014. This should be called after the variable `org-link-types' has changed."
  5015. (let ((types-re (regexp-opt org-link-types t)))
  5016. (setq org-link-types-re
  5017. (concat "\\`" types-re ":")
  5018. org-link-re-with-space
  5019. (concat "<?" types-re ":"
  5020. "\\([^" org-non-link-chars " ]"
  5021. "[^" org-non-link-chars "]*"
  5022. "[^" org-non-link-chars " ]\\)>?")
  5023. org-link-re-with-space2
  5024. (concat "<?" types-re ":"
  5025. "\\([^" org-non-link-chars " ]"
  5026. "[^\t\n\r]*"
  5027. "[^" org-non-link-chars " ]\\)>?")
  5028. org-link-re-with-space3
  5029. (concat "<?" types-re ":"
  5030. "\\([^" org-non-link-chars " ]"
  5031. "[^\t\n\r]*\\)")
  5032. org-angle-link-re
  5033. (concat "<" types-re ":"
  5034. "\\([^" org-non-link-chars " ]"
  5035. "[^" org-non-link-chars "]*"
  5036. "\\)>")
  5037. org-plain-link-re
  5038. (concat
  5039. "\\<" types-re ":"
  5040. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  5041. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5042. org-bracket-link-regexp
  5043. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5044. org-bracket-link-analytic-regexp
  5045. (concat
  5046. "\\[\\["
  5047. "\\(" types-re ":\\)?"
  5048. "\\([^]]+\\)"
  5049. "\\]"
  5050. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5051. "\\]")
  5052. org-bracket-link-analytic-regexp++
  5053. (concat
  5054. "\\[\\["
  5055. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5056. "\\([^]]+\\)"
  5057. "\\]"
  5058. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5059. "\\]")
  5060. org-any-link-re
  5061. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5062. org-angle-link-re "\\)\\|\\("
  5063. org-plain-link-re "\\)"))))
  5064. (org-make-link-regexps)
  5065. (defvar org-emph-face nil)
  5066. (defun org-do-emphasis-faces (limit)
  5067. "Run through the buffer and emphasize strings."
  5068. (let (rtn a)
  5069. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5070. (let* ((border (char-after (match-beginning 3)))
  5071. (bre (regexp-quote (char-to-string border))))
  5072. (if (and (not (= border (char-after (match-beginning 4))))
  5073. (not (save-match-data
  5074. (string-match (concat bre ".*" bre)
  5075. (replace-regexp-in-string
  5076. "\n" " "
  5077. (substring (match-string 2) 1 -1))))))
  5078. (progn
  5079. (setq rtn t)
  5080. (setq a (assoc (match-string 3) org-emphasis-alist))
  5081. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5082. 'face
  5083. (nth 1 a))
  5084. (and (nth 2 a)
  5085. (org-remove-flyspell-overlays-in
  5086. (match-beginning 0) (match-end 0)))
  5087. (add-text-properties (match-beginning 2) (match-end 2)
  5088. '(font-lock-multiline t org-emphasis t))
  5089. (when org-hide-emphasis-markers
  5090. (add-text-properties (match-end 4) (match-beginning 5)
  5091. '(invisible org-link))
  5092. (add-text-properties (match-beginning 3) (match-end 3)
  5093. '(invisible org-link))))))
  5094. (goto-char (1+ (match-beginning 0))))
  5095. rtn))
  5096. (defun org-emphasize (&optional char)
  5097. "Insert or change an emphasis, i.e. a font like bold or italic.
  5098. If there is an active region, change that region to a new emphasis.
  5099. If there is no region, just insert the marker characters and position
  5100. the cursor between them.
  5101. CHAR should be the marker character. If it is a space, it means to
  5102. remove the emphasis of the selected region.
  5103. If CHAR is not given (for example in an interactive call) it will be
  5104. prompted for."
  5105. (interactive)
  5106. (let ((erc org-emphasis-regexp-components)
  5107. (prompt "")
  5108. (string "") beg end move c s)
  5109. (if (org-region-active-p)
  5110. (setq beg (region-beginning) end (region-end)
  5111. string (buffer-substring beg end))
  5112. (setq move t))
  5113. (unless char
  5114. (message "Emphasis marker or tag: [%s]"
  5115. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5116. (setq char (read-char-exclusive)))
  5117. (if (equal char ?\ )
  5118. (setq s "" move nil)
  5119. (unless (assoc (char-to-string char) org-emphasis-alist)
  5120. (user-error "No such emphasis marker: \"%c\"" char))
  5121. (setq s (char-to-string char)))
  5122. (while (and (> (length string) 1)
  5123. (equal (substring string 0 1) (substring string -1))
  5124. (assoc (substring string 0 1) org-emphasis-alist))
  5125. (setq string (substring string 1 -1)))
  5126. (setq string (concat s string s))
  5127. (if beg (delete-region beg end))
  5128. (unless (or (bolp)
  5129. (string-match (concat "[" (nth 0 erc) "\n]")
  5130. (char-to-string (char-before (point)))))
  5131. (insert " "))
  5132. (unless (or (eobp)
  5133. (string-match (concat "[" (nth 1 erc) "\n]")
  5134. (char-to-string (char-after (point)))))
  5135. (insert " ") (backward-char 1))
  5136. (insert string)
  5137. (and move (backward-char 1))))
  5138. (defconst org-nonsticky-props
  5139. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5140. (defsubst org-rear-nonsticky-at (pos)
  5141. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5142. (defun org-activate-plain-links (limit)
  5143. "Add link properties for plain links."
  5144. (let (f hl)
  5145. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5146. (not (member 'org-tag
  5147. (get-text-property (1- (match-beginning 0)) 'face)))
  5148. (not (org-in-src-block-p)))
  5149. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5150. (setq f (get-text-property (match-beginning 0) 'face))
  5151. (setq hl (org-match-string-no-properties 0))
  5152. (if (or (eq f 'org-tag)
  5153. (and (listp f) (memq 'org-tag f)))
  5154. nil
  5155. (add-text-properties (match-beginning 0) (match-end 0)
  5156. (list 'mouse-face 'highlight
  5157. 'face 'org-link
  5158. 'htmlize-link `(:uri ,hl)
  5159. 'keymap org-mouse-map))
  5160. (org-rear-nonsticky-at (match-end 0)))
  5161. t)))
  5162. (defun org-activate-code (limit)
  5163. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5164. (progn
  5165. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5166. (remove-text-properties (match-beginning 0) (match-end 0)
  5167. '(display t invisible t intangible t))
  5168. t)))
  5169. (defcustom org-src-fontify-natively t
  5170. "When non-nil, fontify code in code blocks."
  5171. :type 'boolean
  5172. :version "24.4"
  5173. :package-version '(Org . "8.3")
  5174. :group 'org-appearance
  5175. :group 'org-babel)
  5176. (defcustom org-allow-promoting-top-level-subtree nil
  5177. "When non-nil, allow promoting a top level subtree.
  5178. The leading star of the top level headline will be replaced
  5179. by a #."
  5180. :type 'boolean
  5181. :version "24.1"
  5182. :group 'org-appearance)
  5183. (defun org-fontify-meta-lines-and-blocks (limit)
  5184. (condition-case nil
  5185. (org-fontify-meta-lines-and-blocks-1 limit)
  5186. (error (message "org-mode fontification error"))))
  5187. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5188. "Fontify #+ lines and blocks."
  5189. (let ((case-fold-search t))
  5190. (if (re-search-forward
  5191. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5192. limit t)
  5193. (let ((beg (match-beginning 0))
  5194. (block-start (match-end 0))
  5195. (block-end nil)
  5196. (lang (match-string 7))
  5197. (beg1 (line-beginning-position 2))
  5198. (dc1 (downcase (match-string 2)))
  5199. (dc3 (downcase (match-string 3)))
  5200. end end1 quoting block-type ovl)
  5201. (cond
  5202. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5203. ;; a single line of backend-specific content
  5204. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5205. (remove-text-properties (match-beginning 0) (match-end 0)
  5206. '(display t invisible t intangible t))
  5207. (add-text-properties (match-beginning 1) (match-end 3)
  5208. '(font-lock-fontified t face org-meta-line))
  5209. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5210. '(font-lock-fontified t face org-block))
  5211. ; for backend-specific code
  5212. t)
  5213. ((and (match-end 4) (equal dc3 "+begin"))
  5214. ;; Truly a block
  5215. (setq block-type (downcase (match-string 5))
  5216. quoting (member block-type org-protecting-blocks))
  5217. (when (re-search-forward
  5218. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5219. nil t) ;; on purpose, we look further than LIMIT
  5220. (setq end (min (point-max) (match-end 0))
  5221. end1 (min (point-max) (1- (match-beginning 0))))
  5222. (setq block-end (match-beginning 0))
  5223. (when quoting
  5224. (org-remove-flyspell-overlays-in beg1 end1)
  5225. (remove-text-properties beg end
  5226. '(display t invisible t intangible t)))
  5227. (add-text-properties
  5228. beg end '(font-lock-fontified t font-lock-multiline t))
  5229. (add-text-properties beg beg1 '(face org-meta-line))
  5230. (org-remove-flyspell-overlays-in beg beg1)
  5231. (add-text-properties ; For end_src
  5232. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5233. (org-remove-flyspell-overlays-in end1 end)
  5234. (cond
  5235. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5236. (org-src-font-lock-fontify-block lang block-start block-end)
  5237. (add-text-properties beg1 block-end '(src-block t)))
  5238. (quoting
  5239. (add-text-properties beg1 (min (point-max) (1+ end1))
  5240. '(face org-block))) ; end of source block
  5241. ((not org-fontify-quote-and-verse-blocks))
  5242. ((string= block-type "quote")
  5243. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5244. ((string= block-type "verse")
  5245. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5246. (add-text-properties beg beg1 '(face org-block-begin-line))
  5247. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5248. '(face org-block-end-line))
  5249. t))
  5250. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5251. (org-remove-flyspell-overlays-in
  5252. (match-beginning 0)
  5253. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5254. (add-text-properties
  5255. beg (match-end 3)
  5256. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5257. '(font-lock-fontified t invisible t)
  5258. '(font-lock-fontified t face org-document-info-keyword)))
  5259. (add-text-properties
  5260. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5261. (if (string-equal dc1 "+title:")
  5262. '(font-lock-fontified t face org-document-title)
  5263. '(font-lock-fontified t face org-document-info))))
  5264. ((or (equal dc1 "+results")
  5265. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5266. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5267. "+call:" "+header:" "+headers:" "+name:"))
  5268. (and (match-end 4) (equal dc3 "+attr")))
  5269. (org-remove-flyspell-overlays-in
  5270. (match-beginning 0)
  5271. (if (equal "+caption:" dc1) (match-end 2) (match-end 0)))
  5272. (add-text-properties
  5273. beg (match-end 0)
  5274. '(font-lock-fontified t face org-meta-line))
  5275. t)
  5276. ((member dc3 '(" " ""))
  5277. (org-remove-flyspell-overlays-in beg (match-end 0))
  5278. (add-text-properties
  5279. beg (match-end 0)
  5280. '(font-lock-fontified t face font-lock-comment-face)))
  5281. (t ;; just any other in-buffer setting, but not indented
  5282. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5283. (add-text-properties
  5284. beg (match-end 0)
  5285. '(font-lock-fontified t face org-meta-line))
  5286. t))))))
  5287. (defun org-fontify-drawers (limit)
  5288. "Fontify drawers."
  5289. (when (re-search-forward org-drawer-regexp limit t)
  5290. (add-text-properties
  5291. (match-beginning 0) (match-end 0)
  5292. '(font-lock-fontified t face org-special-keyword))
  5293. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5294. t))
  5295. (defun org-fontify-macros (limit)
  5296. "Fontify macros."
  5297. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5298. (add-text-properties
  5299. (match-beginning 0) (match-end 0)
  5300. '(font-lock-fontified t face org-macro))
  5301. (when org-hide-macro-markers
  5302. (add-text-properties (match-end 2) (match-beginning 2)
  5303. '(invisible t))
  5304. (add-text-properties (match-beginning 1) (match-end 1)
  5305. '(invisible t)))
  5306. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5307. t))
  5308. (defun org-activate-angle-links (limit)
  5309. "Add text properties for angle links."
  5310. (if (and (re-search-forward org-angle-link-re limit t)
  5311. (not (org-in-src-block-p)))
  5312. (progn
  5313. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5314. (add-text-properties (match-beginning 0) (match-end 0)
  5315. (list 'mouse-face 'highlight
  5316. 'keymap org-mouse-map))
  5317. (org-rear-nonsticky-at (match-end 0))
  5318. t)))
  5319. (defun org-activate-footnote-links (limit)
  5320. "Add text properties for footnotes."
  5321. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5322. (when fn
  5323. (let* ((beg (nth 1 fn))
  5324. (end (nth 2 fn))
  5325. (label (car fn))
  5326. (referencep (/= (line-beginning-position) beg)))
  5327. (when (and referencep (nth 3 fn))
  5328. (save-excursion
  5329. (goto-char beg)
  5330. (search-forward (or label "fn:"))
  5331. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5332. (add-text-properties beg end
  5333. (list 'mouse-face 'highlight
  5334. 'keymap org-mouse-map
  5335. 'help-echo
  5336. (if referencep "Footnote reference"
  5337. "Footnote definition")
  5338. 'font-lock-fontified t
  5339. 'font-lock-multiline t
  5340. 'face 'org-footnote))))))
  5341. (defun org-activate-bracket-links (limit)
  5342. "Add text properties for bracketed links."
  5343. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5344. (not (org-in-src-block-p)))
  5345. (let* ((hl (org-match-string-no-properties 1))
  5346. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5347. (ip (org-maybe-intangible
  5348. (list 'invisible 'org-link
  5349. 'keymap org-mouse-map 'mouse-face 'highlight
  5350. 'font-lock-multiline t 'help-echo help
  5351. 'htmlize-link `(:uri ,hl))))
  5352. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5353. 'font-lock-multiline t 'help-echo help
  5354. 'htmlize-link `(:uri ,hl))))
  5355. ;; We need to remove the invisible property here. Table narrowing
  5356. ;; may have made some of this invisible.
  5357. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5358. (remove-text-properties (match-beginning 0) (match-end 0)
  5359. '(invisible nil))
  5360. (if (match-end 3)
  5361. (progn
  5362. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5363. (org-rear-nonsticky-at (match-beginning 3))
  5364. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5365. (org-rear-nonsticky-at (match-end 3))
  5366. (add-text-properties (match-end 3) (match-end 0) ip)
  5367. (org-rear-nonsticky-at (match-end 0)))
  5368. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5369. (org-rear-nonsticky-at (match-beginning 1))
  5370. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5371. (org-rear-nonsticky-at (match-end 1))
  5372. (add-text-properties (match-end 1) (match-end 0) ip)
  5373. (org-rear-nonsticky-at (match-end 0)))
  5374. t)))
  5375. (defun org-activate-dates (limit)
  5376. "Add text properties for dates."
  5377. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5378. (not (equal (char-before (match-beginning 0)) 91)))
  5379. (progn
  5380. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5381. (add-text-properties (match-beginning 0) (match-end 0)
  5382. (list 'mouse-face 'highlight
  5383. 'keymap org-mouse-map))
  5384. (org-rear-nonsticky-at (match-end 0))
  5385. (when org-display-custom-times
  5386. (if (match-end 3)
  5387. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5388. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5389. t)))
  5390. (defvar org-target-link-regexp nil
  5391. "Regular expression matching radio targets in plain text.")
  5392. (make-variable-buffer-local 'org-target-link-regexp)
  5393. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5394. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5395. border border border))
  5396. "Regular expression matching a link target.")
  5397. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5398. "Regular expression matching a radio target.")
  5399. (defconst org-any-target-regexp
  5400. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5401. "Regular expression matching any target.")
  5402. (defun org-activate-target-links (limit)
  5403. "Add text properties for target matches."
  5404. (when org-target-link-regexp
  5405. (let ((case-fold-search t))
  5406. (if (re-search-forward org-target-link-regexp limit t)
  5407. (progn
  5408. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5409. (add-text-properties (match-beginning 1) (match-end 1)
  5410. (list 'mouse-face 'highlight
  5411. 'keymap org-mouse-map
  5412. 'help-echo "Radio target link"
  5413. 'org-linked-text t))
  5414. (org-rear-nonsticky-at (match-end 1))
  5415. t)))))
  5416. (defun org-update-radio-target-regexp ()
  5417. "Find all radio targets in this file and update the regular expression.
  5418. Also refresh fontification if needed."
  5419. (interactive)
  5420. (let ((old-regexp org-target-link-regexp)
  5421. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5422. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5423. (targets
  5424. (org-with-wide-buffer
  5425. (goto-char (point-min))
  5426. (let (rtn)
  5427. (while (re-search-forward org-radio-target-regexp nil t)
  5428. ;; Make sure point is really within the object.
  5429. (backward-char)
  5430. (let ((obj (org-element-context)))
  5431. (when (eq (org-element-type obj) 'radio-target)
  5432. (add-to-list 'rtn (org-element-property :value obj)))))
  5433. rtn))))
  5434. (setq org-target-link-regexp
  5435. (and targets
  5436. (concat before-re
  5437. (mapconcat
  5438. (lambda (x)
  5439. (replace-regexp-in-string
  5440. " +" "\\s-+" (regexp-quote x) t t))
  5441. targets
  5442. "\\|")
  5443. after-re)))
  5444. (unless (equal old-regexp org-target-link-regexp)
  5445. ;; Clean-up cache.
  5446. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5447. ((not org-target-link-regexp) old-regexp)
  5448. (t
  5449. (concat before-re
  5450. (mapconcat
  5451. (lambda (re)
  5452. (substring re (length before-re)
  5453. (- (length after-re))))
  5454. (list old-regexp org-target-link-regexp)
  5455. "\\|")
  5456. after-re)))))
  5457. (org-with-wide-buffer
  5458. (goto-char (point-min))
  5459. (while (re-search-forward regexp nil t)
  5460. (org-element-cache-refresh (match-beginning 1)))))
  5461. ;; Re fontify buffer.
  5462. (when (memq 'radio org-highlight-links)
  5463. (org-restart-font-lock)))))
  5464. (defun org-hide-wide-columns (limit)
  5465. (let (s e)
  5466. (setq s (text-property-any (point) (or limit (point-max))
  5467. 'org-cwidth t))
  5468. (when s
  5469. (setq e (next-single-property-change s 'org-cwidth))
  5470. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5471. (goto-char e)
  5472. t)))
  5473. (defvar org-latex-and-related-regexp nil
  5474. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5475. (defun org-compute-latex-and-related-regexp ()
  5476. "Compute regular expression for LaTeX, entities and sub/superscript.
  5477. Result depends on variable `org-highlight-latex-and-related'."
  5478. (org-set-local
  5479. 'org-latex-and-related-regexp
  5480. (let* ((re-sub
  5481. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5482. ((eq org-use-sub-superscripts '{})
  5483. (list org-match-substring-with-braces-regexp))
  5484. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5485. (re-latex
  5486. (when (memq 'latex org-highlight-latex-and-related)
  5487. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5488. (delq nil
  5489. (mapcar (lambda (x)
  5490. (and (member (car x) matchers) (nth 1 x)))
  5491. org-latex-regexps)))))
  5492. (re-entities
  5493. (when (memq 'entities org-highlight-latex-and-related)
  5494. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5495. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5496. (defun org-do-latex-and-related (limit)
  5497. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5498. LIMIT bounds the search for syntax to highlight. Stop at first
  5499. highlighted object, if any. Return t if some highlighting was
  5500. done, nil otherwise."
  5501. (when (org-string-nw-p org-latex-and-related-regexp)
  5502. (catch 'found
  5503. (while (re-search-forward org-latex-and-related-regexp limit t)
  5504. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5505. 'face))
  5506. '(org-code org-verbatim underline))
  5507. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5508. '(?_ ?^))
  5509. 1
  5510. 0)))
  5511. (font-lock-prepend-text-property
  5512. (+ offset (match-beginning 0)) (match-end 0)
  5513. 'face 'org-latex-and-related)
  5514. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5515. '(font-lock-multiline t)))
  5516. (throw 'found t)))
  5517. nil)))
  5518. (defun org-restart-font-lock ()
  5519. "Restart `font-lock-mode', to force refontification."
  5520. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5521. (font-lock-mode -1)
  5522. (font-lock-mode 1)))
  5523. (defun org-activate-tags (limit)
  5524. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5525. (progn
  5526. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5527. (add-text-properties (match-beginning 1) (match-end 1)
  5528. (list 'mouse-face 'highlight
  5529. 'keymap org-mouse-map))
  5530. (org-rear-nonsticky-at (match-end 1))
  5531. t)))
  5532. (defun org-outline-level ()
  5533. "Compute the outline level of the heading at point.
  5534. If this is called at a normal headline, the level is the number of stars.
  5535. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5536. (save-excursion
  5537. (if (not (ignore-errors (org-back-to-heading t)))
  5538. 0
  5539. (looking-at org-outline-regexp)
  5540. (1- (- (match-end 0) (match-beginning 0))))))
  5541. (defvar org-font-lock-keywords nil)
  5542. (defsubst org-re-property (property &optional literal allow-null)
  5543. "Return a regexp matching a PROPERTY line.
  5544. Match group 3 will be set to the value if it exists."
  5545. (concat "^\\(?4:[ \t]*\\)\\(?1::\\(?2:"
  5546. (if literal property (regexp-quote property))
  5547. "\\):\\)[ \t]+\\(?3:[^ \t\r\n]"
  5548. (if allow-null "*")
  5549. ".*?\\)\\(?5:[ \t]*\\)$"))
  5550. (defconst org-property-re
  5551. (org-re-property ".*?" 'literal t)
  5552. "Regular expression matching a property line.
  5553. There are four matching groups:
  5554. 1: :PROPKEY: including the leading and trailing colon,
  5555. 2: PROPKEY without the leading and trailing colon,
  5556. 3: PROPVAL without leading or trailing spaces,
  5557. 4: the indentation of the current line,
  5558. 5: trailing whitespace.")
  5559. (defvar org-font-lock-hook nil
  5560. "Functions to be called for special font lock stuff.")
  5561. (defvar org-font-lock-set-keywords-hook nil
  5562. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5563. This is called after `org-font-lock-extra-keywords' is defined, but before
  5564. it is installed to be used by font lock. This can be useful if something
  5565. needs to be inserted at a specific position in the font-lock sequence.")
  5566. (defun org-font-lock-hook (limit)
  5567. "Run `org-font-lock-hook' within LIMIT."
  5568. (run-hook-with-args 'org-font-lock-hook limit))
  5569. (defun org-set-font-lock-defaults ()
  5570. "Set font lock defaults for the current buffer."
  5571. (let* ((em org-fontify-emphasized-text)
  5572. (lk org-highlight-links)
  5573. (org-font-lock-extra-keywords
  5574. (list
  5575. ;; Call the hook
  5576. '(org-font-lock-hook)
  5577. ;; Headlines
  5578. `(,(if org-fontify-whole-heading-line
  5579. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5580. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5581. (1 (org-get-level-face 1))
  5582. (2 (org-get-level-face 2))
  5583. (3 (org-get-level-face 3)))
  5584. ;; Table lines
  5585. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5586. (1 'org-table t))
  5587. ;; Table internals
  5588. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5589. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5590. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5591. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5592. ;; Drawers
  5593. '(org-fontify-drawers)
  5594. ;; Properties
  5595. (list org-property-re
  5596. '(1 'org-special-keyword t)
  5597. '(3 'org-property-value t))
  5598. ;; Links
  5599. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5600. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5601. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5602. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5603. (if (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5604. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5605. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5606. ;; Targets.
  5607. (list org-any-target-regexp '(0 'org-target t))
  5608. ;; Diary sexps.
  5609. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5610. ;; Macro
  5611. '(org-fontify-macros)
  5612. '(org-hide-wide-columns (0 nil append))
  5613. ;; TODO keyword
  5614. (list (format org-heading-keyword-regexp-format
  5615. org-todo-regexp)
  5616. '(2 (org-get-todo-face 2) t))
  5617. ;; DONE
  5618. (if org-fontify-done-headline
  5619. (list (format org-heading-keyword-regexp-format
  5620. (concat
  5621. "\\(?:"
  5622. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5623. "\\)"))
  5624. '(2 'org-headline-done t))
  5625. nil)
  5626. ;; Priorities
  5627. '(org-font-lock-add-priority-faces)
  5628. ;; Tags
  5629. '(org-font-lock-add-tag-faces)
  5630. ;; Tags groups
  5631. (if (and org-group-tags org-tag-groups-alist)
  5632. (list (concat org-outline-regexp-bol ".+\\(:"
  5633. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5634. ":\\).*$")
  5635. '(1 'org-tag-group prepend)))
  5636. ;; Special keywords
  5637. (list (concat "\\<" org-comment-string) '(0 'org-special-keyword t))
  5638. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5639. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5640. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5641. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5642. ;; Emphasis
  5643. (if em
  5644. (if (featurep 'xemacs)
  5645. '(org-do-emphasis-faces (0 nil append))
  5646. '(org-do-emphasis-faces)))
  5647. ;; Checkboxes
  5648. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5649. 1 'org-checkbox prepend)
  5650. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5651. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5652. (0 (org-get-checkbox-statistics-face) t)))
  5653. ;; Description list items
  5654. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5655. 1 'org-list-dt prepend)
  5656. ;; ARCHIVEd headings
  5657. (list (concat
  5658. org-outline-regexp-bol
  5659. "\\(.*:" org-archive-tag ":.*\\)")
  5660. '(1 'org-archived prepend))
  5661. ;; Specials
  5662. '(org-do-latex-and-related)
  5663. '(org-fontify-entities)
  5664. '(org-raise-scripts)
  5665. ;; Code
  5666. '(org-activate-code (1 'org-code t))
  5667. ;; COMMENT
  5668. (list (format
  5669. "^\\*\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5670. org-todo-regexp
  5671. org-comment-string)
  5672. '(9 'org-special-keyword t))
  5673. ;; Blocks and meta lines
  5674. '(org-fontify-meta-lines-and-blocks))))
  5675. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5676. (run-hooks 'org-font-lock-set-keywords-hook)
  5677. ;; Now set the full font-lock-keywords
  5678. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5679. (org-set-local 'font-lock-defaults
  5680. '(org-font-lock-keywords t nil nil backward-paragraph))
  5681. (kill-local-variable 'font-lock-keywords) nil))
  5682. (defun org-toggle-pretty-entities ()
  5683. "Toggle the composition display of entities as UTF8 characters."
  5684. (interactive)
  5685. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5686. (org-restart-font-lock)
  5687. (if org-pretty-entities
  5688. (message "Entities are now displayed as UTF8 characters")
  5689. (save-restriction
  5690. (widen)
  5691. (org-decompose-region (point-min) (point-max))
  5692. (message "Entities are now displayed as plain text"))))
  5693. (defvar org-custom-properties-overlays nil
  5694. "List of overlays used for custom properties.")
  5695. (make-variable-buffer-local 'org-custom-properties-overlays)
  5696. (defun org-toggle-custom-properties-visibility ()
  5697. "Display or hide properties in `org-custom-properties'."
  5698. (interactive)
  5699. (if org-custom-properties-overlays
  5700. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5701. (setq org-custom-properties-overlays nil))
  5702. (unless (not org-custom-properties)
  5703. (save-excursion
  5704. (save-restriction
  5705. (widen)
  5706. (goto-char (point-min))
  5707. (while (re-search-forward org-property-re nil t)
  5708. (mapc (lambda(p)
  5709. (when (equal p (substring (match-string 1) 1 -1))
  5710. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5711. (overlay-put o 'invisible t)
  5712. (overlay-put o 'org-custom-property t)
  5713. (push o org-custom-properties-overlays))))
  5714. org-custom-properties)))))))
  5715. (defun org-fontify-entities (limit)
  5716. "Find an entity to fontify."
  5717. (let (ee)
  5718. (when org-pretty-entities
  5719. (catch 'match
  5720. (while (re-search-forward
  5721. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5722. limit t)
  5723. (if (and (not (org-at-comment-p))
  5724. (setq ee (org-entity-get (match-string 1)))
  5725. (= (length (nth 6 ee)) 1))
  5726. (let*
  5727. ((end (if (equal (match-string 2) "{}")
  5728. (match-end 2)
  5729. (match-end 1))))
  5730. (add-text-properties
  5731. (match-beginning 0) end
  5732. (list 'font-lock-fontified t))
  5733. (compose-region (match-beginning 0) end
  5734. (nth 6 ee) nil)
  5735. (backward-char 1)
  5736. (throw 'match t))))
  5737. nil))))
  5738. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5739. "Fontify string S like in Org-mode."
  5740. (with-temp-buffer
  5741. (insert s)
  5742. (let ((org-odd-levels-only odd-levels))
  5743. (org-mode)
  5744. (font-lock-ensure)
  5745. (buffer-string))))
  5746. (defvar org-m nil)
  5747. (defvar org-l nil)
  5748. (defvar org-f nil)
  5749. (defun org-get-level-face (n)
  5750. "Get the right face for match N in font-lock matching of headlines."
  5751. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5752. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5753. (if org-cycle-level-faces
  5754. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5755. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5756. (cond
  5757. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5758. ((eq n 2) org-f)
  5759. (t (if org-level-color-stars-only nil org-f))))
  5760. (defun org-get-todo-face (kwd)
  5761. "Get the right face for a TODO keyword KWD.
  5762. If KWD is a number, get the corresponding match group."
  5763. (if (numberp kwd) (setq kwd (match-string kwd)))
  5764. (or (org-face-from-face-or-color
  5765. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5766. (and (member kwd org-done-keywords) 'org-done)
  5767. 'org-todo))
  5768. (defun org-face-from-face-or-color (context inherit face-or-color)
  5769. "Create a face list that inherits INHERIT, but sets the foreground color.
  5770. When FACE-OR-COLOR is not a string, just return it."
  5771. (if (stringp face-or-color)
  5772. (list :inherit inherit
  5773. (cdr (assoc context org-faces-easy-properties))
  5774. face-or-color)
  5775. face-or-color))
  5776. (defun org-font-lock-add-tag-faces (limit)
  5777. "Add the special tag faces."
  5778. (when (and org-tag-faces org-tags-special-faces-re)
  5779. (while (re-search-forward org-tags-special-faces-re limit t)
  5780. (add-text-properties (match-beginning 1) (match-end 1)
  5781. (list 'face (org-get-tag-face 1)
  5782. 'font-lock-fontified t))
  5783. (backward-char 1))))
  5784. (defun org-font-lock-add-priority-faces (limit)
  5785. "Add the special priority faces."
  5786. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5787. (when (save-match-data (org-at-heading-p))
  5788. (add-text-properties
  5789. (match-beginning 0) (match-end 0)
  5790. (list 'face (or (org-face-from-face-or-color
  5791. 'priority 'org-priority
  5792. (cdr (assoc (char-after (match-beginning 1))
  5793. org-priority-faces)))
  5794. 'org-priority)
  5795. 'font-lock-fontified t)))))
  5796. (defun org-get-tag-face (kwd)
  5797. "Get the right face for a TODO keyword KWD.
  5798. If KWD is a number, get the corresponding match group."
  5799. (if (numberp kwd) (setq kwd (match-string kwd)))
  5800. (or (org-face-from-face-or-color
  5801. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5802. 'org-tag))
  5803. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5804. "Remove fontification and activation overlays from links."
  5805. (font-lock-default-unfontify-region beg end)
  5806. (let* ((buffer-undo-list t)
  5807. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5808. (inhibit-modification-hooks t)
  5809. deactivate-mark buffer-file-name buffer-file-truename)
  5810. (org-decompose-region beg end)
  5811. (remove-text-properties beg end
  5812. '(mouse-face t keymap t org-linked-text t
  5813. invisible t intangible t
  5814. org-emphasis t))
  5815. (org-remove-font-lock-display-properties beg end)))
  5816. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5817. ((raise 0.3) (height 0.7))
  5818. ((raise -0.5))
  5819. ((raise 0.5)))
  5820. "Display properties for showing superscripts and subscripts.")
  5821. (defun org-remove-font-lock-display-properties (beg end)
  5822. "Remove specific display properties that have been added by font lock.
  5823. The will remove the raise properties that are used to show superscripts
  5824. and subscripts."
  5825. (let (next prop)
  5826. (while (< beg end)
  5827. (setq next (next-single-property-change beg 'display nil end)
  5828. prop (get-text-property beg 'display))
  5829. (if (member prop org-script-display)
  5830. (put-text-property beg next 'display nil))
  5831. (setq beg next))))
  5832. (defun org-raise-scripts (limit)
  5833. "Add raise properties to sub/superscripts."
  5834. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5835. (if (re-search-forward
  5836. (if (eq org-use-sub-superscripts t)
  5837. org-match-substring-regexp
  5838. org-match-substring-with-braces-regexp)
  5839. limit t)
  5840. (let* ((pos (point)) table-p comment-p
  5841. (mpos (match-beginning 3))
  5842. (emph-p (get-text-property mpos 'org-emphasis))
  5843. (link-p (get-text-property mpos 'mouse-face))
  5844. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5845. (goto-char (point-at-bol))
  5846. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5847. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5848. (goto-char pos)
  5849. ;; Handle a_b^c
  5850. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5851. (if (or comment-p emph-p link-p keyw-p)
  5852. t
  5853. (put-text-property (match-beginning 3) (match-end 0)
  5854. 'display
  5855. (if (equal (char-after (match-beginning 2)) ?^)
  5856. (nth (if table-p 3 1) org-script-display)
  5857. (nth (if table-p 2 0) org-script-display)))
  5858. (add-text-properties (match-beginning 2) (match-end 2)
  5859. (list 'invisible t
  5860. 'org-dwidth t 'org-dwidth-n 1))
  5861. (if (and (eq (char-after (match-beginning 3)) ?{)
  5862. (eq (char-before (match-end 3)) ?}))
  5863. (progn
  5864. (add-text-properties
  5865. (match-beginning 3) (1+ (match-beginning 3))
  5866. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5867. (add-text-properties
  5868. (1- (match-end 3)) (match-end 3)
  5869. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5870. t)))))
  5871. ;;;; Visibility cycling, including org-goto and indirect buffer
  5872. ;;; Cycling
  5873. (defvar org-cycle-global-status nil)
  5874. (make-variable-buffer-local 'org-cycle-global-status)
  5875. (put 'org-cycle-global-status 'org-state t)
  5876. (defvar org-cycle-subtree-status nil)
  5877. (make-variable-buffer-local 'org-cycle-subtree-status)
  5878. (put 'org-cycle-subtree-status 'org-state t)
  5879. (defvar org-inlinetask-min-level)
  5880. (defun org-unlogged-message (&rest args)
  5881. "Display a message, but avoid logging it in the *Messages* buffer."
  5882. (let ((message-log-max nil))
  5883. (apply 'message args)))
  5884. ;;;###autoload
  5885. (defun org-cycle (&optional arg)
  5886. "TAB-action and visibility cycling for Org-mode.
  5887. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5888. is outline visibility cycling, but it also invokes other actions
  5889. in special contexts.
  5890. - When this function is called with a prefix argument, rotate the entire
  5891. buffer through 3 states (global cycling)
  5892. 1. OVERVIEW: Show only top-level headlines.
  5893. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5894. 3. SHOW ALL: Show everything.
  5895. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5896. determined by the variable `org-startup-folded', and by any VISIBILITY
  5897. properties in the buffer.
  5898. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5899. including any drawers.
  5900. - When inside a table, re-align the table and move to the next field.
  5901. - When point is at the beginning of a headline, rotate the subtree started
  5902. by this line through 3 different states (local cycling)
  5903. 1. FOLDED: Only the main headline is shown.
  5904. 2. CHILDREN: The main headline and the direct children are shown.
  5905. From this state, you can move to one of the children
  5906. and zoom in further.
  5907. 3. SUBTREE: Show the entire subtree, including body text.
  5908. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5909. - When point is at the beginning of an empty headline and the variable
  5910. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5911. of the headline by demoting and promoting it to likely levels. This
  5912. speeds up creation document structure by pressing TAB once or several
  5913. times right after creating a new headline.
  5914. - When there is a numeric prefix, go up to a heading with level ARG, do
  5915. a `show-subtree' and return to the previous cursor position. If ARG
  5916. is negative, go up that many levels.
  5917. - When point is not at the beginning of a headline, execute the global
  5918. binding for TAB, which is re-indenting the line. See the option
  5919. `org-cycle-emulate-tab' for details.
  5920. - Special case: if point is at the beginning of the buffer and there is
  5921. no headline in line 1, this function will act as if called with prefix arg
  5922. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5923. But only if also the variable `org-cycle-global-at-bob' is t."
  5924. (interactive "P")
  5925. (org-load-modules-maybe)
  5926. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5927. (and org-cycle-level-after-item/entry-creation
  5928. (or (org-cycle-level)
  5929. (org-cycle-item-indentation))))
  5930. (let* ((limit-level
  5931. (or org-cycle-max-level
  5932. (and (boundp 'org-inlinetask-min-level)
  5933. org-inlinetask-min-level
  5934. (1- org-inlinetask-min-level))))
  5935. (nstars (and limit-level
  5936. (if org-odd-levels-only
  5937. (and limit-level (1- (* limit-level 2)))
  5938. limit-level)))
  5939. (org-outline-regexp
  5940. (if (not (derived-mode-p 'org-mode))
  5941. outline-regexp
  5942. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5943. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5944. (not (looking-at org-outline-regexp))))
  5945. (org-cycle-hook
  5946. (if bob-special
  5947. (delq 'org-optimize-window-after-visibility-change
  5948. (copy-sequence org-cycle-hook))
  5949. org-cycle-hook))
  5950. (pos (point)))
  5951. (if (or bob-special (equal arg '(4)))
  5952. ;; special case: use global cycling
  5953. (setq arg t))
  5954. (cond
  5955. ((equal arg '(16))
  5956. (setq last-command 'dummy)
  5957. (org-set-startup-visibility)
  5958. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5959. ((equal arg '(64))
  5960. (show-all)
  5961. (org-unlogged-message "Entire buffer visible, including drawers"))
  5962. ;; Try cdlatex TAB completion
  5963. ((org-try-cdlatex-tab))
  5964. ;; Table: enter it or move to the next field.
  5965. ((org-at-table-p 'any)
  5966. (if (org-at-table.el-p)
  5967. (message "Use C-c ' to edit table.el tables")
  5968. (if arg (org-table-edit-field t)
  5969. (org-table-justify-field-maybe)
  5970. (call-interactively 'org-table-next-field))))
  5971. ((run-hook-with-args-until-success
  5972. 'org-tab-after-check-for-table-hook))
  5973. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5974. ((eq arg t) (org-cycle-internal-global))
  5975. ;; Drawers: delegate to `org-flag-drawer'.
  5976. ((save-excursion
  5977. (beginning-of-line 1)
  5978. (looking-at org-drawer-regexp))
  5979. (org-flag-drawer ; toggle block visibility
  5980. (not (get-char-property (match-end 0) 'invisible))))
  5981. ;; Show-subtree, ARG levels up from here.
  5982. ((integerp arg)
  5983. (save-excursion
  5984. (org-back-to-heading)
  5985. (outline-up-heading (if (< arg 0) (- arg)
  5986. (- (funcall outline-level) arg)))
  5987. (org-show-subtree)))
  5988. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5989. ((and (featurep 'org-inlinetask)
  5990. (org-inlinetask-at-task-p)
  5991. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5992. (org-inlinetask-toggle-visibility))
  5993. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5994. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5995. (save-excursion (beginning-of-line 1)
  5996. (looking-at org-outline-regexp)))
  5997. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5998. (org-cycle-internal-local))
  5999. ;; From there: TAB emulation and template completion.
  6000. (buffer-read-only (org-back-to-heading))
  6001. ((run-hook-with-args-until-success
  6002. 'org-tab-after-check-for-cycling-hook))
  6003. ((org-try-structure-completion))
  6004. ((run-hook-with-args-until-success
  6005. 'org-tab-before-tab-emulation-hook))
  6006. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  6007. (or (not (bolp))
  6008. (not (looking-at org-outline-regexp))))
  6009. (call-interactively (global-key-binding "\t")))
  6010. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  6011. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  6012. (or (and (eq org-cycle-emulate-tab 'white)
  6013. (= (match-end 0) (point-at-eol)))
  6014. (and (eq org-cycle-emulate-tab 'whitestart)
  6015. (>= (match-end 0) pos))))
  6016. t
  6017. (eq org-cycle-emulate-tab t))
  6018. (call-interactively (global-key-binding "\t")))
  6019. (t (save-excursion
  6020. (org-back-to-heading)
  6021. (org-cycle)))))))
  6022. (defun org-cycle-internal-global ()
  6023. "Do the global cycling action."
  6024. ;; Hack to avoid display of messages for .org attachments in Gnus
  6025. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6026. (cond
  6027. ((and (eq last-command this-command)
  6028. (eq org-cycle-global-status 'overview))
  6029. ;; We just created the overview - now do table of contents
  6030. ;; This can be slow in very large buffers, so indicate action
  6031. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6032. (unless ga (org-unlogged-message "CONTENTS..."))
  6033. (org-content)
  6034. (unless ga (org-unlogged-message "CONTENTS...done"))
  6035. (setq org-cycle-global-status 'contents)
  6036. (run-hook-with-args 'org-cycle-hook 'contents))
  6037. ((and (eq last-command this-command)
  6038. (eq org-cycle-global-status 'contents))
  6039. ;; We just showed the table of contents - now show everything
  6040. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6041. (show-all)
  6042. (unless ga (org-unlogged-message "SHOW ALL"))
  6043. (setq org-cycle-global-status 'all)
  6044. (run-hook-with-args 'org-cycle-hook 'all))
  6045. (t
  6046. ;; Default action: go to overview
  6047. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6048. (org-overview)
  6049. (unless ga (org-unlogged-message "OVERVIEW"))
  6050. (setq org-cycle-global-status 'overview)
  6051. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6052. (defvar org-called-with-limited-levels nil
  6053. "Non-nil when `org-with-limited-levels' is currently active.")
  6054. (defun org-cycle-internal-local ()
  6055. "Do the local cycling action."
  6056. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6057. ;; First, determine end of headline (EOH), end of subtree or item
  6058. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6059. (save-excursion
  6060. (if (org-at-item-p)
  6061. (progn
  6062. (beginning-of-line)
  6063. (setq struct (org-list-struct))
  6064. (setq eoh (point-at-eol))
  6065. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6066. (setq has-children (org-list-has-child-p (point) struct)))
  6067. (org-back-to-heading)
  6068. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6069. (setq eos (save-excursion (org-end-of-subtree t t)
  6070. (when (bolp) (backward-char)) (point)))
  6071. (setq has-children
  6072. (or (save-excursion
  6073. (let ((level (funcall outline-level)))
  6074. (outline-next-heading)
  6075. (and (org-at-heading-p t)
  6076. (> (funcall outline-level) level))))
  6077. (save-excursion
  6078. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6079. ;; Determine end invisible part of buffer (EOL)
  6080. (beginning-of-line 2)
  6081. ;; XEmacs doesn't have `next-single-char-property-change'
  6082. (if (featurep 'xemacs)
  6083. (while (and (not (eobp)) ;; this is like `next-line'
  6084. (get-char-property (1- (point)) 'invisible))
  6085. (beginning-of-line 2))
  6086. (while (and (not (eobp)) ;; this is like `next-line'
  6087. (get-char-property (1- (point)) 'invisible))
  6088. (goto-char (next-single-char-property-change (point) 'invisible))
  6089. (and (eolp) (beginning-of-line 2))))
  6090. (setq eol (point)))
  6091. ;; Find out what to do next and set `this-command'
  6092. (cond
  6093. ((= eos eoh)
  6094. ;; Nothing is hidden behind this heading
  6095. (unless (org-before-first-heading-p)
  6096. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6097. (org-unlogged-message "EMPTY ENTRY")
  6098. (setq org-cycle-subtree-status nil)
  6099. (save-excursion
  6100. (goto-char eos)
  6101. (outline-next-heading)
  6102. (if (outline-invisible-p) (org-flag-heading nil))))
  6103. ((and (or (>= eol eos)
  6104. (not (string-match "\\S-" (buffer-substring eol eos))))
  6105. (or has-children
  6106. (not (setq children-skipped
  6107. org-cycle-skip-children-state-if-no-children))))
  6108. ;; Entire subtree is hidden in one line: children view
  6109. (unless (org-before-first-heading-p)
  6110. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6111. (if (org-at-item-p)
  6112. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6113. (org-show-entry)
  6114. (org-with-limited-levels (show-children))
  6115. ;; FIXME: This slows down the func way too much.
  6116. ;; How keep drawers hidden in subtree anyway?
  6117. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6118. ;; (org-cycle-hide-drawers 'subtree))
  6119. ;; Fold every list in subtree to top-level items.
  6120. (when (eq org-cycle-include-plain-lists 'integrate)
  6121. (save-excursion
  6122. (org-back-to-heading)
  6123. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6124. (beginning-of-line 1)
  6125. (let* ((struct (org-list-struct))
  6126. (prevs (org-list-prevs-alist struct))
  6127. (end (org-list-get-bottom-point struct)))
  6128. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6129. (org-list-get-all-items (point) struct prevs))
  6130. (goto-char (if (< end eos) end eos)))))))
  6131. (org-unlogged-message "CHILDREN")
  6132. (save-excursion
  6133. (goto-char eos)
  6134. (outline-next-heading)
  6135. (if (outline-invisible-p) (org-flag-heading nil)))
  6136. (setq org-cycle-subtree-status 'children)
  6137. (unless (org-before-first-heading-p)
  6138. (run-hook-with-args 'org-cycle-hook 'children)))
  6139. ((or children-skipped
  6140. (and (eq last-command this-command)
  6141. (eq org-cycle-subtree-status 'children)))
  6142. ;; We just showed the children, or no children are there,
  6143. ;; now show everything.
  6144. (unless (org-before-first-heading-p)
  6145. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6146. (outline-flag-region eoh eos nil)
  6147. (org-unlogged-message
  6148. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6149. (setq org-cycle-subtree-status 'subtree)
  6150. (unless (org-before-first-heading-p)
  6151. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6152. (t
  6153. ;; Default action: hide the subtree.
  6154. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6155. (outline-flag-region eoh eos t)
  6156. (org-unlogged-message "FOLDED")
  6157. (setq org-cycle-subtree-status 'folded)
  6158. (unless (org-before-first-heading-p)
  6159. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6160. ;;;###autoload
  6161. (defun org-global-cycle (&optional arg)
  6162. "Cycle the global visibility. For details see `org-cycle'.
  6163. With \\[universal-argument] prefix arg, switch to startup visibility.
  6164. With a numeric prefix, show all headlines up to that level."
  6165. (interactive "P")
  6166. (let ((org-cycle-include-plain-lists
  6167. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6168. (cond
  6169. ((integerp arg)
  6170. (show-all)
  6171. (hide-sublevels arg)
  6172. (setq org-cycle-global-status 'contents))
  6173. ((equal arg '(4))
  6174. (org-set-startup-visibility)
  6175. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6176. (t
  6177. (org-cycle '(4))))))
  6178. (defun org-set-startup-visibility ()
  6179. "Set the visibility required by startup options and properties."
  6180. (cond
  6181. ((eq org-startup-folded t)
  6182. (org-overview))
  6183. ((eq org-startup-folded 'content)
  6184. (org-content))
  6185. ((or (eq org-startup-folded 'showeverything)
  6186. (eq org-startup-folded nil))
  6187. (show-all)))
  6188. (unless (eq org-startup-folded 'showeverything)
  6189. (if org-hide-block-startup (org-hide-block-all))
  6190. (org-set-visibility-according-to-property 'no-cleanup)
  6191. (org-cycle-hide-archived-subtrees 'all)
  6192. (org-cycle-hide-drawers 'all)
  6193. (org-cycle-show-empty-lines t)))
  6194. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6195. "Switch subtree visibilities according to :VISIBILITY: property."
  6196. (interactive)
  6197. (let (org-show-entry-below state)
  6198. (save-excursion
  6199. (goto-char (point-min))
  6200. (while (re-search-forward
  6201. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6202. nil t)
  6203. (setq state (match-string 1))
  6204. (save-excursion
  6205. (org-back-to-heading t)
  6206. (hide-subtree)
  6207. (org-reveal)
  6208. (cond
  6209. ((equal state '("fold" "folded"))
  6210. (hide-subtree))
  6211. ((equal state "children")
  6212. (org-show-hidden-entry)
  6213. (show-children))
  6214. ((equal state "content")
  6215. (save-excursion
  6216. (save-restriction
  6217. (org-narrow-to-subtree)
  6218. (org-content))))
  6219. ((member state '("all" "showall"))
  6220. (show-subtree)))))
  6221. (unless no-cleanup
  6222. (org-cycle-hide-archived-subtrees 'all)
  6223. (org-cycle-hide-drawers 'all)
  6224. (org-cycle-show-empty-lines 'all)))))
  6225. ;; This function uses outline-regexp instead of the more fundamental
  6226. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6227. ;; buffers, where outline-regexp is needed.
  6228. (defun org-overview ()
  6229. "Switch to overview mode, showing only top-level headlines.
  6230. This shows all headlines with a level equal or greater than the level
  6231. of the first headline in the buffer. This is important, because if the
  6232. first headline is not level one, then (hide-sublevels 1) gives confusing
  6233. results."
  6234. (interactive)
  6235. (save-excursion
  6236. (let ((level
  6237. (save-excursion
  6238. (goto-char (point-min))
  6239. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6240. (progn
  6241. (goto-char (match-beginning 0))
  6242. (funcall outline-level))))))
  6243. (and level (hide-sublevels level)))))
  6244. (defun org-content (&optional arg)
  6245. "Show all headlines in the buffer, like a table of contents.
  6246. With numerical argument N, show content up to level N."
  6247. (interactive "P")
  6248. (org-overview)
  6249. (save-excursion
  6250. ;; Visit all headings and show their offspring
  6251. (and (integerp arg) (org-overview))
  6252. (goto-char (point-max))
  6253. (catch 'exit
  6254. (while (and (progn (condition-case nil
  6255. (outline-previous-visible-heading 1)
  6256. (error (goto-char (point-min))))
  6257. t)
  6258. (looking-at org-outline-regexp))
  6259. (if (integerp arg)
  6260. (show-children (1- arg))
  6261. (show-branches))
  6262. (if (bobp) (throw 'exit nil))))))
  6263. (defun org-optimize-window-after-visibility-change (state)
  6264. "Adjust the window after a change in outline visibility.
  6265. This function is the default value of the hook `org-cycle-hook'."
  6266. (when (get-buffer-window (current-buffer))
  6267. (cond
  6268. ((eq state 'content) nil)
  6269. ((eq state 'all) nil)
  6270. ((eq state 'folded) nil)
  6271. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6272. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6273. (defun org-remove-empty-overlays-at (pos)
  6274. "Remove outline overlays that do not contain non-white stuff."
  6275. (mapc
  6276. (lambda (o)
  6277. (and (eq 'outline (overlay-get o 'invisible))
  6278. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6279. (overlay-end o))))
  6280. (delete-overlay o)))
  6281. (overlays-at pos)))
  6282. (defun org-clean-visibility-after-subtree-move ()
  6283. "Fix visibility issues after moving a subtree."
  6284. ;; First, find a reasonable region to look at:
  6285. ;; Start two siblings above, end three below
  6286. (let* ((beg (save-excursion
  6287. (and (org-get-last-sibling)
  6288. (org-get-last-sibling))
  6289. (point)))
  6290. (end (save-excursion
  6291. (and (org-get-next-sibling)
  6292. (org-get-next-sibling)
  6293. (org-get-next-sibling))
  6294. (if (org-at-heading-p)
  6295. (point-at-eol)
  6296. (point))))
  6297. (level (looking-at "\\*+"))
  6298. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6299. (save-excursion
  6300. (save-restriction
  6301. (narrow-to-region beg end)
  6302. (when re
  6303. ;; Properly fold already folded siblings
  6304. (goto-char (point-min))
  6305. (while (re-search-forward re nil t)
  6306. (if (and (not (outline-invisible-p))
  6307. (save-excursion
  6308. (goto-char (point-at-eol)) (outline-invisible-p)))
  6309. (hide-entry))))
  6310. (org-cycle-show-empty-lines 'overview)
  6311. (org-cycle-hide-drawers 'overview)))))
  6312. (defun org-cycle-show-empty-lines (state)
  6313. "Show empty lines above all visible headlines.
  6314. The region to be covered depends on STATE when called through
  6315. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6316. entire buffer covered. Note that an empty line is only shown if there
  6317. are at least `org-cycle-separator-lines' empty lines before the headline."
  6318. (when (not (= org-cycle-separator-lines 0))
  6319. (save-excursion
  6320. (let* ((n (abs org-cycle-separator-lines))
  6321. (re (cond
  6322. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6323. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6324. (t (let ((ns (number-to-string (- n 2))))
  6325. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6326. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6327. beg end b e)
  6328. (cond
  6329. ((memq state '(overview contents t))
  6330. (setq beg (point-min) end (point-max)))
  6331. ((memq state '(children folded))
  6332. (setq beg (point) end (progn (org-end-of-subtree t t)
  6333. (beginning-of-line 2)
  6334. (point)))))
  6335. (when beg
  6336. (goto-char beg)
  6337. (while (re-search-forward re end t)
  6338. (unless (get-char-property (match-end 1) 'invisible)
  6339. (setq e (match-end 1))
  6340. (if (< org-cycle-separator-lines 0)
  6341. (setq b (save-excursion
  6342. (goto-char (match-beginning 0))
  6343. (org-back-over-empty-lines)
  6344. (if (save-excursion
  6345. (goto-char (max (point-min) (1- (point))))
  6346. (org-at-heading-p))
  6347. (1- (point))
  6348. (point))))
  6349. (setq b (match-beginning 1)))
  6350. (outline-flag-region b e nil)))))))
  6351. ;; Never hide empty lines at the end of the file.
  6352. (save-excursion
  6353. (goto-char (point-max))
  6354. (outline-previous-heading)
  6355. (outline-end-of-heading)
  6356. (if (and (looking-at "[ \t\n]+")
  6357. (= (match-end 0) (point-max)))
  6358. (outline-flag-region (point) (match-end 0) nil))))
  6359. (defun org-show-empty-lines-in-parent ()
  6360. "Move to the parent and re-show empty lines before visible headlines."
  6361. (save-excursion
  6362. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6363. (org-cycle-show-empty-lines context))))
  6364. (defun org-files-list ()
  6365. "Return `org-agenda-files' list, plus all open org-mode files.
  6366. This is useful for operations that need to scan all of a user's
  6367. open and agenda-wise Org files."
  6368. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6369. (dolist (buf (buffer-list))
  6370. (with-current-buffer buf
  6371. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6372. (let ((file (expand-file-name (buffer-file-name))))
  6373. (unless (member file files)
  6374. (push file files))))))
  6375. files))
  6376. (defsubst org-entry-beginning-position ()
  6377. "Return the beginning position of the current entry."
  6378. (save-excursion (outline-back-to-heading t) (point)))
  6379. (defsubst org-entry-end-position ()
  6380. "Return the end position of the current entry."
  6381. (save-excursion (outline-next-heading) (point)))
  6382. (defun org-cycle-hide-drawers (state &optional exceptions)
  6383. "Re-hide all drawers after a visibility state change.
  6384. When non-nil, optional argument EXCEPTIONS is a list of strings
  6385. specifying which drawers should not be hidden."
  6386. (when (and (derived-mode-p 'org-mode)
  6387. (not (memq state '(overview folded contents))))
  6388. (save-excursion
  6389. (let* ((globalp (memq state '(contents all)))
  6390. (beg (if globalp (point-min) (point)))
  6391. (end (if globalp (point-max)
  6392. (if (eq state 'children)
  6393. (save-excursion (outline-next-heading) (point))
  6394. (org-end-of-subtree t)))))
  6395. (goto-char beg)
  6396. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6397. (unless (member-ignore-case (match-string 1) exceptions)
  6398. (let ((drawer (org-element-at-point)))
  6399. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6400. (org-flag-drawer t drawer)
  6401. ;; Make sure to skip drawer entirely or we might flag
  6402. ;; it another time when matching its ending line with
  6403. ;; `org-drawer-regexp'.
  6404. (goto-char (org-element-property :end drawer))))))))))
  6405. (defun org-cycle-hide-inline-tasks (state)
  6406. "Re-hide inline tasks when switching to 'contents or 'children
  6407. visibility state."
  6408. (case state
  6409. (contents
  6410. (when (org-bound-and-true-p org-inlinetask-min-level)
  6411. (hide-sublevels (1- org-inlinetask-min-level))))
  6412. (children
  6413. (when (featurep 'org-inlinetask)
  6414. (save-excursion
  6415. (while (and (outline-next-heading)
  6416. (org-inlinetask-at-task-p))
  6417. (org-inlinetask-toggle-visibility)
  6418. (org-inlinetask-goto-end)))))))
  6419. (defun org-flag-drawer (flag &optional element)
  6420. "When FLAG is non-nil, hide the drawer we are at.
  6421. Otherwise make it visible. When optional argument ELEMENT is
  6422. a parsed drawer, as returned by `org-element-at-point', hide or
  6423. show that drawer instead."
  6424. (when (save-excursion
  6425. (beginning-of-line)
  6426. (org-looking-at-p org-drawer-regexp))
  6427. (let ((drawer (or element (org-element-at-point))))
  6428. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6429. (let ((post (org-element-property :post-affiliated drawer)))
  6430. (save-excursion
  6431. (outline-flag-region
  6432. (progn (goto-char post) (line-end-position))
  6433. (progn (goto-char (org-element-property :end drawer))
  6434. (skip-chars-backward " \r\t\n")
  6435. (line-end-position))
  6436. flag))
  6437. ;; When the drawer is hidden away, make sure point lies in
  6438. ;; a visible part of the buffer.
  6439. (when (and flag (> (line-beginning-position) post))
  6440. (goto-char post)))))))
  6441. (defun org-subtree-end-visible-p ()
  6442. "Is the end of the current subtree visible?"
  6443. (pos-visible-in-window-p
  6444. (save-excursion (org-end-of-subtree t) (point))))
  6445. (defun org-first-headline-recenter ()
  6446. "Move cursor to the first headline and recenter the headline."
  6447. (goto-char (point-min))
  6448. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6449. (set-window-start (selected-window) (point-at-bol))))
  6450. ;;; Saving and restoring visibility
  6451. (defun org-outline-overlay-data (&optional use-markers)
  6452. "Return a list of the locations of all outline overlays.
  6453. These are overlays with the `invisible' property value `outline'.
  6454. The return value is a list of cons cells, with start and stop
  6455. positions for each overlay.
  6456. If USE-MARKERS is set, return the positions as markers."
  6457. (let (beg end)
  6458. (save-excursion
  6459. (save-restriction
  6460. (widen)
  6461. (delq nil
  6462. (mapcar (lambda (o)
  6463. (when (eq (overlay-get o 'invisible) 'outline)
  6464. (setq beg (overlay-start o)
  6465. end (overlay-end o))
  6466. (and beg end (> end beg)
  6467. (if use-markers
  6468. (cons (copy-marker beg)
  6469. (copy-marker end t))
  6470. (cons beg end)))))
  6471. (overlays-in (point-min) (point-max))))))))
  6472. (defun org-set-outline-overlay-data (data)
  6473. "Create visibility overlays for all positions in DATA.
  6474. DATA should have been made by `org-outline-overlay-data'."
  6475. (let (o)
  6476. (save-excursion
  6477. (save-restriction
  6478. (widen)
  6479. (show-all)
  6480. (mapc (lambda (c)
  6481. (outline-flag-region (car c) (cdr c) t))
  6482. data)))))
  6483. ;;; Folding of blocks
  6484. (defvar org-hide-block-overlays nil
  6485. "Overlays hiding blocks.")
  6486. (make-variable-buffer-local 'org-hide-block-overlays)
  6487. (defun org-block-map (function &optional start end)
  6488. "Call FUNCTION at the head of all source blocks in the current buffer.
  6489. Optional arguments START and END can be used to limit the range."
  6490. (let ((start (or start (point-min)))
  6491. (end (or end (point-max))))
  6492. (save-excursion
  6493. (goto-char start)
  6494. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6495. (save-excursion
  6496. (save-match-data
  6497. (goto-char (match-beginning 0))
  6498. (funcall function)))))))
  6499. (defun org-hide-block-toggle-all ()
  6500. "Toggle the visibility of all blocks in the current buffer."
  6501. (org-block-map 'org-hide-block-toggle))
  6502. (defun org-hide-block-all ()
  6503. "Fold all blocks in the current buffer."
  6504. (interactive)
  6505. (org-show-block-all)
  6506. (org-block-map 'org-hide-block-toggle-maybe))
  6507. (defun org-show-block-all ()
  6508. "Unfold all blocks in the current buffer."
  6509. (interactive)
  6510. (mapc 'delete-overlay org-hide-block-overlays)
  6511. (setq org-hide-block-overlays nil))
  6512. (defun org-hide-block-toggle-maybe ()
  6513. "Toggle visibility of block at point."
  6514. (interactive)
  6515. (let ((case-fold-search t))
  6516. (if (save-excursion
  6517. (beginning-of-line 1)
  6518. (looking-at org-block-regexp))
  6519. (progn (org-hide-block-toggle)
  6520. t) ;; to signal that we took action
  6521. nil))) ;; to signal that we did not
  6522. (defun org-hide-block-toggle (&optional force)
  6523. "Toggle the visibility of the current block.
  6524. When optional argument FORCE is `off', make block visible. If it
  6525. is non-nil, hide it unconditionally."
  6526. (interactive)
  6527. (let ((element (org-element-at-point)))
  6528. (unless (memq (org-element-type element)
  6529. '(center-block comment-block example-block quote-block
  6530. src-block verse-block))
  6531. (user-error "Not at a block"))
  6532. (let* ((start (save-excursion
  6533. (goto-char (org-element-property :post-affiliated element))
  6534. (line-end-position)))
  6535. (end (save-excursion
  6536. (goto-char (org-element-property :end element))
  6537. (skip-chars-backward " \r\t\n")
  6538. (line-end-position)))
  6539. (overlays (overlays-at start)))
  6540. (cond
  6541. ;; Do nothing when not before or at the block opening line or
  6542. ;; at the block closing line.
  6543. ((let ((eol (line-end-position)))
  6544. (and (> eol start) (/= eol end))))
  6545. ((and (not (eq force 'off))
  6546. (not (memq t (mapcar
  6547. (lambda (o)
  6548. (eq (overlay-get o 'invisible) 'org-hide-block))
  6549. overlays))))
  6550. (let ((ov (make-overlay start end)))
  6551. (overlay-put ov 'invisible 'org-hide-block)
  6552. ;; Make the block accessible to `isearch'.
  6553. (overlay-put
  6554. ov 'isearch-open-invisible
  6555. (lambda (ov)
  6556. (when (memq ov org-hide-block-overlays)
  6557. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6558. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6559. (delete-overlay ov))))
  6560. (push ov org-hide-block-overlays)
  6561. ;; When the block is hidden away, make sure point is left in
  6562. ;; a visible part of the buffer.
  6563. (when (> (line-beginning-position) start)
  6564. (goto-char start)
  6565. (beginning-of-line))))
  6566. ((or (not force) (eq force 'off))
  6567. (dolist (ov overlays)
  6568. (when (memq ov org-hide-block-overlays)
  6569. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6570. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6571. (delete-overlay ov))))))))
  6572. ;; org-tab-after-check-for-cycling-hook
  6573. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6574. ;; Remove overlays when changing major mode
  6575. (add-hook 'org-mode-hook
  6576. (lambda () (org-add-hook 'change-major-mode-hook
  6577. 'org-show-block-all 'append 'local)))
  6578. ;;; Org-goto
  6579. (defvar org-goto-window-configuration nil)
  6580. (defvar org-goto-marker nil)
  6581. (defvar org-goto-map)
  6582. (defun org-goto-map ()
  6583. "Set the keymap `org-goto'."
  6584. (setq org-goto-map
  6585. (let ((map (make-sparse-keymap)))
  6586. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6587. mouse-drag-region universal-argument org-occur))
  6588. cmd)
  6589. (while (setq cmd (pop cmds))
  6590. (substitute-key-definition cmd cmd map global-map)))
  6591. (suppress-keymap map)
  6592. (org-defkey map "\C-m" 'org-goto-ret)
  6593. (org-defkey map [(return)] 'org-goto-ret)
  6594. (org-defkey map [(left)] 'org-goto-left)
  6595. (org-defkey map [(right)] 'org-goto-right)
  6596. (org-defkey map [(control ?g)] 'org-goto-quit)
  6597. (org-defkey map "\C-i" 'org-cycle)
  6598. (org-defkey map [(tab)] 'org-cycle)
  6599. (org-defkey map [(down)] 'outline-next-visible-heading)
  6600. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6601. (if org-goto-auto-isearch
  6602. (if (fboundp 'define-key-after)
  6603. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6604. nil)
  6605. (org-defkey map "q" 'org-goto-quit)
  6606. (org-defkey map "n" 'outline-next-visible-heading)
  6607. (org-defkey map "p" 'outline-previous-visible-heading)
  6608. (org-defkey map "f" 'outline-forward-same-level)
  6609. (org-defkey map "b" 'outline-backward-same-level)
  6610. (org-defkey map "u" 'outline-up-heading))
  6611. (org-defkey map "/" 'org-occur)
  6612. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6613. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6614. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6615. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6616. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6617. map)))
  6618. (defconst org-goto-help
  6619. "Browse buffer copy, to find location or copy text.%s
  6620. RET=jump to location C-g=quit and return to previous location
  6621. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6622. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6623. (defun org-goto (&optional alternative-interface)
  6624. "Look up a different location in the current file, keeping current visibility.
  6625. When you want look-up or go to a different location in a
  6626. document, the fastest way is often to fold the entire buffer and
  6627. then dive into the tree. This method has the disadvantage, that
  6628. the previous location will be folded, which may not be what you
  6629. want.
  6630. This command works around this by showing a copy of the current
  6631. buffer in an indirect buffer, in overview mode. You can dive
  6632. into the tree in that copy, use org-occur and incremental search
  6633. to find a location. When pressing RET or `Q', the command
  6634. returns to the original buffer in which the visibility is still
  6635. unchanged. After RET it will also jump to the location selected
  6636. in the indirect buffer and expose the headline hierarchy above.
  6637. With a prefix argument, use the alternative interface: e.g. if
  6638. `org-goto-interface' is 'outline use 'outline-path-completion."
  6639. (interactive "P")
  6640. (org-goto-map)
  6641. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6642. (org-refile-use-outline-path t)
  6643. (org-refile-target-verify-function nil)
  6644. (interface
  6645. (if (not alternative-interface)
  6646. org-goto-interface
  6647. (if (eq org-goto-interface 'outline)
  6648. 'outline-path-completion
  6649. 'outline)))
  6650. (org-goto-start-pos (point))
  6651. (selected-point
  6652. (if (eq interface 'outline)
  6653. (car (org-get-location (current-buffer) org-goto-help))
  6654. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6655. (org-refile-check-position pa)
  6656. (nth 3 pa)))))
  6657. (if selected-point
  6658. (progn
  6659. (org-mark-ring-push org-goto-start-pos)
  6660. (goto-char selected-point)
  6661. (if (or (outline-invisible-p) (org-invisible-p2))
  6662. (org-show-context 'org-goto)))
  6663. (message "Quit"))))
  6664. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6665. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6666. (defvar org-goto-local-auto-isearch-map) ; defined below
  6667. (defun org-get-location (buf help)
  6668. "Let the user select a location in the Org-mode buffer BUF.
  6669. This function uses a recursive edit. It returns the selected position
  6670. or nil."
  6671. (org-no-popups
  6672. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6673. (isearch-hide-immediately nil)
  6674. (isearch-search-fun-function
  6675. (lambda () 'org-goto-local-search-headings))
  6676. (org-goto-selected-point org-goto-exit-command))
  6677. (save-excursion
  6678. (save-window-excursion
  6679. (delete-other-windows)
  6680. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6681. (org-pop-to-buffer-same-window
  6682. (condition-case nil
  6683. (make-indirect-buffer (current-buffer) "*org-goto*")
  6684. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6685. (with-output-to-temp-buffer "*Org Help*"
  6686. (princ (format help (if org-goto-auto-isearch
  6687. " Just type for auto-isearch."
  6688. " n/p/f/b/u to navigate, q to quit."))))
  6689. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6690. (setq buffer-read-only nil)
  6691. (let ((org-startup-truncated t)
  6692. (org-startup-folded nil)
  6693. (org-startup-align-all-tables nil))
  6694. (org-mode)
  6695. (org-overview))
  6696. (setq buffer-read-only t)
  6697. (if (and (boundp 'org-goto-start-pos)
  6698. (integer-or-marker-p org-goto-start-pos))
  6699. (let ((org-show-hierarchy-above t)
  6700. (org-show-siblings t)
  6701. (org-show-following-heading t))
  6702. (goto-char org-goto-start-pos)
  6703. (and (outline-invisible-p) (org-show-context)))
  6704. (goto-char (point-min)))
  6705. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6706. (message "Select location and press RET")
  6707. (use-local-map org-goto-map)
  6708. (recursive-edit)))
  6709. (kill-buffer "*org-goto*")
  6710. (cons org-goto-selected-point org-goto-exit-command))))
  6711. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6712. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6713. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6714. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6715. (defun org-goto-local-search-headings (string bound noerror)
  6716. "Search and make sure that any matches are in headlines."
  6717. (catch 'return
  6718. (while (if isearch-forward
  6719. (search-forward string bound noerror)
  6720. (search-backward string bound noerror))
  6721. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6722. (and (member :headline context)
  6723. (not (member :tags context))))
  6724. (throw 'return (point))))))
  6725. (defun org-goto-local-auto-isearch ()
  6726. "Start isearch."
  6727. (interactive)
  6728. (goto-char (point-min))
  6729. (let ((keys (this-command-keys)))
  6730. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6731. (isearch-mode t)
  6732. (isearch-process-search-char (string-to-char keys)))))
  6733. (defun org-goto-ret (&optional arg)
  6734. "Finish `org-goto' by going to the new location."
  6735. (interactive "P")
  6736. (setq org-goto-selected-point (point)
  6737. org-goto-exit-command 'return)
  6738. (throw 'exit nil))
  6739. (defun org-goto-left ()
  6740. "Finish `org-goto' by going to the new location."
  6741. (interactive)
  6742. (if (org-at-heading-p)
  6743. (progn
  6744. (beginning-of-line 1)
  6745. (setq org-goto-selected-point (point)
  6746. org-goto-exit-command 'left)
  6747. (throw 'exit nil))
  6748. (user-error "Not on a heading")))
  6749. (defun org-goto-right ()
  6750. "Finish `org-goto' by going to the new location."
  6751. (interactive)
  6752. (if (org-at-heading-p)
  6753. (progn
  6754. (setq org-goto-selected-point (point)
  6755. org-goto-exit-command 'right)
  6756. (throw 'exit nil))
  6757. (user-error "Not on a heading")))
  6758. (defun org-goto-quit ()
  6759. "Finish `org-goto' without cursor motion."
  6760. (interactive)
  6761. (setq org-goto-selected-point nil)
  6762. (setq org-goto-exit-command 'quit)
  6763. (throw 'exit nil))
  6764. ;;; Indirect buffer display of subtrees
  6765. (defvar org-indirect-dedicated-frame nil
  6766. "This is the frame being used for indirect tree display.")
  6767. (defvar org-last-indirect-buffer nil)
  6768. (defun org-tree-to-indirect-buffer (&optional arg)
  6769. "Create indirect buffer and narrow it to current subtree.
  6770. With a numerical prefix ARG, go up to this level and then take that tree.
  6771. If ARG is negative, go up that many levels.
  6772. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6773. indirect buffer previously made with this command, to avoid proliferation of
  6774. indirect buffers. However, when you call the command with a \
  6775. \\[universal-argument] prefix, or
  6776. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6777. is kept so that you can work with several indirect buffers at the same time.
  6778. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6779. \\[universal-argument] prefix also
  6780. requests that a new frame be made for the new buffer, so that the dedicated
  6781. frame is not changed."
  6782. (interactive "P")
  6783. (let ((cbuf (current-buffer))
  6784. (cwin (selected-window))
  6785. (pos (point))
  6786. beg end level heading ibuf)
  6787. (save-excursion
  6788. (org-back-to-heading t)
  6789. (when (numberp arg)
  6790. (setq level (org-outline-level))
  6791. (if (< arg 0) (setq arg (+ level arg)))
  6792. (while (> (setq level (org-outline-level)) arg)
  6793. (org-up-heading-safe)))
  6794. (setq beg (point)
  6795. heading (org-get-heading))
  6796. (org-end-of-subtree t t)
  6797. (if (org-at-heading-p) (backward-char 1))
  6798. (setq end (point)))
  6799. (if (and (buffer-live-p org-last-indirect-buffer)
  6800. (not (eq org-indirect-buffer-display 'new-frame))
  6801. (not arg))
  6802. (kill-buffer org-last-indirect-buffer))
  6803. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6804. org-last-indirect-buffer ibuf)
  6805. (cond
  6806. ((or (eq org-indirect-buffer-display 'new-frame)
  6807. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6808. (select-frame (make-frame))
  6809. (delete-other-windows)
  6810. (org-pop-to-buffer-same-window ibuf)
  6811. (org-set-frame-title heading))
  6812. ((eq org-indirect-buffer-display 'dedicated-frame)
  6813. (raise-frame
  6814. (select-frame (or (and org-indirect-dedicated-frame
  6815. (frame-live-p org-indirect-dedicated-frame)
  6816. org-indirect-dedicated-frame)
  6817. (setq org-indirect-dedicated-frame (make-frame)))))
  6818. (delete-other-windows)
  6819. (org-pop-to-buffer-same-window ibuf)
  6820. (org-set-frame-title (concat "Indirect: " heading)))
  6821. ((eq org-indirect-buffer-display 'current-window)
  6822. (org-pop-to-buffer-same-window ibuf))
  6823. ((eq org-indirect-buffer-display 'other-window)
  6824. (pop-to-buffer ibuf))
  6825. (t (error "Invalid value")))
  6826. (if (featurep 'xemacs)
  6827. (save-excursion (org-mode) (turn-on-font-lock)))
  6828. (narrow-to-region beg end)
  6829. (show-all)
  6830. (goto-char pos)
  6831. (run-hook-with-args 'org-cycle-hook 'all)
  6832. (and (window-live-p cwin) (select-window cwin))))
  6833. (defun org-get-indirect-buffer (&optional buffer heading)
  6834. (setq buffer (or buffer (current-buffer)))
  6835. (let ((n 1) (base (buffer-name buffer)) bname)
  6836. (while (buffer-live-p
  6837. (get-buffer
  6838. (setq bname
  6839. (concat base "-"
  6840. (if heading (concat heading "-" (number-to-string n))
  6841. (number-to-string n))))))
  6842. (setq n (1+ n)))
  6843. (condition-case nil
  6844. (make-indirect-buffer buffer bname 'clone)
  6845. (error (make-indirect-buffer buffer bname)))))
  6846. (defun org-set-frame-title (title)
  6847. "Set the title of the current frame to the string TITLE."
  6848. ;; FIXME: how to name a single frame in XEmacs???
  6849. (unless (featurep 'xemacs)
  6850. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6851. ;;;; Structure editing
  6852. ;;; Inserting headlines
  6853. (defun org-previous-line-empty-p (&optional next)
  6854. "Is the previous line a blank line?
  6855. When NEXT is non-nil, check the next line instead."
  6856. (save-excursion
  6857. (and (not (bobp))
  6858. (or (beginning-of-line (if next 2 0)) t)
  6859. (save-match-data
  6860. (looking-at "[ \t]*$")))))
  6861. (defun org-insert-heading (&optional arg invisible-ok)
  6862. "Insert a new heading or an item with the same depth at point.
  6863. If point is at the beginning of a heading or a list item, insert
  6864. a new heading or a new item above the current one. If point is
  6865. at the beginning of a normal line, turn the line into a heading.
  6866. If point is in the middle of a headline or a list item, split the
  6867. headline or the item and create a new headline/item with the text
  6868. in the current line after point \(see `org-M-RET-may-split-line'
  6869. on how to modify this behavior).
  6870. With one universal prefix argument, set the user option
  6871. `org-insert-heading-respect-content' to t for the duration of
  6872. the command. This modifies the behavior described above in this
  6873. ways: on list items and at the beginning of normal lines, force
  6874. the insertion of a heading after the current subtree.
  6875. With two universal prefix arguments, insert the heading at the
  6876. end of the grandparent subtree. For example, if point is within
  6877. a 2nd-level heading, then it will insert a 2nd-level heading at
  6878. the end of the 1st-level parent heading.
  6879. If point is at the beginning of a headline, insert a sibling
  6880. before the current headline. If point is not at the beginning,
  6881. split the line and create a new headline with the text in the
  6882. current line after point \(see `org-M-RET-may-split-line' on how
  6883. to modify this behavior).
  6884. If point is at the beginning of a normal line, turn this line
  6885. into a heading.
  6886. When INVISIBLE-OK is set, stop at invisible headlines when going
  6887. back. This is important for non-interactive uses of the
  6888. command."
  6889. (interactive "P")
  6890. (if (org-called-interactively-p 'any) (org-reveal))
  6891. (let ((itemp (org-in-item-p))
  6892. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6893. (respect-content (or org-insert-heading-respect-content
  6894. (equal arg '(4))))
  6895. (initial-content "")
  6896. (adjust-empty-lines t))
  6897. (cond
  6898. ((or (= (buffer-size) 0)
  6899. (and (not (save-excursion
  6900. (and (ignore-errors (org-back-to-heading invisible-ok))
  6901. (org-at-heading-p))))
  6902. (or arg (not itemp))))
  6903. ;; At beginning of buffer or so high up that only a heading
  6904. ;; makes sense.
  6905. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6906. ((not respect-content)
  6907. (unless may-split (end-of-line))
  6908. (insert "\n* "))
  6909. ((re-search-forward org-outline-regexp-bol nil t)
  6910. (beginning-of-line)
  6911. (insert "* \n")
  6912. (backward-char))
  6913. (t (goto-char (point-max))
  6914. (insert "\n* ")))
  6915. (run-hooks 'org-insert-heading-hook))
  6916. ((and itemp (not (member arg '((4) (16)))))
  6917. ;; Insert an item
  6918. (org-insert-item))
  6919. (t
  6920. ;; Maybe move at the end of the subtree
  6921. (when (equal arg '(16))
  6922. (org-up-heading-safe)
  6923. (org-end-of-subtree t))
  6924. ;; Insert a heading
  6925. (save-restriction
  6926. (widen)
  6927. (let* ((level nil)
  6928. (on-heading (org-at-heading-p))
  6929. (empty-line-p (if on-heading
  6930. (org-previous-line-empty-p)
  6931. ;; We will decide later
  6932. nil))
  6933. ;; Get a level string to fall back on
  6934. (fix-level
  6935. (if (org-before-first-heading-p) "*"
  6936. (save-excursion
  6937. (org-back-to-heading t)
  6938. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6939. (looking-at org-outline-regexp)
  6940. (make-string (1- (length (match-string 0))) ?*))))
  6941. (stars
  6942. (save-excursion
  6943. (condition-case nil
  6944. (progn
  6945. (org-back-to-heading invisible-ok)
  6946. (when (and (not on-heading)
  6947. (featurep 'org-inlinetask)
  6948. (integerp org-inlinetask-min-level)
  6949. (>= (length (match-string 0))
  6950. org-inlinetask-min-level))
  6951. ;; Find a heading level before the inline task
  6952. (while (and (setq level (org-up-heading-safe))
  6953. (>= level org-inlinetask-min-level)))
  6954. (if (org-at-heading-p)
  6955. (org-back-to-heading invisible-ok)
  6956. (error "This should not happen")))
  6957. (unless (and (save-excursion
  6958. (save-match-data
  6959. (org-backward-heading-same-level
  6960. 1 invisible-ok))
  6961. (= (point) (match-beginning 0)))
  6962. (not (org-previous-line-empty-p t)))
  6963. (setq empty-line-p (or empty-line-p
  6964. (org-previous-line-empty-p))))
  6965. (match-string 0))
  6966. (error (or fix-level "* ")))))
  6967. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6968. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6969. pos hide-previous previous-pos)
  6970. ;; If we insert after content, move there and clean up whitespace
  6971. (when (and respect-content
  6972. (not (org-looking-at-p org-outline-regexp-bol)))
  6973. (if (not (org-before-first-heading-p))
  6974. (org-end-of-subtree nil t)
  6975. (re-search-forward org-outline-regexp-bol)
  6976. (beginning-of-line 0))
  6977. (skip-chars-backward " \r\n")
  6978. (and (not (looking-back "^\\*+"))
  6979. (looking-at "[ \t]+") (replace-match ""))
  6980. (unless (eobp) (forward-char 1))
  6981. (when (looking-at "^\\*")
  6982. (unless (bobp) (backward-char 1))
  6983. (insert "\n")))
  6984. ;; If we are splitting, grab the text that should be moved to the new headline
  6985. (when may-split
  6986. (if (org-on-heading-p)
  6987. ;; This is a heading, we split intelligently (keeping tags)
  6988. (let ((pos (point)))
  6989. (goto-char (point-at-bol))
  6990. (unless (looking-at org-complex-heading-regexp)
  6991. (error "This should not happen"))
  6992. (when (and (match-beginning 4)
  6993. (> pos (match-beginning 4))
  6994. (< pos (match-end 4)))
  6995. (setq initial-content (buffer-substring pos (match-end 4)))
  6996. (goto-char pos)
  6997. (delete-region (point) (match-end 4))
  6998. (if (looking-at "[ \t]*$")
  6999. (replace-match "")
  7000. (insert (make-string (length initial-content) ?\ )))
  7001. (setq initial-content (org-trim initial-content)))
  7002. (goto-char pos))
  7003. ;; a normal line
  7004. (setq initial-content
  7005. (org-trim (buffer-substring (point) (point-at-eol))))
  7006. (delete-region (point) (point-at-eol))))
  7007. ;; If we are at the beginning of the line, insert before it. Else after
  7008. (cond
  7009. ((and (bolp) (looking-at "[ \t]*$")))
  7010. ((and (bolp) (not (looking-at "[ \t]*$")))
  7011. (open-line 1))
  7012. (t
  7013. (goto-char (point-at-eol))
  7014. (insert "\n")))
  7015. ;; Insert the new heading
  7016. (insert stars)
  7017. (just-one-space)
  7018. (insert initial-content)
  7019. (when adjust-empty-lines
  7020. (if (or (not blank)
  7021. (and blank (not (org-previous-line-empty-p))))
  7022. (org-N-empty-lines-before-current (if blank 1 0))))
  7023. (run-hooks 'org-insert-heading-hook)))))))
  7024. (defun org-N-empty-lines-before-current (N)
  7025. "Make the number of empty lines before current exactly N.
  7026. So this will delete or add empty lines."
  7027. (save-excursion
  7028. (goto-char (point-at-bol))
  7029. (if (looking-back "\\s-+" nil 'greedy)
  7030. (replace-match ""))
  7031. (or (bobp) (insert "\n"))
  7032. (while (> N 0)
  7033. (insert "\n")
  7034. (setq N (1- N)))))
  7035. (defun org-get-heading (&optional no-tags no-todo)
  7036. "Return the heading of the current entry, without the stars.
  7037. When NO-TAGS is non-nil, don't include tags.
  7038. When NO-TODO is non-nil, don't include TODO keywords."
  7039. (save-excursion
  7040. (org-back-to-heading t)
  7041. (cond
  7042. ((and no-tags no-todo)
  7043. (looking-at org-complex-heading-regexp)
  7044. (match-string 4))
  7045. (no-tags
  7046. (looking-at (concat org-outline-regexp
  7047. "\\(.*?\\)"
  7048. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7049. (match-string 1))
  7050. (no-todo
  7051. (looking-at org-todo-line-regexp)
  7052. (match-string 3))
  7053. (t (looking-at org-heading-regexp)
  7054. (match-string 2)))))
  7055. (defvar orgstruct-mode) ; defined below
  7056. (defun org-heading-components ()
  7057. "Return the components of the current heading.
  7058. This is a list with the following elements:
  7059. - the level as an integer
  7060. - the reduced level, different if `org-odd-levels-only' is set.
  7061. - the TODO keyword, or nil
  7062. - the priority character, like ?A, or nil if no priority is given
  7063. - the headline text itself, or the tags string if no headline text
  7064. - the tags string, or nil."
  7065. (save-excursion
  7066. (org-back-to-heading t)
  7067. (if (let (case-fold-search)
  7068. (looking-at
  7069. (if orgstruct-mode
  7070. org-heading-regexp
  7071. org-complex-heading-regexp)))
  7072. (if orgstruct-mode
  7073. (list (length (match-string 1))
  7074. (org-reduced-level (length (match-string 1)))
  7075. nil
  7076. nil
  7077. (match-string 2)
  7078. nil)
  7079. (list (length (match-string 1))
  7080. (org-reduced-level (length (match-string 1)))
  7081. (org-match-string-no-properties 2)
  7082. (and (match-end 3) (aref (match-string 3) 2))
  7083. (org-match-string-no-properties 4)
  7084. (org-match-string-no-properties 5))))))
  7085. (defun org-get-entry ()
  7086. "Get the entry text, after heading, entire subtree."
  7087. (save-excursion
  7088. (org-back-to-heading t)
  7089. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7090. (defun org-insert-heading-after-current ()
  7091. "Insert a new heading with same level as current, after current subtree."
  7092. (interactive)
  7093. (org-back-to-heading)
  7094. (org-insert-heading)
  7095. (org-move-subtree-down)
  7096. (end-of-line 1))
  7097. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7098. "Insert heading with `org-insert-heading-respect-content' set to t."
  7099. (interactive)
  7100. (org-insert-heading '(4) invisible-ok))
  7101. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7102. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7103. (interactive)
  7104. (org-insert-todo-heading force-state '(4)))
  7105. (defun org-insert-todo-heading (arg &optional force-heading)
  7106. "Insert a new heading with the same level and TODO state as current heading.
  7107. If the heading has no TODO state, or if the state is DONE, use the first
  7108. state (TODO by default). Also with one prefix arg, force first state. With
  7109. two prefix args, force inserting at the end of the parent subtree."
  7110. (interactive "P")
  7111. (when (or force-heading (not (org-insert-item 'checkbox)))
  7112. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7113. force-heading))
  7114. (save-excursion
  7115. (org-back-to-heading)
  7116. (outline-previous-heading)
  7117. (looking-at org-todo-line-regexp))
  7118. (let*
  7119. ((new-mark-x
  7120. (if (or (equal arg '(4))
  7121. (not (match-beginning 2))
  7122. (member (match-string 2) org-done-keywords))
  7123. (car org-todo-keywords-1)
  7124. (match-string 2)))
  7125. (new-mark
  7126. (or
  7127. (run-hook-with-args-until-success
  7128. 'org-todo-get-default-hook new-mark-x nil)
  7129. new-mark-x)))
  7130. (beginning-of-line 1)
  7131. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7132. (if org-treat-insert-todo-heading-as-state-change
  7133. (org-todo new-mark)
  7134. (insert new-mark " "))))
  7135. (when org-provide-todo-statistics
  7136. (org-update-parent-todo-statistics))))
  7137. (defun org-insert-subheading (arg)
  7138. "Insert a new subheading and demote it.
  7139. Works for outline headings and for plain lists alike."
  7140. (interactive "P")
  7141. (org-insert-heading arg)
  7142. (cond
  7143. ((org-at-heading-p) (org-do-demote))
  7144. ((org-at-item-p) (org-indent-item))))
  7145. (defun org-insert-todo-subheading (arg)
  7146. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7147. Works for outline headings and for plain lists alike."
  7148. (interactive "P")
  7149. (org-insert-todo-heading arg)
  7150. (cond
  7151. ((org-at-heading-p) (org-do-demote))
  7152. ((org-at-item-p) (org-indent-item))))
  7153. ;;; Promotion and Demotion
  7154. (defvar org-after-demote-entry-hook nil
  7155. "Hook run after an entry has been demoted.
  7156. The cursor will be at the beginning of the entry.
  7157. When a subtree is being demoted, the hook will be called for each node.")
  7158. (defvar org-after-promote-entry-hook nil
  7159. "Hook run after an entry has been promoted.
  7160. The cursor will be at the beginning of the entry.
  7161. When a subtree is being promoted, the hook will be called for each node.")
  7162. (defun org-promote-subtree ()
  7163. "Promote the entire subtree.
  7164. See also `org-promote'."
  7165. (interactive)
  7166. (save-excursion
  7167. (org-with-limited-levels (org-map-tree 'org-promote)))
  7168. (org-fix-position-after-promote))
  7169. (defun org-demote-subtree ()
  7170. "Demote the entire subtree. See `org-demote'.
  7171. See also `org-promote'."
  7172. (interactive)
  7173. (save-excursion
  7174. (org-with-limited-levels (org-map-tree 'org-demote)))
  7175. (org-fix-position-after-promote))
  7176. (defun org-do-promote ()
  7177. "Promote the current heading higher up the tree.
  7178. If the region is active in `transient-mark-mode', promote all headings
  7179. in the region."
  7180. (interactive)
  7181. (save-excursion
  7182. (if (org-region-active-p)
  7183. (org-map-region 'org-promote (region-beginning) (region-end))
  7184. (org-promote)))
  7185. (org-fix-position-after-promote))
  7186. (defun org-do-demote ()
  7187. "Demote the current heading lower down the tree.
  7188. If the region is active in `transient-mark-mode', demote all headings
  7189. in the region."
  7190. (interactive)
  7191. (save-excursion
  7192. (if (org-region-active-p)
  7193. (org-map-region 'org-demote (region-beginning) (region-end))
  7194. (org-demote)))
  7195. (org-fix-position-after-promote))
  7196. (defun org-fix-position-after-promote ()
  7197. "Make sure that after pro/demotion cursor position is right."
  7198. (let ((pos (point)))
  7199. (when (save-excursion
  7200. (beginning-of-line 1)
  7201. (looking-at org-todo-line-regexp)
  7202. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7203. (cond ((eobp) (insert " "))
  7204. ((eolp) (insert " "))
  7205. ((equal (char-after) ?\ ) (forward-char 1))))))
  7206. (defun org-current-level ()
  7207. "Return the level of the current entry, or nil if before the first headline.
  7208. The level is the number of stars at the beginning of the headline."
  7209. (save-excursion
  7210. (org-with-limited-levels
  7211. (if (ignore-errors (org-back-to-heading t))
  7212. (funcall outline-level)))))
  7213. (defun org-get-previous-line-level ()
  7214. "Return the outline depth of the last headline before the current line.
  7215. Returns 0 for the first headline in the buffer, and nil if before the
  7216. first headline."
  7217. (and (org-current-level)
  7218. (or (and (/= (line-beginning-position) (point-min))
  7219. (save-excursion (beginning-of-line 0) (org-current-level)))
  7220. 0)))
  7221. (defun org-reduced-level (l)
  7222. "Compute the effective level of a heading.
  7223. This takes into account the setting of `org-odd-levels-only'."
  7224. (cond
  7225. ((zerop l) 0)
  7226. (org-odd-levels-only (1+ (floor (/ l 2))))
  7227. (t l)))
  7228. (defun org-level-increment ()
  7229. "Return the number of stars that will be added or removed at a
  7230. time to headlines when structure editing, based on the value of
  7231. `org-odd-levels-only'."
  7232. (if org-odd-levels-only 2 1))
  7233. (defun org-get-valid-level (level &optional change)
  7234. "Rectify a level change under the influence of `org-odd-levels-only'
  7235. LEVEL is a current level, CHANGE is by how much the level should be
  7236. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7237. even level numbers will become the next higher odd number."
  7238. (if org-odd-levels-only
  7239. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7240. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7241. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7242. (max 1 (+ level (or change 0)))))
  7243. (if (boundp 'define-obsolete-function-alias)
  7244. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7245. (define-obsolete-function-alias 'org-get-legal-level
  7246. 'org-get-valid-level)
  7247. (define-obsolete-function-alias 'org-get-legal-level
  7248. 'org-get-valid-level "23.1")))
  7249. (defun org-promote ()
  7250. "Promote the current heading higher up the tree.
  7251. If the region is active in `transient-mark-mode', promote all headings
  7252. in the region."
  7253. (org-back-to-heading t)
  7254. (let* ((level (save-match-data (funcall outline-level)))
  7255. (after-change-functions (remove 'flyspell-after-change-function
  7256. after-change-functions))
  7257. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7258. (diff (abs (- level (length up-head) -1))))
  7259. (cond ((and (= level 1) org-called-with-limited-levels
  7260. org-allow-promoting-top-level-subtree)
  7261. (replace-match "# " nil t))
  7262. ((= level 1)
  7263. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7264. (t (replace-match up-head nil t)))
  7265. ;; Fixup tag positioning
  7266. (unless (= level 1)
  7267. (and org-auto-align-tags (org-set-tags nil 'ignore-column))
  7268. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7269. (run-hooks 'org-after-promote-entry-hook)))
  7270. (defun org-demote ()
  7271. "Demote the current heading lower down the tree.
  7272. If the region is active in `transient-mark-mode', demote all headings
  7273. in the region."
  7274. (org-back-to-heading t)
  7275. (let* ((level (save-match-data (funcall outline-level)))
  7276. (after-change-functions (remove 'flyspell-after-change-function
  7277. after-change-functions))
  7278. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7279. (diff (abs (- level (length down-head) -1))))
  7280. (replace-match down-head nil t)
  7281. ;; Fixup tag positioning
  7282. (and org-auto-align-tags (org-set-tags nil 'ignore-column))
  7283. (if org-adapt-indentation (org-fixup-indentation diff))
  7284. (run-hooks 'org-after-demote-entry-hook)))
  7285. (defun org-cycle-level ()
  7286. "Cycle the level of an empty headline through possible states.
  7287. This goes first to child, then to parent, level, then up the hierarchy.
  7288. After top level, it switches back to sibling level."
  7289. (interactive)
  7290. (let ((org-adapt-indentation nil))
  7291. (when (org-point-at-end-of-empty-headline)
  7292. (setq this-command 'org-cycle-level) ; Only needed for caching
  7293. (let ((cur-level (org-current-level))
  7294. (prev-level (org-get-previous-line-level)))
  7295. (cond
  7296. ;; If first headline in file, promote to top-level.
  7297. ((= prev-level 0)
  7298. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7299. do (org-do-promote)))
  7300. ;; If same level as prev, demote one.
  7301. ((= prev-level cur-level)
  7302. (org-do-demote))
  7303. ;; If parent is top-level, promote to top level if not already.
  7304. ((= prev-level 1)
  7305. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7306. do (org-do-promote)))
  7307. ;; If top-level, return to prev-level.
  7308. ((= cur-level 1)
  7309. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7310. do (org-do-demote)))
  7311. ;; If less than prev-level, promote one.
  7312. ((< cur-level prev-level)
  7313. (org-do-promote))
  7314. ;; If deeper than prev-level, promote until higher than
  7315. ;; prev-level.
  7316. ((> cur-level prev-level)
  7317. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7318. do (org-do-promote))))
  7319. t))))
  7320. (defun org-map-tree (fun)
  7321. "Call FUN for every heading underneath the current one."
  7322. (org-back-to-heading)
  7323. (let ((level (funcall outline-level)))
  7324. (save-excursion
  7325. (funcall fun)
  7326. (while (and (progn
  7327. (outline-next-heading)
  7328. (> (funcall outline-level) level))
  7329. (not (eobp)))
  7330. (funcall fun)))))
  7331. (defun org-map-region (fun beg end)
  7332. "Call FUN for every heading between BEG and END."
  7333. (let ((org-ignore-region t))
  7334. (save-excursion
  7335. (setq end (copy-marker end))
  7336. (goto-char beg)
  7337. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7338. (< (point) end))
  7339. (funcall fun))
  7340. (while (and (progn
  7341. (outline-next-heading)
  7342. (< (point) end))
  7343. (not (eobp)))
  7344. (funcall fun)))))
  7345. (defvar org-property-end-re) ; silence byte-compiler
  7346. (defun org-fixup-indentation (diff)
  7347. "Change the indentation in the current entry by DIFF.
  7348. However, if any line in the current entry has no indentation, or if it
  7349. would end up with no indentation after the change, nothing at all is done."
  7350. (save-excursion
  7351. (let ((end (save-excursion (outline-next-heading)
  7352. (point-marker)))
  7353. (prohibit (if (> diff 0)
  7354. "^\\S-"
  7355. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7356. col)
  7357. (unless (save-excursion (end-of-line 1)
  7358. (re-search-forward prohibit end t))
  7359. (while (and (< (point) end)
  7360. (re-search-forward "^[ \t]+" end t))
  7361. (goto-char (match-end 0))
  7362. (setq col (current-column))
  7363. (if (< diff 0) (replace-match ""))
  7364. (org-indent-to-column (+ diff col))))
  7365. (move-marker end nil))))
  7366. (defun org-convert-to-odd-levels ()
  7367. "Convert an org-mode file with all levels allowed to one with odd levels.
  7368. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7369. level 5 etc."
  7370. (interactive)
  7371. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7372. (let ((outline-level 'org-outline-level)
  7373. (org-odd-levels-only nil) n)
  7374. (save-excursion
  7375. (goto-char (point-min))
  7376. (while (re-search-forward "^\\*\\*+ " nil t)
  7377. (setq n (- (length (match-string 0)) 2))
  7378. (while (>= (setq n (1- n)) 0)
  7379. (org-demote))
  7380. (end-of-line 1))))))
  7381. (defun org-convert-to-oddeven-levels ()
  7382. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7383. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7384. file contains a section with an even level, conversion would
  7385. destroy the structure of the file. An error is signaled in this
  7386. case."
  7387. (interactive)
  7388. (goto-char (point-min))
  7389. ;; First check if there are no even levels
  7390. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7391. (org-show-context t)
  7392. (error "Not all levels are odd in this file. Conversion not possible"))
  7393. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7394. (let ((outline-regexp org-outline-regexp)
  7395. (outline-level 'org-outline-level)
  7396. (org-odd-levels-only nil) n)
  7397. (save-excursion
  7398. (goto-char (point-min))
  7399. (while (re-search-forward "^\\*\\*+ " nil t)
  7400. (setq n (/ (1- (length (match-string 0))) 2))
  7401. (while (>= (setq n (1- n)) 0)
  7402. (org-promote))
  7403. (end-of-line 1))))))
  7404. (defun org-tr-level (n)
  7405. "Make N odd if required."
  7406. (if org-odd-levels-only (1+ (/ n 2)) n))
  7407. ;;; Vertical tree motion, cutting and pasting of subtrees
  7408. (defun org-move-subtree-up (&optional arg)
  7409. "Move the current subtree up past ARG headlines of the same level."
  7410. (interactive "p")
  7411. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7412. (defun org-move-subtree-down (&optional arg)
  7413. "Move the current subtree down past ARG headlines of the same level."
  7414. (interactive "p")
  7415. (setq arg (prefix-numeric-value arg))
  7416. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7417. 'org-get-last-sibling))
  7418. (ins-point (make-marker))
  7419. (cnt (abs arg))
  7420. (col (current-column))
  7421. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7422. ;; Select the tree
  7423. (org-back-to-heading)
  7424. (setq beg0 (point))
  7425. (save-excursion
  7426. (setq ne-beg (org-back-over-empty-lines))
  7427. (setq beg (point)))
  7428. (save-match-data
  7429. (save-excursion (outline-end-of-heading)
  7430. (setq folded (outline-invisible-p)))
  7431. (progn (org-end-of-subtree nil t)
  7432. (unless (eobp) (backward-char))))
  7433. (outline-next-heading)
  7434. (setq ne-end (org-back-over-empty-lines))
  7435. (setq end (point))
  7436. (goto-char beg0)
  7437. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7438. ;; include less whitespace
  7439. (save-excursion
  7440. (goto-char beg)
  7441. (forward-line (- ne-beg ne-end))
  7442. (setq beg (point))))
  7443. ;; Find insertion point, with error handling
  7444. (while (> cnt 0)
  7445. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7446. (progn (goto-char beg0)
  7447. (user-error "Cannot move past superior level or buffer limit")))
  7448. (setq cnt (1- cnt)))
  7449. (if (> arg 0)
  7450. ;; Moving forward - still need to move over subtree
  7451. (progn (org-end-of-subtree t t)
  7452. (save-excursion
  7453. (org-back-over-empty-lines)
  7454. (or (bolp) (newline)))))
  7455. (setq ne-ins (org-back-over-empty-lines))
  7456. (move-marker ins-point (point))
  7457. (setq txt (buffer-substring beg end))
  7458. (org-save-markers-in-region beg end)
  7459. (delete-region beg end)
  7460. (org-remove-empty-overlays-at beg)
  7461. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7462. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7463. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7464. (let ((bbb (point)))
  7465. (insert-before-markers txt)
  7466. (org-reinstall-markers-in-region bbb)
  7467. (move-marker ins-point bbb))
  7468. (or (bolp) (insert "\n"))
  7469. (setq ins-end (point))
  7470. (goto-char ins-point)
  7471. (org-skip-whitespace)
  7472. (when (and (< arg 0)
  7473. (org-first-sibling-p)
  7474. (> ne-ins ne-beg))
  7475. ;; Move whitespace back to beginning
  7476. (save-excursion
  7477. (goto-char ins-end)
  7478. (let ((kill-whole-line t))
  7479. (kill-line (- ne-ins ne-beg)) (point)))
  7480. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7481. (move-marker ins-point nil)
  7482. (if folded
  7483. (hide-subtree)
  7484. (org-show-entry)
  7485. (show-children)
  7486. (org-cycle-hide-drawers 'children))
  7487. (org-clean-visibility-after-subtree-move)
  7488. ;; move back to the initial column we were at
  7489. (move-to-column col)))
  7490. (defvar org-subtree-clip ""
  7491. "Clipboard for cut and paste of subtrees.
  7492. This is actually only a copy of the kill, because we use the normal kill
  7493. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7494. (defvar org-subtree-clip-folded nil
  7495. "Was the last copied subtree folded?
  7496. This is used to fold the tree back after pasting.")
  7497. (defun org-cut-subtree (&optional n)
  7498. "Cut the current subtree into the clipboard.
  7499. With prefix arg N, cut this many sequential subtrees.
  7500. This is a short-hand for marking the subtree and then cutting it."
  7501. (interactive "p")
  7502. (org-copy-subtree n 'cut))
  7503. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7504. "Copy the current subtree it in the clipboard.
  7505. With prefix arg N, copy this many sequential subtrees.
  7506. This is a short-hand for marking the subtree and then copying it.
  7507. If CUT is non-nil, actually cut the subtree.
  7508. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7509. of some markers in the region, even if CUT is non-nil. This is
  7510. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7511. (interactive "p")
  7512. (let (beg end folded (beg0 (point)))
  7513. (if (org-called-interactively-p 'any)
  7514. (org-back-to-heading nil) ; take what looks like a subtree
  7515. (org-back-to-heading t)) ; take what is really there
  7516. (setq beg (point))
  7517. (skip-chars-forward " \t\r\n")
  7518. (save-match-data
  7519. (if nosubtrees
  7520. (outline-next-heading)
  7521. (save-excursion (outline-end-of-heading)
  7522. (setq folded (outline-invisible-p)))
  7523. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7524. (org-end-of-subtree t t)))
  7525. (setq end (point))
  7526. (goto-char beg0)
  7527. (when (> end beg)
  7528. (setq org-subtree-clip-folded folded)
  7529. (when (or cut force-store-markers)
  7530. (org-save-markers-in-region beg end))
  7531. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7532. (setq org-subtree-clip (current-kill 0))
  7533. (message "%s: Subtree(s) with %d characters"
  7534. (if cut "Cut" "Copied")
  7535. (length org-subtree-clip)))))
  7536. (defun org-paste-subtree (&optional level tree for-yank remove)
  7537. "Paste the clipboard as a subtree, with modification of headline level.
  7538. The entire subtree is promoted or demoted in order to match a new headline
  7539. level.
  7540. If the cursor is at the beginning of a headline, the same level as
  7541. that headline is used to paste the tree.
  7542. If not, the new level is derived from the *visible* headings
  7543. before and after the insertion point, and taken to be the inferior headline
  7544. level of the two. So if the previous visible heading is level 3 and the
  7545. next is level 4 (or vice versa), level 4 will be used for insertion.
  7546. This makes sure that the subtree remains an independent subtree and does
  7547. not swallow low level entries.
  7548. You can also force a different level, either by using a numeric prefix
  7549. argument, or by inserting the heading marker by hand. For example, if the
  7550. cursor is after \"*****\", then the tree will be shifted to level 5.
  7551. If optional TREE is given, use this text instead of the kill ring.
  7552. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7553. move back over whitespace before inserting, and move point to the end of
  7554. the inserted text when done.
  7555. When REMOVE is non-nil, remove the subtree from the clipboard."
  7556. (interactive "P")
  7557. (setq tree (or tree (and kill-ring (current-kill 0))))
  7558. (unless (org-kill-is-subtree-p tree)
  7559. (user-error "%s"
  7560. (substitute-command-keys
  7561. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7562. (org-with-limited-levels
  7563. (let* ((visp (not (outline-invisible-p)))
  7564. (txt tree)
  7565. (^re_ "\\(\\*+\\)[ \t]*")
  7566. (old-level (if (string-match org-outline-regexp-bol txt)
  7567. (- (match-end 0) (match-beginning 0) 1)
  7568. -1))
  7569. (force-level (cond (level (prefix-numeric-value level))
  7570. ((and (looking-at "[ \t]*$")
  7571. (string-match
  7572. "^\\*+$" (buffer-substring
  7573. (point-at-bol) (point))))
  7574. (- (match-end 0) (match-beginning 0)))
  7575. ((and (bolp)
  7576. (looking-at org-outline-regexp))
  7577. (- (match-end 0) (point) 1))))
  7578. (previous-level (save-excursion
  7579. (condition-case nil
  7580. (progn
  7581. (outline-previous-visible-heading 1)
  7582. (if (looking-at ^re_)
  7583. (- (match-end 0) (match-beginning 0) 1)
  7584. 1))
  7585. (error 1))))
  7586. (next-level (save-excursion
  7587. (condition-case nil
  7588. (progn
  7589. (or (looking-at org-outline-regexp)
  7590. (outline-next-visible-heading 1))
  7591. (if (looking-at ^re_)
  7592. (- (match-end 0) (match-beginning 0) 1)
  7593. 1))
  7594. (error 1))))
  7595. (new-level (or force-level (max previous-level next-level)))
  7596. (shift (if (or (= old-level -1)
  7597. (= new-level -1)
  7598. (= old-level new-level))
  7599. 0
  7600. (- new-level old-level)))
  7601. (delta (if (> shift 0) -1 1))
  7602. (func (if (> shift 0) 'org-demote 'org-promote))
  7603. (org-odd-levels-only nil)
  7604. beg end newend)
  7605. ;; Remove the forced level indicator
  7606. (if force-level
  7607. (delete-region (point-at-bol) (point)))
  7608. ;; Paste
  7609. (beginning-of-line (if (bolp) 1 2))
  7610. (setq beg (point))
  7611. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7612. (insert-before-markers txt)
  7613. (unless (string-match "\n\\'" txt) (insert "\n"))
  7614. (setq newend (point))
  7615. (org-reinstall-markers-in-region beg)
  7616. (setq end (point))
  7617. (goto-char beg)
  7618. (skip-chars-forward " \t\n\r")
  7619. (setq beg (point))
  7620. (if (and (outline-invisible-p) visp)
  7621. (save-excursion (outline-show-heading)))
  7622. ;; Shift if necessary
  7623. (unless (= shift 0)
  7624. (save-restriction
  7625. (narrow-to-region beg end)
  7626. (while (not (= shift 0))
  7627. (org-map-region func (point-min) (point-max))
  7628. (setq shift (+ delta shift)))
  7629. (goto-char (point-min))
  7630. (setq newend (point-max))))
  7631. (when (or (org-called-interactively-p 'interactive) for-yank)
  7632. (message "Clipboard pasted as level %d subtree" new-level))
  7633. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7634. kill-ring
  7635. (eq org-subtree-clip (current-kill 0))
  7636. org-subtree-clip-folded)
  7637. ;; The tree was folded before it was killed/copied
  7638. (hide-subtree))
  7639. (and for-yank (goto-char newend))
  7640. (and remove (setq kill-ring (cdr kill-ring))))))
  7641. (defun org-kill-is-subtree-p (&optional txt)
  7642. "Check if the current kill is an outline subtree, or a set of trees.
  7643. Returns nil if kill does not start with a headline, or if the first
  7644. headline level is not the largest headline level in the tree.
  7645. So this will actually accept several entries of equal levels as well,
  7646. which is OK for `org-paste-subtree'.
  7647. If optional TXT is given, check this string instead of the current kill."
  7648. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7649. (re (org-get-limited-outline-regexp))
  7650. (^re (concat "^" re))
  7651. (start-level (and kill
  7652. (string-match
  7653. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7654. kill)
  7655. (- (match-end 2) (match-beginning 2) 1)))
  7656. (start (1+ (or (match-beginning 2) -1))))
  7657. (if (not start-level)
  7658. (progn
  7659. nil) ;; does not even start with a heading
  7660. (catch 'exit
  7661. (while (setq start (string-match ^re kill (1+ start)))
  7662. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7663. (throw 'exit nil)))
  7664. t))))
  7665. (defvar org-markers-to-move nil
  7666. "Markers that should be moved with a cut-and-paste operation.
  7667. Those markers are stored together with their positions relative to
  7668. the start of the region.")
  7669. (defun org-save-markers-in-region (beg end)
  7670. "Check markers in region.
  7671. If these markers are between BEG and END, record their position relative
  7672. to BEG, so that after moving the block of text, we can put the markers back
  7673. into place.
  7674. This function gets called just before an entry or tree gets cut from the
  7675. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7676. called immediately, to move the markers with the entries."
  7677. (setq org-markers-to-move nil)
  7678. (when (featurep 'org-clock)
  7679. (org-clock-save-markers-for-cut-and-paste beg end))
  7680. (when (featurep 'org-agenda)
  7681. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7682. (defun org-check-and-save-marker (marker beg end)
  7683. "Check if MARKER is between BEG and END.
  7684. If yes, remember the marker and the distance to BEG."
  7685. (when (and (marker-buffer marker)
  7686. (equal (marker-buffer marker) (current-buffer)))
  7687. (if (and (>= marker beg) (< marker end))
  7688. (push (cons marker (- marker beg)) org-markers-to-move))))
  7689. (defun org-reinstall-markers-in-region (beg)
  7690. "Move all remembered markers to their position relative to BEG."
  7691. (mapc (lambda (x)
  7692. (move-marker (car x) (+ beg (cdr x))))
  7693. org-markers-to-move)
  7694. (setq org-markers-to-move nil))
  7695. (defun org-narrow-to-subtree ()
  7696. "Narrow buffer to the current subtree."
  7697. (interactive)
  7698. (save-excursion
  7699. (save-match-data
  7700. (org-with-limited-levels
  7701. (narrow-to-region
  7702. (progn (org-back-to-heading t) (point))
  7703. (progn (org-end-of-subtree t t)
  7704. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7705. (point)))))))
  7706. (defun org-narrow-to-block ()
  7707. "Narrow buffer to the current block."
  7708. (interactive)
  7709. (let* ((case-fold-search t)
  7710. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7711. "^[ \t]*#\\+end_.*")))
  7712. (if blockp
  7713. (narrow-to-region (car blockp) (cdr blockp))
  7714. (user-error "Not in a block"))))
  7715. (eval-when-compile
  7716. (defvar org-property-drawer-re))
  7717. (defvar org-property-start-re) ;; defined below
  7718. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7719. "Clone the task (subtree) at point N times.
  7720. The clones will be inserted as siblings.
  7721. In interactive use, the user will be prompted for the number of
  7722. clones to be produced. If the entry has a timestamp, the user
  7723. will also be prompted for a time shift, which may be a repeater
  7724. as used in time stamps, for example `+3d'. To disable this,
  7725. you can call the function with a universal prefix argument.
  7726. When a valid repeater is given and the entry contains any time
  7727. stamps, the clones will become a sequence in time, with time
  7728. stamps in the subtree shifted for each clone produced. If SHIFT
  7729. is nil or the empty string, time stamps will be left alone. The
  7730. ID property of the original subtree is removed.
  7731. If the original subtree did contain time stamps with a repeater,
  7732. the following will happen:
  7733. - the repeater will be removed in each clone
  7734. - an additional clone will be produced, with the current, unshifted
  7735. date(s) in the entry.
  7736. - the original entry will be placed *after* all the clones, with
  7737. repeater intact.
  7738. - the start days in the repeater in the original entry will be shifted
  7739. to past the last clone.
  7740. In this way you can spell out a number of instances of a repeating task,
  7741. and still retain the repeater to cover future instances of the task."
  7742. (interactive "nNumber of clones to produce: ")
  7743. (let ((shift
  7744. (or shift
  7745. (if (and (not (equal current-prefix-arg '(4)))
  7746. (save-excursion
  7747. (re-search-forward org-ts-regexp-both
  7748. (save-excursion
  7749. (org-end-of-subtree t)
  7750. (point)) t)))
  7751. (read-from-minibuffer
  7752. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7753. ""))) ;; No time shift
  7754. (n-no-remove -1)
  7755. (drawer-re org-drawer-regexp)
  7756. beg end template task idprop
  7757. shift-n shift-what doshift nmin nmax)
  7758. (if (not (and (integerp n) (> n 0)))
  7759. (user-error "Invalid number of replications %s" n))
  7760. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7761. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7762. shift)))
  7763. (user-error "Invalid shift specification %s" shift))
  7764. (when doshift
  7765. (setq shift-n (string-to-number (match-string 1 shift))
  7766. shift-what (cdr (assoc (match-string 2 shift)
  7767. '(("d" . day) ("w" . week)
  7768. ("m" . month) ("y" . year))))))
  7769. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7770. (setq nmin 1 nmax n)
  7771. (org-back-to-heading t)
  7772. (setq beg (point))
  7773. (setq idprop (org-entry-get nil "ID"))
  7774. (org-end-of-subtree t t)
  7775. (or (bolp) (insert "\n"))
  7776. (setq end (point))
  7777. (setq template (buffer-substring beg end))
  7778. (when (and doshift
  7779. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7780. (delete-region beg end)
  7781. (setq end beg)
  7782. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7783. (goto-char end)
  7784. (loop for n from nmin to nmax do
  7785. ;; prepare clone
  7786. (with-temp-buffer
  7787. (insert template)
  7788. (org-mode)
  7789. (goto-char (point-min))
  7790. (org-show-subtree)
  7791. (and idprop (if org-clone-delete-id
  7792. (org-entry-delete nil "ID")
  7793. (org-id-get-create t)))
  7794. (unless (= n 0)
  7795. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7796. (kill-whole-line))
  7797. (goto-char (point-min))
  7798. (while (re-search-forward drawer-re nil t)
  7799. (org-remove-empty-drawer-at (point))))
  7800. (goto-char (point-min))
  7801. (when doshift
  7802. (while (re-search-forward org-ts-regexp-both nil t)
  7803. (org-timestamp-change (* n shift-n) shift-what))
  7804. (unless (= n n-no-remove)
  7805. (goto-char (point-min))
  7806. (while (re-search-forward org-ts-regexp nil t)
  7807. (save-excursion
  7808. (goto-char (match-beginning 0))
  7809. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7810. (delete-region (match-beginning 1) (match-end 1)))))))
  7811. (setq task (buffer-string)))
  7812. (insert task))
  7813. (goto-char beg)))
  7814. ;;; Outline Sorting
  7815. (defun org-sort (with-case)
  7816. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7817. Optional argument WITH-CASE means sort case-sensitively."
  7818. (interactive "P")
  7819. (cond
  7820. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7821. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7822. (t
  7823. (org-call-with-arg 'org-sort-entries with-case))))
  7824. (defun org-sort-remove-invisible (s)
  7825. "Remove invisible links from string S."
  7826. (remove-text-properties 0 (length s) org-rm-props s)
  7827. (while (string-match org-bracket-link-regexp s)
  7828. (setq s (replace-match (if (match-end 2)
  7829. (match-string 3 s)
  7830. (match-string 1 s)) t t s)))
  7831. (let ((st (format " %s " s)))
  7832. (while (string-match org-emph-re st)
  7833. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7834. (setq s (substring st 1 -1)))
  7835. s)
  7836. (defvar org-priority-regexp) ; defined later in the file
  7837. (defvar org-after-sorting-entries-or-items-hook nil
  7838. "Hook that is run after a bunch of entries or items have been sorted.
  7839. When children are sorted, the cursor is in the parent line when this
  7840. hook gets called. When a region or a plain list is sorted, the cursor
  7841. will be in the first entry of the sorted region/list.")
  7842. (defun org-sort-entries
  7843. (&optional with-case sorting-type getkey-func compare-func property)
  7844. "Sort entries on a certain level of an outline tree.
  7845. If there is an active region, the entries in the region are sorted.
  7846. Else, if the cursor is before the first entry, sort the top-level items.
  7847. Else, the children of the entry at point are sorted.
  7848. Sorting can be alphabetically, numerically, by date/time as given by
  7849. a time stamp, by a property, by priority order, or by a custom function.
  7850. The command prompts for the sorting type unless it has been given to the
  7851. function through the SORTING-TYPE argument, which needs to be a character,
  7852. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7853. the precise meaning of each character:
  7854. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7855. c By creation time, which is assumed to be the first inactive time stamp
  7856. at the beginning of a line.
  7857. d By deadline date/time.
  7858. k By clocking time.
  7859. n Numerically, by converting the beginning of the entry/item to a number.
  7860. o By order of TODO keywords.
  7861. p By priority according to the cookie.
  7862. r By the value of a property.
  7863. s By scheduled date/time.
  7864. t By date/time, either the first active time stamp in the entry, or, if
  7865. none exist, by the first inactive one.
  7866. Capital letters will reverse the sort order.
  7867. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7868. called with point at the beginning of the record. It must return either
  7869. a string or a number that should serve as the sorting key for that record.
  7870. Comparing entries ignores case by default. However, with an optional argument
  7871. WITH-CASE, the sorting considers case as well.
  7872. Sorting is done against the visible part of the headlines, it ignores hidden
  7873. links.
  7874. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7875. (interactive "P")
  7876. (let ((case-func (if with-case 'identity 'downcase))
  7877. (cmstr
  7878. ;; The clock marker is lost when using `sort-subr', let's
  7879. ;; store the clocking string.
  7880. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7881. (save-excursion
  7882. (goto-char org-clock-marker)
  7883. (looking-back "^.*") (match-string-no-properties 0))))
  7884. start beg end stars re re2
  7885. txt what tmp)
  7886. ;; Find beginning and end of region to sort
  7887. (cond
  7888. ((org-region-active-p)
  7889. ;; we will sort the region
  7890. (setq end (region-end)
  7891. what "region")
  7892. (goto-char (region-beginning))
  7893. (if (not (org-at-heading-p)) (outline-next-heading))
  7894. (setq start (point)))
  7895. ((or (org-at-heading-p)
  7896. (ignore-errors (progn (org-back-to-heading) t)))
  7897. ;; we will sort the children of the current headline
  7898. (org-back-to-heading)
  7899. (setq start (point)
  7900. end (progn (org-end-of-subtree t t)
  7901. (or (bolp) (insert "\n"))
  7902. (when (>= (org-back-over-empty-lines) 1)
  7903. (forward-line 1))
  7904. (point))
  7905. what "children")
  7906. (goto-char start)
  7907. (show-subtree)
  7908. (outline-next-heading))
  7909. (t
  7910. ;; we will sort the top-level entries in this file
  7911. (goto-char (point-min))
  7912. (or (org-at-heading-p) (outline-next-heading))
  7913. (setq start (point))
  7914. (goto-char (point-max))
  7915. (beginning-of-line 1)
  7916. (when (looking-at ".*?\\S-")
  7917. ;; File ends in a non-white line
  7918. (end-of-line 1)
  7919. (insert "\n"))
  7920. (setq end (point-max))
  7921. (setq what "top-level")
  7922. (goto-char start)
  7923. (show-all)))
  7924. (setq beg (point))
  7925. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7926. (looking-at "\\(\\*+\\)")
  7927. (setq stars (match-string 1)
  7928. re (concat "^" (regexp-quote stars) " +")
  7929. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7930. txt (buffer-substring beg end))
  7931. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7932. (if (and (not (equal stars "*")) (string-match re2 txt))
  7933. (user-error "Region to sort contains a level above the first entry"))
  7934. (unless sorting-type
  7935. (message
  7936. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7937. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  7938. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  7939. what)
  7940. (setq sorting-type (read-char-exclusive))
  7941. (unless getkey-func
  7942. (and (= (downcase sorting-type) ?f)
  7943. (setq getkey-func
  7944. (org-icompleting-read "Sort using function: "
  7945. obarray 'fboundp t nil nil))
  7946. (setq getkey-func (intern getkey-func))))
  7947. (and (= (downcase sorting-type) ?r)
  7948. (not property)
  7949. (setq property
  7950. (org-icompleting-read "Property: "
  7951. (mapcar 'list (org-buffer-property-keys t))
  7952. nil t))))
  7953. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  7954. (message "Sorting entries...")
  7955. (save-restriction
  7956. (narrow-to-region start end)
  7957. (let ((dcst (downcase sorting-type))
  7958. (case-fold-search nil)
  7959. (now (current-time)))
  7960. (sort-subr
  7961. (/= dcst sorting-type)
  7962. ;; This function moves to the beginning character of the "record" to
  7963. ;; be sorted.
  7964. (lambda nil
  7965. (if (re-search-forward re nil t)
  7966. (goto-char (match-beginning 0))
  7967. (goto-char (point-max))))
  7968. ;; This function moves to the last character of the "record" being
  7969. ;; sorted.
  7970. (lambda nil
  7971. (save-match-data
  7972. (condition-case nil
  7973. (outline-forward-same-level 1)
  7974. (error
  7975. (goto-char (point-max))))))
  7976. ;; This function returns the value that gets sorted against.
  7977. (lambda nil
  7978. (cond
  7979. ((= dcst ?n)
  7980. (if (looking-at org-complex-heading-regexp)
  7981. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7982. nil))
  7983. ((= dcst ?a)
  7984. (if (looking-at org-complex-heading-regexp)
  7985. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7986. nil))
  7987. ((= dcst ?k)
  7988. (or (get-text-property (point) :org-clock-minutes) 0))
  7989. ((= dcst ?t)
  7990. (let ((end (save-excursion (outline-next-heading) (point))))
  7991. (if (or (re-search-forward org-ts-regexp end t)
  7992. (re-search-forward org-ts-regexp-both end t))
  7993. (org-time-string-to-seconds (match-string 0))
  7994. (org-float-time now))))
  7995. ((= dcst ?c)
  7996. (let ((end (save-excursion (outline-next-heading) (point))))
  7997. (if (re-search-forward
  7998. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7999. end t)
  8000. (org-time-string-to-seconds (match-string 0))
  8001. (org-float-time now))))
  8002. ((= dcst ?s)
  8003. (let ((end (save-excursion (outline-next-heading) (point))))
  8004. (if (re-search-forward org-scheduled-time-regexp end t)
  8005. (org-time-string-to-seconds (match-string 1))
  8006. (org-float-time now))))
  8007. ((= dcst ?d)
  8008. (let ((end (save-excursion (outline-next-heading) (point))))
  8009. (if (re-search-forward org-deadline-time-regexp end t)
  8010. (org-time-string-to-seconds (match-string 1))
  8011. (org-float-time now))))
  8012. ((= dcst ?p)
  8013. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8014. (string-to-char (match-string 2))
  8015. org-default-priority))
  8016. ((= dcst ?r)
  8017. (or (org-entry-get nil property) ""))
  8018. ((= dcst ?o)
  8019. (if (looking-at org-complex-heading-regexp)
  8020. (let* ((m (match-string 2))
  8021. (s (if (member m org-done-keywords) '- '+)))
  8022. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8023. ((= dcst ?f)
  8024. (if getkey-func
  8025. (progn
  8026. (setq tmp (funcall getkey-func))
  8027. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  8028. tmp)
  8029. (error "Invalid key function `%s'" getkey-func)))
  8030. (t (error "Invalid sorting type `%c'" sorting-type))))
  8031. nil
  8032. (cond
  8033. ((= dcst ?a) 'string<)
  8034. ((= dcst ?f) compare-func)
  8035. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8036. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8037. ;; Reset the clock marker if needed
  8038. (when cmstr
  8039. (save-excursion
  8040. (goto-char start)
  8041. (search-forward cmstr nil t)
  8042. (move-marker org-clock-marker (point))))
  8043. (message "Sorting entries...done")))
  8044. (defun org-do-sort (table what &optional with-case sorting-type)
  8045. "Sort TABLE of WHAT according to SORTING-TYPE.
  8046. The user will be prompted for the SORTING-TYPE if the call to this
  8047. function does not specify it.
  8048. WHAT is only for the prompt, to indicate what is being sorted.
  8049. The sorting key will be extracted from the car of the elements of
  8050. the table.
  8051. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  8052. (unless sorting-type
  8053. (message
  8054. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  8055. what)
  8056. (setq sorting-type (read-char-exclusive)))
  8057. (let ((dcst (downcase sorting-type))
  8058. extractfun comparefun)
  8059. ;; Define the appropriate functions
  8060. (cond
  8061. ((= dcst ?n)
  8062. (setq extractfun 'string-to-number
  8063. comparefun (if (= dcst sorting-type) '< '>)))
  8064. ((= dcst ?a)
  8065. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  8066. (lambda(x) (downcase (org-sort-remove-invisible x))))
  8067. comparefun (if (= dcst sorting-type)
  8068. 'string<
  8069. (lambda (a b) (and (not (string< a b))
  8070. (not (string= a b)))))))
  8071. ((= dcst ?t)
  8072. (setq extractfun
  8073. (lambda (x)
  8074. (cond ((or (string-match org-ts-regexp x)
  8075. (string-match org-ts-regexp-both x))
  8076. (org-float-time
  8077. (org-time-string-to-time (match-string 0 x))))
  8078. ((string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" x)
  8079. (org-hh:mm-string-to-minutes x))
  8080. (t 0)))
  8081. comparefun (if (= dcst sorting-type) '< '>)))
  8082. (t (error "Invalid sorting type `%c'" sorting-type)))
  8083. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  8084. table)
  8085. (lambda (a b) (funcall comparefun (car a) (car b))))))
  8086. ;;; The orgstruct minor mode
  8087. ;; Define a minor mode which can be used in other modes in order to
  8088. ;; integrate the org-mode structure editing commands.
  8089. ;; This is really a hack, because the org-mode structure commands use
  8090. ;; keys which normally belong to the major mode. Here is how it
  8091. ;; works: The minor mode defines all the keys necessary to operate the
  8092. ;; structure commands, but wraps the commands into a function which
  8093. ;; tests if the cursor is currently at a headline or a plain list
  8094. ;; item. If that is the case, the structure command is used,
  8095. ;; temporarily setting many Org-mode variables like regular
  8096. ;; expressions for filling etc. However, when any of those keys is
  8097. ;; used at a different location, function uses `key-binding' to look
  8098. ;; up if the key has an associated command in another currently active
  8099. ;; keymap (minor modes, major mode, global), and executes that
  8100. ;; command. There might be problems if any of the keys is otherwise
  8101. ;; used as a prefix key.
  8102. (defcustom orgstruct-heading-prefix-regexp ""
  8103. "Regexp that matches the custom prefix of Org headlines in
  8104. orgstruct(++)-mode."
  8105. :group 'org
  8106. :version "24.4"
  8107. :package-version '(Org . "8.3")
  8108. :type 'regexp)
  8109. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8110. (defcustom orgstruct-setup-hook nil
  8111. "Hook run after orgstruct-mode-map is filled."
  8112. :group 'org
  8113. :version "24.4"
  8114. :package-version '(Org . "8.0")
  8115. :type 'hook)
  8116. (defvar orgstruct-initialized nil)
  8117. (defvar org-local-vars nil
  8118. "List of local variables, for use by `orgstruct-mode'.")
  8119. ;;;###autoload
  8120. (define-minor-mode orgstruct-mode
  8121. "Toggle the minor mode `orgstruct-mode'.
  8122. This mode is for using Org-mode structure commands in other
  8123. modes. The following keys behave as if Org-mode were active, if
  8124. the cursor is on a headline, or on a plain list item (both as
  8125. defined by Org-mode)."
  8126. nil " OrgStruct" (make-sparse-keymap)
  8127. (funcall (if orgstruct-mode
  8128. 'add-to-invisibility-spec
  8129. 'remove-from-invisibility-spec)
  8130. '(outline . t))
  8131. (when orgstruct-mode
  8132. (org-load-modules-maybe)
  8133. (unless orgstruct-initialized
  8134. (orgstruct-setup)
  8135. (setq orgstruct-initialized t))))
  8136. ;;;###autoload
  8137. (defun turn-on-orgstruct ()
  8138. "Unconditionally turn on `orgstruct-mode'."
  8139. (orgstruct-mode 1))
  8140. (defvar org-fb-vars nil)
  8141. (make-variable-buffer-local 'org-fb-vars)
  8142. (defun orgstruct++-mode (&optional arg)
  8143. "Toggle `orgstruct-mode', the enhanced version of it.
  8144. In addition to setting orgstruct-mode, this also exports all
  8145. indentation and autofilling variables from org-mode into the
  8146. buffer. It will also recognize item context in multiline items."
  8147. (interactive "P")
  8148. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8149. (if (< arg 1)
  8150. (progn (orgstruct-mode -1)
  8151. (mapc (lambda(v)
  8152. (org-set-local (car v)
  8153. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8154. org-fb-vars))
  8155. (orgstruct-mode 1)
  8156. (setq org-fb-vars nil)
  8157. (unless org-local-vars
  8158. (setq org-local-vars (org-get-local-variables)))
  8159. (let (var val)
  8160. (mapc
  8161. (lambda (x)
  8162. (when (string-match
  8163. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8164. (symbol-name (car x)))
  8165. (setq var (car x) val (nth 1 x))
  8166. (push (list var `(quote ,(eval var))) org-fb-vars)
  8167. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8168. org-local-vars)
  8169. (org-set-local 'orgstruct-is-++ t))))
  8170. (defvar orgstruct-is-++ nil
  8171. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8172. (make-variable-buffer-local 'orgstruct-is-++)
  8173. ;;;###autoload
  8174. (defun turn-on-orgstruct++ ()
  8175. "Unconditionally turn on `orgstruct++-mode'."
  8176. (orgstruct++-mode 1))
  8177. (defun orgstruct-error ()
  8178. "Error when there is no default binding for a structure key."
  8179. (interactive)
  8180. (funcall (if (fboundp 'user-error)
  8181. 'user-error
  8182. 'error)
  8183. "This key has no function outside structure elements"))
  8184. (defun orgstruct-setup ()
  8185. "Setup orgstruct keymap."
  8186. (dolist (cell '((org-demote . t)
  8187. (org-metaleft . t)
  8188. (org-metaright . t)
  8189. (org-promote . t)
  8190. (org-shiftmetaleft . t)
  8191. (org-shiftmetaright . t)
  8192. org-backward-element
  8193. org-backward-heading-same-level
  8194. org-ctrl-c-ret
  8195. org-ctrl-c-minus
  8196. org-ctrl-c-star
  8197. org-cycle
  8198. org-forward-heading-same-level
  8199. org-insert-heading
  8200. org-insert-heading-respect-content
  8201. org-kill-note-or-show-branches
  8202. org-mark-subtree
  8203. org-meta-return
  8204. org-metadown
  8205. org-metaup
  8206. org-narrow-to-subtree
  8207. org-promote-subtree
  8208. org-reveal
  8209. org-shiftdown
  8210. org-shiftleft
  8211. org-shiftmetadown
  8212. org-shiftmetaup
  8213. org-shiftright
  8214. org-shifttab
  8215. org-shifttab
  8216. org-shiftup
  8217. org-show-subtree
  8218. org-sort
  8219. org-up-element
  8220. outline-demote
  8221. outline-next-visible-heading
  8222. outline-previous-visible-heading
  8223. outline-promote
  8224. outline-up-heading
  8225. show-children))
  8226. (let ((f (or (car-safe cell) cell))
  8227. (disable-when-heading-prefix (cdr-safe cell)))
  8228. (when (fboundp f)
  8229. (let ((new-bindings))
  8230. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8231. (where-is-internal f outline-mode-map)))
  8232. (push binding new-bindings)
  8233. ;; TODO use local-function-key-map
  8234. (dolist (rep '(("<tab>" . "TAB")
  8235. ("<return>" . "RET")
  8236. ("<escape>" . "ESC")
  8237. ("<delete>" . "DEL")))
  8238. (setq binding (read-kbd-macro
  8239. (let ((case-fold-search))
  8240. (replace-regexp-in-string
  8241. (regexp-quote (cdr rep))
  8242. (car rep)
  8243. (key-description binding)))))
  8244. (pushnew binding new-bindings :test 'equal)))
  8245. (dolist (binding new-bindings)
  8246. (let ((key (lookup-key orgstruct-mode-map binding)))
  8247. (when (or (not key) (numberp key))
  8248. (ignore-errors
  8249. (org-defkey orgstruct-mode-map
  8250. binding
  8251. (orgstruct-make-binding
  8252. f binding disable-when-heading-prefix))))))))))
  8253. (run-hooks 'orgstruct-setup-hook))
  8254. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8255. "Create a function for binding in the structure minor mode.
  8256. FUN is the command to call inside a table. KEY is the key that
  8257. should be checked in for a command to execute outside of tables.
  8258. Non-nil `disable-when-heading-prefix' means to disable the command
  8259. if `orgstruct-heading-prefix-regexp' is not empty."
  8260. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8261. (let ((nname name)
  8262. (i 0))
  8263. (while (fboundp (intern nname))
  8264. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8265. (setq name (intern nname)))
  8266. (eval
  8267. (let ((bindings '((org-heading-regexp
  8268. (concat "^"
  8269. orgstruct-heading-prefix-regexp
  8270. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8271. (org-outline-regexp
  8272. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8273. (org-outline-regexp-bol
  8274. (concat "^" org-outline-regexp))
  8275. (outline-regexp org-outline-regexp)
  8276. (outline-heading-end-regexp "\n")
  8277. (outline-level 'org-outline-level)
  8278. (outline-heading-alist))))
  8279. `(defun ,name (arg)
  8280. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8281. "Outside of structure, run the binding of `"
  8282. (key-description key) "'."
  8283. (when disable-when-heading-prefix
  8284. (concat
  8285. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8286. "back to the default binding due to limitations of Org's implementation of\n"
  8287. "`" (symbol-name fun) "'.")))
  8288. (interactive "p")
  8289. (let* ((disable
  8290. ,(and disable-when-heading-prefix
  8291. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8292. (fallback
  8293. (or disable
  8294. (not
  8295. (let* ,bindings
  8296. (org-context-p 'headline 'item
  8297. ,(when (memq fun
  8298. '(org-insert-heading
  8299. org-insert-heading-respect-content
  8300. org-meta-return))
  8301. '(when orgstruct-is-++
  8302. 'item-body))))))))
  8303. (if fallback
  8304. (let* ((orgstruct-mode)
  8305. (binding
  8306. (loop with key = ,key
  8307. for rep in
  8308. '(nil
  8309. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8310. ("<\\([^>]*\\)return>" . "\\1RET")
  8311. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8312. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8313. do
  8314. (when rep
  8315. (setq key (read-kbd-macro
  8316. (let ((case-fold-search))
  8317. (replace-regexp-in-string
  8318. (car rep)
  8319. (cdr rep)
  8320. (key-description key))))))
  8321. thereis (key-binding key))))
  8322. (if (keymapp binding)
  8323. (org-set-transient-map binding)
  8324. (let ((func (or binding
  8325. (unless disable
  8326. 'orgstruct-error))))
  8327. (when func
  8328. (call-interactively func)))))
  8329. (org-run-like-in-org-mode
  8330. (lambda ()
  8331. (interactive)
  8332. (let* ,bindings
  8333. (call-interactively ',fun)))))))))
  8334. name))
  8335. (defun org-contextualize-keys (alist contexts)
  8336. "Return valid elements in ALIST depending on CONTEXTS.
  8337. `org-agenda-custom-commands' or `org-capture-templates' are the
  8338. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8339. or `org-capture-templates-contexts' are the associated contexts
  8340. definitions."
  8341. (let ((contexts
  8342. ;; normalize contexts
  8343. (mapcar
  8344. (lambda(c) (cond ((listp (cadr c))
  8345. (list (car c) (car c) (cadr c)))
  8346. ((string= "" (cadr c))
  8347. (list (car c) (car c) (caddr c)))
  8348. (t c))) contexts))
  8349. (a alist) c r s)
  8350. ;; loop over all commands or templates
  8351. (while (setq c (pop a))
  8352. (let (vrules repl)
  8353. (cond
  8354. ((not (assoc (car c) contexts))
  8355. (push c r))
  8356. ((and (assoc (car c) contexts)
  8357. (setq vrules (org-contextualize-validate-key
  8358. (car c) contexts)))
  8359. (mapc (lambda (vr)
  8360. (when (not (equal (car vr) (cadr vr)))
  8361. (setq repl vr))) vrules)
  8362. (if (not repl) (push c r)
  8363. (push (cadr repl) s)
  8364. (push
  8365. (cons (car c)
  8366. (cdr (or (assoc (cadr repl) alist)
  8367. (error "Undefined key `%s' as contextual replacement for `%s'"
  8368. (cadr repl) (car c)))))
  8369. r))))))
  8370. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8371. (delq
  8372. nil
  8373. (delete-dups
  8374. (mapcar (lambda (x)
  8375. (let ((tpl (car x)))
  8376. (when (not (delq
  8377. nil
  8378. (mapcar (lambda(y)
  8379. (equal y tpl)) s))) x)))
  8380. (reverse r))))))
  8381. (defun org-contextualize-validate-key (key contexts)
  8382. "Check CONTEXTS for agenda or capture KEY."
  8383. (let (r rr res)
  8384. (while (setq r (pop contexts))
  8385. (mapc
  8386. (lambda (rr)
  8387. (when
  8388. (and (equal key (car r))
  8389. (if (functionp rr) (funcall rr)
  8390. (or (and (eq (car rr) 'in-file)
  8391. (buffer-file-name)
  8392. (string-match (cdr rr) (buffer-file-name)))
  8393. (and (eq (car rr) 'in-mode)
  8394. (string-match (cdr rr) (symbol-name major-mode)))
  8395. (and (eq (car rr) 'in-buffer)
  8396. (string-match (cdr rr) (buffer-name)))
  8397. (when (and (eq (car rr) 'not-in-file)
  8398. (buffer-file-name))
  8399. (not (string-match (cdr rr) (buffer-file-name))))
  8400. (when (eq (car rr) 'not-in-mode)
  8401. (not (string-match (cdr rr) (symbol-name major-mode))))
  8402. (when (eq (car rr) 'not-in-buffer)
  8403. (not (string-match (cdr rr) (buffer-name)))))))
  8404. (push r res)))
  8405. (car (last r))))
  8406. (delete-dups (delq nil res))))
  8407. (defun org-context-p (&rest contexts)
  8408. "Check if local context is any of CONTEXTS.
  8409. Possible values in the list of contexts are `table', `headline', and `item'."
  8410. (let ((pos (point)))
  8411. (goto-char (point-at-bol))
  8412. (prog1 (or (and (memq 'table contexts)
  8413. (looking-at "[ \t]*|"))
  8414. (and (memq 'headline contexts)
  8415. (looking-at org-outline-regexp))
  8416. (and (memq 'item contexts)
  8417. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8418. (and (memq 'item-body contexts)
  8419. (org-in-item-p)))
  8420. (goto-char pos))))
  8421. (defun org-get-local-variables ()
  8422. "Return a list of all local variables in an Org mode buffer."
  8423. (let (varlist)
  8424. (with-current-buffer (get-buffer-create "*Org tmp*")
  8425. (erase-buffer)
  8426. (org-mode)
  8427. (setq varlist (buffer-local-variables)))
  8428. (kill-buffer "*Org tmp*")
  8429. (delq nil
  8430. (mapcar
  8431. (lambda (x)
  8432. (setq x
  8433. (if (symbolp x)
  8434. (list x)
  8435. (list (car x) (cdr x))))
  8436. (if (and (not (get (car x) 'org-state))
  8437. (string-match
  8438. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8439. (symbol-name (car x))))
  8440. x nil))
  8441. varlist))))
  8442. (defun org-clone-local-variables (from-buffer &optional regexp)
  8443. "Clone local variables from FROM-BUFFER.
  8444. Optional argument REGEXP selects variables to clone."
  8445. (mapc
  8446. (lambda (pair)
  8447. (and (symbolp (car pair))
  8448. (or (null regexp)
  8449. (string-match regexp (symbol-name (car pair))))
  8450. (set (make-local-variable (car pair))
  8451. (cdr pair))))
  8452. (buffer-local-variables from-buffer)))
  8453. ;;;###autoload
  8454. (defun org-run-like-in-org-mode (cmd)
  8455. "Run a command, pretending that the current buffer is in Org-mode.
  8456. This will temporarily bind local variables that are typically bound in
  8457. Org-mode to the values they have in Org-mode, and then interactively
  8458. call CMD."
  8459. (org-load-modules-maybe)
  8460. (unless org-local-vars
  8461. (setq org-local-vars (org-get-local-variables)))
  8462. (let (binds)
  8463. (dolist (var org-local-vars)
  8464. (when (or (not (boundp (car var)))
  8465. (eq (symbol-value (car var))
  8466. (default-value (car var))))
  8467. (push (list (car var) `(quote ,(cadr var))) binds)))
  8468. (eval `(let ,binds
  8469. (call-interactively (quote ,cmd))))))
  8470. (defun org-get-category (&optional pos force-refresh)
  8471. "Get the category applying to position POS."
  8472. (save-match-data
  8473. (if force-refresh (org-refresh-category-properties))
  8474. (let ((pos (or pos (point))))
  8475. (or (get-text-property pos 'org-category)
  8476. (progn (org-refresh-category-properties)
  8477. (get-text-property pos 'org-category))))))
  8478. ;;; Refresh properties
  8479. (defun org-refresh-properties (dprop tprop)
  8480. "Refresh buffer text properties.
  8481. DPROP is the drawer property and TPROP is either the
  8482. corresponding text property to set, or an alist with each element
  8483. being a text property (as a symbol) and a function to apply to
  8484. the value of the drawer property."
  8485. (let ((case-fold-search t)
  8486. (inhibit-read-only t))
  8487. (org-with-silent-modifications
  8488. (save-excursion
  8489. (save-restriction
  8490. (widen)
  8491. (goto-char (point-min))
  8492. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8493. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8494. (defun org-refresh-property (tprop p)
  8495. "Refresh the buffer text property TPROP from the drawer property P.
  8496. The refresh happens only for the current tree (not subtree)."
  8497. (save-excursion
  8498. (org-back-to-heading t)
  8499. ;; tprop is a text property symbol
  8500. (if (symbolp tprop)
  8501. (put-text-property
  8502. (point) (or (outline-next-heading) (point-max)) tprop p)
  8503. ;; tprop is an alist with (properties . function) elements
  8504. (mapc (lambda(al)
  8505. (save-excursion
  8506. (put-text-property
  8507. (point-at-bol) (or (outline-next-heading) (point-max))
  8508. (car al)
  8509. (funcall (cdr al) p))))
  8510. tprop))))
  8511. (defun org-refresh-category-properties ()
  8512. "Refresh category text properties in the buffer."
  8513. (let ((case-fold-search t)
  8514. (inhibit-read-only t)
  8515. (def-cat (cond
  8516. ((null org-category)
  8517. (if buffer-file-name
  8518. (file-name-sans-extension
  8519. (file-name-nondirectory buffer-file-name))
  8520. "???"))
  8521. ((symbolp org-category) (symbol-name org-category))
  8522. (t org-category)))
  8523. beg end cat pos optionp)
  8524. (org-with-silent-modifications
  8525. (save-excursion
  8526. (save-restriction
  8527. (widen)
  8528. (goto-char (point-min))
  8529. (put-text-property (point) (point-max) 'org-category def-cat)
  8530. (while (re-search-forward
  8531. "^[ \t]*\\(\\(?:#\\+\\|:\\)CATEGORY:\\)\\(.*\\)" nil t)
  8532. (setq pos (match-end 0)
  8533. optionp (equal (char-after (match-beginning 0)) ?#)
  8534. cat (org-trim (match-string 2)))
  8535. (if optionp
  8536. (setq beg (point-at-bol) end (point-max))
  8537. (org-back-to-heading t)
  8538. (setq beg (point) end (org-end-of-subtree t t)))
  8539. (put-text-property beg end 'org-category cat)
  8540. (goto-char pos)))))))
  8541. (defun org-refresh-stats-properties ()
  8542. "Refresh stats text properties in the buffer."
  8543. (let (stats)
  8544. (org-with-silent-modifications
  8545. (save-excursion
  8546. (save-restriction
  8547. (widen)
  8548. (goto-char (point-min))
  8549. (while (re-search-forward
  8550. (concat org-outline-regexp-bol ".*"
  8551. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8552. nil t)
  8553. (setq stats (cond ((equal (match-string 3) "0") 0)
  8554. ((match-string 2)
  8555. (/ (* (string-to-number (match-string 2)) 100)
  8556. (string-to-number (match-string 3))))
  8557. (t (string-to-number (match-string 1)))))
  8558. (org-back-to-heading t)
  8559. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8560. 'org-stats stats)))))))
  8561. (defun org-refresh-effort-properties ()
  8562. "Refresh effort properties"
  8563. (org-refresh-properties
  8564. org-effort-property
  8565. '((effort . identity)
  8566. (effort-minutes . org-duration-string-to-minutes))))
  8567. ;;;; Link Stuff
  8568. ;;; Link abbreviations
  8569. (defun org-link-expand-abbrev (link)
  8570. "Apply replacements as defined in `org-link-abbrev-alist'."
  8571. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8572. (let* ((key (match-string 1 link))
  8573. (as (or (assoc key org-link-abbrev-alist-local)
  8574. (assoc key org-link-abbrev-alist)))
  8575. (tag (and (match-end 2) (match-string 3 link)))
  8576. rpl)
  8577. (if (not as)
  8578. link
  8579. (setq rpl (cdr as))
  8580. (cond
  8581. ((symbolp rpl) (funcall rpl tag))
  8582. ((string-match "%(\\([^)]+\\))" rpl)
  8583. (replace-match
  8584. (save-match-data
  8585. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8586. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8587. ((string-match "%h" rpl)
  8588. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8589. (t (concat rpl tag)))))
  8590. link))
  8591. ;;; Storing and inserting links
  8592. (defvar org-insert-link-history nil
  8593. "Minibuffer history for links inserted with `org-insert-link'.")
  8594. (defvar org-stored-links nil
  8595. "Contains the links stored with `org-store-link'.")
  8596. (defvar org-store-link-plist nil
  8597. "Plist with info about the most recently link created with `org-store-link'.")
  8598. (defvar org-link-protocols nil
  8599. "Link protocols added to Org-mode using `org-add-link-type'.")
  8600. (defvar org-store-link-functions nil
  8601. "List of functions that are called to create and store a link.
  8602. Each function will be called in turn until one returns a non-nil
  8603. value. Each function should check if it is responsible for creating
  8604. this link (for example by looking at the major mode).
  8605. If not, it must exit and return nil.
  8606. If yes, it should return a non-nil value after a calling
  8607. `org-store-link-props' with a list of properties and values.
  8608. Special properties are:
  8609. :type The link prefix, like \"http\". This must be given.
  8610. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8611. This is obligatory as well.
  8612. :description Optional default description for the second pair
  8613. of brackets in an Org-mode link. The user can still change
  8614. this when inserting this link into an Org-mode buffer.
  8615. In addition to these, any additional properties can be specified
  8616. and then used in capture templates.")
  8617. (defun org-add-link-type (type &optional follow export)
  8618. "Add TYPE to the list of `org-link-types'.
  8619. Re-compute all regular expressions depending on `org-link-types'
  8620. FOLLOW and EXPORT are two functions.
  8621. FOLLOW should take the link path as the single argument and do whatever
  8622. is necessary to follow the link, for example find a file or display
  8623. a mail message.
  8624. EXPORT should format the link path for export to one of the export formats.
  8625. It should be a function accepting three arguments:
  8626. path the path of the link, the text after the prefix (like \"http:\")
  8627. desc the description of the link, if any, or a description added by
  8628. org-export-normalize-links if there is none
  8629. format the export format, a symbol like `html' or `latex' or `ascii'..
  8630. The function may use the FORMAT information to return different values
  8631. depending on the format. The return value will be put literally into
  8632. the exported file. If the return value is nil, this means Org should
  8633. do what it normally does with links which do not have EXPORT defined.
  8634. Org-mode has a built-in default for exporting links. If you are happy with
  8635. this default, there is no need to define an export function for the link
  8636. type. For a simple example of an export function, see `org-bbdb.el'."
  8637. (add-to-list 'org-link-types type t)
  8638. (org-make-link-regexps)
  8639. (if (assoc type org-link-protocols)
  8640. (setcdr (assoc type org-link-protocols) (list follow export))
  8641. (push (list type follow export) org-link-protocols)))
  8642. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8643. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8644. ;;;###autoload
  8645. (defun org-store-link (arg)
  8646. "\\<org-mode-map>Store an org-link to the current location.
  8647. This link is added to `org-stored-links' and can later be inserted
  8648. into an org-buffer with \\[org-insert-link].
  8649. For some link types, a prefix arg is interpreted.
  8650. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8651. For file links, arg negates `org-context-in-file-links'.
  8652. A double prefix arg force skipping storing functions that are not
  8653. part of Org's core.
  8654. A triple prefix arg force storing a link for each line in the
  8655. active region."
  8656. (interactive "P")
  8657. (org-load-modules-maybe)
  8658. (if (and (equal arg '(64)) (org-region-active-p))
  8659. (save-excursion
  8660. (let ((end (region-end)))
  8661. (goto-char (region-beginning))
  8662. (set-mark (point))
  8663. (while (< (point-at-eol) end)
  8664. (move-end-of-line 1) (activate-mark)
  8665. (let (current-prefix-arg)
  8666. (call-interactively 'org-store-link))
  8667. (move-beginning-of-line 2)
  8668. (set-mark (point)))))
  8669. (org-with-limited-levels
  8670. (setq org-store-link-plist nil)
  8671. (let (link cpltxt desc description search
  8672. txt custom-id agenda-link sfuns sfunsn)
  8673. (cond
  8674. ;; Store a link using an external link type
  8675. ((and (not (equal arg '(16)))
  8676. (setq sfuns
  8677. (delq
  8678. nil (mapcar (lambda (f)
  8679. (let (fs) (if (funcall f) (push f fs))))
  8680. org-store-link-functions))
  8681. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8682. (or (and (cdr sfuns)
  8683. (funcall (intern
  8684. (completing-read
  8685. "Which function for creating the link? "
  8686. sfunsn nil t (car sfunsn)))))
  8687. (funcall (caar sfuns)))
  8688. (setq link (plist-get org-store-link-plist :link)
  8689. desc (or (plist-get org-store-link-plist
  8690. :description) link))))
  8691. ;; Store a link from a source code buffer
  8692. ((org-src-edit-buffer-p)
  8693. (let (label gc)
  8694. (while (or (not label)
  8695. (save-excursion
  8696. (save-restriction
  8697. (widen)
  8698. (goto-char (point-min))
  8699. (re-search-forward
  8700. (regexp-quote (format org-coderef-label-format label))
  8701. nil t))))
  8702. (when label (message "Label exists already") (sit-for 2))
  8703. (setq label (read-string "Code line label: " label)))
  8704. (end-of-line 1)
  8705. (setq link (format org-coderef-label-format label))
  8706. (setq gc (- 79 (length link)))
  8707. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8708. (insert link)
  8709. (setq link (concat "(" label ")") desc nil)))
  8710. ;; We are in the agenda, link to referenced location
  8711. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8712. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8713. (get-text-property (point) 'org-marker))))
  8714. (when m
  8715. (org-with-point-at m
  8716. (setq agenda-link
  8717. (if (org-called-interactively-p 'any)
  8718. (call-interactively 'org-store-link)
  8719. (org-store-link nil)))))))
  8720. ((eq major-mode 'calendar-mode)
  8721. (let ((cd (calendar-cursor-to-date)))
  8722. (setq link
  8723. (format-time-string
  8724. (car org-time-stamp-formats)
  8725. (apply 'encode-time
  8726. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8727. nil nil nil))))
  8728. (org-store-link-props :type "calendar" :date cd)))
  8729. ((eq major-mode 'help-mode)
  8730. (setq link (concat "help:" (save-excursion
  8731. (goto-char (point-min))
  8732. (looking-at "^[^ ]+")
  8733. (match-string 0))))
  8734. (org-store-link-props :type "help"))
  8735. ((eq major-mode 'w3-mode)
  8736. (setq cpltxt (if (and (buffer-name)
  8737. (not (string-match "Untitled" (buffer-name))))
  8738. (buffer-name)
  8739. (url-view-url t))
  8740. link (url-view-url t))
  8741. (org-store-link-props :type "w3" :url (url-view-url t)))
  8742. ((eq major-mode 'image-mode)
  8743. (setq cpltxt (concat "file:"
  8744. (abbreviate-file-name buffer-file-name))
  8745. link cpltxt)
  8746. (org-store-link-props :type "image" :file buffer-file-name))
  8747. ;; In dired, store a link to the file of the current line
  8748. ((derived-mode-p 'dired-mode)
  8749. (let ((file (dired-get-filename nil t)))
  8750. (setq file (if file
  8751. (abbreviate-file-name
  8752. (expand-file-name (dired-get-filename nil t)))
  8753. ;; otherwise, no file so use current directory.
  8754. default-directory))
  8755. (setq cpltxt (concat "file:" file)
  8756. link cpltxt)))
  8757. ((setq search (run-hook-with-args-until-success
  8758. 'org-create-file-search-functions))
  8759. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8760. "::" search))
  8761. (setq cpltxt (or description link)))
  8762. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8763. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8764. (cond
  8765. ;; Store a link using the target at point
  8766. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8767. (setq cpltxt
  8768. (concat "file:"
  8769. (abbreviate-file-name
  8770. (buffer-file-name (buffer-base-buffer)))
  8771. "::" (match-string 1))
  8772. link cpltxt))
  8773. ((and (featurep 'org-id)
  8774. (or (eq org-id-link-to-org-use-id t)
  8775. (and (org-called-interactively-p 'any)
  8776. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8777. (and (eq org-id-link-to-org-use-id
  8778. 'create-if-interactive-and-no-custom-id)
  8779. (not custom-id))))
  8780. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8781. ;; Store a link using the ID at point
  8782. (setq link (condition-case nil
  8783. (prog1 (org-id-store-link)
  8784. (setq desc (or (plist-get org-store-link-plist
  8785. :description)
  8786. "")))
  8787. (error
  8788. ;; Probably before first headline, link only to file
  8789. (concat "file:"
  8790. (abbreviate-file-name
  8791. (buffer-file-name (buffer-base-buffer))))))))
  8792. (t
  8793. ;; Just link to current headline
  8794. (setq cpltxt (concat "file:"
  8795. (abbreviate-file-name
  8796. (buffer-file-name (buffer-base-buffer)))))
  8797. ;; Add a context search string
  8798. (when (org-xor org-context-in-file-links arg)
  8799. (let* ((ee (org-element-at-point))
  8800. (et (org-element-type ee))
  8801. (ev (plist-get (cadr ee) :value))
  8802. (ek (plist-get (cadr ee) :key))
  8803. (eok (and (stringp ek) (string-match "name" ek))))
  8804. (setq txt (cond
  8805. ((org-at-heading-p) nil)
  8806. ((and (eq et 'keyword) eok) ev)
  8807. ((org-region-active-p)
  8808. (buffer-substring (region-beginning) (region-end)))))
  8809. (when (or (null txt) (string-match "\\S-" txt))
  8810. (setq cpltxt
  8811. (concat cpltxt "::"
  8812. (condition-case nil
  8813. (org-make-org-heading-search-string txt)
  8814. (error "")))
  8815. desc (or (and (eq et 'keyword) eok ev)
  8816. (nth 4 (ignore-errors (org-heading-components)))
  8817. "NONE")))))
  8818. (if (string-match "::\\'" cpltxt)
  8819. (setq cpltxt (substring cpltxt 0 -2)))
  8820. (setq link cpltxt))))
  8821. ((buffer-file-name (buffer-base-buffer))
  8822. ;; Just link to this file here.
  8823. (setq cpltxt (concat "file:"
  8824. (abbreviate-file-name
  8825. (buffer-file-name (buffer-base-buffer)))))
  8826. ;; Add a context string.
  8827. (when (org-xor org-context-in-file-links arg)
  8828. (setq txt (if (org-region-active-p)
  8829. (buffer-substring (region-beginning) (region-end))
  8830. (buffer-substring (point-at-bol) (point-at-eol))))
  8831. ;; Only use search option if there is some text.
  8832. (when (string-match "\\S-" txt)
  8833. (setq cpltxt
  8834. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8835. desc "NONE")))
  8836. (setq link cpltxt))
  8837. ((org-called-interactively-p 'interactive)
  8838. (user-error "No method for storing a link from this buffer"))
  8839. (t (setq link nil)))
  8840. ;; We're done setting link and desc, clean up
  8841. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8842. (setq link (or link cpltxt)
  8843. desc (or desc cpltxt))
  8844. (cond ((equal desc "NONE") (setq desc nil))
  8845. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8846. (let ((d0 (match-string 3 desc))
  8847. (p0 (match-string 5 desc)))
  8848. (setq desc
  8849. (replace-regexp-in-string
  8850. org-bracket-link-regexp
  8851. (concat (or p0 d0)
  8852. (if (equal (length (match-string 0 desc))
  8853. (length desc)) "*" "")) desc)))))
  8854. ;; Return the link
  8855. (if (not (and (or (org-called-interactively-p 'any)
  8856. executing-kbd-macro) link))
  8857. (or agenda-link (and link (org-make-link-string link desc)))
  8858. (push (list link desc) org-stored-links)
  8859. (message "Stored: %s" (or desc link))
  8860. (when custom-id
  8861. (setq link (concat "file:" (abbreviate-file-name
  8862. (buffer-file-name)) "::#" custom-id))
  8863. (push (list link desc) org-stored-links))
  8864. (car org-stored-links))))))
  8865. (defun org-store-link-props (&rest plist)
  8866. "Store link properties, extract names and addresses."
  8867. (let (x adr)
  8868. (when (setq x (plist-get plist :from))
  8869. (setq adr (mail-extract-address-components x))
  8870. (setq plist (plist-put plist :fromname (car adr)))
  8871. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8872. (when (setq x (plist-get plist :to))
  8873. (setq adr (mail-extract-address-components x))
  8874. (setq plist (plist-put plist :toname (car adr)))
  8875. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8876. (let ((from (plist-get plist :from))
  8877. (to (plist-get plist :to)))
  8878. (when (and from to org-from-is-user-regexp)
  8879. (setq plist
  8880. (plist-put plist :fromto
  8881. (if (string-match org-from-is-user-regexp from)
  8882. (concat "to %t")
  8883. (concat "from %f"))))))
  8884. (setq org-store-link-plist plist))
  8885. (defun org-add-link-props (&rest plist)
  8886. "Add these properties to the link property list."
  8887. (let (key value)
  8888. (while plist
  8889. (setq key (pop plist) value (pop plist))
  8890. (setq org-store-link-plist
  8891. (plist-put org-store-link-plist key value)))))
  8892. (defun org-email-link-description (&optional fmt)
  8893. "Return the description part of an email link.
  8894. This takes information from `org-store-link-plist' and formats it
  8895. according to FMT (default from `org-email-link-description-format')."
  8896. (setq fmt (or fmt org-email-link-description-format))
  8897. (let* ((p org-store-link-plist)
  8898. (to (plist-get p :toaddress))
  8899. (from (plist-get p :fromaddress))
  8900. (table
  8901. (list
  8902. (cons "%c" (plist-get p :fromto))
  8903. (cons "%F" (plist-get p :from))
  8904. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8905. (cons "%T" (plist-get p :to))
  8906. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8907. (cons "%s" (plist-get p :subject))
  8908. (cons "%d" (plist-get p :date))
  8909. (cons "%m" (plist-get p :message-id)))))
  8910. (when (string-match "%c" fmt)
  8911. ;; Check if the user wrote this message
  8912. (if (and org-from-is-user-regexp from to
  8913. (save-match-data (string-match org-from-is-user-regexp from)))
  8914. (setq fmt (replace-match "to %t" t t fmt))
  8915. (setq fmt (replace-match "from %f" t t fmt))))
  8916. (org-replace-escapes fmt table)))
  8917. (defun org-make-org-heading-search-string (&optional string)
  8918. "Make search string for the current headline or STRING."
  8919. (let ((s (or string
  8920. (and (derived-mode-p 'org-mode)
  8921. (save-excursion
  8922. (org-back-to-heading t)
  8923. (org-element-property :raw-value (org-element-at-point))))))
  8924. (lines org-context-in-file-links))
  8925. (or string (setq s (concat "*" s))) ; Add * for headlines
  8926. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8927. (when (and string (integerp lines) (> lines 0))
  8928. (let ((slines (org-split-string s "\n")))
  8929. (when (< lines (length slines))
  8930. (setq s (mapconcat
  8931. 'identity
  8932. (reverse (nthcdr (- (length slines) lines)
  8933. (reverse slines))) "\n")))))
  8934. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8935. (defun org-make-link-string (link &optional description)
  8936. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8937. (unless (string-match "\\S-" link)
  8938. (error "Empty link"))
  8939. (when (and description
  8940. (stringp description)
  8941. (not (string-match "\\S-" description)))
  8942. (setq description nil))
  8943. (when (stringp description)
  8944. ;; Remove brackets from the description, they are fatal.
  8945. (while (string-match "\\[" description)
  8946. (setq description (replace-match "{" t t description)))
  8947. (while (string-match "\\]" description)
  8948. (setq description (replace-match "}" t t description))))
  8949. (when (equal link description)
  8950. ;; No description needed, it is identical
  8951. (setq description nil))
  8952. (when (and (not description)
  8953. (not (string-match (org-image-file-name-regexp) link))
  8954. (not (equal link (org-link-escape link))))
  8955. (setq description (org-extract-attributes link)))
  8956. (setq link
  8957. (cond ((string-match (org-image-file-name-regexp) link) link)
  8958. ((string-match org-link-types-re link)
  8959. (concat (match-string 1 link)
  8960. (org-link-escape (substring link (match-end 1)))))
  8961. (t (org-link-escape link))))
  8962. (concat "[[" link "]"
  8963. (if description (concat "[" description "]") "")
  8964. "]"))
  8965. (defconst org-link-escape-chars
  8966. ;;%20 %5B %5D
  8967. '(?\ ?\[ ?\])
  8968. "List of characters that should be escaped in a link when stored to Org.
  8969. This is the list that is used for internal purposes.")
  8970. (defconst org-link-escape-chars-browser
  8971. ;;%20 %22
  8972. '(?\ ?\")
  8973. "List of characters to be escaped before handing over to the browser.
  8974. If you consider using this constant then you probably want to use
  8975. the function `org-link-escape-browser' instead. See there why
  8976. this constant is a candidate to be removed once Org drops support
  8977. for Emacs 24.1 and 24.2.")
  8978. (defun org-link-escape (text &optional table merge)
  8979. "Return percent escaped representation of TEXT.
  8980. TEXT is a string with the text to escape.
  8981. Optional argument TABLE is a list with characters that should be
  8982. escaped. When nil, `org-link-escape-chars' is used.
  8983. If optional argument MERGE is set, merge TABLE into
  8984. `org-link-escape-chars'."
  8985. ;; Don't escape chars in internal links
  8986. (if (string-match "^\\*[[:alnum:]]+" text)
  8987. text
  8988. (cond
  8989. ((and table merge)
  8990. (mapc (lambda (defchr)
  8991. (unless (member defchr table)
  8992. (setq table (cons defchr table))))
  8993. org-link-escape-chars))
  8994. ((null table)
  8995. (setq table org-link-escape-chars)))
  8996. (mapconcat
  8997. (lambda (char)
  8998. (if (or (member char table)
  8999. (and (or (< char 32) (= char ?\%) (> char 126))
  9000. org-url-hexify-p))
  9001. (mapconcat (lambda (sequence-element)
  9002. (format "%%%.2X" sequence-element))
  9003. (or (encode-coding-char char 'utf-8)
  9004. (error "Unable to percent escape character: %s"
  9005. (char-to-string char))) "")
  9006. (char-to-string char))) text "")))
  9007. (defun org-link-escape-browser (text)
  9008. "Escape some characters before handing over to the browser.
  9009. This function is a candidate to be removed together with the
  9010. constant `org-link-escape-chars-browser' once Org drops support
  9011. for Emacs 24.1 and 24.2. All calls to this function will have to
  9012. be replaced with `url-encode-url' which is available since Emacs
  9013. 24.3.1."
  9014. ;; Example with the Org link
  9015. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  9016. ;; to open the browser with +subject:"Release 8.2" filled into the
  9017. ;; query field: In this case the variable TEXT contains the
  9018. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  9019. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  9020. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9021. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9022. (if (fboundp 'url-encode-url)
  9023. (url-encode-url text)
  9024. (if (org-string-match-p
  9025. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9026. text)
  9027. (org-link-escape text org-link-escape-chars-browser)
  9028. text)))
  9029. (defun org-link-unescape (str)
  9030. "Unhex hexified Unicode strings as returned from the JavaScript function
  9031. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  9032. (unless (and (null str) (string= "" str))
  9033. (let ((pos 0) (case-fold-search t) unhexed)
  9034. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  9035. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  9036. (setq str (replace-match unhexed t t str))
  9037. (setq pos (+ pos (length unhexed))))))
  9038. str)
  9039. (defun org-link-unescape-compound (hex)
  9040. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9041. Note: this function also decodes single byte encodings like
  9042. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9043. (save-match-data
  9044. (let* ((bytes (cdr (split-string hex "%")))
  9045. (ret "")
  9046. (eat 0)
  9047. (sum 0))
  9048. (while bytes
  9049. (let* ((val (string-to-number (pop bytes) 16))
  9050. (shift-xor
  9051. (if (= 0 eat)
  9052. (cond
  9053. ((>= val 252) (cons 6 252))
  9054. ((>= val 248) (cons 5 248))
  9055. ((>= val 240) (cons 4 240))
  9056. ((>= val 224) (cons 3 224))
  9057. ((>= val 192) (cons 2 192))
  9058. (t (cons 0 0)))
  9059. (cons 6 128))))
  9060. (if (>= val 192) (setq eat (car shift-xor)))
  9061. (setq val (logxor val (cdr shift-xor)))
  9062. (setq sum (+ (lsh sum (car shift-xor)) val))
  9063. (if (> eat 0) (setq eat (- eat 1)))
  9064. (cond
  9065. ((= 0 eat) ;multi byte
  9066. (setq ret (concat ret (org-char-to-string sum)))
  9067. (setq sum 0))
  9068. ((not bytes) ; single byte(s)
  9069. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9070. ret)))
  9071. (defun org-link-unescape-single-byte-sequence (hex)
  9072. "Unhexify hex-encoded single byte character sequences."
  9073. (mapconcat (lambda (byte)
  9074. (char-to-string (string-to-number byte 16)))
  9075. (cdr (split-string hex "%")) ""))
  9076. (defun org-xor (a b)
  9077. "Exclusive or."
  9078. (if a (not b) b))
  9079. (defun org-fixup-message-id-for-http (s)
  9080. "Replace special characters in a message id, so it can be used in an http query."
  9081. (when (string-match "%" s)
  9082. (setq s (mapconcat (lambda (c)
  9083. (if (eq c ?%)
  9084. "%25"
  9085. (char-to-string c)))
  9086. s "")))
  9087. (while (string-match "<" s)
  9088. (setq s (replace-match "%3C" t t s)))
  9089. (while (string-match ">" s)
  9090. (setq s (replace-match "%3E" t t s)))
  9091. (while (string-match "@" s)
  9092. (setq s (replace-match "%40" t t s)))
  9093. s)
  9094. (defun org-link-prettify (link)
  9095. "Return a human-readable representation of LINK.
  9096. The car of LINK must be a raw link.
  9097. The cdr of LINK must be either a link description or nil."
  9098. (let ((desc (or (cadr link) "<no description>")))
  9099. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9100. "<" (car link) ">")))
  9101. ;;;###autoload
  9102. (defun org-insert-link-global ()
  9103. "Insert a link like Org-mode does.
  9104. This command can be called in any mode to insert a link in Org-mode syntax."
  9105. (interactive)
  9106. (org-load-modules-maybe)
  9107. (org-run-like-in-org-mode 'org-insert-link))
  9108. (defun org-insert-all-links (arg &optional pre post)
  9109. "Insert all links in `org-stored-links'.
  9110. When a universal prefix, do not delete the links from `org-stored-links'.
  9111. When `ARG' is a number, insert the last N link(s).
  9112. `PRE' and `POST' are optional arguments to define a string to
  9113. prepend or to append."
  9114. (interactive "P")
  9115. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9116. (links (copy-seq org-stored-links))
  9117. (pr (or pre "- "))
  9118. (po (or post "\n"))
  9119. (cnt 1) l)
  9120. (if (null org-stored-links)
  9121. (message "No link to insert")
  9122. (while (and (or (listp arg) (>= arg cnt))
  9123. (setq l (if (listp arg)
  9124. (pop links)
  9125. (pop org-stored-links))))
  9126. (setq cnt (1+ cnt))
  9127. (insert pr)
  9128. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9129. (insert po)))))
  9130. (defun org-insert-last-stored-link (arg)
  9131. "Insert the last link stored in `org-stored-links'."
  9132. (interactive "p")
  9133. (org-insert-all-links arg "" "\n"))
  9134. (defun org-link-fontify-links-to-this-file ()
  9135. "Fontify links to the current file in `org-stored-links'."
  9136. (let ((f (buffer-file-name)) a b)
  9137. (setq a (mapcar (lambda(l)
  9138. (let ((ll (car l)))
  9139. (when (and (string-match "^file:\\(.+\\)::" ll)
  9140. (equal f (expand-file-name (match-string 1 ll))))
  9141. ll)))
  9142. org-stored-links))
  9143. (when (featurep 'org-id)
  9144. (setq b (mapcar (lambda(l)
  9145. (let ((ll (car l)))
  9146. (when (and (string-match "^id:\\(.+\\)$" ll)
  9147. (equal f (expand-file-name
  9148. (or (org-id-find-id-file
  9149. (match-string 1 ll)) ""))))
  9150. ll)))
  9151. org-stored-links)))
  9152. (mapcar (lambda(l)
  9153. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9154. (delq nil (append a b)))))
  9155. (defvar org-link-links-in-this-file nil)
  9156. (defun org-insert-link (&optional complete-file link-location default-description)
  9157. "Insert a link. At the prompt, enter the link.
  9158. Completion can be used to insert any of the link protocol prefixes like
  9159. http or ftp in use.
  9160. The history can be used to select a link previously stored with
  9161. `org-store-link'. When the empty string is entered (i.e. if you just
  9162. press RET at the prompt), the link defaults to the most recently
  9163. stored link. As SPC triggers completion in the minibuffer, you need to
  9164. use M-SPC or C-q SPC to force the insertion of a space character.
  9165. You will also be prompted for a description, and if one is given, it will
  9166. be displayed in the buffer instead of the link.
  9167. If there is already a link at point, this command will allow you to edit link
  9168. and description parts.
  9169. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9170. be selected using completion. The path to the file will be relative to the
  9171. current directory if the file is in the current directory or a subdirectory.
  9172. Otherwise, the link will be the absolute path as completed in the minibuffer
  9173. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9174. option `org-link-file-path-type'.
  9175. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9176. the current directory or below.
  9177. With three \\[universal-argument] prefixes, negate the meaning of
  9178. `org-keep-stored-link-after-insertion'.
  9179. If `org-make-link-description-function' is non-nil, this function will be
  9180. called with the link target, and the result will be the default
  9181. link description.
  9182. If the LINK-LOCATION parameter is non-nil, this value will be
  9183. used as the link location instead of reading one interactively.
  9184. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9185. be used as the default description."
  9186. (interactive "P")
  9187. (let* ((wcf (current-window-configuration))
  9188. (origbuf (current-buffer))
  9189. (region (if (org-region-active-p)
  9190. (buffer-substring (region-beginning) (region-end))))
  9191. (remove (and region (list (region-beginning) (region-end))))
  9192. (desc region)
  9193. tmphist ; byte-compile incorrectly complains about this
  9194. (link link-location)
  9195. (abbrevs org-link-abbrev-alist-local)
  9196. entry file all-prefixes auto-desc)
  9197. (cond
  9198. (link-location) ; specified by arg, just use it.
  9199. ((org-in-regexp org-bracket-link-regexp 1)
  9200. ;; We do have a link at point, and we are going to edit it.
  9201. (setq remove (list (match-beginning 0) (match-end 0)))
  9202. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9203. (setq link (read-string "Link: "
  9204. (org-link-unescape
  9205. (org-match-string-no-properties 1)))))
  9206. ((or (org-in-regexp org-angle-link-re)
  9207. (org-in-regexp org-plain-link-re))
  9208. ;; Convert to bracket link
  9209. (setq remove (list (match-beginning 0) (match-end 0))
  9210. link (read-string "Link: "
  9211. (org-remove-angle-brackets (match-string 0)))))
  9212. ((member complete-file '((4) (16)))
  9213. ;; Completing read for file names.
  9214. (setq link (org-file-complete-link complete-file)))
  9215. (t
  9216. ;; Read link, with completion for stored links.
  9217. (org-link-fontify-links-to-this-file)
  9218. (org-switch-to-buffer-other-window "*Org Links*")
  9219. (with-current-buffer "*Org Links*"
  9220. (erase-buffer)
  9221. (insert "Insert a link.
  9222. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9223. (when org-stored-links
  9224. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9225. (insert (mapconcat 'org-link-prettify
  9226. (reverse org-stored-links) "\n")))
  9227. (goto-char (point-min)))
  9228. (let ((cw (selected-window)))
  9229. (select-window (get-buffer-window "*Org Links*" 'visible))
  9230. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9231. (unless (pos-visible-in-window-p (point-max))
  9232. (org-fit-window-to-buffer))
  9233. (and (window-live-p cw) (select-window cw)))
  9234. ;; Fake a link history, containing the stored links.
  9235. (setq tmphist (append (mapcar 'car org-stored-links)
  9236. org-insert-link-history))
  9237. (setq all-prefixes (append (mapcar 'car abbrevs)
  9238. (mapcar 'car org-link-abbrev-alist)
  9239. org-link-types))
  9240. (unwind-protect
  9241. (progn
  9242. (setq link
  9243. (org-completing-read
  9244. "Link: "
  9245. (append
  9246. (mapcar (lambda (x) (concat x ":"))
  9247. all-prefixes)
  9248. (mapcar 'car org-stored-links))
  9249. nil nil nil
  9250. 'tmphist
  9251. (caar org-stored-links)))
  9252. (if (not (string-match "\\S-" link))
  9253. (user-error "No link selected"))
  9254. (mapc (lambda(l)
  9255. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9256. org-stored-links)
  9257. (if (or (member link all-prefixes)
  9258. (and (equal ":" (substring link -1))
  9259. (member (substring link 0 -1) all-prefixes)
  9260. (setq link (substring link 0 -1))))
  9261. (setq link (with-current-buffer origbuf
  9262. (org-link-try-special-completion link)))))
  9263. (set-window-configuration wcf)
  9264. (kill-buffer "*Org Links*"))
  9265. (setq entry (assoc link org-stored-links))
  9266. (or entry (push link org-insert-link-history))
  9267. (setq desc (or desc (nth 1 entry)))))
  9268. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9269. (not org-keep-stored-link-after-insertion))
  9270. (setq org-stored-links (delq (assoc link org-stored-links)
  9271. org-stored-links)))
  9272. (if (and (string-match org-plain-link-re link)
  9273. (not (string-match org-ts-regexp link)))
  9274. ;; URL-like link, normalize the use of angular brackets.
  9275. (setq link (org-remove-angle-brackets link)))
  9276. ;; Check if we are linking to the current file with a search
  9277. ;; option If yes, simplify the link by using only the search
  9278. ;; option.
  9279. (when (and buffer-file-name
  9280. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9281. (let* ((path (match-string 1 link))
  9282. (case-fold-search nil)
  9283. (search (match-string 2 link)))
  9284. (save-match-data
  9285. (if (equal (file-truename buffer-file-name) (file-truename path))
  9286. ;; We are linking to this same file, with a search option
  9287. (setq link search)))))
  9288. ;; Check if we can/should use a relative path. If yes, simplify the link
  9289. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9290. (let* ((type (match-string 1 link))
  9291. (path (match-string 2 link))
  9292. (origpath path)
  9293. (case-fold-search nil))
  9294. (cond
  9295. ((or (eq org-link-file-path-type 'absolute)
  9296. (equal complete-file '(16)))
  9297. (setq path (abbreviate-file-name (expand-file-name path))))
  9298. ((eq org-link-file-path-type 'noabbrev)
  9299. (setq path (expand-file-name path)))
  9300. ((eq org-link-file-path-type 'relative)
  9301. (setq path (file-relative-name path)))
  9302. (t
  9303. (save-match-data
  9304. (if (string-match (concat "^" (regexp-quote
  9305. (expand-file-name
  9306. (file-name-as-directory
  9307. default-directory))))
  9308. (expand-file-name path))
  9309. ;; We are linking a file with relative path name.
  9310. (setq path (substring (expand-file-name path)
  9311. (match-end 0)))
  9312. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9313. (setq link (concat type path))
  9314. (if (equal desc origpath)
  9315. (setq desc path))))
  9316. (if org-make-link-description-function
  9317. (setq desc
  9318. (or (condition-case nil
  9319. (funcall org-make-link-description-function link desc)
  9320. (error (progn (message "Can't get link description from `%s'"
  9321. (symbol-name org-make-link-description-function))
  9322. (sit-for 2) nil)))
  9323. (read-string "Description: " default-description)))
  9324. (if default-description (setq desc default-description)
  9325. (setq desc (or (and auto-desc desc)
  9326. (read-string "Description: " desc)))))
  9327. (unless (string-match "\\S-" desc) (setq desc nil))
  9328. (if remove (apply 'delete-region remove))
  9329. (insert (org-make-link-string link desc))))
  9330. (defun org-link-try-special-completion (type)
  9331. "If there is completion support for link type TYPE, offer it."
  9332. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9333. (if (functionp fun)
  9334. (funcall fun)
  9335. (read-string "Link (no completion support): " (concat type ":")))))
  9336. (defun org-file-complete-link (&optional arg)
  9337. "Create a file link using completion."
  9338. (let (file link)
  9339. (setq file (org-iread-file-name "File: "))
  9340. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9341. (pwd1 (file-name-as-directory (abbreviate-file-name
  9342. (expand-file-name ".")))))
  9343. (cond
  9344. ((equal arg '(16))
  9345. (setq link (concat
  9346. "file:"
  9347. (abbreviate-file-name (expand-file-name file)))))
  9348. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9349. (setq link (concat "file:" (match-string 1 file))))
  9350. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9351. (expand-file-name file))
  9352. (setq link (concat
  9353. "file:" (match-string 1 (expand-file-name file)))))
  9354. (t (setq link (concat "file:" file)))))
  9355. link))
  9356. (defun org-iread-file-name (&rest args)
  9357. "Read-file-name using `ido-mode' speedup if available.
  9358. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9359. See `read-file-name' for a description of parameters."
  9360. (org-without-partial-completion
  9361. (if (and org-completion-use-ido
  9362. (fboundp 'ido-read-file-name)
  9363. (boundp 'ido-mode) ido-mode
  9364. (listp (second args)))
  9365. (let ((ido-enter-matching-directory nil))
  9366. (apply 'ido-read-file-name args))
  9367. (apply 'read-file-name args))))
  9368. (defun org-completing-read (&rest args)
  9369. "Completing-read with SPACE being a normal character."
  9370. (let ((enable-recursive-minibuffers t)
  9371. (minibuffer-local-completion-map
  9372. (copy-keymap minibuffer-local-completion-map)))
  9373. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9374. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9375. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9376. (apply 'org-icompleting-read args)))
  9377. (defun org-completing-read-no-i (&rest args)
  9378. (let (org-completion-use-ido org-completion-use-iswitchb)
  9379. (apply 'org-completing-read args)))
  9380. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9381. "Use iswitch as a completing-read replacement to choose from choices.
  9382. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9383. from."
  9384. (let* ((iswitchb-use-virtual-buffers nil)
  9385. (iswitchb-make-buflist-hook
  9386. (lambda ()
  9387. (setq iswitchb-temp-buflist choices))))
  9388. (iswitchb-read-buffer prompt)))
  9389. (defun org-icompleting-read (&rest args)
  9390. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9391. (org-without-partial-completion
  9392. (if (and org-completion-use-ido
  9393. (fboundp 'ido-completing-read)
  9394. (boundp 'ido-mode) ido-mode
  9395. (listp (second args)))
  9396. (let ((ido-enter-matching-directory nil))
  9397. (apply 'ido-completing-read (concat (car args))
  9398. (if (consp (car (nth 1 args)))
  9399. (mapcar 'car (nth 1 args))
  9400. (nth 1 args))
  9401. (cddr args)))
  9402. (if (and org-completion-use-iswitchb
  9403. (boundp 'iswitchb-mode) iswitchb-mode
  9404. (listp (second args)))
  9405. (apply 'org-iswitchb-completing-read (concat (car args))
  9406. (if (consp (car (nth 1 args)))
  9407. (mapcar 'car (nth 1 args))
  9408. (nth 1 args))
  9409. (cddr args))
  9410. (apply 'completing-read args)))))
  9411. (defun org-extract-attributes (s)
  9412. "Extract the attributes cookie from a string and set as text property."
  9413. (let (a attr (start 0) key value)
  9414. (save-match-data
  9415. (when (string-match "{{\\([^}]+\\)}}$" s)
  9416. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9417. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9418. (setq key (match-string 1 a) value (match-string 2 a)
  9419. start (match-end 0)
  9420. attr (plist-put attr (intern key) value))))
  9421. (org-add-props s nil 'org-attr attr))
  9422. s))
  9423. ;;; Opening/following a link
  9424. (defvar org-link-search-failed nil)
  9425. (defvar org-open-link-functions nil
  9426. "Hook for functions finding a plain text link.
  9427. These functions must take a single argument, the link content.
  9428. They will be called for links that look like [[link text][description]]
  9429. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9430. like a filename (e.g. \"./blue.png\").
  9431. These functions will be called *before* Org attempts to resolve the
  9432. link by doing text searches in the current buffer - so if you want a
  9433. link \"[[target]]\" to still find \"<<target>>\", your function should
  9434. handle this as a special case.
  9435. When the function does handle the link, it must return a non-nil value.
  9436. If it decides that it is not responsible for this link, it must return
  9437. nil to indicate that that Org-mode can continue with other options
  9438. like exact and fuzzy text search.")
  9439. (defun org-next-link (&optional search-backward)
  9440. "Move forward to the next link.
  9441. If the link is in hidden text, expose it."
  9442. (interactive "P")
  9443. (when (and org-link-search-failed (eq this-command last-command))
  9444. (goto-char (point-min))
  9445. (message "Link search wrapped back to beginning of buffer"))
  9446. (setq org-link-search-failed nil)
  9447. (let* ((pos (point))
  9448. (ct (org-context))
  9449. (a (assoc :link ct))
  9450. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9451. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9452. ((looking-at org-any-link-re)
  9453. ;; Don't stay stuck at link without an org-link face
  9454. (forward-char (if search-backward -1 1))))
  9455. (if (funcall srch-fun org-any-link-re nil t)
  9456. (progn
  9457. (goto-char (match-beginning 0))
  9458. (if (outline-invisible-p) (org-show-context)))
  9459. (goto-char pos)
  9460. (setq org-link-search-failed t)
  9461. (message "No further link found"))))
  9462. (defun org-previous-link ()
  9463. "Move backward to the previous link.
  9464. If the link is in hidden text, expose it."
  9465. (interactive)
  9466. (funcall 'org-next-link t))
  9467. (defun org-translate-link (s)
  9468. "Translate a link string if a translation function has been defined."
  9469. (if (and org-link-translation-function
  9470. (fboundp org-link-translation-function)
  9471. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9472. (progn
  9473. (setq s (funcall org-link-translation-function
  9474. (match-string 1 s) (match-string 2 s)))
  9475. (concat (car s) ":" (cdr s)))
  9476. s))
  9477. (defun org-translate-link-from-planner (type path)
  9478. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9479. This is still an experimental function, your mileage may vary."
  9480. (cond
  9481. ((member type '("http" "https" "news" "ftp"))
  9482. ;; standard Internet links are the same.
  9483. nil)
  9484. ((and (equal type "irc") (string-match "^//" path))
  9485. ;; Planner has two / at the beginning of an irc link, we have 1.
  9486. ;; We should have zero, actually....
  9487. (setq path (substring path 1)))
  9488. ((and (equal type "lisp") (string-match "^/" path))
  9489. ;; Planner has a slash, we do not.
  9490. (setq type "elisp" path (substring path 1)))
  9491. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9492. ;; A typical message link. Planner has the id after the final slash,
  9493. ;; we separate it with a hash mark
  9494. (setq path (concat (match-string 1 path) "#"
  9495. (org-remove-angle-brackets (match-string 2 path))))))
  9496. (cons type path))
  9497. (defun org-find-file-at-mouse (ev)
  9498. "Open file link or URL at mouse."
  9499. (interactive "e")
  9500. (mouse-set-point ev)
  9501. (org-open-at-point 'in-emacs))
  9502. (defun org-open-at-mouse (ev)
  9503. "Open file link or URL at mouse.
  9504. See the docstring of `org-open-file' for details."
  9505. (interactive "e")
  9506. (mouse-set-point ev)
  9507. (if (eq major-mode 'org-agenda-mode)
  9508. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9509. (org-open-at-point))
  9510. (defvar org-window-config-before-follow-link nil
  9511. "The window configuration before following a link.
  9512. This is saved in case the need arises to restore it.")
  9513. (defvar org-open-link-marker (make-marker)
  9514. "Marker pointing to the location where `org-open-at-point' was called.")
  9515. ;;;###autoload
  9516. (defun org-open-at-point-global ()
  9517. "Follow a link like Org-mode does.
  9518. This command can be called in any mode to follow a link that has
  9519. Org-mode syntax."
  9520. (interactive)
  9521. (org-run-like-in-org-mode 'org-open-at-point))
  9522. ;;;###autoload
  9523. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9524. "Open a link in the string S, as if it was in Org-mode."
  9525. (interactive "sLink: \nP")
  9526. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9527. (with-temp-buffer
  9528. (let ((org-inhibit-startup (not reference-buffer)))
  9529. (org-mode)
  9530. (insert s)
  9531. (goto-char (point-min))
  9532. (when reference-buffer
  9533. (setq org-link-abbrev-alist-local
  9534. (with-current-buffer reference-buffer
  9535. org-link-abbrev-alist-local)))
  9536. (org-open-at-point arg reference-buffer)))))
  9537. (defvar org-open-at-point-functions nil
  9538. "Hook that is run when following a link at point.
  9539. Functions in this hook must return t if they identify and follow
  9540. a link at point. If they don't find anything interesting at point,
  9541. they must return nil.")
  9542. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9543. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9544. (defun org-open-at-point (&optional arg reference-buffer)
  9545. "Open link, timestamp, footnote or tags at point.
  9546. When point is on a link, follow it. Normally, files will be
  9547. opened by an appropriate application. If the optional prefix
  9548. argument ARG is non-nil, Emacs will visit the file. With
  9549. a double prefix argument, try to open outside of Emacs, in the
  9550. application the system uses for this file type.
  9551. When point is on a timestamp, open the agenda at the day
  9552. specified.
  9553. When point is a footnote definition, move to the first reference
  9554. found. If it is on a reference, move to the associated
  9555. definition.
  9556. When point is on a headline, display a list of every link in the
  9557. entry, so it is possible to pick one, or all, of them. If point
  9558. is on a tag, call `org-tags-view' instead.
  9559. When optional argument REFERENCE-BUFFER is non-nil, it should
  9560. specify a buffer from where the link search should happen. This
  9561. is used internally by `org-open-link-from-string'.
  9562. On top of syntactically correct links, this function will open
  9563. the link at point in comments or comment blocks and the first
  9564. link in a property drawer line."
  9565. (interactive "P")
  9566. ;; On a code block, open block's results.
  9567. (unless (call-interactively 'org-babel-open-src-block-result)
  9568. (org-load-modules-maybe)
  9569. (move-marker org-open-link-marker (point))
  9570. (setq org-window-config-before-follow-link (current-window-configuration))
  9571. (org-remove-occur-highlights nil nil t)
  9572. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9573. (let* ((context (org-element-context)) type value)
  9574. ;; On an unsupported type, check if point is contained within
  9575. ;; a support one.
  9576. (while (and (not (memq (setq type (org-element-type context))
  9577. '(comment comment-block
  9578. headline inlinetask link
  9579. footnote-definition footnote-reference
  9580. node-property timestamp)))
  9581. (setq context (org-element-property :parent context))))
  9582. (setq value (org-element-property :value context))
  9583. (cond
  9584. ;; Blank lines at the beginning of buffer: bail out.
  9585. ((not context) (user-error "No link found"))
  9586. ;; Exception n°1: links in property drawers
  9587. ((eq type 'node-property)
  9588. (org-open-link-from-string
  9589. (and (string-match org-any-link-re value)
  9590. (match-string-no-properties 0 value))))
  9591. ;; Exception n°2: links in comments.
  9592. ((memq type '(comment comment-block))
  9593. (save-excursion
  9594. (skip-chars-forward "\\S-" (point-at-eol))
  9595. (let ((string-rear (replace-regexp-in-string
  9596. "^[ \t]*# [ \t]*" ""
  9597. (buffer-substring (point) (line-beginning-position))))
  9598. (string-front (buffer-substring (point) (line-end-position))))
  9599. (with-temp-buffer
  9600. (let ((org-inhibit-startup t)) (org-mode))
  9601. (insert value)
  9602. (goto-char (point-min))
  9603. (when (and (search-forward string-rear nil t)
  9604. (search-forward string-front (line-end-position) t))
  9605. (goto-char (match-beginning 0))
  9606. (org-open-at-point)
  9607. (when (string= string-rear "") (forward-char)))))))
  9608. ;; On a headline or an inlinetask, but not on a timestamp,
  9609. ;; a link, a footnote reference or on tags.
  9610. ((and (memq type '(headline inlinetask))
  9611. ;; Not on tags.
  9612. (progn (save-excursion (beginning-of-line)
  9613. (looking-at org-complex-heading-regexp))
  9614. (or (not (match-beginning 5))
  9615. (< (point) (match-beginning 5)))))
  9616. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9617. (links (car data))
  9618. (links-end (cdr data)))
  9619. (if links
  9620. (dolist (link (if (stringp links) (list links) links))
  9621. (search-forward link nil links-end)
  9622. (goto-char (match-beginning 0))
  9623. (org-open-at-point))
  9624. (require 'org-attach)
  9625. (org-attach-reveal 'if-exists))))
  9626. ;; Do nothing on white spaces after an object, unless point
  9627. ;; is right after it.
  9628. ((> (point)
  9629. (save-excursion
  9630. (goto-char (org-element-property :end context))
  9631. (skip-chars-backward " \t")
  9632. (point)))
  9633. (user-error "No link found"))
  9634. ((eq type 'timestamp) (org-follow-timestamp-link))
  9635. ;; On tags within a headline or an inlinetask.
  9636. ((and (memq type '(headline inlinetask))
  9637. (progn (save-excursion (beginning-of-line)
  9638. (looking-at org-complex-heading-regexp))
  9639. (and (match-beginning 5)
  9640. (>= (point) (match-beginning 5)))))
  9641. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9642. ((eq type 'link)
  9643. (let ((type (org-element-property :type context))
  9644. (path (org-link-unescape (org-element-property :path context))))
  9645. ;; Switch back to REFERENCE-BUFFER needed when called in
  9646. ;; a temporary buffer through `org-open-link-from-string'.
  9647. (with-current-buffer (or reference-buffer (current-buffer))
  9648. (cond
  9649. ((equal type "file")
  9650. (if (string-match "[*?{]" (file-name-nondirectory path))
  9651. (dired path)
  9652. ;; Look into `org-link-protocols' in order to find
  9653. ;; a DEDICATED-FUNCTION to open file. The function
  9654. ;; will be applied on raw link instead of parsed
  9655. ;; link due to the limitation in `org-add-link-type'
  9656. ;; ("open" function called with a single argument).
  9657. ;; If no such function is found, fallback to
  9658. ;; `org-open-file'.
  9659. ;;
  9660. ;; Note : "file+emacs" and "file+sys" types are
  9661. ;; hard-coded in order to escape the previous
  9662. ;; limitation.
  9663. (let* ((option (org-element-property :search-option context))
  9664. (app (org-element-property :application context))
  9665. (dedicated-function
  9666. (nth 1 (assoc app org-link-protocols))))
  9667. (if dedicated-function
  9668. (funcall dedicated-function
  9669. (concat path
  9670. (and option (concat "::" option))))
  9671. (apply 'org-open-file
  9672. path
  9673. (cond (arg)
  9674. ((equal app "emacs") 'emacs)
  9675. ((equal app "sys") 'system))
  9676. (cond ((not option) nil)
  9677. ((org-string-match-p "\\`[0-9]+\\'" option)
  9678. (list (string-to-number option)))
  9679. (t (list nil
  9680. (org-link-unescape option)))))))))
  9681. ((assoc type org-link-protocols)
  9682. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9683. ((equal type "help")
  9684. (let ((f-or-v (intern path)))
  9685. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9686. ((boundp f-or-v) (describe-variable f-or-v))
  9687. (t (error "Not a known function or variable")))))
  9688. ((member type '("http" "https" "ftp" "mailto" "news"))
  9689. (browse-url (org-link-escape-browser (concat type ":" path))))
  9690. ((equal type "doi")
  9691. (browse-url
  9692. (org-link-escape-browser (concat org-doi-server-url path))))
  9693. ((equal type "message") (browse-url (concat type ":" path)))
  9694. ((equal type "shell")
  9695. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9696. (cmd path))
  9697. (if (or (and (org-string-nw-p
  9698. org-confirm-shell-link-not-regexp)
  9699. (string-match
  9700. org-confirm-shell-link-not-regexp cmd))
  9701. (not org-confirm-shell-link-function)
  9702. (funcall org-confirm-shell-link-function
  9703. (format "Execute \"%s\" in shell? "
  9704. (org-add-props cmd nil
  9705. 'face 'org-warning))))
  9706. (progn
  9707. (message "Executing %s" cmd)
  9708. (shell-command cmd buf)
  9709. (when (featurep 'midnight)
  9710. (setq clean-buffer-list-kill-buffer-names
  9711. (cons buf
  9712. clean-buffer-list-kill-buffer-names))))
  9713. (user-error "Abort"))))
  9714. ((equal type "elisp")
  9715. (let ((cmd path))
  9716. (if (or (and (org-string-nw-p
  9717. org-confirm-elisp-link-not-regexp)
  9718. (org-string-match-p
  9719. org-confirm-elisp-link-not-regexp cmd))
  9720. (not org-confirm-elisp-link-function)
  9721. (funcall org-confirm-elisp-link-function
  9722. (format "Execute \"%s\" as elisp? "
  9723. (org-add-props cmd nil
  9724. 'face 'org-warning))))
  9725. (message "%s => %s" cmd
  9726. (if (eq (string-to-char cmd) ?\()
  9727. (eval (read cmd))
  9728. (call-interactively (read cmd))))
  9729. (user-error "Abort"))))
  9730. ((equal type "id")
  9731. (require 'ord-id)
  9732. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9733. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9734. (unless (run-hook-with-args-until-success
  9735. 'org-open-link-functions path)
  9736. (if (not arg) (org-mark-ring-push)
  9737. (switch-to-buffer-other-window
  9738. (org-get-buffer-for-internal-link (current-buffer))))
  9739. (let ((cmd `(org-link-search
  9740. ,(if (member type '("custom-id" "coderef"))
  9741. (org-element-property :raw-link context)
  9742. path)
  9743. ,(cond ((equal arg '(4)) 'occur)
  9744. ((equal arg '(16)) 'org-occur))
  9745. ,(org-element-property :begin context))))
  9746. (condition-case nil
  9747. (let ((org-link-search-inhibit-query t))
  9748. (eval cmd))
  9749. (error (progn (widen) (eval cmd)))))))
  9750. (t (browse-url-at-point))))))
  9751. ;; On a footnote reference or at a footnote definition's label.
  9752. ((or (eq type 'footnote-reference)
  9753. (and (eq type 'footnote-definition)
  9754. (save-excursion
  9755. ;; Do not validate action when point is on the
  9756. ;; spaces right after the footnote label, in
  9757. ;; order to be on par with behaviour on links.
  9758. (skip-chars-forward " \t")
  9759. (let ((begin
  9760. (org-element-property :contents-begin context)))
  9761. (if begin (< (point) begin)
  9762. (= (org-element-property :post-affiliated context)
  9763. (line-beginning-position)))))))
  9764. (org-footnote-action))
  9765. (t (user-error "No link found")))))
  9766. (move-marker org-open-link-marker nil)
  9767. (run-hook-with-args 'org-follow-link-hook)))
  9768. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9769. "Offer links in the current entry and return the selected link.
  9770. If there is only one link, return it.
  9771. If NTH is an integer, return the NTH link found.
  9772. If ZERO is a string, check also this string for a link, and if
  9773. there is one, return it."
  9774. (with-current-buffer buffer
  9775. (save-excursion
  9776. (save-restriction
  9777. (widen)
  9778. (goto-char marker)
  9779. (let ((cnt ?0)
  9780. (in-emacs (if (integerp nth) nil nth))
  9781. have-zero end links link c)
  9782. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9783. (push (match-string 0 zero) links)
  9784. (setq cnt (1- cnt) have-zero t))
  9785. (save-excursion
  9786. (org-back-to-heading t)
  9787. (setq end (save-excursion (outline-next-heading) (point)))
  9788. (while (re-search-forward org-any-link-re end t)
  9789. (push (match-string 0) links))
  9790. (setq links (org-uniquify (reverse links))))
  9791. (cond
  9792. ((null links)
  9793. (message "No links"))
  9794. ((equal (length links) 1)
  9795. (setq link (car links)))
  9796. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9797. (setq link (nth (if have-zero nth (1- nth)) links)))
  9798. (t ; we have to select a link
  9799. (save-excursion
  9800. (save-window-excursion
  9801. (delete-other-windows)
  9802. (with-output-to-temp-buffer "*Select Link*"
  9803. (mapc (lambda (l)
  9804. (if (not (string-match org-bracket-link-regexp l))
  9805. (princ (format "[%c] %s\n" (incf cnt)
  9806. (org-remove-angle-brackets l)))
  9807. (if (match-end 3)
  9808. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9809. (match-string 3 l) (match-string 1 l)))
  9810. (princ (format "[%c] %s\n" (incf cnt)
  9811. (match-string 1 l))))))
  9812. links))
  9813. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9814. (message "Select link to open, RET to open all:")
  9815. (setq c (read-char-exclusive))
  9816. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9817. (when (equal c ?q) (user-error "Abort"))
  9818. (if (equal c ?\C-m)
  9819. (setq link links)
  9820. (setq nth (- c ?0))
  9821. (if have-zero (setq nth (1+ nth)))
  9822. (unless (and (integerp nth) (>= (length links) nth))
  9823. (user-error "Invalid link selection"))
  9824. (setq link (nth (1- nth) links)))))
  9825. (cons link end))))))
  9826. ;; TODO: These functions are deprecated since `org-open-at-point'
  9827. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9828. (defun org-open-file-with-system (path)
  9829. "Open file at PATH using the system way of opening it."
  9830. (org-open-file path 'system))
  9831. (defun org-open-file-with-emacs (path)
  9832. "Open file at PATH in Emacs."
  9833. (org-open-file path 'emacs))
  9834. ;;; File search
  9835. (defvar org-create-file-search-functions nil
  9836. "List of functions to construct the right search string for a file link.
  9837. These functions are called in turn with point at the location to
  9838. which the link should point.
  9839. A function in the hook should first test if it would like to
  9840. handle this file type, for example by checking the `major-mode'
  9841. or the file extension. If it decides not to handle this file, it
  9842. should just return nil to give other functions a chance. If it
  9843. does handle the file, it must return the search string to be used
  9844. when following the link. The search string will be part of the
  9845. file link, given after a double colon, and `org-open-at-point'
  9846. will automatically search for it. If special measures must be
  9847. taken to make the search successful, another function should be
  9848. added to the companion hook `org-execute-file-search-functions',
  9849. which see.
  9850. A function in this hook may also use `setq' to set the variable
  9851. `description' to provide a suggestion for the descriptive text to
  9852. be used for this link when it gets inserted into an Org-mode
  9853. buffer with \\[org-insert-link].")
  9854. (defvar org-execute-file-search-functions nil
  9855. "List of functions to execute a file search triggered by a link.
  9856. Functions added to this hook must accept a single argument, the
  9857. search string that was part of the file link, the part after the
  9858. double colon. The function must first check if it would like to
  9859. handle this search, for example by checking the `major-mode' or
  9860. the file extension. If it decides not to handle this search, it
  9861. should just return nil to give other functions a chance. If it
  9862. does handle the search, it must return a non-nil value to keep
  9863. other functions from trying.
  9864. Each function can access the current prefix argument through the
  9865. variable `current-prefix-arg'. Note that a single prefix is used
  9866. to force opening a link in Emacs, so it may be good to only use a
  9867. numeric or double prefix to guide the search function.
  9868. In case this is needed, a function in this hook can also restore
  9869. the window configuration before `org-open-at-point' was called using:
  9870. (set-window-configuration org-window-config-before-follow-link)")
  9871. (defun org-link-search (s &optional type avoid-pos stealth)
  9872. "Search for a link search option.
  9873. If S is surrounded by forward slashes, it is interpreted as a
  9874. regular expression. In org-mode files, this will create an `org-occur'
  9875. sparse tree. In ordinary files, `occur' will be used to list matches.
  9876. If the current buffer is in `dired-mode', grep will be used to search
  9877. in all files. If AVOID-POS is given, ignore matches near that position.
  9878. When optional argument STEALTH is non-nil, do not modify
  9879. visibility around point, thus ignoring
  9880. `org-show-hierarchy-above', `org-show-following-heading' and
  9881. `org-show-siblings' variables."
  9882. (let ((case-fold-search t)
  9883. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9884. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9885. (append '(("") (" ") ("\t") ("\n"))
  9886. org-emphasis-alist)
  9887. "\\|") "\\)"))
  9888. (pos (point))
  9889. (pre nil) (post nil)
  9890. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9891. (cond
  9892. ;; First check if there are any special search functions
  9893. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9894. ;; Now try the builtin stuff
  9895. ((and (equal (string-to-char s0) ?#)
  9896. (> (length s0) 1)
  9897. (save-excursion
  9898. (goto-char (point-min))
  9899. (and
  9900. (re-search-forward
  9901. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9902. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9903. (setq type 'dedicated
  9904. pos (match-beginning 0))))
  9905. ;; There is an exact target for this
  9906. (goto-char pos)
  9907. (org-back-to-heading t)))
  9908. ((save-excursion
  9909. (goto-char (point-min))
  9910. (and
  9911. (re-search-forward
  9912. (concat "<<" (regexp-quote s0) ">>") nil t)
  9913. (setq type 'dedicated
  9914. pos (match-beginning 0))))
  9915. ;; There is an exact target for this
  9916. (goto-char pos))
  9917. ((save-excursion
  9918. (goto-char (point-min))
  9919. (and
  9920. (re-search-forward
  9921. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9922. (setq type 'dedicated pos (match-beginning 0))))
  9923. ;; Found an element with a matching #+name affiliated keyword.
  9924. (goto-char pos))
  9925. ((and (string-match "^(\\(.*\\))$" s0)
  9926. (save-excursion
  9927. (goto-char (point-min))
  9928. (and
  9929. (re-search-forward
  9930. (concat "[^[]" (regexp-quote
  9931. (format org-coderef-label-format
  9932. (match-string 1 s0))))
  9933. nil t)
  9934. (setq type 'dedicated
  9935. pos (1+ (match-beginning 0))))))
  9936. ;; There is a coderef target for this
  9937. (goto-char pos))
  9938. ((string-match "^/\\(.*\\)/$" s)
  9939. ;; A regular expression
  9940. (cond
  9941. ((derived-mode-p 'org-mode)
  9942. (org-occur (match-string 1 s)))
  9943. (t (org-do-occur (match-string 1 s)))))
  9944. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9945. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9946. (goto-char (point-min))
  9947. (cond
  9948. ((let (case-fold-search)
  9949. (re-search-forward (format org-complex-heading-regexp-format
  9950. (regexp-quote s))
  9951. nil t))
  9952. ;; OK, found a match
  9953. (setq type 'dedicated)
  9954. (goto-char (match-beginning 0)))
  9955. ((and (not org-link-search-inhibit-query)
  9956. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9957. (y-or-n-p "No match - create this as a new heading? "))
  9958. (goto-char (point-max))
  9959. (or (bolp) (newline))
  9960. (insert "* " s "\n")
  9961. (beginning-of-line 0))
  9962. (t
  9963. (goto-char pos)
  9964. (error "No match"))))
  9965. (t
  9966. ;; A normal search string
  9967. (when (equal (string-to-char s) ?*)
  9968. ;; Anchor on headlines, post may include tags.
  9969. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9970. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9971. s (substring s 1)))
  9972. (remove-text-properties
  9973. 0 (length s)
  9974. '(face nil mouse-face nil keymap nil fontified nil) s)
  9975. ;; Make a series of regular expressions to find a match
  9976. (setq words (org-split-string s "[ \n\r\t]+")
  9977. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9978. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9979. "\\)" markers)
  9980. re2a_ (concat "\\(" (mapconcat 'downcase words
  9981. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9982. re2a (concat "[ \t\r\n]" re2a_)
  9983. re4_ (concat "\\(" (mapconcat 'downcase words
  9984. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9985. re4 (concat "[^a-zA-Z_]" re4_)
  9986. re1 (concat pre re2 post)
  9987. re3 (concat pre (if pre re4_ re4) post)
  9988. re5 (concat pre ".*" re4)
  9989. re2 (concat pre re2)
  9990. re2a (concat pre (if pre re2a_ re2a))
  9991. re4 (concat pre (if pre re4_ re4))
  9992. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9993. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9994. re5 "\\)"))
  9995. (cond
  9996. ((eq type 'org-occur) (org-occur reall))
  9997. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9998. (t (goto-char (point-min))
  9999. (setq type 'fuzzy)
  10000. (if (or (and (org-search-not-self 1 re0 nil t)
  10001. (setq type 'dedicated))
  10002. (org-search-not-self 1 re1 nil t)
  10003. (org-search-not-self 1 re2 nil t)
  10004. (org-search-not-self 1 re2a nil t)
  10005. (org-search-not-self 1 re3 nil t)
  10006. (org-search-not-self 1 re4 nil t)
  10007. (org-search-not-self 1 re5 nil t))
  10008. (goto-char (match-beginning 1))
  10009. (goto-char pos)
  10010. (error "No match"))))))
  10011. (and (derived-mode-p 'org-mode)
  10012. (not stealth)
  10013. (org-show-context 'link-search))
  10014. type))
  10015. (defun org-search-not-self (group &rest args)
  10016. "Execute `re-search-forward', but only accept matches that do not
  10017. enclose the position of `org-open-link-marker'."
  10018. (let ((m org-open-link-marker))
  10019. (catch 'exit
  10020. (while (apply 're-search-forward args)
  10021. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  10022. (goto-char (match-end group))
  10023. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  10024. (> (match-beginning 0) (marker-position m))
  10025. (< (match-end 0) (marker-position m)))
  10026. (save-match-data
  10027. (or (not (org-in-regexp
  10028. org-bracket-link-analytic-regexp 1))
  10029. (not (match-end 4)) ; no description
  10030. (and (<= (match-beginning 4) (point))
  10031. (>= (match-end 4) (point))))))
  10032. (throw 'exit (point))))))))
  10033. (defun org-get-buffer-for-internal-link (buffer)
  10034. "Return a buffer to be used for displaying the link target of internal links."
  10035. (cond
  10036. ((not org-display-internal-link-with-indirect-buffer)
  10037. buffer)
  10038. ((string-match "(Clone)$" (buffer-name buffer))
  10039. (message "Buffer is already a clone, not making another one")
  10040. ;; we also do not modify visibility in this case
  10041. buffer)
  10042. (t ; make a new indirect buffer for displaying the link
  10043. (let* ((bn (buffer-name buffer))
  10044. (ibn (concat bn "(Clone)"))
  10045. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10046. (with-current-buffer ib (org-overview))
  10047. ib))))
  10048. (defun org-do-occur (regexp &optional cleanup)
  10049. "Call the Emacs command `occur'.
  10050. If CLEANUP is non-nil, remove the printout of the regular expression
  10051. in the *Occur* buffer. This is useful if the regex is long and not useful
  10052. to read."
  10053. (occur regexp)
  10054. (when cleanup
  10055. (let ((cwin (selected-window)) win beg end)
  10056. (when (setq win (get-buffer-window "*Occur*"))
  10057. (select-window win))
  10058. (goto-char (point-min))
  10059. (when (re-search-forward "match[a-z]+" nil t)
  10060. (setq beg (match-end 0))
  10061. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10062. (setq end (1- (match-beginning 0)))))
  10063. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10064. (goto-char (point-min))
  10065. (select-window cwin))))
  10066. ;;; The mark ring for links jumps
  10067. (defvar org-mark-ring nil
  10068. "Mark ring for positions before jumps in Org-mode.")
  10069. (defvar org-mark-ring-last-goto nil
  10070. "Last position in the mark ring used to go back.")
  10071. ;; Fill and close the ring
  10072. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10073. (loop for i from 1 to org-mark-ring-length do
  10074. (push (make-marker) org-mark-ring))
  10075. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10076. org-mark-ring)
  10077. (defun org-mark-ring-push (&optional pos buffer)
  10078. "Put the current position or POS into the mark ring and rotate it."
  10079. (interactive)
  10080. (setq pos (or pos (point)))
  10081. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10082. (move-marker (car org-mark-ring)
  10083. (or pos (point))
  10084. (or buffer (current-buffer)))
  10085. (message "%s"
  10086. (substitute-command-keys
  10087. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10088. (defun org-mark-ring-goto (&optional n)
  10089. "Jump to the previous position in the mark ring.
  10090. With prefix arg N, jump back that many stored positions. When
  10091. called several times in succession, walk through the entire ring.
  10092. Org-mode commands jumping to a different position in the current file,
  10093. or to another Org-mode file, automatically push the old position
  10094. onto the ring."
  10095. (interactive "p")
  10096. (let (p m)
  10097. (if (eq last-command this-command)
  10098. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10099. (setq p org-mark-ring))
  10100. (setq org-mark-ring-last-goto p)
  10101. (setq m (car p))
  10102. (org-pop-to-buffer-same-window (marker-buffer m))
  10103. (goto-char m)
  10104. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10105. (defun org-remove-angle-brackets (s)
  10106. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10107. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10108. s)
  10109. (defun org-add-angle-brackets (s)
  10110. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10111. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10112. s)
  10113. (defun org-remove-double-quotes (s)
  10114. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10115. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10116. s)
  10117. ;;; Following specific links
  10118. (defun org-follow-timestamp-link ()
  10119. "Open an agenda view for the time-stamp date/range at point."
  10120. (cond
  10121. ((org-at-date-range-p t)
  10122. (let ((org-agenda-start-on-weekday)
  10123. (t1 (match-string 1))
  10124. (t2 (match-string 2)) tt1 tt2)
  10125. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10126. tt2 (time-to-days (org-time-string-to-time t2)))
  10127. (let ((org-agenda-buffer-tmp-name
  10128. (format "*Org Agenda(a:%s)"
  10129. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10130. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10131. ((org-at-timestamp-p t)
  10132. (let ((org-agenda-buffer-tmp-name
  10133. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10134. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10135. (substring (match-string 1) 0 10)))
  10136. 1)))
  10137. (t (error "This should not happen"))))
  10138. ;;; Following file links
  10139. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10140. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10141. (declare-function mailcap-mime-info
  10142. "mailcap" (string &optional request no-decode))
  10143. (defvar org-wait nil)
  10144. (defun org-open-file (path &optional in-emacs line search)
  10145. "Open the file at PATH.
  10146. First, this expands any special file name abbreviations. Then the
  10147. configuration variable `org-file-apps' is checked if it contains an
  10148. entry for this file type, and if yes, the corresponding command is launched.
  10149. If no application is found, Emacs simply visits the file.
  10150. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10151. With a double \\[universal-argument] \\[universal-argument] \
  10152. prefix arg, Org tries to avoid opening in Emacs
  10153. and to use an external application to visit the file.
  10154. Optional LINE specifies a line to go to, optional SEARCH a string
  10155. to search for. If LINE or SEARCH is given, the file will be
  10156. opened in Emacs, unless an entry from org-file-apps that makes
  10157. use of groups in a regexp matches.
  10158. If you want to change the way frames are used when following a
  10159. link, please customize `org-link-frame-setup'.
  10160. If the file does not exist, an error is thrown."
  10161. (let* ((file (if (equal path "")
  10162. buffer-file-name
  10163. (substitute-in-file-name (expand-file-name path))))
  10164. (file-apps (append org-file-apps (org-default-apps)))
  10165. (apps (org-remove-if
  10166. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10167. (apps-dlink (org-remove-if-not
  10168. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10169. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10170. (dirp (if remp nil (file-directory-p file)))
  10171. (file (if (and dirp org-open-directory-means-index-dot-org)
  10172. (concat (file-name-as-directory file) "index.org")
  10173. file))
  10174. (a-m-a-p (assq 'auto-mode apps))
  10175. (dfile (downcase file))
  10176. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10177. (link (cond ((and (eq line nil)
  10178. (eq search nil))
  10179. file)
  10180. (line
  10181. (concat file "::" (number-to-string line)))
  10182. (search
  10183. (concat file "::" search))))
  10184. (dlink (downcase link))
  10185. (old-buffer (current-buffer))
  10186. (old-pos (point))
  10187. (old-mode major-mode)
  10188. ext cmd link-match-data)
  10189. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10190. (setq ext (match-string 1 dfile))
  10191. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10192. (setq ext (match-string 1 dfile))))
  10193. (cond
  10194. ((member in-emacs '((16) system))
  10195. (setq cmd (cdr (assoc 'system apps))))
  10196. (in-emacs (setq cmd 'emacs))
  10197. (t
  10198. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10199. (and dirp (cdr (assoc 'directory apps)))
  10200. ; first, try matching against apps-dlink
  10201. ; if we get a match here, store the match data for later
  10202. (let ((match (assoc-default dlink apps-dlink
  10203. 'string-match)))
  10204. (if match
  10205. (progn (setq link-match-data (match-data))
  10206. match)
  10207. (progn (setq in-emacs (or in-emacs line search))
  10208. nil))) ; if we have no match in apps-dlink,
  10209. ; always open the file in emacs if line or search
  10210. ; is given (for backwards compatibility)
  10211. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10212. 'string-match)
  10213. (cdr (assoc ext apps))
  10214. (cdr (assoc t apps))))))
  10215. (when (eq cmd 'system)
  10216. (setq cmd (cdr (assoc 'system apps))))
  10217. (when (eq cmd 'default)
  10218. (setq cmd (cdr (assoc t apps))))
  10219. (when (eq cmd 'mailcap)
  10220. (require 'mailcap)
  10221. (mailcap-parse-mailcaps)
  10222. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10223. (command (mailcap-mime-info mime-type)))
  10224. (if (stringp command)
  10225. (setq cmd command)
  10226. (setq cmd 'emacs))))
  10227. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10228. (not (file-exists-p file))
  10229. (not org-open-non-existing-files))
  10230. (user-error "No such file: %s" file))
  10231. (cond
  10232. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10233. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10234. (while (string-match "['\"]%s['\"]" cmd)
  10235. (setq cmd (replace-match "%s" t t cmd)))
  10236. (while (string-match "%s" cmd)
  10237. (setq cmd (replace-match
  10238. (save-match-data
  10239. (shell-quote-argument
  10240. (convert-standard-filename file)))
  10241. t t cmd)))
  10242. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10243. (save-match-data
  10244. (let ((match-index 1)
  10245. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10246. (set-match-data link-match-data)
  10247. (while (<= match-index number-of-groups)
  10248. (let ((regex (concat "%" (number-to-string match-index)))
  10249. (replace-with (match-string match-index dlink)))
  10250. (while (string-match regex cmd)
  10251. (setq cmd (replace-match replace-with t t cmd))))
  10252. (setq match-index (+ match-index 1)))))
  10253. (save-window-excursion
  10254. (message "Running %s...done" cmd)
  10255. (start-process-shell-command cmd nil cmd)
  10256. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10257. ((or (stringp cmd)
  10258. (eq cmd 'emacs))
  10259. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10260. (widen)
  10261. (if line (progn (org-goto-line line)
  10262. (if (derived-mode-p 'org-mode)
  10263. (org-reveal)))
  10264. (if search (org-link-search search))))
  10265. ((consp cmd)
  10266. (let ((file (convert-standard-filename file)))
  10267. (save-match-data
  10268. (set-match-data link-match-data)
  10269. (eval cmd))))
  10270. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10271. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10272. (or (not (equal old-buffer (current-buffer)))
  10273. (not (equal old-pos (point))))
  10274. (org-mark-ring-push old-pos old-buffer))))
  10275. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10276. "This function returns non-nil if `entry' uses a regular
  10277. expression which should be matched against the whole link by
  10278. org-open-file.
  10279. It assumes that is the case when the entry uses a regular
  10280. expression which has at least one grouping construct and the
  10281. action is either a lisp form or a command string containing
  10282. '%1', i.e. using at least one subexpression match as a
  10283. parameter."
  10284. (let ((selector (car entry))
  10285. (action (cdr entry)))
  10286. (if (stringp selector)
  10287. (and (> (regexp-opt-depth selector) 0)
  10288. (or (and (stringp action)
  10289. (string-match "%[0-9]" action))
  10290. (consp action)))
  10291. nil)))
  10292. (defun org-default-apps ()
  10293. "Return the default applications for this operating system."
  10294. (cond
  10295. ((eq system-type 'darwin)
  10296. org-file-apps-defaults-macosx)
  10297. ((eq system-type 'windows-nt)
  10298. org-file-apps-defaults-windowsnt)
  10299. (t org-file-apps-defaults-gnu)))
  10300. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10301. "Convert extensions to regular expressions in the cars of LIST.
  10302. Also, weed out any non-string entries, because the return value is used
  10303. only for regexp matching.
  10304. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10305. point to the symbol `emacs', indicating that the file should
  10306. be opened in Emacs."
  10307. (append
  10308. (delq nil
  10309. (mapcar (lambda (x)
  10310. (if (not (stringp (car x)))
  10311. nil
  10312. (if (string-match "\\W" (car x))
  10313. x
  10314. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10315. list))
  10316. (if add-auto-mode
  10317. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10318. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10319. (defun org-file-remote-p (file)
  10320. "Test whether FILE specifies a location on a remote system.
  10321. Return non-nil if the location is indeed remote.
  10322. For example, the filename \"/user@host:/foo\" specifies a location
  10323. on the system \"/user@host:\"."
  10324. (cond ((fboundp 'file-remote-p)
  10325. (file-remote-p file))
  10326. ((fboundp 'tramp-handle-file-remote-p)
  10327. (tramp-handle-file-remote-p file))
  10328. ((and (boundp 'ange-ftp-name-format)
  10329. (string-match (car ange-ftp-name-format) file))
  10330. t)))
  10331. ;;;; Refiling
  10332. (defun org-get-org-file ()
  10333. "Read a filename, with default directory `org-directory'."
  10334. (let ((default (or org-default-notes-file remember-data-file)))
  10335. (read-file-name (format "File name [%s]: " default)
  10336. (file-name-as-directory org-directory)
  10337. default)))
  10338. (defun org-notes-order-reversed-p ()
  10339. "Check if the current file should receive notes in reversed order."
  10340. (cond
  10341. ((not org-reverse-note-order) nil)
  10342. ((eq t org-reverse-note-order) t)
  10343. ((not (listp org-reverse-note-order)) nil)
  10344. (t (catch 'exit
  10345. (let ((all org-reverse-note-order)
  10346. entry)
  10347. (while (setq entry (pop all))
  10348. (if (string-match (car entry) buffer-file-name)
  10349. (throw 'exit (cdr entry))))
  10350. nil)))))
  10351. (defvar org-refile-target-table nil
  10352. "The list of refile targets, created by `org-refile'.")
  10353. (defvar org-agenda-new-buffers nil
  10354. "Buffers created to visit agenda files.")
  10355. (defvar org-refile-cache nil
  10356. "Cache for refile targets.")
  10357. (defvar org-refile-markers nil
  10358. "All the markers used for caching refile locations.")
  10359. (defun org-refile-marker (pos)
  10360. "Get a new refile marker, but only if caching is in use."
  10361. (if (not org-refile-use-cache)
  10362. pos
  10363. (let ((m (make-marker)))
  10364. (move-marker m pos)
  10365. (push m org-refile-markers)
  10366. m)))
  10367. (defun org-refile-cache-clear ()
  10368. "Clear the refile cache and disable all the markers."
  10369. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10370. (setq org-refile-markers nil)
  10371. (setq org-refile-cache nil)
  10372. (message "Refile cache has been cleared"))
  10373. (defun org-refile-cache-check-set (set)
  10374. "Check if all the markers in the cache still have live buffers."
  10375. (let (marker)
  10376. (catch 'exit
  10377. (while (and set (setq marker (nth 3 (pop set))))
  10378. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10379. (when (and marker (null (marker-buffer marker)))
  10380. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10381. (sit-for 3)
  10382. (throw 'exit nil)))
  10383. t)))
  10384. (defun org-refile-cache-put (set &rest identifiers)
  10385. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10386. (let* ((key (sha1 (prin1-to-string identifiers)))
  10387. (entry (assoc key org-refile-cache)))
  10388. (if entry
  10389. (setcdr entry set)
  10390. (push (cons key set) org-refile-cache))))
  10391. (defun org-refile-cache-get (&rest identifiers)
  10392. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10393. (cond
  10394. ((not org-refile-cache) nil)
  10395. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10396. (t
  10397. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10398. org-refile-cache))))
  10399. (and set (org-refile-cache-check-set set) set)))))
  10400. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10401. "Produce a table with refile targets."
  10402. (let ((case-fold-search nil)
  10403. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10404. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10405. targets tgs txt re files f desc descre fast-path-p level pos0)
  10406. (message "Getting targets...")
  10407. (with-current-buffer (or default-buffer (current-buffer))
  10408. (while (setq entry (pop entries))
  10409. (setq files (car entry) desc (cdr entry))
  10410. (setq fast-path-p nil)
  10411. (cond
  10412. ((null files) (setq files (list (current-buffer))))
  10413. ((eq files 'org-agenda-files)
  10414. (setq files (org-agenda-files 'unrestricted)))
  10415. ((and (symbolp files) (fboundp files))
  10416. (setq files (funcall files)))
  10417. ((and (symbolp files) (boundp files))
  10418. (setq files (symbol-value files))))
  10419. (if (stringp files) (setq files (list files)))
  10420. (cond
  10421. ((eq (car desc) :tag)
  10422. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10423. ((eq (car desc) :todo)
  10424. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10425. ((eq (car desc) :regexp)
  10426. (setq descre (cdr desc)))
  10427. ((eq (car desc) :level)
  10428. (setq descre (concat "^\\*\\{" (number-to-string
  10429. (if org-odd-levels-only
  10430. (1- (* 2 (cdr desc)))
  10431. (cdr desc)))
  10432. "\\}[ \t]")))
  10433. ((eq (car desc) :maxlevel)
  10434. (setq fast-path-p t)
  10435. (setq descre (concat "^\\*\\{1," (number-to-string
  10436. (if org-odd-levels-only
  10437. (1- (* 2 (cdr desc)))
  10438. (cdr desc)))
  10439. "\\}[ \t]")))
  10440. (t (error "Bad refiling target description %s" desc)))
  10441. (while (setq f (pop files))
  10442. (with-current-buffer
  10443. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10444. (or
  10445. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10446. (progn
  10447. (if (bufferp f) (setq f (buffer-file-name
  10448. (buffer-base-buffer f))))
  10449. (setq f (and f (expand-file-name f)))
  10450. (if (eq org-refile-use-outline-path 'file)
  10451. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10452. (save-excursion
  10453. (save-restriction
  10454. (widen)
  10455. (goto-char (point-min))
  10456. (while (re-search-forward descre nil t)
  10457. (goto-char (setq pos0 (point-at-bol)))
  10458. (catch 'next
  10459. (when org-refile-target-verify-function
  10460. (save-match-data
  10461. (or (funcall org-refile-target-verify-function)
  10462. (throw 'next t))))
  10463. (when (and (looking-at org-complex-heading-regexp)
  10464. (not (member (match-string 4) excluded-entries))
  10465. (match-string 4))
  10466. (setq level (org-reduced-level
  10467. (- (match-end 1) (match-beginning 1)))
  10468. txt (org-link-display-format (match-string 4))
  10469. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10470. re (format org-complex-heading-regexp-format
  10471. (regexp-quote (match-string 4))))
  10472. (when org-refile-use-outline-path
  10473. (setq txt (mapconcat
  10474. 'org-protect-slash
  10475. (append
  10476. (if (eq org-refile-use-outline-path
  10477. 'file)
  10478. (list (file-name-nondirectory
  10479. (buffer-file-name
  10480. (buffer-base-buffer))))
  10481. (if (eq org-refile-use-outline-path
  10482. 'full-file-path)
  10483. (list (buffer-file-name
  10484. (buffer-base-buffer)))))
  10485. (org-get-outline-path fast-path-p
  10486. level txt)
  10487. (list txt))
  10488. "/")))
  10489. (push (list txt f re (org-refile-marker (point)))
  10490. tgs)))
  10491. (when (= (point) pos0)
  10492. ;; verification function has not moved point
  10493. (goto-char (point-at-eol))))))))
  10494. (when org-refile-use-cache
  10495. (org-refile-cache-put tgs (buffer-file-name) descre))
  10496. (setq targets (append tgs targets))))))
  10497. (message "Getting targets...done")
  10498. (nreverse targets)))
  10499. (defun org-protect-slash (s)
  10500. (while (string-match "/" s)
  10501. (setq s (replace-match "\\" t t s)))
  10502. s)
  10503. (defvar org-olpa (make-vector 20 nil))
  10504. (defun org-get-outline-path (&optional fastp level heading)
  10505. "Return the outline path to the current entry, as a list.
  10506. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10507. routine which makes outline path derivations for an entire file,
  10508. avoiding backtracing. Refile target collection makes use of that."
  10509. (if fastp
  10510. (progn
  10511. (if (> level 19)
  10512. (error "Outline path failure, more than 19 levels"))
  10513. (loop for i from level upto 19 do
  10514. (aset org-olpa i nil))
  10515. (prog1
  10516. (delq nil (append org-olpa nil))
  10517. (aset org-olpa level heading)))
  10518. (let (rtn case-fold-search)
  10519. (save-excursion
  10520. (save-restriction
  10521. (widen)
  10522. (while (org-up-heading-safe)
  10523. (when (looking-at org-complex-heading-regexp)
  10524. (push (org-trim
  10525. (replace-regexp-in-string
  10526. ;; Remove statistical/checkboxes cookies
  10527. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10528. (org-match-string-no-properties 4)))
  10529. rtn)))
  10530. rtn)))))
  10531. (defun org-format-outline-path (path &optional width prefix separator)
  10532. "Format the outline path PATH for display.
  10533. WIDTH is the maximum number of characters that is available.
  10534. PREFIX is a prefix to be included in the returned string,
  10535. such as the file name.
  10536. SEPARATOR is inserted between the different parts of the path,
  10537. the default is \"/\"."
  10538. (setq width (or width 79))
  10539. (if prefix (setq width (- width (length prefix))))
  10540. (if (not path)
  10541. (or prefix "")
  10542. (let* ((nsteps (length path))
  10543. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10544. (maxwidth (if (<= total-width width)
  10545. 10000 ;; everything fits
  10546. ;; we need to shorten the level headings
  10547. (/ (- width nsteps) nsteps)))
  10548. (org-odd-levels-only nil)
  10549. (n 0)
  10550. (total (1+ (length prefix))))
  10551. (setq maxwidth (max maxwidth 10))
  10552. (concat prefix
  10553. (if prefix (or separator "/"))
  10554. (mapconcat
  10555. (lambda (h)
  10556. (setq n (1+ n))
  10557. (if (and (= n nsteps) (< maxwidth 10000))
  10558. (setq maxwidth (- total-width total)))
  10559. (if (< (length h) maxwidth)
  10560. (progn (setq total (+ total (length h) 1)) h)
  10561. (setq h (substring h 0 (- maxwidth 2))
  10562. total (+ total maxwidth 1))
  10563. (if (string-match "[ \t]+\\'" h)
  10564. (setq h (substring h 0 (match-beginning 0))))
  10565. (setq h (concat h "..")))
  10566. (org-add-props h nil 'face
  10567. (nth (% (1- n) org-n-level-faces)
  10568. org-level-faces))
  10569. h)
  10570. path (or separator "/"))))))
  10571. (defun org-display-outline-path (&optional file current separator just-return-string)
  10572. "Display the current outline path in the echo area.
  10573. If FILE is non-nil, prepend the output with the file name.
  10574. If CURRENT is non-nil, append the current heading to the output.
  10575. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10576. the different parts of the path and defaults to \"/\".
  10577. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10578. (interactive "P")
  10579. (let* (case-fold-search
  10580. (bfn (buffer-file-name (buffer-base-buffer)))
  10581. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10582. res)
  10583. (if current (setq path (append path
  10584. (save-excursion
  10585. (org-back-to-heading t)
  10586. (if (looking-at org-complex-heading-regexp)
  10587. (list (match-string 4)))))))
  10588. (setq res
  10589. (org-format-outline-path
  10590. path
  10591. (1- (frame-width))
  10592. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10593. separator))
  10594. (if just-return-string
  10595. (org-no-properties res)
  10596. (org-unlogged-message "%s" res))))
  10597. (defvar org-refile-history nil
  10598. "History for refiling operations.")
  10599. (defvar org-after-refile-insert-hook nil
  10600. "Hook run after `org-refile' has inserted its stuff at the new location.
  10601. Note that this is still *before* the stuff will be removed from
  10602. the *old* location.")
  10603. (defvar org-capture-last-stored-marker)
  10604. (defvar org-refile-keep nil
  10605. "Non-nil means `org-refile' will copy instead of refile.")
  10606. (defun org-copy ()
  10607. "Like `org-refile', but copy."
  10608. (interactive)
  10609. (let ((org-refile-keep t))
  10610. (funcall 'org-refile nil nil nil "Copy")))
  10611. (defun org-refile (&optional arg default-buffer rfloc msg)
  10612. "Move the entry or entries at point to another heading.
  10613. The list of target headings is compiled using the information in
  10614. `org-refile-targets', which see.
  10615. At the target location, the entry is filed as a subitem of the
  10616. target heading. Depending on `org-reverse-note-order', the new
  10617. subitem will either be the first or the last subitem.
  10618. If there is an active region, all entries in that region will be
  10619. refiled. However, the region must fulfill the requirement that
  10620. the first heading sets the top-level of the moved text.
  10621. With prefix arg ARG, the command will only visit the target
  10622. location and not actually move anything.
  10623. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10624. refiling operation has put the subtree.
  10625. With a numeric prefix argument of `2', refile to the running clock.
  10626. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10627. being set to `t' and copy to the target location, don't move it.
  10628. Beware that keeping refiled entries may result in duplicated ID
  10629. properties.
  10630. RFLOC can be a refile location obtained in a different way.
  10631. MSG is a string to replace \"Refile\" in the default prompt with
  10632. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10633. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10634. If you are using target caching (see `org-refile-use-cache'), you
  10635. have to clear the target cache in order to find new targets.
  10636. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10637. prefix argument (`C-u C-u C-u C-c C-w')."
  10638. (interactive "P")
  10639. (if (member arg '(0 (64)))
  10640. (org-refile-cache-clear)
  10641. (let* ((actionmsg (cond (msg msg)
  10642. ((equal arg 3) "Refile (and keep)")
  10643. (t "Refile")))
  10644. (cbuf (current-buffer))
  10645. (regionp (org-region-active-p))
  10646. (region-start (and regionp (region-beginning)))
  10647. (region-end (and regionp (region-end)))
  10648. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10649. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10650. pos it nbuf file re level reversed)
  10651. (setq last-command nil)
  10652. (when regionp
  10653. (goto-char region-start)
  10654. (or (bolp) (goto-char (point-at-bol)))
  10655. (setq region-start (point))
  10656. (unless (or (org-kill-is-subtree-p
  10657. (buffer-substring region-start region-end))
  10658. (prog1 org-refile-active-region-within-subtree
  10659. (let ((s (point-at-eol)))
  10660. (org-toggle-heading)
  10661. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10662. (user-error "The region is not a (sequence of) subtree(s)")))
  10663. (if (equal arg '(16))
  10664. (org-refile-goto-last-stored)
  10665. (when (or
  10666. (and (equal arg 2)
  10667. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10668. (prog1
  10669. (setq it (list (or org-clock-heading "running clock")
  10670. (buffer-file-name
  10671. (marker-buffer org-clock-hd-marker))
  10672. ""
  10673. (marker-position org-clock-hd-marker)))
  10674. (setq arg nil)))
  10675. (setq it (or rfloc
  10676. (let (heading-text)
  10677. (save-excursion
  10678. (unless (and arg (listp arg))
  10679. (org-back-to-heading t)
  10680. (setq heading-text
  10681. (replace-regexp-in-string
  10682. org-bracket-link-regexp
  10683. "\\3"
  10684. (nth 4 (org-heading-components)))))
  10685. (org-refile-get-location
  10686. (cond ((and arg (listp arg)) "Goto")
  10687. (regionp (concat actionmsg " region to"))
  10688. (t (concat actionmsg " subtree \""
  10689. heading-text "\" to")))
  10690. default-buffer
  10691. (and (not (equal '(4) arg))
  10692. org-refile-allow-creating-parent-nodes)
  10693. arg))))))
  10694. (setq file (nth 1 it)
  10695. re (nth 2 it)
  10696. pos (nth 3 it))
  10697. (if (and (not arg)
  10698. pos
  10699. (equal (buffer-file-name) file)
  10700. (if regionp
  10701. (and (>= pos region-start)
  10702. (<= pos region-end))
  10703. (and (>= pos (point))
  10704. (< pos (save-excursion
  10705. (org-end-of-subtree t t))))))
  10706. (error "Cannot refile to position inside the tree or region"))
  10707. (setq nbuf (or (find-buffer-visiting file)
  10708. (find-file-noselect file)))
  10709. (if (and arg (not (equal arg 3)))
  10710. (progn
  10711. (org-pop-to-buffer-same-window nbuf)
  10712. (goto-char pos)
  10713. (org-show-context 'org-goto))
  10714. (if regionp
  10715. (progn
  10716. (org-kill-new (buffer-substring region-start region-end))
  10717. (org-save-markers-in-region region-start region-end))
  10718. (org-copy-subtree 1 nil t))
  10719. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10720. (find-file-noselect file)))
  10721. (setq reversed (org-notes-order-reversed-p))
  10722. (save-excursion
  10723. (save-restriction
  10724. (widen)
  10725. (if pos
  10726. (progn
  10727. (goto-char pos)
  10728. (looking-at org-outline-regexp)
  10729. (setq level (org-get-valid-level (funcall outline-level) 1))
  10730. (goto-char
  10731. (if reversed
  10732. (or (outline-next-heading) (point-max))
  10733. (or (save-excursion (org-get-next-sibling))
  10734. (org-end-of-subtree t t)
  10735. (point-max)))))
  10736. (setq level 1)
  10737. (if (not reversed)
  10738. (goto-char (point-max))
  10739. (goto-char (point-min))
  10740. (or (outline-next-heading) (goto-char (point-max)))))
  10741. (if (not (bolp)) (newline))
  10742. (org-paste-subtree level nil nil t)
  10743. (when org-log-refile
  10744. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10745. (unless (eq org-log-refile 'note)
  10746. (save-excursion (org-add-log-note))))
  10747. (and org-auto-align-tags
  10748. (let ((org-loop-over-headlines-in-active-region nil))
  10749. (org-set-tags nil t)))
  10750. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10751. :last-refile)))
  10752. (when bookmark-name
  10753. (with-demoted-errors
  10754. (bookmark-set bookmark-name))))
  10755. ;; If we are refiling for capture, make sure that the
  10756. ;; last-capture pointers point here
  10757. (when (org-bound-and-true-p org-refile-for-capture)
  10758. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10759. :last-capture-marker)))
  10760. (when bookmark-name
  10761. (with-demoted-errors
  10762. (bookmark-set bookmark-name))))
  10763. (move-marker org-capture-last-stored-marker (point)))
  10764. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10765. (run-hooks 'org-after-refile-insert-hook))))
  10766. (unless org-refile-keep
  10767. (if regionp
  10768. (delete-region (point) (+ (point) (- region-end region-start)))
  10769. (delete-region
  10770. (and (org-back-to-heading t) (point))
  10771. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10772. (when (featurep 'org-inlinetask)
  10773. (org-inlinetask-remove-END-maybe))
  10774. (setq org-markers-to-move nil)
  10775. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10776. (defun org-refile-goto-last-stored ()
  10777. "Go to the location where the last refile was stored."
  10778. (interactive)
  10779. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10780. (message "This is the location of the last refile"))
  10781. (defun org-refile--get-location (refloc tbl)
  10782. "When user refile to REFLOC, find the associated target in TBL.
  10783. Also check `org-refile-target-table'."
  10784. (car (delq
  10785. nil
  10786. (mapcar
  10787. (lambda (r) (or (assoc r tbl)
  10788. (assoc r org-refile-target-table)))
  10789. (list (replace-regexp-in-string "/$" "" refloc)
  10790. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10791. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10792. no-exclude)
  10793. "Prompt the user for a refile location, using PROMPT.
  10794. PROMPT should not be suffixed with a colon and a space, because
  10795. this function appends the default value from
  10796. `org-refile-history' automatically, if that is not empty.
  10797. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10798. this is used for the GOTO interface."
  10799. (let ((org-refile-targets org-refile-targets)
  10800. (org-refile-use-outline-path org-refile-use-outline-path)
  10801. excluded-entries)
  10802. (when (and (derived-mode-p 'org-mode)
  10803. (not org-refile-use-cache)
  10804. (not no-exclude))
  10805. (org-map-tree
  10806. (lambda()
  10807. (setq excluded-entries
  10808. (append excluded-entries (list (org-get-heading t t)))))))
  10809. (setq org-refile-target-table
  10810. (org-refile-get-targets default-buffer excluded-entries)))
  10811. (unless org-refile-target-table
  10812. (user-error "No refile targets"))
  10813. (let* ((cbuf (current-buffer))
  10814. (partial-completion-mode nil)
  10815. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10816. (cfunc (if (and org-refile-use-outline-path
  10817. org-outline-path-complete-in-steps)
  10818. 'org-olpath-completing-read
  10819. 'org-icompleting-read))
  10820. (extra (if org-refile-use-outline-path "/" ""))
  10821. (cbnex (concat (buffer-name) extra))
  10822. (filename (and cfn (expand-file-name cfn)))
  10823. (tbl (mapcar
  10824. (lambda (x)
  10825. (if (and (not (member org-refile-use-outline-path
  10826. '(file full-file-path)))
  10827. (not (equal filename (nth 1 x))))
  10828. (cons (concat (car x) extra " ("
  10829. (file-name-nondirectory (nth 1 x)) ")")
  10830. (cdr x))
  10831. (cons (concat (car x) extra) (cdr x))))
  10832. org-refile-target-table))
  10833. (completion-ignore-case t)
  10834. cdef
  10835. (prompt (concat prompt
  10836. (or (and (car org-refile-history)
  10837. (concat " (default " (car org-refile-history) ")"))
  10838. (and (assoc cbnex tbl) (setq cdef cbnex)
  10839. (concat " (default " cbnex ")"))) ": "))
  10840. pa answ parent-target child parent old-hist)
  10841. (setq old-hist org-refile-history)
  10842. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10843. nil 'org-refile-history (or cdef (car org-refile-history))))
  10844. (if (setq pa (org-refile--get-location answ tbl))
  10845. (progn
  10846. (org-refile-check-position pa)
  10847. (when (or (not org-refile-history)
  10848. (not (eq old-hist org-refile-history))
  10849. (not (equal (car pa) (car org-refile-history))))
  10850. (setq org-refile-history
  10851. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10852. org-refile-history
  10853. (cdr org-refile-history))))
  10854. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10855. (pop org-refile-history)))
  10856. pa)
  10857. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10858. (progn
  10859. (setq parent (match-string 1 answ)
  10860. child (match-string 2 answ))
  10861. (setq parent-target (org-refile--get-location parent tbl))
  10862. (when (and parent-target
  10863. (or (eq new-nodes t)
  10864. (and (eq new-nodes 'confirm)
  10865. (y-or-n-p (format "Create new node \"%s\"? "
  10866. child)))))
  10867. (org-refile-new-child parent-target child)))
  10868. (user-error "Invalid target location")))))
  10869. (declare-function org-string-nw-p "org-macs" (s))
  10870. (defun org-refile-check-position (refile-pointer)
  10871. "Check if the refile pointer matches the headline to which it points."
  10872. (let* ((file (nth 1 refile-pointer))
  10873. (re (nth 2 refile-pointer))
  10874. (pos (nth 3 refile-pointer))
  10875. buffer)
  10876. (if (and (not (markerp pos)) (not file))
  10877. (user-error "Please indicate a target file in the refile path")
  10878. (when (org-string-nw-p re)
  10879. (setq buffer (if (markerp pos)
  10880. (marker-buffer pos)
  10881. (or (find-buffer-visiting file)
  10882. (find-file-noselect file))))
  10883. (with-current-buffer buffer
  10884. (save-excursion
  10885. (save-restriction
  10886. (widen)
  10887. (goto-char pos)
  10888. (beginning-of-line 1)
  10889. (unless (org-looking-at-p re)
  10890. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10891. (defun org-refile-new-child (parent-target child)
  10892. "Use refile target PARENT-TARGET to add new CHILD below it."
  10893. (unless parent-target
  10894. (error "Cannot find parent for new node"))
  10895. (let ((file (nth 1 parent-target))
  10896. (pos (nth 3 parent-target))
  10897. level)
  10898. (with-current-buffer (or (find-buffer-visiting file)
  10899. (find-file-noselect file))
  10900. (save-excursion
  10901. (save-restriction
  10902. (widen)
  10903. (if pos
  10904. (goto-char pos)
  10905. (goto-char (point-max))
  10906. (if (not (bolp)) (newline)))
  10907. (when (looking-at org-outline-regexp)
  10908. (setq level (funcall outline-level))
  10909. (org-end-of-subtree t t))
  10910. (org-back-over-empty-lines)
  10911. (insert "\n" (make-string
  10912. (if pos (org-get-valid-level level 1) 1) ?*)
  10913. " " child "\n")
  10914. (beginning-of-line 0)
  10915. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10916. (defun org-olpath-completing-read (prompt collection &rest args)
  10917. "Read an outline path like a file name."
  10918. (let ((thetable collection)
  10919. (org-completion-use-ido nil) ; does not work with ido.
  10920. (org-completion-use-iswitchb nil)) ; or iswitchb
  10921. (apply
  10922. 'org-icompleting-read prompt
  10923. (lambda (string predicate &optional flag)
  10924. (let (rtn r f (l (length string)))
  10925. (cond
  10926. ((eq flag nil)
  10927. ;; try completion
  10928. (try-completion string thetable))
  10929. ((eq flag t)
  10930. ;; all-completions
  10931. (setq rtn (all-completions string thetable predicate))
  10932. (mapcar
  10933. (lambda (x)
  10934. (setq r (substring x l))
  10935. (if (string-match " ([^)]*)$" x)
  10936. (setq f (match-string 0 x))
  10937. (setq f ""))
  10938. (if (string-match "/" r)
  10939. (concat string (substring r 0 (match-end 0)) f)
  10940. x))
  10941. rtn))
  10942. ((eq flag 'lambda)
  10943. ;; exact match?
  10944. (assoc string thetable)))))
  10945. args)))
  10946. ;;;; Dynamic blocks
  10947. (defun org-find-dblock (name)
  10948. "Find the first dynamic block with name NAME in the buffer.
  10949. If not found, stay at current position and return nil."
  10950. (let ((case-fold-search t) pos)
  10951. (save-excursion
  10952. (goto-char (point-min))
  10953. (setq pos (and (re-search-forward
  10954. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10955. (match-beginning 0))))
  10956. (if pos (goto-char pos))
  10957. pos))
  10958. (defun org-create-dblock (plist)
  10959. "Create a dynamic block section, with parameters taken from PLIST.
  10960. PLIST must contain a :name entry which is used as the name of the block."
  10961. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10962. (end-of-line 1)
  10963. (newline))
  10964. (let ((col (current-column))
  10965. (name (plist-get plist :name)))
  10966. (insert "#+BEGIN: " name)
  10967. (while plist
  10968. (if (eq (car plist) :name)
  10969. (setq plist (cddr plist))
  10970. (insert " " (prin1-to-string (pop plist)))))
  10971. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10972. (beginning-of-line -2)))
  10973. (defun org-prepare-dblock ()
  10974. "Prepare dynamic block for refresh.
  10975. This empties the block, puts the cursor at the insert position and returns
  10976. the property list including an extra property :name with the block name."
  10977. (unless (looking-at org-dblock-start-re)
  10978. (user-error "Not at a dynamic block"))
  10979. (let* ((begdel (1+ (match-end 0)))
  10980. (name (org-no-properties (match-string 1)))
  10981. (params (append (list :name name)
  10982. (read (concat "(" (match-string 3) ")")))))
  10983. (save-excursion
  10984. (beginning-of-line 1)
  10985. (skip-chars-forward " \t")
  10986. (setq params (plist-put params :indentation-column (current-column))))
  10987. (unless (re-search-forward org-dblock-end-re nil t)
  10988. (error "Dynamic block not terminated"))
  10989. (setq params
  10990. (append params
  10991. (list :content (buffer-substring
  10992. begdel (match-beginning 0)))))
  10993. (delete-region begdel (match-beginning 0))
  10994. (goto-char begdel)
  10995. (open-line 1)
  10996. params))
  10997. (defun org-map-dblocks (&optional command)
  10998. "Apply COMMAND to all dynamic blocks in the current buffer.
  10999. If COMMAND is not given, use `org-update-dblock'."
  11000. (let ((cmd (or command 'org-update-dblock)))
  11001. (save-excursion
  11002. (goto-char (point-min))
  11003. (while (re-search-forward org-dblock-start-re nil t)
  11004. (goto-char (match-beginning 0))
  11005. (save-excursion
  11006. (condition-case nil
  11007. (funcall cmd)
  11008. (error (message "Error during update of dynamic block"))))
  11009. (unless (re-search-forward org-dblock-end-re nil t)
  11010. (error "Dynamic block not terminated"))))))
  11011. (defun org-dblock-update (&optional arg)
  11012. "User command for updating dynamic blocks.
  11013. Update the dynamic block at point. With prefix ARG, update all dynamic
  11014. blocks in the buffer."
  11015. (interactive "P")
  11016. (if arg
  11017. (org-update-all-dblocks)
  11018. (or (looking-at org-dblock-start-re)
  11019. (org-beginning-of-dblock))
  11020. (org-update-dblock)))
  11021. (defun org-update-dblock ()
  11022. "Update the dynamic block at point.
  11023. This means to empty the block, parse for parameters and then call
  11024. the correct writing function."
  11025. (interactive)
  11026. (save-excursion
  11027. (let* ((win (selected-window))
  11028. (pos (point))
  11029. (line (org-current-line))
  11030. (params (org-prepare-dblock))
  11031. (name (plist-get params :name))
  11032. (indent (plist-get params :indentation-column))
  11033. (cmd (intern (concat "org-dblock-write:" name))))
  11034. (message "Updating dynamic block `%s' at line %d..." name line)
  11035. (funcall cmd params)
  11036. (message "Updating dynamic block `%s' at line %d...done" name line)
  11037. (goto-char pos)
  11038. (when (and indent (> indent 0))
  11039. (setq indent (make-string indent ?\ ))
  11040. (save-excursion
  11041. (select-window win)
  11042. (org-beginning-of-dblock)
  11043. (forward-line 1)
  11044. (while (not (looking-at org-dblock-end-re))
  11045. (insert indent)
  11046. (beginning-of-line 2))
  11047. (when (looking-at org-dblock-end-re)
  11048. (and (looking-at "[ \t]+")
  11049. (replace-match ""))
  11050. (insert indent)))))))
  11051. (defun org-beginning-of-dblock ()
  11052. "Find the beginning of the dynamic block at point.
  11053. Error if there is no such block at point."
  11054. (let ((pos (point))
  11055. beg)
  11056. (end-of-line 1)
  11057. (if (and (re-search-backward org-dblock-start-re nil t)
  11058. (setq beg (match-beginning 0))
  11059. (re-search-forward org-dblock-end-re nil t)
  11060. (> (match-end 0) pos))
  11061. (goto-char beg)
  11062. (goto-char pos)
  11063. (error "Not in a dynamic block"))))
  11064. (defun org-update-all-dblocks ()
  11065. "Update all dynamic blocks in the buffer.
  11066. This function can be used in a hook."
  11067. (interactive)
  11068. (when (derived-mode-p 'org-mode)
  11069. (org-map-dblocks 'org-update-dblock)))
  11070. ;;;; Completion
  11071. (declare-function org-export-backend-name "org-export" (cl-x))
  11072. (declare-function org-export-backend-options "org-export" (cl-x))
  11073. (defun org-get-export-keywords ()
  11074. "Return a list of all currently understood export keywords.
  11075. Export keywords include options, block names, attributes and
  11076. keywords relative to each registered export back-end."
  11077. (let (keywords)
  11078. (dolist (backend
  11079. (org-bound-and-true-p org-export--registered-backends)
  11080. (delq nil keywords))
  11081. ;; Back-end name (for keywords, like #+LATEX:)
  11082. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11083. (dolist (option-entry (org-export-backend-options backend))
  11084. ;; Back-end options.
  11085. (push (nth 1 option-entry) keywords)))))
  11086. (defconst org-options-keywords
  11087. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11088. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11089. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11090. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11091. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11092. (defcustom org-structure-template-alist
  11093. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11094. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11095. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11096. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11097. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11098. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11099. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX")
  11100. ("L" "#+LaTeX: ")
  11101. ("h" "#+BEGIN_HTML\n?\n#+END_HTML")
  11102. ("H" "#+HTML: ")
  11103. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  11104. ("A" "#+ASCII: ")
  11105. ("i" "#+INDEX: ?")
  11106. ("I" "#+INCLUDE: %file ?"))
  11107. "Structure completion elements.
  11108. This is a list of abbreviation keys and values. The value gets inserted
  11109. if you type `<' followed by the key and then press the completion key,
  11110. usually `TAB'. %file will be replaced by a file name after prompting
  11111. for the file using completion. The cursor will be placed at the position
  11112. of the `?` in the template.
  11113. There are two templates for each key, the first uses the original Org syntax,
  11114. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11115. the default when the /org-mtags.el/ module has been loaded. See also the
  11116. variable `org-mtags-prefer-muse-templates'."
  11117. :group 'org-completion
  11118. :type '(repeat
  11119. (list
  11120. (string :tag "Key")
  11121. (string :tag "Template")))
  11122. :version 24.4
  11123. :package-version '(Org . "8.3"))
  11124. (defun org-try-structure-completion ()
  11125. "Try to complete a structure template before point.
  11126. This looks for strings like \"<e\" on an otherwise empty line and
  11127. expands them."
  11128. (let ((l (buffer-substring (point-at-bol) (point)))
  11129. a)
  11130. (when (and (looking-at "[ \t]*$")
  11131. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11132. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11133. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11134. (match-beginning 1)) a)
  11135. t)))
  11136. (defun org-complete-expand-structure-template (start cell)
  11137. "Expand a structure template."
  11138. (let ((rpl (nth 1 cell))
  11139. (ind ""))
  11140. (delete-region start (point))
  11141. (when (string-match "\\`[ \t]*#\\+" rpl)
  11142. (cond
  11143. ((bolp))
  11144. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11145. (setq ind (buffer-substring (point-at-bol) (point))))
  11146. (t (newline))))
  11147. (setq start (point))
  11148. (if (string-match "%file" rpl)
  11149. (setq rpl (replace-match
  11150. (concat
  11151. "\""
  11152. (save-match-data
  11153. (abbreviate-file-name (read-file-name "Include file: ")))
  11154. "\"")
  11155. t t rpl)))
  11156. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11157. (concat "\n" ind)))
  11158. (insert rpl)
  11159. (if (re-search-backward "\\?" start t) (delete-char 1))))
  11160. ;;;; TODO, DEADLINE, Comments
  11161. (defun org-toggle-comment ()
  11162. "Change the COMMENT state of an entry."
  11163. (interactive)
  11164. (save-excursion
  11165. (org-back-to-heading)
  11166. (looking-at org-complex-heading-regexp)
  11167. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11168. (skip-chars-forward " \t")
  11169. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11170. (if (org-in-commented-heading-p t)
  11171. (delete-region (point)
  11172. (progn (search-forward " " (line-end-position) 'move)
  11173. (skip-chars-forward " \t")
  11174. (point)))
  11175. (insert org-comment-string)
  11176. (unless (eolp) (insert " ")))))
  11177. (defvar org-last-todo-state-is-todo nil
  11178. "This is non-nil when the last TODO state change led to a TODO state.
  11179. If the last change removed the TODO tag or switched to DONE, then
  11180. this is nil.")
  11181. (defvar org-setting-tags nil) ; dynamically skipped
  11182. (defvar org-todo-setup-filter-hook nil
  11183. "Hook for functions that pre-filter todo specs.
  11184. Each function takes a todo spec and returns either nil or the spec
  11185. transformed into canonical form." )
  11186. (defvar org-todo-get-default-hook nil
  11187. "Hook for functions that get a default item for todo.
  11188. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11189. nil or a string to be used for the todo mark." )
  11190. (defvar org-agenda-headline-snapshot-before-repeat)
  11191. (defun org-current-effective-time ()
  11192. "Return current time adjusted for `org-extend-today-until' variable."
  11193. (let* ((ct (org-current-time))
  11194. (dct (decode-time ct))
  11195. (ct1
  11196. (cond
  11197. (org-use-last-clock-out-time-as-effective-time
  11198. (or (org-clock-get-last-clock-out-time) ct))
  11199. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11200. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11201. (t ct))))
  11202. ct1))
  11203. (defun org-todo-yesterday (&optional arg)
  11204. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11205. (interactive "P")
  11206. (if (eq major-mode 'org-agenda-mode)
  11207. (apply 'org-agenda-todo-yesterday arg)
  11208. (let* ((hour (third (decode-time
  11209. (org-current-time))))
  11210. (org-extend-today-until (1+ hour)))
  11211. (org-todo arg))))
  11212. (defvar org-block-entry-blocking ""
  11213. "First entry preventing the TODO state change.")
  11214. (defun org-cancel-repeater ()
  11215. "Cancel a repeater by setting its numeric value to zero."
  11216. (interactive)
  11217. (save-excursion
  11218. (org-back-to-heading t)
  11219. (let ((bound1 (point))
  11220. (bound0 (save-excursion (outline-next-heading) (point))))
  11221. (when (re-search-forward
  11222. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11223. org-deadline-time-regexp "\\)\\|\\("
  11224. org-ts-regexp "\\)")
  11225. bound0 t)
  11226. (if (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]" bound1 t)
  11227. (replace-match "0" t nil nil 1))))))
  11228. (defun org-todo (&optional arg)
  11229. "Change the TODO state of an item.
  11230. The state of an item is given by a keyword at the start of the heading,
  11231. like
  11232. *** TODO Write paper
  11233. *** DONE Call mom
  11234. The different keywords are specified in the variable `org-todo-keywords'.
  11235. By default the available states are \"TODO\" and \"DONE\".
  11236. So for this example: when the item starts with TODO, it is changed to DONE.
  11237. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11238. DONE are present, add TODO at the beginning of the heading.
  11239. With \\[universal-argument] prefix arg, use completion to determine the new \
  11240. state.
  11241. With numeric prefix arg, switch to that state.
  11242. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11243. keywords (nextset).
  11244. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11245. With a numeric prefix arg of 0, inhibit note taking for the change.
  11246. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11247. When called through ELisp, arg is also interpreted in the following way:
  11248. 'none -> empty state
  11249. \"\"(empty string) -> switch to empty state
  11250. 'done -> switch to DONE
  11251. 'nextset -> switch to the next set of keywords
  11252. 'previousset -> switch to the previous set of keywords
  11253. \"WAITING\" -> switch to the specified keyword, but only if it
  11254. really is a member of `org-todo-keywords'."
  11255. (interactive "P")
  11256. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11257. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11258. 'region-start-level 'region))
  11259. org-loop-over-headlines-in-active-region)
  11260. (org-map-entries
  11261. `(org-todo ,arg)
  11262. org-loop-over-headlines-in-active-region
  11263. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11264. (if (equal arg '(16)) (setq arg 'nextset))
  11265. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11266. (let ((org-blocker-hook org-blocker-hook)
  11267. commentp
  11268. case-fold-search)
  11269. (when (equal arg '(64))
  11270. (setq arg nil org-blocker-hook nil))
  11271. (when (and org-blocker-hook
  11272. (or org-inhibit-blocking
  11273. (org-entry-get nil "NOBLOCKING")))
  11274. (setq org-blocker-hook nil))
  11275. (save-excursion
  11276. (catch 'exit
  11277. (org-back-to-heading t)
  11278. (when (org-in-commented-heading-p t)
  11279. (org-toggle-comment)
  11280. (setq commentp t))
  11281. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11282. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11283. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11284. (let* ((match-data (match-data))
  11285. (startpos (point-at-bol))
  11286. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11287. (org-log-done org-log-done)
  11288. (org-log-repeat org-log-repeat)
  11289. (org-todo-log-states org-todo-log-states)
  11290. (org-inhibit-logging
  11291. (if (equal arg 0)
  11292. (progn (setq arg nil) 'note) org-inhibit-logging))
  11293. (this (match-string 1))
  11294. (hl-pos (match-beginning 0))
  11295. (head (org-get-todo-sequence-head this))
  11296. (ass (assoc head org-todo-kwd-alist))
  11297. (interpret (nth 1 ass))
  11298. (done-word (nth 3 ass))
  11299. (final-done-word (nth 4 ass))
  11300. (org-last-state (or this ""))
  11301. (completion-ignore-case t)
  11302. (member (member this org-todo-keywords-1))
  11303. (tail (cdr member))
  11304. (org-state (cond
  11305. ((and org-todo-key-trigger
  11306. (or (and (equal arg '(4))
  11307. (eq org-use-fast-todo-selection 'prefix))
  11308. (and (not arg) org-use-fast-todo-selection
  11309. (not (eq org-use-fast-todo-selection
  11310. 'prefix)))))
  11311. ;; Use fast selection
  11312. (org-fast-todo-selection))
  11313. ((and (equal arg '(4))
  11314. (or (not org-use-fast-todo-selection)
  11315. (not org-todo-key-trigger)))
  11316. ;; Read a state with completion
  11317. (org-icompleting-read
  11318. "State: " (mapcar 'list org-todo-keywords-1)
  11319. nil t))
  11320. ((eq arg 'right)
  11321. (if this
  11322. (if tail (car tail) nil)
  11323. (car org-todo-keywords-1)))
  11324. ((eq arg 'left)
  11325. (if (equal member org-todo-keywords-1)
  11326. nil
  11327. (if this
  11328. (nth (- (length org-todo-keywords-1)
  11329. (length tail) 2)
  11330. org-todo-keywords-1)
  11331. (org-last org-todo-keywords-1))))
  11332. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11333. (setq arg nil))) ; hack to fall back to cycling
  11334. (arg
  11335. ;; user or caller requests a specific state
  11336. (cond
  11337. ((equal arg "") nil)
  11338. ((eq arg 'none) nil)
  11339. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11340. ((eq arg 'nextset)
  11341. (or (car (cdr (member head org-todo-heads)))
  11342. (car org-todo-heads)))
  11343. ((eq arg 'previousset)
  11344. (let ((org-todo-heads (reverse org-todo-heads)))
  11345. (or (car (cdr (member head org-todo-heads)))
  11346. (car org-todo-heads))))
  11347. ((car (member arg org-todo-keywords-1)))
  11348. ((stringp arg)
  11349. (user-error "State `%s' not valid in this file" arg))
  11350. ((nth (1- (prefix-numeric-value arg))
  11351. org-todo-keywords-1))))
  11352. ((null member) (or head (car org-todo-keywords-1)))
  11353. ((equal this final-done-word) nil) ;; -> make empty
  11354. ((null tail) nil) ;; -> first entry
  11355. ((memq interpret '(type priority))
  11356. (if (eq this-command last-command)
  11357. (car tail)
  11358. (if (> (length tail) 0)
  11359. (or done-word (car org-done-keywords))
  11360. nil)))
  11361. (t
  11362. (car tail))))
  11363. (org-state (or
  11364. (run-hook-with-args-until-success
  11365. 'org-todo-get-default-hook org-state org-last-state)
  11366. org-state))
  11367. (next (if org-state (concat " " org-state " ") " "))
  11368. (change-plist (list :type 'todo-state-change :from this :to org-state
  11369. :position startpos))
  11370. dolog now-done-p)
  11371. (when org-blocker-hook
  11372. (setq org-last-todo-state-is-todo
  11373. (not (member this org-done-keywords)))
  11374. (unless (save-excursion
  11375. (save-match-data
  11376. (org-with-wide-buffer
  11377. (run-hook-with-args-until-failure
  11378. 'org-blocker-hook change-plist))))
  11379. (if (org-called-interactively-p 'interactive)
  11380. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11381. this org-state org-block-entry-blocking)
  11382. ;; fail silently
  11383. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11384. this org-state org-block-entry-blocking)
  11385. (throw 'exit nil))))
  11386. (store-match-data match-data)
  11387. (replace-match next t t)
  11388. (cond ((equal this org-state)
  11389. (message "TODO state was already %s" (org-trim next)))
  11390. ((pos-visible-in-window-p hl-pos)
  11391. (message "TODO state changed to %s" (org-trim next))))
  11392. (unless head
  11393. (setq head (org-get-todo-sequence-head org-state)
  11394. ass (assoc head org-todo-kwd-alist)
  11395. interpret (nth 1 ass)
  11396. done-word (nth 3 ass)
  11397. final-done-word (nth 4 ass)))
  11398. (when (memq arg '(nextset previousset))
  11399. (message "Keyword-Set %d/%d: %s"
  11400. (- (length org-todo-sets) -1
  11401. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11402. (length org-todo-sets)
  11403. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11404. (setq org-last-todo-state-is-todo
  11405. (not (member org-state org-done-keywords)))
  11406. (setq now-done-p (and (member org-state org-done-keywords)
  11407. (not (member this org-done-keywords))))
  11408. (and logging (org-local-logging logging))
  11409. (when (and (or org-todo-log-states org-log-done)
  11410. (not (eq org-inhibit-logging t))
  11411. (not (memq arg '(nextset previousset))))
  11412. ;; we need to look at recording a time and note
  11413. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11414. (nth 2 (assoc this org-todo-log-states))))
  11415. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11416. (setq dolog 'time))
  11417. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11418. (and org-state
  11419. (member org-state org-not-done-keywords)
  11420. (not (member this org-not-done-keywords))))
  11421. ;; This is now a todo state and was not one before
  11422. ;; If there was a CLOSED time stamp, get rid of it.
  11423. (org-add-planning-info nil nil 'closed))
  11424. (when (and now-done-p org-log-done)
  11425. ;; It is now done, and it was not done before
  11426. (org-add-planning-info 'closed (org-current-effective-time))
  11427. (if (and (not dolog) (eq 'note org-log-done))
  11428. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11429. (when (and org-state dolog)
  11430. ;; This is a non-nil state, and we need to log it
  11431. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11432. ;; Fixup tag positioning
  11433. (org-todo-trigger-tag-changes org-state)
  11434. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11435. (when org-provide-todo-statistics
  11436. (org-update-parent-todo-statistics))
  11437. (run-hooks 'org-after-todo-state-change-hook)
  11438. (if (and arg (not (member org-state org-done-keywords)))
  11439. (setq head (org-get-todo-sequence-head org-state)))
  11440. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11441. ;; Do we need to trigger a repeat?
  11442. (when now-done-p
  11443. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11444. ;; This is for the agenda, take a snapshot of the headline.
  11445. (save-match-data
  11446. (setq org-agenda-headline-snapshot-before-repeat
  11447. (org-get-heading))))
  11448. (org-auto-repeat-maybe org-state))
  11449. ;; Fixup cursor location if close to the keyword
  11450. (if (and (outline-on-heading-p)
  11451. (not (bolp))
  11452. (save-excursion (beginning-of-line 1)
  11453. (looking-at org-todo-line-regexp))
  11454. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11455. (progn
  11456. (goto-char (or (match-end 2) (match-end 1)))
  11457. (and (looking-at " ") (just-one-space))))
  11458. (when org-trigger-hook
  11459. (save-excursion
  11460. (run-hook-with-args 'org-trigger-hook change-plist)))
  11461. (when commentp (org-toggle-comment))))))))
  11462. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11463. "Block turning an entry into a TODO, using the hierarchy.
  11464. This checks whether the current task should be blocked from state
  11465. changes. Such blocking occurs when:
  11466. 1. The task has children which are not all in a completed state.
  11467. 2. A task has a parent with the property :ORDERED:, and there
  11468. are siblings prior to the current task with incomplete
  11469. status.
  11470. 3. The parent of the task is blocked because it has siblings that should
  11471. be done first, or is child of a block grandparent TODO entry."
  11472. (if (not org-enforce-todo-dependencies)
  11473. t ; if locally turned off don't block
  11474. (catch 'dont-block
  11475. ;; If this is not a todo state change, or if this entry is already DONE,
  11476. ;; do not block
  11477. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11478. (member (plist-get change-plist :from)
  11479. (cons 'done org-done-keywords))
  11480. (member (plist-get change-plist :to)
  11481. (cons 'todo org-not-done-keywords))
  11482. (not (plist-get change-plist :to)))
  11483. (throw 'dont-block t))
  11484. ;; If this task has children, and any are undone, it's blocked
  11485. (save-excursion
  11486. (org-back-to-heading t)
  11487. (let ((this-level (funcall outline-level)))
  11488. (outline-next-heading)
  11489. (let ((child-level (funcall outline-level)))
  11490. (while (and (not (eobp))
  11491. (> child-level this-level))
  11492. ;; this todo has children, check whether they are all
  11493. ;; completed
  11494. (if (and (not (org-entry-is-done-p))
  11495. (org-entry-is-todo-p))
  11496. (progn (setq org-block-entry-blocking (org-get-heading))
  11497. (throw 'dont-block nil)))
  11498. (outline-next-heading)
  11499. (setq child-level (funcall outline-level))))))
  11500. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11501. ;; any previous siblings are undone, it's blocked
  11502. (save-excursion
  11503. (org-back-to-heading t)
  11504. (let* ((pos (point))
  11505. (parent-pos (and (org-up-heading-safe) (point))))
  11506. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11507. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11508. (forward-line 1)
  11509. (re-search-forward org-not-done-heading-regexp pos t))
  11510. (setq org-block-entry-blocking (match-string 0))
  11511. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11512. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11513. (while t
  11514. (goto-char parent-pos)
  11515. (if (not (looking-at org-not-done-heading-regexp))
  11516. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11517. (setq pos (point))
  11518. (setq parent-pos (and (org-up-heading-safe) (point)))
  11519. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11520. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11521. (forward-line 1)
  11522. (re-search-forward org-not-done-heading-regexp pos t)
  11523. (setq org-block-entry-blocking (org-get-heading)))
  11524. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11525. (defcustom org-track-ordered-property-with-tag nil
  11526. "Should the ORDERED property also be shown as a tag?
  11527. The ORDERED property decides if an entry should require subtasks to be
  11528. completed in sequence. Since a property is not very visible, setting
  11529. this option means that toggling the ORDERED property with the command
  11530. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11531. not relevant for the behavior, but it makes things more visible.
  11532. Note that toggling the tag with tags commands will not change the property
  11533. and therefore not influence behavior!
  11534. This can be t, meaning the tag ORDERED should be used, It can also be a
  11535. string to select a different tag for this task."
  11536. :group 'org-todo
  11537. :type '(choice
  11538. (const :tag "No tracking" nil)
  11539. (const :tag "Track with ORDERED tag" t)
  11540. (string :tag "Use other tag")))
  11541. (defun org-toggle-ordered-property ()
  11542. "Toggle the ORDERED property of the current entry.
  11543. For better visibility, you can track the value of this property with a tag.
  11544. See variable `org-track-ordered-property-with-tag'."
  11545. (interactive)
  11546. (let* ((t1 org-track-ordered-property-with-tag)
  11547. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11548. (save-excursion
  11549. (org-back-to-heading)
  11550. (if (org-entry-get nil "ORDERED")
  11551. (progn
  11552. (org-delete-property "ORDERED")
  11553. (and tag (org-toggle-tag tag 'off))
  11554. (message "Subtasks can be completed in arbitrary order"))
  11555. (org-entry-put nil "ORDERED" "t")
  11556. (and tag (org-toggle-tag tag 'on))
  11557. (message "Subtasks must be completed in sequence")))))
  11558. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11559. (defun org-block-todo-from-checkboxes (change-plist)
  11560. "Block turning an entry into a TODO, using checkboxes.
  11561. This checks whether the current task should be blocked from state
  11562. changes because there are unchecked boxes in this entry."
  11563. (if (not org-enforce-todo-checkbox-dependencies)
  11564. t ; if locally turned off don't block
  11565. (catch 'dont-block
  11566. ;; If this is not a todo state change, or if this entry is already DONE,
  11567. ;; do not block
  11568. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11569. (member (plist-get change-plist :from)
  11570. (cons 'done org-done-keywords))
  11571. (member (plist-get change-plist :to)
  11572. (cons 'todo org-not-done-keywords))
  11573. (not (plist-get change-plist :to)))
  11574. (throw 'dont-block t))
  11575. ;; If this task has checkboxes that are not checked, it's blocked
  11576. (save-excursion
  11577. (org-back-to-heading t)
  11578. (let ((beg (point)) end)
  11579. (outline-next-heading)
  11580. (setq end (point))
  11581. (goto-char beg)
  11582. (if (org-list-search-forward
  11583. (concat (org-item-beginning-re)
  11584. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11585. "\\[[- ]\\]")
  11586. end t)
  11587. (progn
  11588. (if (boundp 'org-blocked-by-checkboxes)
  11589. (setq org-blocked-by-checkboxes t))
  11590. (throw 'dont-block nil)))))
  11591. t))) ; do not block
  11592. (defun org-entry-blocked-p ()
  11593. "Is the current entry blocked?"
  11594. (org-with-silent-modifications
  11595. (if (org-entry-get nil "NOBLOCKING")
  11596. nil ;; Never block this entry
  11597. (not (run-hook-with-args-until-failure
  11598. 'org-blocker-hook
  11599. (list :type 'todo-state-change
  11600. :position (point)
  11601. :from 'todo
  11602. :to 'done))))))
  11603. (defun org-update-statistics-cookies (all)
  11604. "Update the statistics cookie, either from TODO or from checkboxes.
  11605. This should be called with the cursor in a line with a statistics cookie."
  11606. (interactive "P")
  11607. (if all
  11608. (progn
  11609. (org-update-checkbox-count 'all)
  11610. (org-map-entries 'org-update-parent-todo-statistics))
  11611. (if (not (org-at-heading-p))
  11612. (org-update-checkbox-count)
  11613. (let ((pos (point-marker))
  11614. end l1 l2)
  11615. (ignore-errors (org-back-to-heading t))
  11616. (if (not (org-at-heading-p))
  11617. (org-update-checkbox-count)
  11618. (setq l1 (org-outline-level))
  11619. (setq end (save-excursion
  11620. (outline-next-heading)
  11621. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11622. (point)))
  11623. (if (and (save-excursion
  11624. (re-search-forward
  11625. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11626. (not (save-excursion (re-search-forward
  11627. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11628. (org-update-checkbox-count)
  11629. (if (and l2 (> l2 l1))
  11630. (progn
  11631. (goto-char end)
  11632. (org-update-parent-todo-statistics))
  11633. (goto-char pos)
  11634. (beginning-of-line 1)
  11635. (while (re-search-forward
  11636. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11637. (point-at-eol) t)
  11638. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11639. (goto-char pos)
  11640. (move-marker pos nil)))))
  11641. (defvar org-entry-property-inherited-from) ;; defined below
  11642. (defun org-update-parent-todo-statistics ()
  11643. "Update any statistics cookie in the parent of the current headline.
  11644. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11645. the entire subtree and this will travel up the hierarchy and update
  11646. statistics everywhere."
  11647. (let* ((prop (save-excursion (org-up-heading-safe)
  11648. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11649. (recursive (or (not org-hierarchical-todo-statistics)
  11650. (and prop (string-match "\\<recursive\\>" prop))))
  11651. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11652. 0))
  11653. (first t)
  11654. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11655. level ltoggle l1 new ndel
  11656. (cnt-all 0) (cnt-done 0) is-percent kwd
  11657. checkbox-beg ov ovs ove cookie-present)
  11658. (catch 'exit
  11659. (save-excursion
  11660. (beginning-of-line 1)
  11661. (setq ltoggle (funcall outline-level))
  11662. ;; Three situations are to consider:
  11663. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11664. ;; to the top-level ancestor on the headline;
  11665. ;; 2. If parent has "recursive" property, repeat up to the
  11666. ;; headline setting that property, taking inheritance into
  11667. ;; account;
  11668. ;; 3. Else, move up to direct parent and proceed only once.
  11669. (while (and (setq level (org-up-heading-safe))
  11670. (or recursive first)
  11671. (>= (point) lim))
  11672. (setq first nil cookie-present nil)
  11673. (unless (and level
  11674. (not (string-match
  11675. "\\<checkbox\\>"
  11676. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11677. "")))))
  11678. (throw 'exit nil))
  11679. (while (re-search-forward box-re (point-at-eol) t)
  11680. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11681. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11682. (save-match-data
  11683. (unless (outline-next-heading) (throw 'exit nil))
  11684. (while (and (looking-at org-complex-heading-regexp)
  11685. (> (setq l1 (length (match-string 1))) level))
  11686. (setq kwd (and (or recursive (= l1 ltoggle))
  11687. (match-string 2)))
  11688. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11689. (and (eq org-provide-todo-statistics t)
  11690. (or (member kwd org-done-keywords)))
  11691. (and (listp org-provide-todo-statistics)
  11692. (stringp (car org-provide-todo-statistics))
  11693. (or (member kwd org-provide-todo-statistics)
  11694. (member kwd org-done-keywords)))
  11695. (and (listp org-provide-todo-statistics)
  11696. (listp (car org-provide-todo-statistics))
  11697. (or (member kwd (car org-provide-todo-statistics))
  11698. (and (member kwd org-done-keywords)
  11699. (member kwd (cadr org-provide-todo-statistics))))))
  11700. (setq cnt-all (1+ cnt-all))
  11701. (if (eq org-provide-todo-statistics t)
  11702. (and kwd (setq cnt-all (1+ cnt-all)))))
  11703. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11704. (member kwd org-done-keywords))
  11705. (and (listp org-provide-todo-statistics)
  11706. (listp (car org-provide-todo-statistics))
  11707. (member kwd org-done-keywords)
  11708. (member kwd (cadr org-provide-todo-statistics)))
  11709. (and (listp org-provide-todo-statistics)
  11710. (stringp (car org-provide-todo-statistics))
  11711. (member kwd org-done-keywords)))
  11712. (setq cnt-done (1+ cnt-done)))
  11713. (outline-next-heading)))
  11714. (setq new
  11715. (if is-percent
  11716. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11717. (format "[%d/%d]" cnt-done cnt-all))
  11718. ndel (- (match-end 0) checkbox-beg))
  11719. ;; handle overlays when updating cookie from column view
  11720. (when (setq ov (car (overlays-at checkbox-beg)))
  11721. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11722. (delete-overlay ov))
  11723. (goto-char checkbox-beg)
  11724. (insert new)
  11725. (delete-region (point) (+ (point) ndel))
  11726. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11727. (when ov (move-overlay ov ovs ove)))
  11728. (when cookie-present
  11729. (run-hook-with-args 'org-after-todo-statistics-hook
  11730. cnt-done (- cnt-all cnt-done))))))
  11731. (run-hooks 'org-todo-statistics-hook)))
  11732. (defvar org-after-todo-statistics-hook nil
  11733. "Hook that is called after a TODO statistics cookie has been updated.
  11734. Each function is called with two arguments: the number of not-done entries
  11735. and the number of done entries.
  11736. For example, the following function, when added to this hook, will switch
  11737. an entry to DONE when all children are done, and back to TODO when new
  11738. entries are set to a TODO status. Note that this hook is only called
  11739. when there is a statistics cookie in the headline!
  11740. (defun org-summary-todo (n-done n-not-done)
  11741. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11742. (let (org-log-done org-log-states) ; turn off logging
  11743. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11744. ")
  11745. (defvar org-todo-statistics-hook nil
  11746. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11747. This hook runs even if there is no statistics cookie present, in which case
  11748. `org-after-todo-statistics-hook' would not run.")
  11749. (defun org-todo-trigger-tag-changes (state)
  11750. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11751. (let ((l org-todo-state-tags-triggers)
  11752. changes)
  11753. (when (or (not state) (equal state ""))
  11754. (setq changes (append changes (cdr (assoc "" l)))))
  11755. (when (and (stringp state) (> (length state) 0))
  11756. (setq changes (append changes (cdr (assoc state l)))))
  11757. (when (member state org-not-done-keywords)
  11758. (setq changes (append changes (cdr (assoc 'todo l)))))
  11759. (when (member state org-done-keywords)
  11760. (setq changes (append changes (cdr (assoc 'done l)))))
  11761. (dolist (c changes)
  11762. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11763. (defun org-local-logging (value)
  11764. "Get logging settings from a property VALUE."
  11765. (let* (words w a)
  11766. ;; directly set the variables, they are already local.
  11767. (setq org-log-done nil
  11768. org-log-repeat nil
  11769. org-todo-log-states nil)
  11770. (setq words (org-split-string value))
  11771. (while (setq w (pop words))
  11772. (cond
  11773. ((setq a (assoc w org-startup-options))
  11774. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11775. (set (nth 1 a) (nth 2 a))))
  11776. ((setq a (org-extract-log-state-settings w))
  11777. (and (member (car a) org-todo-keywords-1)
  11778. (push a org-todo-log-states)))))))
  11779. (defun org-get-todo-sequence-head (kwd)
  11780. "Return the head of the TODO sequence to which KWD belongs.
  11781. If KWD is not set, check if there is a text property remembering the
  11782. right sequence."
  11783. (let (p)
  11784. (cond
  11785. ((not kwd)
  11786. (or (get-text-property (point-at-bol) 'org-todo-head)
  11787. (progn
  11788. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11789. nil (point-at-eol)))
  11790. (get-text-property p 'org-todo-head))))
  11791. ((not (member kwd org-todo-keywords-1))
  11792. (car org-todo-keywords-1))
  11793. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11794. (defun org-fast-todo-selection ()
  11795. "Fast TODO keyword selection with single keys.
  11796. Returns the new TODO keyword, or nil if no state change should occur."
  11797. (let* ((fulltable org-todo-key-alist)
  11798. (done-keywords org-done-keywords) ;; needed for the faces.
  11799. (maxlen (apply 'max (mapcar
  11800. (lambda (x)
  11801. (if (stringp (car x)) (string-width (car x)) 0))
  11802. fulltable)))
  11803. (expert nil)
  11804. (fwidth (+ maxlen 3 1 3))
  11805. (ncol (/ (- (window-width) 4) fwidth))
  11806. tg cnt e c tbl
  11807. groups ingroup)
  11808. (save-excursion
  11809. (save-window-excursion
  11810. (if expert
  11811. (set-buffer (get-buffer-create " *Org todo*"))
  11812. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11813. (erase-buffer)
  11814. (org-set-local 'org-done-keywords done-keywords)
  11815. (setq tbl fulltable cnt 0)
  11816. (while (setq e (pop tbl))
  11817. (cond
  11818. ((equal e '(:startgroup))
  11819. (push '() groups) (setq ingroup t)
  11820. (when (not (= cnt 0))
  11821. (setq cnt 0)
  11822. (insert "\n"))
  11823. (insert "{ "))
  11824. ((equal e '(:endgroup))
  11825. (setq ingroup nil cnt 0)
  11826. (insert "}\n"))
  11827. ((equal e '(:newline))
  11828. (when (not (= cnt 0))
  11829. (setq cnt 0)
  11830. (insert "\n")
  11831. (setq e (car tbl))
  11832. (while (equal (car tbl) '(:newline))
  11833. (insert "\n")
  11834. (setq tbl (cdr tbl)))))
  11835. (t
  11836. (setq tg (car e) c (cdr e))
  11837. (if ingroup (push tg (car groups)))
  11838. (setq tg (org-add-props tg nil 'face
  11839. (org-get-todo-face tg)))
  11840. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11841. (insert "[" c "] " tg (make-string
  11842. (- fwidth 4 (length tg)) ?\ ))
  11843. (when (= (setq cnt (1+ cnt)) ncol)
  11844. (insert "\n")
  11845. (if ingroup (insert " "))
  11846. (setq cnt 0)))))
  11847. (insert "\n")
  11848. (goto-char (point-min))
  11849. (if (not expert) (org-fit-window-to-buffer))
  11850. (message "[a-z..]:Set [SPC]:clear")
  11851. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11852. (cond
  11853. ((or (= c ?\C-g)
  11854. (and (= c ?q) (not (rassoc c fulltable))))
  11855. (setq quit-flag t))
  11856. ((= c ?\ ) nil)
  11857. ((setq e (rassoc c fulltable) tg (car e))
  11858. tg)
  11859. (t (setq quit-flag t)))))))
  11860. (defun org-entry-is-todo-p ()
  11861. (member (org-get-todo-state) org-not-done-keywords))
  11862. (defun org-entry-is-done-p ()
  11863. (member (org-get-todo-state) org-done-keywords))
  11864. (defun org-get-todo-state ()
  11865. "Return the TODO keyword of the current subtree."
  11866. (save-excursion
  11867. (org-back-to-heading t)
  11868. (and (looking-at org-todo-line-regexp)
  11869. (match-end 2)
  11870. (match-string 2))))
  11871. (defun org-at-date-range-p (&optional inactive-ok)
  11872. "Is the cursor inside a date range?"
  11873. (interactive)
  11874. (save-excursion
  11875. (catch 'exit
  11876. (let ((pos (point)))
  11877. (skip-chars-backward "^[<\r\n")
  11878. (skip-chars-backward "<[")
  11879. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11880. (>= (match-end 0) pos)
  11881. (throw 'exit t))
  11882. (skip-chars-backward "^<[\r\n")
  11883. (skip-chars-backward "<[")
  11884. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11885. (>= (match-end 0) pos)
  11886. (throw 'exit t)))
  11887. nil)))
  11888. (defun org-get-repeat (&optional tagline)
  11889. "Check if there is a deadline/schedule with repeater in this entry."
  11890. (save-match-data
  11891. (save-excursion
  11892. (org-back-to-heading t)
  11893. (and (re-search-forward (if tagline
  11894. (concat tagline "\\s-*" org-repeat-re)
  11895. org-repeat-re)
  11896. (org-entry-end-position) t)
  11897. (match-string-no-properties 1)))))
  11898. (defvar org-last-changed-timestamp)
  11899. (defvar org-last-inserted-timestamp)
  11900. (defvar org-log-post-message)
  11901. (defvar org-log-note-purpose)
  11902. (defvar org-log-note-how nil)
  11903. (defvar org-log-note-extra)
  11904. (defun org-auto-repeat-maybe (done-word)
  11905. "Check if the current headline contains a repeated deadline/schedule.
  11906. If yes, set TODO state back to what it was and change the base date
  11907. of repeating deadline/scheduled time stamps to new date.
  11908. This function is run automatically after each state change to a DONE state."
  11909. ;; last-state is dynamically scoped into this function
  11910. (let* ((repeat (org-get-repeat))
  11911. (aa (assoc org-last-state org-todo-kwd-alist))
  11912. (interpret (nth 1 aa))
  11913. (head (nth 2 aa))
  11914. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11915. (msg "Entry repeats: ")
  11916. (org-log-done nil)
  11917. (org-todo-log-states nil)
  11918. re type n what ts time to-state)
  11919. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11920. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11921. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11922. org-todo-repeat-to-state))
  11923. (unless (and to-state (member to-state org-todo-keywords-1))
  11924. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11925. (org-todo to-state)
  11926. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11927. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11928. (org-time-stamp-format t t))))
  11929. (when org-log-repeat
  11930. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11931. (memq 'org-add-log-note post-command-hook))
  11932. ;; OK, we are already setup for some record
  11933. (if (eq org-log-repeat 'note)
  11934. ;; make sure we take a note, not only a time stamp
  11935. (setq org-log-note-how 'note))
  11936. ;; Set up for taking a record
  11937. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11938. org-last-state
  11939. 'findpos org-log-repeat)))
  11940. (org-back-to-heading t)
  11941. (org-add-planning-info nil nil 'closed)
  11942. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11943. org-deadline-time-regexp "\\)\\|\\("
  11944. org-ts-regexp "\\)"))
  11945. (while (re-search-forward
  11946. re (save-excursion (outline-next-heading) (point)) t)
  11947. (setq type (if (match-end 1) org-scheduled-string
  11948. (if (match-end 3) org-deadline-string "Plain:"))
  11949. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11950. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11951. (setq n (string-to-number (match-string 2 ts))
  11952. what (match-string 3 ts))
  11953. (if (equal what "w") (setq n (* n 7) what "d"))
  11954. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11955. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11956. ;; Preparation, see if we need to modify the start date for the change
  11957. (when (match-end 1)
  11958. (setq time (save-match-data (org-time-string-to-time ts)))
  11959. (cond
  11960. ((equal (match-string 1 ts) ".")
  11961. ;; Shift starting date to today
  11962. (org-timestamp-change
  11963. (- (org-today) (time-to-days time))
  11964. 'day))
  11965. ((equal (match-string 1 ts) "+")
  11966. (let ((nshiftmax 10) (nshift 0))
  11967. (while (or (= nshift 0)
  11968. (<= (time-to-days time)
  11969. (time-to-days (current-time))))
  11970. (when (= (incf nshift) nshiftmax)
  11971. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11972. (user-error "Abort")))
  11973. (org-timestamp-change n (cdr (assoc what whata)))
  11974. (org-at-timestamp-p t)
  11975. (setq ts (match-string 1))
  11976. (setq time (save-match-data (org-time-string-to-time ts)))))
  11977. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11978. ;; rematch, so that we have everything in place for the real shift
  11979. (org-at-timestamp-p t)
  11980. (setq ts (match-string 1))
  11981. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11982. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11983. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11984. (setq org-log-post-message msg)
  11985. (message "%s" msg))))
  11986. (defun org-show-todo-tree (arg)
  11987. "Make a compact tree which shows all headlines marked with TODO.
  11988. The tree will show the lines where the regexp matches, and all higher
  11989. headlines above the match.
  11990. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11991. With a numeric prefix N, construct a sparse tree for the Nth element
  11992. of `org-todo-keywords-1'."
  11993. (interactive "P")
  11994. (let ((case-fold-search nil)
  11995. (kwd-re
  11996. (cond ((null arg) org-not-done-regexp)
  11997. ((equal arg '(4))
  11998. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11999. (mapcar 'list org-todo-keywords-1))))
  12000. (concat "\\("
  12001. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12002. "\\)\\>")))
  12003. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12004. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12005. org-todo-keywords-1)))
  12006. (t (user-error "Invalid prefix argument: %s" arg)))))
  12007. (message "%d TODO entries found"
  12008. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  12009. (defun org-deadline (arg &optional time)
  12010. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12011. With one universal prefix argument, remove any deadline from the item.
  12012. With two universal prefix arguments, prompt for a warning delay.
  12013. With argument TIME, set the deadline at the corresponding date. TIME
  12014. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12015. (interactive "P")
  12016. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12017. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12018. 'region-start-level 'region))
  12019. org-loop-over-headlines-in-active-region)
  12020. (org-map-entries
  12021. `(org-deadline ',arg ,time)
  12022. org-loop-over-headlines-in-active-region
  12023. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12024. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12025. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12026. (repeater (and old-date
  12027. (string-match
  12028. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12029. old-date)
  12030. (match-string 1 old-date))))
  12031. (cond
  12032. ((equal arg '(4))
  12033. (when (and old-date org-log-redeadline)
  12034. (org-add-log-setup 'deldeadline nil old-date 'findpos
  12035. org-log-redeadline))
  12036. (org-remove-timestamp-with-keyword org-deadline-string)
  12037. (message "Item no longer has a deadline."))
  12038. ((equal arg '(16))
  12039. (save-excursion
  12040. (org-back-to-heading t)
  12041. (if (re-search-forward
  12042. org-deadline-time-regexp
  12043. (save-excursion (outline-next-heading) (point)) t)
  12044. (let* ((rpl0 (match-string 1))
  12045. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12046. (replace-match
  12047. (concat org-deadline-string
  12048. " <" rpl
  12049. (format " -%dd"
  12050. (abs
  12051. (- (time-to-days
  12052. (save-match-data
  12053. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12054. (time-to-days old-date-time))))
  12055. ">") t t))
  12056. (user-error "No deadline information to update"))))
  12057. (t
  12058. (org-add-planning-info 'deadline time 'closed)
  12059. (when (and old-date org-log-redeadline
  12060. (not (equal old-date
  12061. (substring org-last-inserted-timestamp 1 -1))))
  12062. (org-add-log-setup 'redeadline nil old-date 'findpos
  12063. org-log-redeadline))
  12064. (when repeater
  12065. (save-excursion
  12066. (org-back-to-heading t)
  12067. (when (re-search-forward (concat org-deadline-string " "
  12068. org-last-inserted-timestamp)
  12069. (save-excursion
  12070. (outline-next-heading) (point)) t)
  12071. (goto-char (1- (match-end 0)))
  12072. (insert " " repeater)
  12073. (setq org-last-inserted-timestamp
  12074. (concat (substring org-last-inserted-timestamp 0 -1)
  12075. " " repeater
  12076. (substring org-last-inserted-timestamp -1))))))
  12077. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12078. (defun org-schedule (arg &optional time)
  12079. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12080. With one universal prefix argument, remove any scheduling date from the item.
  12081. With two universal prefix arguments, prompt for a delay cookie.
  12082. With argument TIME, scheduled at the corresponding date. TIME can
  12083. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12084. (interactive "P")
  12085. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12086. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12087. 'region-start-level 'region))
  12088. org-loop-over-headlines-in-active-region)
  12089. (org-map-entries
  12090. `(org-schedule ',arg ,time)
  12091. org-loop-over-headlines-in-active-region
  12092. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12093. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12094. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12095. (repeater (and old-date
  12096. (string-match
  12097. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12098. old-date)
  12099. (match-string 1 old-date))))
  12100. (cond
  12101. ((equal arg '(4))
  12102. (progn
  12103. (when (and old-date org-log-reschedule)
  12104. (org-add-log-setup 'delschedule nil old-date 'findpos
  12105. org-log-reschedule))
  12106. (org-remove-timestamp-with-keyword org-scheduled-string)
  12107. (message "Item is no longer scheduled.")))
  12108. ((equal arg '(16))
  12109. (save-excursion
  12110. (org-back-to-heading t)
  12111. (if (re-search-forward
  12112. org-scheduled-time-regexp
  12113. (save-excursion (outline-next-heading) (point)) t)
  12114. (let* ((rpl0 (match-string 1))
  12115. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12116. (replace-match
  12117. (concat org-scheduled-string
  12118. " <" rpl
  12119. (format " -%dd"
  12120. (abs
  12121. (- (time-to-days
  12122. (save-match-data
  12123. (org-read-date nil t nil "Delay until" old-date-time)))
  12124. (time-to-days old-date-time))))
  12125. ">") t t))
  12126. (user-error "No scheduled information to update"))))
  12127. (t
  12128. (org-add-planning-info 'scheduled time 'closed)
  12129. (when (and old-date org-log-reschedule
  12130. (not (equal old-date
  12131. (substring org-last-inserted-timestamp 1 -1))))
  12132. (org-add-log-setup 'reschedule nil old-date 'findpos
  12133. org-log-reschedule))
  12134. (when repeater
  12135. (save-excursion
  12136. (org-back-to-heading t)
  12137. (when (re-search-forward (concat org-scheduled-string " "
  12138. org-last-inserted-timestamp)
  12139. (save-excursion
  12140. (outline-next-heading) (point)) t)
  12141. (goto-char (1- (match-end 0)))
  12142. (insert " " repeater)
  12143. (setq org-last-inserted-timestamp
  12144. (concat (substring org-last-inserted-timestamp 0 -1)
  12145. " " repeater
  12146. (substring org-last-inserted-timestamp -1))))))
  12147. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12148. (defun org-get-scheduled-time (pom &optional inherit)
  12149. "Get the scheduled time as a time tuple, of a format suitable
  12150. for calling org-schedule with, or if there is no scheduling,
  12151. returns nil."
  12152. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12153. (when time
  12154. (apply 'encode-time (org-parse-time-string time)))))
  12155. (defun org-get-deadline-time (pom &optional inherit)
  12156. "Get the deadline as a time tuple, of a format suitable for
  12157. calling org-deadline with, or if there is no scheduling, returns
  12158. nil."
  12159. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12160. (when time
  12161. (apply 'encode-time (org-parse-time-string time)))))
  12162. (defun org-remove-timestamp-with-keyword (keyword)
  12163. "Remove all time stamps with KEYWORD in the current entry."
  12164. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12165. beg)
  12166. (save-excursion
  12167. (org-back-to-heading t)
  12168. (setq beg (point))
  12169. (outline-next-heading)
  12170. (while (re-search-backward re beg t)
  12171. (replace-match "")
  12172. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12173. (equal (char-before) ?\ ))
  12174. (backward-delete-char 1)
  12175. (if (string-match "^[ \t]*$" (buffer-substring
  12176. (point-at-bol) (point-at-eol)))
  12177. (delete-region (point-at-bol)
  12178. (min (point-max) (1+ (point-at-eol))))))))))
  12179. (defvar org-time-was-given) ; dynamically scoped parameter
  12180. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12181. (defun org-add-planning-info (what &optional time &rest remove)
  12182. "Insert new timestamp with keyword in the line directly after the headline.
  12183. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  12184. If non is given, the user is prompted for a date.
  12185. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  12186. be removed."
  12187. (interactive)
  12188. (let (org-time-was-given org-end-time-was-given ts
  12189. end default-time default-input)
  12190. (catch 'exit
  12191. (when (and (memq what '(scheduled deadline))
  12192. (or (not time)
  12193. (and (stringp time)
  12194. (string-match "^[-+]+[0-9]" time))))
  12195. ;; Try to get a default date/time from existing timestamp
  12196. (save-excursion
  12197. (org-back-to-heading t)
  12198. (setq end (save-excursion (outline-next-heading) (point)))
  12199. (when (re-search-forward (if (eq what 'scheduled)
  12200. org-scheduled-time-regexp
  12201. org-deadline-time-regexp)
  12202. end t)
  12203. (setq ts (match-string 1)
  12204. default-time
  12205. (apply 'encode-time (org-parse-time-string ts))
  12206. default-input (and ts (org-get-compact-tod ts))))))
  12207. (when what
  12208. (setq time
  12209. (if (stringp time)
  12210. ;; This is a string (relative or absolute), set proper date
  12211. (apply 'encode-time
  12212. (org-read-date-analyze
  12213. time default-time (decode-time default-time)))
  12214. ;; If necessary, get the time from the user
  12215. (or time (org-read-date nil 'to-time nil nil
  12216. default-time default-input)))))
  12217. (when (and org-insert-labeled-timestamps-at-point
  12218. (member what '(scheduled deadline)))
  12219. (insert
  12220. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  12221. (org-insert-time-stamp time org-time-was-given
  12222. nil nil nil (list org-end-time-was-given))
  12223. (setq what nil))
  12224. (save-excursion
  12225. (save-restriction
  12226. (let (col list elt ts buffer-invisibility-spec)
  12227. (org-back-to-heading t)
  12228. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  12229. (goto-char (match-end 1))
  12230. (setq col (current-column))
  12231. (goto-char (match-end 0))
  12232. (if (eobp) (insert "\n") (forward-char 1))
  12233. (when (and (not what)
  12234. (not (looking-at
  12235. (concat "[ \t]*"
  12236. org-keyword-time-not-clock-regexp))))
  12237. ;; Nothing to add, nothing to remove...... :-)
  12238. (throw 'exit nil))
  12239. (if (and (not (looking-at org-outline-regexp))
  12240. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  12241. "[^\r\n]*"))
  12242. (not (equal (match-string 1) org-clock-string)))
  12243. (narrow-to-region (match-beginning 0) (match-end 0))
  12244. (insert-before-markers "\n")
  12245. (backward-char 1)
  12246. (narrow-to-region (point) (point))
  12247. (and org-adapt-indentation (org-indent-to-column col)))
  12248. ;; Check if we have to remove something.
  12249. (setq list (cons what remove))
  12250. (while list
  12251. (setq elt (pop list))
  12252. (when (or (and (eq elt 'scheduled)
  12253. (re-search-forward org-scheduled-time-regexp nil t))
  12254. (and (eq elt 'deadline)
  12255. (re-search-forward org-deadline-time-regexp nil t))
  12256. (and (eq elt 'closed)
  12257. (re-search-forward org-closed-time-regexp nil t)))
  12258. (replace-match "")
  12259. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  12260. (and (looking-at "[ \t]+") (replace-match ""))
  12261. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  12262. (when what
  12263. (insert
  12264. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  12265. (cond ((eq what 'scheduled) org-scheduled-string)
  12266. ((eq what 'deadline) org-deadline-string)
  12267. ((eq what 'closed) org-closed-string))
  12268. " ")
  12269. (setq ts (org-insert-time-stamp
  12270. time
  12271. (or org-time-was-given
  12272. (and (eq what 'closed) org-log-done-with-time))
  12273. (eq what 'closed)
  12274. nil nil (list org-end-time-was-given)))
  12275. (insert
  12276. (if (not (or (bolp) (eq (char-before) ?\ )
  12277. (memq (char-after) '(32 10))
  12278. (eobp))) " " ""))
  12279. (end-of-line 1))
  12280. (goto-char (point-min))
  12281. (widen)
  12282. (if (and (looking-at "[ \t]*\n")
  12283. (equal (char-before) ?\n))
  12284. (delete-region (1- (point)) (point-at-eol)))
  12285. ts))))))
  12286. (defvar org-log-note-marker (make-marker))
  12287. (defvar org-log-note-purpose nil)
  12288. (defvar org-log-note-state nil)
  12289. (defvar org-log-note-previous-state nil)
  12290. (defvar org-log-note-extra nil)
  12291. (defvar org-log-note-window-configuration nil)
  12292. (defvar org-log-note-return-to (make-marker))
  12293. (defvar org-log-note-effective-time nil
  12294. "Remembered current time so that dynamically scoped
  12295. `org-extend-today-until' affects tha timestamps in state change
  12296. log")
  12297. (defvar org-log-post-message nil
  12298. "Message to be displayed after a log note has been stored.
  12299. The auto-repeater uses this.")
  12300. (defun org-add-note ()
  12301. "Add a note to the current entry.
  12302. This is done in the same way as adding a state change note."
  12303. (interactive)
  12304. (org-add-log-setup 'note nil nil 'findpos nil))
  12305. (defvar org-property-end-re)
  12306. (defun org-add-log-setup (&optional purpose state prev-state
  12307. findpos how extra)
  12308. "Set up the post command hook to take a note.
  12309. If this is about to TODO state change, the new state is expected in STATE.
  12310. When FINDPOS is non-nil, find the correct position for the note in
  12311. the current entry. If not, assume that it can be inserted at point.
  12312. HOW is an indicator what kind of note should be created.
  12313. EXTRA is additional text that will be inserted into the notes buffer."
  12314. (let* ((org-log-into-drawer (org-log-into-drawer))
  12315. (drawer (cond ((stringp org-log-into-drawer)
  12316. org-log-into-drawer)
  12317. (org-log-into-drawer "LOGBOOK"))))
  12318. (save-restriction
  12319. (save-excursion
  12320. (when findpos
  12321. (org-back-to-heading t)
  12322. (narrow-to-region (point) (save-excursion
  12323. (outline-next-heading) (point)))
  12324. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12325. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12326. "[^\r\n]*\\)?"))
  12327. (goto-char (match-end 0))
  12328. (cond
  12329. (drawer
  12330. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12331. nil t)
  12332. (progn
  12333. (goto-char (match-end 0))
  12334. (or org-log-states-order-reversed
  12335. (and (re-search-forward org-property-end-re nil t)
  12336. (goto-char (1- (match-beginning 0))))))
  12337. (insert "\n:" drawer ":\n:END:")
  12338. (beginning-of-line 0)
  12339. (org-indent-line)
  12340. (beginning-of-line 2)
  12341. (org-indent-line)
  12342. (end-of-line 0)))
  12343. ((and org-log-state-notes-insert-after-drawers
  12344. (save-excursion
  12345. (forward-line) (looking-at org-drawer-regexp)))
  12346. (forward-line)
  12347. (while (looking-at org-drawer-regexp)
  12348. (goto-char (match-end 0))
  12349. (re-search-forward org-property-end-re (point-max) t)
  12350. (forward-line))
  12351. (forward-line -1)))
  12352. (unless org-log-states-order-reversed
  12353. (and (= (char-after) ?\n) (forward-char 1))
  12354. (org-skip-over-state-notes)
  12355. (skip-chars-backward " \t\n\r")))
  12356. (move-marker org-log-note-marker (point))
  12357. (setq org-log-note-purpose purpose
  12358. org-log-note-state state
  12359. org-log-note-previous-state prev-state
  12360. org-log-note-how how
  12361. org-log-note-extra extra
  12362. org-log-note-effective-time (org-current-effective-time))
  12363. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12364. (defun org-skip-over-state-notes ()
  12365. "Skip past the list of State notes in an entry."
  12366. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12367. (when (ignore-errors (goto-char (org-in-item-p)))
  12368. (let* ((struct (org-list-struct))
  12369. (prevs (org-list-prevs-alist struct)))
  12370. (while (looking-at "[ \t]*- State")
  12371. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12372. (org-list-get-item-end (point) struct)))))))
  12373. (defun org-add-log-note (&optional purpose)
  12374. "Pop up a window for taking a note, and add this note later at point."
  12375. (remove-hook 'post-command-hook 'org-add-log-note)
  12376. (setq org-log-note-window-configuration (current-window-configuration))
  12377. (delete-other-windows)
  12378. (move-marker org-log-note-return-to (point))
  12379. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12380. (goto-char org-log-note-marker)
  12381. (org-switch-to-buffer-other-window "*Org Note*")
  12382. (erase-buffer)
  12383. (if (memq org-log-note-how '(time state))
  12384. (let (current-prefix-arg) (org-store-log-note))
  12385. (let ((org-inhibit-startup t)) (org-mode))
  12386. (insert (format "# Insert note for %s.
  12387. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12388. (cond
  12389. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12390. ((eq org-log-note-purpose 'done) "closed todo item")
  12391. ((eq org-log-note-purpose 'state)
  12392. (format "state change from \"%s\" to \"%s\""
  12393. (or org-log-note-previous-state "")
  12394. (or org-log-note-state "")))
  12395. ((eq org-log-note-purpose 'reschedule)
  12396. "rescheduling")
  12397. ((eq org-log-note-purpose 'delschedule)
  12398. "no longer scheduled")
  12399. ((eq org-log-note-purpose 'redeadline)
  12400. "changing deadline")
  12401. ((eq org-log-note-purpose 'deldeadline)
  12402. "removing deadline")
  12403. ((eq org-log-note-purpose 'refile)
  12404. "refiling")
  12405. ((eq org-log-note-purpose 'note)
  12406. "this entry")
  12407. (t (error "This should not happen")))))
  12408. (if org-log-note-extra (insert org-log-note-extra))
  12409. (org-set-local 'org-finish-function 'org-store-log-note)
  12410. (run-hooks 'org-log-buffer-setup-hook)))
  12411. (defvar org-note-abort nil) ; dynamically scoped
  12412. (defun org-store-log-note ()
  12413. "Finish taking a log note, and insert it to where it belongs."
  12414. (let ((txt (buffer-string)))
  12415. (kill-buffer (current-buffer))
  12416. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12417. lines ind bul)
  12418. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12419. (setq txt (replace-match "" t t txt)))
  12420. (if (string-match "\\s-+\\'" txt)
  12421. (setq txt (replace-match "" t t txt)))
  12422. (setq lines (org-split-string txt "\n"))
  12423. (when (and note (string-match "\\S-" note))
  12424. (setq note
  12425. (org-replace-escapes
  12426. note
  12427. (list (cons "%u" (user-login-name))
  12428. (cons "%U" user-full-name)
  12429. (cons "%t" (format-time-string
  12430. (org-time-stamp-format 'long 'inactive)
  12431. org-log-note-effective-time))
  12432. (cons "%T" (format-time-string
  12433. (org-time-stamp-format 'long nil)
  12434. org-log-note-effective-time))
  12435. (cons "%d" (format-time-string
  12436. (org-time-stamp-format nil 'inactive)
  12437. org-log-note-effective-time))
  12438. (cons "%D" (format-time-string
  12439. (org-time-stamp-format nil nil)
  12440. org-log-note-effective-time))
  12441. (cons "%s" (if org-log-note-state
  12442. (concat "\"" org-log-note-state "\"")
  12443. ""))
  12444. (cons "%S" (if org-log-note-previous-state
  12445. (concat "\"" org-log-note-previous-state "\"")
  12446. "\"\"")))))
  12447. (if lines (setq note (concat note " \\\\")))
  12448. (push note lines))
  12449. (when (or current-prefix-arg org-note-abort)
  12450. (when org-log-into-drawer
  12451. (org-remove-empty-drawer-at org-log-note-marker))
  12452. (setq lines nil))
  12453. (when lines
  12454. (with-current-buffer (marker-buffer org-log-note-marker)
  12455. (save-excursion
  12456. (goto-char org-log-note-marker)
  12457. (move-marker org-log-note-marker nil)
  12458. (end-of-line 1)
  12459. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12460. (setq ind (save-excursion
  12461. (if (ignore-errors (goto-char (org-in-item-p)))
  12462. (let ((struct (org-list-struct)))
  12463. (org-list-get-ind
  12464. (org-list-get-top-point struct) struct))
  12465. (skip-chars-backward " \r\t\n")
  12466. (cond
  12467. ((and (org-at-heading-p)
  12468. org-adapt-indentation)
  12469. (1+ (org-current-level)))
  12470. ((org-at-heading-p) 0)
  12471. (t (org-get-indentation))))))
  12472. (setq bul (org-list-bullet-string "-"))
  12473. (org-indent-line-to ind)
  12474. (insert bul (pop lines))
  12475. (let ((ind-body (+ (length bul) ind)))
  12476. (while lines
  12477. (insert "\n")
  12478. (org-indent-line-to ind-body)
  12479. (insert (pop lines))))
  12480. (message "Note stored")
  12481. (org-back-to-heading t)
  12482. (org-cycle-hide-drawers 'children))
  12483. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12484. ;; from within `org-add-log-note' because `buffer-undo-list'
  12485. ;; is then modified outside of `org-with-remote-undo'.
  12486. (when (eq this-command 'org-agenda-todo)
  12487. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12488. ;; Don't add undo information when called from `org-agenda-todo'
  12489. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12490. (set-window-configuration org-log-note-window-configuration)
  12491. (with-current-buffer (marker-buffer org-log-note-return-to)
  12492. (goto-char org-log-note-return-to))
  12493. (move-marker org-log-note-return-to nil)
  12494. (and org-log-post-message (message "%s" org-log-post-message))))
  12495. (defun org-remove-empty-drawer-at (pos)
  12496. "Remove an empty drawer at position POS.
  12497. POS may also be a marker."
  12498. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12499. (org-with-wide-buffer
  12500. (goto-char pos)
  12501. (let ((drawer (org-element-at-point)))
  12502. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12503. (not (org-element-property :contents-begin drawer)))
  12504. (delete-region (org-element-property :begin drawer)
  12505. (progn (goto-char (org-element-property :end drawer))
  12506. (skip-chars-backward " \r\t\n")
  12507. (forward-line)
  12508. (point))))))))
  12509. (defvar org-ts-type nil)
  12510. (defun org-sparse-tree (&optional arg type)
  12511. "Create a sparse tree, prompt for the details.
  12512. This command can create sparse trees. You first need to select the type
  12513. of match used to create the tree:
  12514. t Show all TODO entries.
  12515. T Show entries with a specific TODO keyword.
  12516. m Show entries selected by a tags/property match.
  12517. p Enter a property name and its value (both with completion on existing
  12518. names/values) and show entries with that property.
  12519. r Show entries matching a regular expression (`/' can be used as well).
  12520. b Show deadlines and scheduled items before a date.
  12521. a Show deadlines and scheduled items after a date.
  12522. d Show deadlines due within `org-deadline-warning-days'.
  12523. D Show deadlines and scheduled items between a date range."
  12524. (interactive "P")
  12525. (setq type (or type org-sparse-tree-default-date-type))
  12526. (setq org-ts-type type)
  12527. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12528. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12529. [c]ycle through date types: %s"
  12530. (case type
  12531. (all "all timestamps")
  12532. (scheduled "only scheduled")
  12533. (deadline "only deadline")
  12534. (active "only active timestamps")
  12535. (inactive "only inactive timestamps")
  12536. (scheduled-or-deadline "scheduled/deadline")
  12537. (closed "with a closed time-stamp")
  12538. (otherwise "scheduled/deadline")))
  12539. (let ((answer (read-char-exclusive)))
  12540. (case answer
  12541. (?c
  12542. (org-sparse-tree
  12543. arg
  12544. (cadr (memq type '(scheduled-or-deadline all scheduled deadline active
  12545. inactive closed)))))
  12546. (?d (call-interactively 'org-check-deadlines))
  12547. (?b (call-interactively 'org-check-before-date))
  12548. (?a (call-interactively 'org-check-after-date))
  12549. (?D (call-interactively 'org-check-dates-range))
  12550. (?t (call-interactively 'org-show-todo-tree))
  12551. (?T (org-show-todo-tree '(4)))
  12552. (?m (call-interactively 'org-match-sparse-tree))
  12553. ((?p ?P)
  12554. (let* ((kwd (org-icompleting-read
  12555. "Property: " (mapcar 'list (org-buffer-property-keys))))
  12556. (value (org-icompleting-read
  12557. "Value: " (mapcar 'list (org-property-values kwd)))))
  12558. (unless (string-match "\\`{.*}\\'" value)
  12559. (setq value (concat "\"" value "\"")))
  12560. (org-match-sparse-tree arg (concat kwd "=" value))))
  12561. ((?r ?R ?/) (call-interactively 'org-occur))
  12562. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12563. (defvar org-occur-highlights nil
  12564. "List of overlays used for occur matches.")
  12565. (make-variable-buffer-local 'org-occur-highlights)
  12566. (defvar org-occur-parameters nil
  12567. "Parameters of the active org-occur calls.
  12568. This is a list, each call to org-occur pushes as cons cell,
  12569. containing the regular expression and the callback, onto the list.
  12570. The list can contain several entries if `org-occur' has been called
  12571. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12572. will only contain one set of parameters. When the highlights are
  12573. removed (for example with `C-c C-c', or with the next edit (depending
  12574. on `org-remove-highlights-with-change'), this variable is emptied
  12575. as well.")
  12576. (make-variable-buffer-local 'org-occur-parameters)
  12577. (defun org-occur (regexp &optional keep-previous callback)
  12578. "Make a compact tree which shows all matches of REGEXP.
  12579. The tree will show the lines where the regexp matches, and all higher
  12580. headlines above the match. It will also show the heading after the match,
  12581. to make sure editing the matching entry is easy.
  12582. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12583. call to `org-occur' will be kept, to allow stacking of calls to this
  12584. command.
  12585. If CALLBACK is non-nil, it is a function which is called to confirm
  12586. that the match should indeed be shown."
  12587. (interactive "sRegexp: \nP")
  12588. (when (equal regexp "")
  12589. (user-error "Regexp cannot be empty"))
  12590. (unless keep-previous
  12591. (org-remove-occur-highlights nil nil t))
  12592. (push (cons regexp callback) org-occur-parameters)
  12593. (let ((cnt 0))
  12594. (save-excursion
  12595. (goto-char (point-min))
  12596. (if (or (not keep-previous) ; do not want to keep
  12597. (not org-occur-highlights)) ; no previous matches
  12598. ;; hide everything
  12599. (org-overview))
  12600. (while (re-search-forward regexp nil t)
  12601. (when (or (not callback)
  12602. (save-match-data (funcall callback)))
  12603. (setq cnt (1+ cnt))
  12604. (when org-highlight-sparse-tree-matches
  12605. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12606. (org-show-context 'occur-tree))))
  12607. (when org-remove-highlights-with-change
  12608. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12609. nil 'local))
  12610. (unless org-sparse-tree-open-archived-trees
  12611. (org-hide-archived-subtrees (point-min) (point-max)))
  12612. (run-hooks 'org-occur-hook)
  12613. (if (org-called-interactively-p 'interactive)
  12614. (message "%d match(es) for regexp %s" cnt regexp))
  12615. cnt))
  12616. (defun org-occur-next-match (&optional n reset)
  12617. "Function for `next-error-function' to find sparse tree matches.
  12618. N is the number of matches to move, when negative move backwards.
  12619. RESET is entirely ignored - this function always goes back to the
  12620. starting point when no match is found."
  12621. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12622. (search-func (if (< n 0)
  12623. 'previous-single-char-property-change
  12624. 'next-single-char-property-change))
  12625. (n (abs n))
  12626. (pos (point))
  12627. p1)
  12628. (catch 'exit
  12629. (while (setq p1 (funcall search-func (point) 'org-type))
  12630. (when (equal p1 limit)
  12631. (goto-char pos)
  12632. (user-error "No more matches"))
  12633. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12634. (setq n (1- n))
  12635. (when (= n 0)
  12636. (goto-char p1)
  12637. (throw 'exit (point))))
  12638. (goto-char p1))
  12639. (goto-char p1)
  12640. (user-error "No more matches"))))
  12641. (defun org-show-context (&optional key)
  12642. "Make sure point and context are visible.
  12643. How much context is shown depends upon the variables
  12644. `org-show-hierarchy-above', `org-show-following-heading',
  12645. `org-show-entry-below' and `org-show-siblings'."
  12646. (let ((heading-p (org-at-heading-p t))
  12647. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12648. (following-p (org-get-alist-option org-show-following-heading key))
  12649. (entry-p (org-get-alist-option org-show-entry-below key))
  12650. (siblings-p (org-get-alist-option org-show-siblings key)))
  12651. ;; Show heading or entry text
  12652. (if (and heading-p (not entry-p))
  12653. (org-flag-heading nil) ; only show the heading
  12654. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12655. (org-show-hidden-entry))) ; show entire entry
  12656. (when following-p
  12657. ;; Show next sibling, or heading below text
  12658. (save-excursion
  12659. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12660. (org-flag-heading nil))))
  12661. (when siblings-p (org-show-siblings))
  12662. (when hierarchy-p
  12663. ;; show all higher headings, possibly with siblings
  12664. (save-excursion
  12665. (while (and (ignore-errors (progn (org-up-heading-all 1) t))
  12666. (not (bobp)))
  12667. (org-flag-heading nil)
  12668. (when siblings-p (org-show-siblings)))))))
  12669. (defvar org-reveal-start-hook nil
  12670. "Hook run before revealing a location.")
  12671. (defun org-reveal (&optional siblings)
  12672. "Show current entry, hierarchy above it, and the following headline.
  12673. This can be used to show a consistent set of context around locations
  12674. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12675. not t for the search context.
  12676. With optional argument SIBLINGS, on each level of the hierarchy all
  12677. siblings are shown. This repairs the tree structure to what it would
  12678. look like when opened with hierarchical calls to `org-cycle'.
  12679. With double optional argument \\[universal-argument] \\[universal-argument], \
  12680. go to the parent and show the
  12681. entire tree."
  12682. (interactive "P")
  12683. (run-hooks 'org-reveal-start-hook)
  12684. (let ((org-show-hierarchy-above t)
  12685. (org-show-following-heading t)
  12686. (org-show-siblings (if siblings t org-show-siblings)))
  12687. (org-show-context nil))
  12688. (when (equal siblings '(16))
  12689. (save-excursion
  12690. (when (org-up-heading-safe)
  12691. (org-show-subtree)
  12692. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12693. (defun org-highlight-new-match (beg end)
  12694. "Highlight from BEG to END and mark the highlight is an occur headline."
  12695. (let ((ov (make-overlay beg end)))
  12696. (overlay-put ov 'face 'secondary-selection)
  12697. (overlay-put ov 'org-type 'org-occur)
  12698. (push ov org-occur-highlights)))
  12699. (defun org-remove-occur-highlights (&optional beg end noremove)
  12700. "Remove the occur highlights from the buffer.
  12701. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12702. from the `before-change-functions' in the current buffer."
  12703. (interactive)
  12704. (unless org-inhibit-highlight-removal
  12705. (mapc 'delete-overlay org-occur-highlights)
  12706. (setq org-occur-highlights nil)
  12707. (setq org-occur-parameters nil)
  12708. (unless noremove
  12709. (remove-hook 'before-change-functions
  12710. 'org-remove-occur-highlights 'local))))
  12711. ;;;; Priorities
  12712. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12713. "Regular expression matching the priority indicator.")
  12714. (defvar org-remove-priority-next-time nil)
  12715. (defun org-priority-up ()
  12716. "Increase the priority of the current item."
  12717. (interactive)
  12718. (org-priority 'up))
  12719. (defun org-priority-down ()
  12720. "Decrease the priority of the current item."
  12721. (interactive)
  12722. (org-priority 'down))
  12723. (defun org-priority (&optional action show)
  12724. "Change the priority of an item.
  12725. ACTION can be `set', `up', `down', or a character."
  12726. (interactive "P")
  12727. (if (equal action '(4))
  12728. (org-show-priority)
  12729. (unless org-enable-priority-commands
  12730. (user-error "Priority commands are disabled"))
  12731. (setq action (or action 'set))
  12732. (let (current new news have remove)
  12733. (save-excursion
  12734. (org-back-to-heading t)
  12735. (if (looking-at org-priority-regexp)
  12736. (setq current (string-to-char (match-string 2))
  12737. have t))
  12738. (cond
  12739. ((eq action 'remove)
  12740. (setq remove t new ?\ ))
  12741. ((or (eq action 'set)
  12742. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12743. (if (not (eq action 'set))
  12744. (setq new action)
  12745. (message "Priority %c-%c, SPC to remove: "
  12746. org-highest-priority org-lowest-priority)
  12747. (save-match-data
  12748. (setq new (read-char-exclusive))))
  12749. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12750. (= (upcase org-lowest-priority) org-lowest-priority))
  12751. (setq new (upcase new)))
  12752. (cond ((equal new ?\ ) (setq remove t))
  12753. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12754. (user-error "Priority must be between `%c' and `%c'"
  12755. org-highest-priority org-lowest-priority))))
  12756. ((eq action 'up)
  12757. (setq new (if have
  12758. (1- current) ; normal cycling
  12759. ;; last priority was empty
  12760. (if (eq last-command this-command)
  12761. org-lowest-priority ; wrap around empty to lowest
  12762. ;; default
  12763. (if org-priority-start-cycle-with-default
  12764. org-default-priority
  12765. (1- org-default-priority))))))
  12766. ((eq action 'down)
  12767. (setq new (if have
  12768. (1+ current) ; normal cycling
  12769. ;; last priority was empty
  12770. (if (eq last-command this-command)
  12771. org-highest-priority ; wrap around empty to highest
  12772. ;; default
  12773. (if org-priority-start-cycle-with-default
  12774. org-default-priority
  12775. (1+ org-default-priority))))))
  12776. (t (user-error "Invalid action")))
  12777. (if (or (< (upcase new) org-highest-priority)
  12778. (> (upcase new) org-lowest-priority))
  12779. (if (and (memq action '(up down))
  12780. (not have) (not (eq last-command this-command)))
  12781. ;; `new' is from default priority
  12782. (error
  12783. "The default can not be set, see `org-default-priority' why")
  12784. ;; normal cycling: `new' is beyond highest/lowest priority
  12785. ;; and is wrapped around to the empty priority
  12786. (setq remove t)))
  12787. (setq news (format "%c" new))
  12788. (if have
  12789. (if remove
  12790. (replace-match "" t t nil 1)
  12791. (replace-match news t t nil 2))
  12792. (if remove
  12793. (user-error "No priority cookie found in line")
  12794. (let ((case-fold-search nil))
  12795. (looking-at org-todo-line-regexp))
  12796. (if (match-end 2)
  12797. (progn
  12798. (goto-char (match-end 2))
  12799. (insert " [#" news "]"))
  12800. (goto-char (match-beginning 3))
  12801. (insert "[#" news "] "))))
  12802. (org-set-tags nil 'align))
  12803. (if remove
  12804. (message "Priority removed")
  12805. (message "Priority of current item set to %s" news)))))
  12806. (defun org-show-priority ()
  12807. "Show the priority of the current item.
  12808. This priority is composed of the main priority given with the [#A] cookies,
  12809. and by additional input from the age of a schedules or deadline entry."
  12810. (interactive)
  12811. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12812. (org-get-at-bol 'priority)
  12813. (save-excursion
  12814. (save-match-data
  12815. (beginning-of-line)
  12816. (and (looking-at org-heading-regexp)
  12817. (org-get-priority (match-string 0))))))))
  12818. (message "Priority is %d" (if pri pri -1000))))
  12819. (defun org-get-priority (s)
  12820. "Find priority cookie and return priority."
  12821. (save-match-data
  12822. (if (functionp org-get-priority-function)
  12823. (funcall org-get-priority-function)
  12824. (if (not (string-match org-priority-regexp s))
  12825. (* 1000 (- org-lowest-priority org-default-priority))
  12826. (* 1000 (- org-lowest-priority
  12827. (string-to-char (match-string 2 s))))))))
  12828. ;;;; Tags
  12829. (defvar org-agenda-archives-mode)
  12830. (defvar org-map-continue-from nil
  12831. "Position from where mapping should continue.
  12832. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12833. (defvar org-scanner-tags nil
  12834. "The current tag list while the tags scanner is running.")
  12835. (defvar org-trust-scanner-tags nil
  12836. "Should `org-get-tags-at' use the tags for the scanner.
  12837. This is for internal dynamical scoping only.
  12838. When this is non-nil, the function `org-get-tags-at' will return the value
  12839. of `org-scanner-tags' instead of building the list by itself. This
  12840. can lead to large speed-ups when the tags scanner is used in a file with
  12841. many entries, and when the list of tags is retrieved, for example to
  12842. obtain a list of properties. Building the tags list for each entry in such
  12843. a file becomes an N^2 operation - but with this variable set, it scales
  12844. as N.")
  12845. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12846. "Scan headline tags with inheritance and produce output ACTION.
  12847. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12848. or `agenda' to produce an entry list for an agenda view. It can also be
  12849. a Lisp form or a function that should be called at each matched headline, in
  12850. this case the return value is a list of all return values from these calls.
  12851. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12852. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12853. only lines with a not-done TODO keyword are included in the output.
  12854. This should be the same variable that was scoped into
  12855. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12856. START-LEVEL can be a string with asterisks, reducing the scope to
  12857. headlines matching this string."
  12858. (require 'org-agenda)
  12859. (let* ((re (concat "^"
  12860. (if start-level
  12861. ;; Get the correct level to match
  12862. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12863. org-outline-regexp)
  12864. " *\\(\\<\\("
  12865. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12866. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12867. (props (list 'face 'default
  12868. 'done-face 'org-agenda-done
  12869. 'undone-face 'default
  12870. 'mouse-face 'highlight
  12871. 'org-not-done-regexp org-not-done-regexp
  12872. 'org-todo-regexp org-todo-regexp
  12873. 'org-complex-heading-regexp org-complex-heading-regexp
  12874. 'help-echo
  12875. (format "mouse-2 or RET jump to org file %s"
  12876. (abbreviate-file-name
  12877. (or (buffer-file-name (buffer-base-buffer))
  12878. (buffer-name (buffer-base-buffer)))))))
  12879. (org-map-continue-from nil)
  12880. lspos tags tags-list
  12881. (tags-alist (list (cons 0 org-file-tags)))
  12882. (llast 0) rtn rtn1 level category i txt
  12883. todo marker entry priority)
  12884. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12885. (setq action (list 'lambda nil action)))
  12886. (save-excursion
  12887. (goto-char (point-min))
  12888. (when (eq action 'sparse-tree)
  12889. (org-overview)
  12890. (org-remove-occur-highlights))
  12891. (while (let (case-fold-search)
  12892. (re-search-forward re nil t))
  12893. (setq org-map-continue-from nil)
  12894. (catch :skip
  12895. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12896. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12897. (goto-char (setq lspos (match-beginning 0)))
  12898. (setq level (org-reduced-level (org-outline-level))
  12899. category (org-get-category))
  12900. (setq i llast llast level)
  12901. ;; remove tag lists from same and sublevels
  12902. (while (>= i level)
  12903. (when (setq entry (assoc i tags-alist))
  12904. (setq tags-alist (delete entry tags-alist)))
  12905. (setq i (1- i)))
  12906. ;; add the next tags
  12907. (when tags
  12908. (setq tags (org-split-string tags ":")
  12909. tags-alist
  12910. (cons (cons level tags) tags-alist)))
  12911. ;; compile tags for current headline
  12912. (setq tags-list
  12913. (if org-use-tag-inheritance
  12914. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12915. tags)
  12916. org-scanner-tags tags-list)
  12917. (when org-use-tag-inheritance
  12918. (setcdr (car tags-alist)
  12919. (mapcar (lambda (x)
  12920. (setq x (copy-sequence x))
  12921. (org-add-prop-inherited x))
  12922. (cdar tags-alist))))
  12923. (when (and tags org-use-tag-inheritance
  12924. (or (not (eq t org-use-tag-inheritance))
  12925. org-tags-exclude-from-inheritance))
  12926. ;; selective inheritance, remove uninherited ones
  12927. (setcdr (car tags-alist)
  12928. (org-remove-uninherited-tags (cdar tags-alist))))
  12929. (when (and
  12930. ;; eval matcher only when the todo condition is OK
  12931. (and (or (not todo-only) (member todo org-not-done-keywords))
  12932. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12933. (eval matcher)))
  12934. ;; Call the skipper, but return t if it does not skip,
  12935. ;; so that the `and' form continues evaluating
  12936. (progn
  12937. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12938. t)
  12939. ;; Check if timestamps are deselecting this entry
  12940. (or (not todo-only)
  12941. (and (member todo org-not-done-keywords)
  12942. (or (not org-agenda-tags-todo-honor-ignore-options)
  12943. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12944. ;; select this headline
  12945. (cond
  12946. ((eq action 'sparse-tree)
  12947. (and org-highlight-sparse-tree-matches
  12948. (org-get-heading) (match-end 0)
  12949. (org-highlight-new-match
  12950. (match-beginning 1) (match-end 1)))
  12951. (org-show-context 'tags-tree))
  12952. ((eq action 'agenda)
  12953. (setq txt (org-agenda-format-item
  12954. ""
  12955. (concat
  12956. (if (eq org-tags-match-list-sublevels 'indented)
  12957. (make-string (1- level) ?.) "")
  12958. (org-get-heading))
  12959. level category
  12960. tags-list)
  12961. priority (org-get-priority txt))
  12962. (goto-char lspos)
  12963. (setq marker (org-agenda-new-marker))
  12964. (org-add-props txt props
  12965. 'org-marker marker 'org-hd-marker marker 'org-category category
  12966. 'todo-state todo
  12967. 'priority priority 'type "tagsmatch")
  12968. (push txt rtn))
  12969. ((functionp action)
  12970. (setq org-map-continue-from nil)
  12971. (save-excursion
  12972. (setq rtn1 (funcall action))
  12973. (push rtn1 rtn)))
  12974. (t (user-error "Invalid action")))
  12975. ;; if we are to skip sublevels, jump to end of subtree
  12976. (unless org-tags-match-list-sublevels
  12977. (org-end-of-subtree t)
  12978. (backward-char 1))))
  12979. ;; Get the correct position from where to continue
  12980. (if org-map-continue-from
  12981. (goto-char org-map-continue-from)
  12982. (and (= (point) lspos) (end-of-line 1)))))
  12983. (when (and (eq action 'sparse-tree)
  12984. (not org-sparse-tree-open-archived-trees))
  12985. (org-hide-archived-subtrees (point-min) (point-max)))
  12986. (nreverse rtn)))
  12987. (defun org-remove-uninherited-tags (tags)
  12988. "Remove all tags that are not inherited from the list TAGS."
  12989. (cond
  12990. ((eq org-use-tag-inheritance t)
  12991. (if org-tags-exclude-from-inheritance
  12992. (org-delete-all org-tags-exclude-from-inheritance tags)
  12993. tags))
  12994. ((not org-use-tag-inheritance) nil)
  12995. ((stringp org-use-tag-inheritance)
  12996. (delq nil (mapcar
  12997. (lambda (x)
  12998. (if (and (string-match org-use-tag-inheritance x)
  12999. (not (member x org-tags-exclude-from-inheritance)))
  13000. x nil))
  13001. tags)))
  13002. ((listp org-use-tag-inheritance)
  13003. (delq nil (mapcar
  13004. (lambda (x)
  13005. (if (member x org-use-tag-inheritance) x nil))
  13006. tags)))))
  13007. (defun org-match-sparse-tree (&optional todo-only match)
  13008. "Create a sparse tree according to tags string MATCH.
  13009. MATCH can contain positive and negative selection of tags, like
  13010. \"+WORK+URGENT-WITHBOSS\".
  13011. If optional argument TODO-ONLY is non-nil, only select lines that are
  13012. also TODO lines."
  13013. (interactive "P")
  13014. (org-agenda-prepare-buffers (list (current-buffer)))
  13015. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13016. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13017. (defvar org-cached-props nil)
  13018. (defun org-cached-entry-get (pom property)
  13019. (if (or (eq t org-use-property-inheritance)
  13020. (and (stringp org-use-property-inheritance)
  13021. (string-match org-use-property-inheritance property))
  13022. (and (listp org-use-property-inheritance)
  13023. (member property org-use-property-inheritance)))
  13024. ;; Caching is not possible, check it directly
  13025. (org-entry-get pom property 'inherit)
  13026. ;; Get all properties, so that we can do complicated checks easily
  13027. (cdr (assoc property (or org-cached-props
  13028. (setq org-cached-props
  13029. (org-entry-properties pom)))))))
  13030. (defun org-global-tags-completion-table (&optional files)
  13031. "Return the list of all tags in all agenda buffer/files.
  13032. Optional FILES argument is a list of files which can be used
  13033. instead of the agenda files."
  13034. (save-excursion
  13035. (org-uniquify
  13036. (delq nil
  13037. (apply 'append
  13038. (mapcar
  13039. (lambda (file)
  13040. (set-buffer (find-file-noselect file))
  13041. (append (org-get-buffer-tags)
  13042. (mapcar (lambda (x) (if (stringp (car-safe x))
  13043. (list (car-safe x)) nil))
  13044. org-tag-alist)))
  13045. (if (and files (car files))
  13046. files
  13047. (org-agenda-files))))))))
  13048. (defun org-make-tags-matcher (match)
  13049. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13050. The variable `todo-only' is scoped dynamically into this function.
  13051. It will be set to t if the matcher restricts matching to TODO entries,
  13052. otherwise will not be touched.
  13053. Returns a cons of the selection string MATCH and the constructed
  13054. lisp form implementing the matcher. The matcher is to be evaluated
  13055. at an Org entry, with point on the headline, and returns t if the
  13056. entry matches the selection string MATCH. The returned lisp form
  13057. references two variables with information about the entry, which
  13058. must be bound around the form's evaluation: todo, the TODO keyword
  13059. at the entry (or nil of none); and tags-list, the list of all tags
  13060. at the entry including inherited ones. Additionally, the category
  13061. of the entry (if any) must be specified as the text property
  13062. 'org-category on the headline.
  13063. See also `org-scan-tags'.
  13064. "
  13065. (declare (special todo-only))
  13066. (unless (boundp 'todo-only)
  13067. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  13068. (unless match
  13069. ;; Get a new match request, with completion against the global
  13070. ;; tags table and the local tags in current buffer
  13071. (let ((org-last-tags-completion-table
  13072. (org-uniquify
  13073. (delq nil (append (org-get-buffer-tags)
  13074. (org-global-tags-completion-table))))))
  13075. (setq match (org-completing-read-no-i
  13076. "Match: " 'org-tags-completion-function nil nil nil
  13077. 'org-tags-history))))
  13078. ;; Parse the string and create a lisp form
  13079. (let ((match0 match)
  13080. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13081. minus tag mm
  13082. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13083. orterms term orlist re-p str-p level-p level-op time-p
  13084. prop-p pn pv po gv rest (start 0) (ss 0))
  13085. ;; Expand group tags
  13086. (setq match (org-tags-expand match))
  13087. ;; Check if there is a TODO part of this match, which would be the
  13088. ;; part after a "/". TO make sure that this slash is not part of
  13089. ;; a property value to be matched against, we also check that there
  13090. ;; is no " after that slash.
  13091. ;; First, find the last slash
  13092. (while (string-match "/+" match ss)
  13093. (setq start (match-beginning 0) ss (match-end 0)))
  13094. (if (and (string-match "/+" match start)
  13095. (not (save-match-data (string-match "\"" match start))))
  13096. ;; match contains also a todo-matching request
  13097. (progn
  13098. (setq tagsmatch (substring match 0 (match-beginning 0))
  13099. todomatch (substring match (match-end 0)))
  13100. (if (string-match "^!" todomatch)
  13101. (setq todo-only t todomatch (substring todomatch 1)))
  13102. (if (string-match "^\\s-*$" todomatch)
  13103. (setq todomatch nil)))
  13104. ;; only matching tags
  13105. (setq tagsmatch match todomatch nil))
  13106. ;; Make the tags matcher
  13107. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13108. (setq tagsmatcher t)
  13109. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13110. (while (setq term (pop orterms))
  13111. (while (and (equal (substring term -1) "\\") orterms)
  13112. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13113. (while (string-match re term)
  13114. (setq rest (substring term (match-end 0))
  13115. minus (and (match-end 1)
  13116. (equal (match-string 1 term) "-"))
  13117. tag (save-match-data (replace-regexp-in-string
  13118. "\\\\-" "-"
  13119. (match-string 2 term)))
  13120. re-p (equal (string-to-char tag) ?{)
  13121. level-p (match-end 4)
  13122. prop-p (match-end 5)
  13123. mm (cond
  13124. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13125. (level-p
  13126. (setq level-op (org-op-to-function (match-string 3 term)))
  13127. `(,level-op level ,(string-to-number
  13128. (match-string 4 term))))
  13129. (prop-p
  13130. (setq pn (match-string 5 term)
  13131. po (match-string 6 term)
  13132. pv (match-string 7 term)
  13133. re-p (equal (string-to-char pv) ?{)
  13134. str-p (equal (string-to-char pv) ?\")
  13135. time-p (save-match-data
  13136. (string-match "^\"[[<].*[]>]\"$" pv))
  13137. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  13138. (if time-p (setq pv (org-matcher-time pv)))
  13139. (setq po (org-op-to-function po (if time-p 'time str-p)))
  13140. (cond
  13141. ((equal pn "CATEGORY")
  13142. (setq gv '(get-text-property (point) 'org-category)))
  13143. ((equal pn "TODO")
  13144. (setq gv 'todo))
  13145. (t
  13146. (setq gv `(org-cached-entry-get nil ,pn))))
  13147. (if re-p
  13148. (if (eq po 'org<>)
  13149. `(not (string-match ,pv (or ,gv "")))
  13150. `(string-match ,pv (or ,gv "")))
  13151. (if str-p
  13152. `(,po (or ,gv "") ,pv)
  13153. `(,po (string-to-number (or ,gv ""))
  13154. ,(string-to-number pv) ))))
  13155. (t `(member ,tag tags-list)))
  13156. mm (if minus (list 'not mm) mm)
  13157. term rest)
  13158. (push mm tagsmatcher))
  13159. (push (if (> (length tagsmatcher) 1)
  13160. (cons 'and tagsmatcher)
  13161. (car tagsmatcher))
  13162. orlist)
  13163. (setq tagsmatcher nil))
  13164. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13165. (setq tagsmatcher
  13166. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13167. ;; Make the todo matcher
  13168. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13169. (setq todomatcher t)
  13170. (setq orterms (org-split-string todomatch "|") orlist nil)
  13171. (while (setq term (pop orterms))
  13172. (while (string-match re term)
  13173. (setq minus (and (match-end 1)
  13174. (equal (match-string 1 term) "-"))
  13175. kwd (match-string 2 term)
  13176. re-p (equal (string-to-char kwd) ?{)
  13177. term (substring term (match-end 0))
  13178. mm (if re-p
  13179. `(string-match ,(substring kwd 1 -1) todo)
  13180. (list 'equal 'todo kwd))
  13181. mm (if minus (list 'not mm) mm))
  13182. (push mm todomatcher))
  13183. (push (if (> (length todomatcher) 1)
  13184. (cons 'and todomatcher)
  13185. (car todomatcher))
  13186. orlist)
  13187. (setq todomatcher nil))
  13188. (setq todomatcher (if (> (length orlist) 1)
  13189. (cons 'or orlist) (car orlist))))
  13190. ;; Return the string and lisp forms of the matcher
  13191. (setq matcher (if todomatcher
  13192. (list 'and tagsmatcher todomatcher)
  13193. tagsmatcher))
  13194. (when todo-only
  13195. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13196. matcher)))
  13197. (cons match0 matcher)))
  13198. (defun org-tags-expand (match &optional single-as-list downcased)
  13199. "Expand group tags in MATCH.
  13200. This replaces every group tag in MATCH with a regexp tag search.
  13201. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13202. will be replaced like this:
  13203. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  13204. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  13205. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  13206. Replacing by a regexp preserves the structure of the match.
  13207. E.g., this expansion
  13208. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13209. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13210. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13211. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13212. assumed to be a single group tag, and the function will return
  13213. the list of tags in this group.
  13214. When DOWNCASE is non-nil, expand downcased TAGS."
  13215. (if org-group-tags
  13216. (let* ((case-fold-search t)
  13217. (stable org-mode-syntax-table)
  13218. (tal (or org-tag-groups-alist-for-agenda
  13219. org-tag-groups-alist))
  13220. (tal (if downcased
  13221. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  13222. (tml (mapcar 'car tal))
  13223. (rtnmatch match) rpl)
  13224. ;; @ and _ are allowed as word-components in tags
  13225. (modify-syntax-entry ?@ "w" stable)
  13226. (modify-syntax-entry ?_ "w" stable)
  13227. (while (and tml
  13228. (with-syntax-table stable
  13229. (string-match
  13230. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13231. (regexp-opt tml) "\\>\\)") rtnmatch)))
  13232. (let* ((dir (match-string 1 rtnmatch))
  13233. (tag (match-string 2 rtnmatch))
  13234. (tag (if downcased (downcase tag) tag)))
  13235. (setq tml (delete tag tml))
  13236. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  13237. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  13238. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  13239. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  13240. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  13241. (if single-as-list
  13242. (or (reverse rpl) (list rtnmatch))
  13243. rtnmatch))
  13244. (if single-as-list (list (if downcased (downcase match) match))
  13245. match)))
  13246. (defun org-op-to-function (op &optional stringp)
  13247. "Turn an operator into the appropriate function."
  13248. (setq op
  13249. (cond
  13250. ((equal op "<" ) '(< string< org-time<))
  13251. ((equal op ">" ) '(> org-string> org-time>))
  13252. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13253. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13254. ((member op '("=" "==")) '(= string= org-time=))
  13255. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13256. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13257. (defun org<> (a b) (not (= a b)))
  13258. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13259. (defun org-string>= (a b) (not (string< a b)))
  13260. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13261. (defun org-string<> (a b) (not (string= a b)))
  13262. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13263. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13264. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13265. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13266. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13267. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13268. (defun org-2ft (s)
  13269. "Convert S to a floating point time.
  13270. If S is already a number, just return it. If it is a string, parse
  13271. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13272. (cond
  13273. ((numberp s) s)
  13274. ((stringp s)
  13275. (condition-case nil
  13276. (float-time (apply 'encode-time (org-parse-time-string s)))
  13277. (error 0.)))
  13278. (t 0.)))
  13279. (defun org-time-today ()
  13280. "Time in seconds today at 0:00.
  13281. Returns the float number of seconds since the beginning of the
  13282. epoch to the beginning of today (00:00)."
  13283. (float-time (apply 'encode-time
  13284. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13285. (defun org-matcher-time (s)
  13286. "Interpret a time comparison value."
  13287. (save-match-data
  13288. (cond
  13289. ((string= s "<now>") (float-time))
  13290. ((string= s "<today>") (org-time-today))
  13291. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13292. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13293. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13294. (+ (org-time-today)
  13295. (* (string-to-number (match-string 1 s))
  13296. (cdr (assoc (match-string 2 s)
  13297. '(("d" . 86400.0) ("w" . 604800.0)
  13298. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13299. (t (org-2ft s)))))
  13300. (defun org-match-any-p (re list)
  13301. "Does re match any element of list?"
  13302. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13303. (delq nil list))
  13304. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13305. (defvar org-tags-overlay (make-overlay 1 1))
  13306. (org-detach-overlay org-tags-overlay)
  13307. (defun org-get-local-tags-at (&optional pos)
  13308. "Get a list of tags defined in the current headline."
  13309. (org-get-tags-at pos 'local))
  13310. (defun org-get-local-tags ()
  13311. "Get a list of tags defined in the current headline."
  13312. (org-get-tags-at nil 'local))
  13313. (defun org-get-tags-at (&optional pos local)
  13314. "Get a list of all headline tags applicable at POS.
  13315. POS defaults to point. If tags are inherited, the list contains
  13316. the targets in the same sequence as the headlines appear, i.e.
  13317. the tags of the current headline come last.
  13318. When LOCAL is non-nil, only return tags from the current headline,
  13319. ignore inherited ones."
  13320. (interactive)
  13321. (if (and org-trust-scanner-tags
  13322. (or (not pos) (equal pos (point)))
  13323. (not local))
  13324. org-scanner-tags
  13325. (let (tags ltags lastpos parent)
  13326. (save-excursion
  13327. (save-restriction
  13328. (widen)
  13329. (goto-char (or pos (point)))
  13330. (save-match-data
  13331. (catch 'done
  13332. (condition-case nil
  13333. (progn
  13334. (org-back-to-heading t)
  13335. (while (not (equal lastpos (point)))
  13336. (setq lastpos (point))
  13337. (when (looking-at
  13338. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13339. (setq ltags (org-split-string
  13340. (org-match-string-no-properties 1) ":"))
  13341. (when parent
  13342. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13343. (setq tags (append
  13344. (if parent
  13345. (org-remove-uninherited-tags ltags)
  13346. ltags)
  13347. tags)))
  13348. (or org-use-tag-inheritance (throw 'done t))
  13349. (if local (throw 'done t))
  13350. (or (org-up-heading-safe) (error nil))
  13351. (setq parent t)))
  13352. (error nil)))))
  13353. (if local
  13354. tags
  13355. (reverse (delete-dups
  13356. (reverse (append
  13357. (org-remove-uninherited-tags
  13358. org-file-tags) tags)))))))))
  13359. (defun org-add-prop-inherited (s)
  13360. (add-text-properties 0 (length s) '(inherited t) s)
  13361. s)
  13362. (defun org-toggle-tag (tag &optional onoff)
  13363. "Toggle the tag TAG for the current line.
  13364. If ONOFF is `on' or `off', don't toggle but set to this state."
  13365. (let (res current)
  13366. (save-excursion
  13367. (org-back-to-heading t)
  13368. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13369. (point-at-eol) t)
  13370. (progn
  13371. (setq current (match-string 1))
  13372. (replace-match ""))
  13373. (setq current ""))
  13374. (setq current (nreverse (org-split-string current ":")))
  13375. (cond
  13376. ((eq onoff 'on)
  13377. (setq res t)
  13378. (or (member tag current) (push tag current)))
  13379. ((eq onoff 'off)
  13380. (or (not (member tag current)) (setq current (delete tag current))))
  13381. (t (if (member tag current)
  13382. (setq current (delete tag current))
  13383. (setq res t)
  13384. (push tag current))))
  13385. (end-of-line 1)
  13386. (if current
  13387. (progn
  13388. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13389. (org-set-tags nil t))
  13390. (delete-horizontal-space))
  13391. (run-hooks 'org-after-tags-change-hook))
  13392. res))
  13393. (defun org-align-tags-here (to-col)
  13394. ;; Assumes that this is a headline
  13395. "Align tags on the current headline to TO-COL."
  13396. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13397. (beginning-of-line 1)
  13398. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13399. (< pos (match-beginning 2)))
  13400. (progn
  13401. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13402. (goto-char (match-beginning 1))
  13403. (insert " ")
  13404. (delete-region (point) (1+ (match-beginning 2)))
  13405. (setq ncol (max (current-column)
  13406. (1+ col)
  13407. (if (> to-col 0)
  13408. to-col
  13409. (- (abs to-col) tags-l))))
  13410. (setq p (point))
  13411. (insert (make-string (- ncol (current-column)) ?\ ))
  13412. (setq ncol (current-column))
  13413. (when indent-tabs-mode (tabify p (point-at-eol)))
  13414. (org-move-to-column (min ncol col)))
  13415. (goto-char pos))))
  13416. (defun org-set-tags-command (&optional arg just-align)
  13417. "Call the set-tags command for the current entry."
  13418. (interactive "P")
  13419. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13420. (org-set-tags arg just-align)
  13421. (save-excursion
  13422. (unless (and (org-region-active-p)
  13423. org-loop-over-headlines-in-active-region)
  13424. (org-back-to-heading t))
  13425. (org-set-tags arg just-align))))
  13426. (defun org-set-tags-to (data)
  13427. "Set the tags of the current entry to DATA, replacing the current tags.
  13428. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13429. If DATA is nil or the empty string, any tags will be removed."
  13430. (interactive "sTags: ")
  13431. (setq data
  13432. (cond
  13433. ((eq data nil) "")
  13434. ((equal data "") "")
  13435. ((stringp data)
  13436. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13437. ":"))
  13438. ((listp data)
  13439. (concat ":" (mapconcat 'identity data ":") ":"))))
  13440. (when data
  13441. (save-excursion
  13442. (org-back-to-heading t)
  13443. (when (looking-at org-complex-heading-regexp)
  13444. (if (match-end 5)
  13445. (progn
  13446. (goto-char (match-beginning 5))
  13447. (insert data)
  13448. (delete-region (point) (point-at-eol))
  13449. (org-set-tags nil 'align))
  13450. (goto-char (point-at-eol))
  13451. (insert " " data)
  13452. (org-set-tags nil 'align)))
  13453. (beginning-of-line 1)
  13454. (if (looking-at ".*?\\([ \t]+\\)$")
  13455. (delete-region (match-beginning 1) (match-end 1))))))
  13456. (defun org-align-all-tags ()
  13457. "Align the tags i all headings."
  13458. (interactive)
  13459. (save-excursion
  13460. (or (ignore-errors (org-back-to-heading t))
  13461. (outline-next-heading))
  13462. (if (org-at-heading-p)
  13463. (org-set-tags t)
  13464. (message "No headings"))))
  13465. (defvar org-indent-indentation-per-level)
  13466. (defun org-set-tags (&optional arg just-align)
  13467. "Set the tags for the current headline.
  13468. With prefix ARG, realign all tags in headings in the current buffer.
  13469. When JUST-ALIGN is non-nil, only align tags."
  13470. (interactive "P")
  13471. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13472. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13473. 'region-start-level 'region))
  13474. org-loop-over-headlines-in-active-region)
  13475. (org-map-entries
  13476. ;; We don't use ARG and JUST-ALIGN here because these args
  13477. ;; are not useful when looping over headlines.
  13478. `(org-set-tags)
  13479. org-loop-over-headlines-in-active-region
  13480. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13481. (let* ((re org-outline-regexp-bol)
  13482. (current (unless arg (org-get-tags-string)))
  13483. (col (current-column))
  13484. (org-setting-tags t)
  13485. table current-tags inherited-tags ; computed below when needed
  13486. tags p0 c0 c1 rpl di tc level)
  13487. (if arg
  13488. (save-excursion
  13489. (goto-char (point-min))
  13490. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13491. (while (re-search-forward re nil t)
  13492. (org-set-tags nil t)
  13493. (end-of-line 1)))
  13494. (message "All tags realigned to column %d" org-tags-column))
  13495. (if just-align
  13496. (setq tags current)
  13497. ;; Get a new set of tags from the user
  13498. (save-excursion
  13499. (setq table (append org-tag-persistent-alist
  13500. (or org-tag-alist (org-get-buffer-tags))
  13501. (and
  13502. org-complete-tags-always-offer-all-agenda-tags
  13503. (org-global-tags-completion-table
  13504. (org-agenda-files))))
  13505. org-last-tags-completion-table table
  13506. current-tags (org-split-string current ":")
  13507. inherited-tags (nreverse
  13508. (nthcdr (length current-tags)
  13509. (nreverse (org-get-tags-at))))
  13510. tags
  13511. (if (or (eq t org-use-fast-tag-selection)
  13512. (and org-use-fast-tag-selection
  13513. (delq nil (mapcar 'cdr table))))
  13514. (org-fast-tag-selection
  13515. current-tags inherited-tags table
  13516. (if org-fast-tag-selection-include-todo
  13517. org-todo-key-alist))
  13518. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13519. (org-trim
  13520. (org-icompleting-read "Tags: "
  13521. 'org-tags-completion-function
  13522. nil nil current 'org-tags-history))))))
  13523. (while (string-match "[-+&]+" tags)
  13524. ;; No boolean logic, just a list
  13525. (setq tags (replace-match ":" t t tags))))
  13526. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13527. (if org-tags-sort-function
  13528. (setq tags (mapconcat 'identity
  13529. (sort (org-split-string
  13530. tags (org-re "[^[:alnum:]_@#%]+"))
  13531. org-tags-sort-function) ":")))
  13532. (if (string-match "\\`[\t ]*\\'" tags)
  13533. (setq tags "")
  13534. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13535. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13536. ;; Insert new tags at the correct column
  13537. (beginning-of-line 1)
  13538. (setq level (or (and (looking-at org-outline-regexp)
  13539. (- (match-end 0) (point) 1))
  13540. 1))
  13541. (cond
  13542. ((and (equal current "") (equal tags "")))
  13543. ((re-search-forward
  13544. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13545. (point-at-eol) t)
  13546. (if (equal tags "")
  13547. (setq rpl "")
  13548. (goto-char (match-beginning 0))
  13549. (setq c0 (current-column)
  13550. ;; compute offset for the case of org-indent-mode active
  13551. di (if (org-bound-and-true-p org-indent-mode)
  13552. (* (1- org-indent-indentation-per-level) (1- level))
  13553. 0)
  13554. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13555. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13556. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (string-width tags))))
  13557. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13558. (replace-match rpl t t)
  13559. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13560. tags)
  13561. (t (error "Tags alignment failed")))
  13562. (org-move-to-column col)
  13563. (unless just-align
  13564. (run-hooks 'org-after-tags-change-hook))))))
  13565. (defun org-change-tag-in-region (beg end tag off)
  13566. "Add or remove TAG for each entry in the region.
  13567. This works in the agenda, and also in an org-mode buffer."
  13568. (interactive
  13569. (list (region-beginning) (region-end)
  13570. (let ((org-last-tags-completion-table
  13571. (if (derived-mode-p 'org-mode)
  13572. (org-uniquify
  13573. (delq nil (append (org-get-buffer-tags)
  13574. (org-global-tags-completion-table))))
  13575. (org-global-tags-completion-table))))
  13576. (org-icompleting-read
  13577. "Tag: " 'org-tags-completion-function nil nil nil
  13578. 'org-tags-history))
  13579. (progn
  13580. (message "[s]et or [r]emove? ")
  13581. (equal (read-char-exclusive) ?r))))
  13582. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13583. (let ((agendap (equal major-mode 'org-agenda-mode))
  13584. l1 l2 m buf pos newhead (cnt 0))
  13585. (goto-char end)
  13586. (setq l2 (1- (org-current-line)))
  13587. (goto-char beg)
  13588. (setq l1 (org-current-line))
  13589. (loop for l from l1 to l2 do
  13590. (org-goto-line l)
  13591. (setq m (get-text-property (point) 'org-hd-marker))
  13592. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13593. (and agendap m))
  13594. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13595. pos (if agendap m (point)))
  13596. (with-current-buffer buf
  13597. (save-excursion
  13598. (save-restriction
  13599. (goto-char pos)
  13600. (setq cnt (1+ cnt))
  13601. (org-toggle-tag tag (if off 'off 'on))
  13602. (setq newhead (org-get-heading)))))
  13603. (and agendap (org-agenda-change-all-lines newhead m))))
  13604. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13605. (defun org-tags-completion-function (string predicate &optional flag)
  13606. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13607. (confirm (lambda (x) (stringp (car x)))))
  13608. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13609. (setq s1 (match-string 1 string)
  13610. s2 (match-string 2 string))
  13611. (setq s1 "" s2 string))
  13612. (cond
  13613. ((eq flag nil)
  13614. ;; try completion
  13615. (setq rtn (try-completion s2 ctable confirm))
  13616. (if (stringp rtn)
  13617. (setq rtn
  13618. (concat s1 s2 (substring rtn (length s2))
  13619. (if (and org-add-colon-after-tag-completion
  13620. (assoc rtn ctable))
  13621. ":" ""))))
  13622. rtn)
  13623. ((eq flag t)
  13624. ;; all-completions
  13625. (all-completions s2 ctable confirm))
  13626. ((eq flag 'lambda)
  13627. ;; exact match?
  13628. (assoc s2 ctable)))))
  13629. (defun org-fast-tag-insert (kwd tags face &optional end)
  13630. "Insert KDW, and the TAGS, the latter with face FACE.
  13631. Also insert END."
  13632. (insert (format "%-12s" (concat kwd ":"))
  13633. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13634. (or end "")))
  13635. (defun org-fast-tag-show-exit (flag)
  13636. (save-excursion
  13637. (org-goto-line 3)
  13638. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13639. (replace-match ""))
  13640. (when flag
  13641. (end-of-line 1)
  13642. (org-move-to-column (- (window-width) 19) t)
  13643. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13644. (defun org-set-current-tags-overlay (current prefix)
  13645. "Add an overlay to CURRENT tag with PREFIX."
  13646. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13647. (if (featurep 'xemacs)
  13648. (org-overlay-display org-tags-overlay (concat prefix s)
  13649. 'secondary-selection)
  13650. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13651. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13652. (defvar org-last-tag-selection-key nil)
  13653. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13654. "Fast tag selection with single keys.
  13655. CURRENT is the current list of tags in the headline, INHERITED is the
  13656. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13657. possibly with grouping information. TODO-TABLE is a similar table with
  13658. TODO keywords, should these have keys assigned to them.
  13659. If the keys are nil, a-z are automatically assigned.
  13660. Returns the new tags string, or nil to not change the current settings."
  13661. (let* ((fulltable (append table todo-table))
  13662. (maxlen (apply 'max (mapcar
  13663. (lambda (x)
  13664. (if (stringp (car x)) (string-width (car x)) 0))
  13665. fulltable)))
  13666. (buf (current-buffer))
  13667. (expert (eq org-fast-tag-selection-single-key 'expert))
  13668. (buffer-tags nil)
  13669. (fwidth (+ maxlen 3 1 3))
  13670. (ncol (/ (- (window-width) 4) fwidth))
  13671. (i-face 'org-done)
  13672. (c-face 'org-todo)
  13673. tg cnt e c char c1 c2 ntable tbl rtn
  13674. ov-start ov-end ov-prefix
  13675. (exit-after-next org-fast-tag-selection-single-key)
  13676. (done-keywords org-done-keywords)
  13677. groups ingroup)
  13678. (save-excursion
  13679. (beginning-of-line 1)
  13680. (if (looking-at
  13681. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13682. (setq ov-start (match-beginning 1)
  13683. ov-end (match-end 1)
  13684. ov-prefix "")
  13685. (setq ov-start (1- (point-at-eol))
  13686. ov-end (1+ ov-start))
  13687. (skip-chars-forward "^\n\r")
  13688. (setq ov-prefix
  13689. (concat
  13690. (buffer-substring (1- (point)) (point))
  13691. (if (> (current-column) org-tags-column)
  13692. " "
  13693. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13694. (move-overlay org-tags-overlay ov-start ov-end)
  13695. (save-window-excursion
  13696. (if expert
  13697. (set-buffer (get-buffer-create " *Org tags*"))
  13698. (delete-other-windows)
  13699. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13700. (org-switch-to-buffer-other-window " *Org tags*"))
  13701. (erase-buffer)
  13702. (org-set-local 'org-done-keywords done-keywords)
  13703. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13704. (org-fast-tag-insert "Current" current c-face "\n\n")
  13705. (org-fast-tag-show-exit exit-after-next)
  13706. (org-set-current-tags-overlay current ov-prefix)
  13707. (setq tbl fulltable char ?a cnt 0)
  13708. (while (setq e (pop tbl))
  13709. (cond
  13710. ((equal (car e) :startgroup)
  13711. (push '() groups) (setq ingroup t)
  13712. (when (not (= cnt 0))
  13713. (setq cnt 0)
  13714. (insert "\n"))
  13715. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13716. ((equal (car e) :endgroup)
  13717. (setq ingroup nil cnt 0)
  13718. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13719. ((equal e '(:newline))
  13720. (when (not (= cnt 0))
  13721. (setq cnt 0)
  13722. (insert "\n")
  13723. (setq e (car tbl))
  13724. (while (equal (car tbl) '(:newline))
  13725. (insert "\n")
  13726. (setq tbl (cdr tbl)))))
  13727. ((equal e '(:grouptags)) nil)
  13728. (t
  13729. (setq tg (copy-sequence (car e)) c2 nil)
  13730. (if (cdr e)
  13731. (setq c (cdr e))
  13732. ;; automatically assign a character.
  13733. (setq c1 (string-to-char
  13734. (downcase (substring
  13735. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13736. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13737. (while (or (rassoc char ntable) (rassoc char table))
  13738. (setq char (1+ char)))
  13739. (setq c2 c1))
  13740. (setq c (or c2 char)))
  13741. (if ingroup (push tg (car groups)))
  13742. (setq tg (org-add-props tg nil 'face
  13743. (cond
  13744. ((not (assoc tg table))
  13745. (org-get-todo-face tg))
  13746. ((member tg current) c-face)
  13747. ((member tg inherited) i-face))))
  13748. (if (equal (caar tbl) :grouptags)
  13749. (org-add-props tg nil 'face 'org-tag-group))
  13750. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13751. (insert "[" c "] " tg (make-string
  13752. (- fwidth 4 (length tg)) ?\ ))
  13753. (push (cons tg c) ntable)
  13754. (when (= (setq cnt (1+ cnt)) ncol)
  13755. (insert "\n")
  13756. (if ingroup (insert " "))
  13757. (setq cnt 0)))))
  13758. (setq ntable (nreverse ntable))
  13759. (insert "\n")
  13760. (goto-char (point-min))
  13761. (if (not expert) (org-fit-window-to-buffer))
  13762. (setq rtn
  13763. (catch 'exit
  13764. (while t
  13765. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13766. (if (not groups) "no " "")
  13767. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13768. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13769. (setq org-last-tag-selection-key c)
  13770. (cond
  13771. ((= c ?\r) (throw 'exit t))
  13772. ((= c ?!)
  13773. (setq groups (not groups))
  13774. (goto-char (point-min))
  13775. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13776. ((= c ?\C-c)
  13777. (if (not expert)
  13778. (org-fast-tag-show-exit
  13779. (setq exit-after-next (not exit-after-next)))
  13780. (setq expert nil)
  13781. (delete-other-windows)
  13782. (set-window-buffer (split-window-vertically) " *Org tags*")
  13783. (org-switch-to-buffer-other-window " *Org tags*")
  13784. (org-fit-window-to-buffer)))
  13785. ((or (= c ?\C-g)
  13786. (and (= c ?q) (not (rassoc c ntable))))
  13787. (org-detach-overlay org-tags-overlay)
  13788. (setq quit-flag t))
  13789. ((= c ?\ )
  13790. (setq current nil)
  13791. (if exit-after-next (setq exit-after-next 'now)))
  13792. ((= c ?\t)
  13793. (condition-case nil
  13794. (setq tg (org-icompleting-read
  13795. "Tag: "
  13796. (or buffer-tags
  13797. (with-current-buffer buf
  13798. (org-get-buffer-tags)))))
  13799. (quit (setq tg "")))
  13800. (when (string-match "\\S-" tg)
  13801. (add-to-list 'buffer-tags (list tg))
  13802. (if (member tg current)
  13803. (setq current (delete tg current))
  13804. (push tg current)))
  13805. (if exit-after-next (setq exit-after-next 'now)))
  13806. ((setq e (rassoc c todo-table) tg (car e))
  13807. (with-current-buffer buf
  13808. (save-excursion (org-todo tg)))
  13809. (if exit-after-next (setq exit-after-next 'now)))
  13810. ((setq e (rassoc c ntable) tg (car e))
  13811. (if (member tg current)
  13812. (setq current (delete tg current))
  13813. (loop for g in groups do
  13814. (if (member tg g)
  13815. (mapc (lambda (x)
  13816. (setq current (delete x current)))
  13817. g)))
  13818. (push tg current))
  13819. (if exit-after-next (setq exit-after-next 'now))))
  13820. ;; Create a sorted list
  13821. (setq current
  13822. (sort current
  13823. (lambda (a b)
  13824. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13825. (if (eq exit-after-next 'now) (throw 'exit t))
  13826. (goto-char (point-min))
  13827. (beginning-of-line 2)
  13828. (delete-region (point) (point-at-eol))
  13829. (org-fast-tag-insert "Current" current c-face)
  13830. (org-set-current-tags-overlay current ov-prefix)
  13831. (while (re-search-forward
  13832. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13833. (setq tg (match-string 1))
  13834. (add-text-properties
  13835. (match-beginning 1) (match-end 1)
  13836. (list 'face
  13837. (cond
  13838. ((member tg current) c-face)
  13839. ((member tg inherited) i-face)
  13840. (t (get-text-property (match-beginning 1) 'face))))))
  13841. (goto-char (point-min)))))
  13842. (org-detach-overlay org-tags-overlay)
  13843. (if rtn
  13844. (mapconcat 'identity current ":")
  13845. nil))))
  13846. (defun org-get-tags-string ()
  13847. "Get the TAGS string in the current headline."
  13848. (unless (org-at-heading-p t)
  13849. (user-error "Not on a heading"))
  13850. (save-excursion
  13851. (beginning-of-line 1)
  13852. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13853. (org-match-string-no-properties 1)
  13854. "")))
  13855. (defun org-get-tags ()
  13856. "Get the list of tags specified in the current headline."
  13857. (org-split-string (org-get-tags-string) ":"))
  13858. (defun org-get-buffer-tags ()
  13859. "Get a table of all tags used in the buffer, for completion."
  13860. (let (tags)
  13861. (save-excursion
  13862. (goto-char (point-min))
  13863. (while (re-search-forward
  13864. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13865. (when (equal (char-after (point-at-bol 0)) ?*)
  13866. (mapc (lambda (x) (add-to-list 'tags x))
  13867. (org-split-string (org-match-string-no-properties 1) ":")))))
  13868. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13869. (mapcar 'list tags)))
  13870. ;;;; The mapping API
  13871. (defun org-map-entries (func &optional match scope &rest skip)
  13872. "Call FUNC at each headline selected by MATCH in SCOPE.
  13873. FUNC is a function or a lisp form. The function will be called without
  13874. arguments, with the cursor positioned at the beginning of the headline.
  13875. The return values of all calls to the function will be collected and
  13876. returned as a list.
  13877. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13878. does not need to preserve point. After evaluation, the cursor will be
  13879. moved to the end of the line (presumably of the headline of the
  13880. processed entry) and search continues from there. Under some
  13881. circumstances, this may not produce the wanted results. For example,
  13882. if you have removed (e.g. archived) the current (sub)tree it could
  13883. mean that the next entry will be skipped entirely. In such cases, you
  13884. can specify the position from where search should continue by making
  13885. FUNC set the variable `org-map-continue-from' to the desired buffer
  13886. position.
  13887. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13888. Only headlines that are matched by this query will be considered during
  13889. the iteration. When MATCH is nil or t, all headlines will be
  13890. visited by the iteration.
  13891. SCOPE determines the scope of this command. It can be any of:
  13892. nil The current buffer, respecting the restriction if any
  13893. tree The subtree started with the entry at point
  13894. region The entries within the active region, if any
  13895. region-start-level
  13896. The entries within the active region, but only those at
  13897. the same level than the first one.
  13898. file The current buffer, without restriction
  13899. file-with-archives
  13900. The current buffer, and any archives associated with it
  13901. agenda All agenda files
  13902. agenda-with-archives
  13903. All agenda files with any archive files associated with them
  13904. \(file1 file2 ...)
  13905. If this is a list, all files in the list will be scanned
  13906. The remaining args are treated as settings for the skipping facilities of
  13907. the scanner. The following items can be given here:
  13908. archive skip trees with the archive tag
  13909. comment skip trees with the COMMENT keyword
  13910. function or Emacs Lisp form:
  13911. will be used as value for `org-agenda-skip-function', so
  13912. whenever the function returns a position, FUNC will not be
  13913. called for that entry and search will continue from the
  13914. position returned
  13915. If your function needs to retrieve the tags including inherited tags
  13916. at the *current* entry, you can use the value of the variable
  13917. `org-scanner-tags' which will be much faster than getting the value
  13918. with `org-get-tags-at'. If your function gets properties with
  13919. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13920. to t around the call to `org-entry-properties' to get the same speedup.
  13921. Note that if your function moves around to retrieve tags and properties at
  13922. a *different* entry, you cannot use these techniques."
  13923. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13924. (not (org-region-active-p)))
  13925. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13926. (org-agenda-skip-archived-trees (memq 'archive skip))
  13927. (org-agenda-skip-comment-trees (memq 'comment skip))
  13928. (org-agenda-skip-function
  13929. (car (org-delete-all '(comment archive) skip)))
  13930. (org-tags-match-list-sublevels t)
  13931. (start-level (eq scope 'region-start-level))
  13932. matcher file res
  13933. org-todo-keywords-for-agenda
  13934. org-done-keywords-for-agenda
  13935. org-todo-keyword-alist-for-agenda
  13936. org-tag-alist-for-agenda
  13937. todo-only)
  13938. (cond
  13939. ((eq match t) (setq matcher t))
  13940. ((eq match nil) (setq matcher t))
  13941. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13942. (save-excursion
  13943. (save-restriction
  13944. (cond ((eq scope 'tree)
  13945. (org-back-to-heading t)
  13946. (org-narrow-to-subtree)
  13947. (setq scope nil))
  13948. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13949. (org-region-active-p))
  13950. ;; If needed, set start-level to a string like "2"
  13951. (when start-level
  13952. (save-excursion
  13953. (goto-char (region-beginning))
  13954. (unless (org-at-heading-p) (outline-next-heading))
  13955. (setq start-level (org-current-level))))
  13956. (narrow-to-region (region-beginning)
  13957. (save-excursion
  13958. (goto-char (region-end))
  13959. (unless (and (bolp) (org-at-heading-p))
  13960. (outline-next-heading))
  13961. (point)))
  13962. (setq scope nil)))
  13963. (if (not scope)
  13964. (progn
  13965. (org-agenda-prepare-buffers
  13966. (list (buffer-file-name (current-buffer))))
  13967. (setq res (org-scan-tags func matcher todo-only start-level)))
  13968. ;; Get the right scope
  13969. (cond
  13970. ((and scope (listp scope) (symbolp (car scope)))
  13971. (setq scope (eval scope)))
  13972. ((eq scope 'agenda)
  13973. (setq scope (org-agenda-files t)))
  13974. ((eq scope 'agenda-with-archives)
  13975. (setq scope (org-agenda-files t))
  13976. (setq scope (org-add-archive-files scope)))
  13977. ((eq scope 'file)
  13978. (setq scope (list (buffer-file-name))))
  13979. ((eq scope 'file-with-archives)
  13980. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13981. (org-agenda-prepare-buffers scope)
  13982. (while (setq file (pop scope))
  13983. (with-current-buffer (org-find-base-buffer-visiting file)
  13984. (save-excursion
  13985. (save-restriction
  13986. (widen)
  13987. (goto-char (point-min))
  13988. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13989. res)))
  13990. ;;;; Properties
  13991. ;;; Setting and retrieving properties
  13992. (defconst org-special-properties
  13993. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13994. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13995. "The special properties valid in Org-mode.
  13996. These are properties that are not defined in the property drawer,
  13997. but in some other way.")
  13998. (defconst org-default-properties
  13999. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14000. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14001. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14002. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14003. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  14004. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14005. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14006. "Some properties that are used by Org-mode for various purposes.
  14007. Being in this list makes sure that they are offered for completion.")
  14008. (defun org-property-action ()
  14009. "Do an action on properties."
  14010. (interactive)
  14011. (let (c)
  14012. (org-at-property-p)
  14013. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14014. (setq c (read-char-exclusive))
  14015. (cond
  14016. ((equal c ?s)
  14017. (call-interactively 'org-set-property))
  14018. ((equal c ?d)
  14019. (call-interactively 'org-delete-property))
  14020. ((equal c ?D)
  14021. (call-interactively 'org-delete-property-globally))
  14022. ((equal c ?c)
  14023. (call-interactively 'org-compute-property-at-point))
  14024. (t (user-error "No such property action %c" c)))))
  14025. (defun org-inc-effort ()
  14026. "Increment the value of the effort property in the current entry."
  14027. (interactive)
  14028. (org-set-effort nil t))
  14029. (defvar org-clock-effort) ;; Defined in org-clock.el
  14030. (defvar org-clock-current-task) ;; Defined in org-clock.el
  14031. (defun org-set-effort (&optional value increment)
  14032. "Set the effort property of the current entry.
  14033. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14034. 10th allowed value.
  14035. When INCREMENT is non-nil, set the property to the next allowed value."
  14036. (interactive "P")
  14037. (if (equal value 0) (setq value 10))
  14038. (let* ((completion-ignore-case t)
  14039. (prop org-effort-property)
  14040. (cur (org-entry-get nil prop))
  14041. (allowed (org-property-get-allowed-values nil prop 'table))
  14042. (existing (mapcar 'list (org-property-values prop)))
  14043. (heading (nth 4 (org-heading-components)))
  14044. rpl
  14045. (val (cond
  14046. ((stringp value) value)
  14047. ((and allowed (integerp value))
  14048. (or (car (nth (1- value) allowed))
  14049. (car (org-last allowed))))
  14050. ((and allowed increment)
  14051. (or (caadr (member (list cur) allowed))
  14052. (user-error "Allowed effort values are not set")))
  14053. (allowed
  14054. (message "Select 1-9,0, [RET%s]: %s"
  14055. (if cur (concat "=" cur) "")
  14056. (mapconcat 'car allowed " "))
  14057. (setq rpl (read-char-exclusive))
  14058. (if (equal rpl ?\r)
  14059. cur
  14060. (setq rpl (- rpl ?0))
  14061. (if (equal rpl 0) (setq rpl 10))
  14062. (if (and (> rpl 0) (<= rpl (length allowed)))
  14063. (car (nth (1- rpl) allowed))
  14064. (org-completing-read "Effort: " allowed nil))))
  14065. (t
  14066. (let (org-completion-use-ido org-completion-use-iswitchb)
  14067. (org-completing-read
  14068. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14069. (concat "[" cur "]") "")
  14070. ": ")
  14071. existing nil nil "" nil cur))))))
  14072. (unless (equal (org-entry-get nil prop) val)
  14073. (org-entry-put nil prop val))
  14074. (org-refresh-property
  14075. '((effort . identity)
  14076. (effort-minutes . org-duration-string-to-minutes))
  14077. val)
  14078. (when (string= heading org-clock-current-task)
  14079. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14080. (org-clock-update-mode-line))
  14081. (message "%s is now %s" prop val)))
  14082. (defun org-at-property-p ()
  14083. "Non-nil when point is inside a property drawer.
  14084. See `org-property-re' for match data, if applicable."
  14085. (when (eq (org-element-type (org-element-at-point)) 'node-property)
  14086. (save-excursion
  14087. (beginning-of-line)
  14088. (looking-at org-property-re))))
  14089. (defun org-get-property-block (&optional beg end force)
  14090. "Return the (beg . end) range of the body of the property drawer.
  14091. BEG and END are the beginning and end of the current subtree, or of
  14092. the part before the first headline. If they are not given, they will
  14093. be found. If the drawer does not exist and FORCE is non-nil, create
  14094. the drawer."
  14095. (catch 'exit
  14096. (save-excursion
  14097. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  14098. (progn (org-back-to-heading t) (point))))
  14099. (end (or end (and (not (outline-next-heading)) (point-max))
  14100. (point))))
  14101. (goto-char beg)
  14102. (if (re-search-forward org-property-start-re end t)
  14103. (setq beg (1+ (match-end 0)))
  14104. (if force
  14105. (save-excursion
  14106. (org-insert-property-drawer)
  14107. (setq end (progn (outline-next-heading) (point))))
  14108. (throw 'exit nil))
  14109. (goto-char beg)
  14110. (if (re-search-forward org-property-start-re end t)
  14111. (setq beg (1+ (match-end 0)))))
  14112. (if (re-search-forward org-property-end-re end t)
  14113. (setq end (match-beginning 0))
  14114. (or force (throw 'exit nil))
  14115. (goto-char beg)
  14116. (setq end beg)
  14117. (org-indent-line)
  14118. (insert ":END:\n"))
  14119. (cons beg end)))))
  14120. (defun org-entry-properties (&optional pom which specific)
  14121. "Get all properties of the entry at point-or-marker POM.
  14122. This includes the TODO keyword, the tags, time strings for deadline,
  14123. scheduled, and clocking, and any additional properties defined in the
  14124. entry. The return value is an alist, keys may occur multiple times
  14125. if the property key was used several times.
  14126. POM may also be nil, in which case the current entry is used.
  14127. If WHICH is nil or `all', get all properties. If WHICH is
  14128. `special' or `standard', only get that subclass. If WHICH
  14129. is a string only get exactly this property. SPECIFIC can be a string, the
  14130. specific property we are interested in. Specifying it can speed
  14131. things up because then unnecessary parsing is avoided."
  14132. (setq which (or which 'all))
  14133. (org-with-wide-buffer
  14134. (org-with-point-at pom
  14135. (let ((clockstr (substring org-clock-string 0 -1))
  14136. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  14137. (case-fold-search nil)
  14138. beg end range props sum-props key key1 value string clocksum clocksumt)
  14139. (when (and (derived-mode-p 'org-mode)
  14140. (ignore-errors (org-back-to-heading t)))
  14141. (setq beg (point))
  14142. (setq sum-props (get-text-property (point) 'org-summaries))
  14143. (setq clocksum (get-text-property (point) :org-clock-minutes)
  14144. clocksumt (get-text-property (point) :org-clock-minutes-today))
  14145. (outline-next-heading)
  14146. (setq end (point))
  14147. (when (memq which '(all special))
  14148. ;; Get the special properties, like TODO and tags
  14149. (goto-char beg)
  14150. (when (and (or (not specific) (string= specific "TODO"))
  14151. (looking-at org-todo-line-regexp) (match-end 2))
  14152. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14153. (when (and (or (not specific) (string= specific "PRIORITY"))
  14154. (looking-at org-priority-regexp))
  14155. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14156. (when (or (not specific) (string= specific "FILE"))
  14157. (push (cons "FILE" buffer-file-name) props))
  14158. (when (and (or (not specific) (string= specific "TAGS"))
  14159. (setq value (org-get-tags-string))
  14160. (string-match "\\S-" value))
  14161. (push (cons "TAGS" value) props))
  14162. (when (and (or (not specific) (string= specific "ALLTAGS"))
  14163. (setq value (org-get-tags-at)))
  14164. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  14165. ":"))
  14166. props))
  14167. (when (or (not specific) (string= specific "BLOCKED"))
  14168. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  14169. (when (or (not specific)
  14170. (member specific
  14171. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  14172. "TIMESTAMP" "TIMESTAMP_IA")))
  14173. (catch 'match
  14174. (while (and (re-search-forward org-maybe-keyword-time-regexp end t)
  14175. (not (text-property-any 0 (length (match-string 0))
  14176. 'face 'font-lock-comment-face
  14177. (match-string 0))))
  14178. (setq key (if (match-end 1)
  14179. (substring (org-match-string-no-properties 1)
  14180. 0 -1))
  14181. string (if (equal key clockstr)
  14182. (org-trim
  14183. (buffer-substring-no-properties
  14184. (match-beginning 3) (goto-char
  14185. (point-at-eol))))
  14186. (substring (org-match-string-no-properties 3)
  14187. 1 -1)))
  14188. ;; Get the correct property name from the key. This is
  14189. ;; necessary if the user has configured time keywords.
  14190. (setq key1 (concat key ":"))
  14191. (cond
  14192. ((not key)
  14193. (setq key
  14194. (if (= (char-after (match-beginning 3)) ?\[)
  14195. "TIMESTAMP_IA" "TIMESTAMP")))
  14196. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  14197. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  14198. ((equal key1 org-closed-string) (setq key "CLOSED"))
  14199. ((equal key1 org-clock-string) (setq key "CLOCK")))
  14200. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  14201. (progn
  14202. (push (cons key string) props)
  14203. ;; no need to search further if match is found
  14204. (throw 'match t))
  14205. (when (or (equal key "CLOCK") (not (assoc key props)))
  14206. (push (cons key string) props)))))))
  14207. (when (memq which '(all standard))
  14208. ;; Get the standard properties, like :PROP: ...
  14209. (setq range (org-get-property-block beg end))
  14210. (when range
  14211. (goto-char (car range))
  14212. (while (re-search-forward org-property-re
  14213. (cdr range) t)
  14214. (setq key (org-match-string-no-properties 2)
  14215. value (org-trim (or (org-match-string-no-properties 3) "")))
  14216. (unless (member key excluded)
  14217. (push (cons key (or value "")) props)))))
  14218. (if clocksum
  14219. (push (cons "CLOCKSUM"
  14220. (org-columns-number-to-string (/ (float clocksum) 60.)
  14221. 'add_times))
  14222. props))
  14223. (if clocksumt
  14224. (push (cons "CLOCKSUM_T"
  14225. (org-columns-number-to-string (/ (float clocksumt) 60.)
  14226. 'add_times))
  14227. props))
  14228. (unless (assoc "CATEGORY" props)
  14229. (push (cons "CATEGORY" (org-get-category)) props))
  14230. (append sum-props (nreverse props)))))))
  14231. (defun org-entry-get (pom property &optional inherit literal-nil)
  14232. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14233. If INHERIT is non-nil and the entry does not have the property,
  14234. then also check higher levels of the hierarchy.
  14235. If INHERIT is the symbol `selective', use inheritance only if the setting
  14236. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  14237. If the property is present but empty, the return value is the empty string.
  14238. If the property is not present at all, nil is returned.
  14239. Return the value as a string.
  14240. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  14241. do not interpret it as the list atom nil. This is used for inheritance
  14242. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  14243. (org-with-point-at pom
  14244. (if (and inherit (if (eq inherit 'selective)
  14245. (org-property-inherit-p property)
  14246. t))
  14247. (org-entry-get-with-inheritance property literal-nil)
  14248. (if (member property org-special-properties)
  14249. ;; We need a special property. Use `org-entry-properties'
  14250. ;; to retrieve it, but specify the wanted property
  14251. (cdr (assoc property (org-entry-properties nil 'special property)))
  14252. (org-with-wide-buffer
  14253. (let ((range (org-get-property-block)))
  14254. (when (and range (not (eq (car range) (cdr range)))
  14255. (save-excursion
  14256. (goto-char (car range))
  14257. (re-search-forward
  14258. (concat (org-re-property property) "\\|"
  14259. (org-re-property (concat property "+")))
  14260. (cdr range) t)))
  14261. (let* ((props
  14262. (list (or (assoc property org-file-properties)
  14263. (assoc property org-global-properties)
  14264. (assoc property org-global-properties-fixed))))
  14265. (ap (lambda (key)
  14266. (when (re-search-forward
  14267. (org-re-property key) (cdr range) t)
  14268. (setq props
  14269. (org-update-property-plist
  14270. key
  14271. (if (match-end 3)
  14272. (org-match-string-no-properties 3) "")
  14273. props)))))
  14274. val)
  14275. (goto-char (car range))
  14276. (funcall ap property)
  14277. (goto-char (car range))
  14278. (while (funcall ap (concat property "+")))
  14279. (setq val (cdr (assoc property props)))
  14280. (when val (if literal-nil val (org-not-nil val)))))))))))
  14281. (defun org-property-or-variable-value (var &optional inherit)
  14282. "Check if there is a property fixing the value of VAR.
  14283. If yes, return this value. If not, return the current value of the variable."
  14284. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14285. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14286. (read prop)
  14287. (symbol-value var))))
  14288. (defun org-entry-delete (pom property)
  14289. "Delete the property PROPERTY from entry at point-or-marker POM."
  14290. (unless (member property org-special-properties)
  14291. (org-with-point-at pom
  14292. (let ((range (org-get-property-block)))
  14293. (if (and range
  14294. (goto-char (car range))
  14295. (re-search-forward
  14296. (org-re-property property nil t)
  14297. (cdr range) t))
  14298. (progn
  14299. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14300. (org-remove-empty-drawer-at (car range))
  14301. t)
  14302. nil)))))
  14303. ;; Multi-values properties are properties that contain multiple values
  14304. ;; These values are assumed to be single words, separated by whitespace.
  14305. (defun org-entry-add-to-multivalued-property (pom property value)
  14306. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14307. (let* ((old (org-entry-get pom property))
  14308. (values (and old (org-split-string old "[ \t]"))))
  14309. (setq value (org-entry-protect-space value))
  14310. (unless (member value values)
  14311. (setq values (append values (list value)))
  14312. (org-entry-put pom property
  14313. (mapconcat 'identity values " ")))))
  14314. (defun org-entry-remove-from-multivalued-property (pom property value)
  14315. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14316. (let* ((old (org-entry-get pom property))
  14317. (values (and old (org-split-string old "[ \t]"))))
  14318. (setq value (org-entry-protect-space value))
  14319. (when (member value values)
  14320. (setq values (delete value values))
  14321. (org-entry-put pom property
  14322. (mapconcat 'identity values " ")))))
  14323. (defun org-entry-member-in-multivalued-property (pom property value)
  14324. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14325. (let* ((old (org-entry-get pom property))
  14326. (values (and old (org-split-string old "[ \t]"))))
  14327. (setq value (org-entry-protect-space value))
  14328. (member value values)))
  14329. (defun org-entry-get-multivalued-property (pom property)
  14330. "Return a list of values in a multivalued property."
  14331. (let* ((value (org-entry-get pom property))
  14332. (values (and value (org-split-string value "[ \t]"))))
  14333. (mapcar 'org-entry-restore-space values)))
  14334. (defun org-entry-put-multivalued-property (pom property &rest values)
  14335. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14336. VALUES should be a list of strings. Spaces will be protected."
  14337. (org-entry-put pom property
  14338. (mapconcat 'org-entry-protect-space values " "))
  14339. (let* ((value (org-entry-get pom property))
  14340. (values (and value (org-split-string value "[ \t]"))))
  14341. (mapcar 'org-entry-restore-space values)))
  14342. (defun org-entry-protect-space (s)
  14343. "Protect spaces and newline in string S."
  14344. (while (string-match " " s)
  14345. (setq s (replace-match "%20" t t s)))
  14346. (while (string-match "\n" s)
  14347. (setq s (replace-match "%0A" t t s)))
  14348. s)
  14349. (defun org-entry-restore-space (s)
  14350. "Restore spaces and newline in string S."
  14351. (while (string-match "%20" s)
  14352. (setq s (replace-match " " t t s)))
  14353. (while (string-match "%0A" s)
  14354. (setq s (replace-match "\n" t t s)))
  14355. s)
  14356. (defvar org-entry-property-inherited-from (make-marker)
  14357. "Marker pointing to the entry from where a property was inherited.
  14358. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14359. location of the entry where the inheritance search matched. If there was
  14360. no match, the marker will point nowhere.
  14361. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14362. is set.")
  14363. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14364. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14365. The search will stop at the first ancestor which has the property defined.
  14366. If the value found is \"nil\", return nil to show that the property
  14367. should be considered as undefined (this is the meaning of nil here).
  14368. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14369. (move-marker org-entry-property-inherited-from nil)
  14370. (let (tmp)
  14371. (save-excursion
  14372. (save-restriction
  14373. (widen)
  14374. (catch 'ex
  14375. (while t
  14376. (when (setq tmp (org-entry-get nil property nil literal-nil))
  14377. (or (ignore-errors (org-back-to-heading t))
  14378. (goto-char (point-min)))
  14379. (move-marker org-entry-property-inherited-from (point))
  14380. (throw 'ex tmp))
  14381. (or (ignore-errors (org-up-heading-safe))
  14382. (throw 'ex nil))))))
  14383. (setq tmp (or tmp
  14384. (cdr (assoc property org-file-properties))
  14385. (cdr (assoc property org-global-properties))
  14386. (cdr (assoc property org-global-properties-fixed))))
  14387. (if literal-nil tmp (org-not-nil tmp))))
  14388. (defvar org-property-changed-functions nil
  14389. "Hook called when the value of a property has changed.
  14390. Each hook function should accept two arguments, the name of the property
  14391. and the new value.")
  14392. (defun org-entry-put (pom property value)
  14393. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14394. If the value is `nil', it is converted to the empty string.
  14395. If it is not a string, an error is raised."
  14396. (cond ((null value) (setq value ""))
  14397. ((not (stringp value))
  14398. (error "Properties values should be strings.")))
  14399. (org-with-point-at pom
  14400. (org-back-to-heading t)
  14401. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14402. range)
  14403. (cond
  14404. ((equal property "TODO")
  14405. (when (and (string-match "\\S-" value)
  14406. (not (member value org-todo-keywords-1)))
  14407. (user-error "\"%s\" is not a valid TODO state" value))
  14408. (if (or (not value)
  14409. (not (string-match "\\S-" value)))
  14410. (setq value 'none))
  14411. (org-todo value)
  14412. (org-set-tags nil 'align))
  14413. ((equal property "PRIORITY")
  14414. (org-priority (if (and value (string-match "\\S-" value))
  14415. (string-to-char value) ?\ ))
  14416. (org-set-tags nil 'align))
  14417. ((equal property "CLOCKSUM")
  14418. (if (not (re-search-forward
  14419. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14420. (error "Cannot find a clock log")
  14421. (goto-char (- (match-end 1) 2))
  14422. (cond
  14423. ((eq value 'earlier) (org-timestamp-down))
  14424. ((eq value 'later) (org-timestamp-up)))
  14425. (org-clock-sum-current-item)))
  14426. ((equal property "SCHEDULED")
  14427. (if (re-search-forward org-scheduled-time-regexp end t)
  14428. (cond
  14429. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14430. ((eq value 'later) (org-timestamp-change 1 'day))
  14431. (t (call-interactively 'org-schedule)))
  14432. (call-interactively 'org-schedule)))
  14433. ((equal property "DEADLINE")
  14434. (if (re-search-forward org-deadline-time-regexp end t)
  14435. (cond
  14436. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14437. ((eq value 'later) (org-timestamp-change 1 'day))
  14438. (t (call-interactively 'org-deadline)))
  14439. (call-interactively 'org-deadline)))
  14440. ((member property org-special-properties)
  14441. (error "The %s property can not yet be set with `org-entry-put'"
  14442. property))
  14443. (t ; a non-special property
  14444. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14445. (setq range (org-get-property-block beg end 'force))
  14446. (goto-char (car range))
  14447. (if (re-search-forward
  14448. (org-re-property property nil t) (cdr range) t)
  14449. (progn
  14450. (delete-region (match-beginning 0) (match-end 0))
  14451. (goto-char (match-beginning 0)))
  14452. (goto-char (cdr range))
  14453. (insert "\n")
  14454. (backward-char 1)
  14455. (org-indent-line))
  14456. (insert ":" property ":")
  14457. (and value (insert " " value))
  14458. (org-indent-line)))))
  14459. (run-hook-with-args 'org-property-changed-functions property value)))
  14460. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14461. "Get all property keys in the current buffer.
  14462. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14463. like tags and TODO state.
  14464. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14465. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14466. and others.
  14467. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14468. formats in the current buffer."
  14469. (let (rtn range cfmt s p)
  14470. (save-excursion
  14471. (save-restriction
  14472. (widen)
  14473. (goto-char (point-min))
  14474. (while (re-search-forward org-property-start-re nil t)
  14475. (catch 'cont
  14476. (setq range (or (org-get-property-block)
  14477. (if (y-or-n-p
  14478. (format "Malformed drawer at %d, repair?" (point)))
  14479. (org-get-property-block nil nil t)
  14480. (throw 'cont nil))))
  14481. (goto-char (car range))
  14482. (while (re-search-forward org-property-re
  14483. (cdr range) t)
  14484. (add-to-list 'rtn (org-match-string-no-properties 2)))
  14485. (outline-next-heading)))))
  14486. (when include-specials
  14487. (setq rtn (append org-special-properties rtn)))
  14488. (when include-defaults
  14489. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14490. (add-to-list 'rtn org-effort-property))
  14491. (when include-columns
  14492. (save-excursion
  14493. (save-restriction
  14494. (widen)
  14495. (goto-char (point-min))
  14496. (while (re-search-forward
  14497. "^[ \t]*\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14498. nil t)
  14499. (setq cfmt (match-string 2) s 0)
  14500. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14501. cfmt s)
  14502. (setq s (match-end 0)
  14503. p (match-string 1 cfmt))
  14504. (unless (or (equal p "ITEM")
  14505. (member p org-special-properties))
  14506. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14507. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14508. (defun org-property-values (key)
  14509. "Return a list of all values of property KEY in the current buffer."
  14510. (save-excursion
  14511. (save-restriction
  14512. (widen)
  14513. (goto-char (point-min))
  14514. (let ((re (org-re-property key))
  14515. values)
  14516. (while (re-search-forward re nil t)
  14517. (add-to-list 'values (org-trim (match-string 3))))
  14518. (delete "" values)))))
  14519. (defun org-insert-property-drawer ()
  14520. "Insert a property drawer into the current entry."
  14521. (org-back-to-heading t)
  14522. (looking-at org-outline-regexp)
  14523. (let ((indent (if org-adapt-indentation
  14524. (- (match-end 0) (match-beginning 0))
  14525. 0))
  14526. (beg (point))
  14527. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14528. end hiddenp)
  14529. (outline-next-heading)
  14530. (setq end (point))
  14531. (goto-char beg)
  14532. (while (re-search-forward re end t))
  14533. (setq hiddenp (outline-invisible-p))
  14534. (end-of-line 1)
  14535. (and (equal (char-after) ?\n) (forward-char 1))
  14536. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14537. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14538. ;; just skip this line
  14539. (beginning-of-line 2)
  14540. ;; Drawer start, find the end
  14541. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14542. (beginning-of-line 1)))
  14543. (org-skip-over-state-notes)
  14544. (skip-chars-backward " \t\n\r")
  14545. (if (and (eq (char-before) ?*) (not (eq (char-after) ?\n)))
  14546. (forward-char 1))
  14547. (goto-char (point-at-eol))
  14548. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14549. (beginning-of-line 0)
  14550. (org-indent-to-column indent)
  14551. (beginning-of-line 2)
  14552. (org-indent-to-column indent)
  14553. (beginning-of-line 0)
  14554. (if hiddenp
  14555. (save-excursion
  14556. (org-back-to-heading t)
  14557. (hide-entry))
  14558. (org-flag-drawer t))))
  14559. (defun org-insert-drawer (&optional arg drawer)
  14560. "Insert a drawer at point.
  14561. Optional argument DRAWER, when non-nil, is a string representing
  14562. drawer's name. Otherwise, the user is prompted for a name.
  14563. If a region is active, insert the drawer around that region
  14564. instead.
  14565. Point is left between drawer's boundaries."
  14566. (interactive "P")
  14567. (let* ((drawer (if arg "PROPERTIES"
  14568. (or drawer (read-from-minibuffer "Drawer: ")))))
  14569. (cond
  14570. ;; With C-u, fall back on `org-insert-property-drawer'
  14571. (arg (org-insert-property-drawer))
  14572. ;;
  14573. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14574. (user-error "Invalid drawer name"))
  14575. ;; With an active region, insert a drawer at point.
  14576. ((not (org-region-active-p))
  14577. (progn
  14578. (unless (bolp) (insert "\n"))
  14579. (insert (format ":%s:\n\n:END:\n" drawer))
  14580. (forward-line -2)))
  14581. ;; Otherwise, insert the drawer at point
  14582. (t
  14583. (let ((rbeg (region-beginning))
  14584. (rend (copy-marker (region-end))))
  14585. (unwind-protect
  14586. (progn
  14587. (goto-char rbeg)
  14588. (beginning-of-line)
  14589. (when (save-excursion
  14590. (re-search-forward org-outline-regexp-bol rend t))
  14591. (user-error "Drawers cannot contain headlines"))
  14592. ;; Position point at the beginning of the first
  14593. ;; non-blank line in region. Insert drawer's opening
  14594. ;; there, then indent it.
  14595. (org-skip-whitespace)
  14596. (beginning-of-line)
  14597. (insert ":" drawer ":\n")
  14598. (forward-line -1)
  14599. (indent-for-tab-command)
  14600. ;; Move point to the beginning of the first blank line
  14601. ;; after the last non-blank line in region. Insert
  14602. ;; drawer's closing, then indent it.
  14603. (goto-char rend)
  14604. (skip-chars-backward " \r\t\n")
  14605. (insert "\n:END:")
  14606. (deactivate-mark t)
  14607. (indent-for-tab-command)
  14608. (unless (eolp) (insert "\n")))
  14609. ;; Clear marker, whatever the outcome of insertion is.
  14610. (set-marker rend nil)))))))
  14611. (defvar org-property-set-functions-alist nil
  14612. "Property set function alist.
  14613. Each entry should have the following format:
  14614. (PROPERTY . READ-FUNCTION)
  14615. The read function will be called with the same argument as
  14616. `org-completing-read'.")
  14617. (defun org-set-property-function (property)
  14618. "Get the function that should be used to set PROPERTY.
  14619. This is computed according to `org-property-set-functions-alist'."
  14620. (or (cdr (assoc property org-property-set-functions-alist))
  14621. 'org-completing-read))
  14622. (defun org-read-property-value (property)
  14623. "Read PROPERTY value from user."
  14624. (let* ((completion-ignore-case t)
  14625. (allowed (org-property-get-allowed-values nil property 'table))
  14626. (cur (org-entry-get nil property))
  14627. (prompt (concat property " value"
  14628. (if (and cur (string-match "\\S-" cur))
  14629. (concat " [" cur "]") "") ": "))
  14630. (set-function (org-set-property-function property))
  14631. (val (if allowed
  14632. (funcall set-function prompt allowed nil
  14633. (not (get-text-property 0 'org-unrestricted
  14634. (caar allowed))))
  14635. (let (org-completion-use-ido org-completion-use-iswitchb)
  14636. (funcall set-function prompt
  14637. (mapcar 'list (org-property-values property))
  14638. nil nil "" nil cur)))))
  14639. (org-trim val)))
  14640. (defvar org-last-set-property nil)
  14641. (defvar org-last-set-property-value nil)
  14642. (defun org-read-property-name ()
  14643. "Read a property name."
  14644. (let* ((completion-ignore-case t)
  14645. (keys (org-buffer-property-keys nil t t))
  14646. (default-prop (or (save-excursion
  14647. (save-match-data
  14648. (beginning-of-line)
  14649. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14650. (null (string= (match-string 1) "END"))
  14651. (match-string 1))))
  14652. org-last-set-property))
  14653. (property (org-icompleting-read
  14654. (concat "Property"
  14655. (if default-prop (concat " [" default-prop "]") "")
  14656. ": ")
  14657. (mapcar 'list keys)
  14658. nil nil nil nil
  14659. default-prop)))
  14660. (if (member property keys)
  14661. property
  14662. (or (cdr (assoc (downcase property)
  14663. (mapcar (lambda (x) (cons (downcase x) x))
  14664. keys)))
  14665. property))))
  14666. (defun org-set-property-and-value (use-last)
  14667. "Allow to set [PROPERTY]: [value] direction from prompt.
  14668. When use-default, don't even ask, just use the last
  14669. \"[PROPERTY]: [value]\" string from the history."
  14670. (interactive "P")
  14671. (let* ((completion-ignore-case t)
  14672. (pv (or (and use-last org-last-set-property-value)
  14673. (org-completing-read
  14674. "Enter a \"[Property]: [value]\" pair: "
  14675. nil nil nil nil nil
  14676. org-last-set-property-value)))
  14677. prop val)
  14678. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14679. (setq prop (match-string 1 pv)
  14680. val (match-string 2 pv))
  14681. (org-set-property prop val))))
  14682. (defun org-set-property (property value)
  14683. "In the current entry, set PROPERTY to VALUE.
  14684. When called interactively, this will prompt for a property name, offering
  14685. completion on existing and default properties. And then it will prompt
  14686. for a value, offering completion either on allowed values (via an inherited
  14687. xxx_ALL property) or on existing values in other instances of this property
  14688. in the current file."
  14689. (interactive (list nil nil))
  14690. (let* ((property (or property (org-read-property-name)))
  14691. (value (or value (org-read-property-value property)))
  14692. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14693. (setq org-last-set-property property)
  14694. (setq org-last-set-property-value (concat property ": " value))
  14695. ;; Possibly postprocess the inserted value:
  14696. (when fn (setq value (funcall fn value)))
  14697. (unless (equal (org-entry-get nil property) value)
  14698. (org-entry-put nil property value))))
  14699. (defun org-delete-property (property)
  14700. "In the current entry, delete PROPERTY."
  14701. (interactive
  14702. (let* ((completion-ignore-case t)
  14703. (cat (org-entry-get (point) "CATEGORY"))
  14704. (props0 (org-entry-properties nil 'standard))
  14705. (props (if cat props0
  14706. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14707. (prop (if (< 1 (length props))
  14708. (org-icompleting-read "Property: " props nil t)
  14709. (caar props))))
  14710. (list prop)))
  14711. (if (not property)
  14712. (message "No property to delete in this entry")
  14713. (org-entry-delete nil property)
  14714. (message "Property \"%s\" deleted" property)))
  14715. (defun org-delete-property-globally (property)
  14716. "Remove PROPERTY globally, from all entries."
  14717. (interactive
  14718. (let* ((completion-ignore-case t)
  14719. (prop (org-icompleting-read
  14720. "Globally remove property: "
  14721. (mapcar 'list (org-buffer-property-keys)))))
  14722. (list prop)))
  14723. (save-excursion
  14724. (save-restriction
  14725. (widen)
  14726. (goto-char (point-min))
  14727. (let ((cnt 0))
  14728. (while (re-search-forward
  14729. (org-re-property property)
  14730. nil t)
  14731. (setq cnt (1+ cnt))
  14732. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14733. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14734. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14735. (defun org-compute-property-at-point ()
  14736. "Compute the property at point.
  14737. This looks for an enclosing column format, extracts the operator and
  14738. then applies it to the property in the column format's scope."
  14739. (interactive)
  14740. (unless (org-at-property-p)
  14741. (user-error "Not at a property"))
  14742. (let ((prop (org-match-string-no-properties 2)))
  14743. (org-columns-get-format-and-top-level)
  14744. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14745. (user-error "No operator defined for property %s" prop))
  14746. (org-columns-compute prop)))
  14747. (defvar org-property-allowed-value-functions nil
  14748. "Hook for functions supplying allowed values for a specific property.
  14749. The functions must take a single argument, the name of the property, and
  14750. return a flat list of allowed values. If \":ETC\" is one of
  14751. the values, this means that these values are intended as defaults for
  14752. completion, but that other values should be allowed too.
  14753. The functions must return nil if they are not responsible for this
  14754. property.")
  14755. (defun org-property-get-allowed-values (pom property &optional table)
  14756. "Get allowed values for the property PROPERTY.
  14757. When TABLE is non-nil, return an alist that can directly be used for
  14758. completion."
  14759. (let (vals)
  14760. (cond
  14761. ((equal property "TODO")
  14762. (setq vals (org-with-point-at pom
  14763. (append org-todo-keywords-1 '("")))))
  14764. ((equal property "PRIORITY")
  14765. (let ((n org-lowest-priority))
  14766. (while (>= n org-highest-priority)
  14767. (push (char-to-string n) vals)
  14768. (setq n (1- n)))))
  14769. ((member property org-special-properties))
  14770. ((setq vals (run-hook-with-args-until-success
  14771. 'org-property-allowed-value-functions property)))
  14772. (t
  14773. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14774. (when (and vals (string-match "\\S-" vals))
  14775. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14776. (setq vals (mapcar (lambda (x)
  14777. (cond ((stringp x) x)
  14778. ((numberp x) (number-to-string x))
  14779. ((symbolp x) (symbol-name x))
  14780. (t "???")))
  14781. vals)))))
  14782. (when (member ":ETC" vals)
  14783. (setq vals (remove ":ETC" vals))
  14784. (org-add-props (car vals) '(org-unrestricted t)))
  14785. (if table (mapcar 'list vals) vals)))
  14786. (defun org-property-previous-allowed-value (&optional previous)
  14787. "Switch to the next allowed value for this property."
  14788. (interactive)
  14789. (org-property-next-allowed-value t))
  14790. (defun org-property-next-allowed-value (&optional previous)
  14791. "Switch to the next allowed value for this property."
  14792. (interactive)
  14793. (unless (org-at-property-p)
  14794. (user-error "Not at a property"))
  14795. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14796. (key (match-string 2))
  14797. (value (match-string 3))
  14798. (allowed (or (org-property-get-allowed-values (point) key)
  14799. (and (member value '("[ ]" "[-]" "[X]"))
  14800. '("[ ]" "[X]"))))
  14801. (heading (save-match-data (nth 4 (org-heading-components))))
  14802. nval)
  14803. (unless allowed
  14804. (user-error "Allowed values for this property have not been defined"))
  14805. (if previous (setq allowed (reverse allowed)))
  14806. (if (member value allowed)
  14807. (setq nval (car (cdr (member value allowed)))))
  14808. (setq nval (or nval (car allowed)))
  14809. (if (equal nval value)
  14810. (user-error "Only one allowed value for this property"))
  14811. (org-at-property-p)
  14812. (replace-match (concat " :" key ": " nval) t t)
  14813. (org-indent-line)
  14814. (beginning-of-line 1)
  14815. (skip-chars-forward " \t")
  14816. (when (equal prop org-effort-property)
  14817. (org-refresh-property
  14818. '((effort . identity)
  14819. (effort-minutes . org-duration-string-to-minutes))
  14820. nval)
  14821. (when (string= org-clock-current-task heading)
  14822. (setq org-clock-effort nval)
  14823. (org-clock-update-mode-line)))
  14824. (run-hook-with-args 'org-property-changed-functions key nval)))
  14825. (defun org-find-olp (path &optional this-buffer)
  14826. "Return a marker pointing to the entry at outline path OLP.
  14827. If anything goes wrong, throw an error.
  14828. You can wrap this call to catch the error like this:
  14829. (condition-case msg
  14830. (org-mobile-locate-entry (match-string 4))
  14831. (error (nth 1 msg)))
  14832. The return value will then be either a string with the error message,
  14833. or a marker if everything is OK.
  14834. If THIS-BUFFER is set, the outline path does not contain a file,
  14835. only headings."
  14836. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14837. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14838. (level 1)
  14839. (lmin 1)
  14840. (lmax 1)
  14841. limit re end found pos heading cnt flevel)
  14842. (unless buffer (error "File not found :%s" file))
  14843. (with-current-buffer buffer
  14844. (save-excursion
  14845. (save-restriction
  14846. (widen)
  14847. (setq limit (point-max))
  14848. (goto-char (point-min))
  14849. (while (setq heading (pop path))
  14850. (setq re (format org-complex-heading-regexp-format
  14851. (regexp-quote heading)))
  14852. (setq cnt 0 pos (point))
  14853. (while (re-search-forward re end t)
  14854. (setq level (- (match-end 1) (match-beginning 1)))
  14855. (if (and (>= level lmin) (<= level lmax))
  14856. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14857. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14858. lmax heading))
  14859. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14860. lmax heading))
  14861. (goto-char found)
  14862. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14863. (setq end (save-excursion (org-end-of-subtree t t))))
  14864. (when (org-at-heading-p)
  14865. (point-marker)))))))
  14866. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14867. "Find node HEADING in BUFFER.
  14868. Return a marker to the heading if it was found, or nil if not.
  14869. If POS-ONLY is set, return just the position instead of a marker.
  14870. The heading text must match exact, but it may have a TODO keyword,
  14871. a priority cookie and tags in the standard locations."
  14872. (with-current-buffer (or buffer (current-buffer))
  14873. (save-excursion
  14874. (save-restriction
  14875. (widen)
  14876. (goto-char (point-min))
  14877. (let (case-fold-search)
  14878. (if (re-search-forward
  14879. (format org-complex-heading-regexp-format
  14880. (regexp-quote heading)) nil t)
  14881. (if pos-only
  14882. (match-beginning 0)
  14883. (move-marker (make-marker) (match-beginning 0)))))))))
  14884. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14885. "Find Org node headline HEADING in all .org files in directory DIR.
  14886. When the target headline is found, return a marker to this location."
  14887. (let ((files (directory-files (or dir default-directory)
  14888. t "\\`[^.#].*\\.org\\'"))
  14889. file visiting m buffer)
  14890. (catch 'found
  14891. (while (setq file (pop files))
  14892. (message "trying %s" file)
  14893. (setq visiting (org-find-base-buffer-visiting file))
  14894. (setq buffer (or visiting (find-file-noselect file)))
  14895. (setq m (org-find-exact-headline-in-buffer
  14896. heading buffer))
  14897. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14898. (and m (throw 'found m))))))
  14899. (defun org-find-entry-with-id (ident)
  14900. "Locate the entry that contains the ID property with exact value IDENT.
  14901. IDENT can be a string, a symbol or a number, this function will search for
  14902. the string representation of it.
  14903. Return the position where this entry starts, or nil if there is no such entry."
  14904. (interactive "sID: ")
  14905. (let ((id (cond
  14906. ((stringp ident) ident)
  14907. ((symbol-name ident) (symbol-name ident))
  14908. ((numberp ident) (number-to-string ident))
  14909. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14910. (case-fold-search nil))
  14911. (save-excursion
  14912. (save-restriction
  14913. (widen)
  14914. (goto-char (point-min))
  14915. (when (re-search-forward
  14916. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14917. nil t)
  14918. (org-back-to-heading t)
  14919. (point))))))
  14920. ;;;; Timestamps
  14921. (defvar org-last-changed-timestamp nil)
  14922. (defvar org-last-inserted-timestamp nil
  14923. "The last time stamp inserted with `org-insert-time-stamp'.")
  14924. (defvar org-ts-what) ; dynamically scoped parameter
  14925. (defun org-time-stamp (arg &optional inactive)
  14926. "Prompt for a date/time and insert a time stamp.
  14927. If the user specifies a time like HH:MM or if this command is
  14928. called with at least one prefix argument, the time stamp contains
  14929. the date and the time. Otherwise, only the date is be included.
  14930. All parts of a date not specified by the user is filled in from
  14931. the current date/time. So if you just press return without
  14932. typing anything, the time stamp will represent the current
  14933. date/time.
  14934. If there is already a timestamp at the cursor, it will be
  14935. modified.
  14936. With two universal prefix arguments, insert an active timestamp
  14937. with the current time without prompting the user.
  14938. When called from lisp, the timestamp is inactive if INACTIVE is
  14939. non-nil."
  14940. (interactive "P")
  14941. (let* ((ts nil)
  14942. (default-time
  14943. ;; Default time is either today, or, when entering a range,
  14944. ;; the range start.
  14945. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14946. (save-excursion
  14947. (re-search-backward
  14948. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14949. (- (point) 20) t)))
  14950. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14951. (current-time)))
  14952. (default-input (and ts (org-get-compact-tod ts)))
  14953. (repeater (save-excursion
  14954. (save-match-data
  14955. (beginning-of-line)
  14956. (when (re-search-forward
  14957. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14958. (save-excursion (progn (end-of-line) (point))) t)
  14959. (match-string 0)))))
  14960. org-time-was-given org-end-time-was-given time)
  14961. (cond
  14962. ((and (org-at-timestamp-p t)
  14963. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14964. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14965. (insert "--")
  14966. (setq time (let ((this-command this-command))
  14967. (org-read-date arg 'totime nil nil
  14968. default-time default-input inactive)))
  14969. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14970. ((org-at-timestamp-p t)
  14971. (setq time (let ((this-command this-command))
  14972. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14973. (when (org-at-timestamp-p t) ; just to get the match data
  14974. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14975. (replace-match "")
  14976. (setq org-last-changed-timestamp
  14977. (org-insert-time-stamp
  14978. time (or org-time-was-given arg)
  14979. inactive nil nil (list org-end-time-was-given)))
  14980. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14981. (setq org-last-changed-timestamp
  14982. (concat (substring org-last-inserted-timestamp 0 -1)
  14983. " " repeater ">"))))
  14984. (message "Timestamp updated"))
  14985. ((equal arg '(16))
  14986. (org-insert-time-stamp (current-time) t inactive))
  14987. (t
  14988. (setq time (let ((this-command this-command))
  14989. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14990. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14991. nil nil (list org-end-time-was-given))))))
  14992. ;; FIXME: can we use this for something else, like computing time differences?
  14993. (defun org-get-compact-tod (s)
  14994. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14995. (let* ((t1 (match-string 1 s))
  14996. (h1 (string-to-number (match-string 2 s)))
  14997. (m1 (string-to-number (match-string 3 s)))
  14998. (t2 (and (match-end 4) (match-string 5 s)))
  14999. (h2 (and t2 (string-to-number (match-string 6 s))))
  15000. (m2 (and t2 (string-to-number (match-string 7 s))))
  15001. dh dm)
  15002. (if (not t2)
  15003. t1
  15004. (setq dh (- h2 h1) dm (- m2 m1))
  15005. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15006. (concat t1 "+" (number-to-string dh)
  15007. (and (/= 0 dm) (format ":%02d" dm)))))))
  15008. (defun org-time-stamp-inactive (&optional arg)
  15009. "Insert an inactive time stamp.
  15010. An inactive time stamp is enclosed in square brackets instead of angle
  15011. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15012. does not link to the calendar and cannot be changed with the S-cursor keys.
  15013. So these are more for recording a certain time/date."
  15014. (interactive "P")
  15015. (org-time-stamp arg 'inactive))
  15016. (defvar org-date-ovl (make-overlay 1 1))
  15017. (overlay-put org-date-ovl 'face 'org-date-selected)
  15018. (org-detach-overlay org-date-ovl)
  15019. (defvar org-ans1) ; dynamically scoped parameter
  15020. (defvar org-ans2) ; dynamically scoped parameter
  15021. (defvar org-plain-time-of-day-regexp) ; defined below
  15022. (defvar org-overriding-default-time nil) ; dynamically scoped
  15023. (defvar org-read-date-overlay nil)
  15024. (defvar org-dcst nil) ; dynamically scoped
  15025. (defvar org-read-date-history nil)
  15026. (defvar org-read-date-final-answer nil)
  15027. (defvar org-read-date-analyze-futurep nil)
  15028. (defvar org-read-date-analyze-forced-year nil)
  15029. (defvar org-read-date-inactive)
  15030. (defvar org-read-date-minibuffer-local-map
  15031. (let* ((org-replace-disputed-keys nil)
  15032. (map (make-sparse-keymap)))
  15033. (set-keymap-parent map minibuffer-local-map)
  15034. (org-defkey map (kbd ".")
  15035. (lambda () (interactive)
  15036. ;; Are we at the beginning of the prompt?
  15037. (if (looking-back "^[^:]+: ")
  15038. (org-eval-in-calendar '(calendar-goto-today))
  15039. (insert "."))))
  15040. (org-defkey map (kbd "C-.")
  15041. (lambda () (interactive)
  15042. (org-eval-in-calendar '(calendar-goto-today))))
  15043. (org-defkey map [(meta shift left)]
  15044. (lambda () (interactive)
  15045. (org-eval-in-calendar '(calendar-backward-month 1))))
  15046. (org-defkey map [(meta shift right)]
  15047. (lambda () (interactive)
  15048. (org-eval-in-calendar '(calendar-forward-month 1))))
  15049. (org-defkey map [(meta shift up)]
  15050. (lambda () (interactive)
  15051. (org-eval-in-calendar '(calendar-backward-year 1))))
  15052. (org-defkey map [(meta shift down)]
  15053. (lambda () (interactive)
  15054. (org-eval-in-calendar '(calendar-forward-year 1))))
  15055. (org-defkey map [?\e (shift left)]
  15056. (lambda () (interactive)
  15057. (org-eval-in-calendar '(calendar-backward-month 1))))
  15058. (org-defkey map [?\e (shift right)]
  15059. (lambda () (interactive)
  15060. (org-eval-in-calendar '(calendar-forward-month 1))))
  15061. (org-defkey map [?\e (shift up)]
  15062. (lambda () (interactive)
  15063. (org-eval-in-calendar '(calendar-backward-year 1))))
  15064. (org-defkey map [?\e (shift down)]
  15065. (lambda () (interactive)
  15066. (org-eval-in-calendar '(calendar-forward-year 1))))
  15067. (org-defkey map [(shift up)]
  15068. (lambda () (interactive)
  15069. (org-eval-in-calendar '(calendar-backward-week 1))))
  15070. (org-defkey map [(shift down)]
  15071. (lambda () (interactive)
  15072. (org-eval-in-calendar '(calendar-forward-week 1))))
  15073. (org-defkey map [(shift left)]
  15074. (lambda () (interactive)
  15075. (org-eval-in-calendar '(calendar-backward-day 1))))
  15076. (org-defkey map [(shift right)]
  15077. (lambda () (interactive)
  15078. (org-eval-in-calendar '(calendar-forward-day 1))))
  15079. (org-defkey map "!"
  15080. (lambda () (interactive)
  15081. (org-eval-in-calendar '(diary-view-entries))
  15082. (message "")))
  15083. (org-defkey map ">"
  15084. (lambda () (interactive)
  15085. (org-eval-in-calendar '(scroll-calendar-left 1))))
  15086. (org-defkey map "<"
  15087. (lambda () (interactive)
  15088. (org-eval-in-calendar '(scroll-calendar-right 1))))
  15089. (org-defkey map "\C-v"
  15090. (lambda () (interactive)
  15091. (org-eval-in-calendar
  15092. '(calendar-scroll-left-three-months 1))))
  15093. (org-defkey map "\M-v"
  15094. (lambda () (interactive)
  15095. (org-eval-in-calendar
  15096. '(calendar-scroll-right-three-months 1))))
  15097. map)
  15098. "Keymap for minibuffer commands when using `org-read-date'.")
  15099. (defvar org-def)
  15100. (defvar org-defdecode)
  15101. (defvar org-with-time)
  15102. (defun org-read-date (&optional org-with-time to-time from-string prompt
  15103. default-time default-input inactive)
  15104. "Read a date, possibly a time, and make things smooth for the user.
  15105. The prompt will suggest to enter an ISO date, but you can also enter anything
  15106. which will at least partially be understood by `parse-time-string'.
  15107. Unrecognized parts of the date will default to the current day, month, year,
  15108. hour and minute. If this command is called to replace a timestamp at point,
  15109. or to enter the second timestamp of a range, the default time is taken
  15110. from the existing stamp. Furthermore, the command prefers the future,
  15111. so if you are giving a date where the year is not given, and the day-month
  15112. combination is already past in the current year, it will assume you
  15113. mean next year. For details, see the manual. A few examples:
  15114. 3-2-5 --> 2003-02-05
  15115. feb 15 --> currentyear-02-15
  15116. 2/15 --> currentyear-02-15
  15117. sep 12 9 --> 2009-09-12
  15118. 12:45 --> today 12:45
  15119. 22 sept 0:34 --> currentyear-09-22 0:34
  15120. 12 --> currentyear-currentmonth-12
  15121. Fri --> nearest Friday after today
  15122. -Tue --> last Tuesday
  15123. etc.
  15124. Furthermore you can specify a relative date by giving, as the *first* thing
  15125. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15126. change in days weeks, months, years.
  15127. With a single plus or minus, the date is relative to today. With a double
  15128. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15129. +4d --> four days from today
  15130. +4 --> same as above
  15131. +2w --> two weeks from today
  15132. ++5 --> five days from default date
  15133. The function understands only English month and weekday abbreviations.
  15134. While prompting, a calendar is popped up - you can also select the
  15135. date with the mouse (button 1). The calendar shows a period of three
  15136. months. To scroll it to other months, use the keys `>' and `<'.
  15137. If you don't like the calendar, turn it off with
  15138. \(setq org-read-date-popup-calendar nil)
  15139. With optional argument TO-TIME, the date will immediately be converted
  15140. to an internal time.
  15141. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  15142. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  15143. still enter a time, and this function will inform the calling routine
  15144. about this change. The calling routine may then choose to change the
  15145. format used to insert the time stamp into the buffer to include the time.
  15146. With optional argument FROM-STRING, read from this string instead from
  15147. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15148. the time/date that is used for everything that is not specified by the
  15149. user."
  15150. (require 'parse-time)
  15151. (let* ((org-time-stamp-rounding-minutes
  15152. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  15153. (org-dcst org-display-custom-times)
  15154. (ct (org-current-time))
  15155. (org-def (or org-overriding-default-time default-time ct))
  15156. (org-defdecode (decode-time org-def))
  15157. (dummy (progn
  15158. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15159. (setcar (nthcdr 2 org-defdecode) -1)
  15160. (setcar (nthcdr 1 org-defdecode) 59)
  15161. (setq org-def (apply 'encode-time org-defdecode)
  15162. org-defdecode (decode-time org-def)))))
  15163. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15164. (calendar-frame-setup nil)
  15165. (calendar-setup nil)
  15166. (calendar-move-hook nil)
  15167. (calendar-view-diary-initially-flag nil)
  15168. (calendar-view-holidays-initially-flag nil)
  15169. (timestr (format-time-string
  15170. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15171. (prompt (concat (if prompt (concat prompt " ") "")
  15172. (format "Date+time [%s]: " timestr)))
  15173. ans (org-ans0 "") org-ans1 org-ans2 final)
  15174. (cond
  15175. (from-string (setq ans from-string))
  15176. (org-read-date-popup-calendar
  15177. (save-excursion
  15178. (save-window-excursion
  15179. (calendar)
  15180. (org-eval-in-calendar '(setq cursor-type nil) t)
  15181. (unwind-protect
  15182. (progn
  15183. (calendar-forward-day (- (time-to-days org-def)
  15184. (calendar-absolute-from-gregorian
  15185. (calendar-current-date))))
  15186. (org-eval-in-calendar nil t)
  15187. (let* ((old-map (current-local-map))
  15188. (map (copy-keymap calendar-mode-map))
  15189. (minibuffer-local-map
  15190. (copy-keymap org-read-date-minibuffer-local-map)))
  15191. (org-defkey map (kbd "RET") 'org-calendar-select)
  15192. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15193. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15194. (unwind-protect
  15195. (progn
  15196. (use-local-map map)
  15197. (setq org-read-date-inactive inactive)
  15198. (add-hook 'post-command-hook 'org-read-date-display)
  15199. (setq org-ans0 (read-string prompt default-input
  15200. 'org-read-date-history nil))
  15201. ;; org-ans0: from prompt
  15202. ;; org-ans1: from mouse click
  15203. ;; org-ans2: from calendar motion
  15204. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15205. (remove-hook 'post-command-hook 'org-read-date-display)
  15206. (use-local-map old-map)
  15207. (when org-read-date-overlay
  15208. (delete-overlay org-read-date-overlay)
  15209. (setq org-read-date-overlay nil)))))
  15210. (bury-buffer "*Calendar*")))))
  15211. (t ; Naked prompt only
  15212. (unwind-protect
  15213. (setq ans (read-string prompt default-input
  15214. 'org-read-date-history timestr))
  15215. (when org-read-date-overlay
  15216. (delete-overlay org-read-date-overlay)
  15217. (setq org-read-date-overlay nil)))))
  15218. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15219. (when org-read-date-analyze-forced-year
  15220. (message "Year was forced into %s"
  15221. (if org-read-date-force-compatible-dates
  15222. "compatible range (1970-2037)"
  15223. "range representable on this machine"))
  15224. (ding))
  15225. ;; One round trip to get rid of 34th of August and stuff like that....
  15226. (setq final (decode-time (apply 'encode-time final)))
  15227. (setq org-read-date-final-answer ans)
  15228. (if to-time
  15229. (apply 'encode-time final)
  15230. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15231. (format "%04d-%02d-%02d %02d:%02d"
  15232. (nth 5 final) (nth 4 final) (nth 3 final)
  15233. (nth 2 final) (nth 1 final))
  15234. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15235. (defun org-read-date-display ()
  15236. "Display the current date prompt interpretation in the minibuffer."
  15237. (when org-read-date-display-live
  15238. (when org-read-date-overlay
  15239. (delete-overlay org-read-date-overlay))
  15240. (when (minibufferp (current-buffer))
  15241. (save-excursion
  15242. (end-of-line 1)
  15243. (while (not (equal (buffer-substring
  15244. (max (point-min) (- (point) 4)) (point))
  15245. " "))
  15246. (insert " ")))
  15247. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15248. " " (or org-ans1 org-ans2)))
  15249. (org-end-time-was-given nil)
  15250. (f (org-read-date-analyze ans org-def org-defdecode))
  15251. (fmts (if org-dcst
  15252. org-time-stamp-custom-formats
  15253. org-time-stamp-formats))
  15254. (fmt (if (or org-with-time
  15255. (and (boundp 'org-time-was-given) org-time-was-given))
  15256. (cdr fmts)
  15257. (car fmts)))
  15258. (txt (format-time-string fmt (apply 'encode-time f)))
  15259. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15260. (txt (concat "=> " txt)))
  15261. (when (and org-end-time-was-given
  15262. (string-match org-plain-time-of-day-regexp txt))
  15263. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15264. org-end-time-was-given
  15265. (substring txt (match-end 0)))))
  15266. (when org-read-date-analyze-futurep
  15267. (setq txt (concat txt " (=>F)")))
  15268. (setq org-read-date-overlay
  15269. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15270. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15271. (defun org-read-date-analyze (ans org-def org-defdecode)
  15272. "Analyze the combined answer of the date prompt."
  15273. ;; FIXME: cleanup and comment
  15274. (let ((nowdecode (decode-time (current-time)))
  15275. delta deltan deltaw deltadef year month day
  15276. hour minute second wday pm h2 m2 tl wday1
  15277. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15278. (setq org-read-date-analyze-futurep nil
  15279. org-read-date-analyze-forced-year nil)
  15280. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15281. (setq ans "+0"))
  15282. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15283. (setq ans (replace-match "" t t ans)
  15284. deltan (car delta)
  15285. deltaw (nth 1 delta)
  15286. deltadef (nth 2 delta)))
  15287. ;; Check if there is an iso week date in there. If yes, store the
  15288. ;; info and postpone interpreting it until the rest of the parsing
  15289. ;; is done.
  15290. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15291. (setq iso-year (if (match-end 1)
  15292. (org-small-year-to-year
  15293. (string-to-number (match-string 1 ans))))
  15294. iso-weekday (if (match-end 3)
  15295. (string-to-number (match-string 3 ans)))
  15296. iso-week (string-to-number (match-string 2 ans)))
  15297. (setq ans (replace-match "" t t ans)))
  15298. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15299. (when (string-match
  15300. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15301. (setq year (if (match-end 2)
  15302. (string-to-number (match-string 2 ans))
  15303. (progn (setq kill-year t)
  15304. (string-to-number (format-time-string "%Y"))))
  15305. month (string-to-number (match-string 3 ans))
  15306. day (string-to-number (match-string 4 ans)))
  15307. (if (< year 100) (setq year (+ 2000 year)))
  15308. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15309. t nil ans)))
  15310. ;; Help matching dotted european dates
  15311. (when (string-match
  15312. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15313. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15314. (setq kill-year t)
  15315. (string-to-number (format-time-string "%Y")))
  15316. day (string-to-number (match-string 1 ans))
  15317. month (string-to-number (match-string 2 ans))
  15318. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15319. t nil ans)))
  15320. ;; Help matching american dates, like 5/30 or 5/30/7
  15321. (when (string-match
  15322. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15323. (setq year (if (match-end 4)
  15324. (string-to-number (match-string 4 ans))
  15325. (progn (setq kill-year t)
  15326. (string-to-number (format-time-string "%Y"))))
  15327. month (string-to-number (match-string 1 ans))
  15328. day (string-to-number (match-string 2 ans)))
  15329. (if (< year 100) (setq year (+ 2000 year)))
  15330. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15331. t nil ans)))
  15332. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15333. ;; If there is a time with am/pm, and *no* time without it, we convert
  15334. ;; so that matching will be successful.
  15335. (loop for i from 1 to 2 do ; twice, for end time as well
  15336. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15337. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15338. (setq hour (string-to-number (match-string 1 ans))
  15339. minute (if (match-end 3)
  15340. (string-to-number (match-string 3 ans))
  15341. 0)
  15342. pm (equal ?p
  15343. (string-to-char (downcase (match-string 4 ans)))))
  15344. (if (and (= hour 12) (not pm))
  15345. (setq hour 0)
  15346. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15347. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15348. t t ans))))
  15349. ;; Check if a time range is given as a duration
  15350. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15351. (setq hour (string-to-number (match-string 1 ans))
  15352. h2 (+ hour (string-to-number (match-string 3 ans)))
  15353. minute (string-to-number (match-string 2 ans))
  15354. m2 (+ minute (if (match-end 5) (string-to-number
  15355. (match-string 5 ans))0)))
  15356. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15357. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15358. t t ans)))
  15359. ;; Check if there is a time range
  15360. (when (boundp 'org-end-time-was-given)
  15361. (setq org-time-was-given nil)
  15362. (when (and (string-match org-plain-time-of-day-regexp ans)
  15363. (match-end 8))
  15364. (setq org-end-time-was-given (match-string 8 ans))
  15365. (setq ans (concat (substring ans 0 (match-beginning 7))
  15366. (substring ans (match-end 7))))))
  15367. (setq tl (parse-time-string ans)
  15368. day (or (nth 3 tl) (nth 3 org-defdecode))
  15369. month (or (nth 4 tl)
  15370. (if (and org-read-date-prefer-future
  15371. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15372. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15373. (nth 4 org-defdecode)))
  15374. year (or (and (not kill-year) (nth 5 tl))
  15375. (if (and org-read-date-prefer-future
  15376. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15377. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15378. (nth 5 org-defdecode)))
  15379. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15380. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15381. second (or (nth 0 tl) 0)
  15382. wday (nth 6 tl))
  15383. (when (and (eq org-read-date-prefer-future 'time)
  15384. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15385. (equal day (nth 3 nowdecode))
  15386. (equal month (nth 4 nowdecode))
  15387. (equal year (nth 5 nowdecode))
  15388. (nth 2 tl)
  15389. (or (< (nth 2 tl) (nth 2 nowdecode))
  15390. (and (= (nth 2 tl) (nth 2 nowdecode))
  15391. (nth 1 tl)
  15392. (< (nth 1 tl) (nth 1 nowdecode)))))
  15393. (setq day (1+ day)
  15394. futurep t))
  15395. ;; Special date definitions below
  15396. (cond
  15397. (iso-week
  15398. ;; There was an iso week
  15399. (require 'cal-iso)
  15400. (setq futurep nil)
  15401. (setq year (or iso-year year)
  15402. day (or iso-weekday wday 1)
  15403. wday nil ; to make sure that the trigger below does not match
  15404. iso-date (calendar-gregorian-from-absolute
  15405. (calendar-absolute-from-iso
  15406. (list iso-week day year))))
  15407. ; FIXME: Should we also push ISO weeks into the future?
  15408. ; (when (and org-read-date-prefer-future
  15409. ; (not iso-year)
  15410. ; (< (calendar-absolute-from-gregorian iso-date)
  15411. ; (time-to-days (current-time))))
  15412. ; (setq year (1+ year)
  15413. ; iso-date (calendar-gregorian-from-absolute
  15414. ; (calendar-absolute-from-iso
  15415. ; (list iso-week day year)))))
  15416. (setq month (car iso-date)
  15417. year (nth 2 iso-date)
  15418. day (nth 1 iso-date)))
  15419. (deltan
  15420. (setq futurep nil)
  15421. (unless deltadef
  15422. (let ((now (decode-time (current-time))))
  15423. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15424. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15425. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15426. ((equal deltaw "m") (setq month (+ month deltan)))
  15427. ((equal deltaw "y") (setq year (+ year deltan)))))
  15428. ((and wday (not (nth 3 tl)))
  15429. ;; Weekday was given, but no day, so pick that day in the week
  15430. ;; on or after the derived date.
  15431. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15432. (unless (equal wday wday1)
  15433. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15434. (if (and (boundp 'org-time-was-given)
  15435. (nth 2 tl))
  15436. (setq org-time-was-given t))
  15437. (if (< year 100) (setq year (+ 2000 year)))
  15438. ;; Check of the date is representable
  15439. (if org-read-date-force-compatible-dates
  15440. (progn
  15441. (if (< year 1970)
  15442. (setq year 1970 org-read-date-analyze-forced-year t))
  15443. (if (> year 2037)
  15444. (setq year 2037 org-read-date-analyze-forced-year t)))
  15445. (condition-case nil
  15446. (ignore (encode-time second minute hour day month year))
  15447. (error
  15448. (setq year (nth 5 org-defdecode))
  15449. (setq org-read-date-analyze-forced-year t))))
  15450. (setq org-read-date-analyze-futurep futurep)
  15451. (list second minute hour day month year)))
  15452. (defvar parse-time-weekdays)
  15453. (defun org-read-date-get-relative (s today default)
  15454. "Check string S for special relative date string.
  15455. TODAY and DEFAULT are internal times, for today and for a default.
  15456. Return shift list (N what def-flag)
  15457. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15458. N is the number of WHATs to shift.
  15459. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15460. the DEFAULT date rather than TODAY."
  15461. (require 'parse-time)
  15462. (when (and
  15463. (string-match
  15464. (concat
  15465. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15466. "\\([0-9]+\\)?"
  15467. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15468. "\\([ \t]\\|$\\)") s)
  15469. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15470. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15471. (string-to-char (substring (match-string 1 s) -1))
  15472. ?+))
  15473. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15474. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15475. (what (if (match-end 3) (match-string 3 s) "d"))
  15476. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15477. (date (if rel default today))
  15478. (wday (nth 6 (decode-time date)))
  15479. delta)
  15480. (if wday1
  15481. (progn
  15482. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15483. (if (= delta 0) (setq delta 7))
  15484. (if (= dir ?-)
  15485. (progn
  15486. (setq delta (- delta 7))
  15487. (if (= delta 0) (setq delta -7))))
  15488. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15489. (list delta "d" rel))
  15490. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15491. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15492. "Turn a user-specified date into the internal representation.
  15493. The internal representation needed by the calendar is (month day year).
  15494. This is a wrapper to handle the brain-dead convention in calendar that
  15495. user function argument order change dependent on argument order."
  15496. (if (boundp 'calendar-date-style)
  15497. (cond
  15498. ((eq calendar-date-style 'american)
  15499. (list arg1 arg2 arg3))
  15500. ((eq calendar-date-style 'european)
  15501. (list arg2 arg1 arg3))
  15502. ((eq calendar-date-style 'iso)
  15503. (list arg2 arg3 arg1)))
  15504. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15505. (if (org-bound-and-true-p european-calendar-style)
  15506. (list arg2 arg1 arg3)
  15507. (list arg1 arg2 arg3)))))
  15508. (defun org-eval-in-calendar (form &optional keepdate)
  15509. "Eval FORM in the calendar window and return to current window.
  15510. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15511. otherwise stick to the current value of `org-ans2'."
  15512. (let ((sf (selected-frame))
  15513. (sw (selected-window)))
  15514. (select-window (get-buffer-window "*Calendar*" t))
  15515. (eval form)
  15516. (when (and (not keepdate) (calendar-cursor-to-date))
  15517. (let* ((date (calendar-cursor-to-date))
  15518. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15519. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15520. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15521. (select-window sw)
  15522. (org-select-frame-set-input-focus sf)))
  15523. (defun org-calendar-select ()
  15524. "Return to `org-read-date' with the date currently selected.
  15525. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15526. (interactive)
  15527. (when (calendar-cursor-to-date)
  15528. (let* ((date (calendar-cursor-to-date))
  15529. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15530. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15531. (if (active-minibuffer-window) (exit-minibuffer))))
  15532. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15533. "Insert a date stamp for the date given by the internal TIME.
  15534. WITH-HM means use the stamp format that includes the time of the day.
  15535. INACTIVE means use square brackets instead of angular ones, so that the
  15536. stamp will not contribute to the agenda.
  15537. PRE and POST are optional strings to be inserted before and after the
  15538. stamp.
  15539. The command returns the inserted time stamp."
  15540. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15541. stamp)
  15542. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15543. (insert-before-markers (or pre ""))
  15544. (when (listp extra)
  15545. (setq extra (car extra))
  15546. (if (and (stringp extra)
  15547. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15548. (setq extra (format "-%02d:%02d"
  15549. (string-to-number (match-string 1 extra))
  15550. (string-to-number (match-string 2 extra))))
  15551. (setq extra nil)))
  15552. (when extra
  15553. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15554. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15555. (insert-before-markers (or post ""))
  15556. (setq org-last-inserted-timestamp stamp)))
  15557. (defun org-toggle-time-stamp-overlays ()
  15558. "Toggle the use of custom time stamp formats."
  15559. (interactive)
  15560. (setq org-display-custom-times (not org-display-custom-times))
  15561. (unless org-display-custom-times
  15562. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15563. (while (setq p (next-single-property-change p 'display))
  15564. (if (and (get-text-property p 'display)
  15565. (eq (get-text-property p 'face) 'org-date))
  15566. (remove-text-properties
  15567. p (setq p (next-single-property-change p 'display))
  15568. '(display t))))
  15569. (set-buffer-modified-p bmp)))
  15570. (if (featurep 'xemacs)
  15571. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15572. (org-restart-font-lock)
  15573. (setq org-table-may-need-update t)
  15574. (if org-display-custom-times
  15575. (message "Time stamps are overlaid with custom format")
  15576. (message "Time stamp overlays removed")))
  15577. (defun org-display-custom-time (beg end)
  15578. "Overlay modified time stamp format over timestamp between BEG and END."
  15579. (let* ((ts (buffer-substring beg end))
  15580. t1 w1 with-hm tf time str w2 (off 0))
  15581. (save-match-data
  15582. (setq t1 (org-parse-time-string ts t))
  15583. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15584. (setq off (- (match-end 0) (match-beginning 0)))))
  15585. (setq end (- end off))
  15586. (setq w1 (- end beg)
  15587. with-hm (and (nth 1 t1) (nth 2 t1))
  15588. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15589. time (org-fix-decoded-time t1)
  15590. str (org-add-props
  15591. (format-time-string
  15592. (substring tf 1 -1) (apply 'encode-time time))
  15593. nil 'mouse-face 'highlight)
  15594. w2 (length str))
  15595. (if (not (= w2 w1))
  15596. (add-text-properties (1+ beg) (+ 2 beg)
  15597. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15598. (if (featurep 'xemacs)
  15599. (progn
  15600. (put-text-property beg end 'invisible t)
  15601. (put-text-property beg end 'end-glyph (make-glyph str)))
  15602. (put-text-property beg end 'display str))))
  15603. (defun org-translate-time (string)
  15604. "Translate all timestamps in STRING to custom format.
  15605. But do this only if the variable `org-display-custom-times' is set."
  15606. (when org-display-custom-times
  15607. (save-match-data
  15608. (let* ((start 0)
  15609. (re org-ts-regexp-both)
  15610. t1 with-hm inactive tf time str beg end)
  15611. (while (setq start (string-match re string start))
  15612. (setq beg (match-beginning 0)
  15613. end (match-end 0)
  15614. t1 (save-match-data
  15615. (org-parse-time-string (substring string beg end) t))
  15616. with-hm (and (nth 1 t1) (nth 2 t1))
  15617. inactive (equal (substring string beg (1+ beg)) "[")
  15618. tf (funcall (if with-hm 'cdr 'car)
  15619. org-time-stamp-custom-formats)
  15620. time (org-fix-decoded-time t1)
  15621. str (format-time-string
  15622. (concat
  15623. (if inactive "[" "<") (substring tf 1 -1)
  15624. (if inactive "]" ">"))
  15625. (apply 'encode-time time))
  15626. string (replace-match str t t string)
  15627. start (+ start (length str)))))))
  15628. string)
  15629. (defun org-fix-decoded-time (time)
  15630. "Set 0 instead of nil for the first 6 elements of time.
  15631. Don't touch the rest."
  15632. (let ((n 0))
  15633. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15634. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15635. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15636. "Difference between TIMESTAMP-STRING and now in days.
  15637. If SECONDS is non-nil, return the difference in seconds."
  15638. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15639. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15640. (funcall fdiff (current-time)))))
  15641. (defun org-deadline-close (timestamp-string &optional ndays)
  15642. "Is the time in TIMESTAMP-STRING close to the current date?"
  15643. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15644. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15645. (not (org-entry-is-done-p))))
  15646. (defun org-get-wdays (ts &optional delay zero-delay)
  15647. "Get the deadline lead time appropriate for timestring TS.
  15648. When DELAY is non-nil, get the delay time for scheduled items
  15649. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15650. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15651. don't try to find the delay cookie in the scheduled timestamp."
  15652. (let ((tv (if delay org-scheduled-delay-days
  15653. org-deadline-warning-days)))
  15654. (cond
  15655. ((or (and delay (< tv 0))
  15656. (and delay zero-delay (<= tv 0))
  15657. (and (not delay) (<= tv 0)))
  15658. ;; Enforce this value no matter what
  15659. (- tv))
  15660. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15661. ;; lead time is specified.
  15662. (floor (* (string-to-number (match-string 1 ts))
  15663. (cdr (assoc (match-string 2 ts)
  15664. '(("d" . 1) ("w" . 7)
  15665. ("m" . 30.4) ("y" . 365.25)
  15666. ("h" . 0.041667)))))))
  15667. ;; go for the default.
  15668. (t tv))))
  15669. (defun org-calendar-select-mouse (ev)
  15670. "Return to `org-read-date' with the date currently selected.
  15671. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15672. (interactive "e")
  15673. (mouse-set-point ev)
  15674. (when (calendar-cursor-to-date)
  15675. (let* ((date (calendar-cursor-to-date))
  15676. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15677. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15678. (if (active-minibuffer-window) (exit-minibuffer))))
  15679. (defun org-check-deadlines (ndays)
  15680. "Check if there are any deadlines due or past due.
  15681. A deadline is considered due if it happens within `org-deadline-warning-days'
  15682. days from today's date. If the deadline appears in an entry marked DONE,
  15683. it is not shown. The prefix arg NDAYS can be used to test that many
  15684. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15685. (interactive "P")
  15686. (let* ((org-warn-days
  15687. (cond
  15688. ((equal ndays '(4)) 100000)
  15689. (ndays (prefix-numeric-value ndays))
  15690. (t (abs org-deadline-warning-days))))
  15691. (case-fold-search nil)
  15692. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15693. (callback
  15694. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15695. (message "%d deadlines past-due or due within %d days"
  15696. (org-occur regexp nil callback)
  15697. org-warn-days)))
  15698. (defsubst org-re-timestamp (type)
  15699. "Return a regexp for timestamp TYPE.
  15700. Allowed values for TYPE are:
  15701. all: all timestamps
  15702. active: only active timestamps (<...>)
  15703. inactive: only inactive timestamps ([...])
  15704. scheduled: only scheduled timestamps
  15705. deadline: only deadline timestamps
  15706. closed: only closed time-stamps
  15707. When TYPE is nil, fall back on returning a regexp that matches
  15708. both scheduled and deadline timestamps."
  15709. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15710. ((eq type 'active) org-ts-regexp)
  15711. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15712. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15713. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15714. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15715. ((eq type 'scheduled-or-deadline)
  15716. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15717. (defun org-check-before-date (date)
  15718. "Check if there are deadlines or scheduled entries before DATE."
  15719. (interactive (list (org-read-date)))
  15720. (let ((case-fold-search nil)
  15721. (regexp (org-re-timestamp org-ts-type))
  15722. (callback
  15723. (lambda () (time-less-p
  15724. (org-time-string-to-time (match-string 1))
  15725. (org-time-string-to-time date)))))
  15726. (message "%d entries before %s"
  15727. (org-occur regexp nil callback) date)))
  15728. (defun org-check-after-date (date)
  15729. "Check if there are deadlines or scheduled entries after DATE."
  15730. (interactive (list (org-read-date)))
  15731. (let ((case-fold-search nil)
  15732. (regexp (org-re-timestamp org-ts-type))
  15733. (callback
  15734. (lambda () (not
  15735. (time-less-p
  15736. (org-time-string-to-time (match-string 1))
  15737. (org-time-string-to-time date))))))
  15738. (message "%d entries after %s"
  15739. (org-occur regexp nil callback) date)))
  15740. (defun org-check-dates-range (start-date end-date)
  15741. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15742. (interactive (list (org-read-date nil nil nil "Range starts")
  15743. (org-read-date nil nil nil "Range end")))
  15744. (let ((case-fold-search nil)
  15745. (regexp (org-re-timestamp org-ts-type))
  15746. (callback
  15747. (lambda ()
  15748. (let ((match (match-string 1)))
  15749. (and
  15750. (not (time-less-p
  15751. (org-time-string-to-time match)
  15752. (org-time-string-to-time start-date)))
  15753. (time-less-p
  15754. (org-time-string-to-time match)
  15755. (org-time-string-to-time end-date)))))))
  15756. (message "%d entries between %s and %s"
  15757. (org-occur regexp nil callback) start-date end-date)))
  15758. (defun org-evaluate-time-range (&optional to-buffer)
  15759. "Evaluate a time range by computing the difference between start and end.
  15760. Normally the result is just printed in the echo area, but with prefix arg
  15761. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15762. If the time range is actually in a table, the result is inserted into the
  15763. next column.
  15764. For time difference computation, a year is assumed to be exactly 365
  15765. days in order to avoid rounding problems."
  15766. (interactive "P")
  15767. (or
  15768. (org-clock-update-time-maybe)
  15769. (save-excursion
  15770. (unless (org-at-date-range-p t)
  15771. (goto-char (point-at-bol))
  15772. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15773. (if (not (org-at-date-range-p t))
  15774. (user-error "Not at a time-stamp range, and none found in current line")))
  15775. (let* ((ts1 (match-string 1))
  15776. (ts2 (match-string 2))
  15777. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15778. (match-end (match-end 0))
  15779. (time1 (org-time-string-to-time ts1))
  15780. (time2 (org-time-string-to-time ts2))
  15781. (t1 (org-float-time time1))
  15782. (t2 (org-float-time time2))
  15783. (diff (abs (- t2 t1)))
  15784. (negative (< (- t2 t1) 0))
  15785. ;; (ys (floor (* 365 24 60 60)))
  15786. (ds (* 24 60 60))
  15787. (hs (* 60 60))
  15788. (fy "%dy %dd %02d:%02d")
  15789. (fy1 "%dy %dd")
  15790. (fd "%dd %02d:%02d")
  15791. (fd1 "%dd")
  15792. (fh "%02d:%02d")
  15793. y d h m align)
  15794. (if havetime
  15795. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15796. y 0
  15797. d (floor (/ diff ds)) diff (mod diff ds)
  15798. h (floor (/ diff hs)) diff (mod diff hs)
  15799. m (floor (/ diff 60)))
  15800. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15801. y 0
  15802. d (floor (+ (/ diff ds) 0.5))
  15803. h 0 m 0))
  15804. (if (not to-buffer)
  15805. (message "%s" (org-make-tdiff-string y d h m))
  15806. (if (org-at-table-p)
  15807. (progn
  15808. (goto-char match-end)
  15809. (setq align t)
  15810. (and (looking-at " *|") (goto-char (match-end 0))))
  15811. (goto-char match-end))
  15812. (if (looking-at
  15813. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15814. (replace-match ""))
  15815. (if negative (insert " -"))
  15816. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15817. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15818. (insert " " (format fh h m))))
  15819. (if align (org-table-align))
  15820. (message "Time difference inserted")))))
  15821. (defun org-make-tdiff-string (y d h m)
  15822. (let ((fmt "")
  15823. (l nil))
  15824. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15825. l (push y l)))
  15826. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15827. l (push d l)))
  15828. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15829. l (push h l)))
  15830. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15831. l (push m l)))
  15832. (apply 'format fmt (nreverse l))))
  15833. (defun org-time-string-to-time (s &optional buffer pos)
  15834. "Convert a timestamp string into internal time."
  15835. (condition-case errdata
  15836. (apply 'encode-time (org-parse-time-string s))
  15837. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15838. s (if (not (and buffer pos))
  15839. ""
  15840. (format " at %d in buffer `%s'" pos buffer))
  15841. (cdr errdata)))))
  15842. (defun org-time-string-to-seconds (s)
  15843. "Convert a timestamp string to a number of seconds."
  15844. (org-float-time (org-time-string-to-time s)))
  15845. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15846. "Convert a time stamp to an absolute day number.
  15847. If there is a specifier for a cyclic time stamp, get the closest
  15848. date to DAYNR.
  15849. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15850. The variable `date' is bound by the calendar when this is called."
  15851. (cond
  15852. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15853. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15854. daynr
  15855. (+ daynr 1000)))
  15856. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15857. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15858. (time-to-days (current-time))) (match-string 0 s)
  15859. prefer show-all))
  15860. (t (time-to-days
  15861. (condition-case errdata
  15862. (apply 'encode-time (org-parse-time-string s))
  15863. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15864. s (if (not (and buffer pos))
  15865. ""
  15866. (format " at %d in buffer `%s'" pos buffer))
  15867. (cdr errdata))))))))
  15868. (defun org-days-to-iso-week (days)
  15869. "Return the iso week number."
  15870. (require 'cal-iso)
  15871. (car (calendar-iso-from-absolute days)))
  15872. (defun org-small-year-to-year (year)
  15873. "Convert 2-digit years into 4-digit years.
  15874. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15875. The year 2000 cannot be abbreviated. Any year larger than 99
  15876. is returned unchanged."
  15877. (if (< year 38)
  15878. (setq year (+ 2000 year))
  15879. (if (< year 100)
  15880. (setq year (+ 1900 year))))
  15881. year)
  15882. (defun org-time-from-absolute (d)
  15883. "Return the time corresponding to date D.
  15884. D may be an absolute day number, or a calendar-type list (month day year)."
  15885. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15886. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15887. (defun org-calendar-holiday ()
  15888. "List of holidays, for Diary display in Org-mode."
  15889. (require 'holidays)
  15890. (let ((hl (funcall
  15891. (if (fboundp 'calendar-check-holidays)
  15892. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15893. (if hl (mapconcat 'identity hl "; "))))
  15894. (defun org-diary-sexp-entry (sexp entry date)
  15895. "Process a SEXP diary ENTRY for DATE."
  15896. (require 'diary-lib)
  15897. (let ((result (if calendar-debug-sexp
  15898. (let ((stack-trace-on-error t))
  15899. (eval (car (read-from-string sexp))))
  15900. (condition-case nil
  15901. (eval (car (read-from-string sexp)))
  15902. (error
  15903. (beep)
  15904. (message "Bad sexp at line %d in %s: %s"
  15905. (org-current-line)
  15906. (buffer-file-name) sexp)
  15907. (sleep-for 2))))))
  15908. (cond ((stringp result) (split-string result "; "))
  15909. ((and (consp result)
  15910. (not (consp (cdr result)))
  15911. (stringp (cdr result))) (cdr result))
  15912. ((and (consp result)
  15913. (stringp (car result))) result)
  15914. (result entry))))
  15915. (defun org-diary-to-ical-string (frombuf)
  15916. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15917. This uses the icalendar.el library."
  15918. (let* ((tmpdir (if (featurep 'xemacs)
  15919. (temp-directory)
  15920. temporary-file-directory))
  15921. (tmpfile (make-temp-name
  15922. (expand-file-name "orgics" tmpdir)))
  15923. buf rtn b e)
  15924. (with-current-buffer frombuf
  15925. (icalendar-export-region (point-min) (point-max) tmpfile)
  15926. (setq buf (find-buffer-visiting tmpfile))
  15927. (set-buffer buf)
  15928. (goto-char (point-min))
  15929. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15930. (setq b (match-beginning 0)))
  15931. (goto-char (point-max))
  15932. (if (re-search-backward "^END:VEVENT" nil t)
  15933. (setq e (match-end 0)))
  15934. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15935. (kill-buffer buf)
  15936. (delete-file tmpfile)
  15937. rtn))
  15938. (defun org-closest-date (start current change prefer show-all)
  15939. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15940. When PREFER is `past', return a date that is either CURRENT or past.
  15941. When PREFER is `future', return a date that is either CURRENT or future.
  15942. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15943. ;; Make the proper lists from the dates
  15944. (catch 'exit
  15945. (let ((a1 '(("h" . hour)
  15946. ("d" . day)
  15947. ("w" . week)
  15948. ("m" . month)
  15949. ("y" . year)))
  15950. (shour (nth 2 (org-parse-time-string start)))
  15951. dn dw sday cday n1 n2 n0
  15952. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15953. (setq start (org-date-to-gregorian start)
  15954. current (org-date-to-gregorian
  15955. (if show-all
  15956. current
  15957. (time-to-days (current-time))))
  15958. sday (calendar-absolute-from-gregorian start)
  15959. cday (calendar-absolute-from-gregorian current))
  15960. (if (<= cday sday) (throw 'exit sday))
  15961. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15962. (setq dn (string-to-number (match-string 1 change))
  15963. dw (cdr (assoc (match-string 2 change) a1)))
  15964. (user-error "Invalid change specifier: %s" change))
  15965. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15966. (cond
  15967. ((eq dw 'hour)
  15968. (let ((missing-hours
  15969. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15970. dn)))
  15971. (setq n1 (if (zerop missing-hours) cday
  15972. (- cday (1+ (floor (/ missing-hours 24)))))
  15973. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15974. ((eq dw 'day)
  15975. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15976. n2 (+ n1 dn)))
  15977. ((eq dw 'year)
  15978. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15979. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15980. (setq date1 (list m d y1)
  15981. n1 (calendar-absolute-from-gregorian date1)
  15982. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15983. n2 (calendar-absolute-from-gregorian date2)))
  15984. ((eq dw 'month)
  15985. ;; approx number of month between the two dates
  15986. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15987. ;; How often does dn fit in there?
  15988. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15989. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15990. m (+ m nm)
  15991. ny (floor (/ m 12))
  15992. y (+ y ny)
  15993. m (- m (* ny 12)))
  15994. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15995. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15996. (setq m2 (+ m dn) y2 y)
  15997. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15998. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15999. (while (<= n2 cday)
  16000. (setq n1 n2 m m2 y y2)
  16001. (setq m2 (+ m dn) y2 y)
  16002. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16003. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16004. ;; Make sure n1 is the earlier date
  16005. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  16006. (if show-all
  16007. (cond
  16008. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16009. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16010. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16011. (cond
  16012. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16013. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16014. (t (if (= cday n1) n1 n2)))))))
  16015. (defun org-date-to-gregorian (date)
  16016. "Turn any specification of DATE into a Gregorian date for the calendar."
  16017. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16018. ((and (listp date) (= (length date) 3)) date)
  16019. ((stringp date)
  16020. (setq date (org-parse-time-string date))
  16021. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16022. ((listp date)
  16023. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16024. (defun org-parse-time-string (s &optional nodefault)
  16025. "Parse the standard Org-mode time string.
  16026. This should be a lot faster than the normal `parse-time-string'.
  16027. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16028. hour and minute fields will be nil if not given."
  16029. (cond ((string-match org-ts-regexp0 s)
  16030. (list 0
  16031. (if (or (match-beginning 8) (not nodefault))
  16032. (string-to-number (or (match-string 8 s) "0")))
  16033. (if (or (match-beginning 7) (not nodefault))
  16034. (string-to-number (or (match-string 7 s) "0")))
  16035. (string-to-number (match-string 4 s))
  16036. (string-to-number (match-string 3 s))
  16037. (string-to-number (match-string 2 s))
  16038. nil nil nil))
  16039. ((string-match "^<[^>]+>$" s)
  16040. (decode-time (seconds-to-time (org-matcher-time s))))
  16041. (t (error "Not a standard Org-mode time string: %s" s))))
  16042. (defun org-timestamp-up (&optional arg)
  16043. "Increase the date item at the cursor by one.
  16044. If the cursor is on the year, change the year. If it is on the month,
  16045. the day or the time, change that.
  16046. With prefix ARG, change by that many units."
  16047. (interactive "p")
  16048. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16049. (defun org-timestamp-down (&optional arg)
  16050. "Decrease the date item at the cursor by one.
  16051. If the cursor is on the year, change the year. If it is on the month,
  16052. the day or the time, change that.
  16053. With prefix ARG, change by that many units."
  16054. (interactive "p")
  16055. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16056. (defun org-timestamp-up-day (&optional arg)
  16057. "Increase the date in the time stamp by one day.
  16058. With prefix ARG, change that many days."
  16059. (interactive "p")
  16060. (if (and (not (org-at-timestamp-p t))
  16061. (org-at-heading-p))
  16062. (org-todo 'up)
  16063. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16064. (defun org-timestamp-down-day (&optional arg)
  16065. "Decrease the date in the time stamp by one day.
  16066. With prefix ARG, change that many days."
  16067. (interactive "p")
  16068. (if (and (not (org-at-timestamp-p t))
  16069. (org-at-heading-p))
  16070. (org-todo 'down)
  16071. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16072. (defun org-at-timestamp-p (&optional inactive-ok)
  16073. "Determine if the cursor is in or at a timestamp."
  16074. (interactive)
  16075. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16076. (pos (point))
  16077. (ans (or (looking-at tsr)
  16078. (save-excursion
  16079. (skip-chars-backward "^[<\n\r\t")
  16080. (if (> (point) (point-min)) (backward-char 1))
  16081. (and (looking-at tsr)
  16082. (> (- (match-end 0) pos) -1))))))
  16083. (and ans
  16084. (boundp 'org-ts-what)
  16085. (setq org-ts-what
  16086. (cond
  16087. ((= pos (match-beginning 0)) 'bracket)
  16088. ;; Point is considered to be "on the bracket" whether
  16089. ;; it's really on it or right after it.
  16090. ((= pos (1- (match-end 0))) 'bracket)
  16091. ((= pos (match-end 0)) 'after)
  16092. ((org-pos-in-match-range pos 2) 'year)
  16093. ((org-pos-in-match-range pos 3) 'month)
  16094. ((org-pos-in-match-range pos 7) 'hour)
  16095. ((org-pos-in-match-range pos 8) 'minute)
  16096. ((or (org-pos-in-match-range pos 4)
  16097. (org-pos-in-match-range pos 5)) 'day)
  16098. ((and (> pos (or (match-end 8) (match-end 5)))
  16099. (< pos (match-end 0)))
  16100. (- pos (or (match-end 8) (match-end 5))))
  16101. (t 'day))))
  16102. ans))
  16103. (defun org-toggle-timestamp-type ()
  16104. "Toggle the type (<active> or [inactive]) of a time stamp."
  16105. (interactive)
  16106. (when (org-at-timestamp-p t)
  16107. (let ((beg (match-beginning 0)) (end (match-end 0))
  16108. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16109. (save-excursion
  16110. (goto-char beg)
  16111. (while (re-search-forward "[][<>]" end t)
  16112. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16113. t t)))
  16114. (message "Timestamp is now %sactive"
  16115. (if (equal (char-after beg) ?<) "" "in")))))
  16116. (defun org-at-clock-log-p nil
  16117. "Is the cursor on the clock log line?"
  16118. (save-excursion
  16119. (move-beginning-of-line 1)
  16120. (looking-at "^[ \t]*CLOCK:")))
  16121. (defvar org-clock-history) ; defined in org-clock.el
  16122. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16123. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16124. "Change the date in the time stamp at point.
  16125. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16126. `year', `minute', `second'. If WHAT is not given, the cursor position
  16127. in the timestamp determines what will be changed.
  16128. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16129. (let ((origin (point)) origin-cat
  16130. with-hm inactive
  16131. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16132. org-ts-what
  16133. extra rem
  16134. ts time time0 fixnext clrgx)
  16135. (if (not (org-at-timestamp-p t))
  16136. (user-error "Not at a timestamp"))
  16137. (if (and (not what) (eq org-ts-what 'bracket))
  16138. (org-toggle-timestamp-type)
  16139. ;; Point isn't on brackets. Remember the part of the time-stamp
  16140. ;; the point was in. Indeed, size of time-stamps may change,
  16141. ;; but point must be kept in the same category nonetheless.
  16142. (setq origin-cat org-ts-what)
  16143. (if (and (not what) (not (eq org-ts-what 'day))
  16144. org-display-custom-times
  16145. (get-text-property (point) 'display)
  16146. (not (get-text-property (1- (point)) 'display)))
  16147. (setq org-ts-what 'day))
  16148. (setq org-ts-what (or what org-ts-what)
  16149. inactive (= (char-after (match-beginning 0)) ?\[)
  16150. ts (match-string 0))
  16151. (replace-match "")
  16152. (when (string-match
  16153. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16154. ts)
  16155. (setq extra (match-string 1 ts))
  16156. (if suppress-tmp-delay
  16157. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16158. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16159. (setq with-hm t))
  16160. (setq time0 (org-parse-time-string ts))
  16161. (when (and updown
  16162. (eq org-ts-what 'minute)
  16163. (not current-prefix-arg))
  16164. ;; This looks like s-up and s-down. Change by one rounding step.
  16165. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16166. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  16167. (setcar (cdr time0) (+ (nth 1 time0)
  16168. (if (> n 0) (- rem) (- dm rem))))))
  16169. (setq time
  16170. (encode-time (or (car time0) 0)
  16171. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16172. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16173. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16174. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16175. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16176. (nthcdr 6 time0)))
  16177. (when (and (member org-ts-what '(hour minute))
  16178. extra
  16179. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16180. (setq extra (org-modify-ts-extra
  16181. extra
  16182. (if (eq org-ts-what 'hour) 2 5)
  16183. n dm)))
  16184. (when (integerp org-ts-what)
  16185. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16186. (if (eq what 'calendar)
  16187. (let ((cal-date (org-get-date-from-calendar)))
  16188. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16189. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16190. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16191. (setcar time0 (or (car time0) 0))
  16192. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16193. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16194. (setq time (apply 'encode-time time0))))
  16195. ;; Insert the new time-stamp, and ensure point stays in the same
  16196. ;; category as before (i.e. not after the last position in that
  16197. ;; category).
  16198. (let ((pos (point)))
  16199. ;; Stay before inserted string. `save-excursion' is of no use.
  16200. (setq org-last-changed-timestamp
  16201. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16202. (goto-char pos))
  16203. (save-match-data
  16204. (looking-at org-ts-regexp3)
  16205. (goto-char (cond
  16206. ;; `day' category ends before `hour' if any, or at
  16207. ;; the end of the day name.
  16208. ((eq origin-cat 'day)
  16209. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16210. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16211. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16212. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16213. ;; `year' and `month' have both fixed size: point
  16214. ;; couldn't have moved into another part.
  16215. (t origin))))
  16216. ;; Update clock if on a CLOCK line.
  16217. (org-clock-update-time-maybe)
  16218. ;; Maybe adjust the closest clock in `org-clock-history'
  16219. (when org-clock-adjust-closest
  16220. (if (not (and (org-at-clock-log-p)
  16221. (< 1 (length (delq nil (mapcar 'marker-position
  16222. org-clock-history))))))
  16223. (message "No clock to adjust")
  16224. (cond ((save-excursion ; fix previous clock?
  16225. (re-search-backward org-ts-regexp0 nil t)
  16226. (org-looking-back (concat org-clock-string " \\[")))
  16227. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16228. ((save-excursion ; fix next clock?
  16229. (re-search-backward org-ts-regexp0 nil t)
  16230. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16231. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16232. (save-window-excursion
  16233. ;; Find closest clock to point, adjust the previous/next one in history
  16234. (let* ((p (save-excursion (org-back-to-heading t)))
  16235. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16236. (clfixnth
  16237. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16238. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16239. (if (not clfixpos)
  16240. (message "No clock to adjust")
  16241. (save-excursion
  16242. (org-goto-marker-or-bmk clfixpos)
  16243. (org-show-subtree)
  16244. (when (re-search-forward clrgx nil t)
  16245. (goto-char (match-beginning 1))
  16246. (let (org-clock-adjust-closest)
  16247. (org-timestamp-change n org-ts-what updown))
  16248. (message "Clock adjusted in %s for heading: %s"
  16249. (file-name-nondirectory (buffer-file-name))
  16250. (org-get-heading t t)))))))))
  16251. ;; Try to recenter the calendar window, if any.
  16252. (if (and org-calendar-follow-timestamp-change
  16253. (get-buffer-window "*Calendar*" t)
  16254. (memq org-ts-what '(day month year)))
  16255. (org-recenter-calendar (time-to-days time))))))
  16256. (defun org-modify-ts-extra (s pos n dm)
  16257. "Change the different parts of the lead-time and repeat fields in timestamp."
  16258. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16259. ng h m new rem)
  16260. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16261. (cond
  16262. ((or (org-pos-in-match-range pos 2)
  16263. (org-pos-in-match-range pos 3))
  16264. (setq m (string-to-number (match-string 3 s))
  16265. h (string-to-number (match-string 2 s)))
  16266. (if (org-pos-in-match-range pos 2)
  16267. (setq h (+ h n))
  16268. (setq n (* dm (org-no-warnings (signum n))))
  16269. (when (not (= 0 (setq rem (% m dm))))
  16270. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16271. (setq m (+ m n)))
  16272. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16273. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16274. (setq h (min 24 (max 0 h)))
  16275. (setq ng 1 new (format "-%02d:%02d" h m)))
  16276. ((org-pos-in-match-range pos 6)
  16277. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16278. ((org-pos-in-match-range pos 5)
  16279. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16280. ((org-pos-in-match-range pos 9)
  16281. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16282. ((org-pos-in-match-range pos 8)
  16283. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16284. (when ng
  16285. (setq s (concat
  16286. (substring s 0 (match-beginning ng))
  16287. new
  16288. (substring s (match-end ng))))))
  16289. s))
  16290. (defun org-recenter-calendar (date)
  16291. "If the calendar is visible, recenter it to DATE."
  16292. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16293. (when cwin
  16294. (let ((calendar-move-hook nil))
  16295. (with-selected-window cwin
  16296. (calendar-goto-date (if (listp date) date
  16297. (calendar-gregorian-from-absolute date))))))))
  16298. (defun org-goto-calendar (&optional arg)
  16299. "Go to the Emacs calendar at the current date.
  16300. If there is a time stamp in the current line, go to that date.
  16301. A prefix ARG can be used to force the current date."
  16302. (interactive "P")
  16303. (let ((tsr org-ts-regexp) diff
  16304. (calendar-move-hook nil)
  16305. (calendar-view-holidays-initially-flag nil)
  16306. (calendar-view-diary-initially-flag nil))
  16307. (if (or (org-at-timestamp-p)
  16308. (save-excursion
  16309. (beginning-of-line 1)
  16310. (looking-at (concat ".*" tsr))))
  16311. (let ((d1 (time-to-days (current-time)))
  16312. (d2 (time-to-days
  16313. (org-time-string-to-time (match-string 1)))))
  16314. (setq diff (- d2 d1))))
  16315. (calendar)
  16316. (calendar-goto-today)
  16317. (if (and diff (not arg)) (calendar-forward-day diff))))
  16318. (defun org-get-date-from-calendar ()
  16319. "Return a list (month day year) of date at point in calendar."
  16320. (with-current-buffer "*Calendar*"
  16321. (save-match-data
  16322. (calendar-cursor-to-date))))
  16323. (defun org-date-from-calendar ()
  16324. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16325. If there is already a time stamp at the cursor position, update it."
  16326. (interactive)
  16327. (if (org-at-timestamp-p t)
  16328. (org-timestamp-change 0 'calendar)
  16329. (let ((cal-date (org-get-date-from-calendar)))
  16330. (org-insert-time-stamp
  16331. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16332. (defcustom org-effort-durations
  16333. `(("h" . 60)
  16334. ("d" . ,(* 60 8))
  16335. ("w" . ,(* 60 8 5))
  16336. ("m" . ,(* 60 8 5 4))
  16337. ("y" . ,(* 60 8 5 40)))
  16338. "Conversion factor to minutes for an effort modifier.
  16339. Each entry has the form (MODIFIER . MINUTES).
  16340. In an effort string, a number followed by MODIFIER is multiplied
  16341. by the specified number of MINUTES to obtain an effort in
  16342. minutes.
  16343. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16344. effort string \"2hours\" is equivalent to 120 minutes."
  16345. :group 'org-agenda
  16346. :version "24.1"
  16347. :type '(alist :key-type (string :tag "Modifier")
  16348. :value-type (number :tag "Minutes")))
  16349. (defun org-minutes-to-clocksum-string (m)
  16350. "Format number of minutes as a clocksum string.
  16351. The format is determined by `org-time-clocksum-format',
  16352. `org-time-clocksum-use-fractional' and
  16353. `org-time-clocksum-fractional-format' and
  16354. `org-time-clocksum-use-effort-durations'."
  16355. (let ((clocksum "")
  16356. (m (round m)) ; Don't allow fractions of minutes
  16357. h d w mo y fmt n)
  16358. (setq h (if org-time-clocksum-use-effort-durations
  16359. (cdr (assoc "h" org-effort-durations)) 60)
  16360. d (if org-time-clocksum-use-effort-durations
  16361. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16362. w (if org-time-clocksum-use-effort-durations
  16363. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16364. mo (if org-time-clocksum-use-effort-durations
  16365. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16366. y (if org-time-clocksum-use-effort-durations
  16367. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16368. ;; fractional format
  16369. (if org-time-clocksum-use-fractional
  16370. (cond
  16371. ;; single format string
  16372. ((stringp org-time-clocksum-fractional-format)
  16373. (format org-time-clocksum-fractional-format (/ m (float h))))
  16374. ;; choice of fractional formats for different time units
  16375. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16376. (> (/ (truncate m) (* y d h)) 0))
  16377. (format fmt (/ m (* y d (float h)))))
  16378. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16379. (> (/ (truncate m) (* mo d h)) 0))
  16380. (format fmt (/ m (* mo d (float h)))))
  16381. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16382. (> (/ (truncate m) (* w d h)) 0))
  16383. (format fmt (/ m (* w d (float h)))))
  16384. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16385. (> (/ (truncate m) (* d h)) 0))
  16386. (format fmt (/ m (* d (float h)))))
  16387. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16388. (> (/ (truncate m) h) 0))
  16389. (format fmt (/ m (float h))))
  16390. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16391. (format fmt m))
  16392. ;; fall back to smallest time unit with a format
  16393. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16394. (format fmt (/ m (float h))))
  16395. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16396. (format fmt (/ m (* d (float h)))))
  16397. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16398. (format fmt (/ m (* w d (float h)))))
  16399. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16400. (format fmt (/ m (* mo d (float h)))))
  16401. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16402. (format fmt (/ m (* y d (float h))))))
  16403. ;; standard (non-fractional) format, with single format string
  16404. (if (stringp org-time-clocksum-format)
  16405. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16406. ;; separate formats components
  16407. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16408. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16409. (plist-get org-time-clocksum-format :require-years))
  16410. (setq clocksum (concat clocksum (format fmt n))
  16411. m (- m (* n y d h))))
  16412. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16413. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16414. (plist-get org-time-clocksum-format :require-months))
  16415. (setq clocksum (concat clocksum (format fmt n))
  16416. m (- m (* n mo d h))))
  16417. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16418. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16419. (plist-get org-time-clocksum-format :require-weeks))
  16420. (setq clocksum (concat clocksum (format fmt n))
  16421. m (- m (* n w d h))))
  16422. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16423. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16424. (plist-get org-time-clocksum-format :require-days))
  16425. (setq clocksum (concat clocksum (format fmt n))
  16426. m (- m (* n d h))))
  16427. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16428. (or (> (setq n (/ (truncate m) h)) 0)
  16429. (plist-get org-time-clocksum-format :require-hours))
  16430. (setq clocksum (concat clocksum (format fmt n))
  16431. m (- m (* n h))))
  16432. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16433. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16434. (setq clocksum (concat clocksum (format fmt m))))
  16435. ;; return formatted time duration
  16436. clocksum))))
  16437. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16438. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16439. "Org mode version 8.0")
  16440. (defun org-hours-to-clocksum-string (n)
  16441. (org-minutes-to-clocksum-string (* n 60)))
  16442. (defun org-hh:mm-string-to-minutes (s)
  16443. "Convert a string H:MM to a number of minutes.
  16444. If the string is just a number, interpret it as minutes.
  16445. In fact, the first hh:mm or number in the string will be taken,
  16446. there can be extra stuff in the string.
  16447. If no number is found, the return value is 0."
  16448. (cond
  16449. ((integerp s) s)
  16450. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16451. (+ (* (string-to-number (match-string 1 s)) 60)
  16452. (string-to-number (match-string 2 s))))
  16453. ((string-match "\\([0-9]+\\)" s)
  16454. (string-to-number (match-string 1 s)))
  16455. (t 0)))
  16456. (defcustom org-image-actual-width t
  16457. "Should we use the actual width of images when inlining them?
  16458. When set to `t', always use the image width.
  16459. When set to a number, use imagemagick (when available) to set
  16460. the image's width to this value.
  16461. When set to a number in a list, try to get the width from any
  16462. #+ATTR.* keyword if it matches a width specification like
  16463. #+ATTR_HTML: :width 300px
  16464. and fall back on that number if none is found.
  16465. When set to nil, try to get the width from an #+ATTR.* keyword
  16466. and fall back on the original width if none is found.
  16467. This requires Emacs >= 24.1, build with imagemagick support."
  16468. :group 'org-appearance
  16469. :version "24.4"
  16470. :package-version '(Org . "8.0")
  16471. :type '(choice
  16472. (const :tag "Use the image width" t)
  16473. (integer :tag "Use a number of pixels")
  16474. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16475. (const :tag "Use #+ATTR* or don't resize" nil)))
  16476. (defcustom org-agenda-inhibit-startup nil
  16477. "Inhibit startup when preparing agenda buffers.
  16478. When this variable is `t', the initialization of the Org agenda
  16479. buffers is inhibited: e.g. the visibility state is not set, the
  16480. tables are not re-aligned, etc."
  16481. :type 'boolean
  16482. :version "24.3"
  16483. :group 'org-agenda)
  16484. (define-obsolete-variable-alias
  16485. 'org-agenda-ignore-drawer-properties
  16486. 'org-agenda-ignore-properties "24.5")
  16487. (defcustom org-agenda-ignore-properties nil
  16488. "Avoid updating text properties when building the agenda.
  16489. Properties are used to prepare buffers for effort estimates,
  16490. appointments, statistics and subtree-local categories.
  16491. If you don't use these in the agenda, you can add them to this
  16492. list and agenda building will be a bit faster.
  16493. The value is a list, with zero or more of the symbols `effort', `appt',
  16494. `stats' or `category'."
  16495. :type '(set :greedy t
  16496. (const effort)
  16497. (const appt)
  16498. (const stats)
  16499. (const category))
  16500. :version "24.5"
  16501. :package-version '(Org . "8.3")
  16502. :group 'org-agenda)
  16503. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16504. "Convert a duration string S to minutes.
  16505. A bare number is interpreted as minutes, modifiers can be set by
  16506. customizing `org-effort-durations' (which see).
  16507. Entries containing a colon are interpreted as H:MM by
  16508. `org-hh:mm-string-to-minutes'."
  16509. (let ((result 0)
  16510. (re (concat "\\([0-9.]+\\) *\\("
  16511. (regexp-opt (mapcar 'car org-effort-durations))
  16512. "\\)")))
  16513. (while (string-match re s)
  16514. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16515. (string-to-number (match-string 1 s))))
  16516. (setq s (replace-match "" nil t s)))
  16517. (setq result (floor result))
  16518. (incf result (org-hh:mm-string-to-minutes s))
  16519. (if output-to-string (number-to-string result) result)))
  16520. ;;;; Files
  16521. (defun org-save-all-org-buffers ()
  16522. "Save all Org-mode buffers without user confirmation."
  16523. (interactive)
  16524. (message "Saving all Org-mode buffers...")
  16525. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16526. (when (featurep 'org-id) (org-id-locations-save))
  16527. (message "Saving all Org-mode buffers... done"))
  16528. (defun org-revert-all-org-buffers ()
  16529. "Revert all Org-mode buffers.
  16530. Prompt for confirmation when there are unsaved changes.
  16531. Be sure you know what you are doing before letting this function
  16532. overwrite your changes.
  16533. This function is useful in a setup where one tracks org files
  16534. with a version control system, to revert on one machine after pulling
  16535. changes from another. I believe the procedure must be like this:
  16536. 1. M-x org-save-all-org-buffers
  16537. 2. Pull changes from the other machine, resolve conflicts
  16538. 3. M-x org-revert-all-org-buffers"
  16539. (interactive)
  16540. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16541. (user-error "Abort"))
  16542. (save-excursion
  16543. (save-window-excursion
  16544. (mapc
  16545. (lambda (b)
  16546. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16547. (with-current-buffer b buffer-file-name))
  16548. (org-pop-to-buffer-same-window b)
  16549. (revert-buffer t 'no-confirm)))
  16550. (buffer-list))
  16551. (when (and (featurep 'org-id) org-id-track-globally)
  16552. (org-id-locations-load)))))
  16553. ;;;; Agenda files
  16554. ;;;###autoload
  16555. (defun org-switchb (&optional arg)
  16556. "Switch between Org buffers.
  16557. With one prefix argument, restrict available buffers to files.
  16558. With two prefix arguments, restrict available buffers to agenda files.
  16559. Defaults to `iswitchb' for buffer name completion.
  16560. Set `org-completion-use-ido' to make it use ido instead."
  16561. (interactive "P")
  16562. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16563. ((equal arg '(16)) (org-buffer-list 'agenda))
  16564. (t (org-buffer-list))))
  16565. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16566. (org-completion-use-ido org-completion-use-ido))
  16567. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16568. (setq org-completion-use-iswitchb t))
  16569. (org-pop-to-buffer-same-window
  16570. (org-icompleting-read "Org buffer: "
  16571. (mapcar 'list (mapcar 'buffer-name blist))
  16572. nil t))))
  16573. ;;; Define some older names previously used for this functionality
  16574. ;;;###autoload
  16575. (defalias 'org-ido-switchb 'org-switchb)
  16576. ;;;###autoload
  16577. (defalias 'org-iswitchb 'org-switchb)
  16578. (defun org-buffer-list (&optional predicate exclude-tmp)
  16579. "Return a list of Org buffers.
  16580. PREDICATE can be `export', `files' or `agenda'.
  16581. export restrict the list to Export buffers.
  16582. files restrict the list to buffers visiting Org files.
  16583. agenda restrict the list to buffers visiting agenda files.
  16584. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16585. (let* ((bfn nil)
  16586. (agenda-files (and (eq predicate 'agenda)
  16587. (mapcar 'file-truename (org-agenda-files t))))
  16588. (filter
  16589. (cond
  16590. ((eq predicate 'files)
  16591. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16592. ((eq predicate 'export)
  16593. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16594. ((eq predicate 'agenda)
  16595. (lambda (b)
  16596. (with-current-buffer b
  16597. (and (derived-mode-p 'org-mode)
  16598. (setq bfn (buffer-file-name b))
  16599. (member (file-truename bfn) agenda-files)))))
  16600. (t (lambda (b) (with-current-buffer b
  16601. (or (derived-mode-p 'org-mode)
  16602. (string-match "\*Org .*Export"
  16603. (buffer-name b)))))))))
  16604. (delq nil
  16605. (mapcar
  16606. (lambda(b)
  16607. (if (and (funcall filter b)
  16608. (or (not exclude-tmp)
  16609. (not (string-match "tmp" (buffer-name b)))))
  16610. b
  16611. nil))
  16612. (buffer-list)))))
  16613. (defun org-agenda-files (&optional unrestricted archives)
  16614. "Get the list of agenda files.
  16615. Optional UNRESTRICTED means return the full list even if a restriction
  16616. is currently in place.
  16617. When ARCHIVES is t, include all archive files that are really being
  16618. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16619. `org-agenda-archives-mode' is t."
  16620. (let ((files
  16621. (cond
  16622. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16623. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16624. ((listp org-agenda-files) org-agenda-files)
  16625. (t (error "Invalid value of `org-agenda-files'")))))
  16626. (setq files (apply 'append
  16627. (mapcar (lambda (f)
  16628. (if (file-directory-p f)
  16629. (directory-files
  16630. f t org-agenda-file-regexp)
  16631. (list f)))
  16632. files)))
  16633. (when org-agenda-skip-unavailable-files
  16634. (setq files (delq nil
  16635. (mapcar (function
  16636. (lambda (file)
  16637. (and (file-readable-p file) file)))
  16638. files))))
  16639. (when (or (eq archives t)
  16640. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16641. (setq files (org-add-archive-files files)))
  16642. files))
  16643. (defun org-agenda-file-p (&optional file)
  16644. "Return non-nil, if FILE is an agenda file.
  16645. If FILE is omitted, use the file associated with the current
  16646. buffer."
  16647. (member (or file (buffer-file-name))
  16648. (org-agenda-files t)))
  16649. (defun org-edit-agenda-file-list ()
  16650. "Edit the list of agenda files.
  16651. Depending on setup, this either uses customize to edit the variable
  16652. `org-agenda-files', or it visits the file that is holding the list. In the
  16653. latter case, the buffer is set up in a way that saving it automatically kills
  16654. the buffer and restores the previous window configuration."
  16655. (interactive)
  16656. (if (stringp org-agenda-files)
  16657. (let ((cw (current-window-configuration)))
  16658. (find-file org-agenda-files)
  16659. (org-set-local 'org-window-configuration cw)
  16660. (org-add-hook 'after-save-hook
  16661. (lambda ()
  16662. (set-window-configuration
  16663. (prog1 org-window-configuration
  16664. (kill-buffer (current-buffer))))
  16665. (org-install-agenda-files-menu)
  16666. (message "New agenda file list installed"))
  16667. nil 'local)
  16668. (message "%s" (substitute-command-keys
  16669. "Edit list and finish with \\[save-buffer]")))
  16670. (customize-variable 'org-agenda-files)))
  16671. (defun org-store-new-agenda-file-list (list)
  16672. "Set new value for the agenda file list and save it correctly."
  16673. (if (stringp org-agenda-files)
  16674. (let ((fe (org-read-agenda-file-list t)) b u)
  16675. (while (setq b (find-buffer-visiting org-agenda-files))
  16676. (kill-buffer b))
  16677. (with-temp-file org-agenda-files
  16678. (insert
  16679. (mapconcat
  16680. (lambda (f) ;; Keep un-expanded entries.
  16681. (if (setq u (assoc f fe))
  16682. (cdr u)
  16683. f))
  16684. list "\n")
  16685. "\n")))
  16686. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16687. (org-insert-mode-line-in-empty-file nil))
  16688. (setq org-agenda-files list)
  16689. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16690. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16691. "Read the list of agenda files from a file.
  16692. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16693. filenames, used by `org-store-new-agenda-file-list' to write back
  16694. un-expanded file names."
  16695. (when (file-directory-p org-agenda-files)
  16696. (error "`org-agenda-files' cannot be a single directory"))
  16697. (when (stringp org-agenda-files)
  16698. (with-temp-buffer
  16699. (insert-file-contents org-agenda-files)
  16700. (mapcar
  16701. (lambda (f)
  16702. (let ((e (expand-file-name (substitute-in-file-name f)
  16703. org-directory)))
  16704. (if pair-with-expansion
  16705. (cons e f)
  16706. e)))
  16707. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16708. ;;;###autoload
  16709. (defun org-cycle-agenda-files ()
  16710. "Cycle through the files in `org-agenda-files'.
  16711. If the current buffer visits an agenda file, find the next one in the list.
  16712. If the current buffer does not, find the first agenda file."
  16713. (interactive)
  16714. (let* ((fs (org-agenda-files t))
  16715. (files (append fs (list (car fs))))
  16716. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16717. file)
  16718. (unless files (user-error "No agenda files"))
  16719. (catch 'exit
  16720. (while (setq file (pop files))
  16721. (if (equal (file-truename file) tcf)
  16722. (when (car files)
  16723. (find-file (car files))
  16724. (throw 'exit t))))
  16725. (find-file (car fs)))
  16726. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16727. (defun org-agenda-file-to-front (&optional to-end)
  16728. "Move/add the current file to the top of the agenda file list.
  16729. If the file is not present in the list, it is added to the front. If it is
  16730. present, it is moved there. With optional argument TO-END, add/move to the
  16731. end of the list."
  16732. (interactive "P")
  16733. (let ((org-agenda-skip-unavailable-files nil)
  16734. (file-alist (mapcar (lambda (x)
  16735. (cons (file-truename x) x))
  16736. (org-agenda-files t)))
  16737. (ctf (file-truename
  16738. (or buffer-file-name
  16739. (user-error "Please save the current buffer to a file"))))
  16740. x had)
  16741. (setq x (assoc ctf file-alist) had x)
  16742. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16743. (if to-end
  16744. (setq file-alist (append (delq x file-alist) (list x)))
  16745. (setq file-alist (cons x (delq x file-alist))))
  16746. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16747. (org-install-agenda-files-menu)
  16748. (message "File %s to %s of agenda file list"
  16749. (if had "moved" "added") (if to-end "end" "front"))))
  16750. (defun org-remove-file (&optional file)
  16751. "Remove current file from the list of files in variable `org-agenda-files'.
  16752. These are the files which are being checked for agenda entries.
  16753. Optional argument FILE means use this file instead of the current."
  16754. (interactive)
  16755. (let* ((org-agenda-skip-unavailable-files nil)
  16756. (file (or file buffer-file-name
  16757. (user-error "Current buffer does not visit a file")))
  16758. (true-file (file-truename file))
  16759. (afile (abbreviate-file-name file))
  16760. (files (delq nil (mapcar
  16761. (lambda (x)
  16762. (if (equal true-file
  16763. (file-truename x))
  16764. nil x))
  16765. (org-agenda-files t)))))
  16766. (if (not (= (length files) (length (org-agenda-files t))))
  16767. (progn
  16768. (org-store-new-agenda-file-list files)
  16769. (org-install-agenda-files-menu)
  16770. (message "Removed file: %s" afile))
  16771. (message "File was not in list: %s (not removed)" afile))))
  16772. (defun org-file-menu-entry (file)
  16773. (vector file (list 'find-file file) t))
  16774. (defun org-check-agenda-file (file)
  16775. "Make sure FILE exists. If not, ask user what to do."
  16776. (when (not (file-exists-p file))
  16777. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16778. (abbreviate-file-name file))
  16779. (let ((r (downcase (read-char-exclusive))))
  16780. (cond
  16781. ((equal r ?r)
  16782. (org-remove-file file)
  16783. (throw 'nextfile t))
  16784. (t (user-error "Abort"))))))
  16785. (defun org-get-agenda-file-buffer (file)
  16786. "Get an agenda buffer visiting FILE.
  16787. If the buffer needs to be created, add it to the list of buffers
  16788. which might be released later."
  16789. (let ((buf (org-find-base-buffer-visiting file)))
  16790. (if buf
  16791. buf ; just return it
  16792. ;; Make a new buffer and remember it
  16793. (setq buf (find-file-noselect file))
  16794. (if buf (push buf org-agenda-new-buffers))
  16795. buf)))
  16796. (defun org-release-buffers (blist)
  16797. "Release all buffers in list, asking the user for confirmation when needed.
  16798. When a buffer is unmodified, it is just killed. When modified, it is saved
  16799. \(if the user agrees) and then killed."
  16800. (let (buf file)
  16801. (while (setq buf (pop blist))
  16802. (setq file (buffer-file-name buf))
  16803. (when (and (buffer-modified-p buf)
  16804. file
  16805. (y-or-n-p (format "Save file %s? " file)))
  16806. (with-current-buffer buf (save-buffer)))
  16807. (kill-buffer buf))))
  16808. (defun org-agenda-prepare-buffers (files)
  16809. "Create buffers for all agenda files, protect archived trees and comments."
  16810. (interactive)
  16811. (let ((pa '(:org-archived t))
  16812. (pc '(:org-comment t))
  16813. (pall '(:org-archived t :org-comment t))
  16814. (inhibit-read-only t)
  16815. (org-inhibit-startup org-agenda-inhibit-startup)
  16816. (rea (concat ":" org-archive-tag ":"))
  16817. file re pos)
  16818. (setq org-tag-alist-for-agenda nil
  16819. org-tag-groups-alist-for-agenda nil)
  16820. (save-excursion
  16821. (save-restriction
  16822. (while (setq file (pop files))
  16823. (catch 'nextfile
  16824. (if (bufferp file)
  16825. (set-buffer file)
  16826. (org-check-agenda-file file)
  16827. (set-buffer (org-get-agenda-file-buffer file)))
  16828. (widen)
  16829. (org-set-regexps-and-options-for-tags)
  16830. (setq pos (point))
  16831. (goto-char (point-min))
  16832. (let ((case-fold-search t))
  16833. (when (search-forward "#+setupfile" nil t)
  16834. ;; Don't set all regexps and options systematically as
  16835. ;; this is only run for setting agenda tags from setup
  16836. ;; file
  16837. (org-set-regexps-and-options)))
  16838. (or (memq 'category org-agenda-ignore-properties)
  16839. (org-refresh-category-properties))
  16840. (or (memq 'stats org-agenda-ignore-properties)
  16841. (org-refresh-stats-properties))
  16842. (or (memq 'effort org-agenda-ignore-properties)
  16843. (org-refresh-effort-properties))
  16844. (or (memq 'appt org-agenda-ignore-properties)
  16845. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  16846. (setq org-todo-keywords-for-agenda
  16847. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16848. (setq org-done-keywords-for-agenda
  16849. (append org-done-keywords-for-agenda org-done-keywords))
  16850. (setq org-todo-keyword-alist-for-agenda
  16851. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16852. (setq org-tag-alist-for-agenda
  16853. (org-uniquify
  16854. (append org-tag-alist-for-agenda
  16855. org-tag-alist
  16856. org-tag-persistent-alist)))
  16857. (if org-group-tags
  16858. (setq org-tag-groups-alist-for-agenda
  16859. (org-uniquify-alist
  16860. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16861. (org-with-silent-modifications
  16862. (save-excursion
  16863. (remove-text-properties (point-min) (point-max) pall)
  16864. (when org-agenda-skip-archived-trees
  16865. (goto-char (point-min))
  16866. (while (re-search-forward rea nil t)
  16867. (if (org-at-heading-p t)
  16868. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16869. (goto-char (point-min))
  16870. (setq re (format "^\\* .*\\<%s\\>" org-comment-string))
  16871. (while (re-search-forward re nil t)
  16872. (when (save-match-data (org-in-commented-heading-p t))
  16873. (add-text-properties
  16874. (match-beginning 0) (org-end-of-subtree t) pc)))))
  16875. (goto-char pos)))))
  16876. (setq org-todo-keywords-for-agenda
  16877. (org-uniquify org-todo-keywords-for-agenda))
  16878. (setq org-todo-keyword-alist-for-agenda
  16879. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16880. ;;;; CDLaTeX minor mode
  16881. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16882. "Keymap for the minor `org-cdlatex-mode'.")
  16883. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16884. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16885. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16886. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16887. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  16888. (defvar org-cdlatex-texmathp-advice-is-done nil
  16889. "Flag remembering if we have applied the advice to texmathp already.")
  16890. (define-minor-mode org-cdlatex-mode
  16891. "Toggle the minor `org-cdlatex-mode'.
  16892. This mode supports entering LaTeX environment and math in LaTeX fragments
  16893. in Org-mode.
  16894. \\{org-cdlatex-mode-map}"
  16895. nil " OCDL" nil
  16896. (when org-cdlatex-mode
  16897. (require 'cdlatex)
  16898. (run-hooks 'cdlatex-mode-hook)
  16899. (cdlatex-compute-tables))
  16900. (unless org-cdlatex-texmathp-advice-is-done
  16901. (setq org-cdlatex-texmathp-advice-is-done t)
  16902. (defadvice texmathp (around org-math-always-on activate)
  16903. "Always return t in org-mode buffers.
  16904. This is because we want to insert math symbols without dollars even outside
  16905. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16906. an embedded LaTeX fragment, let texmathp do its job.
  16907. \\[org-cdlatex-mode-map]"
  16908. (interactive)
  16909. (let (p)
  16910. (cond
  16911. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16912. ((eq this-command 'cdlatex-math-symbol)
  16913. (setq ad-return-value t
  16914. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16915. (t
  16916. (let ((p (org-inside-LaTeX-fragment-p)))
  16917. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16918. (setq ad-return-value t
  16919. texmathp-why '("Org-mode embedded math" . 0))
  16920. (if p ad-do-it)))))))))
  16921. (defun turn-on-org-cdlatex ()
  16922. "Unconditionally turn on `org-cdlatex-mode'."
  16923. (org-cdlatex-mode 1))
  16924. (defun org-try-cdlatex-tab ()
  16925. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16926. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16927. - inside a LaTeX fragment, or
  16928. - after the first word in a line, where an abbreviation expansion could
  16929. insert a LaTeX environment."
  16930. (when org-cdlatex-mode
  16931. (cond
  16932. ;; Before any word on the line: No expansion possible.
  16933. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16934. ;; Just after first word on the line: Expand it. Make sure it
  16935. ;; cannot happen on headlines, though.
  16936. ((save-excursion
  16937. (skip-chars-backward "a-zA-Z0-9*")
  16938. (skip-chars-backward " \t")
  16939. (and (bolp) (not (org-at-heading-p))))
  16940. (cdlatex-tab) t)
  16941. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16942. (defun org-cdlatex-underscore-caret (&optional arg)
  16943. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16944. Revert to the normal definition outside of these fragments."
  16945. (interactive "P")
  16946. (if (org-inside-LaTeX-fragment-p)
  16947. (call-interactively 'cdlatex-sub-superscript)
  16948. (let (org-cdlatex-mode)
  16949. (call-interactively (key-binding (vector last-input-event))))))
  16950. (defun org-cdlatex-math-modify (&optional arg)
  16951. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16952. Revert to the normal definition outside of these fragments."
  16953. (interactive "P")
  16954. (if (org-inside-LaTeX-fragment-p)
  16955. (call-interactively 'cdlatex-math-modify)
  16956. (let (org-cdlatex-mode)
  16957. (call-interactively (key-binding (vector last-input-event))))))
  16958. (defun org-cdlatex-environment-indent (&optional environment item)
  16959. "Execute `cdlatex-environment' and indent the inserted environment."
  16960. (interactive)
  16961. (cdlatex-environment environment item)
  16962. (let ((element (org-element-at-point)))
  16963. (org-indent-region (org-element-property :begin element)
  16964. (org-element-property :end element))))
  16965. ;;;; LaTeX fragments
  16966. (defun org-inside-LaTeX-fragment-p ()
  16967. "Test if point is inside a LaTeX fragment.
  16968. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16969. sequence appearing also before point.
  16970. Even though the matchers for math are configurable, this function assumes
  16971. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16972. delimiters are skipped when they have been removed by customization.
  16973. The return value is nil, or a cons cell with the delimiter and the
  16974. position of this delimiter.
  16975. This function does a reasonably good job, but can locally be fooled by
  16976. for example currency specifications. For example it will assume being in
  16977. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16978. fragments that are properly closed, but during editing, we have to live
  16979. with the uncertainty caused by missing closing delimiters. This function
  16980. looks only before point, not after."
  16981. (catch 'exit
  16982. (let ((pos (point))
  16983. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16984. (lim (progn
  16985. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16986. (point)))
  16987. dd-on str (start 0) m re)
  16988. (goto-char pos)
  16989. (when dodollar
  16990. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16991. re (nth 1 (assoc "$" org-latex-regexps)))
  16992. (while (string-match re str start)
  16993. (cond
  16994. ((= (match-end 0) (length str))
  16995. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16996. ((= (match-end 0) (- (length str) 5))
  16997. (throw 'exit nil))
  16998. (t (setq start (match-end 0))))))
  16999. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17000. (goto-char pos)
  17001. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17002. (and (match-beginning 2) (throw 'exit nil))
  17003. ;; count $$
  17004. (while (re-search-backward "\\$\\$" lim t)
  17005. (setq dd-on (not dd-on)))
  17006. (goto-char pos)
  17007. (if dd-on (cons "$$" m))))))
  17008. (defun org-inside-latex-macro-p ()
  17009. "Is point inside a LaTeX macro or its arguments?"
  17010. (save-match-data
  17011. (org-in-regexp
  17012. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17013. (defvar org-latex-fragment-image-overlays nil
  17014. "List of overlays carrying the images of latex fragments.")
  17015. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  17016. (defun org-remove-latex-fragment-image-overlays ()
  17017. "Remove all overlays with LaTeX fragment images in current buffer."
  17018. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  17019. (setq org-latex-fragment-image-overlays nil))
  17020. (define-obsolete-function-alias
  17021. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17022. (defun org-toggle-latex-fragment (&optional subtree)
  17023. "Preview the LaTeX fragment at point, or all locally or globally.
  17024. If the cursor is in a LaTeX fragment, create the image and overlay
  17025. it over the source code. If there is no fragment at point, display
  17026. all fragments in the current text, from one headline to the next. With
  17027. prefix SUBTREE, display all fragments in the current subtree. With a
  17028. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  17029. the cursor is before the first headline,
  17030. display all fragments in the buffer.
  17031. The images can be removed again with \\[org-toggle-latex-fragment]."
  17032. (interactive "P")
  17033. (unless buffer-file-name
  17034. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  17035. (if org-latex-fragment-image-overlays
  17036. (progn (org-remove-latex-fragment-image-overlays)
  17037. (message "LaTeX fragments images removed"))
  17038. (when (display-graphic-p)
  17039. (org-remove-latex-fragment-image-overlays)
  17040. (save-excursion
  17041. (save-restriction
  17042. (let (beg end at msg)
  17043. (cond
  17044. ((or (equal subtree '(16))
  17045. (not (save-excursion
  17046. (re-search-backward org-outline-regexp-bol nil t))))
  17047. (setq beg (point-min) end (point-max)
  17048. msg "Creating images for buffer...%s"))
  17049. ((equal subtree '(4))
  17050. (org-back-to-heading)
  17051. (setq beg (point) end (org-end-of-subtree t)
  17052. msg "Creating images for subtree...%s"))
  17053. (t
  17054. (if (setq at (org-inside-LaTeX-fragment-p))
  17055. (goto-char (max (point-min) (- (cdr at) 2)))
  17056. (org-back-to-heading))
  17057. (setq beg (point) end (progn (outline-next-heading) (point))
  17058. msg (if at "Creating image...%s"
  17059. "Creating images for entry...%s"))))
  17060. (message msg "")
  17061. (narrow-to-region beg end)
  17062. (goto-char beg)
  17063. (org-format-latex
  17064. (concat org-latex-preview-ltxpng-directory
  17065. (file-name-sans-extension
  17066. (file-name-nondirectory buffer-file-name)))
  17067. default-directory 'overlays msg at 'forbuffer
  17068. org-latex-create-formula-image-program)
  17069. (message msg "done. Use `C-c C-x C-l' to remove images.")))))))
  17070. (defun org-format-latex (prefix &optional dir overlays msg at
  17071. forbuffer processing-type)
  17072. "Replace LaTeX fragments with links to an image, and produce images.
  17073. Some of the options can be changed using the variable
  17074. `org-format-latex-options'."
  17075. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17076. (let* ((prefixnodir (file-name-nondirectory prefix))
  17077. (absprefix (expand-file-name prefix dir))
  17078. (todir (file-name-directory absprefix))
  17079. (opt org-format-latex-options)
  17080. (optnew org-format-latex-options)
  17081. (matchers (plist-get opt :matchers))
  17082. (re-list org-latex-regexps)
  17083. (cnt 0) txt hash link beg end re e checkdir
  17084. string
  17085. m n block-type block linkfile movefile ov)
  17086. ;; Check the different regular expressions
  17087. (while (setq e (pop re-list))
  17088. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  17089. block (if block-type "\n\n" ""))
  17090. (when (member m matchers)
  17091. (goto-char (point-min))
  17092. (while (re-search-forward re nil t)
  17093. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  17094. (or (not overlays)
  17095. (not (eq (get-char-property (match-beginning n)
  17096. 'org-overlay-type)
  17097. 'org-latex-overlay))))
  17098. (cond
  17099. ((eq processing-type 'verbatim))
  17100. ((eq processing-type 'mathjax)
  17101. ;; Prepare for MathJax processing.
  17102. (setq string (match-string n))
  17103. (when (member m '("$" "$1"))
  17104. (save-excursion
  17105. (delete-region (match-beginning n) (match-end n))
  17106. (goto-char (match-beginning n))
  17107. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  17108. ((or (eq processing-type 'dvipng)
  17109. (eq processing-type 'imagemagick))
  17110. ;; Process to an image.
  17111. (setq txt (match-string n)
  17112. beg (match-beginning n) end (match-end n)
  17113. cnt (1+ cnt))
  17114. (let ((face (face-at-point))
  17115. (fg (plist-get opt :foreground))
  17116. (bg (plist-get opt :background))
  17117. ;; Ensure full list is printed.
  17118. print-length print-level)
  17119. (when forbuffer
  17120. ;; Get the colors from the face at point.
  17121. (goto-char beg)
  17122. (when (eq fg 'auto)
  17123. (setq fg (face-attribute face :foreground nil 'default)))
  17124. (when (eq bg 'auto)
  17125. (setq bg (face-attribute face :background nil 'default)))
  17126. (setq optnew (copy-sequence opt))
  17127. (plist-put optnew :foreground fg)
  17128. (plist-put optnew :background bg))
  17129. (setq hash (sha1 (prin1-to-string
  17130. (list org-format-latex-header
  17131. org-latex-default-packages-alist
  17132. org-latex-packages-alist
  17133. org-format-latex-options
  17134. forbuffer txt fg bg)))
  17135. linkfile (format "%s_%s.png" prefix hash)
  17136. movefile (format "%s_%s.png" absprefix hash)))
  17137. (setq link (concat block "[[file:" linkfile "]]" block))
  17138. (if msg (message msg cnt))
  17139. (goto-char beg)
  17140. (unless checkdir ; Ensure the directory exists.
  17141. (setq checkdir t)
  17142. (or (file-directory-p todir) (make-directory todir t)))
  17143. (unless (file-exists-p movefile)
  17144. (org-create-formula-image
  17145. txt movefile optnew forbuffer processing-type))
  17146. (if overlays
  17147. (progn
  17148. (mapc (lambda (o)
  17149. (if (eq (overlay-get o 'org-overlay-type)
  17150. 'org-latex-overlay)
  17151. (delete-overlay o)))
  17152. (overlays-in beg end))
  17153. (setq ov (make-overlay beg end))
  17154. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  17155. (if (featurep 'xemacs)
  17156. (progn
  17157. (overlay-put ov 'invisible t)
  17158. (overlay-put
  17159. ov 'end-glyph
  17160. (make-glyph (vector 'png :file movefile))))
  17161. (overlay-put
  17162. ov 'display
  17163. (list 'image :type 'png :file movefile :ascent 'center)))
  17164. (push ov org-latex-fragment-image-overlays)
  17165. (goto-char end))
  17166. (delete-region beg end)
  17167. (insert (org-add-props link
  17168. (list 'org-latex-src
  17169. (replace-regexp-in-string
  17170. "\"" "" txt)
  17171. 'org-latex-src-embed-type
  17172. (if block-type 'paragraph 'character))))))
  17173. ((eq processing-type 'mathml)
  17174. ;; Process to MathML
  17175. (unless (save-match-data (org-format-latex-mathml-available-p))
  17176. (user-error "LaTeX to MathML converter not configured"))
  17177. (setq txt (match-string n)
  17178. beg (match-beginning n) end (match-end n)
  17179. cnt (1+ cnt))
  17180. (if msg (message msg cnt))
  17181. (goto-char beg)
  17182. (delete-region beg end)
  17183. (insert (org-format-latex-as-mathml
  17184. txt block-type prefix dir)))
  17185. (t
  17186. (error "Unknown conversion type %s for LaTeX fragments"
  17187. processing-type)))))))))
  17188. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17189. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17190. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17191. sucessful, return the portion between \"<math...> </math>\"
  17192. elements otherwise return nil. When MATHML-FILE is specified,
  17193. write the results in to that file. When invoked as an
  17194. interactive command, prompt for LATEX-FRAG, with initial value
  17195. set to the current active region and echo the results for user
  17196. inspection."
  17197. (interactive (list (let ((frag (when (org-region-active-p)
  17198. (buffer-substring-no-properties
  17199. (region-beginning) (region-end)))))
  17200. (read-string "LaTeX Fragment: " frag nil frag))))
  17201. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17202. (let* ((tmp-in-file (file-relative-name
  17203. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17204. (ignore (write-region latex-frag nil tmp-in-file))
  17205. (tmp-out-file (file-relative-name
  17206. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17207. (cmd (format-spec
  17208. org-latex-to-mathml-convert-command
  17209. `((?j . ,(shell-quote-argument
  17210. (expand-file-name org-latex-to-mathml-jar-file)))
  17211. (?I . ,(shell-quote-argument tmp-in-file))
  17212. (?o . ,(shell-quote-argument tmp-out-file)))))
  17213. mathml shell-command-output)
  17214. (when (org-called-interactively-p 'any)
  17215. (unless (org-format-latex-mathml-available-p)
  17216. (user-error "LaTeX to MathML converter not configured")))
  17217. (message "Running %s" cmd)
  17218. (setq shell-command-output (shell-command-to-string cmd))
  17219. (setq mathml
  17220. (when (file-readable-p tmp-out-file)
  17221. (with-current-buffer (find-file-noselect tmp-out-file t)
  17222. (goto-char (point-min))
  17223. (when (re-search-forward
  17224. (concat
  17225. (regexp-quote
  17226. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  17227. "\\(.\\|\n\\)*"
  17228. (regexp-quote "</math>")) nil t)
  17229. (prog1 (match-string 0) (kill-buffer))))))
  17230. (cond
  17231. (mathml
  17232. (setq mathml
  17233. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17234. (when mathml-file
  17235. (write-region mathml nil mathml-file))
  17236. (when (org-called-interactively-p 'any)
  17237. (message mathml)))
  17238. ((message "LaTeX to MathML conversion failed")
  17239. (message shell-command-output)))
  17240. (delete-file tmp-in-file)
  17241. (when (file-exists-p tmp-out-file)
  17242. (delete-file tmp-out-file))
  17243. mathml))
  17244. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17245. prefix &optional dir)
  17246. "Use `org-create-math-formula' but check local cache first."
  17247. (let* ((absprefix (expand-file-name prefix dir))
  17248. (print-length nil) (print-level nil)
  17249. (formula-id (concat
  17250. "formula-"
  17251. (sha1
  17252. (prin1-to-string
  17253. (list latex-frag
  17254. org-latex-to-mathml-convert-command)))))
  17255. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17256. (formula-cache-dir (file-name-directory formula-cache)))
  17257. (unless (file-directory-p formula-cache-dir)
  17258. (make-directory formula-cache-dir t))
  17259. (unless (file-exists-p formula-cache)
  17260. (org-create-math-formula latex-frag formula-cache))
  17261. (if (file-exists-p formula-cache)
  17262. ;; Successful conversion. Return the link to MathML file.
  17263. (org-add-props
  17264. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17265. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17266. 'org-latex-src-embed-type (if latex-frag-type
  17267. 'paragraph 'character)))
  17268. ;; Failed conversion. Return the LaTeX fragment verbatim
  17269. latex-frag)))
  17270. (defun org-create-formula-image (string tofile options buffer &optional type)
  17271. "Create an image from LaTeX source using dvipng or convert.
  17272. This function calls either `org-create-formula-image-with-dvipng'
  17273. or `org-create-formula-image-with-imagemagick' depending on the
  17274. value of `org-latex-create-formula-image-program' or on the value
  17275. of the optional TYPE variable.
  17276. Note: ultimately these two function should be combined as they
  17277. share a good deal of logic."
  17278. (org-check-external-command
  17279. "latex" "needed to convert LaTeX fragments to images")
  17280. (funcall
  17281. (case (or type org-latex-create-formula-image-program)
  17282. ('dvipng
  17283. (org-check-external-command
  17284. "dvipng" "needed to convert LaTeX fragments to images")
  17285. 'org-create-formula-image-with-dvipng)
  17286. ('imagemagick
  17287. (org-check-external-command
  17288. "convert" "you need to install imagemagick")
  17289. 'org-create-formula-image-with-imagemagick)
  17290. (t (error
  17291. "Invalid value of `org-latex-create-formula-image-program'")))
  17292. string tofile options buffer))
  17293. (declare-function org-export-get-backend "ox" (name))
  17294. (declare-function org-export--get-global-options "ox" (&optional backend))
  17295. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17296. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17297. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17298. (defun org-create-formula--latex-header ()
  17299. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17300. (let ((info (org-combine-plists (org-export--get-global-options
  17301. (org-export-get-backend 'latex))
  17302. (org-export--get-inbuffer-options
  17303. (org-export-get-backend 'latex)))))
  17304. (org-latex-guess-babel-language
  17305. (org-latex-guess-inputenc
  17306. (org-splice-latex-header
  17307. org-format-latex-header
  17308. org-latex-default-packages-alist
  17309. org-latex-packages-alist t
  17310. (plist-get info :latex-header)))
  17311. info)))
  17312. ;; This function borrows from Ganesh Swami's latex2png.el
  17313. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17314. "This calls dvipng."
  17315. (require 'ox-latex)
  17316. (let* ((tmpdir (if (featurep 'xemacs)
  17317. (temp-directory)
  17318. temporary-file-directory))
  17319. (texfilebase (make-temp-name
  17320. (expand-file-name "orgtex" tmpdir)))
  17321. (texfile (concat texfilebase ".tex"))
  17322. (dvifile (concat texfilebase ".dvi"))
  17323. (pngfile (concat texfilebase ".png"))
  17324. (fnh (if (featurep 'xemacs)
  17325. (font-height (face-font 'default))
  17326. (face-attribute 'default :height nil)))
  17327. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17328. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17329. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17330. "Black"))
  17331. (bg (or (plist-get options (if buffer :background :html-background))
  17332. "Transparent")))
  17333. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17334. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17335. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17336. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17337. (let ((latex-header (org-create-formula--latex-header)))
  17338. (with-temp-file texfile
  17339. (insert latex-header)
  17340. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17341. (let ((dir default-directory))
  17342. (ignore-errors
  17343. (cd tmpdir)
  17344. (call-process "latex" nil nil nil texfile))
  17345. (cd dir))
  17346. (if (not (file-exists-p dvifile))
  17347. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17348. (ignore-errors
  17349. (if (featurep 'xemacs)
  17350. (call-process "dvipng" nil nil nil
  17351. "-fg" fg "-bg" bg
  17352. "-T" "tight"
  17353. "-o" pngfile
  17354. dvifile)
  17355. (call-process "dvipng" nil nil nil
  17356. "-fg" fg "-bg" bg
  17357. "-D" dpi
  17358. ;;"-x" scale "-y" scale
  17359. "-T" "tight"
  17360. "-o" pngfile
  17361. dvifile)))
  17362. (if (not (file-exists-p pngfile))
  17363. (if org-format-latex-signal-error
  17364. (error "Failed to create png file from %s" texfile)
  17365. (message "Failed to create png file from %s" texfile)
  17366. nil)
  17367. ;; Use the requested file name and clean up
  17368. (copy-file pngfile tofile 'replace)
  17369. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17370. (if (file-exists-p (concat texfilebase e))
  17371. (delete-file (concat texfilebase e))))
  17372. pngfile))))
  17373. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17374. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17375. "This calls convert, which is included into imagemagick."
  17376. (require 'ox-latex)
  17377. (let* ((tmpdir (if (featurep 'xemacs)
  17378. (temp-directory)
  17379. temporary-file-directory))
  17380. (texfilebase (make-temp-name
  17381. (expand-file-name "orgtex" tmpdir)))
  17382. (texfile (concat texfilebase ".tex"))
  17383. (pdffile (concat texfilebase ".pdf"))
  17384. (pngfile (concat texfilebase ".png"))
  17385. (fnh (if (featurep 'xemacs)
  17386. (font-height (face-font 'default))
  17387. (face-attribute 'default :height nil)))
  17388. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17389. (dpi (number-to-string (* scale (floor (if buffer fnh 120.)))))
  17390. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17391. "black"))
  17392. (bg (or (plist-get options (if buffer :background :html-background))
  17393. "white")))
  17394. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17395. (setq fg (org-latex-color-format fg)))
  17396. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17397. (setq bg (org-latex-color-format
  17398. (if (string= bg "Transparent") "white" bg))))
  17399. (let ((latex-header (org-create-formula--latex-header)))
  17400. (with-temp-file texfile
  17401. (insert latex-header)
  17402. (insert "\n\\begin{document}\n"
  17403. "\\definecolor{fg}{rgb}{" fg "}\n"
  17404. "\\definecolor{bg}{rgb}{" bg "}\n"
  17405. "\n\\pagecolor{bg}\n"
  17406. "\n{\\color{fg}\n"
  17407. string
  17408. "\n}\n"
  17409. "\n\\end{document}\n")))
  17410. (org-latex-compile texfile t)
  17411. (if (not (file-exists-p pdffile))
  17412. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17413. (ignore-errors
  17414. (if (featurep 'xemacs)
  17415. (call-process "convert" nil nil nil
  17416. "-density" "96"
  17417. "-trim"
  17418. "-antialias"
  17419. pdffile
  17420. "-quality" "100"
  17421. ;; "-sharpen" "0x1.0"
  17422. pngfile)
  17423. (call-process "convert" nil nil nil
  17424. "-density" dpi
  17425. "-trim"
  17426. "-antialias"
  17427. pdffile
  17428. "-quality" "100"
  17429. ;; "-sharpen" "0x1.0"
  17430. pngfile)))
  17431. (if (not (file-exists-p pngfile))
  17432. (if org-format-latex-signal-error
  17433. (error "Failed to create png file from %s" texfile)
  17434. (message "Failed to create png file from %s" texfile)
  17435. nil)
  17436. ;; Use the requested file name and clean up
  17437. (copy-file pngfile tofile 'replace)
  17438. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17439. (if (file-exists-p (concat texfilebase e))
  17440. (delete-file (concat texfilebase e))))
  17441. pngfile))))
  17442. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17443. "Fill a LaTeX header template TPL.
  17444. In the template, the following place holders will be recognized:
  17445. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17446. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17447. [PACKAGES] \\usepackage statements for PKG
  17448. [NO-PACKAGES] do not include PKG
  17449. [EXTRA] the string EXTRA
  17450. [NO-EXTRA] do not include EXTRA
  17451. For backward compatibility, if both the positive and the negative place
  17452. holder is missing, the positive one (without the \"NO-\") will be
  17453. assumed to be present at the end of the template.
  17454. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17455. EXTRA is a string.
  17456. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17457. (let (rpl (end ""))
  17458. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17459. (setq rpl (if (or (match-end 1) (not def-pkg))
  17460. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17461. tpl (replace-match rpl t t tpl))
  17462. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17463. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17464. (setq rpl (if (or (match-end 1) (not pkg))
  17465. "" (org-latex-packages-to-string pkg snippets-p t))
  17466. tpl (replace-match rpl t t tpl))
  17467. (if pkg (setq end
  17468. (concat end "\n"
  17469. (org-latex-packages-to-string pkg snippets-p)))))
  17470. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17471. (setq rpl (if (or (match-end 1) (not extra))
  17472. "" (concat extra "\n"))
  17473. tpl (replace-match rpl t t tpl))
  17474. (if (and extra (string-match "\\S-" extra))
  17475. (setq end (concat end "\n" extra))))
  17476. (if (string-match "\\S-" end)
  17477. (concat tpl "\n" end)
  17478. tpl)))
  17479. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17480. "Turn an alist of packages into a string with the \\usepackage macros."
  17481. (setq pkg (mapconcat (lambda(p)
  17482. (cond
  17483. ((stringp p) p)
  17484. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17485. (format "%% Package %s omitted" (cadr p)))
  17486. ((equal "" (car p))
  17487. (format "\\usepackage{%s}" (cadr p)))
  17488. (t
  17489. (format "\\usepackage[%s]{%s}"
  17490. (car p) (cadr p)))))
  17491. pkg
  17492. "\n"))
  17493. (if newline (concat pkg "\n") pkg))
  17494. (defun org-dvipng-color (attr)
  17495. "Return a RGB color specification for dvipng."
  17496. (apply 'format "rgb %s %s %s"
  17497. (mapcar 'org-normalize-color
  17498. (if (featurep 'xemacs)
  17499. (color-rgb-components
  17500. (face-property 'default
  17501. (cond ((eq attr :foreground) 'foreground)
  17502. ((eq attr :background) 'background))))
  17503. (color-values (face-attribute 'default attr nil))))))
  17504. (defun org-dvipng-color-format (color-name)
  17505. "Convert COLOR-NAME to a RGB color value for dvipng."
  17506. (apply 'format "rgb %s %s %s"
  17507. (mapcar 'org-normalize-color
  17508. (color-values color-name))))
  17509. (defun org-latex-color (attr)
  17510. "Return a RGB color for the LaTeX color package."
  17511. (apply 'format "%s,%s,%s"
  17512. (mapcar 'org-normalize-color
  17513. (if (featurep 'xemacs)
  17514. (color-rgb-components
  17515. (face-property 'default
  17516. (cond ((eq attr :foreground) 'foreground)
  17517. ((eq attr :background) 'background))))
  17518. (color-values (face-attribute 'default attr nil))))))
  17519. (defun org-latex-color-format (color-name)
  17520. "Convert COLOR-NAME to a RGB color value."
  17521. (apply 'format "%s,%s,%s"
  17522. (mapcar 'org-normalize-color
  17523. (color-values color-name))))
  17524. (defun org-normalize-color (value)
  17525. "Return string to be used as color value for an RGB component."
  17526. (format "%g" (/ value 65535.0)))
  17527. ;; Image display
  17528. (defvar org-inline-image-overlays nil)
  17529. (make-variable-buffer-local 'org-inline-image-overlays)
  17530. (defun org-toggle-inline-images (&optional include-linked)
  17531. "Toggle the display of inline images.
  17532. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17533. (interactive "P")
  17534. (if org-inline-image-overlays
  17535. (progn
  17536. (org-remove-inline-images)
  17537. (message "Inline image display turned off"))
  17538. (org-display-inline-images include-linked)
  17539. (if (and (org-called-interactively-p)
  17540. org-inline-image-overlays)
  17541. (message "%d images displayed inline"
  17542. (length org-inline-image-overlays))
  17543. (message "No images to display inline"))))
  17544. (defun org-redisplay-inline-images ()
  17545. "Refresh the display of inline images."
  17546. (interactive)
  17547. (if (not org-inline-image-overlays)
  17548. (org-toggle-inline-images)
  17549. (org-toggle-inline-images)
  17550. (org-toggle-inline-images)))
  17551. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17552. "Display inline images.
  17553. An inline image is a link which follows either of these
  17554. conventions:
  17555. 1. Its path is a file with an extension matching return value
  17556. from `image-file-name-regexp' and it has no contents.
  17557. 2. Its description consists in a single link of the previous
  17558. type.
  17559. When optional argument INCLUDE-LINKED is non-nil, also links with
  17560. a text description part will be inlined. This can be nice for
  17561. a quick look at those images, but it does not reflect what
  17562. exported files will look like.
  17563. When optional argument REFRESH is non-nil, refresh existing
  17564. images between BEG and END. This will create new image displays
  17565. only if necessary. BEG and END default to the buffer
  17566. boundaries."
  17567. (interactive "P")
  17568. (when (display-graphic-p)
  17569. (unless refresh
  17570. (org-remove-inline-images)
  17571. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17572. (org-with-wide-buffer
  17573. (goto-char (or beg (point-min)))
  17574. (let ((case-fold-search t)
  17575. (file-extension-re (org-image-file-name-regexp)))
  17576. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17577. (let ((link (save-match-data (org-element-context))))
  17578. ;; Check if we're at an inline image.
  17579. (when (and (equal (org-element-property :type link) "file")
  17580. (or include-linked
  17581. (not (org-element-property :contents-begin link)))
  17582. (let ((parent (org-element-property :parent link)))
  17583. (or (not (eq (org-element-type parent) 'link))
  17584. (not (cdr (org-element-contents parent)))))
  17585. (org-string-match-p file-extension-re
  17586. (org-element-property :path link)))
  17587. (let ((file (expand-file-name (org-element-property :path link))))
  17588. (when (file-exists-p file)
  17589. (let ((width
  17590. ;; Apply `org-image-actual-width' specifications.
  17591. (cond
  17592. ((not (image-type-available-p 'imagemagick)) nil)
  17593. ((eq org-image-actual-width t) nil)
  17594. ((listp org-image-actual-width)
  17595. (or
  17596. ;; First try to find a width among
  17597. ;; attributes associated to the paragraph
  17598. ;; containing link.
  17599. (let ((paragraph
  17600. (let ((e link))
  17601. (while (and (setq e (org-element-property
  17602. :parent e))
  17603. (not (eq (org-element-type e)
  17604. 'paragraph))))
  17605. e)))
  17606. (when paragraph
  17607. (save-excursion
  17608. (goto-char (org-element-property :begin paragraph))
  17609. (when
  17610. (re-search-forward
  17611. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  17612. (org-element-property
  17613. :post-affiliated paragraph)
  17614. t)
  17615. (string-to-number (match-string 1))))))
  17616. ;; Otherwise, fall-back to provided number.
  17617. (car org-image-actual-width)))
  17618. ((numberp org-image-actual-width)
  17619. org-image-actual-width)))
  17620. (old (get-char-property-and-overlay
  17621. (org-element-property :begin link)
  17622. 'org-image-overlay)))
  17623. (if (and (car-safe old) refresh)
  17624. (image-refresh (overlay-get (cdr old) 'display))
  17625. (let ((image (create-image file
  17626. (and width 'imagemagick)
  17627. nil
  17628. :width width)))
  17629. (when image
  17630. (let* ((link
  17631. ;; If inline image is the description
  17632. ;; of another link, be sure to
  17633. ;; consider the latter as the one to
  17634. ;; apply the overlay on.
  17635. (let ((parent
  17636. (org-element-property :parent link)))
  17637. (if (eq (org-element-type parent) 'link)
  17638. parent
  17639. link)))
  17640. (ov (make-overlay
  17641. (org-element-property :begin link)
  17642. (progn
  17643. (goto-char
  17644. (org-element-property :end link))
  17645. (skip-chars-backward " \t")
  17646. (point)))))
  17647. (overlay-put ov 'display image)
  17648. (overlay-put ov 'face 'default)
  17649. (overlay-put ov 'org-image-overlay t)
  17650. (overlay-put
  17651. ov 'modification-hooks
  17652. (list 'org-display-inline-remove-overlay))
  17653. (push ov org-inline-image-overlays)))))))))))))))
  17654. (define-obsolete-function-alias
  17655. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17656. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17657. "Remove inline-display overlay if a corresponding region is modified."
  17658. (let ((inhibit-modification-hooks t))
  17659. (when (and ov after)
  17660. (delete ov org-inline-image-overlays)
  17661. (delete-overlay ov))))
  17662. (defun org-remove-inline-images ()
  17663. "Remove inline display of images."
  17664. (interactive)
  17665. (mapc 'delete-overlay org-inline-image-overlays)
  17666. (setq org-inline-image-overlays nil))
  17667. ;;;; Key bindings
  17668. ;; Outline functions from `outline-mode-prefix-map'
  17669. ;; that can be remapped in Org:
  17670. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17671. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17672. (define-key org-mode-map [remap outline-forward-same-level]
  17673. 'org-forward-heading-same-level)
  17674. (define-key org-mode-map [remap outline-backward-same-level]
  17675. 'org-backward-heading-same-level)
  17676. (define-key org-mode-map [remap show-branches]
  17677. 'org-kill-note-or-show-branches)
  17678. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17679. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17680. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17681. ;; Outline functions from `outline-mode-prefix-map' that can not
  17682. ;; be remapped in Org:
  17683. ;;
  17684. ;; - the column "key binding" shows whether the Outline function is still
  17685. ;; available in Org mode on the same key that it has been bound to in
  17686. ;; Outline mode:
  17687. ;; - "overridden": key used for a different functionality in Org mode
  17688. ;; - else: key still bound to the same Outline function in Org mode
  17689. ;;
  17690. ;; | Outline function | key binding | Org replacement |
  17691. ;; |------------------------------------+-------------+-----------------------|
  17692. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17693. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17694. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17695. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17696. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17697. ;; | `show-entry' | overridden | no replacement |
  17698. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17699. ;; | `show-branches' | `C-c C-k' | still same function |
  17700. ;; | `show-subtree' | overridden | visibility cycling |
  17701. ;; | `show-all' | overridden | no replacement |
  17702. ;; | `hide-subtree' | overridden | visibility cycling |
  17703. ;; | `hide-body' | overridden | no replacement |
  17704. ;; | `hide-entry' | overridden | visibility cycling |
  17705. ;; | `hide-leaves' | overridden | no replacement |
  17706. ;; | `hide-sublevels' | overridden | no replacement |
  17707. ;; | `hide-other' | overridden | no replacement |
  17708. ;; Make `C-c C-x' a prefix key
  17709. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17710. ;; TAB key with modifiers
  17711. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17712. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17713. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17714. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17715. ;; The following line is necessary under Suse GNU/Linux
  17716. (unless (featurep 'xemacs)
  17717. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17718. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17719. (define-key org-mode-map [backtab] 'org-shifttab)
  17720. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17721. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17722. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17723. ;; Cursor keys with modifiers
  17724. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17725. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17726. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17727. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17728. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  17729. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  17730. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17731. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17732. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17733. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17734. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17735. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17736. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17737. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17738. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17739. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17740. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17741. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17742. ;; Babel keys
  17743. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17744. (mapc (lambda (pair)
  17745. (define-key org-babel-map (car pair) (cdr pair)))
  17746. org-babel-key-bindings)
  17747. ;;; Extra keys for tty access.
  17748. ;; We only set them when really needed because otherwise the
  17749. ;; menus don't show the simple keys
  17750. (when (or org-use-extra-keys
  17751. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17752. (not window-system))
  17753. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17754. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17755. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17756. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17757. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17758. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17759. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17760. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17761. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17762. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17763. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17764. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17765. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17766. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17767. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17768. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17769. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17770. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17771. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17772. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17773. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17774. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17775. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17776. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17777. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17778. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17779. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17780. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17781. ;; All the other keys
  17782. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17783. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17784. (if (boundp 'narrow-map)
  17785. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17786. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17787. (if (boundp 'narrow-map)
  17788. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17789. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17790. (if (boundp 'narrow-map)
  17791. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17792. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17793. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17794. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17795. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17796. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17797. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17798. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17799. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17800. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17801. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17802. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17803. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17804. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17805. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17806. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17807. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17808. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17809. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17810. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17811. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17812. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17813. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17814. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17815. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17816. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17817. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17818. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17819. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17820. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17821. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17822. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17823. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17824. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17825. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17826. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17827. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17828. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17829. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  17830. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17831. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17832. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17833. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17834. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17835. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17836. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17837. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17838. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17839. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17840. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17841. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17842. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17843. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17844. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17845. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17846. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17847. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17848. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17849. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17850. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17851. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17852. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17853. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17854. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  17855. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  17856. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  17857. (org-defkey org-mode-map "\C-m" 'org-return)
  17858. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17859. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17860. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17861. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17862. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17863. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17864. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17865. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17866. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17867. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17868. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17869. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17870. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17871. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17872. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  17873. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17874. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17875. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17876. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17877. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17878. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17879. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17880. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17881. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17882. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17883. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17884. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17885. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17886. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17887. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17888. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17889. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17890. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17891. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17892. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17893. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17894. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  17895. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17896. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17897. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17898. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17899. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17900. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17901. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17902. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17903. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17904. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17905. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17906. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17907. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17908. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17909. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17910. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17911. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17912. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17913. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17914. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17915. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17916. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17917. (when (featurep 'xemacs)
  17918. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17919. (defconst org-speed-commands-default
  17920. '(
  17921. ("Outline Navigation")
  17922. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17923. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17924. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17925. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17926. ("F" . org-next-block)
  17927. ("B" . org-previous-block)
  17928. ("u" . (org-speed-move-safe 'outline-up-heading))
  17929. ("j" . org-goto)
  17930. ("g" . (org-refile t))
  17931. ("Outline Visibility")
  17932. ("c" . org-cycle)
  17933. ("C" . org-shifttab)
  17934. (" " . org-display-outline-path)
  17935. ("s" . org-narrow-to-subtree)
  17936. ("=" . org-columns)
  17937. ("Outline Structure Editing")
  17938. ("U" . org-metaup)
  17939. ("D" . org-metadown)
  17940. ("r" . org-metaright)
  17941. ("l" . org-metaleft)
  17942. ("R" . org-shiftmetaright)
  17943. ("L" . org-shiftmetaleft)
  17944. ("i" . (progn (forward-char 1) (call-interactively
  17945. 'org-insert-heading-respect-content)))
  17946. ("^" . org-sort)
  17947. ("w" . org-refile)
  17948. ("a" . org-archive-subtree-default-with-confirmation)
  17949. ("@" . org-mark-subtree)
  17950. ("#" . org-toggle-comment)
  17951. ("Clock Commands")
  17952. ("I" . org-clock-in)
  17953. ("O" . org-clock-out)
  17954. ("Meta Data Editing")
  17955. ("t" . org-todo)
  17956. ("," . (org-priority))
  17957. ("0" . (org-priority ?\ ))
  17958. ("1" . (org-priority ?A))
  17959. ("2" . (org-priority ?B))
  17960. ("3" . (org-priority ?C))
  17961. (":" . org-set-tags-command)
  17962. ("e" . org-set-effort)
  17963. ("E" . org-inc-effort)
  17964. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17965. (org-entry-put (point) "APPT_WARNTIME" m)))
  17966. ("Agenda Views etc")
  17967. ("v" . org-agenda)
  17968. ("/" . org-sparse-tree)
  17969. ("Misc")
  17970. ("o" . org-open-at-point)
  17971. ("?" . org-speed-command-help)
  17972. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17973. (">" . (org-agenda-remove-restriction-lock))
  17974. )
  17975. "The default speed commands.")
  17976. (defun org-print-speed-command (e)
  17977. (if (> (length (car e)) 1)
  17978. (progn
  17979. (princ "\n")
  17980. (princ (car e))
  17981. (princ "\n")
  17982. (princ (make-string (length (car e)) ?-))
  17983. (princ "\n"))
  17984. (princ (car e))
  17985. (princ " ")
  17986. (if (symbolp (cdr e))
  17987. (princ (symbol-name (cdr e)))
  17988. (prin1 (cdr e)))
  17989. (princ "\n")))
  17990. (defun org-speed-command-help ()
  17991. "Show the available speed commands."
  17992. (interactive)
  17993. (if (not org-use-speed-commands)
  17994. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  17995. (with-output-to-temp-buffer "*Help*"
  17996. (princ "User-defined Speed commands\n===========================\n")
  17997. (mapc 'org-print-speed-command org-speed-commands-user)
  17998. (princ "\n")
  17999. (princ "Built-in Speed commands\n=======================\n")
  18000. (mapc 'org-print-speed-command org-speed-commands-default))
  18001. (with-current-buffer "*Help*"
  18002. (setq truncate-lines t))))
  18003. (defun org-speed-move-safe (cmd)
  18004. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18005. If not, return to the original position and throw an error."
  18006. (interactive)
  18007. (let ((pos (point)))
  18008. (call-interactively cmd)
  18009. (unless (and (bolp) (org-at-heading-p))
  18010. (goto-char pos)
  18011. (error "Boundary reached while executing %s" cmd))))
  18012. (defvar org-self-insert-command-undo-counter 0)
  18013. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18014. (defvar org-speed-command nil)
  18015. (define-obsolete-function-alias
  18016. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18017. (defun org-speed-command-activate (keys)
  18018. "Hook for activating single-letter speed commands.
  18019. `org-speed-commands-default' specifies a minimal command set.
  18020. Use `org-speed-commands-user' for further customization."
  18021. (when (or (and (bolp) (looking-at org-outline-regexp))
  18022. (and (functionp org-use-speed-commands)
  18023. (funcall org-use-speed-commands)))
  18024. (cdr (assoc keys (append org-speed-commands-user
  18025. org-speed-commands-default)))))
  18026. (define-obsolete-function-alias
  18027. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18028. (defun org-babel-speed-command-activate (keys)
  18029. "Hook for activating single-letter code block commands."
  18030. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18031. (cdr (assoc keys org-babel-key-bindings))))
  18032. (defcustom org-speed-command-hook
  18033. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18034. "Hook for activating speed commands at strategic locations.
  18035. Hook functions are called in sequence until a valid handler is
  18036. found.
  18037. Each hook takes a single argument, a user-pressed command key
  18038. which is also a `self-insert-command' from the global map.
  18039. Within the hook, examine the cursor position and the command key
  18040. and return nil or a valid handler as appropriate. Handler could
  18041. be one of an interactive command, a function, or a form.
  18042. Set `org-use-speed-commands' to non-nil value to enable this
  18043. hook. The default setting is `org-speed-command-activate'."
  18044. :group 'org-structure
  18045. :version "24.1"
  18046. :type 'hook)
  18047. (defun org-self-insert-command (N)
  18048. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18049. If the cursor is in a table looking at whitespace, the whitespace is
  18050. overwritten, and the table is not marked as requiring realignment."
  18051. (interactive "p")
  18052. (org-check-before-invisible-edit 'insert)
  18053. (cond
  18054. ((and org-use-speed-commands
  18055. (setq org-speed-command
  18056. (run-hook-with-args-until-success
  18057. 'org-speed-command-hook (this-command-keys))))
  18058. (cond
  18059. ((commandp org-speed-command)
  18060. (setq this-command org-speed-command)
  18061. (call-interactively org-speed-command))
  18062. ((functionp org-speed-command)
  18063. (funcall org-speed-command))
  18064. ((and org-speed-command (listp org-speed-command))
  18065. (eval org-speed-command))
  18066. (t (let (org-use-speed-commands)
  18067. (call-interactively 'org-self-insert-command)))))
  18068. ((and
  18069. (org-table-p)
  18070. (progn
  18071. ;; check if we blank the field, and if that triggers align
  18072. (and (featurep 'org-table) org-table-auto-blank-field
  18073. (member last-command
  18074. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas-expand))
  18075. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  18076. ;; got extra space, this field does not determine column width
  18077. (let (org-table-may-need-update) (org-table-blank-field))
  18078. ;; no extra space, this field may determine column width
  18079. (org-table-blank-field)))
  18080. t)
  18081. (eq N 1)
  18082. (looking-at "[^|\n]* |"))
  18083. (let (org-table-may-need-update)
  18084. (goto-char (1- (match-end 0)))
  18085. (backward-delete-char 1)
  18086. (goto-char (match-beginning 0))
  18087. (self-insert-command N)))
  18088. (t
  18089. (setq org-table-may-need-update t)
  18090. (self-insert-command N)
  18091. (org-fix-tags-on-the-fly)
  18092. (if org-self-insert-cluster-for-undo
  18093. (if (not (eq last-command 'org-self-insert-command))
  18094. (setq org-self-insert-command-undo-counter 1)
  18095. (if (>= org-self-insert-command-undo-counter 20)
  18096. (setq org-self-insert-command-undo-counter 1)
  18097. (and (> org-self-insert-command-undo-counter 0)
  18098. buffer-undo-list (listp buffer-undo-list)
  18099. (not (cadr buffer-undo-list)) ; remove nil entry
  18100. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18101. (setq org-self-insert-command-undo-counter
  18102. (1+ org-self-insert-command-undo-counter))))))))
  18103. (defun org-check-before-invisible-edit (kind)
  18104. "Check is editing if kind KIND would be dangerous with invisible text around.
  18105. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18106. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18107. (if (and org-catch-invisible-edits
  18108. (or (not (boundp 'visible-mode)) (not visible-mode))
  18109. (or (get-char-property (point) 'invisible)
  18110. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18111. ;; OK, we need to take a closer look
  18112. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18113. (invisible-before-point (if (bobp) nil (get-char-property
  18114. (1- (point)) 'invisible)))
  18115. (border-and-ok-direction
  18116. (or
  18117. ;; Check if we are acting predictably before invisible text
  18118. (and invisible-at-point (not invisible-before-point)
  18119. (memq kind '(insert delete-backward)))
  18120. ;; Check if we are acting predictably after invisible text
  18121. ;; This works not well, and I have turned it off. It seems
  18122. ;; better to always show and stop after invisible text.
  18123. ;; (and (not invisible-at-point) invisible-before-point
  18124. ;; (memq kind '(insert delete)))
  18125. )))
  18126. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18127. (memq invisible-before-point '(outline org-hide-block t)))
  18128. (if (eq org-catch-invisible-edits 'error)
  18129. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18130. (if (and org-custom-properties-overlays
  18131. (y-or-n-p "Display invisible properties in this buffer? "))
  18132. (org-toggle-custom-properties-visibility)
  18133. ;; Make the area visible
  18134. (save-excursion
  18135. (if invisible-before-point
  18136. (goto-char (previous-single-char-property-change
  18137. (point) 'invisible)))
  18138. (show-subtree))
  18139. (cond
  18140. ((eq org-catch-invisible-edits 'show)
  18141. ;; That's it, we do the edit after showing
  18142. (message
  18143. "Unfolding invisible region around point before editing")
  18144. (sit-for 1))
  18145. ((and (eq org-catch-invisible-edits 'smart)
  18146. border-and-ok-direction)
  18147. (message "Unfolding invisible region around point before editing"))
  18148. (t
  18149. ;; Don't do the edit, make the user repeat it in full visibility
  18150. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18151. (defun org-fix-tags-on-the-fly ()
  18152. (when (and (equal (char-after (point-at-bol)) ?*)
  18153. (org-at-heading-p))
  18154. (org-align-tags-here org-tags-column)))
  18155. (defun org-delete-backward-char (N)
  18156. "Like `delete-backward-char', insert whitespace at field end in tables.
  18157. When deleting backwards, in tables this function will insert whitespace in
  18158. front of the next \"|\" separator, to keep the table aligned. The table will
  18159. still be marked for re-alignment if the field did fill the entire column,
  18160. because, in this case the deletion might narrow the column."
  18161. (interactive "p")
  18162. (save-match-data
  18163. (org-check-before-invisible-edit 'delete-backward)
  18164. (if (and (org-table-p)
  18165. (eq N 1)
  18166. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18167. (looking-at ".*?|"))
  18168. (let ((pos (point))
  18169. (noalign (looking-at "[^|\n\r]* |"))
  18170. (c org-table-may-need-update))
  18171. (backward-delete-char N)
  18172. (if (not overwrite-mode)
  18173. (progn
  18174. (skip-chars-forward "^|")
  18175. (insert " ")
  18176. (goto-char (1- pos))))
  18177. ;; noalign: if there were two spaces at the end, this field
  18178. ;; does not determine the width of the column.
  18179. (if noalign (setq org-table-may-need-update c)))
  18180. (backward-delete-char N)
  18181. (org-fix-tags-on-the-fly))))
  18182. (defun org-delete-char (N)
  18183. "Like `delete-char', but insert whitespace at field end in tables.
  18184. When deleting characters, in tables this function will insert whitespace in
  18185. front of the next \"|\" separator, to keep the table aligned. The table will
  18186. still be marked for re-alignment if the field did fill the entire column,
  18187. because, in this case the deletion might narrow the column."
  18188. (interactive "p")
  18189. (save-match-data
  18190. (org-check-before-invisible-edit 'delete)
  18191. (if (and (org-table-p)
  18192. (not (bolp))
  18193. (not (= (char-after) ?|))
  18194. (eq N 1))
  18195. (if (looking-at ".*?|")
  18196. (let ((pos (point))
  18197. (noalign (looking-at "[^|\n\r]* |"))
  18198. (c org-table-may-need-update))
  18199. (replace-match
  18200. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18201. (goto-char pos)
  18202. ;; noalign: if there were two spaces at the end, this field
  18203. ;; does not determine the width of the column.
  18204. (if noalign (setq org-table-may-need-update c)))
  18205. (delete-char N))
  18206. (delete-char N)
  18207. (org-fix-tags-on-the-fly))))
  18208. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18209. (put 'org-self-insert-command 'delete-selection
  18210. (lambda ()
  18211. (not (run-hook-with-args-until-success
  18212. 'self-insert-uses-region-functions))))
  18213. (put 'orgtbl-self-insert-command 'delete-selection
  18214. (lambda ()
  18215. (not (run-hook-with-args-until-success
  18216. 'self-insert-uses-region-functions))))
  18217. (put 'org-delete-char 'delete-selection 'supersede)
  18218. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18219. (put 'org-yank 'delete-selection 'yank)
  18220. ;; Make `flyspell-mode' delay after some commands
  18221. (put 'org-self-insert-command 'flyspell-delayed t)
  18222. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18223. (put 'org-delete-char 'flyspell-delayed t)
  18224. (put 'org-delete-backward-char 'flyspell-delayed t)
  18225. ;; Make pabbrev-mode expand after org-mode commands
  18226. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18227. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18228. (defun org-remap (map &rest commands)
  18229. "In MAP, remap the functions given in COMMANDS.
  18230. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18231. (let (new old)
  18232. (while commands
  18233. (setq old (pop commands) new (pop commands))
  18234. (if (fboundp 'command-remapping)
  18235. (org-defkey map (vector 'remap old) new)
  18236. (substitute-key-definition old new map global-map)))))
  18237. (defun org-transpose-words ()
  18238. "Transpose words for Org.
  18239. This uses the `org-mode-transpose-word-syntax-table' syntax
  18240. table, which interprets characters in `org-emphasis-alist' as
  18241. word constituents."
  18242. (interactive)
  18243. (with-syntax-table org-mode-transpose-word-syntax-table
  18244. (call-interactively 'transpose-words)))
  18245. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18246. (when (eq org-enable-table-editor 'optimized)
  18247. ;; If the user wants maximum table support, we need to hijack
  18248. ;; some standard editing functions
  18249. (org-remap org-mode-map
  18250. 'self-insert-command 'org-self-insert-command
  18251. 'delete-char 'org-delete-char
  18252. 'delete-backward-char 'org-delete-backward-char)
  18253. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18254. (defvar org-ctrl-c-ctrl-c-hook nil
  18255. "Hook for functions attaching themselves to `C-c C-c'.
  18256. This can be used to add additional functionality to the C-c C-c
  18257. key which executes context-dependent commands. This hook is run
  18258. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18259. run after the last test.
  18260. Each function will be called with no arguments. The function
  18261. must check if the context is appropriate for it to act. If yes,
  18262. it should do its thing and then return a non-nil value. If the
  18263. context is wrong, just do nothing and return nil.")
  18264. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18265. "Hook for functions attaching themselves to `C-c C-c'.
  18266. This can be used to add additional functionality to the C-c C-c
  18267. key which executes context-dependent commands. This hook is run
  18268. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18269. before the first test.
  18270. Each function will be called with no arguments. The function
  18271. must check if the context is appropriate for it to act. If yes,
  18272. it should do its thing and then return a non-nil value. If the
  18273. context is wrong, just do nothing and return nil.")
  18274. (defvar org-tab-first-hook nil
  18275. "Hook for functions to attach themselves to TAB.
  18276. See `org-ctrl-c-ctrl-c-hook' for more information.
  18277. This hook runs as the first action when TAB is pressed, even before
  18278. `org-cycle' messes around with the `outline-regexp' to cater for
  18279. inline tasks and plain list item folding.
  18280. If any function in this hook returns t, any other actions that
  18281. would have been caused by TAB (such as table field motion or visibility
  18282. cycling) will not occur.")
  18283. (defvar org-tab-after-check-for-table-hook nil
  18284. "Hook for functions to attach themselves to TAB.
  18285. See `org-ctrl-c-ctrl-c-hook' for more information.
  18286. This hook runs after it has been established that the cursor is not in a
  18287. table, but before checking if the cursor is in a headline or if global cycling
  18288. should be done.
  18289. If any function in this hook returns t, not other actions like visibility
  18290. cycling will be done.")
  18291. (defvar org-tab-after-check-for-cycling-hook nil
  18292. "Hook for functions to attach themselves to TAB.
  18293. See `org-ctrl-c-ctrl-c-hook' for more information.
  18294. This hook runs after it has been established that not table field motion and
  18295. not visibility should be done because of current context. This is probably
  18296. the place where a package like yasnippets can hook in.")
  18297. (defvar org-tab-before-tab-emulation-hook nil
  18298. "Hook for functions to attach themselves to TAB.
  18299. See `org-ctrl-c-ctrl-c-hook' for more information.
  18300. This hook runs after every other options for TAB have been exhausted, but
  18301. before indentation and \t insertion takes place.")
  18302. (defvar org-metaleft-hook nil
  18303. "Hook for functions attaching themselves to `M-left'.
  18304. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18305. (defvar org-metaright-hook nil
  18306. "Hook for functions attaching themselves to `M-right'.
  18307. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18308. (defvar org-metaup-hook nil
  18309. "Hook for functions attaching themselves to `M-up'.
  18310. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18311. (defvar org-metadown-hook nil
  18312. "Hook for functions attaching themselves to `M-down'.
  18313. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18314. (defvar org-shiftmetaleft-hook nil
  18315. "Hook for functions attaching themselves to `M-S-left'.
  18316. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18317. (defvar org-shiftmetaright-hook nil
  18318. "Hook for functions attaching themselves to `M-S-right'.
  18319. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18320. (defvar org-shiftmetaup-hook nil
  18321. "Hook for functions attaching themselves to `M-S-up'.
  18322. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18323. (defvar org-shiftmetadown-hook nil
  18324. "Hook for functions attaching themselves to `M-S-down'.
  18325. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18326. (defvar org-metareturn-hook nil
  18327. "Hook for functions attaching themselves to `M-RET'.
  18328. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18329. (defvar org-shiftup-hook nil
  18330. "Hook for functions attaching themselves to `S-up'.
  18331. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18332. (defvar org-shiftup-final-hook nil
  18333. "Hook for functions attaching themselves to `S-up'.
  18334. This one runs after all other options except shift-select have been excluded.
  18335. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18336. (defvar org-shiftdown-hook nil
  18337. "Hook for functions attaching themselves to `S-down'.
  18338. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18339. (defvar org-shiftdown-final-hook nil
  18340. "Hook for functions attaching themselves to `S-down'.
  18341. This one runs after all other options except shift-select have been excluded.
  18342. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18343. (defvar org-shiftleft-hook nil
  18344. "Hook for functions attaching themselves to `S-left'.
  18345. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18346. (defvar org-shiftleft-final-hook nil
  18347. "Hook for functions attaching themselves to `S-left'.
  18348. This one runs after all other options except shift-select have been excluded.
  18349. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18350. (defvar org-shiftright-hook nil
  18351. "Hook for functions attaching themselves to `S-right'.
  18352. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18353. (defvar org-shiftright-final-hook nil
  18354. "Hook for functions attaching themselves to `S-right'.
  18355. This one runs after all other options except shift-select have been excluded.
  18356. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18357. (defun org-modifier-cursor-error ()
  18358. "Throw an error, a modified cursor command was applied in wrong context."
  18359. (user-error "This command is active in special context like tables, headlines or items"))
  18360. (defun org-shiftselect-error ()
  18361. "Throw an error because Shift-Cursor command was applied in wrong context."
  18362. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18363. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18364. (user-error "This command works only in special context like headlines or timestamps")))
  18365. (defun org-call-for-shift-select (cmd)
  18366. (let ((this-command-keys-shift-translated t))
  18367. (call-interactively cmd)))
  18368. (defun org-shifttab (&optional arg)
  18369. "Global visibility cycling or move to previous table field.
  18370. Call `org-table-previous-field' within a table.
  18371. When ARG is nil, cycle globally through visibility states.
  18372. When ARG is a numeric prefix, show contents of this level."
  18373. (interactive "P")
  18374. (cond
  18375. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18376. ((integerp arg)
  18377. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18378. (message "Content view to level: %d" arg)
  18379. (org-content (prefix-numeric-value arg2))
  18380. (org-cycle-show-empty-lines t)
  18381. (setq org-cycle-global-status 'overview)))
  18382. (t (call-interactively 'org-global-cycle))))
  18383. (defun org-shiftmetaleft ()
  18384. "Promote subtree or delete table column.
  18385. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18386. `org-table-delete-column', depending on context. See the
  18387. individual commands for more information."
  18388. (interactive)
  18389. (cond
  18390. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18391. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18392. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18393. ((if (not (org-region-active-p)) (org-at-item-p)
  18394. (save-excursion (goto-char (region-beginning))
  18395. (org-at-item-p)))
  18396. (call-interactively 'org-outdent-item-tree))
  18397. (t (org-modifier-cursor-error))))
  18398. (defun org-shiftmetaright ()
  18399. "Demote subtree or insert table column.
  18400. Calls `org-demote-subtree', `org-indent-item-tree', or
  18401. `org-table-insert-column', depending on context. See the
  18402. individual commands for more information."
  18403. (interactive)
  18404. (cond
  18405. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18406. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18407. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18408. ((if (not (org-region-active-p)) (org-at-item-p)
  18409. (save-excursion (goto-char (region-beginning))
  18410. (org-at-item-p)))
  18411. (call-interactively 'org-indent-item-tree))
  18412. (t (org-modifier-cursor-error))))
  18413. (defun org-shiftmetaup (&optional arg)
  18414. "Drag the line at point up.
  18415. In a table, kill the current row.
  18416. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18417. but also adjust the previous clocked item in the clock history.
  18418. Everywhere else, drag the line at point up."
  18419. (interactive "P")
  18420. (cond
  18421. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18422. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18423. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18424. (call-interactively 'org-timestamp-up)))
  18425. (t (call-interactively 'org-drag-line-backward))))
  18426. (defun org-shiftmetadown (&optional arg)
  18427. "Drag the line at point down.
  18428. In a table, insert an empty row at the current line.
  18429. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18430. but also adjust the previous clocked item in the clock history.
  18431. Everywhere else, drag the line at point down."
  18432. (interactive "P")
  18433. (cond
  18434. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18435. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18436. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18437. (call-interactively 'org-timestamp-down)))
  18438. (t (call-interactively 'org-drag-line-forward))))
  18439. (defsubst org-hidden-tree-error ()
  18440. (user-error
  18441. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18442. (defun org-metaleft (&optional arg)
  18443. "Promote heading or move table column to left.
  18444. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18445. With no specific context, calls the Emacs default `backward-word'.
  18446. See the individual commands for more information."
  18447. (interactive "P")
  18448. (cond
  18449. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18450. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18451. ((org-with-limited-levels
  18452. (or (org-at-heading-p)
  18453. (and (org-region-active-p)
  18454. (save-excursion
  18455. (goto-char (region-beginning))
  18456. (org-at-heading-p)))))
  18457. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18458. (call-interactively 'org-do-promote))
  18459. ;; At an inline task.
  18460. ((org-at-heading-p)
  18461. (call-interactively 'org-inlinetask-promote))
  18462. ((or (org-at-item-p)
  18463. (and (org-region-active-p)
  18464. (save-excursion
  18465. (goto-char (region-beginning))
  18466. (org-at-item-p))))
  18467. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18468. (call-interactively 'org-outdent-item))
  18469. (t (call-interactively 'backward-word))))
  18470. (defun org-metaright (&optional arg)
  18471. "Demote a subtree, a list item or move table column to right.
  18472. In front of a drawer or a block keyword, indent it correctly.
  18473. With no specific context, calls the Emacs default `forward-word'.
  18474. See the individual commands for more information."
  18475. (interactive "P")
  18476. (cond
  18477. ((run-hook-with-args-until-success 'org-metaright-hook))
  18478. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18479. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18480. ((org-at-block-p) (call-interactively 'org-indent-block))
  18481. ((org-with-limited-levels
  18482. (or (org-at-heading-p)
  18483. (and (org-region-active-p)
  18484. (save-excursion
  18485. (goto-char (region-beginning))
  18486. (org-at-heading-p)))))
  18487. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18488. (call-interactively 'org-do-demote))
  18489. ;; At an inline task.
  18490. ((org-at-heading-p)
  18491. (call-interactively 'org-inlinetask-demote))
  18492. ((or (org-at-item-p)
  18493. (and (org-region-active-p)
  18494. (save-excursion
  18495. (goto-char (region-beginning))
  18496. (org-at-item-p))))
  18497. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18498. (call-interactively 'org-indent-item))
  18499. (t (call-interactively 'forward-word))))
  18500. (defun org-check-for-hidden (what)
  18501. "Check if there are hidden headlines/items in the current visual line.
  18502. WHAT can be either `headlines' or `items'. If the current line is
  18503. an outline or item heading and it has a folded subtree below it,
  18504. this function returns t, nil otherwise."
  18505. (let ((re (cond
  18506. ((eq what 'headlines) org-outline-regexp-bol)
  18507. ((eq what 'items) (org-item-beginning-re))
  18508. (t (error "This should not happen"))))
  18509. beg end)
  18510. (save-excursion
  18511. (catch 'exit
  18512. (unless (org-region-active-p)
  18513. (setq beg (point-at-bol))
  18514. (beginning-of-line 2)
  18515. (while (and (not (eobp)) ;; this is like `next-line'
  18516. (get-char-property (1- (point)) 'invisible))
  18517. (beginning-of-line 2))
  18518. (setq end (point))
  18519. (goto-char beg)
  18520. (goto-char (point-at-eol))
  18521. (setq end (max end (point)))
  18522. (while (re-search-forward re end t)
  18523. (if (get-char-property (match-beginning 0) 'invisible)
  18524. (throw 'exit t))))
  18525. nil))))
  18526. (defun org-metaup (&optional arg)
  18527. "Move subtree up or move table row up.
  18528. Calls `org-move-subtree-up' or `org-table-move-row' or
  18529. `org-move-item-up', depending on context. See the individual commands
  18530. for more information."
  18531. (interactive "P")
  18532. (cond
  18533. ((run-hook-with-args-until-success 'org-metaup-hook))
  18534. ((org-region-active-p)
  18535. (let* ((a (min (region-beginning) (region-end)))
  18536. (b (1- (max (region-beginning) (region-end))))
  18537. (c (save-excursion (goto-char a)
  18538. (move-beginning-of-line 0)))
  18539. (d (save-excursion (goto-char a)
  18540. (move-end-of-line 0) (point))))
  18541. (transpose-regions a b c d)
  18542. (goto-char c)))
  18543. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18544. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18545. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18546. (t (org-drag-element-backward))))
  18547. (defun org-metadown (&optional arg)
  18548. "Move subtree down or move table row down.
  18549. Calls `org-move-subtree-down' or `org-table-move-row' or
  18550. `org-move-item-down', depending on context. See the individual
  18551. commands for more information."
  18552. (interactive "P")
  18553. (cond
  18554. ((run-hook-with-args-until-success 'org-metadown-hook))
  18555. ((org-region-active-p)
  18556. (let* ((a (min (region-beginning) (region-end)))
  18557. (b (max (region-beginning) (region-end)))
  18558. (c (save-excursion (goto-char b)
  18559. (move-beginning-of-line 1)))
  18560. (d (save-excursion (goto-char b)
  18561. (move-end-of-line 1) (1+ (point)))))
  18562. (transpose-regions a b c d)
  18563. (goto-char d)))
  18564. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18565. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18566. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18567. (t (org-drag-element-forward))))
  18568. (defun org-shiftup (&optional arg)
  18569. "Increase item in timestamp or increase priority of current headline.
  18570. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18571. depending on context. See the individual commands for more information."
  18572. (interactive "P")
  18573. (cond
  18574. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18575. ((and org-support-shift-select (org-region-active-p))
  18576. (org-call-for-shift-select 'previous-line))
  18577. ((org-at-timestamp-p t)
  18578. (call-interactively (if org-edit-timestamp-down-means-later
  18579. 'org-timestamp-down 'org-timestamp-up)))
  18580. ((and (not (eq org-support-shift-select 'always))
  18581. org-enable-priority-commands
  18582. (org-at-heading-p))
  18583. (call-interactively 'org-priority-up))
  18584. ((and (not org-support-shift-select) (org-at-item-p))
  18585. (call-interactively 'org-previous-item))
  18586. ((org-clocktable-try-shift 'up arg))
  18587. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18588. (org-support-shift-select
  18589. (org-call-for-shift-select 'previous-line))
  18590. (t (org-shiftselect-error))))
  18591. (defun org-shiftdown (&optional arg)
  18592. "Decrease item in timestamp or decrease priority of current headline.
  18593. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18594. depending on context. See the individual commands for more information."
  18595. (interactive "P")
  18596. (cond
  18597. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18598. ((and org-support-shift-select (org-region-active-p))
  18599. (org-call-for-shift-select 'next-line))
  18600. ((org-at-timestamp-p t)
  18601. (call-interactively (if org-edit-timestamp-down-means-later
  18602. 'org-timestamp-up 'org-timestamp-down)))
  18603. ((and (not (eq org-support-shift-select 'always))
  18604. org-enable-priority-commands
  18605. (org-at-heading-p))
  18606. (call-interactively 'org-priority-down))
  18607. ((and (not org-support-shift-select) (org-at-item-p))
  18608. (call-interactively 'org-next-item))
  18609. ((org-clocktable-try-shift 'down arg))
  18610. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18611. (org-support-shift-select
  18612. (org-call-for-shift-select 'next-line))
  18613. (t (org-shiftselect-error))))
  18614. (defun org-shiftright (&optional arg)
  18615. "Cycle the thing at point or in the current line, depending on context.
  18616. Depending on context, this does one of the following:
  18617. - switch a timestamp at point one day into the future
  18618. - on a headline, switch to the next TODO keyword.
  18619. - on an item, switch entire list to the next bullet type
  18620. - on a property line, switch to the next allowed value
  18621. - on a clocktable definition line, move time block into the future"
  18622. (interactive "P")
  18623. (cond
  18624. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18625. ((and org-support-shift-select (org-region-active-p))
  18626. (org-call-for-shift-select 'forward-char))
  18627. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18628. ((and (not (eq org-support-shift-select 'always))
  18629. (org-at-heading-p))
  18630. (let ((org-inhibit-logging
  18631. (not org-treat-S-cursor-todo-selection-as-state-change))
  18632. (org-inhibit-blocking
  18633. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18634. (org-call-with-arg 'org-todo 'right)))
  18635. ((or (and org-support-shift-select
  18636. (not (eq org-support-shift-select 'always))
  18637. (org-at-item-bullet-p))
  18638. (and (not org-support-shift-select) (org-at-item-p)))
  18639. (org-call-with-arg 'org-cycle-list-bullet nil))
  18640. ((and (not (eq org-support-shift-select 'always))
  18641. (org-at-property-p))
  18642. (call-interactively 'org-property-next-allowed-value))
  18643. ((org-clocktable-try-shift 'right arg))
  18644. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18645. (org-support-shift-select
  18646. (org-call-for-shift-select 'forward-char))
  18647. (t (org-shiftselect-error))))
  18648. (defun org-shiftleft (&optional arg)
  18649. "Cycle the thing at point or in the current line, depending on context.
  18650. Depending on context, this does one of the following:
  18651. - switch a timestamp at point one day into the past
  18652. - on a headline, switch to the previous TODO keyword.
  18653. - on an item, switch entire list to the previous bullet type
  18654. - on a property line, switch to the previous allowed value
  18655. - on a clocktable definition line, move time block into the past"
  18656. (interactive "P")
  18657. (cond
  18658. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18659. ((and org-support-shift-select (org-region-active-p))
  18660. (org-call-for-shift-select 'backward-char))
  18661. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18662. ((and (not (eq org-support-shift-select 'always))
  18663. (org-at-heading-p))
  18664. (let ((org-inhibit-logging
  18665. (not org-treat-S-cursor-todo-selection-as-state-change))
  18666. (org-inhibit-blocking
  18667. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18668. (org-call-with-arg 'org-todo 'left)))
  18669. ((or (and org-support-shift-select
  18670. (not (eq org-support-shift-select 'always))
  18671. (org-at-item-bullet-p))
  18672. (and (not org-support-shift-select) (org-at-item-p)))
  18673. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18674. ((and (not (eq org-support-shift-select 'always))
  18675. (org-at-property-p))
  18676. (call-interactively 'org-property-previous-allowed-value))
  18677. ((org-clocktable-try-shift 'left arg))
  18678. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18679. (org-support-shift-select
  18680. (org-call-for-shift-select 'backward-char))
  18681. (t (org-shiftselect-error))))
  18682. (defun org-shiftcontrolright ()
  18683. "Switch to next TODO set."
  18684. (interactive)
  18685. (cond
  18686. ((and org-support-shift-select (org-region-active-p))
  18687. (org-call-for-shift-select 'forward-word))
  18688. ((and (not (eq org-support-shift-select 'always))
  18689. (org-at-heading-p))
  18690. (org-call-with-arg 'org-todo 'nextset))
  18691. (org-support-shift-select
  18692. (org-call-for-shift-select 'forward-word))
  18693. (t (org-shiftselect-error))))
  18694. (defun org-shiftcontrolleft ()
  18695. "Switch to previous TODO set."
  18696. (interactive)
  18697. (cond
  18698. ((and org-support-shift-select (org-region-active-p))
  18699. (org-call-for-shift-select 'backward-word))
  18700. ((and (not (eq org-support-shift-select 'always))
  18701. (org-at-heading-p))
  18702. (org-call-with-arg 'org-todo 'previousset))
  18703. (org-support-shift-select
  18704. (org-call-for-shift-select 'backward-word))
  18705. (t (org-shiftselect-error))))
  18706. (defun org-shiftcontrolup (&optional n)
  18707. "Change timestamps synchronously up in CLOCK log lines.
  18708. Optional argument N tells to change by that many units."
  18709. (interactive "P")
  18710. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18711. (let (org-support-shift-select)
  18712. (org-clock-timestamps-up n))
  18713. (user-error "Not at a clock log")))
  18714. (defun org-shiftcontroldown (&optional n)
  18715. "Change timestamps synchronously down in CLOCK log lines.
  18716. Optional argument N tells to change by that many units."
  18717. (interactive "P")
  18718. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18719. (let (org-support-shift-select)
  18720. (org-clock-timestamps-down n))
  18721. (user-error "Not at a clock log")))
  18722. (defun org-increase-number-at-point (&optional inc)
  18723. "Increment the number at point.
  18724. With an optional prefix numeric argument INC, increment using
  18725. this numeric value."
  18726. (interactive "p")
  18727. (if (not (number-at-point))
  18728. (user-error "Not on a number")
  18729. (unless inc (setq inc 1))
  18730. (let ((pos (point))
  18731. (beg (skip-chars-backward "-+^/*0-9eE."))
  18732. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  18733. (setq nap (buffer-substring-no-properties
  18734. (+ pos beg) (+ pos beg end)))
  18735. (delete-region (+ pos beg) (+ pos beg end))
  18736. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  18737. (when (org-at-table-p)
  18738. (org-table-align)
  18739. (org-table-end-of-field 1))))
  18740. (defun org-decrease-number-at-point (&optional inc)
  18741. "Decrement the number at point.
  18742. With an optional prefix numeric argument INC, decrement using
  18743. this numeric value."
  18744. (interactive "p")
  18745. (org-increase-number-at-point (- inc)))
  18746. (defun org-ctrl-c-ret ()
  18747. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18748. (interactive)
  18749. (cond
  18750. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18751. (t (call-interactively 'org-insert-heading))))
  18752. (defun org-find-visible ()
  18753. (let ((s (point)))
  18754. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18755. (get-char-property s 'invisible)))
  18756. s))
  18757. (defun org-find-invisible ()
  18758. (let ((s (point)))
  18759. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18760. (not (get-char-property s 'invisible))))
  18761. s))
  18762. (defun org-copy-visible (beg end)
  18763. "Copy the visible parts of the region."
  18764. (interactive "r")
  18765. (let (snippets s)
  18766. (save-excursion
  18767. (save-restriction
  18768. (narrow-to-region beg end)
  18769. (setq s (goto-char (point-min)))
  18770. (while (not (= (point) (point-max)))
  18771. (goto-char (org-find-invisible))
  18772. (push (buffer-substring s (point)) snippets)
  18773. (setq s (goto-char (org-find-visible))))))
  18774. (kill-new (apply 'concat (nreverse snippets)))))
  18775. (defun org-copy-special ()
  18776. "Copy region in table or copy current subtree.
  18777. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18778. See the individual commands for more information."
  18779. (interactive)
  18780. (call-interactively
  18781. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18782. (defun org-cut-special ()
  18783. "Cut region in table or cut current subtree.
  18784. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18785. See the individual commands for more information."
  18786. (interactive)
  18787. (call-interactively
  18788. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18789. (defun org-paste-special (arg)
  18790. "Paste rectangular region into table, or past subtree relative to level.
  18791. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18792. See the individual commands for more information."
  18793. (interactive "P")
  18794. (if (org-at-table-p)
  18795. (org-table-paste-rectangle)
  18796. (org-paste-subtree arg)))
  18797. (defsubst org-in-fixed-width-region-p ()
  18798. "Is point in a fixed-width region?"
  18799. (save-match-data
  18800. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18801. (defun org-edit-special (&optional arg)
  18802. "Call a special editor for the element at point.
  18803. When at a table, call the formula editor with `org-table-edit-formulas'.
  18804. When in a source code block, call `org-edit-src-code'.
  18805. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18806. When at an #+INCLUDE keyword, visit the included file.
  18807. On a link, call `ffap' to visit the link at point.
  18808. Otherwise, return a user error."
  18809. (interactive "P")
  18810. (let ((element (org-element-at-point)))
  18811. (assert (not buffer-read-only) nil
  18812. "Buffer is read-only: %s" (buffer-name))
  18813. (case (org-element-type element)
  18814. (src-block
  18815. (if (not arg) (org-edit-src-code)
  18816. (let* ((info (org-babel-get-src-block-info))
  18817. (lang (nth 0 info))
  18818. (params (nth 2 info))
  18819. (session (cdr (assq :session params))))
  18820. (if (not session) (org-edit-src-code)
  18821. ;; At a src-block with a session and function called with
  18822. ;; an ARG: switch to the buffer related to the inferior
  18823. ;; process.
  18824. (switch-to-buffer
  18825. (funcall (intern (concat "org-babel-prep-session:" lang))
  18826. session params))))))
  18827. (keyword
  18828. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18829. (find-file-other-window
  18830. (org-remove-double-quotes
  18831. (car (org-split-string (org-element-property :value element)))))
  18832. (user-error "No special environment to edit here")))
  18833. (table
  18834. (if (eq (org-element-property :type element) 'table.el)
  18835. (org-edit-src-code)
  18836. (call-interactively 'org-table-edit-formulas)))
  18837. ;; Only Org tables contain `table-row' type elements.
  18838. (table-row (call-interactively 'org-table-edit-formulas))
  18839. ((example-block export-block) (org-edit-src-code))
  18840. (fixed-width (org-edit-fixed-width-region))
  18841. (otherwise
  18842. ;; No notable element at point. Though, we may be at a link,
  18843. ;; which is an object. Thus, scan deeper.
  18844. (if (eq (org-element-type (org-element-context element)) 'link)
  18845. (call-interactively 'ffap)
  18846. (user-error "No special environment to edit here"))))))
  18847. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18848. (defun org-ctrl-c-ctrl-c (&optional arg)
  18849. "Set tags in headline, or update according to changed information at point.
  18850. This command does many different things, depending on context:
  18851. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18852. this is what we do.
  18853. - If the cursor is on a statistics cookie, update it.
  18854. - If the cursor is in a headline, prompt for tags and insert them
  18855. into the current line, aligned to `org-tags-column'. When called
  18856. with prefix arg, realign all tags in the current buffer.
  18857. - If the cursor is in one of the special #+KEYWORD lines, this
  18858. triggers scanning the buffer for these lines and updating the
  18859. information.
  18860. - If the cursor is inside a table, realign the table. This command
  18861. works even if the automatic table editor has been turned off.
  18862. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18863. the entire table.
  18864. - If the cursor is at a footnote reference or definition, jump to
  18865. the corresponding definition or references, respectively.
  18866. - If the cursor is a the beginning of a dynamic block, update it.
  18867. - If the current buffer is a capture buffer, close note and file it.
  18868. - If the cursor is on a <<<target>>>, update radio targets and
  18869. corresponding links in this buffer.
  18870. - If the cursor is on a numbered item in a plain list, renumber the
  18871. ordered list.
  18872. - If the cursor is on a checkbox, toggle it.
  18873. - If the cursor is on a code block, evaluate it. The variable
  18874. `org-confirm-babel-evaluate' can be used to control prompting
  18875. before code block evaluation, by default every code block
  18876. evaluation requires confirmation. Code block evaluation can be
  18877. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18878. (interactive "P")
  18879. (cond
  18880. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18881. org-occur-highlights)
  18882. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18883. (org-remove-occur-highlights)
  18884. (message "Temporary highlights/overlays removed from current buffer"))
  18885. ((and (local-variable-p 'org-finish-function (current-buffer))
  18886. (fboundp org-finish-function))
  18887. (funcall org-finish-function))
  18888. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18889. (t
  18890. (let* ((context (org-element-context)) (type (org-element-type context)))
  18891. ;; Test if point is within a blank line.
  18892. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18893. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18894. (user-error "C-c C-c can do nothing useful at this location"))
  18895. (case type
  18896. ;; When at a link, act according to the parent instead.
  18897. (link (setq context (org-element-property :parent context))
  18898. (setq type (org-element-type context)))
  18899. ;; Unsupported object types: refer to the first supported
  18900. ;; element or object containing it.
  18901. ((bold code entity export-snippet inline-babel-call inline-src-block
  18902. italic latex-fragment line-break macro strike-through subscript
  18903. superscript underline verbatim)
  18904. (while (and (setq context (org-element-property :parent context))
  18905. (not (memq (setq type (org-element-type context))
  18906. '(radio-target paragraph verse-block
  18907. table-cell)))))))
  18908. ;; For convenience: at the first line of a paragraph on the
  18909. ;; same line as an item, apply function on that item instead.
  18910. (when (eq type 'paragraph)
  18911. (let ((parent (org-element-property :parent context)))
  18912. (when (and (eq (org-element-type parent) 'item)
  18913. (= (point-at-bol) (org-element-property :begin parent)))
  18914. (setq context parent type 'item))))
  18915. ;; Act according to type of element or object at point.
  18916. (case type
  18917. (clock (org-clock-update-time-maybe))
  18918. (dynamic-block
  18919. (save-excursion
  18920. (goto-char (org-element-property :post-affiliated context))
  18921. (org-update-dblock)))
  18922. (footnote-definition
  18923. (goto-char (org-element-property :post-affiliated context))
  18924. (call-interactively 'org-footnote-action))
  18925. (footnote-reference (call-interactively 'org-footnote-action))
  18926. ((headline inlinetask)
  18927. (save-excursion (goto-char (org-element-property :begin context))
  18928. (call-interactively 'org-set-tags)))
  18929. (item
  18930. ;; At an item: a double C-u set checkbox to "[-]"
  18931. ;; unconditionally, whereas a single one will toggle its
  18932. ;; presence. Without an universal argument, if the item
  18933. ;; has a checkbox, toggle it. Otherwise repair the list.
  18934. (let* ((box (org-element-property :checkbox context))
  18935. (struct (org-element-property :structure context))
  18936. (old-struct (copy-tree struct))
  18937. (parents (org-list-parents-alist struct))
  18938. (prevs (org-list-prevs-alist struct))
  18939. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18940. (org-list-set-checkbox
  18941. (org-element-property :begin context) struct
  18942. (cond ((equal arg '(16)) "[-]")
  18943. ((and (not box) (equal arg '(4))) "[ ]")
  18944. ((or (not box) (equal arg '(4))) nil)
  18945. ((eq box 'on) "[ ]")
  18946. (t "[X]")))
  18947. ;; Mimic `org-list-write-struct' but with grabbing
  18948. ;; a return value from `org-list-struct-fix-box'.
  18949. (org-list-struct-fix-ind struct parents 2)
  18950. (org-list-struct-fix-item-end struct)
  18951. (org-list-struct-fix-bul struct prevs)
  18952. (org-list-struct-fix-ind struct parents)
  18953. (let ((block-item
  18954. (org-list-struct-fix-box struct parents prevs orderedp)))
  18955. (if (and box (equal struct old-struct))
  18956. (if (equal arg '(16))
  18957. (message "Checkboxes already reset")
  18958. (user-error "Cannot toggle this checkbox: %s"
  18959. (if (eq box 'on)
  18960. "all subitems checked"
  18961. "unchecked subitems")))
  18962. (org-list-struct-apply-struct struct old-struct)
  18963. (org-update-checkbox-count-maybe))
  18964. (when block-item
  18965. (message "Checkboxes were removed due to empty box at line %d"
  18966. (org-current-line block-item))))))
  18967. (keyword
  18968. (let ((org-inhibit-startup-visibility-stuff t)
  18969. (org-startup-align-all-tables nil))
  18970. (when (boundp 'org-table-coordinate-overlays)
  18971. (mapc 'delete-overlay org-table-coordinate-overlays)
  18972. (setq org-table-coordinate-overlays nil))
  18973. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18974. (message "Local setup has been refreshed"))
  18975. (plain-list
  18976. ;; At a plain list, with a double C-u argument, set
  18977. ;; checkboxes of each item to "[-]", whereas a single one
  18978. ;; will toggle their presence according to the state of the
  18979. ;; first item in the list. Without an argument, repair the
  18980. ;; list.
  18981. (let* ((begin (org-element-property :contents-begin context))
  18982. (beginm (move-marker (make-marker) begin))
  18983. (struct (org-element-property :structure context))
  18984. (old-struct (copy-tree struct))
  18985. (first-box (save-excursion
  18986. (goto-char begin)
  18987. (looking-at org-list-full-item-re)
  18988. (match-string-no-properties 3)))
  18989. (new-box (cond ((equal arg '(16)) "[-]")
  18990. ((equal arg '(4)) (unless first-box "[ ]"))
  18991. ((equal first-box "[X]") "[ ]")
  18992. (t "[X]"))))
  18993. (cond
  18994. (arg
  18995. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18996. (org-list-get-all-items
  18997. begin struct (org-list-prevs-alist struct))))
  18998. ((and first-box (eq (point) begin))
  18999. ;; For convenience, when point is at bol on the first
  19000. ;; item of the list and no argument is provided, simply
  19001. ;; toggle checkbox of that item, if any.
  19002. (org-list-set-checkbox begin struct new-box)))
  19003. (org-list-write-struct
  19004. struct (org-list-parents-alist struct) old-struct)
  19005. (org-update-checkbox-count-maybe)
  19006. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19007. ((property-drawer node-property)
  19008. (call-interactively 'org-property-action))
  19009. ((radio-target target)
  19010. (call-interactively 'org-update-radio-target-regexp))
  19011. (statistics-cookie
  19012. (call-interactively 'org-update-statistics-cookies))
  19013. ((table table-cell table-row)
  19014. ;; At a table, recalculate every field and align it. Also
  19015. ;; send the table if necessary. If the table has
  19016. ;; a `table.el' type, just give up. At a table row or
  19017. ;; cell, maybe recalculate line but always align table.
  19018. (if (eq (org-element-property :type context) 'table.el)
  19019. (message "Use C-c ' to edit table.el tables")
  19020. (let ((org-enable-table-editor t))
  19021. (if (or (eq type 'table)
  19022. ;; Check if point is at a TBLFM line.
  19023. (and (eq type 'table-row)
  19024. (= (point) (org-element-property :end context))))
  19025. (save-excursion
  19026. (if (org-at-TBLFM-p)
  19027. (progn (require 'org-table)
  19028. (org-table-calc-current-TBLFM))
  19029. (goto-char (org-element-property :contents-begin context))
  19030. (org-call-with-arg 'org-table-recalculate (or arg t))
  19031. (orgtbl-send-table 'maybe)))
  19032. (org-table-maybe-eval-formula)
  19033. (cond (arg (call-interactively 'org-table-recalculate))
  19034. ((org-table-maybe-recalculate-line))
  19035. (t (org-table-align)))))))
  19036. (timestamp (org-timestamp-change 0 'day))
  19037. (otherwise
  19038. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19039. (user-error
  19040. "C-c C-c can do nothing useful at this location")))))))))
  19041. (defun org-mode-restart ()
  19042. (interactive)
  19043. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19044. (funcall major-mode)
  19045. (hack-local-variables)
  19046. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19047. (org-indent-mode -1)))
  19048. (message "%s restarted" major-mode))
  19049. (defun org-kill-note-or-show-branches ()
  19050. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19051. (interactive)
  19052. (if (not org-finish-function)
  19053. (progn
  19054. (hide-subtree)
  19055. (call-interactively 'show-branches))
  19056. (let ((org-note-abort t))
  19057. (funcall org-finish-function))))
  19058. (defun org-open-line (n)
  19059. "Insert a new row in tables, call `open-line' elsewhere.
  19060. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19061. (interactive "*p")
  19062. (cond
  19063. ((not org-special-ctrl-o)
  19064. (open-line n))
  19065. ((org-at-table-p)
  19066. (org-table-insert-row))
  19067. (t
  19068. (open-line n))))
  19069. (defun org-return (&optional indent)
  19070. "Goto next table row or insert a newline.
  19071. Calls `org-table-next-row' or `newline', depending on context.
  19072. See the individual commands for more information."
  19073. (interactive)
  19074. (let (org-ts-what)
  19075. (cond
  19076. ((or (bobp) (org-in-src-block-p))
  19077. (if indent (newline-and-indent) (newline)))
  19078. ((org-at-table-p)
  19079. (org-table-justify-field-maybe)
  19080. (call-interactively 'org-table-next-row))
  19081. ;; when `newline-and-indent' is called within a list, make sure
  19082. ;; text moved stays inside the item.
  19083. ((and (org-in-item-p) indent)
  19084. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  19085. (progn
  19086. (save-match-data (newline))
  19087. (org-indent-line-to (length (match-string 0))))
  19088. (let ((ind (org-get-indentation)))
  19089. (newline)
  19090. (if (org-looking-back org-list-end-re)
  19091. (org-indent-line)
  19092. (org-indent-line-to ind)))))
  19093. ((and org-return-follows-link
  19094. (org-at-timestamp-p t)
  19095. (not (eq org-ts-what 'after)))
  19096. (org-follow-timestamp-link))
  19097. ((and org-return-follows-link
  19098. (let ((tprop (get-text-property (point) 'face)))
  19099. (or (eq tprop 'org-link)
  19100. (and (listp tprop) (memq 'org-link tprop)))))
  19101. (call-interactively 'org-open-at-point))
  19102. ((and (org-at-heading-p)
  19103. (looking-at
  19104. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  19105. (org-show-entry)
  19106. (end-of-line 1)
  19107. (newline))
  19108. (t (if indent (newline-and-indent) (newline))))))
  19109. (defun org-return-indent ()
  19110. "Goto next table row or insert a newline and indent.
  19111. Calls `org-table-next-row' or `newline-and-indent', depending on
  19112. context. See the individual commands for more information."
  19113. (interactive)
  19114. (org-return t))
  19115. (defun org-ctrl-c-star ()
  19116. "Compute table, or change heading status of lines.
  19117. Calls `org-table-recalculate' or `org-toggle-heading',
  19118. depending on context."
  19119. (interactive)
  19120. (cond
  19121. ((org-at-table-p)
  19122. (call-interactively 'org-table-recalculate))
  19123. (t
  19124. ;; Convert all lines in region to list items
  19125. (call-interactively 'org-toggle-heading))))
  19126. (defun org-ctrl-c-minus ()
  19127. "Insert separator line in table or modify bullet status of line.
  19128. Also turns a plain line or a region of lines into list items.
  19129. Calls `org-table-insert-hline', `org-toggle-item', or
  19130. `org-cycle-list-bullet', depending on context."
  19131. (interactive)
  19132. (cond
  19133. ((org-at-table-p)
  19134. (call-interactively 'org-table-insert-hline))
  19135. ((org-region-active-p)
  19136. (call-interactively 'org-toggle-item))
  19137. ((org-in-item-p)
  19138. (call-interactively 'org-cycle-list-bullet))
  19139. (t
  19140. (call-interactively 'org-toggle-item))))
  19141. (defun org-toggle-item (arg)
  19142. "Convert headings or normal lines to items, items to normal lines.
  19143. If there is no active region, only the current line is considered.
  19144. If the first non blank line in the region is a headline, convert
  19145. all headlines to items, shifting text accordingly.
  19146. If it is an item, convert all items to normal lines.
  19147. If it is normal text, change region into a list of items.
  19148. With a prefix argument ARG, change the region in a single item."
  19149. (interactive "P")
  19150. (let ((shift-text
  19151. (function
  19152. ;; Shift text in current section to IND, from point to END.
  19153. ;; The function leaves point to END line.
  19154. (lambda (ind end)
  19155. (let ((min-i 1000) (end (copy-marker end)))
  19156. ;; First determine the minimum indentation (MIN-I) of
  19157. ;; the text.
  19158. (save-excursion
  19159. (catch 'exit
  19160. (while (< (point) end)
  19161. (let ((i (org-get-indentation)))
  19162. (cond
  19163. ;; Skip blank lines and inline tasks.
  19164. ((looking-at "^[ \t]*$"))
  19165. ((looking-at org-outline-regexp-bol))
  19166. ;; We can't find less than 0 indentation.
  19167. ((zerop i) (throw 'exit (setq min-i 0)))
  19168. ((< i min-i) (setq min-i i))))
  19169. (forward-line))))
  19170. ;; Then indent each line so that a line indented to
  19171. ;; MIN-I becomes indented to IND. Ignore blank lines
  19172. ;; and inline tasks in the process.
  19173. (let ((delta (- ind min-i)))
  19174. (while (< (point) end)
  19175. (unless (or (looking-at "^[ \t]*$")
  19176. (looking-at org-outline-regexp-bol))
  19177. (org-indent-line-to (+ (org-get-indentation) delta)))
  19178. (forward-line)))))))
  19179. (skip-blanks
  19180. (function
  19181. ;; Return beginning of first non-blank line, starting from
  19182. ;; line at POS.
  19183. (lambda (pos)
  19184. (save-excursion
  19185. (goto-char pos)
  19186. (skip-chars-forward " \r\t\n")
  19187. (point-at-bol)))))
  19188. beg end)
  19189. ;; Determine boundaries of changes.
  19190. (if (org-region-active-p)
  19191. (setq beg (funcall skip-blanks (region-beginning))
  19192. end (copy-marker (region-end)))
  19193. (setq beg (funcall skip-blanks (point-at-bol))
  19194. end (copy-marker (point-at-eol))))
  19195. ;; Depending on the starting line, choose an action on the text
  19196. ;; between BEG and END.
  19197. (org-with-limited-levels
  19198. (save-excursion
  19199. (goto-char beg)
  19200. (cond
  19201. ;; Case 1. Start at an item: de-itemize. Note that it only
  19202. ;; happens when a region is active: `org-ctrl-c-minus'
  19203. ;; would call `org-cycle-list-bullet' otherwise.
  19204. ((org-at-item-p)
  19205. (while (< (point) end)
  19206. (when (org-at-item-p)
  19207. (skip-chars-forward " \t")
  19208. (delete-region (point) (match-end 0)))
  19209. (forward-line)))
  19210. ;; Case 2. Start at an heading: convert to items.
  19211. ((org-at-heading-p)
  19212. (let* ((bul (org-list-bullet-string "-"))
  19213. (bul-len (length bul))
  19214. (done (org-entry-is-done-p))
  19215. (todo (org-entry-is-todo-p))
  19216. ;; Indentation of the first heading. It should be
  19217. ;; relative to the indentation of its parent, if any.
  19218. (start-ind (save-excursion
  19219. (cond
  19220. ((not org-adapt-indentation) 0)
  19221. ((not (outline-previous-heading)) 0)
  19222. (t (length (match-string 0))))))
  19223. ;; Level of first heading. Further headings will be
  19224. ;; compared to it to determine hierarchy in the list.
  19225. (ref-level (org-reduced-level (org-outline-level))))
  19226. (when (or done todo) (org-todo ""))
  19227. (while (< (point) end)
  19228. (let* ((level (org-reduced-level (org-outline-level)))
  19229. (delta (max 0 (- level ref-level))))
  19230. ;; If current headline is less indented than the first
  19231. ;; one, set it as reference, in order to preserve
  19232. ;; subtrees.
  19233. (when (< level ref-level) (setq ref-level level))
  19234. (replace-match bul t t)
  19235. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19236. (when (or done todo)
  19237. (let* ((struct (org-list-struct))
  19238. (old (copy-tree struct)))
  19239. (org-list-set-checkbox (line-beginning-position)
  19240. struct
  19241. (if done "[X]" "[ ]"))
  19242. (org-list-write-struct struct
  19243. (org-list-parents-alist struct)
  19244. old)))
  19245. ;; Ensure all text down to END (or SECTION-END) belongs
  19246. ;; to the newly created item.
  19247. (let ((section-end (save-excursion
  19248. (or (outline-next-heading) (point)))))
  19249. (forward-line)
  19250. (funcall shift-text
  19251. (+ start-ind (* (1+ delta) bul-len))
  19252. (min end section-end)))))))
  19253. ;; Case 3. Normal line with ARG: make the first line of region
  19254. ;; an item, and shift indentation of others lines to
  19255. ;; set them as item's body.
  19256. (arg (let* ((bul (org-list-bullet-string "-"))
  19257. (bul-len (length bul))
  19258. (ref-ind (org-get-indentation)))
  19259. (skip-chars-forward " \t")
  19260. (insert bul)
  19261. (forward-line)
  19262. (while (< (point) end)
  19263. ;; Ensure that lines less indented than first one
  19264. ;; still get included in item body.
  19265. (funcall shift-text
  19266. (+ ref-ind bul-len)
  19267. (min end (save-excursion (or (outline-next-heading)
  19268. (point)))))
  19269. (forward-line))))
  19270. ;; Case 4. Normal line without ARG: turn each non-item line
  19271. ;; into an item.
  19272. (t
  19273. (while (< (point) end)
  19274. (unless (or (org-at-heading-p) (org-at-item-p))
  19275. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19276. (replace-match
  19277. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19278. (forward-line))))))))
  19279. (defun org-toggle-heading (&optional nstars)
  19280. "Convert headings to normal text, or items or text to headings.
  19281. If there is no active region, only convert the current line.
  19282. With a \\[universal-argument] prefix, convert the whole list at
  19283. point into heading.
  19284. In a region:
  19285. - If the first non blank line is a headline, remove the stars
  19286. from all headlines in the region.
  19287. - If it is a normal line, turn each and every normal line (i.e.,
  19288. not an heading or an item) in the region into headings. If you
  19289. want to convert only the first line of this region, use one
  19290. universal prefix argument.
  19291. - If it is a plain list item, turn all plain list items into headings.
  19292. When converting a line into a heading, the number of stars is chosen
  19293. such that the lines become children of the current entry. However,
  19294. when a numeric prefix argument is given, its value determines the
  19295. number of stars to add."
  19296. (interactive "P")
  19297. (let ((skip-blanks
  19298. (function
  19299. ;; Return beginning of first non-blank line, starting from
  19300. ;; line at POS.
  19301. (lambda (pos)
  19302. (save-excursion
  19303. (goto-char pos)
  19304. (while (org-at-comment-p) (forward-line))
  19305. (skip-chars-forward " \r\t\n")
  19306. (point-at-bol)))))
  19307. beg end toggled)
  19308. ;; Determine boundaries of changes. If a universal prefix has
  19309. ;; been given, put the list in a region. If region ends at a bol,
  19310. ;; do not consider the last line to be in the region.
  19311. (when (and current-prefix-arg (org-at-item-p))
  19312. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19313. (org-mark-element))
  19314. (if (org-region-active-p)
  19315. (setq beg (funcall skip-blanks (region-beginning))
  19316. end (copy-marker (save-excursion
  19317. (goto-char (region-end))
  19318. (if (bolp) (point) (point-at-eol)))))
  19319. (setq beg (funcall skip-blanks (point-at-bol))
  19320. end (copy-marker (point-at-eol))))
  19321. ;; Ensure inline tasks don't count as headings.
  19322. (org-with-limited-levels
  19323. (save-excursion
  19324. (goto-char beg)
  19325. (cond
  19326. ;; Case 1. Started at an heading: de-star headings.
  19327. ((org-at-heading-p)
  19328. (while (< (point) end)
  19329. (when (org-at-heading-p t)
  19330. (looking-at org-outline-regexp) (replace-match "")
  19331. (setq toggled t))
  19332. (forward-line)))
  19333. ;; Case 2. Started at an item: change items into headlines.
  19334. ;; One star will be added by `org-list-to-subtree'.
  19335. ((org-at-item-p)
  19336. (let* ((stars (make-string
  19337. ;; subtract the star that will be added again by
  19338. ;; `org-list-to-subtree'
  19339. (if (numberp nstars) (1- nstars)
  19340. (or (org-current-level) 0))
  19341. ?*))
  19342. (add-stars
  19343. (cond (nstars "") ; stars from prefix only
  19344. ((equal stars "") "") ; before first heading
  19345. (org-odd-levels-only "*") ; inside heading, odd
  19346. (t "")))) ; inside heading, oddeven
  19347. (while (< (point) end)
  19348. (when (org-at-item-p)
  19349. ;; Pay attention to cases when region ends before list.
  19350. (let* ((struct (org-list-struct))
  19351. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19352. (save-restriction
  19353. (narrow-to-region (point) list-end)
  19354. (insert
  19355. (org-list-to-subtree
  19356. (org-list-parse-list t)
  19357. '(:istart (concat stars add-stars (funcall get-stars depth))
  19358. :icount (concat stars add-stars (funcall get-stars depth)))))))
  19359. (setq toggled t))
  19360. (forward-line))))
  19361. ;; Case 3. Started at normal text: make every line an heading,
  19362. ;; skipping headlines and items.
  19363. (t (let* ((stars
  19364. (make-string
  19365. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19366. (add-stars
  19367. (cond (nstars "") ; stars from prefix only
  19368. ((equal stars "") "*") ; before first heading
  19369. (org-odd-levels-only "**") ; inside heading, odd
  19370. (t "*"))) ; inside heading, oddeven
  19371. (rpl (concat stars add-stars " "))
  19372. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19373. (while (< (point) (if (equal nstars '(4)) lend end))
  19374. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19375. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19376. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19377. (forward-line)))))))
  19378. (unless toggled (message "Cannot toggle heading from here"))))
  19379. (defun org-meta-return (&optional arg)
  19380. "Insert a new heading or wrap a region in a table.
  19381. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19382. on context. See the individual commands for more information."
  19383. (interactive "P")
  19384. (org-check-before-invisible-edit 'insert)
  19385. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19386. (let* ((element (org-element-at-point))
  19387. (type (org-element-type element)))
  19388. (when (eq type 'table-row)
  19389. (setq element (org-element-property :parent element))
  19390. (setq type 'table))
  19391. (if (and (eq type 'table)
  19392. (eq (org-element-property :type element) 'org)
  19393. (>= (point) (org-element-property :contents-begin element))
  19394. (< (point) (org-element-property :contents-end element)))
  19395. (call-interactively 'org-table-wrap-region)
  19396. (call-interactively 'org-insert-heading)))))
  19397. ;;; Menu entries
  19398. (defsubst org-in-subtree-not-table-p ()
  19399. "Are we in a subtree and not in a table?"
  19400. (and (not (org-before-first-heading-p))
  19401. (not (org-at-table-p))))
  19402. ;; Define the Org-mode menus
  19403. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19404. '("Tbl"
  19405. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19406. ["Next Field" org-cycle (org-at-table-p)]
  19407. ["Previous Field" org-shifttab (org-at-table-p)]
  19408. ["Next Row" org-return (org-at-table-p)]
  19409. "--"
  19410. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19411. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19412. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19413. "--"
  19414. ("Column"
  19415. ["Move Column Left" org-metaleft (org-at-table-p)]
  19416. ["Move Column Right" org-metaright (org-at-table-p)]
  19417. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19418. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19419. ("Row"
  19420. ["Move Row Up" org-metaup (org-at-table-p)]
  19421. ["Move Row Down" org-metadown (org-at-table-p)]
  19422. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19423. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19424. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19425. "--"
  19426. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19427. ("Rectangle"
  19428. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19429. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19430. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19431. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19432. "--"
  19433. ("Calculate"
  19434. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19435. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19436. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19437. "--"
  19438. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19439. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19440. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19441. "--"
  19442. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19443. "--"
  19444. ["Sum Column/Rectangle" org-table-sum
  19445. (or (org-at-table-p) (org-region-active-p))]
  19446. ["Which Column?" org-table-current-column (org-at-table-p)])
  19447. ["Debug Formulas"
  19448. org-table-toggle-formula-debugger
  19449. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19450. ["Show Col/Row Numbers"
  19451. org-table-toggle-coordinate-overlays
  19452. :style toggle
  19453. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19454. "--"
  19455. ["Create" org-table-create (and (not (org-at-table-p))
  19456. org-enable-table-editor)]
  19457. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19458. ["Import from File" org-table-import (not (org-at-table-p))]
  19459. ["Export to File" org-table-export (org-at-table-p)]
  19460. "--"
  19461. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  19462. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19463. '("Org"
  19464. ("Show/Hide"
  19465. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19466. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19467. ["Sparse Tree..." org-sparse-tree t]
  19468. ["Reveal Context" org-reveal t]
  19469. ["Show All" show-all t]
  19470. "--"
  19471. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19472. "--"
  19473. ["New Heading" org-insert-heading t]
  19474. ("Navigate Headings"
  19475. ["Up" outline-up-heading t]
  19476. ["Next" outline-next-visible-heading t]
  19477. ["Previous" outline-previous-visible-heading t]
  19478. ["Next Same Level" outline-forward-same-level t]
  19479. ["Previous Same Level" outline-backward-same-level t]
  19480. "--"
  19481. ["Jump" org-goto t])
  19482. ("Edit Structure"
  19483. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19484. "--"
  19485. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19486. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19487. "--"
  19488. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19489. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19490. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19491. "--"
  19492. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19493. "--"
  19494. ["Copy visible text" org-copy-visible t]
  19495. "--"
  19496. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19497. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19498. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19499. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19500. "--"
  19501. ["Sort Region/Children" org-sort t]
  19502. "--"
  19503. ["Convert to odd levels" org-convert-to-odd-levels t]
  19504. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19505. ("Editing"
  19506. ["Emphasis..." org-emphasize t]
  19507. ["Edit Source Example" org-edit-special t]
  19508. "--"
  19509. ["Footnote new/jump" org-footnote-action t]
  19510. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19511. ("Archive"
  19512. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19513. "--"
  19514. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19515. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19516. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19517. )
  19518. "--"
  19519. ("Hyperlinks"
  19520. ["Store Link (Global)" org-store-link t]
  19521. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19522. ["Insert Link" org-insert-link t]
  19523. ["Follow Link" org-open-at-point t]
  19524. "--"
  19525. ["Next link" org-next-link t]
  19526. ["Previous link" org-previous-link t]
  19527. "--"
  19528. ["Descriptive Links"
  19529. org-toggle-link-display
  19530. :style radio
  19531. :selected org-descriptive-links
  19532. ]
  19533. ["Literal Links"
  19534. org-toggle-link-display
  19535. :style radio
  19536. :selected (not org-descriptive-links)])
  19537. "--"
  19538. ("TODO Lists"
  19539. ["TODO/DONE/-" org-todo t]
  19540. ("Select keyword"
  19541. ["Next keyword" org-shiftright (org-at-heading-p)]
  19542. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19543. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19544. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19545. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19546. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19547. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19548. "--"
  19549. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19550. :selected org-enforce-todo-dependencies :style toggle :active t]
  19551. "Settings for tree at point"
  19552. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19553. :selected (org-entry-get nil "ORDERED")
  19554. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19555. ["Do Children parallel" org-toggle-ordered-property :style radio
  19556. :selected (not (org-entry-get nil "ORDERED"))
  19557. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19558. "--"
  19559. ["Set Priority" org-priority t]
  19560. ["Priority Up" org-shiftup t]
  19561. ["Priority Down" org-shiftdown t]
  19562. "--"
  19563. ["Get news from all feeds" org-feed-update-all t]
  19564. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19565. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19566. ("TAGS and Properties"
  19567. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19568. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19569. "--"
  19570. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19571. ["Column view of properties" org-columns t]
  19572. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19573. ("Dates and Scheduling"
  19574. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19575. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19576. ("Change Date"
  19577. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19578. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19579. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19580. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19581. ["Compute Time Range" org-evaluate-time-range t]
  19582. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19583. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19584. "--"
  19585. ["Custom time format" org-toggle-time-stamp-overlays
  19586. :style radio :selected org-display-custom-times]
  19587. "--"
  19588. ["Goto Calendar" org-goto-calendar t]
  19589. ["Date from Calendar" org-date-from-calendar t]
  19590. "--"
  19591. ["Start/Restart Timer" org-timer-start t]
  19592. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19593. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19594. ["Insert Timer String" org-timer t]
  19595. ["Insert Timer Item" org-timer-item t])
  19596. ("Logging work"
  19597. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19598. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19599. ["Clock out" org-clock-out t]
  19600. ["Clock cancel" org-clock-cancel t]
  19601. "--"
  19602. ["Mark as default task" org-clock-mark-default-task t]
  19603. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19604. ["Goto running clock" org-clock-goto t]
  19605. "--"
  19606. ["Display times" org-clock-display t]
  19607. ["Create clock table" org-clock-report t]
  19608. "--"
  19609. ["Record DONE time"
  19610. (progn (setq org-log-done (not org-log-done))
  19611. (message "Switching to %s will %s record a timestamp"
  19612. (car org-done-keywords)
  19613. (if org-log-done "automatically" "not")))
  19614. :style toggle :selected org-log-done])
  19615. "--"
  19616. ["Agenda Command..." org-agenda t]
  19617. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19618. ("File List for Agenda")
  19619. ("Special views current file"
  19620. ["TODO Tree" org-show-todo-tree t]
  19621. ["Check Deadlines" org-check-deadlines t]
  19622. ["Timeline" org-timeline t]
  19623. ["Tags/Property tree" org-match-sparse-tree t])
  19624. "--"
  19625. ["Export/Publish..." org-export-dispatch t]
  19626. ("LaTeX"
  19627. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19628. :selected org-cdlatex-mode]
  19629. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19630. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19631. ["Modify math symbol" org-cdlatex-math-modify
  19632. (org-inside-LaTeX-fragment-p)]
  19633. ["Insert citation" org-reftex-citation t]
  19634. "--"
  19635. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19636. "--"
  19637. ("MobileOrg"
  19638. ["Push Files and Views" org-mobile-push t]
  19639. ["Get Captured and Flagged" org-mobile-pull t]
  19640. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19641. "--"
  19642. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19643. "--"
  19644. ("Documentation"
  19645. ["Show Version" org-version t]
  19646. ["Info Documentation" org-info t])
  19647. ("Customize"
  19648. ["Browse Org Group" org-customize t]
  19649. "--"
  19650. ["Expand This Menu" org-create-customize-menu
  19651. (fboundp 'customize-menu-create)])
  19652. ["Send bug report" org-submit-bug-report t]
  19653. "--"
  19654. ("Refresh/Reload"
  19655. ["Refresh setup current buffer" org-mode-restart t]
  19656. ["Reload Org (after update)" org-reload t]
  19657. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19658. ))
  19659. (defun org-info (&optional node)
  19660. "Read documentation for Org-mode in the info system.
  19661. With optional NODE, go directly to that node."
  19662. (interactive)
  19663. (info (format "(org)%s" (or node ""))))
  19664. ;;;###autoload
  19665. (defun org-submit-bug-report ()
  19666. "Submit a bug report on Org-mode via mail.
  19667. Don't hesitate to report any problems or inaccurate documentation.
  19668. If you don't have setup sending mail from (X)Emacs, please copy the
  19669. output buffer into your mail program, as it gives us important
  19670. information about your Org-mode version and configuration."
  19671. (interactive)
  19672. (require 'reporter)
  19673. (org-load-modules-maybe)
  19674. (org-require-autoloaded-modules)
  19675. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19676. (reporter-submit-bug-report
  19677. "emacs-orgmode@gnu.org"
  19678. (org-version nil 'full)
  19679. (let (list)
  19680. (save-window-excursion
  19681. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19682. (delete-other-windows)
  19683. (erase-buffer)
  19684. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19685. We would like to add your full Org-mode and Outline configuration to the
  19686. bug report. This greatly simplifies the work of the maintainer and
  19687. other experts on the mailing list.
  19688. HOWEVER, some variables you have customized may contain private
  19689. information. The names of customers, colleagues, or friends, might
  19690. appear in the form of file names, tags, todo states, or search strings.
  19691. If you answer yes to the prompt, you might want to check and remove
  19692. such private information before sending the email.")
  19693. (add-text-properties (point-min) (point-max) '(face org-warning))
  19694. (when (yes-or-no-p "Include your Org-mode configuration ")
  19695. (mapatoms
  19696. (lambda (v)
  19697. (and (boundp v)
  19698. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19699. (or (and (symbol-value v)
  19700. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19701. (and
  19702. (get v 'custom-type) (get v 'standard-value)
  19703. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19704. (push v list)))))
  19705. (kill-buffer (get-buffer "*Warn about privacy*"))
  19706. list))
  19707. nil nil
  19708. "Remember to cover the basics, that is, what you expected to happen and
  19709. what in fact did happen. You don't know how to make a good report? See
  19710. http://orgmode.org/manual/Feedback.html#Feedback
  19711. Your bug report will be posted to the Org-mode mailing list.
  19712. ------------------------------------------------------------------------")
  19713. (save-excursion
  19714. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19715. (replace-match "\\1Bug: \\3 [\\2]")))))
  19716. (defun org-install-agenda-files-menu ()
  19717. (let ((bl (buffer-list)))
  19718. (save-excursion
  19719. (while bl
  19720. (set-buffer (pop bl))
  19721. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19722. (when (derived-mode-p 'org-mode)
  19723. (easy-menu-change
  19724. '("Org") "File List for Agenda"
  19725. (append
  19726. (list
  19727. ["Edit File List" (org-edit-agenda-file-list) t]
  19728. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19729. ["Remove Current File from List" org-remove-file t]
  19730. ["Cycle through agenda files" org-cycle-agenda-files t]
  19731. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19732. "--")
  19733. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19734. ;;;; Documentation
  19735. (defun org-require-autoloaded-modules ()
  19736. (interactive)
  19737. (mapc 'require
  19738. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19739. org-table org-timer)))
  19740. ;;;###autoload
  19741. (defun org-reload (&optional uncompiled)
  19742. "Reload all org lisp files.
  19743. With prefix arg UNCOMPILED, load the uncompiled versions."
  19744. (interactive "P")
  19745. (require 'loadhist)
  19746. (let* ((org-dir (org-find-library-dir "org"))
  19747. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19748. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19749. (remove-re (mapconcat 'identity
  19750. (mapcar (lambda (f) (concat "^" f "$"))
  19751. (list (if (featurep 'xemacs)
  19752. "org-colview"
  19753. "org-colview-xemacs")
  19754. "org" "org-loaddefs" "org-version"))
  19755. "\\|"))
  19756. (feats (delete-dups
  19757. (mapcar 'file-name-sans-extension
  19758. (mapcar 'file-name-nondirectory
  19759. (delq nil
  19760. (mapcar 'feature-file
  19761. features))))))
  19762. (lfeat (append
  19763. (sort
  19764. (setq feats
  19765. (delq nil (mapcar
  19766. (lambda (f)
  19767. (if (and (string-match feature-re f)
  19768. (not (string-match remove-re f)))
  19769. f nil))
  19770. feats)))
  19771. 'string-lessp)
  19772. (list "org-version" "org")))
  19773. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19774. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19775. load-uncore load-misses)
  19776. (setq load-misses
  19777. (delq 't
  19778. (mapcar (lambda (f)
  19779. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19780. (and (string= org-dir contrib-dir)
  19781. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19782. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19783. (add-to-list 'load-uncore f 'append)
  19784. 't)
  19785. f))
  19786. lfeat)))
  19787. (if load-uncore
  19788. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19789. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19790. (if load-misses
  19791. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19792. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19793. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19794. ;;;###autoload
  19795. (defun org-customize ()
  19796. "Call the customize function with org as argument."
  19797. (interactive)
  19798. (org-load-modules-maybe)
  19799. (org-require-autoloaded-modules)
  19800. (customize-browse 'org))
  19801. (defun org-create-customize-menu ()
  19802. "Create a full customization menu for Org-mode, insert it into the menu."
  19803. (interactive)
  19804. (org-load-modules-maybe)
  19805. (org-require-autoloaded-modules)
  19806. (if (fboundp 'customize-menu-create)
  19807. (progn
  19808. (easy-menu-change
  19809. '("Org") "Customize"
  19810. `(["Browse Org group" org-customize t]
  19811. "--"
  19812. ,(customize-menu-create 'org)
  19813. ["Set" Custom-set t]
  19814. ["Save" Custom-save t]
  19815. ["Reset to Current" Custom-reset-current t]
  19816. ["Reset to Saved" Custom-reset-saved t]
  19817. ["Reset to Standard Settings" Custom-reset-standard t]))
  19818. (message "\"Org\"-menu now contains full customization menu"))
  19819. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19820. ;;;; Miscellaneous stuff
  19821. ;;; Generally useful functions
  19822. (defsubst org-get-at-eol (property n)
  19823. "Get text property PROPERTY at the end of line less N characters."
  19824. (get-text-property (- (point-at-eol) n) property))
  19825. (defun org-find-text-property-in-string (prop s)
  19826. "Return the first non-nil value of property PROP in string S."
  19827. (or (get-text-property 0 prop s)
  19828. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19829. prop s)))
  19830. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19831. "Display the given MESSAGE as a warning."
  19832. (if (fboundp 'display-warning)
  19833. (display-warning 'org message
  19834. (if (featurep 'xemacs) 'warning :warning))
  19835. (let ((buf (get-buffer-create "*Org warnings*")))
  19836. (with-current-buffer buf
  19837. (goto-char (point-max))
  19838. (insert "Warning (Org): " message)
  19839. (unless (bolp)
  19840. (newline)))
  19841. (display-buffer buf)
  19842. (sit-for 0))))
  19843. (defun org-eval (form)
  19844. "Eval FORM and return result."
  19845. (condition-case error
  19846. (eval form)
  19847. (error (format "%%![Error: %s]" error))))
  19848. (defun org-in-clocktable-p ()
  19849. "Check if the cursor is in a clocktable."
  19850. (let ((pos (point)) start)
  19851. (save-excursion
  19852. (end-of-line 1)
  19853. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19854. (setq start (match-beginning 0))
  19855. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19856. (>= (match-end 0) pos)
  19857. start))))
  19858. (defun org-in-verbatim-emphasis ()
  19859. (save-match-data
  19860. (and (org-in-regexp org-emph-re 2)
  19861. (>= (point) (match-beginning 3))
  19862. (<= (point) (match-end 4))
  19863. (member (match-string 3) '("=" "~")))))
  19864. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19865. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19866. (if (and marker (marker-buffer marker)
  19867. (buffer-live-p (marker-buffer marker)))
  19868. (progn
  19869. (org-pop-to-buffer-same-window (marker-buffer marker))
  19870. (if (or (> marker (point-max)) (< marker (point-min)))
  19871. (widen))
  19872. (goto-char marker)
  19873. (org-show-context 'org-goto))
  19874. (if bookmark
  19875. (bookmark-jump bookmark)
  19876. (error "Cannot find location"))))
  19877. (defun org-quote-csv-field (s)
  19878. "Quote field for inclusion in CSV material."
  19879. (if (string-match "[\",]" s)
  19880. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19881. s))
  19882. (defun org-force-self-insert (N)
  19883. "Needed to enforce self-insert under remapping."
  19884. (interactive "p")
  19885. (self-insert-command N))
  19886. (defun org-string-width (s)
  19887. "Compute width of string, ignoring invisible characters.
  19888. This ignores character with invisibility property `org-link', and also
  19889. characters with property `org-cwidth', because these will become invisible
  19890. upon the next fontification round."
  19891. (let (b l)
  19892. (when (or (eq t buffer-invisibility-spec)
  19893. (assq 'org-link buffer-invisibility-spec))
  19894. (while (setq b (text-property-any 0 (length s)
  19895. 'invisible 'org-link s))
  19896. (setq s (concat (substring s 0 b)
  19897. (substring s (or (next-single-property-change
  19898. b 'invisible s) (length s)))))))
  19899. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19900. (setq s (concat (substring s 0 b)
  19901. (substring s (or (next-single-property-change
  19902. b 'org-cwidth s) (length s))))))
  19903. (setq l (string-width s) b -1)
  19904. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19905. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19906. l))
  19907. (defun org-shorten-string (s maxlength)
  19908. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19909. If the string is shorter or has length MAXLENGTH, just return the
  19910. original string. If it is longer, the functions finds a space in the
  19911. string, breaks this string off at that locations and adds three dots
  19912. as ellipsis. Including the ellipsis, the string will not be longer
  19913. than MAXLENGTH. If finding a good breaking point in the string does
  19914. not work, the string is just chopped off in the middle of a word
  19915. if necessary."
  19916. (if (<= (length s) maxlength)
  19917. s
  19918. (let* ((n (max (- maxlength 4) 1))
  19919. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19920. (if (string-match re s)
  19921. (concat (match-string 1 s) "...")
  19922. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19923. (defun org-get-indentation (&optional line)
  19924. "Get the indentation of the current line, interpreting tabs.
  19925. When LINE is given, assume it represents a line and compute its indentation."
  19926. (if line
  19927. (if (string-match "^ *" (org-remove-tabs line))
  19928. (match-end 0))
  19929. (save-excursion
  19930. (beginning-of-line 1)
  19931. (skip-chars-forward " \t")
  19932. (current-column))))
  19933. (defun org-get-string-indentation (s)
  19934. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19935. (let ((n -1) (i 0) (w tab-width) c)
  19936. (catch 'exit
  19937. (while (< (setq n (1+ n)) (length s))
  19938. (setq c (aref s n))
  19939. (cond ((= c ?\ ) (setq i (1+ i)))
  19940. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19941. (t (throw 'exit t)))))
  19942. i))
  19943. (defun org-remove-tabs (s &optional width)
  19944. "Replace tabulators in S with spaces.
  19945. Assumes that s is a single line, starting in column 0."
  19946. (setq width (or width tab-width))
  19947. (while (string-match "\t" s)
  19948. (setq s (replace-match
  19949. (make-string
  19950. (- (* width (/ (+ (match-beginning 0) width) width))
  19951. (match-beginning 0)) ?\ )
  19952. t t s)))
  19953. s)
  19954. (defun org-fix-indentation (line ind)
  19955. "Fix indentation in LINE.
  19956. IND is a cons cell with target and minimum indentation.
  19957. If the current indentation in LINE is smaller than the minimum,
  19958. leave it alone. If it is larger than ind, set it to the target."
  19959. (let* ((l (org-remove-tabs line))
  19960. (i (org-get-indentation l))
  19961. (i1 (car ind)) (i2 (cdr ind)))
  19962. (if (>= i i2) (setq l (substring line i2)))
  19963. (if (> i1 0)
  19964. (concat (make-string i1 ?\ ) l)
  19965. l)))
  19966. (defun org-remove-indentation (code &optional n)
  19967. "Remove the maximum common indentation from the lines in CODE.
  19968. N may optionally be the number of spaces to remove."
  19969. (with-temp-buffer
  19970. (insert code)
  19971. (org-do-remove-indentation n)
  19972. (buffer-string)))
  19973. (defun org-do-remove-indentation (&optional n)
  19974. "Remove the maximum common indentation from the buffer."
  19975. (untabify (point-min) (point-max))
  19976. (let ((min 10000) re)
  19977. (if n
  19978. (setq min n)
  19979. (goto-char (point-min))
  19980. (while (re-search-forward "^ *[^ \n]" nil t)
  19981. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19982. (unless (or (= min 0) (= min 10000))
  19983. (setq re (format "^ \\{%d\\}" min))
  19984. (goto-char (point-min))
  19985. (while (re-search-forward re nil t)
  19986. (replace-match "")
  19987. (end-of-line 1))
  19988. min)))
  19989. (defun org-fill-template (template alist)
  19990. "Find each %key of ALIST in TEMPLATE and replace it."
  19991. (let ((case-fold-search nil)
  19992. entry key value)
  19993. (setq alist (sort (copy-sequence alist)
  19994. (lambda (a b) (< (length (car a)) (length (car b))))))
  19995. (while (setq entry (pop alist))
  19996. (setq template
  19997. (replace-regexp-in-string
  19998. (concat "%" (regexp-quote (car entry)))
  19999. (or (cdr entry) "") template t t)))
  20000. template))
  20001. (defun org-base-buffer (buffer)
  20002. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20003. (if (not buffer)
  20004. buffer
  20005. (or (buffer-base-buffer buffer)
  20006. buffer)))
  20007. (defun org-wrap (string &optional width lines)
  20008. "Wrap string to either a number of lines, or a width in characters.
  20009. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20010. that costs. If there is a word longer than WIDTH, the text is actually
  20011. wrapped to the length of that word.
  20012. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20013. many lines, whatever width that takes.
  20014. The return value is a list of lines, without newlines at the end."
  20015. (let* ((words (org-split-string string "[ \t\n]+"))
  20016. (maxword (apply 'max (mapcar 'org-string-width words)))
  20017. w ll)
  20018. (cond (width
  20019. (org-do-wrap words (max maxword width)))
  20020. (lines
  20021. (setq w maxword)
  20022. (setq ll (org-do-wrap words maxword))
  20023. (if (<= (length ll) lines)
  20024. ll
  20025. (setq ll words)
  20026. (while (> (length ll) lines)
  20027. (setq w (1+ w))
  20028. (setq ll (org-do-wrap words w)))
  20029. ll))
  20030. (t (error "Cannot wrap this")))))
  20031. (defun org-do-wrap (words width)
  20032. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20033. (let (lines line)
  20034. (while words
  20035. (setq line (pop words))
  20036. (while (and words (< (+ (length line) (length (car words))) width))
  20037. (setq line (concat line " " (pop words))))
  20038. (setq lines (push line lines)))
  20039. (nreverse lines)))
  20040. (defun org-split-string (string &optional separators)
  20041. "Splits STRING into substrings at SEPARATORS.
  20042. No empty strings are returned if there are matches at the beginning
  20043. and end of string."
  20044. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  20045. (start 0)
  20046. notfirst
  20047. (list nil))
  20048. (while (and (string-match rexp string
  20049. (if (and notfirst
  20050. (= start (match-beginning 0))
  20051. (< start (length string)))
  20052. (1+ start) start))
  20053. (< (match-beginning 0) (length string)))
  20054. (setq notfirst t)
  20055. (or (eq (match-beginning 0) 0)
  20056. (and (eq (match-beginning 0) (match-end 0))
  20057. (eq (match-beginning 0) start))
  20058. (setq list
  20059. (cons (substring string start (match-beginning 0))
  20060. list)))
  20061. (setq start (match-end 0)))
  20062. (or (eq start (length string))
  20063. (setq list
  20064. (cons (substring string start)
  20065. list)))
  20066. (nreverse list)))
  20067. (defun org-quote-vert (s)
  20068. "Replace \"|\" with \"\\vert\"."
  20069. (while (string-match "|" s)
  20070. (setq s (replace-match "\\vert" t t s)))
  20071. s)
  20072. (defun org-uuidgen-p (s)
  20073. "Is S an ID created by UUIDGEN?"
  20074. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20075. (defun org-in-src-block-p (&optional inside)
  20076. "Whether point is in a code source block.
  20077. When INSIDE is non-nil, don't consider we are within a src block
  20078. when point is at #+BEGIN_SRC or #+END_SRC."
  20079. (let ((case-fold-search t) ov)
  20080. (or (and (eq (get-char-property (point) 'src-block) t))
  20081. (and (not inside)
  20082. (save-match-data
  20083. (save-excursion
  20084. (beginning-of-line)
  20085. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20086. (defun org-context ()
  20087. "Return a list of contexts of the current cursor position.
  20088. If several contexts apply, all are returned.
  20089. Each context entry is a list with a symbol naming the context, and
  20090. two positions indicating start and end of the context. Possible
  20091. contexts are:
  20092. :headline anywhere in a headline
  20093. :headline-stars on the leading stars in a headline
  20094. :todo-keyword on a TODO keyword (including DONE) in a headline
  20095. :tags on the TAGS in a headline
  20096. :priority on the priority cookie in a headline
  20097. :item on the first line of a plain list item
  20098. :item-bullet on the bullet/number of a plain list item
  20099. :checkbox on the checkbox in a plain list item
  20100. :table in an org-mode table
  20101. :table-special on a special filed in a table
  20102. :table-table in a table.el table
  20103. :clocktable in a clocktable
  20104. :src-block in a source block
  20105. :link on a hyperlink
  20106. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20107. :target on a <<target>>
  20108. :radio-target on a <<<radio-target>>>
  20109. :latex-fragment on a LaTeX fragment
  20110. :latex-preview on a LaTeX fragment with overlaid preview image
  20111. This function expects the position to be visible because it uses font-lock
  20112. faces as a help to recognize the following contexts: :table-special, :link,
  20113. and :keyword."
  20114. (let* ((f (get-text-property (point) 'face))
  20115. (faces (if (listp f) f (list f)))
  20116. (case-fold-search t)
  20117. (p (point)) clist o)
  20118. ;; First the large context
  20119. (cond
  20120. ((org-at-heading-p t)
  20121. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20122. (when (progn
  20123. (beginning-of-line 1)
  20124. (looking-at org-todo-line-tags-regexp))
  20125. (push (org-point-in-group p 1 :headline-stars) clist)
  20126. (push (org-point-in-group p 2 :todo-keyword) clist)
  20127. (push (org-point-in-group p 4 :tags) clist))
  20128. (goto-char p)
  20129. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20130. (if (looking-at "\\[#[A-Z0-9]\\]")
  20131. (push (org-point-in-group p 0 :priority) clist)))
  20132. ((org-at-item-p)
  20133. (push (org-point-in-group p 2 :item-bullet) clist)
  20134. (push (list :item (point-at-bol)
  20135. (save-excursion (org-end-of-item) (point)))
  20136. clist)
  20137. (and (org-at-item-checkbox-p)
  20138. (push (org-point-in-group p 0 :checkbox) clist)))
  20139. ((org-at-table-p)
  20140. (push (list :table (org-table-begin) (org-table-end)) clist)
  20141. (if (memq 'org-formula faces)
  20142. (push (list :table-special
  20143. (previous-single-property-change p 'face)
  20144. (next-single-property-change p 'face)) clist)))
  20145. ((org-at-table-p 'any)
  20146. (push (list :table-table) clist)))
  20147. (goto-char p)
  20148. (let ((case-fold-search t))
  20149. ;; New the "medium" contexts: clocktables, source blocks
  20150. (cond ((org-in-clocktable-p)
  20151. (push (list :clocktable
  20152. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20153. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20154. (match-beginning 1))
  20155. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20156. (match-end 0))) clist))
  20157. ((org-in-src-block-p)
  20158. (push (list :src-block
  20159. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20160. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20161. (match-beginning 1))
  20162. (and (search-forward "#+END_SRC" nil t)
  20163. (match-beginning 0))) clist))))
  20164. (goto-char p)
  20165. ;; Now the small context
  20166. (cond
  20167. ((org-at-timestamp-p)
  20168. (push (org-point-in-group p 0 :timestamp) clist))
  20169. ((memq 'org-link faces)
  20170. (push (list :link
  20171. (previous-single-property-change p 'face)
  20172. (next-single-property-change p 'face)) clist))
  20173. ((memq 'org-special-keyword faces)
  20174. (push (list :keyword
  20175. (previous-single-property-change p 'face)
  20176. (next-single-property-change p 'face)) clist))
  20177. ((org-at-target-p)
  20178. (push (org-point-in-group p 0 :target) clist)
  20179. (goto-char (1- (match-beginning 0)))
  20180. (if (looking-at org-radio-target-regexp)
  20181. (push (org-point-in-group p 0 :radio-target) clist))
  20182. (goto-char p))
  20183. ((setq o (car (delq nil
  20184. (mapcar
  20185. (lambda (x)
  20186. (if (memq x org-latex-fragment-image-overlays) x))
  20187. (overlays-at (point))))))
  20188. (push (list :latex-fragment
  20189. (overlay-start o) (overlay-end o)) clist)
  20190. (push (list :latex-preview
  20191. (overlay-start o) (overlay-end o)) clist))
  20192. ((org-inside-LaTeX-fragment-p)
  20193. ;; FIXME: positions wrong.
  20194. (push (list :latex-fragment (point) (point)) clist)))
  20195. (setq clist (nreverse (delq nil clist)))
  20196. clist))
  20197. ;; FIXME: Compare with at-regexp-p Do we need both?
  20198. (defun org-in-regexp (re &optional nlines visually)
  20199. "Check if point is inside a match of regexp.
  20200. Normally only the current line is checked, but you can include NLINES extra
  20201. lines both before and after point into the search.
  20202. If VISUALLY is set, require that the cursor is not after the match but
  20203. really on, so that the block visually is on the match."
  20204. (catch 'exit
  20205. (let ((pos (point))
  20206. (eol (point-at-eol (+ 1 (or nlines 0))))
  20207. (inc (if visually 1 0)))
  20208. (save-excursion
  20209. (beginning-of-line (- 1 (or nlines 0)))
  20210. (while (re-search-forward re eol t)
  20211. (if (and (<= (match-beginning 0) pos)
  20212. (>= (+ inc (match-end 0)) pos))
  20213. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  20214. (defun org-at-regexp-p (regexp)
  20215. "Is point inside a match of REGEXP in the current line?"
  20216. (catch 'exit
  20217. (save-excursion
  20218. (let ((pos (point)) (end (point-at-eol)))
  20219. (beginning-of-line 1)
  20220. (while (re-search-forward regexp end t)
  20221. (if (and (<= (match-beginning 0) pos)
  20222. (>= (match-end 0) pos))
  20223. (throw 'exit t)))
  20224. nil))))
  20225. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20226. "Non-nil when point is between matches of START-RE and END-RE.
  20227. Also return a non-nil value when point is on one of the matches.
  20228. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20229. buffer positions. Default values are the positions of headlines
  20230. surrounding the point.
  20231. The functions returns a cons cell whose car (resp. cdr) is the
  20232. position before START-RE (resp. after END-RE)."
  20233. (save-match-data
  20234. (let ((pos (point))
  20235. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20236. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20237. beg end)
  20238. (save-excursion
  20239. ;; Point is on a block when on START-RE or if START-RE can be
  20240. ;; found before it...
  20241. (and (or (org-at-regexp-p start-re)
  20242. (re-search-backward start-re limit-up t))
  20243. (setq beg (match-beginning 0))
  20244. ;; ... and END-RE after it...
  20245. (goto-char (match-end 0))
  20246. (re-search-forward end-re limit-down t)
  20247. (> (setq end (match-end 0)) pos)
  20248. ;; ... without another START-RE in-between.
  20249. (goto-char (match-beginning 0))
  20250. (not (re-search-backward start-re (1+ beg) t))
  20251. ;; Return value.
  20252. (cons beg end))))))
  20253. (defun org-in-block-p (names)
  20254. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20255. NAMES is a list of strings containing names of blocks.
  20256. Return first block name matched, or nil. Beware that in case of
  20257. nested blocks, the returned name may not belong to the closest
  20258. block from point."
  20259. (save-match-data
  20260. (catch 'exit
  20261. (let ((case-fold-search t)
  20262. (lim-up (save-excursion (outline-previous-heading)))
  20263. (lim-down (save-excursion (outline-next-heading))))
  20264. (mapc (lambda (name)
  20265. (let ((n (regexp-quote name)))
  20266. (when (org-between-regexps-p
  20267. (concat "^[ \t]*#\\+begin_" n)
  20268. (concat "^[ \t]*#\\+end_" n)
  20269. lim-up lim-down)
  20270. (throw 'exit n))))
  20271. names))
  20272. nil)))
  20273. (defun org-in-drawer-p ()
  20274. "Non-nil if point is within a drawer.
  20275. If point is within a drawer, return it, as parsed data."
  20276. (let ((element (save-match-data (org-element-at-point))))
  20277. (while (and element (not (memq (org-element-type element)
  20278. '(drawer property-drawer))))
  20279. (setq element (org-element-property :parent element)))
  20280. element))
  20281. (defun org-occur-in-agenda-files (regexp &optional nlines)
  20282. "Call `multi-occur' with buffers for all agenda files."
  20283. (interactive "sOrg-files matching: \np")
  20284. (let* ((files (org-agenda-files))
  20285. (tnames (mapcar 'file-truename files))
  20286. (extra org-agenda-text-search-extra-files)
  20287. f)
  20288. (when (eq (car extra) 'agenda-archives)
  20289. (setq extra (cdr extra))
  20290. (setq files (org-add-archive-files files)))
  20291. (while (setq f (pop extra))
  20292. (unless (member (file-truename f) tnames)
  20293. (add-to-list 'files f 'append)
  20294. (add-to-list 'tnames (file-truename f) 'append)))
  20295. (multi-occur
  20296. (mapcar (lambda (x)
  20297. (with-current-buffer
  20298. (or (get-file-buffer x) (find-file-noselect x))
  20299. (widen)
  20300. (current-buffer)))
  20301. files)
  20302. regexp)))
  20303. (if (boundp 'occur-mode-find-occurrence-hook)
  20304. ;; Emacs 23
  20305. (add-hook 'occur-mode-find-occurrence-hook
  20306. (lambda ()
  20307. (when (derived-mode-p 'org-mode)
  20308. (org-reveal))))
  20309. ;; Emacs 22
  20310. (defadvice occur-mode-goto-occurrence
  20311. (after org-occur-reveal activate)
  20312. (and (derived-mode-p 'org-mode) (org-reveal)))
  20313. (defadvice occur-mode-goto-occurrence-other-window
  20314. (after org-occur-reveal activate)
  20315. (and (derived-mode-p 'org-mode) (org-reveal)))
  20316. (defadvice occur-mode-display-occurrence
  20317. (after org-occur-reveal activate)
  20318. (when (derived-mode-p 'org-mode)
  20319. (let ((pos (occur-mode-find-occurrence)))
  20320. (with-current-buffer (marker-buffer pos)
  20321. (save-excursion
  20322. (goto-char pos)
  20323. (org-reveal)))))))
  20324. (defun org-occur-link-in-agenda-files ()
  20325. "Create a link and search for it in the agendas.
  20326. The link is not stored in `org-stored-links', it is just created
  20327. for the search purpose."
  20328. (interactive)
  20329. (let ((link (condition-case nil
  20330. (org-store-link nil)
  20331. (error "Unable to create a link to here"))))
  20332. (org-occur-in-agenda-files (regexp-quote link))))
  20333. (defun org-reverse-string (string)
  20334. "Return the reverse of STRING."
  20335. (apply 'string (reverse (string-to-list string))))
  20336. ;; defsubst org-uniquify must be defined before first use
  20337. (defun org-uniquify-alist (alist)
  20338. "Merge elements of ALIST with the same key.
  20339. For example, in this alist:
  20340. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20341. => '((a 1 3) (b 2))
  20342. merge (a 1) and (a 3) into (a 1 3).
  20343. The function returns the new ALIST."
  20344. (let (rtn)
  20345. (mapc
  20346. (lambda (e)
  20347. (let (n)
  20348. (if (not (assoc (car e) rtn))
  20349. (push e rtn)
  20350. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20351. (setq rtn (assq-delete-all (car e) rtn))
  20352. (push n rtn))))
  20353. alist)
  20354. rtn))
  20355. (defun org-delete-all (elts list)
  20356. "Remove all elements in ELTS from LIST."
  20357. (while elts
  20358. (setq list (delete (pop elts) list)))
  20359. list)
  20360. (defun org-count (cl-item cl-seq)
  20361. "Count the number of occurrences of ITEM in SEQ.
  20362. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20363. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20364. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20365. (while (< cl-start cl-end)
  20366. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20367. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20368. (setq cl-start (1+ cl-start)))
  20369. cl-count))
  20370. (defun org-remove-if (predicate seq)
  20371. "Remove everything from SEQ that fulfills PREDICATE."
  20372. (let (res e)
  20373. (while seq
  20374. (setq e (pop seq))
  20375. (if (not (funcall predicate e)) (push e res)))
  20376. (nreverse res)))
  20377. (defun org-remove-if-not (predicate seq)
  20378. "Remove everything from SEQ that does not fulfill PREDICATE."
  20379. (let (res e)
  20380. (while seq
  20381. (setq e (pop seq))
  20382. (if (funcall predicate e) (push e res)))
  20383. (nreverse res)))
  20384. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20385. "Reduce two-argument FUNCTION across SEQ.
  20386. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20387. \":initial-value\" removed."
  20388. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20389. (cadr (memq :initial-value cl-keys)))
  20390. (cl-seq (pop cl-seq))
  20391. (t (funcall cl-func)))))
  20392. (while cl-seq
  20393. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20394. cl-accum))
  20395. (defun org-every (pred seq)
  20396. "Return true if PREDICATE is true of every element of SEQ.
  20397. Adapted from `every' in cl.el."
  20398. (catch 'org-every
  20399. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20400. t))
  20401. (defun org-some (pred seq)
  20402. "Return true if PREDICATE is true of any element of SEQ.
  20403. Adapted from `some' in cl.el."
  20404. (catch 'org-some
  20405. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20406. nil))
  20407. (defun org-back-over-empty-lines ()
  20408. "Move backwards over whitespace, to the beginning of the first empty line.
  20409. Returns the number of empty lines passed."
  20410. (let ((pos (point)))
  20411. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20412. (skip-chars-backward " \t\n\r")
  20413. (unless (eobp)
  20414. (forward-line -1)))
  20415. (beginning-of-line 2)
  20416. (goto-char (min (point) pos))
  20417. (count-lines (point) pos)))
  20418. (defun org-skip-whitespace ()
  20419. (skip-chars-forward " \t\n\r"))
  20420. (defun org-point-in-group (point group &optional context)
  20421. "Check if POINT is in match-group GROUP.
  20422. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20423. match. If the match group does not exist or point is not inside it,
  20424. return nil."
  20425. (and (match-beginning group)
  20426. (>= point (match-beginning group))
  20427. (<= point (match-end group))
  20428. (if context
  20429. (list context (match-beginning group) (match-end group))
  20430. t)))
  20431. (defun org-switch-to-buffer-other-window (&rest args)
  20432. "Switch to buffer in a second window on the current frame.
  20433. In particular, do not allow pop-up frames.
  20434. Returns the newly created buffer."
  20435. (org-no-popups
  20436. (apply 'switch-to-buffer-other-window args)))
  20437. (defun org-combine-plists (&rest plists)
  20438. "Create a single property list from all plists in PLISTS.
  20439. The process starts by copying the first list, and then setting properties
  20440. from the other lists. Settings in the last list are the most significant
  20441. ones and overrule settings in the other lists."
  20442. (let ((rtn (copy-sequence (pop plists)))
  20443. p v ls)
  20444. (while plists
  20445. (setq ls (pop plists))
  20446. (while ls
  20447. (setq p (pop ls) v (pop ls))
  20448. (setq rtn (plist-put rtn p v))))
  20449. rtn))
  20450. (defun org-replace-escapes (string table)
  20451. "Replace %-escapes in STRING with values in TABLE.
  20452. TABLE is an association list with keys like \"%a\" and string values.
  20453. The sequences in STRING may contain normal field width and padding information,
  20454. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20455. so values can contain further %-escapes if they are define later in TABLE."
  20456. (let ((tbl (copy-alist table))
  20457. (case-fold-search nil)
  20458. (pchg 0)
  20459. e re rpl)
  20460. (while (setq e (pop tbl))
  20461. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20462. (when (and (cdr e) (string-match re (cdr e)))
  20463. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20464. (safe "SREF"))
  20465. (add-text-properties 0 3 (list 'sref sref) safe)
  20466. (setcdr e (replace-match safe t t (cdr e)))))
  20467. (while (string-match re string)
  20468. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20469. (cdr e)))
  20470. (setq string (replace-match rpl t t string))))
  20471. (while (setq pchg (next-property-change pchg string))
  20472. (let ((sref (get-text-property pchg 'sref string)))
  20473. (when (and sref (string-match "SREF" string pchg))
  20474. (setq string (replace-match sref t t string)))))
  20475. string))
  20476. (defun org-sublist (list start end)
  20477. "Return a section of LIST, from START to END.
  20478. Counting starts at 1."
  20479. (let (rtn (c start))
  20480. (setq list (nthcdr (1- start) list))
  20481. (while (and list (<= c end))
  20482. (push (pop list) rtn)
  20483. (setq c (1+ c)))
  20484. (nreverse rtn)))
  20485. (defun org-find-base-buffer-visiting (file)
  20486. "Like `find-buffer-visiting' but always return the base buffer and
  20487. not an indirect buffer."
  20488. (let ((buf (or (get-file-buffer file)
  20489. (find-buffer-visiting file))))
  20490. (if buf
  20491. (or (buffer-base-buffer buf) buf)
  20492. nil)))
  20493. (defun org-image-file-name-regexp (&optional extensions)
  20494. "Return regexp matching the file names of images.
  20495. If EXTENSIONS is given, only match these."
  20496. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20497. (image-file-name-regexp)
  20498. (let ((image-file-name-extensions
  20499. (or extensions
  20500. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20501. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20502. (concat "\\."
  20503. (regexp-opt (nconc (mapcar 'upcase
  20504. image-file-name-extensions)
  20505. image-file-name-extensions)
  20506. t)
  20507. "\\'"))))
  20508. (defun org-file-image-p (file &optional extensions)
  20509. "Return non-nil if FILE is an image."
  20510. (save-match-data
  20511. (string-match (org-image-file-name-regexp extensions) file)))
  20512. (defun org-get-cursor-date (&optional with-time)
  20513. "Return the date at cursor in as a time.
  20514. This works in the calendar and in the agenda, anywhere else it just
  20515. returns the current time.
  20516. If WITH-TIME is non-nil, returns the time of the event at point (in
  20517. the agenda) or the current time of the day."
  20518. (let (date day defd tp tm hod mod)
  20519. (when with-time
  20520. (setq tp (get-text-property (point) 'time))
  20521. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20522. (setq hod (string-to-number (match-string 1 tp))
  20523. mod (string-to-number (match-string 2 tp))))
  20524. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20525. mod (nth 1 (decode-time (current-time))))))
  20526. (cond
  20527. ((eq major-mode 'calendar-mode)
  20528. (setq date (calendar-cursor-to-date)
  20529. defd (encode-time 0 (or mod 0) (or hod 0)
  20530. (nth 1 date) (nth 0 date) (nth 2 date))))
  20531. ((eq major-mode 'org-agenda-mode)
  20532. (setq day (get-text-property (point) 'day))
  20533. (if day
  20534. (setq date (calendar-gregorian-from-absolute day)
  20535. defd (encode-time 0 (or mod 0) (or hod 0)
  20536. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20537. (or defd (current-time))))
  20538. (defun org-mark-subtree (&optional up)
  20539. "Mark the current subtree.
  20540. This puts point at the start of the current subtree, and mark at
  20541. the end. If a numeric prefix UP is given, move up into the
  20542. hierarchy of headlines by UP levels before marking the subtree."
  20543. (interactive "P")
  20544. (org-with-limited-levels
  20545. (cond ((org-at-heading-p) (beginning-of-line))
  20546. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20547. (t (outline-previous-visible-heading 1))))
  20548. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20549. (if (org-called-interactively-p 'any)
  20550. (call-interactively 'org-mark-element)
  20551. (org-mark-element)))
  20552. ;;; Indentation
  20553. (defun org--get-expected-indentation (element contentsp)
  20554. "Expected indentation column for current line, according to ELEMENT.
  20555. ELEMENT is an element containing point. CONTENTSP is non-nil
  20556. when indentation is to be computed according to contents of
  20557. ELEMENT."
  20558. (let ((type (org-element-type element))
  20559. (start (org-element-property :begin element)))
  20560. (org-with-wide-buffer
  20561. (cond
  20562. (contentsp
  20563. (case type
  20564. (footnote-definition 0)
  20565. ((headline inlinetask nil)
  20566. (if (not org-adapt-indentation) 0
  20567. (let ((level (org-current-level)))
  20568. (if level (1+ level) 0))))
  20569. ((item plain list)
  20570. (org-list-item-body-column
  20571. (or (org-element-property :post-affiliated element) start)))
  20572. (otherwise
  20573. (goto-char start)
  20574. (org-get-indentation))))
  20575. ((memq type '(headline inlinetask nil))
  20576. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  20577. (org--get-expected-indentation element t)
  20578. 0))
  20579. ((eq type 'footnote-definition) 0)
  20580. ;; First paragraph of a footnote definition or an item.
  20581. ;; Indent like parent.
  20582. ((< (line-beginning-position) start)
  20583. (org--get-expected-indentation
  20584. (org-element-property :parent element) t))
  20585. ;; At first line: indent according to previous sibling, if any,
  20586. ;; ignoring footnote definitions and inline tasks, or parent's
  20587. ;; contents.
  20588. ((= (line-beginning-position) start)
  20589. (catch 'exit
  20590. (while t
  20591. (if (= (point-min) start) (throw 'exit 0)
  20592. (goto-char (1- start))
  20593. (let ((previous (org-element-at-point)))
  20594. (while (let ((parent (org-element-property :parent previous)))
  20595. (and parent
  20596. (setq previous parent)
  20597. (<= (org-element-property :end parent) start))))
  20598. (cond ((or (not previous)
  20599. (> (org-element-property :end previous) start))
  20600. (throw 'exit (org--get-expected-indentation previous t)))
  20601. ((memq (org-element-type previous)
  20602. '(footnote-definition inlinetask))
  20603. (setq start (org-element-property :begin previous)))
  20604. (t (goto-char (org-element-property :begin previous))
  20605. (throw 'exit (org-get-indentation)))))))))
  20606. ;; Otherwise, move to the first non-blank line above.
  20607. (t
  20608. (beginning-of-line)
  20609. (let ((pos (point)))
  20610. (skip-chars-backward " \r\t\n")
  20611. (cond
  20612. ;; Two blank lines end a footnote definition or a plain
  20613. ;; list. When we indent an empty line after them, the
  20614. ;; containing list or footnote definition is over, so it
  20615. ;; qualifies as a previous sibling. Therefore, we indent
  20616. ;; like its first line.
  20617. ((and (memq type '(footnote-definition plain-list))
  20618. (> (count-lines (point) pos) 2))
  20619. (goto-char start)
  20620. (org-get-indentation))
  20621. ;; Line above is the first one of a paragraph at the
  20622. ;; beginning of an item or a footnote definition. Indent
  20623. ;; like parent.
  20624. ((< (line-beginning-position) start)
  20625. (org--get-expected-indentation
  20626. (org-element-property :parent element) t))
  20627. ;; POS is after contents in a greater element. Indent like
  20628. ;; the beginning of the element.
  20629. ;;
  20630. ;; As a special case, if point is at the end of a footnote
  20631. ;; definition or an item, indent like the very last element
  20632. ;; within.
  20633. ((let ((cend (org-element-property :contents-end element)))
  20634. (and cend (<= cend pos)))
  20635. (if (memq type '(footnote-definition item plain-list))
  20636. (org--get-expected-indentation (org-element-at-point) nil)
  20637. (goto-char start)
  20638. (org-get-indentation)))
  20639. ;; In any other case, indent like the current line.
  20640. (t (org-get-indentation)))))))))
  20641. (defun org--align-node-property ()
  20642. "Align node property at point.
  20643. Alignment is done according to `org-property-format', which see."
  20644. (when (save-excursion
  20645. (beginning-of-line)
  20646. (looking-at org-property-re))
  20647. (replace-match
  20648. (concat (match-string 4)
  20649. (format org-property-format (match-string 1) (match-string 3)))
  20650. t t)))
  20651. (defun org-indent-line ()
  20652. "Indent line depending on context.
  20653. Indentation is done according to the following rules:
  20654. - Footnote definitions, headlines and inline tasks have to
  20655. start at column 0.
  20656. - On the very first line of an element, consider, in order, the
  20657. next rules until one matches:
  20658. 1. If there's a sibling element before, ignoring footnote
  20659. definitions and inline tasks, indent like its first line.
  20660. 2. If element has a parent, indent like its contents. More
  20661. precisely, if parent is an item, indent after the
  20662. description part, if any, or the bullet (see
  20663. ``org-list-description-max-indent'). Else, indent like
  20664. parent's first line.
  20665. 3. Otherwise, indent relatively to current level, if
  20666. `org-adapt-indentation' is non-nil, or to left margin.
  20667. - On a blank line at the end of a plain list, an item, or
  20668. a footnote definition, indent like the very last element
  20669. within.
  20670. - In the code part of a source block, use language major mode
  20671. to indent current line if `org-src-tab-acts-natively' is
  20672. non-nil. If it is nil, do nothing.
  20673. - Otherwise, indent like the first non-blank line above.
  20674. The function doesn't indent an item as it could break the whole
  20675. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  20676. \\[org-shiftmetaright].
  20677. Also align node properties according to `org-property-format'."
  20678. (interactive)
  20679. (cond
  20680. (orgstruct-is-++
  20681. (let ((indent-line-function
  20682. (cadadr (assq 'indent-line-function org-fb-vars))))
  20683. (indent-according-to-mode)))
  20684. ((org-at-heading-p) 'noindent)
  20685. (t
  20686. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  20687. (type (org-element-type element)))
  20688. (cond ((and (memq type '(plain-list item))
  20689. (= (line-beginning-position)
  20690. (or (org-element-property :post-affiliated element)
  20691. (org-element-property :begin element))))
  20692. 'noindent)
  20693. ((and (eq type 'src-block)
  20694. org-src-tab-acts-natively
  20695. (> (line-beginning-position)
  20696. (org-element-property :post-affiliated element))
  20697. (< (line-beginning-position)
  20698. (org-with-wide-buffer
  20699. (goto-char (org-element-property :end element))
  20700. (skip-chars-backward " \r\t\n")
  20701. (line-beginning-position))))
  20702. (let ((org-src-strip-leading-and-trailing-blank-lines nil))
  20703. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB"))))
  20704. (t
  20705. (let ((column (org--get-expected-indentation element nil)))
  20706. ;; Preserve current column.
  20707. (if (<= (current-column) (current-indentation))
  20708. (org-indent-line-to column)
  20709. (save-excursion (org-indent-line-to column))))
  20710. ;; Align node property. Also preserve current column.
  20711. (when (eq type 'node-property)
  20712. (let ((column (current-column)))
  20713. (org--align-node-property)
  20714. (org-move-to-column column)))))))))
  20715. (defun org-indent-region (start end)
  20716. "Indent each non-blank line in the region.
  20717. Called from a program, START and END specify the region to
  20718. indent. The function will not indent contents of example blocks,
  20719. verse blocks and export blocks as leading white spaces are
  20720. assumed to be significant there."
  20721. (interactive "r")
  20722. (save-excursion
  20723. (goto-char start)
  20724. (beginning-of-line)
  20725. (let ((indent-to
  20726. (lambda (ind pos)
  20727. ;; Set IND as indentation for all lines between point and
  20728. ;; POS or END, whichever comes first. Blank lines are
  20729. ;; ignored. Leave point after POS once done.
  20730. (let ((limit (copy-marker (min end pos))))
  20731. (while (< (point) limit)
  20732. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  20733. (forward-line))
  20734. (set-marker limit nil))))
  20735. (end (copy-marker end)))
  20736. (while (< (point) end)
  20737. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  20738. (let* ((element (org-element-at-point))
  20739. (type (org-element-type element))
  20740. (element-end (copy-marker (org-element-property :end element)))
  20741. (ind (org--get-expected-indentation element nil)))
  20742. (if (or (memq type '(paragraph table table-row))
  20743. (not (or (org-element-property :contents-begin element)
  20744. (memq type
  20745. '(example-block export-block src-block)))))
  20746. ;; Elements here are indented as a single block. Also
  20747. ;; align node properties.
  20748. (progn
  20749. (when (eq type 'node-property)
  20750. (org--align-node-property)
  20751. (beginning-of-line))
  20752. (funcall indent-to ind element-end))
  20753. ;; Elements in this category consist of three parts:
  20754. ;; before the contents, the contents, and after the
  20755. ;; contents. The contents are treated specially,
  20756. ;; according to the element type, or not indented at
  20757. ;; all. Other parts are indented as a single block.
  20758. (let* ((post (copy-marker
  20759. (or (org-element-property :post-affiliated element)
  20760. (org-element-property :begin element))))
  20761. (cbeg
  20762. (copy-marker
  20763. (cond
  20764. ((not (org-element-property :contents-begin element))
  20765. ;; Fake contents for source blocks.
  20766. (org-with-wide-buffer
  20767. (goto-char post)
  20768. (forward-line)
  20769. (point)))
  20770. ((memq type '(footnote-definition item plain-list))
  20771. ;; Contents in these elements could start on
  20772. ;; the same line as the beginning of the
  20773. ;; element. Make sure we start indenting
  20774. ;; from the second line.
  20775. (org-with-wide-buffer
  20776. (goto-char post)
  20777. (forward-line)
  20778. (point)))
  20779. (t (org-element-property :contents-begin element)))))
  20780. (cend (copy-marker
  20781. (or (org-element-property :contents-end element)
  20782. ;; Fake contents for source blocks.
  20783. (org-with-wide-buffer
  20784. (goto-char element-end)
  20785. (skip-chars-backward " \r\t\n")
  20786. (line-beginning-position))))))
  20787. (funcall indent-to ind cbeg)
  20788. (when (< (point) end)
  20789. (case type
  20790. ((example-block export-block verse-block))
  20791. (src-block
  20792. ;; In a source block, indent source code according
  20793. ;; to language major mode, but only if
  20794. ;; `org-src-tab-acts-natively' is non-nil.
  20795. (when (and (< (point) end) org-src-tab-acts-natively)
  20796. (ignore-errors
  20797. (let (org-src-strip-leading-and-trailing-blank-lines
  20798. ;; Region boundaries in edit buffer.
  20799. (start (1+ (- (point) cbeg)))
  20800. (end (- (min cend end) cbeg)))
  20801. (org-babel-do-in-edit-buffer
  20802. (indent-region start end))))))
  20803. (t (org-indent-region (point) (min cend end))))
  20804. (goto-char (min cend end))
  20805. (when (< (point) end) (funcall indent-to ind element-end)))
  20806. (set-marker post nil)
  20807. (set-marker cbeg nil)
  20808. (set-marker cend nil)))
  20809. (set-marker element-end nil))))
  20810. (set-marker end nil))))
  20811. (defun org-indent-drawer ()
  20812. "Indent the drawer at point."
  20813. (interactive)
  20814. (unless (save-excursion
  20815. (beginning-of-line)
  20816. (org-looking-at-p org-drawer-regexp))
  20817. (user-error "Not at a drawer"))
  20818. (let ((element (org-element-at-point)))
  20819. (unless (memq (org-element-type element) '(drawer property-drawer))
  20820. (user-error "Not at a drawer"))
  20821. (org-with-wide-buffer
  20822. (org-indent-region (org-element-property :begin element)
  20823. (org-element-property :end element))))
  20824. (message "Drawer at point indented"))
  20825. (defun org-indent-block ()
  20826. "Indent the block at point."
  20827. (interactive)
  20828. (unless (save-excursion
  20829. (beginning-of-line)
  20830. (let ((case-fold-search t))
  20831. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  20832. (user-error "Not at a block"))
  20833. (let ((element (org-element-at-point)))
  20834. (unless (memq (org-element-type element)
  20835. '(comment-block center-block example-block export-block
  20836. quote-block special-block src-block
  20837. verse-block))
  20838. (user-error "Not at a block"))
  20839. (org-with-wide-buffer
  20840. (org-indent-region (org-element-property :begin element)
  20841. (org-element-property :end element))))
  20842. (message "Block at point indented"))
  20843. ;;; Filling
  20844. ;; We use our own fill-paragraph and auto-fill functions.
  20845. ;; `org-fill-paragraph' relies on adaptive filling and context
  20846. ;; checking. Appropriate `fill-prefix' is computed with
  20847. ;; `org-adaptive-fill-function'.
  20848. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20849. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20850. ;; `org-adaptive-fill-function' value. Internally,
  20851. ;; `org-comment-line-break-function' breaks the line.
  20852. ;; `org-setup-filling' installs filling and auto-filling related
  20853. ;; variables during `org-mode' initialization.
  20854. (defvar org-element-paragraph-separate) ; org-element.el
  20855. (defun org-setup-filling ()
  20856. (require 'org-element)
  20857. ;; Prevent auto-fill from inserting unwanted new items.
  20858. (when (boundp 'fill-nobreak-predicate)
  20859. (org-set-local
  20860. 'fill-nobreak-predicate
  20861. (org-uniquify
  20862. (append fill-nobreak-predicate
  20863. '(org-fill-line-break-nobreak-p
  20864. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20865. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  20866. (org-set-local 'paragraph-start paragraph-ending)
  20867. (org-set-local 'paragraph-separate paragraph-ending))
  20868. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20869. (org-set-local 'auto-fill-inhibit-regexp nil)
  20870. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20871. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20872. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20873. (defun org-fill-line-break-nobreak-p ()
  20874. "Non-nil when a new line at point would create an Org line break."
  20875. (save-excursion
  20876. (skip-chars-backward "[ \t]")
  20877. (skip-chars-backward "\\\\")
  20878. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20879. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20880. "Non-nil when a new line at point would split a timestamp."
  20881. (and (org-at-timestamp-p t)
  20882. (not (looking-at org-ts-regexp-both))))
  20883. (declare-function message-in-body-p "message" ())
  20884. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20885. (defun org-adaptive-fill-function ()
  20886. "Compute a fill prefix for the current line.
  20887. Return fill prefix, as a string, or nil if current line isn't
  20888. meant to be filled. For convenience, if `adaptive-fill-regexp'
  20889. matches in paragraphs or comments, use it."
  20890. (catch 'exit
  20891. (when (derived-mode-p 'message-mode)
  20892. (save-excursion
  20893. (beginning-of-line)
  20894. (cond ((or (not (message-in-body-p))
  20895. (looking-at orgtbl-line-start-regexp))
  20896. (throw 'exit nil))
  20897. ((looking-at message-cite-prefix-regexp)
  20898. (throw 'exit (match-string-no-properties 0)))
  20899. ((looking-at org-outline-regexp)
  20900. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  20901. (org-with-wide-buffer
  20902. (unless (org-at-heading-p)
  20903. (let* ((p (line-beginning-position))
  20904. (element (save-excursion
  20905. (beginning-of-line)
  20906. (org-element-at-point)))
  20907. (type (org-element-type element))
  20908. (post-affiliated (org-element-property :post-affiliated element)))
  20909. (unless (and post-affiliated (< p post-affiliated))
  20910. (case type
  20911. (comment
  20912. (save-excursion
  20913. (beginning-of-line)
  20914. (looking-at "[ \t]*")
  20915. (concat (match-string 0) "# ")))
  20916. (footnote-definition "")
  20917. ((item plain-list)
  20918. (make-string (org-list-item-body-column
  20919. (or post-affiliated
  20920. (org-element-property :begin element)))
  20921. ?\s))
  20922. (paragraph
  20923. ;; Fill prefix is usually the same as the current line,
  20924. ;; unless the paragraph is at the beginning of an item.
  20925. (let ((parent (org-element-property :parent element)))
  20926. (save-excursion
  20927. (beginning-of-line)
  20928. (cond ((eq (org-element-type parent) 'item)
  20929. (make-string (org-list-item-body-column
  20930. (org-element-property :begin parent))
  20931. ?\s))
  20932. ((and adaptive-fill-regexp
  20933. ;; Locally disable
  20934. ;; `adaptive-fill-function' to let
  20935. ;; `fill-context-prefix' handle
  20936. ;; `adaptive-fill-regexp' variable.
  20937. (let (adaptive-fill-function)
  20938. (fill-context-prefix
  20939. post-affiliated
  20940. (org-element-property :end element)))))
  20941. ((looking-at "[ \t]+") (match-string 0))
  20942. (t "")))))
  20943. (comment-block
  20944. ;; Only fill contents if P is within block boundaries.
  20945. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20946. (forward-line)
  20947. (point)))
  20948. (cend (save-excursion
  20949. (goto-char (org-element-property :end element))
  20950. (skip-chars-backward " \r\t\n")
  20951. (line-beginning-position))))
  20952. (when (and (>= p cbeg) (< p cend))
  20953. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20954. (match-string 0)
  20955. "")))))))))))
  20956. (declare-function message-goto-body "message" ())
  20957. (defvar message-cite-prefix-regexp) ; From message.el
  20958. (defun org-fill-paragraph (&optional justify)
  20959. "Fill element at point, when applicable.
  20960. This function only applies to comment blocks, comments, example
  20961. blocks and paragraphs. Also, as a special case, re-align table
  20962. when point is at one.
  20963. If JUSTIFY is non-nil (interactively, with prefix argument),
  20964. justify as well. If `sentence-end-double-space' is non-nil, then
  20965. period followed by one space does not end a sentence, so don't
  20966. break a line there. The variable `fill-column' controls the
  20967. width for filling.
  20968. For convenience, when point is at a plain list, an item or
  20969. a footnote definition, try to fill the first paragraph within."
  20970. (interactive)
  20971. (if (and (derived-mode-p 'message-mode)
  20972. (or (not (message-in-body-p))
  20973. (save-excursion (move-beginning-of-line 1)
  20974. (looking-at message-cite-prefix-regexp))))
  20975. ;; First ensure filling is correct in message-mode.
  20976. (let ((fill-paragraph-function
  20977. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20978. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20979. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20980. (paragraph-separate
  20981. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20982. (fill-paragraph nil))
  20983. (with-syntax-table org-mode-transpose-word-syntax-table
  20984. ;; Move to end of line in order to get the first paragraph
  20985. ;; within a plain list or a footnote definition.
  20986. (let ((element (save-excursion
  20987. (end-of-line)
  20988. (or (ignore-errors (org-element-at-point))
  20989. (user-error "An element cannot be parsed line %d"
  20990. (line-number-at-pos (point)))))))
  20991. ;; First check if point is in a blank line at the beginning of
  20992. ;; the buffer. In that case, ignore filling.
  20993. (case (org-element-type element)
  20994. ;; Use major mode filling function is src blocks.
  20995. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20996. ;; Align Org tables, leave table.el tables as-is.
  20997. (table-row (org-table-align) t)
  20998. (table
  20999. (when (eq (org-element-property :type element) 'org)
  21000. (save-excursion
  21001. (goto-char (org-element-property :post-affiliated element))
  21002. (org-table-align)))
  21003. t)
  21004. (paragraph
  21005. ;; Paragraphs may contain `line-break' type objects.
  21006. (let ((beg (max (point-min)
  21007. (org-element-property :contents-begin element)))
  21008. (end (min (point-max)
  21009. (org-element-property :contents-end element))))
  21010. ;; Do nothing if point is at an affiliated keyword.
  21011. (if (< (line-end-position) beg) t
  21012. (when (derived-mode-p 'message-mode)
  21013. ;; In `message-mode', do not fill following citation
  21014. ;; in current paragraph nor text before message body.
  21015. (let ((body-start (save-excursion (message-goto-body))))
  21016. (when body-start (setq beg (max body-start beg))))
  21017. (when (save-excursion
  21018. (re-search-forward
  21019. (concat "^" message-cite-prefix-regexp) end t))
  21020. (setq end (match-beginning 0))))
  21021. ;; Fill paragraph, taking line breaks into account.
  21022. (save-excursion
  21023. (goto-char beg)
  21024. (let ((cuts (list beg)))
  21025. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21026. (when (eq 'line-break
  21027. (org-element-type
  21028. (save-excursion (backward-char)
  21029. (org-element-context))))
  21030. (push (point) cuts)))
  21031. (dolist (c (delq end cuts))
  21032. (fill-region-as-paragraph c end justify)
  21033. (setq end c))))
  21034. t)))
  21035. ;; Contents of `comment-block' type elements should be
  21036. ;; filled as plain text, but only if point is within block
  21037. ;; markers.
  21038. (comment-block
  21039. (let* ((case-fold-search t)
  21040. (beg (save-excursion
  21041. (goto-char (org-element-property :begin element))
  21042. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21043. (forward-line)
  21044. (point)))
  21045. (end (save-excursion
  21046. (goto-char (org-element-property :end element))
  21047. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21048. (line-beginning-position))))
  21049. (if (or (< (point) beg) (> (point) end)) t
  21050. (fill-region-as-paragraph
  21051. (save-excursion (end-of-line)
  21052. (re-search-backward "^[ \t]*$" beg 'move)
  21053. (line-beginning-position))
  21054. (save-excursion (beginning-of-line)
  21055. (re-search-forward "^[ \t]*$" end 'move)
  21056. (line-beginning-position))
  21057. justify))))
  21058. ;; Fill comments.
  21059. (comment
  21060. (let ((begin (org-element-property :post-affiliated element))
  21061. (end (org-element-property :end element)))
  21062. (when (and (>= (point) begin) (<= (point) end))
  21063. (let ((begin (save-excursion
  21064. (end-of-line)
  21065. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21066. (progn (forward-line) (point))
  21067. begin)))
  21068. (end (save-excursion
  21069. (end-of-line)
  21070. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21071. (1- (line-beginning-position))
  21072. (skip-chars-backward " \r\t\n")
  21073. (line-end-position)))))
  21074. ;; Do not fill comments when at a blank line.
  21075. (when (> end begin)
  21076. (let ((fill-prefix
  21077. (save-excursion
  21078. (beginning-of-line)
  21079. (looking-at "[ \t]*#")
  21080. (let ((comment-prefix (match-string 0)))
  21081. (goto-char (match-end 0))
  21082. (if (looking-at adaptive-fill-regexp)
  21083. (concat comment-prefix (match-string 0))
  21084. (concat comment-prefix " "))))))
  21085. (save-excursion
  21086. (fill-region-as-paragraph begin end justify))))))
  21087. t))
  21088. ;; Ignore every other element.
  21089. (otherwise t))))))
  21090. (defun org-auto-fill-function ()
  21091. "Auto-fill function."
  21092. ;; Check if auto-filling is meaningful.
  21093. (let ((fc (current-fill-column)))
  21094. (when (and fc (> (current-column) fc))
  21095. (let* ((fill-prefix (org-adaptive-fill-function))
  21096. ;; Enforce empty fill prefix, if required. Otherwise, it
  21097. ;; will be computed again.
  21098. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21099. (when fill-prefix (do-auto-fill))))))
  21100. (defun org-comment-line-break-function (&optional soft)
  21101. "Break line at point and indent, continuing comment if within one.
  21102. The inserted newline is marked hard if variable
  21103. `use-hard-newlines' is true, unless optional argument SOFT is
  21104. non-nil."
  21105. (if soft (insert-and-inherit ?\n) (newline 1))
  21106. (save-excursion (forward-char -1) (delete-horizontal-space))
  21107. (delete-horizontal-space)
  21108. (indent-to-left-margin)
  21109. (insert-before-markers-and-inherit fill-prefix))
  21110. ;;; Fixed Width Areas
  21111. (defun org-toggle-fixed-width ()
  21112. "Toggle fixed-width markup.
  21113. Add or remove fixed-width markup on current line, whenever it
  21114. makes sense. Return an error otherwise.
  21115. If a region is active and if it contains only fixed-width areas
  21116. or blank lines, remove all fixed-width markup in it. If the
  21117. region contains anything else, convert all non-fixed-width lines
  21118. to fixed-width ones.
  21119. Blank lines at the end of the region are ignored unless the
  21120. region only contains such lines."
  21121. (interactive)
  21122. (if (not (org-region-active-p))
  21123. ;; No region:
  21124. ;;
  21125. ;; Remove fixed width marker only in a fixed-with element.
  21126. ;;
  21127. ;; Add fixed width maker in paragraphs, in blank lines after
  21128. ;; elements or at the beginning of a headline or an inlinetask,
  21129. ;; and before any one-line elements (e.g., a clock).
  21130. (progn
  21131. (beginning-of-line)
  21132. (let* ((element (org-element-at-point))
  21133. (type (org-element-type element)))
  21134. (cond
  21135. ((and (eq type 'fixed-width)
  21136. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21137. (replace-match
  21138. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21139. ((and (memq type '(babel-call clock comment diary-sexp headline
  21140. horizontal-rule keyword paragraph
  21141. planning))
  21142. (or (not (org-element-property :post-affiliated element))
  21143. (<= (org-element-property :post-affiliated element)
  21144. (point))))
  21145. (skip-chars-forward " \t")
  21146. (insert ": "))
  21147. ((and (org-looking-at-p "[ \t]*$")
  21148. (or (eq type 'inlinetask)
  21149. (save-excursion
  21150. (skip-chars-forward " \r\t\n")
  21151. (<= (org-element-property :end element) (point)))))
  21152. (delete-region (point) (line-end-position))
  21153. (org-indent-line)
  21154. (insert ": "))
  21155. (t (user-error "Cannot insert a fixed-width line here")))))
  21156. ;; Region active.
  21157. (let* ((begin (save-excursion
  21158. (goto-char (region-beginning))
  21159. (line-beginning-position)))
  21160. (end (copy-marker
  21161. (save-excursion
  21162. (goto-char (region-end))
  21163. (unless (eolp) (beginning-of-line))
  21164. (if (save-excursion (re-search-backward "\\S-" begin t))
  21165. (progn (skip-chars-backward " \r\t\n") (point))
  21166. (point)))))
  21167. (all-fixed-width-p
  21168. (catch 'not-all-p
  21169. (save-excursion
  21170. (goto-char begin)
  21171. (skip-chars-forward " \r\t\n")
  21172. (when (eobp) (throw 'not-all-p nil))
  21173. (while (< (point) end)
  21174. (let ((element (org-element-at-point)))
  21175. (if (eq (org-element-type element) 'fixed-width)
  21176. (goto-char (org-element-property :end element))
  21177. (throw 'not-all-p nil))))
  21178. t))))
  21179. (if all-fixed-width-p
  21180. (save-excursion
  21181. (goto-char begin)
  21182. (while (< (point) end)
  21183. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21184. (replace-match
  21185. "" nil nil nil
  21186. (if (= (line-end-position) (match-end 0)) 0 1)))
  21187. (forward-line)))
  21188. (let ((min-ind (point-max)))
  21189. ;; Find minimum indentation across all lines.
  21190. (save-excursion
  21191. (goto-char begin)
  21192. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21193. (setq min-ind 0)
  21194. (catch 'zerop
  21195. (while (< (point) end)
  21196. (unless (org-looking-at-p "[ \t]*$")
  21197. (let ((ind (org-get-indentation)))
  21198. (setq min-ind (min min-ind ind))
  21199. (when (zerop ind) (throw 'zerop t))))
  21200. (forward-line)))))
  21201. ;; Loop over all lines and add fixed-width markup everywhere
  21202. ;; but in fixed-width lines.
  21203. (save-excursion
  21204. (goto-char begin)
  21205. (while (< (point) end)
  21206. (cond
  21207. ((org-at-heading-p)
  21208. (insert ": ")
  21209. (forward-line)
  21210. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21211. (insert ":")
  21212. (forward-line)))
  21213. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21214. (let* ((element (org-element-at-point))
  21215. (element-end (org-element-property :end element)))
  21216. (if (eq (org-element-type element) 'fixed-width)
  21217. (progn (goto-char element-end)
  21218. (skip-chars-backward " \r\t\n")
  21219. (forward-line))
  21220. (let ((limit (min end element-end)))
  21221. (while (< (point) limit)
  21222. (org-move-to-column min-ind t)
  21223. (insert ": ")
  21224. (forward-line))))))
  21225. (t
  21226. (org-move-to-column min-ind t)
  21227. (insert ": ")
  21228. (forward-line)))))))
  21229. (set-marker end nil))))
  21230. ;;; Comments
  21231. ;; Org comments syntax is quite complex. It requires the entire line
  21232. ;; to be just a comment. Also, even with the right syntax at the
  21233. ;; beginning of line, some some elements (i.e. verse-block or
  21234. ;; example-block) don't accept comments. Usual Emacs comment commands
  21235. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21236. ;; Org still relies on `comment-dwim', but cannot trust
  21237. ;; `comment-only-p'. So, `comment-region-function' and
  21238. ;; `uncomment-region-function' both point
  21239. ;; to`org-comment-or-uncomment-region'. Eventually,
  21240. ;; `org-insert-comment' takes care of insertion of comments at the
  21241. ;; beginning of line.
  21242. ;; `org-setup-comments-handling' install comments related variables
  21243. ;; during `org-mode' initialization.
  21244. (defun org-setup-comments-handling ()
  21245. (interactive)
  21246. (org-set-local 'comment-use-syntax nil)
  21247. (org-set-local 'comment-start "# ")
  21248. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21249. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  21250. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  21251. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  21252. (defun org-insert-comment ()
  21253. "Insert an empty comment above current line.
  21254. If the line is empty, insert comment at its beginning. When
  21255. point is within a source block, comment according to the related
  21256. major mode."
  21257. (if (let ((element (org-element-at-point)))
  21258. (and (eq (org-element-type element) 'src-block)
  21259. (< (save-excursion
  21260. (goto-char (org-element-property :post-affiliated element))
  21261. (line-end-position))
  21262. (point))
  21263. (> (save-excursion
  21264. (goto-char (org-element-property :end element))
  21265. (skip-chars-backward " \r\t\n")
  21266. (line-beginning-position))
  21267. (point))))
  21268. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21269. (beginning-of-line)
  21270. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21271. (open-line 1))
  21272. (org-indent-line)
  21273. (insert "# ")))
  21274. (defvar comment-empty-lines) ; From newcomment.el.
  21275. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  21276. "Comment or uncomment each non-blank line in the region.
  21277. Uncomment each non-blank line between BEG and END if it only
  21278. contains commented lines. Otherwise, comment them. If region is
  21279. strictly within a source block, use appropriate comment syntax."
  21280. (if (let ((element (org-element-at-point)))
  21281. (and (eq (org-element-type element) 'src-block)
  21282. (< (save-excursion
  21283. (goto-char (org-element-property :post-affiliated element))
  21284. (line-end-position))
  21285. beg)
  21286. (>= (save-excursion
  21287. (goto-char (org-element-property :end element))
  21288. (skip-chars-backward " \r\t\n")
  21289. (line-beginning-position))
  21290. end)))
  21291. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21292. (save-restriction
  21293. ;; Restrict region
  21294. (narrow-to-region (save-excursion (goto-char beg)
  21295. (skip-chars-forward " \r\t\n" end)
  21296. (line-beginning-position))
  21297. (save-excursion (goto-char end)
  21298. (skip-chars-backward " \r\t\n" beg)
  21299. (line-end-position)))
  21300. (let ((uncommentp
  21301. ;; UNCOMMENTP is non-nil when every non blank line between
  21302. ;; BEG and END is a comment.
  21303. (save-excursion
  21304. (goto-char (point-min))
  21305. (while (and (not (eobp))
  21306. (let ((element (org-element-at-point)))
  21307. (and (eq (org-element-type element) 'comment)
  21308. (goto-char (min (point-max)
  21309. (org-element-property
  21310. :end element)))))))
  21311. (eobp))))
  21312. (if uncommentp
  21313. ;; Only blank lines and comments in region: uncomment it.
  21314. (save-excursion
  21315. (goto-char (point-min))
  21316. (while (not (eobp))
  21317. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21318. (replace-match "" nil nil nil 1))
  21319. (forward-line)))
  21320. ;; Comment each line in region.
  21321. (let ((min-indent (point-max)))
  21322. ;; First find the minimum indentation across all lines.
  21323. (save-excursion
  21324. (goto-char (point-min))
  21325. (while (and (not (eobp)) (not (zerop min-indent)))
  21326. (unless (looking-at "[ \t]*$")
  21327. (setq min-indent (min min-indent (current-indentation))))
  21328. (forward-line)))
  21329. ;; Then loop over all lines.
  21330. (save-excursion
  21331. (goto-char (point-min))
  21332. (while (not (eobp))
  21333. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21334. ;; Don't get fooled by invisible text (e.g. link path)
  21335. ;; when moving to column MIN-INDENT.
  21336. (let ((buffer-invisibility-spec nil))
  21337. (org-move-to-column min-indent t))
  21338. (insert comment-start))
  21339. (forward-line)))))))))
  21340. (defun org-comment-dwim (arg)
  21341. "Call `comment-dwim' within a source edit buffer if needed."
  21342. (interactive "*P")
  21343. (if (org-in-src-block-p)
  21344. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21345. (call-interactively 'comment-dwim)))
  21346. ;;; Planning
  21347. ;; This section contains tools to operate on timestamp objects, as
  21348. ;; returned by, e.g. `org-element-context'.
  21349. (defun org-timestamp-has-time-p (timestamp)
  21350. "Non-nil when TIMESTAMP has a time specified."
  21351. (org-element-property :hour-start timestamp))
  21352. (defun org-timestamp-format (timestamp format &optional end utc)
  21353. "Format a TIMESTAMP element into a string.
  21354. FORMAT is a format specifier to be passed to
  21355. `format-time-string'.
  21356. When optional argument END is non-nil, use end of date-range or
  21357. time-range, if possible.
  21358. When optional argument UTC is non-nil, time will be expressed as
  21359. Universal Time."
  21360. (format-time-string
  21361. format
  21362. (apply 'encode-time
  21363. (cons 0
  21364. (mapcar
  21365. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21366. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21367. '(:minute-start :hour-start :day-start :month-start
  21368. :year-start)))))
  21369. utc))
  21370. (defun org-timestamp-split-range (timestamp &optional end)
  21371. "Extract a timestamp object from a date or time range.
  21372. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  21373. the end of the range. Otherwise, extract its start.
  21374. Return a new timestamp object sharing the same parent as
  21375. TIMESTAMP."
  21376. (let ((type (org-element-property :type timestamp)))
  21377. (if (memq type '(active inactive diary)) timestamp
  21378. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  21379. ;; Set new type.
  21380. (org-element-put-property
  21381. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21382. ;; Copy start properties over end properties if END is
  21383. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21384. (let ((p-alist '((:minute-start . :minute-end)
  21385. (:hour-start . :hour-end)
  21386. (:day-start . :day-end)
  21387. (:month-start . :month-end)
  21388. (:year-start . :year-end))))
  21389. (dolist (p-cell p-alist)
  21390. (org-element-put-property
  21391. split-ts
  21392. (funcall (if end 'car 'cdr) p-cell)
  21393. (org-element-property
  21394. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  21395. ;; Eventually refresh `:raw-value'.
  21396. (org-element-put-property split-ts :raw-value nil)
  21397. (org-element-put-property
  21398. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21399. (defun org-timestamp-translate (timestamp &optional boundary)
  21400. "Apply `org-translate-time' on a TIMESTAMP object.
  21401. When optional argument BOUNDARY is non-nil, it is either the
  21402. symbol `start' or `end'. In this case, only translate the
  21403. starting or ending part of TIMESTAMP if it is a date or time
  21404. range. Otherwise, translate both parts."
  21405. (if (and (not boundary)
  21406. (memq (org-element-property :type timestamp)
  21407. '(active-range inactive-range)))
  21408. (concat
  21409. (org-translate-time
  21410. (org-element-property :raw-value
  21411. (org-timestamp-split-range timestamp)))
  21412. "--"
  21413. (org-translate-time
  21414. (org-element-property :raw-value
  21415. (org-timestamp-split-range timestamp t))))
  21416. (org-translate-time
  21417. (org-element-property
  21418. :raw-value
  21419. (if (not boundary) timestamp
  21420. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  21421. ;;; Other stuff.
  21422. (defun org-reftex-citation ()
  21423. "Use reftex-citation to insert a citation into the buffer.
  21424. This looks for a line like
  21425. #+BIBLIOGRAPHY: foo plain option:-d
  21426. and derives from it that foo.bib is the bibliography file relevant
  21427. for this document. It then installs the necessary environment for RefTeX
  21428. to work in this buffer and calls `reftex-citation' to insert a citation
  21429. into the buffer.
  21430. Export of such citations to both LaTeX and HTML is handled by the contributed
  21431. package ox-bibtex by Taru Karttunen."
  21432. (interactive)
  21433. (let ((reftex-docstruct-symbol 'rds)
  21434. (reftex-cite-format "\\cite{%l}")
  21435. rds bib)
  21436. (save-excursion
  21437. (save-restriction
  21438. (widen)
  21439. (let ((case-fold-search t)
  21440. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21441. (if (not (save-excursion
  21442. (or (re-search-forward re nil t)
  21443. (re-search-backward re nil t))))
  21444. (user-error "No bibliography defined in file")
  21445. (setq bib (concat (match-string 1) ".bib")
  21446. rds (list (list 'bib bib)))))))
  21447. (call-interactively 'reftex-citation)))
  21448. ;;;; Functions extending outline functionality
  21449. (defun org-beginning-of-line (&optional arg)
  21450. "Go to the beginning of the current line. If that is invisible, continue
  21451. to a visible line beginning. This makes the function of C-a more intuitive.
  21452. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21453. first attempt, and only move to after the tags when the cursor is already
  21454. beyond the end of the headline."
  21455. (interactive "P")
  21456. (let ((pos (point))
  21457. (special (if (consp org-special-ctrl-a/e)
  21458. (car org-special-ctrl-a/e)
  21459. org-special-ctrl-a/e))
  21460. deactivate-mark refpos)
  21461. (if (org-bound-and-true-p visual-line-mode)
  21462. (beginning-of-visual-line 1)
  21463. (beginning-of-line 1))
  21464. (if (and arg (fboundp 'move-beginning-of-line))
  21465. (call-interactively 'move-beginning-of-line)
  21466. (if (bobp)
  21467. nil
  21468. (backward-char 1)
  21469. (if (org-truely-invisible-p)
  21470. (while (and (not (bobp)) (org-truely-invisible-p))
  21471. (backward-char 1)
  21472. (beginning-of-line 1))
  21473. (forward-char 1))))
  21474. (when special
  21475. (cond
  21476. ((and (looking-at org-complex-heading-regexp)
  21477. (= (char-after (match-end 1)) ?\ ))
  21478. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21479. (point-at-eol)))
  21480. (goto-char
  21481. (if (eq special t)
  21482. (cond ((> pos refpos) refpos)
  21483. ((= pos (point)) refpos)
  21484. (t (point)))
  21485. (cond ((> pos (point)) (point))
  21486. ((not (eq last-command this-command)) (point))
  21487. (t refpos)))))
  21488. ((org-at-item-p)
  21489. ;; Being at an item and not looking at an the item means point
  21490. ;; was previously moved to beginning of a visual line, which
  21491. ;; doesn't contain the item. Therefore, do nothing special,
  21492. ;; just stay here.
  21493. (when (looking-at org-list-full-item-re)
  21494. ;; Set special position at first white space character after
  21495. ;; bullet, and check-box, if any.
  21496. (let ((after-bullet
  21497. (let ((box (match-end 3)))
  21498. (if (not box) (match-end 1)
  21499. (let ((after (char-after box)))
  21500. (if (and after (= after ? )) (1+ box) box))))))
  21501. ;; Special case: Move point to special position when
  21502. ;; currently after it or at beginning of line.
  21503. (if (eq special t)
  21504. (when (or (> pos after-bullet) (= (point) pos))
  21505. (goto-char after-bullet))
  21506. ;; Reversed case: Move point to special position when
  21507. ;; point was already at beginning of line and command is
  21508. ;; repeated.
  21509. (when (and (= (point) pos) (eq last-command this-command))
  21510. (goto-char after-bullet))))))))
  21511. (org-no-warnings
  21512. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21513. (setq disable-point-adjustment
  21514. (or (not (invisible-p (point)))
  21515. (not (invisible-p (max (point-min) (1- (point))))))))
  21516. (defun org-end-of-line (&optional arg)
  21517. "Go to the end of the line.
  21518. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21519. tags on the first attempt, and only move to after the tags when
  21520. the cursor is already beyond the end of the headline."
  21521. (interactive "P")
  21522. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21523. org-special-ctrl-a/e))
  21524. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21525. 'end-of-visual-line)
  21526. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21527. (t 'end-of-line)))
  21528. deactivate-mark)
  21529. (if (or (not special) arg) (call-interactively move-fun)
  21530. (let* ((element (save-excursion (beginning-of-line)
  21531. (org-element-at-point)))
  21532. (type (org-element-type element)))
  21533. (cond
  21534. ((memq type '(headline inlinetask))
  21535. (let ((pos (point)))
  21536. (beginning-of-line 1)
  21537. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21538. (if (eq special t)
  21539. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21540. (goto-char (match-beginning 1))
  21541. (goto-char (match-end 0)))
  21542. (if (or (< pos (match-end 0))
  21543. (not (eq this-command last-command)))
  21544. (goto-char (match-end 0))
  21545. (goto-char (match-beginning 1))))
  21546. (call-interactively move-fun))))
  21547. ((outline-invisible-p (line-end-position))
  21548. ;; If element is hidden, `move-end-of-line' would put point
  21549. ;; after it. Use `end-of-line' to stay on current line.
  21550. (call-interactively 'end-of-line))
  21551. (t (call-interactively move-fun)))))
  21552. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21553. (setq disable-point-adjustment
  21554. (or (not (invisible-p (point)))
  21555. (not (invisible-p (max (point-min) (1- (point))))))))
  21556. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21557. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21558. (defun org-backward-sentence (&optional arg)
  21559. "Go to beginning of sentence, or beginning of table field.
  21560. This will call `backward-sentence' or `org-table-beginning-of-field',
  21561. depending on context."
  21562. (interactive "P")
  21563. (cond
  21564. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  21565. (t (call-interactively 'backward-sentence))))
  21566. (defun org-forward-sentence (&optional arg)
  21567. "Go to end of sentence, or end of table field.
  21568. This will call `forward-sentence' or `org-table-end-of-field',
  21569. depending on context."
  21570. (interactive "P")
  21571. (cond
  21572. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  21573. (t (call-interactively 'forward-sentence))))
  21574. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21575. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21576. (defun org-kill-line (&optional arg)
  21577. "Kill line, to tags or end of line."
  21578. (interactive "P")
  21579. (cond
  21580. ((or (not org-special-ctrl-k)
  21581. (bolp)
  21582. (not (org-at-heading-p)))
  21583. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21584. org-ctrl-k-protect-subtree)
  21585. (if (or (eq org-ctrl-k-protect-subtree 'error)
  21586. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  21587. (user-error "C-k aborted as it would kill a hidden subtree")))
  21588. (call-interactively
  21589. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  21590. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  21591. (kill-region (point) (match-beginning 1))
  21592. (org-set-tags nil t))
  21593. (t (kill-region (point) (point-at-eol)))))
  21594. (define-key org-mode-map "\C-k" 'org-kill-line)
  21595. (defun org-yank (&optional arg)
  21596. "Yank. If the kill is a subtree, treat it specially.
  21597. This command will look at the current kill and check if is a single
  21598. subtree, or a series of subtrees[1]. If it passes the test, and if the
  21599. cursor is at the beginning of a line or after the stars of a currently
  21600. empty headline, then the yank is handled specially. How exactly depends
  21601. on the value of the following variables, both set by default.
  21602. `org-yank-folded-subtrees'
  21603. When set, the subtree(s) will be folded after insertion, but only
  21604. if doing so would now swallow text after the yanked text.
  21605. `org-yank-adjusted-subtrees'
  21606. When set, the subtree will be promoted or demoted in order to
  21607. fit into the local outline tree structure, which means that the
  21608. level will be adjusted so that it becomes the smaller one of the
  21609. two *visible* surrounding headings.
  21610. Any prefix to this command will cause `yank' to be called directly with
  21611. no special treatment. In particular, a simple \\[universal-argument] prefix \
  21612. will just
  21613. plainly yank the text as it is.
  21614. \[1] The test checks if the first non-white line is a heading
  21615. and if there are no other headings with fewer stars."
  21616. (interactive "P")
  21617. (org-yank-generic 'yank arg))
  21618. (defun org-yank-generic (command arg)
  21619. "Perform some yank-like command.
  21620. This function implements the behavior described in the `org-yank'
  21621. documentation. However, it has been generalized to work for any
  21622. interactive command with similar behavior."
  21623. ;; pretend to be command COMMAND
  21624. (setq this-command command)
  21625. (if arg
  21626. (call-interactively command)
  21627. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  21628. (and (org-kill-is-subtree-p)
  21629. (or (bolp)
  21630. (and (looking-at "[ \t]*$")
  21631. (string-match
  21632. "\\`\\*+\\'"
  21633. (buffer-substring (point-at-bol) (point)))))))
  21634. swallowp)
  21635. (cond
  21636. ((and subtreep org-yank-folded-subtrees)
  21637. (let ((beg (point))
  21638. end)
  21639. (if (and subtreep org-yank-adjusted-subtrees)
  21640. (org-paste-subtree nil nil 'for-yank)
  21641. (call-interactively command))
  21642. (setq end (point))
  21643. (goto-char beg)
  21644. (when (and (bolp) subtreep
  21645. (not (setq swallowp
  21646. (org-yank-folding-would-swallow-text beg end))))
  21647. (org-with-limited-levels
  21648. (or (looking-at org-outline-regexp)
  21649. (re-search-forward org-outline-regexp-bol end t))
  21650. (while (and (< (point) end) (looking-at org-outline-regexp))
  21651. (hide-subtree)
  21652. (org-cycle-show-empty-lines 'folded)
  21653. (condition-case nil
  21654. (outline-forward-same-level 1)
  21655. (error (goto-char end))))))
  21656. (when swallowp
  21657. (message
  21658. "Inserted text not folded because that would swallow text"))
  21659. (goto-char end)
  21660. (skip-chars-forward " \t\n\r")
  21661. (beginning-of-line 1)
  21662. (push-mark beg 'nomsg)))
  21663. ((and subtreep org-yank-adjusted-subtrees)
  21664. (let ((beg (point-at-bol)))
  21665. (org-paste-subtree nil nil 'for-yank)
  21666. (push-mark beg 'nomsg)))
  21667. (t
  21668. (call-interactively command))))))
  21669. (defun org-yank-folding-would-swallow-text (beg end)
  21670. "Would hide-subtree at BEG swallow any text after END?"
  21671. (let (level)
  21672. (org-with-limited-levels
  21673. (save-excursion
  21674. (goto-char beg)
  21675. (when (or (looking-at org-outline-regexp)
  21676. (re-search-forward org-outline-regexp-bol end t))
  21677. (setq level (org-outline-level)))
  21678. (goto-char end)
  21679. (skip-chars-forward " \t\r\n\v\f")
  21680. (if (or (eobp)
  21681. (and (bolp) (looking-at org-outline-regexp)
  21682. (<= (org-outline-level) level)))
  21683. nil ; Nothing would be swallowed
  21684. t))))) ; something would swallow
  21685. (define-key org-mode-map "\C-y" 'org-yank)
  21686. (defun org-truely-invisible-p ()
  21687. "Check if point is at a character currently not visible.
  21688. This version does not only check the character property, but also
  21689. `visible-mode'."
  21690. ;; Early versions of noutline don't have `outline-invisible-p'.
  21691. (if (org-bound-and-true-p visible-mode)
  21692. nil
  21693. (outline-invisible-p)))
  21694. (defun org-invisible-p2 ()
  21695. "Check if point is at a character currently not visible."
  21696. (save-excursion
  21697. (if (and (eolp) (not (bobp))) (backward-char 1))
  21698. ;; Early versions of noutline don't have `outline-invisible-p'.
  21699. (outline-invisible-p)))
  21700. (defun org-back-to-heading (&optional invisible-ok)
  21701. "Call `outline-back-to-heading', but provide a better error message."
  21702. (condition-case nil
  21703. (outline-back-to-heading invisible-ok)
  21704. (error (error "Before first headline at position %d in buffer %s"
  21705. (point) (current-buffer)))))
  21706. (defun org-before-first-heading-p ()
  21707. "Before first heading?"
  21708. (save-excursion
  21709. (end-of-line)
  21710. (null (re-search-backward org-outline-regexp-bol nil t))))
  21711. (defun org-at-heading-p (&optional ignored)
  21712. (outline-on-heading-p t))
  21713. ;; Compatibility alias with Org versions < 7.8.03
  21714. (defalias 'org-on-heading-p 'org-at-heading-p)
  21715. (defun org-in-commented-heading-p (&optional no-inheritance)
  21716. "Non-nil if point is under a commented heading.
  21717. This function also checks ancestors of the current headline,
  21718. unless optional argument NO-INHERITANCE is non-nil."
  21719. (cond
  21720. ((org-before-first-heading-p) nil)
  21721. ((let ((headline (nth 4 (org-heading-components))))
  21722. (and headline
  21723. (let ((case-fold-search nil))
  21724. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  21725. headline)))))
  21726. (no-inheritance nil)
  21727. (t
  21728. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  21729. (defun org-at-comment-p nil
  21730. "Is cursor in a commented line?"
  21731. (save-excursion
  21732. (save-match-data
  21733. (beginning-of-line)
  21734. (looking-at "^[ \t]*# "))))
  21735. (defun org-at-drawer-p nil
  21736. "Is cursor at a drawer keyword?"
  21737. (save-excursion
  21738. (move-beginning-of-line 1)
  21739. (looking-at org-drawer-regexp)))
  21740. (defun org-at-block-p nil
  21741. "Is cursor at a block keyword?"
  21742. (save-excursion
  21743. (move-beginning-of-line 1)
  21744. (looking-at org-block-regexp)))
  21745. (defun org-point-at-end-of-empty-headline ()
  21746. "If point is at the end of an empty headline, return t, else nil.
  21747. If the heading only contains a TODO keyword, it is still still considered
  21748. empty."
  21749. (and (looking-at "[ \t]*$")
  21750. (when org-todo-line-regexp
  21751. (save-excursion
  21752. (beginning-of-line 1)
  21753. (let ((case-fold-search nil))
  21754. (looking-at org-todo-line-regexp)
  21755. (string= (match-string 3) ""))))))
  21756. (defun org-at-heading-or-item-p ()
  21757. (or (org-at-heading-p) (org-at-item-p)))
  21758. (defun org-at-target-p ()
  21759. (or (org-in-regexp org-radio-target-regexp)
  21760. (org-in-regexp org-target-regexp)))
  21761. ;; Compatibility alias with Org versions < 7.8.03
  21762. (defalias 'org-on-target-p 'org-at-target-p)
  21763. (defun org-up-heading-all (arg)
  21764. "Move to the heading line of which the present line is a subheading.
  21765. This function considers both visible and invisible heading lines.
  21766. With argument, move up ARG levels."
  21767. (if (fboundp 'outline-up-heading-all)
  21768. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21769. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21770. (defun org-up-heading-safe ()
  21771. "Move to the heading line of which the present line is a subheading.
  21772. This version will not throw an error. It will return the level of the
  21773. headline found, or nil if no higher level is found.
  21774. Also, this function will be a lot faster than `outline-up-heading',
  21775. because it relies on stars being the outline starters. This can really
  21776. make a significant difference in outlines with very many siblings."
  21777. (let (start-level re)
  21778. (org-back-to-heading t)
  21779. (setq start-level (funcall outline-level))
  21780. (if (equal start-level 1)
  21781. nil
  21782. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21783. (if (re-search-backward re nil t)
  21784. (funcall outline-level)))))
  21785. (defun org-first-sibling-p ()
  21786. "Is this heading the first child of its parents?"
  21787. (interactive)
  21788. (let ((re org-outline-regexp-bol)
  21789. level l)
  21790. (unless (org-at-heading-p t)
  21791. (user-error "Not at a heading"))
  21792. (setq level (funcall outline-level))
  21793. (save-excursion
  21794. (if (not (re-search-backward re nil t))
  21795. t
  21796. (setq l (funcall outline-level))
  21797. (< l level)))))
  21798. (defun org-goto-sibling (&optional previous)
  21799. "Goto the next sibling, even if it is invisible.
  21800. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21801. when a sibling was found. When none is found, return nil and don't
  21802. move point."
  21803. (let ((fun (if previous 're-search-backward 're-search-forward))
  21804. (pos (point))
  21805. (re org-outline-regexp-bol)
  21806. level l)
  21807. (when (ignore-errors (org-back-to-heading t))
  21808. (setq level (funcall outline-level))
  21809. (catch 'exit
  21810. (or previous (forward-char 1))
  21811. (while (funcall fun re nil t)
  21812. (setq l (funcall outline-level))
  21813. (when (< l level) (goto-char pos) (throw 'exit nil))
  21814. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21815. (goto-char pos)
  21816. nil))))
  21817. (defun org-show-siblings ()
  21818. "Show all siblings of the current headline."
  21819. (save-excursion
  21820. (while (org-goto-sibling) (org-flag-heading nil)))
  21821. (save-excursion
  21822. (while (org-goto-sibling 'previous)
  21823. (org-flag-heading nil))))
  21824. (defun org-goto-first-child ()
  21825. "Goto the first child, even if it is invisible.
  21826. Return t when a child was found. Otherwise don't move point and
  21827. return nil."
  21828. (let (level (pos (point)) (re org-outline-regexp-bol))
  21829. (when (ignore-errors (org-back-to-heading t))
  21830. (setq level (outline-level))
  21831. (forward-char 1)
  21832. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21833. (progn (goto-char (match-beginning 0)) t)
  21834. (goto-char pos) nil))))
  21835. (defun org-show-hidden-entry ()
  21836. "Show an entry where even the heading is hidden."
  21837. (save-excursion
  21838. (org-show-entry)))
  21839. (defun org-flag-heading (flag &optional entry)
  21840. "Flag the current heading. FLAG non-nil means make invisible.
  21841. When ENTRY is non-nil, show the entire entry."
  21842. (save-excursion
  21843. (org-back-to-heading t)
  21844. ;; Check if we should show the entire entry
  21845. (if entry
  21846. (progn
  21847. (org-show-entry)
  21848. (save-excursion
  21849. (and (outline-next-heading)
  21850. (org-flag-heading nil))))
  21851. (outline-flag-region (max (point-min) (1- (point)))
  21852. (save-excursion (outline-end-of-heading) (point))
  21853. flag))))
  21854. (defun org-get-next-sibling ()
  21855. "Move to next heading of the same level, and return point.
  21856. If there is no such heading, return nil.
  21857. This is like outline-next-sibling, but invisible headings are ok."
  21858. (let ((level (funcall outline-level)))
  21859. (outline-next-heading)
  21860. (while (and (not (eobp)) (> (funcall outline-level) level))
  21861. (outline-next-heading))
  21862. (if (or (eobp) (< (funcall outline-level) level))
  21863. nil
  21864. (point))))
  21865. (defun org-get-last-sibling ()
  21866. "Move to previous heading of the same level, and return point.
  21867. If there is no such heading, return nil."
  21868. (let ((opoint (point))
  21869. (level (funcall outline-level)))
  21870. (outline-previous-heading)
  21871. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21872. (while (and (> (funcall outline-level) level)
  21873. (not (bobp)))
  21874. (outline-previous-heading))
  21875. (if (< (funcall outline-level) level)
  21876. nil
  21877. (point)))))
  21878. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21879. "Goto to the end of a subtree."
  21880. ;; This contains an exact copy of the original function, but it uses
  21881. ;; `org-back-to-heading', to make it work also in invisible
  21882. ;; trees. And is uses an invisible-ok argument.
  21883. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21884. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21885. ;; with many children and grandchildren etc, this can be much faster
  21886. ;; than the outline version.
  21887. (org-back-to-heading invisible-ok)
  21888. (let ((first t)
  21889. (level (funcall outline-level)))
  21890. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21891. ;; A true heading (not a plain list item), in Org-mode
  21892. ;; This means we can easily find the end by looking
  21893. ;; only for the right number of stars. Using a regexp to do
  21894. ;; this is so much faster than using a Lisp loop.
  21895. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21896. (forward-char 1)
  21897. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21898. ;; something else, do it the slow way
  21899. (while (and (not (eobp))
  21900. (or first (> (funcall outline-level) level)))
  21901. (setq first nil)
  21902. (outline-next-heading)))
  21903. (unless to-heading
  21904. (if (memq (preceding-char) '(?\n ?\^M))
  21905. (progn
  21906. ;; Go to end of line before heading
  21907. (forward-char -1)
  21908. (if (memq (preceding-char) '(?\n ?\^M))
  21909. ;; leave blank line before heading
  21910. (forward-char -1))))))
  21911. (point))
  21912. (defun org-end-of-meta-data-and-drawers ()
  21913. "Jump to the first text after meta data and drawers in the current entry.
  21914. This will move over empty lines, lines with planning time stamps,
  21915. clocking lines, and drawers."
  21916. (org-back-to-heading t)
  21917. (let ((end (save-excursion (outline-next-heading) (point)))
  21918. (re (concat "\\(" org-drawer-regexp "\\)"
  21919. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21920. (forward-line 1)
  21921. (while (re-search-forward re end t)
  21922. (if (not (match-end 1))
  21923. ;; empty or planning line
  21924. (forward-line 1)
  21925. ;; a drawer, find the end
  21926. (re-search-forward "^[ \t]*:END:" end 'move)
  21927. (forward-line 1)))
  21928. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21929. (point)))
  21930. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21931. "Move forward to the ARG'th subheading at same level as this one.
  21932. Stop at the first and last subheadings of a superior heading.
  21933. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21934. non-nil it will also look at invisible ones."
  21935. (interactive "p")
  21936. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21937. (if (and arg (< arg 0))
  21938. (goto-char (point-min))
  21939. (outline-next-heading))
  21940. (org-at-heading-p)
  21941. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21942. (f (if (and arg (< arg 0))
  21943. 're-search-backward
  21944. 're-search-forward))
  21945. (count (if arg (abs arg) 1))
  21946. (result (point)))
  21947. (while (and (prog1 (> count 0)
  21948. (forward-char (if (and arg (< arg 0)) -1 1)))
  21949. (funcall f org-outline-regexp-bol nil 'move))
  21950. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21951. (cond ((< l level) (setq count 0))
  21952. ((and (= l level)
  21953. (or invisible-ok
  21954. (progn
  21955. (goto-char (line-beginning-position))
  21956. (not (outline-invisible-p)))))
  21957. (setq count (1- count))
  21958. (when (eq l level)
  21959. (setq result (point)))))))
  21960. (goto-char result))
  21961. (beginning-of-line 1)))
  21962. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21963. "Move backward to the ARG'th subheading at same level as this one.
  21964. Stop at the first and last subheadings of a superior heading."
  21965. (interactive "p")
  21966. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21967. (defun org-next-block (arg &optional backward block-regexp)
  21968. "Jump to the next block.
  21969. With a prefix argument ARG, jump forward ARG many source blocks.
  21970. When BACKWARD is non-nil, jump to the previous block.
  21971. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21972. (interactive "p")
  21973. (let ((re (or block-regexp org-block-regexp))
  21974. (re-search-fn (or (and backward 're-search-backward)
  21975. 're-search-forward)))
  21976. (if (looking-at re) (forward-char 1))
  21977. (condition-case nil
  21978. (funcall re-search-fn re nil nil arg)
  21979. (error (user-error "No %s code blocks"
  21980. (if backward "previous" "further" ))))
  21981. (goto-char (match-beginning 0)) (org-show-context)))
  21982. (defun org-previous-block (arg &optional block-regexp)
  21983. "Jump to the previous block.
  21984. With a prefix argument ARG, jump backward ARG many source blocks.
  21985. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21986. (interactive "p")
  21987. (org-next-block arg t block-regexp))
  21988. (defun org-forward-paragraph ()
  21989. "Move forward to beginning of next paragraph or equivalent.
  21990. The function moves point to the beginning of the next visible
  21991. structural element, which can be a paragraph, a table, a list
  21992. item, etc. It also provides some special moves for convenience:
  21993. - On an affiliated keyword, jump to the beginning of the
  21994. relative element.
  21995. - On an item or a footnote definition, move to the second
  21996. element inside, if any.
  21997. - On a table or a property drawer, jump after it.
  21998. - On a verse or source block, stop after blank lines."
  21999. (interactive)
  22000. (when (eobp) (user-error "Cannot move further down"))
  22001. (let* ((deactivate-mark nil)
  22002. (element (org-element-at-point))
  22003. (type (org-element-type element))
  22004. (post-affiliated (org-element-property :post-affiliated element))
  22005. (contents-begin (org-element-property :contents-begin element))
  22006. (contents-end (org-element-property :contents-end element))
  22007. (end (let ((end (org-element-property :end element)) (parent element))
  22008. (while (and (setq parent (org-element-property :parent parent))
  22009. (= (org-element-property :contents-end parent) end))
  22010. (setq end (org-element-property :end parent)))
  22011. end)))
  22012. (cond ((not element)
  22013. (skip-chars-forward " \r\t\n")
  22014. (or (eobp) (beginning-of-line)))
  22015. ;; On affiliated keywords, move to element's beginning.
  22016. ((and post-affiliated (< (point) post-affiliated))
  22017. (goto-char post-affiliated))
  22018. ;; At a table row, move to the end of the table. Similarly,
  22019. ;; at a node property, move to the end of the property
  22020. ;; drawer.
  22021. ((memq type '(node-property table-row))
  22022. (goto-char (org-element-property
  22023. :end (org-element-property :parent element))))
  22024. ((memq type '(property-drawer table)) (goto-char end))
  22025. ;; Consider blank lines as separators in verse and source
  22026. ;; blocks to ease editing.
  22027. ((memq type '(src-block verse-block))
  22028. (when (eq type 'src-block)
  22029. (setq contents-end
  22030. (save-excursion (goto-char end)
  22031. (skip-chars-backward " \r\t\n")
  22032. (line-beginning-position))))
  22033. (beginning-of-line)
  22034. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22035. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22036. (goto-char end)
  22037. (skip-chars-forward " \r\t\n")
  22038. (if (= (point) contents-end) (goto-char end)
  22039. (beginning-of-line))))
  22040. ;; With no contents, just skip element.
  22041. ((not contents-begin) (goto-char end))
  22042. ;; If contents are invisible, skip the element altogether.
  22043. ((outline-invisible-p (line-end-position))
  22044. (case type
  22045. (headline
  22046. (org-with-limited-levels (outline-next-visible-heading 1)))
  22047. ;; At a plain list, make sure we move to the next item
  22048. ;; instead of skipping the whole list.
  22049. (plain-list (forward-char)
  22050. (org-forward-paragraph))
  22051. (otherwise (goto-char end))))
  22052. ((>= (point) contents-end) (goto-char end))
  22053. ((>= (point) contents-begin)
  22054. ;; This can only happen on paragraphs and plain lists.
  22055. (case type
  22056. (paragraph (goto-char end))
  22057. ;; At a plain list, try to move to second element in
  22058. ;; first item, if possible.
  22059. (plain-list (end-of-line)
  22060. (org-forward-paragraph))))
  22061. ;; When contents start on the middle of a line (e.g. in
  22062. ;; items and footnote definitions), try to reach first
  22063. ;; element starting after current line.
  22064. ((> (line-end-position) contents-begin)
  22065. (end-of-line)
  22066. (org-forward-paragraph))
  22067. (t (goto-char contents-begin)))))
  22068. (defun org-backward-paragraph ()
  22069. "Move backward to start of previous paragraph or equivalent.
  22070. The function moves point to the beginning of the current
  22071. structural element, which can be a paragraph, a table, a list
  22072. item, etc., or to the beginning of the previous visible one if
  22073. point is already there. It also provides some special moves for
  22074. convenience:
  22075. - On an affiliated keyword, jump to the first one.
  22076. - On a table or a property drawer, move to its beginning.
  22077. - On a verse or source block, stop before blank lines."
  22078. (interactive)
  22079. (when (bobp) (user-error "Cannot move further up"))
  22080. (let* ((deactivate-mark nil)
  22081. (element (org-element-at-point))
  22082. (type (org-element-type element))
  22083. (contents-begin (org-element-property :contents-begin element))
  22084. (contents-end (org-element-property :contents-end element))
  22085. (post-affiliated (org-element-property :post-affiliated element))
  22086. (begin (org-element-property :begin element)))
  22087. (cond
  22088. ((not element) (goto-char (point-min)))
  22089. ((= (point) begin)
  22090. (backward-char)
  22091. (org-backward-paragraph))
  22092. ((and post-affiliated (<= (point) post-affiliated)) (goto-char begin))
  22093. ((memq type '(node-property table-row))
  22094. (goto-char (org-element-property
  22095. :post-affiliated (org-element-property :parent element))))
  22096. ((memq type '(property-drawer table)) (goto-char begin))
  22097. ((memq type '(src-block verse-block))
  22098. (when (eq type 'src-block)
  22099. (setq contents-begin
  22100. (save-excursion (goto-char begin) (forward-line) (point))))
  22101. (if (= (point) contents-begin) (goto-char post-affiliated)
  22102. ;; Inside a verse block, see blank lines as paragraph
  22103. ;; separators.
  22104. (let ((origin (point)))
  22105. (skip-chars-backward " \r\t\n" contents-begin)
  22106. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22107. (skip-chars-forward " \r\t\n" origin)
  22108. (if (= (point) origin) (goto-char contents-begin)
  22109. (beginning-of-line))))))
  22110. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22111. ((eq type 'paragraph)
  22112. (goto-char contents-begin)
  22113. ;; When at first paragraph in an item or a footnote definition,
  22114. ;; move directly to beginning of line.
  22115. (let ((parent-contents
  22116. (org-element-property
  22117. :contents-begin (org-element-property :parent element))))
  22118. (when (and parent-contents (= parent-contents contents-begin))
  22119. (beginning-of-line))))
  22120. ;; At the end of a greater element, move to the beginning of the
  22121. ;; last element within.
  22122. ((>= (point) contents-end)
  22123. (goto-char (1- contents-end))
  22124. (org-backward-paragraph))
  22125. (t (goto-char (or post-affiliated begin))))
  22126. ;; Ensure we never leave point invisible.
  22127. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22128. (defun org-forward-element ()
  22129. "Move forward by one element.
  22130. Move to the next element at the same level, when possible."
  22131. (interactive)
  22132. (cond ((eobp) (user-error "Cannot move further down"))
  22133. ((org-with-limited-levels (org-at-heading-p))
  22134. (let ((origin (point)))
  22135. (goto-char (org-end-of-subtree nil t))
  22136. (unless (org-with-limited-levels (org-at-heading-p))
  22137. (goto-char origin)
  22138. (user-error "Cannot move further down"))))
  22139. (t
  22140. (let* ((elem (org-element-at-point))
  22141. (end (org-element-property :end elem))
  22142. (parent (org-element-property :parent elem)))
  22143. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22144. (goto-char (org-element-property :end parent)))
  22145. ((integer-or-marker-p end) (goto-char end))
  22146. (t (message "No element at point")))))))
  22147. (defun org-backward-element ()
  22148. "Move backward by one element.
  22149. Move to the previous element at the same level, when possible."
  22150. (interactive)
  22151. (cond ((bobp) (user-error "Cannot move further up"))
  22152. ((org-with-limited-levels (org-at-heading-p))
  22153. ;; At a headline, move to the previous one, if any, or stay
  22154. ;; here.
  22155. (let ((origin (point)))
  22156. (org-with-limited-levels (org-backward-heading-same-level 1))
  22157. ;; When current headline has no sibling above, move to its
  22158. ;; parent.
  22159. (when (= (point) origin)
  22160. (or (org-with-limited-levels (org-up-heading-safe))
  22161. (progn (goto-char origin)
  22162. (user-error "Cannot move further up"))))))
  22163. (t
  22164. (let* ((elem (org-element-at-point))
  22165. (beg (org-element-property :begin elem)))
  22166. (cond
  22167. ;; Move to beginning of current element if point isn't
  22168. ;; there already.
  22169. ((null beg) (message "No element at point"))
  22170. ((/= (point) beg) (goto-char beg))
  22171. (t (goto-char beg)
  22172. (skip-chars-backward " \r\t\n")
  22173. (unless (bobp)
  22174. (let ((prev (org-element-at-point)))
  22175. (goto-char (org-element-property :begin prev))
  22176. (while (and (setq prev (org-element-property :parent prev))
  22177. (<= (org-element-property :end prev) beg))
  22178. (goto-char (org-element-property :begin prev)))))))))))
  22179. (defun org-up-element ()
  22180. "Move to upper element."
  22181. (interactive)
  22182. (if (org-with-limited-levels (org-at-heading-p))
  22183. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22184. (let* ((elem (org-element-at-point))
  22185. (parent (org-element-property :parent elem)))
  22186. (if parent (goto-char (org-element-property :begin parent))
  22187. (if (org-with-limited-levels (org-before-first-heading-p))
  22188. (user-error "No surrounding element")
  22189. (org-with-limited-levels (org-back-to-heading)))))))
  22190. (defvar org-element-greater-elements)
  22191. (defun org-down-element ()
  22192. "Move to inner element."
  22193. (interactive)
  22194. (let ((element (org-element-at-point)))
  22195. (cond
  22196. ((memq (org-element-type element) '(plain-list table))
  22197. (goto-char (org-element-property :contents-begin element))
  22198. (forward-char))
  22199. ((memq (org-element-type element) org-element-greater-elements)
  22200. ;; If contents are hidden, first disclose them.
  22201. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22202. (goto-char (or (org-element-property :contents-begin element)
  22203. (user-error "No content for this element"))))
  22204. (t (user-error "No inner element")))))
  22205. (defun org-drag-element-backward ()
  22206. "Move backward element at point."
  22207. (interactive)
  22208. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22209. (let* ((elem (org-element-at-point))
  22210. (prev-elem
  22211. (save-excursion
  22212. (goto-char (org-element-property :begin elem))
  22213. (skip-chars-backward " \r\t\n")
  22214. (unless (bobp)
  22215. (let* ((beg (org-element-property :begin elem))
  22216. (prev (org-element-at-point))
  22217. (up prev))
  22218. (while (and (setq up (org-element-property :parent up))
  22219. (<= (org-element-property :end up) beg))
  22220. (setq prev up))
  22221. prev)))))
  22222. ;; Error out if no previous element or previous element is
  22223. ;; a parent of the current one.
  22224. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22225. (user-error "Cannot drag element backward")
  22226. (let ((pos (point)))
  22227. (org-element-swap-A-B prev-elem elem)
  22228. (goto-char (+ (org-element-property :begin prev-elem)
  22229. (- pos (org-element-property :begin elem)))))))))
  22230. (defun org-drag-element-forward ()
  22231. "Move forward element at point."
  22232. (interactive)
  22233. (let* ((pos (point))
  22234. (elem (org-element-at-point)))
  22235. (when (= (point-max) (org-element-property :end elem))
  22236. (user-error "Cannot drag element forward"))
  22237. (goto-char (org-element-property :end elem))
  22238. (let ((next-elem (org-element-at-point)))
  22239. (when (or (org-element-nested-p elem next-elem)
  22240. (and (eq (org-element-type next-elem) 'headline)
  22241. (not (eq (org-element-type elem) 'headline))))
  22242. (goto-char pos)
  22243. (user-error "Cannot drag element forward"))
  22244. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22245. ;; body's length (without final blanks) and by the length of
  22246. ;; blanks between ELEM and NEXT-ELEM.
  22247. (let ((size-next (- (save-excursion
  22248. (goto-char (org-element-property :end next-elem))
  22249. (skip-chars-backward " \r\t\n")
  22250. (forward-line)
  22251. ;; Small correction if buffer doesn't end
  22252. ;; with a newline character.
  22253. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22254. (org-element-property :begin next-elem)))
  22255. (size-blank (- (org-element-property :end elem)
  22256. (save-excursion
  22257. (goto-char (org-element-property :end elem))
  22258. (skip-chars-backward " \r\t\n")
  22259. (forward-line)
  22260. (point)))))
  22261. (org-element-swap-A-B elem next-elem)
  22262. (goto-char (+ pos size-next size-blank))))))
  22263. (defun org-drag-line-forward (arg)
  22264. "Drag the line at point ARG lines forward."
  22265. (interactive "p")
  22266. (dotimes (n (abs arg))
  22267. (let ((c (current-column)))
  22268. (if (< 0 arg)
  22269. (progn
  22270. (beginning-of-line 2)
  22271. (transpose-lines 1)
  22272. (beginning-of-line 0))
  22273. (transpose-lines 1)
  22274. (beginning-of-line -1))
  22275. (org-move-to-column c))))
  22276. (defun org-drag-line-backward (arg)
  22277. "Drag the line at point ARG lines backward."
  22278. (interactive "p")
  22279. (org-drag-line-forward (- arg)))
  22280. (defun org-mark-element ()
  22281. "Put point at beginning of this element, mark at end.
  22282. Interactively, if this command is repeated or (in Transient Mark
  22283. mode) if the mark is active, it marks the next element after the
  22284. ones already marked."
  22285. (interactive)
  22286. (let (deactivate-mark)
  22287. (if (and (org-called-interactively-p 'any)
  22288. (or (and (eq last-command this-command) (mark t))
  22289. (and transient-mark-mode mark-active)))
  22290. (set-mark
  22291. (save-excursion
  22292. (goto-char (mark))
  22293. (goto-char (org-element-property :end (org-element-at-point)))))
  22294. (let ((element (org-element-at-point)))
  22295. (end-of-line)
  22296. (push-mark (org-element-property :end element) t t)
  22297. (goto-char (org-element-property :begin element))))))
  22298. (defun org-narrow-to-element ()
  22299. "Narrow buffer to current element."
  22300. (interactive)
  22301. (let ((elem (org-element-at-point)))
  22302. (cond
  22303. ((eq (car elem) 'headline)
  22304. (narrow-to-region
  22305. (org-element-property :begin elem)
  22306. (org-element-property :end elem)))
  22307. ((memq (car elem) org-element-greater-elements)
  22308. (narrow-to-region
  22309. (org-element-property :contents-begin elem)
  22310. (org-element-property :contents-end elem)))
  22311. (t
  22312. (narrow-to-region
  22313. (org-element-property :begin elem)
  22314. (org-element-property :end elem))))))
  22315. (defun org-transpose-element ()
  22316. "Transpose current and previous elements, keeping blank lines between.
  22317. Point is moved after both elements."
  22318. (interactive)
  22319. (org-skip-whitespace)
  22320. (let ((end (org-element-property :end (org-element-at-point))))
  22321. (org-drag-element-backward)
  22322. (goto-char end)))
  22323. (defun org-unindent-buffer ()
  22324. "Un-indent the visible part of the buffer.
  22325. Relative indentation (between items, inside blocks, etc.) isn't
  22326. modified."
  22327. (interactive)
  22328. (unless (eq major-mode 'org-mode)
  22329. (user-error "Cannot un-indent a buffer not in Org mode"))
  22330. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  22331. unindent-tree ; For byte-compiler.
  22332. (unindent-tree
  22333. (function
  22334. (lambda (contents)
  22335. (mapc
  22336. (lambda (element)
  22337. (if (memq (org-element-type element) '(headline section))
  22338. (funcall unindent-tree (org-element-contents element))
  22339. (save-excursion
  22340. (save-restriction
  22341. (narrow-to-region
  22342. (org-element-property :begin element)
  22343. (org-element-property :end element))
  22344. (org-do-remove-indentation)))))
  22345. (reverse contents))))))
  22346. (funcall unindent-tree (org-element-contents parse-tree))))
  22347. (defun org-show-subtree ()
  22348. "Show everything after this heading at deeper levels."
  22349. (interactive)
  22350. (outline-flag-region
  22351. (point)
  22352. (save-excursion
  22353. (org-end-of-subtree t t))
  22354. nil))
  22355. (defun org-show-entry ()
  22356. "Show the body directly following this heading.
  22357. Show the heading too, if it is currently invisible."
  22358. (interactive)
  22359. (save-excursion
  22360. (ignore-errors
  22361. (org-back-to-heading t)
  22362. (outline-flag-region
  22363. (max (point-min) (1- (point)))
  22364. (save-excursion
  22365. (if (re-search-forward
  22366. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22367. (match-beginning 1)
  22368. (point-max)))
  22369. nil)
  22370. (org-cycle-hide-drawers 'children))))
  22371. (defun org-make-options-regexp (kwds &optional extra)
  22372. "Make a regular expression for keyword lines."
  22373. (concat
  22374. "^[ \t]*#\\+\\("
  22375. (mapconcat 'regexp-quote kwds "\\|")
  22376. (if extra (concat "\\|" extra))
  22377. "\\):[ \t]*\\(.*\\)"))
  22378. ;; Make isearch reveal the necessary context
  22379. (defun org-isearch-end ()
  22380. "Reveal context after isearch exits."
  22381. (when isearch-success ; only if search was successful
  22382. (if (featurep 'xemacs)
  22383. ;; Under XEmacs, the hook is run in the correct place,
  22384. ;; we directly show the context.
  22385. (org-show-context 'isearch)
  22386. ;; In Emacs the hook runs *before* restoring the overlays.
  22387. ;; So we have to use a one-time post-command-hook to do this.
  22388. ;; (Emacs 22 has a special variable, see function `org-mode')
  22389. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22390. isearch-mode-end-hook-quit)
  22391. ;; Only when the isearch was not quitted.
  22392. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22393. 'append 'local)))))
  22394. (defun org-isearch-post-command ()
  22395. "Remove self from hook, and show context."
  22396. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22397. (org-show-context 'isearch))
  22398. ;;;; Integration with and fixes for other packages
  22399. ;;; Imenu support
  22400. (defvar org-imenu-markers nil
  22401. "All markers currently used by Imenu.")
  22402. (make-variable-buffer-local 'org-imenu-markers)
  22403. (defun org-imenu-new-marker (&optional pos)
  22404. "Return a new marker for use by Imenu, and remember the marker."
  22405. (let ((m (make-marker)))
  22406. (move-marker m (or pos (point)))
  22407. (push m org-imenu-markers)
  22408. m))
  22409. (defun org-imenu-get-tree ()
  22410. "Produce the index for Imenu."
  22411. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  22412. (setq org-imenu-markers nil)
  22413. (let* ((n org-imenu-depth)
  22414. (re (concat "^" (org-get-limited-outline-regexp)))
  22415. (subs (make-vector (1+ n) nil))
  22416. (last-level 0)
  22417. m level head0 head)
  22418. (save-excursion
  22419. (save-restriction
  22420. (widen)
  22421. (goto-char (point-max))
  22422. (while (re-search-backward re nil t)
  22423. (setq level (org-reduced-level (funcall outline-level)))
  22424. (when (and (<= level n)
  22425. (looking-at org-complex-heading-regexp)
  22426. (setq head0 (org-match-string-no-properties 4)))
  22427. (setq head (org-link-display-format head0)
  22428. m (org-imenu-new-marker))
  22429. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22430. (if (>= level last-level)
  22431. (push (cons head m) (aref subs level))
  22432. (push (cons head (aref subs (1+ level))) (aref subs level))
  22433. (loop for i from (1+ level) to n do (aset subs i nil)))
  22434. (setq last-level level)))))
  22435. (aref subs 1)))
  22436. (eval-after-load "imenu"
  22437. '(progn
  22438. (add-hook 'imenu-after-jump-hook
  22439. (lambda ()
  22440. (if (derived-mode-p 'org-mode)
  22441. (org-show-context 'org-goto))))))
  22442. (defun org-link-display-format (link)
  22443. "Replace a link with its the description.
  22444. If there is no description, use the link target."
  22445. (save-match-data
  22446. (if (string-match org-bracket-link-analytic-regexp link)
  22447. (replace-match (if (match-end 5)
  22448. (match-string 5 link)
  22449. (concat (match-string 1 link)
  22450. (match-string 3 link)))
  22451. nil t link)
  22452. link)))
  22453. (defun org-toggle-link-display ()
  22454. "Toggle the literal or descriptive display of links."
  22455. (interactive)
  22456. (if org-descriptive-links
  22457. (progn (org-remove-from-invisibility-spec '(org-link))
  22458. (org-restart-font-lock)
  22459. (setq org-descriptive-links nil))
  22460. (progn (add-to-invisibility-spec '(org-link))
  22461. (org-restart-font-lock)
  22462. (setq org-descriptive-links t))))
  22463. ;; Speedbar support
  22464. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22465. "Overlay marking the agenda restriction line in speedbar.")
  22466. (overlay-put org-speedbar-restriction-lock-overlay
  22467. 'face 'org-agenda-restriction-lock)
  22468. (overlay-put org-speedbar-restriction-lock-overlay
  22469. 'help-echo "Agendas are currently limited to this item.")
  22470. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  22471. (defun org-speedbar-set-agenda-restriction ()
  22472. "Restrict future agenda commands to the location at point in speedbar.
  22473. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  22474. (interactive)
  22475. (require 'org-agenda)
  22476. (let (p m tp np dir txt)
  22477. (cond
  22478. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22479. 'org-imenu t))
  22480. (setq m (get-text-property p 'org-imenu-marker))
  22481. (with-current-buffer (marker-buffer m)
  22482. (goto-char m)
  22483. (org-agenda-set-restriction-lock 'subtree)))
  22484. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22485. 'speedbar-function 'speedbar-find-file))
  22486. (setq tp (previous-single-property-change
  22487. (1+ p) 'speedbar-function)
  22488. np (next-single-property-change
  22489. tp 'speedbar-function)
  22490. dir (speedbar-line-directory)
  22491. txt (buffer-substring-no-properties (or tp (point-min))
  22492. (or np (point-max))))
  22493. (with-current-buffer (find-file-noselect
  22494. (let ((default-directory dir))
  22495. (expand-file-name txt)))
  22496. (unless (derived-mode-p 'org-mode)
  22497. (user-error "Cannot restrict to non-Org-mode file"))
  22498. (org-agenda-set-restriction-lock 'file)))
  22499. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  22500. (move-overlay org-speedbar-restriction-lock-overlay
  22501. (point-at-bol) (point-at-eol))
  22502. (setq current-prefix-arg nil)
  22503. (org-agenda-maybe-redo)))
  22504. (defvar speedbar-file-key-map)
  22505. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  22506. (eval-after-load "speedbar"
  22507. '(progn
  22508. (speedbar-add-supported-extension ".org")
  22509. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  22510. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  22511. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  22512. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  22513. (add-hook 'speedbar-visiting-tag-hook
  22514. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  22515. ;;; Fixes and Hacks for problems with other packages
  22516. (defun org-mode-flyspell-verify ()
  22517. "Function used for `flyspell-generic-check-word-predicate'."
  22518. (if (org-at-heading-p)
  22519. ;; At a headline or an inlinetask, check title only. This is
  22520. ;; faster than relying on `org-element-at-point'.
  22521. (and (save-excursion (beginning-of-line)
  22522. (and (let ((case-fold-search t))
  22523. (not (looking-at "\\*+ END[ \t]*$")))
  22524. (looking-at org-complex-heading-regexp)))
  22525. (match-beginning 4)
  22526. (>= (point) (match-beginning 4))
  22527. (or (not (match-beginning 5))
  22528. (< (point) (match-beginning 5))))
  22529. (let* ((element (org-element-at-point))
  22530. (post-affiliated (org-element-property :post-affiliated element))
  22531. (object-check
  22532. (function
  22533. ;; Non-nil if checks can be done for object at point.
  22534. (lambda ()
  22535. (let ((object (save-excursion
  22536. (when (org-looking-at-p "\\>") (backward-char))
  22537. (org-element-context element))))
  22538. (case (org-element-type object)
  22539. ;; Prevent checks in links due to keybinding conflict
  22540. ;; with Flyspell.
  22541. ((code entity export-snippet inline-babel-call
  22542. inline-src-block line-break latex-fragment link macro
  22543. statistics-cookie target timestamp verbatim)
  22544. nil)
  22545. (footnote-reference
  22546. ;; Only in inline footnotes, within the definition.
  22547. (and (eq (org-element-property :type object) 'inline)
  22548. (< (save-excursion
  22549. (goto-char (org-element-property :begin object))
  22550. (search-forward ":" nil t 2))
  22551. (point))))
  22552. (otherwise t)))))))
  22553. (cond
  22554. ;; Ignore checks in all affiliated keywords but captions.
  22555. ((and post-affiliated (< (point) post-affiliated))
  22556. (and (save-excursion
  22557. (beginning-of-line)
  22558. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  22559. (> (point) (match-end 0))
  22560. (funcall object-check)))
  22561. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  22562. ((and org-log-into-drawer
  22563. (let ((log (or (org-string-nw-p org-log-into-drawer) "LOGBOOK"))
  22564. (parent element))
  22565. (while (and parent (not (eq (org-element-type parent) 'drawer)))
  22566. (setq parent (org-element-property :parent parent)))
  22567. (and parent
  22568. (eq (compare-strings
  22569. log nil nil
  22570. (org-element-property :drawer-name parent) nil nil t)
  22571. t))))
  22572. nil)
  22573. (t
  22574. (case (org-element-type element)
  22575. ((comment quote-section) t)
  22576. (comment-block
  22577. ;; Allow checks between block markers, not on them.
  22578. (and (> (line-beginning-position)
  22579. (org-element-property :post-affiliated element))
  22580. (save-excursion
  22581. (end-of-line)
  22582. (skip-chars-forward " \r\t\n")
  22583. (< (point) (org-element-property :end element)))))
  22584. ;; Arbitrary list of keywords where checks are meaningful.
  22585. ;; Make sure point is on the value part of the element.
  22586. (keyword
  22587. (and (member (org-element-property :key element)
  22588. '("DESCRIPTION" "TITLE"))
  22589. (< (save-excursion
  22590. (beginning-of-line) (search-forward ":") (point))
  22591. (point))))
  22592. ;; Check is globally allowed in paragraphs verse blocks and
  22593. ;; table rows (after affiliated keywords) but some objects
  22594. ;; must not be affected.
  22595. ((paragraph table-row verse-block)
  22596. (and (>= (point) (org-element-property :contents-begin element))
  22597. (< (point) (org-element-property :contents-end element))
  22598. (funcall object-check)))))))))
  22599. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  22600. (defun org-remove-flyspell-overlays-in (beg end)
  22601. "Remove flyspell overlays in region."
  22602. (and (org-bound-and-true-p flyspell-mode)
  22603. (fboundp 'flyspell-delete-region-overlays)
  22604. (flyspell-delete-region-overlays beg end)))
  22605. (defvar flyspell-delayed-commands)
  22606. (eval-after-load "flyspell"
  22607. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  22608. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  22609. (eval-after-load "bookmark"
  22610. '(if (boundp 'bookmark-after-jump-hook)
  22611. ;; We can use the hook
  22612. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  22613. ;; Hook not available, use advice
  22614. (defadvice bookmark-jump (after org-make-visible activate)
  22615. "Make the position visible."
  22616. (org-bookmark-jump-unhide))))
  22617. ;; Make sure saveplace shows the location if it was hidden
  22618. (eval-after-load "saveplace"
  22619. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  22620. "Make the position visible."
  22621. (org-bookmark-jump-unhide)))
  22622. ;; Make sure ecb shows the location if it was hidden
  22623. (eval-after-load "ecb"
  22624. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  22625. "Make hierarchy visible when jumping into location from ECB tree buffer."
  22626. (if (derived-mode-p 'org-mode)
  22627. (org-show-context))))
  22628. (defun org-bookmark-jump-unhide ()
  22629. "Unhide the current position, to show the bookmark location."
  22630. (and (derived-mode-p 'org-mode)
  22631. (or (outline-invisible-p)
  22632. (save-excursion (goto-char (max (point-min) (1- (point))))
  22633. (outline-invisible-p)))
  22634. (org-show-context 'bookmark-jump)))
  22635. (defun org-mark-jump-unhide ()
  22636. "Make the point visible with `org-show-context' after jumping to the mark."
  22637. (when (and (derived-mode-p 'org-mode)
  22638. (outline-invisible-p))
  22639. (org-show-context 'mark-goto)))
  22640. (eval-after-load "simple"
  22641. '(defadvice pop-to-mark-command (after org-make-visible activate)
  22642. "Make the point visible with `org-show-context'."
  22643. (org-mark-jump-unhide)))
  22644. (eval-after-load "simple"
  22645. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  22646. "Make the point visible with `org-show-context'."
  22647. (org-mark-jump-unhide)))
  22648. (eval-after-load "simple"
  22649. '(defadvice pop-global-mark (after org-make-visible activate)
  22650. "Make the point visible with `org-show-context'."
  22651. (org-mark-jump-unhide)))
  22652. ;; Make session.el ignore our circular variable
  22653. (defvar session-globals-exclude)
  22654. (eval-after-load "session"
  22655. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  22656. ;;;; Finish up
  22657. (provide 'org)
  22658. (run-hooks 'org-load-hook)
  22659. ;;; org.el ends here