org.el 873 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum))
  70. (require 'calendar)
  71. (require 'find-func)
  72. (require 'format-spec)
  73. (load "org-loaddefs.el" t t t)
  74. (require 'org-macs)
  75. (require 'org-compat)
  76. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  77. ;; some places -- e.g. see the macro org-with-limited-levels.
  78. ;;
  79. ;; In Org buffers, the value of `outline-regexp' is that of
  80. ;; `org-outline-regexp'. The only function still directly relying on
  81. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  82. ;; job when `orgstruct-mode' is active.
  83. (defvar org-outline-regexp "\\*+ "
  84. "Regexp to match Org headlines.")
  85. (defvar org-outline-regexp-bol "^\\*+ "
  86. "Regexp to match Org headlines.
  87. This is similar to `org-outline-regexp' but additionally makes
  88. sure that we are at the beginning of the line.")
  89. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  90. "Matches an headline, putting stars and text into groups.
  91. Stars are put in group 1 and the trimmed body in group 2.")
  92. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  93. (when (fboundp 'defvaralias)
  94. (unless (boundp 'calendar-view-holidays-initially-flag)
  95. (defvaralias 'calendar-view-holidays-initially-flag
  96. 'view-calendar-holidays-initially))
  97. (unless (boundp 'calendar-view-diary-initially-flag)
  98. (defvaralias 'calendar-view-diary-initially-flag
  99. 'view-diary-entries-initially))
  100. (unless (boundp 'diary-fancy-buffer)
  101. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  102. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  103. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  104. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  105. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  106. (declare-function org-at-clock-log-p "org-clock" ())
  107. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  108. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  109. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  110. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  111. (declare-function orgtbl-mode "org-table" (&optional arg))
  112. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  113. (declare-function org-beamer-mode "org-beamer" ())
  114. (declare-function org-table-edit-field "org-table" (arg))
  115. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  116. (declare-function org-id-get-create "org-id" (&optional force))
  117. (declare-function org-id-find-id-file "org-id" (id))
  118. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  119. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  120. (declare-function org-table-align "org-table" ())
  121. (declare-function org-table-paste-rectangle "org-table" ())
  122. (declare-function org-table-maybe-eval-formula "org-table" ())
  123. (declare-function org-table-maybe-recalculate-line "org-table" ())
  124. (autoload 'org-element-at-point "org-element")
  125. (autoload 'org-element-type "org-element")
  126. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  127. (declare-function org-element-type "org-element" (element))
  128. (declare-function org-element-context "org-element" (&optional element))
  129. (declare-function org-element-contents "org-element" (element))
  130. (declare-function org-element-property "org-element" (property element))
  131. (declare-function org-element-put-property "org-element"
  132. (element property value))
  133. (declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion))
  134. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  135. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  136. (declare-function org-element--parse-objects "org-element" (beg end acc restriction))
  137. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  138. ;; load languages based on value of `org-babel-load-languages'
  139. (defvar org-babel-load-languages)
  140. ;;;###autoload
  141. (defun org-babel-do-load-languages (sym value)
  142. "Load the languages defined in `org-babel-load-languages'."
  143. (set-default sym value)
  144. (mapc (lambda (pair)
  145. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  146. (if active
  147. (progn
  148. (require (intern (concat "ob-" lang))))
  149. (progn
  150. (funcall 'fmakunbound
  151. (intern (concat "org-babel-execute:" lang)))
  152. (funcall 'fmakunbound
  153. (intern (concat "org-babel-expand-body:" lang)))))))
  154. org-babel-load-languages))
  155. (defcustom org-babel-load-languages '((emacs-lisp . t))
  156. "Languages which can be evaluated in Org-mode buffers.
  157. This list can be used to load support for any of the languages
  158. below, note that each language will depend on a different set of
  159. system executables and/or Emacs modes. When a language is
  160. \"loaded\", then code blocks in that language can be evaluated
  161. with `org-babel-execute-src-block' bound by default to C-c
  162. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  163. be set to remove code block evaluation from the C-c C-c
  164. keybinding. By default only Emacs Lisp (which has no
  165. requirements) is loaded."
  166. :group 'org-babel
  167. :set 'org-babel-do-load-languages
  168. :version "24.1"
  169. :type '(alist :tag "Babel Languages"
  170. :key-type
  171. (choice
  172. (const :tag "Awk" awk)
  173. (const :tag "C" C)
  174. (const :tag "R" R)
  175. (const :tag "Asymptote" asymptote)
  176. (const :tag "Calc" calc)
  177. (const :tag "Clojure" clojure)
  178. (const :tag "CSS" css)
  179. (const :tag "Ditaa" ditaa)
  180. (const :tag "Dot" dot)
  181. (const :tag "Emacs Lisp" emacs-lisp)
  182. (const :tag "Fortran" fortran)
  183. (const :tag "Gnuplot" gnuplot)
  184. (const :tag "Haskell" haskell)
  185. (const :tag "IO" io)
  186. (const :tag "Java" java)
  187. (const :tag "Javascript" js)
  188. (const :tag "LaTeX" latex)
  189. (const :tag "Ledger" ledger)
  190. (const :tag "Lilypond" lilypond)
  191. (const :tag "Lisp" lisp)
  192. (const :tag "Makefile" makefile)
  193. (const :tag "Maxima" maxima)
  194. (const :tag "Matlab" matlab)
  195. (const :tag "Mscgen" mscgen)
  196. (const :tag "Ocaml" ocaml)
  197. (const :tag "Octave" octave)
  198. (const :tag "Org" org)
  199. (const :tag "Perl" perl)
  200. (const :tag "Pico Lisp" picolisp)
  201. (const :tag "PlantUML" plantuml)
  202. (const :tag "Python" python)
  203. (const :tag "Ruby" ruby)
  204. (const :tag "Sass" sass)
  205. (const :tag "Scala" scala)
  206. (const :tag "Scheme" scheme)
  207. (const :tag "Screen" screen)
  208. (const :tag "Shell Script" sh)
  209. (const :tag "Shen" shen)
  210. (const :tag "Sql" sql)
  211. (const :tag "Sqlite" sqlite))
  212. :value-type (boolean :tag "Activate" :value t)))
  213. ;;;; Customization variables
  214. (defcustom org-clone-delete-id nil
  215. "Remove ID property of clones of a subtree.
  216. When non-nil, clones of a subtree don't inherit the ID property.
  217. Otherwise they inherit the ID property with a new unique
  218. identifier."
  219. :type 'boolean
  220. :version "24.1"
  221. :group 'org-id)
  222. ;;; Version
  223. (org-check-version)
  224. ;;;###autoload
  225. (defun org-version (&optional here full message)
  226. "Show the org-mode version in the echo area.
  227. With prefix argument HERE, insert it at point.
  228. When FULL is non-nil, use a verbose version string.
  229. When MESSAGE is non-nil, display a message with the version."
  230. (interactive "P")
  231. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  232. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  233. (load-suffixes (list ".el"))
  234. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  235. (org-trash (or
  236. (and (fboundp 'org-release) (fboundp 'org-git-version))
  237. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  238. (load-suffixes save-load-suffixes)
  239. (org-version (org-release))
  240. (git-version (org-git-version))
  241. (version (format "Org-mode version %s (%s @ %s)"
  242. org-version
  243. git-version
  244. (if org-install-dir
  245. (if (string= org-dir org-install-dir)
  246. org-install-dir
  247. (concat "mixed installation! " org-install-dir " and " org-dir))
  248. "org-loaddefs.el can not be found!")))
  249. (_version (if full version org-version)))
  250. (if (org-called-interactively-p 'interactive)
  251. (if here
  252. (insert version)
  253. (message version))
  254. (if message (message _version))
  255. _version)))
  256. (defconst org-version (org-version))
  257. ;;; Compatibility constants
  258. ;;; The custom variables
  259. (defgroup org nil
  260. "Outline-based notes management and organizer."
  261. :tag "Org"
  262. :group 'outlines
  263. :group 'calendar)
  264. (defcustom org-mode-hook nil
  265. "Mode hook for Org-mode, run after the mode was turned on."
  266. :group 'org
  267. :type 'hook)
  268. (defcustom org-load-hook nil
  269. "Hook that is run after org.el has been loaded."
  270. :group 'org
  271. :type 'hook)
  272. (defcustom org-log-buffer-setup-hook nil
  273. "Hook that is run after an Org log buffer is created."
  274. :group 'org
  275. :version "24.1"
  276. :type 'hook)
  277. (defvar org-modules) ; defined below
  278. (defvar org-modules-loaded nil
  279. "Have the modules been loaded already?")
  280. (defun org-load-modules-maybe (&optional force)
  281. "Load all extensions listed in `org-modules'."
  282. (when (or force (not org-modules-loaded))
  283. (mapc (lambda (ext)
  284. (condition-case nil (require ext)
  285. (error (message "Problems while trying to load feature `%s'" ext))))
  286. org-modules)
  287. (setq org-modules-loaded t)))
  288. (defun org-set-modules (var value)
  289. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  290. (set var value)
  291. (when (featurep 'org)
  292. (org-load-modules-maybe 'force)))
  293. (when (org-bound-and-true-p org-modules)
  294. (let ((a (member 'org-infojs org-modules)))
  295. (and a (setcar a 'org-jsinfo))))
  296. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  297. "Modules that should always be loaded together with org.el.
  298. If a description starts with <C>, the file is not part of Emacs
  299. and loading it will require that you have downloaded and properly installed
  300. the org-mode distribution.
  301. You can also use this system to load external packages (i.e. neither Org
  302. core modules, nor modules from the CONTRIB directory). Just add symbols
  303. to the end of the list. If the package is called org-xyz.el, then you need
  304. to add the symbol `xyz', and the package must have a call to
  305. (provide 'org-xyz)"
  306. :group 'org
  307. :set 'org-set-modules
  308. :type
  309. '(set :greedy t
  310. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  311. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  312. (const :tag " crypt: Encryption of subtrees" org-crypt)
  313. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  314. (const :tag " docview: Links to doc-view buffers" org-docview)
  315. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  316. (const :tag " id: Global IDs for identifying entries" org-id)
  317. (const :tag " info: Links to Info nodes" org-info)
  318. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  319. (const :tag " habit: Track your consistency with habits" org-habit)
  320. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  321. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  322. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  323. (const :tag " mew Links to Mew folders/messages" org-mew)
  324. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  325. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  326. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  327. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  328. (const :tag " vm: Links to VM folders/messages" org-vm)
  329. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  330. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  331. (const :tag " mouse: Additional mouse support" org-mouse)
  332. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  333. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  334. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  335. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  336. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  337. (const :tag "C collector: Collect properties into tables" org-collector)
  338. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  339. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  340. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  341. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  342. (const :tag "C eval: Include command output as text" org-eval)
  343. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  344. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  345. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  346. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  347. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  348. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  349. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  350. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  351. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  352. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  353. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  354. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  355. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  356. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  357. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  358. (const :tag "C registry: A registry for Org-mode links" org-registry)
  359. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  360. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  361. (const :tag "C secretary: Team management with org-mode" org-secretary)
  362. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  363. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  364. (const :tag "C track: Keep up with Org-mode development" org-track)
  365. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  366. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  367. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  368. (defcustom org-support-shift-select nil
  369. "Non-nil means make shift-cursor commands select text when possible.
  370. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  371. start selecting a region, or enlarge regions started in this way.
  372. In Org-mode, in special contexts, these same keys are used for
  373. other purposes, important enough to compete with shift selection.
  374. Org tries to balance these needs by supporting `shift-select-mode'
  375. outside these special contexts, under control of this variable.
  376. The default of this variable is nil, to avoid confusing behavior. Shifted
  377. cursor keys will then execute Org commands in the following contexts:
  378. - on a headline, changing TODO state (left/right) and priority (up/down)
  379. - on a time stamp, changing the time
  380. - in a plain list item, changing the bullet type
  381. - in a property definition line, switching between allowed values
  382. - in the BEGIN line of a clock table (changing the time block).
  383. Outside these contexts, the commands will throw an error.
  384. When this variable is t and the cursor is not in a special
  385. context, Org-mode will support shift-selection for making and
  386. enlarging regions. To make this more effective, the bullet
  387. cycling will no longer happen anywhere in an item line, but only
  388. if the cursor is exactly on the bullet.
  389. If you set this variable to the symbol `always', then the keys
  390. will not be special in headlines, property lines, and item lines,
  391. to make shift selection work there as well. If this is what you
  392. want, you can use the following alternative commands: `C-c C-t'
  393. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  394. can be used to switch TODO sets, `C-c -' to cycle item bullet
  395. types, and properties can be edited by hand or in column view.
  396. However, when the cursor is on a timestamp, shift-cursor commands
  397. will still edit the time stamp - this is just too good to give up.
  398. XEmacs user should have this variable set to nil, because
  399. `shift-select-mode' is in Emacs 23 or later only."
  400. :group 'org
  401. :type '(choice
  402. (const :tag "Never" nil)
  403. (const :tag "When outside special context" t)
  404. (const :tag "Everywhere except timestamps" always)))
  405. (defcustom org-loop-over-headlines-in-active-region nil
  406. "Shall some commands act upon headlines in the active region?
  407. When set to `t', some commands will be performed in all headlines
  408. within the active region.
  409. When set to `start-level', some commands will be performed in all
  410. headlines within the active region, provided that these headlines
  411. are of the same level than the first one.
  412. When set to a string, those commands will be performed on the
  413. matching headlines within the active region. Such string must be
  414. a tags/property/todo match as it is used in the agenda tags view.
  415. The list of commands is: `org-schedule', `org-deadline',
  416. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  417. `org-archive-to-archive-sibling'. The archiving commands skip
  418. already archived entries."
  419. :type '(choice (const :tag "Don't loop" nil)
  420. (const :tag "All headlines in active region" t)
  421. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  422. (string :tag "Tags/Property/Todo matcher"))
  423. :version "24.1"
  424. :group 'org-todo
  425. :group 'org-archive)
  426. (defgroup org-startup nil
  427. "Options concerning startup of Org-mode."
  428. :tag "Org Startup"
  429. :group 'org)
  430. (defcustom org-startup-folded t
  431. "Non-nil means entering Org-mode will switch to OVERVIEW.
  432. This can also be configured on a per-file basis by adding one of
  433. the following lines anywhere in the buffer:
  434. #+STARTUP: fold (or `overview', this is equivalent)
  435. #+STARTUP: nofold (or `showall', this is equivalent)
  436. #+STARTUP: content
  437. #+STARTUP: showeverything"
  438. :group 'org-startup
  439. :type '(choice
  440. (const :tag "nofold: show all" nil)
  441. (const :tag "fold: overview" t)
  442. (const :tag "content: all headlines" content)
  443. (const :tag "show everything, even drawers" showeverything)))
  444. (defcustom org-startup-truncated t
  445. "Non-nil means entering Org-mode will set `truncate-lines'.
  446. This is useful since some lines containing links can be very long and
  447. uninteresting. Also tables look terrible when wrapped."
  448. :group 'org-startup
  449. :type 'boolean)
  450. (defcustom org-startup-indented nil
  451. "Non-nil means turn on `org-indent-mode' on startup.
  452. This can also be configured on a per-file basis by adding one of
  453. the following lines anywhere in the buffer:
  454. #+STARTUP: indent
  455. #+STARTUP: noindent"
  456. :group 'org-structure
  457. :type '(choice
  458. (const :tag "Not" nil)
  459. (const :tag "Globally (slow on startup in large files)" t)))
  460. (defcustom org-use-sub-superscripts t
  461. "Non-nil means interpret \"_\" and \"^\" for export.
  462. When this option is turned on, you can use TeX-like syntax for sub- and
  463. superscripts. Several characters after \"_\" or \"^\" will be
  464. considered as a single item - so grouping with {} is normally not
  465. needed. For example, the following things will be parsed as single
  466. sub- or superscripts.
  467. 10^24 or 10^tau several digits will be considered 1 item.
  468. 10^-12 or 10^-tau a leading sign with digits or a word
  469. x^2-y^3 will be read as x^2 - y^3, because items are
  470. terminated by almost any nonword/nondigit char.
  471. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  472. Still, ambiguity is possible - so when in doubt use {} to enclose the
  473. sub/superscript. If you set this variable to the symbol `{}',
  474. the braces are *required* in order to trigger interpretations as
  475. sub/superscript. This can be helpful in documents that need \"_\"
  476. frequently in plain text.
  477. Not all export backends support this, but HTML does.
  478. This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
  479. :group 'org-startup
  480. :group 'org-export-translation
  481. :version "24.1"
  482. :type '(choice
  483. (const :tag "Always interpret" t)
  484. (const :tag "Only with braces" {})
  485. (const :tag "Never interpret" nil)))
  486. (if (fboundp 'defvaralias)
  487. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  488. (defcustom org-startup-with-beamer-mode nil
  489. "Non-nil means turn on `org-beamer-mode' on startup.
  490. This can also be configured on a per-file basis by adding one of
  491. the following lines anywhere in the buffer:
  492. #+STARTUP: beamer"
  493. :group 'org-startup
  494. :version "24.1"
  495. :type 'boolean)
  496. (defcustom org-startup-align-all-tables nil
  497. "Non-nil means align all tables when visiting a file.
  498. This is useful when the column width in tables is forced with <N> cookies
  499. in table fields. Such tables will look correct only after the first re-align.
  500. This can also be configured on a per-file basis by adding one of
  501. the following lines anywhere in the buffer:
  502. #+STARTUP: align
  503. #+STARTUP: noalign"
  504. :group 'org-startup
  505. :type 'boolean)
  506. (defcustom org-startup-with-inline-images nil
  507. "Non-nil means show inline images when loading a new Org file.
  508. This can also be configured on a per-file basis by adding one of
  509. the following lines anywhere in the buffer:
  510. #+STARTUP: inlineimages
  511. #+STARTUP: noinlineimages"
  512. :group 'org-startup
  513. :version "24.1"
  514. :type 'boolean)
  515. (defcustom org-insert-mode-line-in-empty-file nil
  516. "Non-nil means insert the first line setting Org-mode in empty files.
  517. When the function `org-mode' is called interactively in an empty file, this
  518. normally means that the file name does not automatically trigger Org-mode.
  519. To ensure that the file will always be in Org-mode in the future, a
  520. line enforcing Org-mode will be inserted into the buffer, if this option
  521. has been set."
  522. :group 'org-startup
  523. :type 'boolean)
  524. (defcustom org-replace-disputed-keys nil
  525. "Non-nil means use alternative key bindings for some keys.
  526. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  527. These keys are also used by other packages like shift-selection-mode'
  528. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  529. If you want to use Org-mode together with one of these other modes,
  530. or more generally if you would like to move some Org-mode commands to
  531. other keys, set this variable and configure the keys with the variable
  532. `org-disputed-keys'.
  533. This option is only relevant at load-time of Org-mode, and must be set
  534. *before* org.el is loaded. Changing it requires a restart of Emacs to
  535. become effective."
  536. :group 'org-startup
  537. :type 'boolean)
  538. (defcustom org-use-extra-keys nil
  539. "Non-nil means use extra key sequence definitions for certain commands.
  540. This happens automatically if you run XEmacs or if `window-system'
  541. is nil. This variable lets you do the same manually. You must
  542. set it before loading org.
  543. Example: on Carbon Emacs 22 running graphically, with an external
  544. keyboard on a Powerbook, the default way of setting M-left might
  545. not work for either Alt or ESC. Setting this variable will make
  546. it work for ESC."
  547. :group 'org-startup
  548. :type 'boolean)
  549. (if (fboundp 'defvaralias)
  550. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  551. (defcustom org-disputed-keys
  552. '(([(shift up)] . [(meta p)])
  553. ([(shift down)] . [(meta n)])
  554. ([(shift left)] . [(meta -)])
  555. ([(shift right)] . [(meta +)])
  556. ([(control shift right)] . [(meta shift +)])
  557. ([(control shift left)] . [(meta shift -)]))
  558. "Keys for which Org-mode and other modes compete.
  559. This is an alist, cars are the default keys, second element specifies
  560. the alternative to use when `org-replace-disputed-keys' is t.
  561. Keys can be specified in any syntax supported by `define-key'.
  562. The value of this option takes effect only at Org-mode's startup,
  563. therefore you'll have to restart Emacs to apply it after changing."
  564. :group 'org-startup
  565. :type 'alist)
  566. (defun org-key (key)
  567. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  568. Or return the original if not disputed.
  569. Also apply the translations defined in `org-xemacs-key-equivalents'."
  570. (when org-replace-disputed-keys
  571. (let* ((nkey (key-description key))
  572. (x (org-find-if (lambda (x)
  573. (equal (key-description (car x)) nkey))
  574. org-disputed-keys)))
  575. (setq key (if x (cdr x) key))))
  576. (when (featurep 'xemacs)
  577. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  578. key)
  579. (defun org-find-if (predicate seq)
  580. (catch 'exit
  581. (while seq
  582. (if (funcall predicate (car seq))
  583. (throw 'exit (car seq))
  584. (pop seq)))))
  585. (defun org-defkey (keymap key def)
  586. "Define a key, possibly translated, as returned by `org-key'."
  587. (define-key keymap (org-key key) def))
  588. (defcustom org-ellipsis nil
  589. "The ellipsis to use in the Org-mode outline.
  590. When nil, just use the standard three dots. When a string, use that instead,
  591. When a face, use the standard 3 dots, but with the specified face.
  592. The change affects only Org-mode (which will then use its own display table).
  593. Changing this requires executing `M-x org-mode' in a buffer to become
  594. effective."
  595. :group 'org-startup
  596. :type '(choice (const :tag "Default" nil)
  597. (face :tag "Face" :value org-warning)
  598. (string :tag "String" :value "...#")))
  599. (defvar org-display-table nil
  600. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  601. (defgroup org-keywords nil
  602. "Keywords in Org-mode."
  603. :tag "Org Keywords"
  604. :group 'org)
  605. (defcustom org-deadline-string "DEADLINE:"
  606. "String to mark deadline entries.
  607. A deadline is this string, followed by a time stamp. Should be a word,
  608. terminated by a colon. You can insert a schedule keyword and
  609. a timestamp with \\[org-deadline].
  610. Changes become only effective after restarting Emacs."
  611. :group 'org-keywords
  612. :type 'string)
  613. (defcustom org-scheduled-string "SCHEDULED:"
  614. "String to mark scheduled TODO entries.
  615. A schedule is this string, followed by a time stamp. Should be a word,
  616. terminated by a colon. You can insert a schedule keyword and
  617. a timestamp with \\[org-schedule].
  618. Changes become only effective after restarting Emacs."
  619. :group 'org-keywords
  620. :type 'string)
  621. (defcustom org-closed-string "CLOSED:"
  622. "String used as the prefix for timestamps logging closing a TODO entry."
  623. :group 'org-keywords
  624. :type 'string)
  625. (defcustom org-clock-string "CLOCK:"
  626. "String used as prefix for timestamps clocking work hours on an item."
  627. :group 'org-keywords
  628. :type 'string)
  629. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  630. org-scheduled-string "\\|"
  631. org-deadline-string "\\|"
  632. org-closed-string "\\|"
  633. org-clock-string "\\)")
  634. "Matches a line with planning or clock info.")
  635. (defcustom org-comment-string "COMMENT"
  636. "Entries starting with this keyword will never be exported.
  637. An entry can be toggled between COMMENT and normal with
  638. \\[org-toggle-comment].
  639. Changes become only effective after restarting Emacs."
  640. :group 'org-keywords
  641. :type 'string)
  642. (defcustom org-quote-string "QUOTE"
  643. "Entries starting with this keyword will be exported in fixed-width font.
  644. Quoting applies only to the text in the entry following the headline, and does
  645. not extend beyond the next headline, even if that is lower level.
  646. An entry can be toggled between QUOTE and normal with
  647. \\[org-toggle-fixed-width-section]."
  648. :group 'org-keywords
  649. :type 'string)
  650. (defconst org-repeat-re
  651. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  652. "Regular expression for specifying repeated events.
  653. After a match, group 1 contains the repeat expression.")
  654. (defgroup org-structure nil
  655. "Options concerning the general structure of Org-mode files."
  656. :tag "Org Structure"
  657. :group 'org)
  658. (defgroup org-reveal-location nil
  659. "Options about how to make context of a location visible."
  660. :tag "Org Reveal Location"
  661. :group 'org-structure)
  662. (defconst org-context-choice
  663. '(choice
  664. (const :tag "Always" t)
  665. (const :tag "Never" nil)
  666. (repeat :greedy t :tag "Individual contexts"
  667. (cons
  668. (choice :tag "Context"
  669. (const agenda)
  670. (const org-goto)
  671. (const occur-tree)
  672. (const tags-tree)
  673. (const link-search)
  674. (const mark-goto)
  675. (const bookmark-jump)
  676. (const isearch)
  677. (const default))
  678. (boolean))))
  679. "Contexts for the reveal options.")
  680. (defcustom org-show-hierarchy-above '((default . t))
  681. "Non-nil means show full hierarchy when revealing a location.
  682. Org-mode often shows locations in an org-mode file which might have
  683. been invisible before. When this is set, the hierarchy of headings
  684. above the exposed location is shown.
  685. Turning this off for example for sparse trees makes them very compact.
  686. Instead of t, this can also be an alist specifying this option for different
  687. contexts. Valid contexts are
  688. agenda when exposing an entry from the agenda
  689. org-goto when using the command `org-goto' on key C-c C-j
  690. occur-tree when using the command `org-occur' on key C-c /
  691. tags-tree when constructing a sparse tree based on tags matches
  692. link-search when exposing search matches associated with a link
  693. mark-goto when exposing the jump goal of a mark
  694. bookmark-jump when exposing a bookmark location
  695. isearch when exiting from an incremental search
  696. default default for all contexts not set explicitly"
  697. :group 'org-reveal-location
  698. :type org-context-choice)
  699. (defcustom org-show-following-heading '((default . nil))
  700. "Non-nil means show following heading when revealing a location.
  701. Org-mode often shows locations in an org-mode file which might have
  702. been invisible before. When this is set, the heading following the
  703. match is shown.
  704. Turning this off for example for sparse trees makes them very compact,
  705. but makes it harder to edit the location of the match. In such a case,
  706. use the command \\[org-reveal] to show more context.
  707. Instead of t, this can also be an alist specifying this option for different
  708. contexts. See `org-show-hierarchy-above' for valid contexts."
  709. :group 'org-reveal-location
  710. :type org-context-choice)
  711. (defcustom org-show-siblings '((default . nil) (isearch t))
  712. "Non-nil means show all sibling heading when revealing a location.
  713. Org-mode often shows locations in an org-mode file which might have
  714. been invisible before. When this is set, the sibling of the current entry
  715. heading are all made visible. If `org-show-hierarchy-above' is t,
  716. the same happens on each level of the hierarchy above the current entry.
  717. By default this is on for the isearch context, off for all other contexts.
  718. Turning this off for example for sparse trees makes them very compact,
  719. but makes it harder to edit the location of the match. In such a case,
  720. use the command \\[org-reveal] to show more context.
  721. Instead of t, this can also be an alist specifying this option for different
  722. contexts. See `org-show-hierarchy-above' for valid contexts."
  723. :group 'org-reveal-location
  724. :type org-context-choice)
  725. (defcustom org-show-entry-below '((default . nil))
  726. "Non-nil means show the entry below a headline when revealing a location.
  727. Org-mode often shows locations in an org-mode file which might have
  728. been invisible before. When this is set, the text below the headline that is
  729. exposed is also shown.
  730. By default this is off for all contexts.
  731. Instead of t, this can also be an alist specifying this option for different
  732. contexts. See `org-show-hierarchy-above' for valid contexts."
  733. :group 'org-reveal-location
  734. :type org-context-choice)
  735. (defcustom org-indirect-buffer-display 'other-window
  736. "How should indirect tree buffers be displayed?
  737. This applies to indirect buffers created with the commands
  738. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  739. Valid values are:
  740. current-window Display in the current window
  741. other-window Just display in another window.
  742. dedicated-frame Create one new frame, and re-use it each time.
  743. new-frame Make a new frame each time. Note that in this case
  744. previously-made indirect buffers are kept, and you need to
  745. kill these buffers yourself."
  746. :group 'org-structure
  747. :group 'org-agenda-windows
  748. :type '(choice
  749. (const :tag "In current window" current-window)
  750. (const :tag "In current frame, other window" other-window)
  751. (const :tag "Each time a new frame" new-frame)
  752. (const :tag "One dedicated frame" dedicated-frame)))
  753. (defcustom org-use-speed-commands nil
  754. "Non-nil means activate single letter commands at beginning of a headline.
  755. This may also be a function to test for appropriate locations where speed
  756. commands should be active."
  757. :group 'org-structure
  758. :type '(choice
  759. (const :tag "Never" nil)
  760. (const :tag "At beginning of headline stars" t)
  761. (function)))
  762. (defcustom org-speed-commands-user nil
  763. "Alist of additional speed commands.
  764. This list will be checked before `org-speed-commands-default'
  765. when the variable `org-use-speed-commands' is non-nil
  766. and when the cursor is at the beginning of a headline.
  767. The car if each entry is a string with a single letter, which must
  768. be assigned to `self-insert-command' in the global map.
  769. The cdr is either a command to be called interactively, a function
  770. to be called, or a form to be evaluated.
  771. An entry that is just a list with a single string will be interpreted
  772. as a descriptive headline that will be added when listing the speed
  773. commands in the Help buffer using the `?' speed command."
  774. :group 'org-structure
  775. :type '(repeat :value ("k" . ignore)
  776. (choice :value ("k" . ignore)
  777. (list :tag "Descriptive Headline" (string :tag "Headline"))
  778. (cons :tag "Letter and Command"
  779. (string :tag "Command letter")
  780. (choice
  781. (function)
  782. (sexp))))))
  783. (defgroup org-cycle nil
  784. "Options concerning visibility cycling in Org-mode."
  785. :tag "Org Cycle"
  786. :group 'org-structure)
  787. (defcustom org-cycle-skip-children-state-if-no-children t
  788. "Non-nil means skip CHILDREN state in entries that don't have any."
  789. :group 'org-cycle
  790. :type 'boolean)
  791. (defcustom org-cycle-max-level nil
  792. "Maximum level which should still be subject to visibility cycling.
  793. Levels higher than this will, for cycling, be treated as text, not a headline.
  794. When `org-odd-levels-only' is set, a value of N in this variable actually
  795. means 2N-1 stars as the limiting headline.
  796. When nil, cycle all levels.
  797. Note that the limiting level of cycling is also influenced by
  798. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  799. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  800. than its value."
  801. :group 'org-cycle
  802. :type '(choice
  803. (const :tag "No limit" nil)
  804. (integer :tag "Maximum level")))
  805. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  806. "Names of drawers. Drawers are not opened by cycling on the headline above.
  807. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  808. this:
  809. :DRAWERNAME:
  810. .....
  811. :END:
  812. The drawer \"PROPERTIES\" is special for capturing properties through
  813. the property API.
  814. Drawers can be defined on the per-file basis with a line like:
  815. #+DRAWERS: HIDDEN STATE PROPERTIES"
  816. :group 'org-structure
  817. :group 'org-cycle
  818. :type '(repeat (string :tag "Drawer Name")))
  819. (defcustom org-hide-block-startup nil
  820. "Non-nil means entering Org-mode will fold all blocks.
  821. This can also be set in on a per-file basis with
  822. #+STARTUP: hideblocks
  823. #+STARTUP: showblocks"
  824. :group 'org-startup
  825. :group 'org-cycle
  826. :type 'boolean)
  827. (defcustom org-cycle-global-at-bob nil
  828. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  829. This makes it possible to do global cycling without having to use S-TAB or
  830. \\[universal-argument] TAB. For this special case to work, the first line
  831. of the buffer must not be a headline -- it may be empty or some other text.
  832. When used in this way, `org-cycle-hook' is disabled temporarily to make
  833. sure the cursor stays at the beginning of the buffer. When this option is
  834. nil, don't do anything special at the beginning of the buffer."
  835. :group 'org-cycle
  836. :type 'boolean)
  837. (defcustom org-cycle-level-after-item/entry-creation t
  838. "Non-nil means cycle entry level or item indentation in new empty entries.
  839. When the cursor is at the end of an empty headline, i.e with only stars
  840. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  841. and then all possible ancestor states, before returning to the original state.
  842. This makes data entry extremely fast: M-RET to create a new headline,
  843. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  844. When the cursor is at the end of an empty plain list item, one TAB will
  845. make it a subitem, two or more tabs will back up to make this an item
  846. higher up in the item hierarchy."
  847. :group 'org-cycle
  848. :type 'boolean)
  849. (defcustom org-cycle-emulate-tab t
  850. "Where should `org-cycle' emulate TAB.
  851. nil Never
  852. white Only in completely white lines
  853. whitestart Only at the beginning of lines, before the first non-white char
  854. t Everywhere except in headlines
  855. exc-hl-bol Everywhere except at the start of a headline
  856. If TAB is used in a place where it does not emulate TAB, the current subtree
  857. visibility is cycled."
  858. :group 'org-cycle
  859. :type '(choice (const :tag "Never" nil)
  860. (const :tag "Only in completely white lines" white)
  861. (const :tag "Before first char in a line" whitestart)
  862. (const :tag "Everywhere except in headlines" t)
  863. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  864. ))
  865. (defcustom org-cycle-separator-lines 2
  866. "Number of empty lines needed to keep an empty line between collapsed trees.
  867. If you leave an empty line between the end of a subtree and the following
  868. headline, this empty line is hidden when the subtree is folded.
  869. Org-mode will leave (exactly) one empty line visible if the number of
  870. empty lines is equal or larger to the number given in this variable.
  871. So the default 2 means at least 2 empty lines after the end of a subtree
  872. are needed to produce free space between a collapsed subtree and the
  873. following headline.
  874. If the number is negative, and the number of empty lines is at least -N,
  875. all empty lines are shown.
  876. Special case: when 0, never leave empty lines in collapsed view."
  877. :group 'org-cycle
  878. :type 'integer)
  879. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  880. (defcustom org-pre-cycle-hook nil
  881. "Hook that is run before visibility cycling is happening.
  882. The function(s) in this hook must accept a single argument which indicates
  883. the new state that will be set right after running this hook. The
  884. argument is a symbol. Before a global state change, it can have the values
  885. `overview', `content', or `all'. Before a local state change, it can have
  886. the values `folded', `children', or `subtree'."
  887. :group 'org-cycle
  888. :type 'hook)
  889. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  890. org-cycle-hide-drawers
  891. org-cycle-hide-inline-tasks
  892. org-cycle-show-empty-lines
  893. org-optimize-window-after-visibility-change)
  894. "Hook that is run after `org-cycle' has changed the buffer visibility.
  895. The function(s) in this hook must accept a single argument which indicates
  896. the new state that was set by the most recent `org-cycle' command. The
  897. argument is a symbol. After a global state change, it can have the values
  898. `overview', `contents', or `all'. After a local state change, it can have
  899. the values `folded', `children', or `subtree'."
  900. :group 'org-cycle
  901. :type 'hook)
  902. (defgroup org-edit-structure nil
  903. "Options concerning structure editing in Org-mode."
  904. :tag "Org Edit Structure"
  905. :group 'org-structure)
  906. (defcustom org-odd-levels-only nil
  907. "Non-nil means skip even levels and only use odd levels for the outline.
  908. This has the effect that two stars are being added/taken away in
  909. promotion/demotion commands. It also influences how levels are
  910. handled by the exporters.
  911. Changing it requires restart of `font-lock-mode' to become effective
  912. for fontification also in regions already fontified.
  913. You may also set this on a per-file basis by adding one of the following
  914. lines to the buffer:
  915. #+STARTUP: odd
  916. #+STARTUP: oddeven"
  917. :group 'org-edit-structure
  918. :group 'org-appearance
  919. :type 'boolean)
  920. (defcustom org-adapt-indentation t
  921. "Non-nil means adapt indentation to outline node level.
  922. When this variable is set, Org assumes that you write outlines by
  923. indenting text in each node to align with the headline (after the stars).
  924. The following issues are influenced by this variable:
  925. - When this is set and the *entire* text in an entry is indented, the
  926. indentation is increased by one space in a demotion command, and
  927. decreased by one in a promotion command. If any line in the entry
  928. body starts with text at column 0, indentation is not changed at all.
  929. - Property drawers and planning information is inserted indented when
  930. this variable s set. When nil, they will not be indented.
  931. - TAB indents a line relative to context. The lines below a headline
  932. will be indented when this variable is set.
  933. Note that this is all about true indentation, by adding and removing
  934. space characters. See also `org-indent.el' which does level-dependent
  935. indentation in a virtual way, i.e. at display time in Emacs."
  936. :group 'org-edit-structure
  937. :type 'boolean)
  938. (defcustom org-special-ctrl-a/e nil
  939. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  940. When t, `C-a' will bring back the cursor to the beginning of the
  941. headline text, i.e. after the stars and after a possible TODO
  942. keyword. In an item, this will be the position after bullet and
  943. check-box, if any. When the cursor is already at that position,
  944. another `C-a' will bring it to the beginning of the line.
  945. `C-e' will jump to the end of the headline, ignoring the presence
  946. of tags in the headline. A second `C-e' will then jump to the
  947. true end of the line, after any tags. This also means that, when
  948. this variable is non-nil, `C-e' also will never jump beyond the
  949. end of the heading of a folded section, i.e. not after the
  950. ellipses.
  951. When set to the symbol `reversed', the first `C-a' or `C-e' works
  952. normally, going to the true line boundary first. Only a directly
  953. following, identical keypress will bring the cursor to the
  954. special positions.
  955. This may also be a cons cell where the behavior for `C-a' and
  956. `C-e' is set separately."
  957. :group 'org-edit-structure
  958. :type '(choice
  959. (const :tag "off" nil)
  960. (const :tag "on: after stars/bullet and before tags first" t)
  961. (const :tag "reversed: true line boundary first" reversed)
  962. (cons :tag "Set C-a and C-e separately"
  963. (choice :tag "Special C-a"
  964. (const :tag "off" nil)
  965. (const :tag "on: after stars/bullet first" t)
  966. (const :tag "reversed: before stars/bullet first" reversed))
  967. (choice :tag "Special C-e"
  968. (const :tag "off" nil)
  969. (const :tag "on: before tags first" t)
  970. (const :tag "reversed: after tags first" reversed)))))
  971. (if (fboundp 'defvaralias)
  972. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  973. (defcustom org-special-ctrl-k nil
  974. "Non-nil means `C-k' will behave specially in headlines.
  975. When nil, `C-k' will call the default `kill-line' command.
  976. When t, the following will happen while the cursor is in the headline:
  977. - When the cursor is at the beginning of a headline, kill the entire
  978. line and possible the folded subtree below the line.
  979. - When in the middle of the headline text, kill the headline up to the tags.
  980. - When after the headline text, kill the tags."
  981. :group 'org-edit-structure
  982. :type 'boolean)
  983. (defcustom org-ctrl-k-protect-subtree nil
  984. "Non-nil means, do not delete a hidden subtree with C-k.
  985. When set to the symbol `error', simply throw an error when C-k is
  986. used to kill (part-of) a headline that has hidden text behind it.
  987. Any other non-nil value will result in a query to the user, if it is
  988. OK to kill that hidden subtree. When nil, kill without remorse."
  989. :group 'org-edit-structure
  990. :version "24.1"
  991. :type '(choice
  992. (const :tag "Do not protect hidden subtrees" nil)
  993. (const :tag "Protect hidden subtrees with a security query" t)
  994. (const :tag "Never kill a hidden subtree with C-k" error)))
  995. (defcustom org-catch-invisible-edits nil
  996. "Check if in invisible region before inserting or deleting a character.
  997. Valid values are:
  998. nil Do not check, so just do invisible edits.
  999. error Throw an error and do nothing.
  1000. show Make point visible, and do the requested edit.
  1001. show-and-error Make point visible, then throw an error and abort the edit.
  1002. smart Make point visible, and do insertion/deletion if it is
  1003. adjacent to visible text and the change feels predictable.
  1004. Never delete a previously invisible character or add in the
  1005. middle or right after an invisible region. Basically, this
  1006. allows insertion and backward-delete right before ellipses.
  1007. FIXME: maybe in this case we should not even show?"
  1008. :group 'org-edit-structure
  1009. :version "24.1"
  1010. :type '(choice
  1011. (const :tag "Do not check" nil)
  1012. (const :tag "Throw error when trying to edit" error)
  1013. (const :tag "Unhide, but do not do the edit" show-and-error)
  1014. (const :tag "Show invisible part and do the edit" show)
  1015. (const :tag "Be smart and do the right thing" smart)))
  1016. (defcustom org-yank-folded-subtrees t
  1017. "Non-nil means when yanking subtrees, fold them.
  1018. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1019. it starts with a heading and all other headings in it are either children
  1020. or siblings, then fold all the subtrees. However, do this only if no
  1021. text after the yank would be swallowed into a folded tree by this action."
  1022. :group 'org-edit-structure
  1023. :type 'boolean)
  1024. (defcustom org-yank-adjusted-subtrees nil
  1025. "Non-nil means when yanking subtrees, adjust the level.
  1026. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1027. this function for details."
  1028. :group 'org-edit-structure
  1029. :type 'boolean)
  1030. (defcustom org-M-RET-may-split-line '((default . t))
  1031. "Non-nil means M-RET will split the line at the cursor position.
  1032. When nil, it will go to the end of the line before making a
  1033. new line.
  1034. You may also set this option in a different way for different
  1035. contexts. Valid contexts are:
  1036. headline when creating a new headline
  1037. item when creating a new item
  1038. table in a table field
  1039. default the value to be used for all contexts not explicitly
  1040. customized"
  1041. :group 'org-structure
  1042. :group 'org-table
  1043. :type '(choice
  1044. (const :tag "Always" t)
  1045. (const :tag "Never" nil)
  1046. (repeat :greedy t :tag "Individual contexts"
  1047. (cons
  1048. (choice :tag "Context"
  1049. (const headline)
  1050. (const item)
  1051. (const table)
  1052. (const default))
  1053. (boolean)))))
  1054. (defcustom org-insert-heading-respect-content nil
  1055. "Non-nil means insert new headings after the current subtree.
  1056. When nil, the new heading is created directly after the current line.
  1057. The commands \\[org-insert-heading-respect-content] and
  1058. \\[org-insert-todo-heading-respect-content] turn this variable on
  1059. for the duration of the command."
  1060. :group 'org-structure
  1061. :type 'boolean)
  1062. (defcustom org-blank-before-new-entry '((heading . auto)
  1063. (plain-list-item . auto))
  1064. "Should `org-insert-heading' leave a blank line before new heading/item?
  1065. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1066. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1067. which case Org will look at the surrounding headings/items and try to
  1068. make an intelligent decision whether to insert a blank line or not.
  1069. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1070. set, the setting here is ignored and no empty line is inserted, to avoid
  1071. breaking the list structure."
  1072. :group 'org-edit-structure
  1073. :type '(list
  1074. (cons (const heading)
  1075. (choice (const :tag "Never" nil)
  1076. (const :tag "Always" t)
  1077. (const :tag "Auto" auto)))
  1078. (cons (const plain-list-item)
  1079. (choice (const :tag "Never" nil)
  1080. (const :tag "Always" t)
  1081. (const :tag "Auto" auto)))))
  1082. (defcustom org-insert-heading-hook nil
  1083. "Hook being run after inserting a new heading."
  1084. :group 'org-edit-structure
  1085. :type 'hook)
  1086. (defcustom org-enable-fixed-width-editor t
  1087. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1088. This currently only means they are never auto-wrapped.
  1089. When nil, such lines will be treated like ordinary lines.
  1090. See also the QUOTE keyword."
  1091. :group 'org-edit-structure
  1092. :type 'boolean)
  1093. (defcustom org-goto-auto-isearch t
  1094. "Non-nil means typing characters in `org-goto' starts incremental search.
  1095. When nil, you can use these keybindings to navigate the buffer:
  1096. q Quit the org-goto interface
  1097. n Go to the next visible heading
  1098. p Go to the previous visible heading
  1099. f Go one heading forward on same level
  1100. b Go one heading backward on same level
  1101. u Go one heading up"
  1102. :group 'org-edit-structure
  1103. :type 'boolean)
  1104. (defgroup org-sparse-trees nil
  1105. "Options concerning sparse trees in Org-mode."
  1106. :tag "Org Sparse Trees"
  1107. :group 'org-structure)
  1108. (defcustom org-highlight-sparse-tree-matches t
  1109. "Non-nil means highlight all matches that define a sparse tree.
  1110. The highlights will automatically disappear the next time the buffer is
  1111. changed by an edit command."
  1112. :group 'org-sparse-trees
  1113. :type 'boolean)
  1114. (defcustom org-remove-highlights-with-change t
  1115. "Non-nil means any change to the buffer will remove temporary highlights.
  1116. Such highlights are created by `org-occur' and `org-clock-display'.
  1117. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1118. The highlights created by `org-preview-latex-fragment' always need
  1119. `C-c C-c' to be removed."
  1120. :group 'org-sparse-trees
  1121. :group 'org-time
  1122. :type 'boolean)
  1123. (defcustom org-occur-hook '(org-first-headline-recenter)
  1124. "Hook that is run after `org-occur' has constructed a sparse tree.
  1125. This can be used to recenter the window to show as much of the structure
  1126. as possible."
  1127. :group 'org-sparse-trees
  1128. :type 'hook)
  1129. (defgroup org-imenu-and-speedbar nil
  1130. "Options concerning imenu and speedbar in Org-mode."
  1131. :tag "Org Imenu and Speedbar"
  1132. :group 'org-structure)
  1133. (defcustom org-imenu-depth 2
  1134. "The maximum level for Imenu access to Org-mode headlines.
  1135. This also applied for speedbar access."
  1136. :group 'org-imenu-and-speedbar
  1137. :type 'integer)
  1138. (defgroup org-table nil
  1139. "Options concerning tables in Org-mode."
  1140. :tag "Org Table"
  1141. :group 'org)
  1142. (defcustom org-enable-table-editor 'optimized
  1143. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1144. When nil, such lines will be treated like ordinary lines.
  1145. When equal to the symbol `optimized', the table editor will be optimized to
  1146. do the following:
  1147. - Automatic overwrite mode in front of whitespace in table fields.
  1148. This makes the structure of the table stay in tact as long as the edited
  1149. field does not exceed the column width.
  1150. - Minimize the number of realigns. Normally, the table is aligned each time
  1151. TAB or RET are pressed to move to another field. With optimization this
  1152. happens only if changes to a field might have changed the column width.
  1153. Optimization requires replacing the functions `self-insert-command',
  1154. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1155. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1156. very good at guessing when a re-align will be necessary, but you can always
  1157. force one with \\[org-ctrl-c-ctrl-c].
  1158. If you would like to use the optimized version in Org-mode, but the
  1159. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1160. This variable can be used to turn on and off the table editor during a session,
  1161. but in order to toggle optimization, a restart is required.
  1162. See also the variable `org-table-auto-blank-field'."
  1163. :group 'org-table
  1164. :type '(choice
  1165. (const :tag "off" nil)
  1166. (const :tag "on" t)
  1167. (const :tag "on, optimized" optimized)))
  1168. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1169. (version<= emacs-version "24.1"))
  1170. "Non-nil means cluster self-insert commands for undo when possible.
  1171. If this is set, then, like in the Emacs command loop, 20 consecutive
  1172. characters will be undone together.
  1173. This is configurable, because there is some impact on typing performance."
  1174. :group 'org-table
  1175. :type 'boolean)
  1176. (defcustom org-table-tab-recognizes-table.el t
  1177. "Non-nil means TAB will automatically notice a table.el table.
  1178. When it sees such a table, it moves point into it and - if necessary -
  1179. calls `table-recognize-table'."
  1180. :group 'org-table-editing
  1181. :type 'boolean)
  1182. (defgroup org-link nil
  1183. "Options concerning links in Org-mode."
  1184. :tag "Org Link"
  1185. :group 'org)
  1186. (defvar org-link-abbrev-alist-local nil
  1187. "Buffer-local version of `org-link-abbrev-alist', which see.
  1188. The value of this is taken from the #+LINK lines.")
  1189. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1190. (defcustom org-link-abbrev-alist nil
  1191. "Alist of link abbreviations.
  1192. The car of each element is a string, to be replaced at the start of a link.
  1193. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1194. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1195. [[linkkey:tag][description]]
  1196. The 'linkkey' must be a word word, starting with a letter, followed
  1197. by letters, numbers, '-' or '_'.
  1198. If REPLACE is a string, the tag will simply be appended to create the link.
  1199. If the string contains \"%s\", the tag will be inserted there. If the string
  1200. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1201. at that point (see the function `url-hexify-string'). If the string contains
  1202. the specifier \"%(my-function)\", then the custom function `my-function' will
  1203. be invoked: this function takes the tag as its only argument and must return
  1204. a string.
  1205. REPLACE may also be a function that will be called with the tag as the
  1206. only argument to create the link, which should be returned as a string.
  1207. See the manual for examples."
  1208. :group 'org-link
  1209. :type '(repeat
  1210. (cons
  1211. (string :tag "Protocol")
  1212. (choice
  1213. (string :tag "Format")
  1214. (function)))))
  1215. (defcustom org-descriptive-links t
  1216. "Non-nil means Org will display descriptive links.
  1217. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1218. \"Org Website\", hiding the link itself and just displaying its
  1219. description. When set to `nil', Org will display the full links
  1220. literally.
  1221. You can interactively set the value of this variable by calling
  1222. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1223. :group 'org-link
  1224. :type 'boolean)
  1225. (defcustom org-link-file-path-type 'adaptive
  1226. "How the path name in file links should be stored.
  1227. Valid values are:
  1228. relative Relative to the current directory, i.e. the directory of the file
  1229. into which the link is being inserted.
  1230. absolute Absolute path, if possible with ~ for home directory.
  1231. noabbrev Absolute path, no abbreviation of home directory.
  1232. adaptive Use relative path for files in the current directory and sub-
  1233. directories of it. For other files, use an absolute path."
  1234. :group 'org-link
  1235. :type '(choice
  1236. (const relative)
  1237. (const absolute)
  1238. (const noabbrev)
  1239. (const adaptive)))
  1240. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1241. "Types of links that should be activated in Org-mode files.
  1242. This is a list of symbols, each leading to the activation of a certain link
  1243. type. In principle, it does not hurt to turn on most link types - there may
  1244. be a small gain when turning off unused link types. The types are:
  1245. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1246. angle Links in angular brackets that may contain whitespace like
  1247. <bbdb:Carsten Dominik>.
  1248. plain Plain links in normal text, no whitespace, like http://google.com.
  1249. radio Text that is matched by a radio target, see manual for details.
  1250. tag Tag settings in a headline (link to tag search).
  1251. date Time stamps (link to calendar).
  1252. footnote Footnote labels.
  1253. Changing this variable requires a restart of Emacs to become effective."
  1254. :group 'org-link
  1255. :type '(set :greedy t
  1256. (const :tag "Double bracket links" bracket)
  1257. (const :tag "Angular bracket links" angle)
  1258. (const :tag "Plain text links" plain)
  1259. (const :tag "Radio target matches" radio)
  1260. (const :tag "Tags" tag)
  1261. (const :tag "Timestamps" date)
  1262. (const :tag "Footnotes" footnote)))
  1263. (defcustom org-make-link-description-function nil
  1264. "Function to use for generating link descriptions from links.
  1265. When nil, the link location will be used. This function must take
  1266. two parameters: the first one is the link, the second one is the
  1267. description generated by `org-insert-link'. The function should
  1268. return the description to use."
  1269. :group 'org-link
  1270. :type 'function)
  1271. (defgroup org-link-store nil
  1272. "Options concerning storing links in Org-mode."
  1273. :tag "Org Store Link"
  1274. :group 'org-link)
  1275. (defcustom org-url-hexify-p t
  1276. "When non-nil, hexify URL when creating a link."
  1277. :type 'boolean
  1278. :version "24.3"
  1279. :group 'org-link-store)
  1280. (defcustom org-email-link-description-format "Email %c: %.30s"
  1281. "Format of the description part of a link to an email or usenet message.
  1282. The following %-escapes will be replaced by corresponding information:
  1283. %F full \"From\" field
  1284. %f name, taken from \"From\" field, address if no name
  1285. %T full \"To\" field
  1286. %t first name in \"To\" field, address if no name
  1287. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1288. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1289. %s subject
  1290. %d date
  1291. %m message-id.
  1292. You may use normal field width specification between the % and the letter.
  1293. This is for example useful to limit the length of the subject.
  1294. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1295. :group 'org-link-store
  1296. :type 'string)
  1297. (defcustom org-from-is-user-regexp
  1298. (let (r1 r2)
  1299. (when (and user-mail-address (not (string= user-mail-address "")))
  1300. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1301. (when (and user-full-name (not (string= user-full-name "")))
  1302. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1303. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1304. "Regexp matched against the \"From:\" header of an email or usenet message.
  1305. It should match if the message is from the user him/herself."
  1306. :group 'org-link-store
  1307. :type 'regexp)
  1308. (defcustom org-context-in-file-links t
  1309. "Non-nil means file links from `org-store-link' contain context.
  1310. A search string will be added to the file name with :: as separator and
  1311. used to find the context when the link is activated by the command
  1312. `org-open-at-point'. When this option is t, the entire active region
  1313. will be placed in the search string of the file link. If set to a
  1314. positive integer, only the first n lines of context will be stored.
  1315. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1316. negates this setting for the duration of the command."
  1317. :group 'org-link-store
  1318. :type '(choice boolean integer))
  1319. (defcustom org-keep-stored-link-after-insertion nil
  1320. "Non-nil means keep link in list for entire session.
  1321. The command `org-store-link' adds a link pointing to the current
  1322. location to an internal list. These links accumulate during a session.
  1323. The command `org-insert-link' can be used to insert links into any
  1324. Org-mode file (offering completion for all stored links). When this
  1325. option is nil, every link which has been inserted once using \\[org-insert-link]
  1326. will be removed from the list, to make completing the unused links
  1327. more efficient."
  1328. :group 'org-link-store
  1329. :type 'boolean)
  1330. (defgroup org-link-follow nil
  1331. "Options concerning following links in Org-mode."
  1332. :tag "Org Follow Link"
  1333. :group 'org-link)
  1334. (defcustom org-link-translation-function nil
  1335. "Function to translate links with different syntax to Org syntax.
  1336. This can be used to translate links created for example by the Planner
  1337. or emacs-wiki packages to Org syntax.
  1338. The function must accept two parameters, a TYPE containing the link
  1339. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1340. which is everything after the link protocol. It should return a cons
  1341. with possibly modified values of type and path.
  1342. Org contains a function for this, so if you set this variable to
  1343. `org-translate-link-from-planner', you should be able follow many
  1344. links created by planner."
  1345. :group 'org-link-follow
  1346. :type 'function)
  1347. (defcustom org-follow-link-hook nil
  1348. "Hook that is run after a link has been followed."
  1349. :group 'org-link-follow
  1350. :type 'hook)
  1351. (defcustom org-tab-follows-link nil
  1352. "Non-nil means on links TAB will follow the link.
  1353. Needs to be set before org.el is loaded.
  1354. This really should not be used, it does not make sense, and the
  1355. implementation is bad."
  1356. :group 'org-link-follow
  1357. :type 'boolean)
  1358. (defcustom org-return-follows-link nil
  1359. "Non-nil means on links RET will follow the link."
  1360. :group 'org-link-follow
  1361. :type 'boolean)
  1362. (defcustom org-mouse-1-follows-link
  1363. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1364. "Non-nil means mouse-1 on a link will follow the link.
  1365. A longer mouse click will still set point. Does not work on XEmacs.
  1366. Needs to be set before org.el is loaded."
  1367. :group 'org-link-follow
  1368. :type 'boolean)
  1369. (defcustom org-mark-ring-length 4
  1370. "Number of different positions to be recorded in the ring.
  1371. Changing this requires a restart of Emacs to work correctly."
  1372. :group 'org-link-follow
  1373. :type 'integer)
  1374. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1375. "Non-nil means internal links in Org files must exactly match a headline.
  1376. When nil, the link search tries to match a phrase with all words
  1377. in the search text."
  1378. :group 'org-link-follow
  1379. :version "24.1"
  1380. :type '(choice
  1381. (const :tag "Use fuzzy text search" nil)
  1382. (const :tag "Match only exact headline" t)
  1383. (const :tag "Match exact headline or query to create it"
  1384. query-to-create)))
  1385. (defcustom org-link-frame-setup
  1386. '((vm . vm-visit-folder-other-frame)
  1387. (vm-imap . vm-visit-imap-folder-other-frame)
  1388. (gnus . org-gnus-no-new-news)
  1389. (file . find-file-other-window)
  1390. (wl . wl-other-frame))
  1391. "Setup the frame configuration for following links.
  1392. When following a link with Emacs, it may often be useful to display
  1393. this link in another window or frame. This variable can be used to
  1394. set this up for the different types of links.
  1395. For VM, use any of
  1396. `vm-visit-folder'
  1397. `vm-visit-folder-other-window'
  1398. `vm-visit-folder-other-frame'
  1399. For Gnus, use any of
  1400. `gnus'
  1401. `gnus-other-frame'
  1402. `org-gnus-no-new-news'
  1403. For FILE, use any of
  1404. `find-file'
  1405. `find-file-other-window'
  1406. `find-file-other-frame'
  1407. For Wanderlust use any of
  1408. `wl'
  1409. `wl-other-frame'
  1410. For the calendar, use the variable `calendar-setup'.
  1411. For BBDB, it is currently only possible to display the matches in
  1412. another window."
  1413. :group 'org-link-follow
  1414. :type '(list
  1415. (cons (const vm)
  1416. (choice
  1417. (const vm-visit-folder)
  1418. (const vm-visit-folder-other-window)
  1419. (const vm-visit-folder-other-frame)))
  1420. (cons (const gnus)
  1421. (choice
  1422. (const gnus)
  1423. (const gnus-other-frame)
  1424. (const org-gnus-no-new-news)))
  1425. (cons (const file)
  1426. (choice
  1427. (const find-file)
  1428. (const find-file-other-window)
  1429. (const find-file-other-frame)))
  1430. (cons (const wl)
  1431. (choice
  1432. (const wl)
  1433. (const wl-other-frame)))))
  1434. (defcustom org-display-internal-link-with-indirect-buffer nil
  1435. "Non-nil means use indirect buffer to display infile links.
  1436. Activating internal links (from one location in a file to another location
  1437. in the same file) normally just jumps to the location. When the link is
  1438. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1439. is displayed in
  1440. another window. When this option is set, the other window actually displays
  1441. an indirect buffer clone of the current buffer, to avoid any visibility
  1442. changes to the current buffer."
  1443. :group 'org-link-follow
  1444. :type 'boolean)
  1445. (defcustom org-open-non-existing-files nil
  1446. "Non-nil means `org-open-file' will open non-existing files.
  1447. When nil, an error will be generated.
  1448. This variable applies only to external applications because they
  1449. might choke on non-existing files. If the link is to a file that
  1450. will be opened in Emacs, the variable is ignored."
  1451. :group 'org-link-follow
  1452. :type 'boolean)
  1453. (defcustom org-open-directory-means-index-dot-org nil
  1454. "Non-nil means a link to a directory really means to index.org.
  1455. When nil, following a directory link will run dired or open a finder/explorer
  1456. window on that directory."
  1457. :group 'org-link-follow
  1458. :type 'boolean)
  1459. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1460. "Function and arguments to call for following mailto links.
  1461. This is a list with the first element being a Lisp function, and the
  1462. remaining elements being arguments to the function. In string arguments,
  1463. %a will be replaced by the address, and %s will be replaced by the subject
  1464. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1465. :group 'org-link-follow
  1466. :type '(choice
  1467. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1468. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1469. (const :tag "message-mail" (message-mail "%a" "%s"))
  1470. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1471. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1472. "Non-nil means ask for confirmation before executing shell links.
  1473. Shell links can be dangerous: just think about a link
  1474. [[shell:rm -rf ~/*][Google Search]]
  1475. This link would show up in your Org-mode document as \"Google Search\",
  1476. but really it would remove your entire home directory.
  1477. Therefore we advise against setting this variable to nil.
  1478. Just change it to `y-or-n-p' if you want to confirm with a
  1479. single keystroke rather than having to type \"yes\"."
  1480. :group 'org-link-follow
  1481. :type '(choice
  1482. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1483. (const :tag "with y-or-n (faster)" y-or-n-p)
  1484. (const :tag "no confirmation (dangerous)" nil)))
  1485. (put 'org-confirm-shell-link-function
  1486. 'safe-local-variable
  1487. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1488. (defcustom org-confirm-shell-link-not-regexp ""
  1489. "A regexp to skip confirmation for shell links."
  1490. :group 'org-link-follow
  1491. :version "24.1"
  1492. :type 'regexp)
  1493. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1494. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1495. Elisp links can be dangerous: just think about a link
  1496. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1497. This link would show up in your Org-mode document as \"Google Search\",
  1498. but really it would remove your entire home directory.
  1499. Therefore we advise against setting this variable to nil.
  1500. Just change it to `y-or-n-p' if you want to confirm with a
  1501. single keystroke rather than having to type \"yes\"."
  1502. :group 'org-link-follow
  1503. :type '(choice
  1504. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1505. (const :tag "with y-or-n (faster)" y-or-n-p)
  1506. (const :tag "no confirmation (dangerous)" nil)))
  1507. (put 'org-confirm-shell-link-function
  1508. 'safe-local-variable
  1509. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1510. (defcustom org-confirm-elisp-link-not-regexp ""
  1511. "A regexp to skip confirmation for Elisp links."
  1512. :group 'org-link-follow
  1513. :version "24.1"
  1514. :type 'regexp)
  1515. (defconst org-file-apps-defaults-gnu
  1516. '((remote . emacs)
  1517. (system . mailcap)
  1518. (t . mailcap))
  1519. "Default file applications on a UNIX or GNU/Linux system.
  1520. See `org-file-apps'.")
  1521. (defconst org-file-apps-defaults-macosx
  1522. '((remote . emacs)
  1523. (t . "open %s")
  1524. (system . "open %s")
  1525. ("ps.gz" . "gv %s")
  1526. ("eps.gz" . "gv %s")
  1527. ("dvi" . "xdvi %s")
  1528. ("fig" . "xfig %s"))
  1529. "Default file applications on a MacOS X system.
  1530. The system \"open\" is known as a default, but we use X11 applications
  1531. for some files for which the OS does not have a good default.
  1532. See `org-file-apps'.")
  1533. (defconst org-file-apps-defaults-windowsnt
  1534. (list
  1535. '(remote . emacs)
  1536. (cons t
  1537. (list (if (featurep 'xemacs)
  1538. 'mswindows-shell-execute
  1539. 'w32-shell-execute)
  1540. "open" 'file))
  1541. (cons 'system
  1542. (list (if (featurep 'xemacs)
  1543. 'mswindows-shell-execute
  1544. 'w32-shell-execute)
  1545. "open" 'file)))
  1546. "Default file applications on a Windows NT system.
  1547. The system \"open\" is used for most files.
  1548. See `org-file-apps'.")
  1549. (defcustom org-file-apps
  1550. '(
  1551. (auto-mode . emacs)
  1552. ("\\.mm\\'" . default)
  1553. ("\\.x?html?\\'" . default)
  1554. ("\\.pdf\\'" . default)
  1555. )
  1556. "External applications for opening `file:path' items in a document.
  1557. Org-mode uses system defaults for different file types, but
  1558. you can use this variable to set the application for a given file
  1559. extension. The entries in this list are cons cells where the car identifies
  1560. files and the cdr the corresponding command. Possible values for the
  1561. file identifier are
  1562. \"string\" A string as a file identifier can be interpreted in different
  1563. ways, depending on its contents:
  1564. - Alphanumeric characters only:
  1565. Match links with this file extension.
  1566. Example: (\"pdf\" . \"evince %s\")
  1567. to open PDFs with evince.
  1568. - Regular expression: Match links where the
  1569. filename matches the regexp. If you want to
  1570. use groups here, use shy groups.
  1571. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1572. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1573. to open *.html and *.xhtml with firefox.
  1574. - Regular expression which contains (non-shy) groups:
  1575. Match links where the whole link, including \"::\", and
  1576. anything after that, matches the regexp.
  1577. In a custom command string, %1, %2, etc. are replaced with
  1578. the parts of the link that were matched by the groups.
  1579. For backwards compatibility, if a command string is given
  1580. that does not use any of the group matches, this case is
  1581. handled identically to the second one (i.e. match against
  1582. file name only).
  1583. In a custom lisp form, you can access the group matches with
  1584. (match-string n link).
  1585. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1586. to open [[file:document.pdf::5]] with evince at page 5.
  1587. `directory' Matches a directory
  1588. `remote' Matches a remote file, accessible through tramp or efs.
  1589. Remote files most likely should be visited through Emacs
  1590. because external applications cannot handle such paths.
  1591. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1592. so all files Emacs knows how to handle. Using this with
  1593. command `emacs' will open most files in Emacs. Beware that this
  1594. will also open html files inside Emacs, unless you add
  1595. (\"html\" . default) to the list as well.
  1596. t Default for files not matched by any of the other options.
  1597. `system' The system command to open files, like `open' on Windows
  1598. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1599. that will be selected if you call `C-c C-o' with a double
  1600. \\[universal-argument] \\[universal-argument] prefix.
  1601. Possible values for the command are:
  1602. `emacs' The file will be visited by the current Emacs process.
  1603. `default' Use the default application for this file type, which is the
  1604. association for t in the list, most likely in the system-specific
  1605. part.
  1606. This can be used to overrule an unwanted setting in the
  1607. system-specific variable.
  1608. `system' Use the system command for opening files, like \"open\".
  1609. This command is specified by the entry whose car is `system'.
  1610. Most likely, the system-specific version of this variable
  1611. does define this command, but you can overrule/replace it
  1612. here.
  1613. string A command to be executed by a shell; %s will be replaced
  1614. by the path to the file.
  1615. sexp A Lisp form which will be evaluated. The file path will
  1616. be available in the Lisp variable `file'.
  1617. For more examples, see the system specific constants
  1618. `org-file-apps-defaults-macosx'
  1619. `org-file-apps-defaults-windowsnt'
  1620. `org-file-apps-defaults-gnu'."
  1621. :group 'org-link-follow
  1622. :type '(repeat
  1623. (cons (choice :value ""
  1624. (string :tag "Extension")
  1625. (const :tag "System command to open files" system)
  1626. (const :tag "Default for unrecognized files" t)
  1627. (const :tag "Remote file" remote)
  1628. (const :tag "Links to a directory" directory)
  1629. (const :tag "Any files that have Emacs modes"
  1630. auto-mode))
  1631. (choice :value ""
  1632. (const :tag "Visit with Emacs" emacs)
  1633. (const :tag "Use default" default)
  1634. (const :tag "Use the system command" system)
  1635. (string :tag "Command")
  1636. (sexp :tag "Lisp form")))))
  1637. (defcustom org-doi-server-url "http://dx.doi.org/"
  1638. "The URL of the DOI server."
  1639. :type 'string
  1640. :version "24.3"
  1641. :group 'org-link-follow)
  1642. (defgroup org-refile nil
  1643. "Options concerning refiling entries in Org-mode."
  1644. :tag "Org Refile"
  1645. :group 'org)
  1646. (defcustom org-directory "~/org"
  1647. "Directory with org files.
  1648. This is just a default location to look for Org files. There is no need
  1649. at all to put your files into this directory. It is only used in the
  1650. following situations:
  1651. 1. When a capture template specifies a target file that is not an
  1652. absolute path. The path will then be interpreted relative to
  1653. `org-directory'
  1654. 2. When a capture note is filed away in an interactive way (when exiting the
  1655. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1656. with `org-directory' as the default path."
  1657. :group 'org-refile
  1658. :group 'org-remember
  1659. :group 'org-capture
  1660. :type 'directory)
  1661. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1662. "Default target for storing notes.
  1663. Used as a fall back file for org-remember.el and org-capture.el, for
  1664. templates that do not specify a target file."
  1665. :group 'org-refile
  1666. :group 'org-remember
  1667. :group 'org-capture
  1668. :type '(choice
  1669. (const :tag "Default from remember-data-file" nil)
  1670. file))
  1671. (defcustom org-goto-interface 'outline
  1672. "The default interface to be used for `org-goto'.
  1673. Allowed values are:
  1674. outline The interface shows an outline of the relevant file
  1675. and the correct heading is found by moving through
  1676. the outline or by searching with incremental search.
  1677. outline-path-completion Headlines in the current buffer are offered via
  1678. completion. This is the interface also used by
  1679. the refile command."
  1680. :group 'org-refile
  1681. :type '(choice
  1682. (const :tag "Outline" outline)
  1683. (const :tag "Outline-path-completion" outline-path-completion)))
  1684. (defcustom org-goto-max-level 5
  1685. "Maximum target level when running `org-goto' with refile interface."
  1686. :group 'org-refile
  1687. :type 'integer)
  1688. (defcustom org-reverse-note-order nil
  1689. "Non-nil means store new notes at the beginning of a file or entry.
  1690. When nil, new notes will be filed to the end of a file or entry.
  1691. This can also be a list with cons cells of regular expressions that
  1692. are matched against file names, and values."
  1693. :group 'org-remember
  1694. :group 'org-capture
  1695. :group 'org-refile
  1696. :type '(choice
  1697. (const :tag "Reverse always" t)
  1698. (const :tag "Reverse never" nil)
  1699. (repeat :tag "By file name regexp"
  1700. (cons regexp boolean))))
  1701. (defcustom org-log-refile nil
  1702. "Information to record when a task is refiled.
  1703. Possible values are:
  1704. nil Don't add anything
  1705. time Add a time stamp to the task
  1706. note Prompt for a note and add it with template `org-log-note-headings'
  1707. This option can also be set with on a per-file-basis with
  1708. #+STARTUP: nologrefile
  1709. #+STARTUP: logrefile
  1710. #+STARTUP: lognoterefile
  1711. You can have local logging settings for a subtree by setting the LOGGING
  1712. property to one or more of these keywords.
  1713. When bulk-refiling from the agenda, the value `note' is forbidden and
  1714. will temporarily be changed to `time'."
  1715. :group 'org-refile
  1716. :group 'org-progress
  1717. :version "24.1"
  1718. :type '(choice
  1719. (const :tag "No logging" nil)
  1720. (const :tag "Record timestamp" time)
  1721. (const :tag "Record timestamp with note." note)))
  1722. (defcustom org-refile-targets nil
  1723. "Targets for refiling entries with \\[org-refile].
  1724. This is a list of cons cells. Each cell contains:
  1725. - a specification of the files to be considered, either a list of files,
  1726. or a symbol whose function or variable value will be used to retrieve
  1727. a file name or a list of file names. If you use `org-agenda-files' for
  1728. that, all agenda files will be scanned for targets. Nil means consider
  1729. headings in the current buffer.
  1730. - A specification of how to find candidate refile targets. This may be
  1731. any of:
  1732. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1733. This tag has to be present in all target headlines, inheritance will
  1734. not be considered.
  1735. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1736. todo keyword.
  1737. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1738. headlines that are refiling targets.
  1739. - a cons cell (:level . N). Any headline of level N is considered a target.
  1740. Note that, when `org-odd-levels-only' is set, level corresponds to
  1741. order in hierarchy, not to the number of stars.
  1742. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1743. Note that, when `org-odd-levels-only' is set, level corresponds to
  1744. order in hierarchy, not to the number of stars.
  1745. Each element of this list generates a set of possible targets.
  1746. The union of these sets is presented (with completion) to
  1747. the user by `org-refile'.
  1748. You can set the variable `org-refile-target-verify-function' to a function
  1749. to verify each headline found by the simple criteria above.
  1750. When this variable is nil, all top-level headlines in the current buffer
  1751. are used, equivalent to the value `((nil . (:level . 1))'."
  1752. :group 'org-refile
  1753. :type '(repeat
  1754. (cons
  1755. (choice :value org-agenda-files
  1756. (const :tag "All agenda files" org-agenda-files)
  1757. (const :tag "Current buffer" nil)
  1758. (function) (variable) (file))
  1759. (choice :tag "Identify target headline by"
  1760. (cons :tag "Specific tag" (const :value :tag) (string))
  1761. (cons :tag "TODO keyword" (const :value :todo) (string))
  1762. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1763. (cons :tag "Level number" (const :value :level) (integer))
  1764. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1765. (defcustom org-refile-target-verify-function nil
  1766. "Function to verify if the headline at point should be a refile target.
  1767. The function will be called without arguments, with point at the
  1768. beginning of the headline. It should return t and leave point
  1769. where it is if the headline is a valid target for refiling.
  1770. If the target should not be selected, the function must return nil.
  1771. In addition to this, it may move point to a place from where the search
  1772. should be continued. For example, the function may decide that the entire
  1773. subtree of the current entry should be excluded and move point to the end
  1774. of the subtree."
  1775. :group 'org-refile
  1776. :type 'function)
  1777. (defcustom org-refile-use-cache nil
  1778. "Non-nil means cache refile targets to speed up the process.
  1779. The cache for a particular file will be updated automatically when
  1780. the buffer has been killed, or when any of the marker used for flagging
  1781. refile targets no longer points at a live buffer.
  1782. If you have added new entries to a buffer that might themselves be targets,
  1783. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1784. find that easier, `C-u C-u C-u C-c C-w'."
  1785. :group 'org-refile
  1786. :version "24.1"
  1787. :type 'boolean)
  1788. (defcustom org-refile-use-outline-path nil
  1789. "Non-nil means provide refile targets as paths.
  1790. So a level 3 headline will be available as level1/level2/level3.
  1791. When the value is `file', also include the file name (without directory)
  1792. into the path. In this case, you can also stop the completion after
  1793. the file name, to get entries inserted as top level in the file.
  1794. When `full-file-path', include the full file path."
  1795. :group 'org-refile
  1796. :type '(choice
  1797. (const :tag "Not" nil)
  1798. (const :tag "Yes" t)
  1799. (const :tag "Start with file name" file)
  1800. (const :tag "Start with full file path" full-file-path)))
  1801. (defcustom org-outline-path-complete-in-steps t
  1802. "Non-nil means complete the outline path in hierarchical steps.
  1803. When Org-mode uses the refile interface to select an outline path
  1804. \(see variable `org-refile-use-outline-path'), the completion of
  1805. the path can be done is a single go, or if can be done in steps down
  1806. the headline hierarchy. Going in steps is probably the best if you
  1807. do not use a special completion package like `ido' or `icicles'.
  1808. However, when using these packages, going in one step can be very
  1809. fast, while still showing the whole path to the entry."
  1810. :group 'org-refile
  1811. :type 'boolean)
  1812. (defcustom org-refile-allow-creating-parent-nodes nil
  1813. "Non-nil means allow to create new nodes as refile targets.
  1814. New nodes are then created by adding \"/new node name\" to the completion
  1815. of an existing node. When the value of this variable is `confirm',
  1816. new node creation must be confirmed by the user (recommended)
  1817. When nil, the completion must match an existing entry.
  1818. Note that, if the new heading is not seen by the criteria
  1819. listed in `org-refile-targets', multiple instances of the same
  1820. heading would be created by trying again to file under the new
  1821. heading."
  1822. :group 'org-refile
  1823. :type '(choice
  1824. (const :tag "Never" nil)
  1825. (const :tag "Always" t)
  1826. (const :tag "Prompt for confirmation" confirm)))
  1827. (defcustom org-refile-active-region-within-subtree nil
  1828. "Non-nil means also refile active region within a subtree.
  1829. By default `org-refile' doesn't allow refiling regions if they
  1830. don't contain a set of subtrees, but it might be convenient to
  1831. do so sometimes: in that case, the first line of the region is
  1832. converted to a headline before refiling."
  1833. :group 'org-refile
  1834. :version "24.1"
  1835. :type 'boolean)
  1836. (defgroup org-todo nil
  1837. "Options concerning TODO items in Org-mode."
  1838. :tag "Org TODO"
  1839. :group 'org)
  1840. (defgroup org-progress nil
  1841. "Options concerning Progress logging in Org-mode."
  1842. :tag "Org Progress"
  1843. :group 'org-time)
  1844. (defvar org-todo-interpretation-widgets
  1845. '((:tag "Sequence (cycling hits every state)" sequence)
  1846. (:tag "Type (cycling directly to DONE)" type))
  1847. "The available interpretation symbols for customizing `org-todo-keywords'.
  1848. Interested libraries should add to this list.")
  1849. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1850. "List of TODO entry keyword sequences and their interpretation.
  1851. \\<org-mode-map>This is a list of sequences.
  1852. Each sequence starts with a symbol, either `sequence' or `type',
  1853. indicating if the keywords should be interpreted as a sequence of
  1854. action steps, or as different types of TODO items. The first
  1855. keywords are states requiring action - these states will select a headline
  1856. for inclusion into the global TODO list Org-mode produces. If one of
  1857. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1858. signify that no further action is necessary. If \"|\" is not found,
  1859. the last keyword is treated as the only DONE state of the sequence.
  1860. The command \\[org-todo] cycles an entry through these states, and one
  1861. additional state where no keyword is present. For details about this
  1862. cycling, see the manual.
  1863. TODO keywords and interpretation can also be set on a per-file basis with
  1864. the special #+SEQ_TODO and #+TYP_TODO lines.
  1865. Each keyword can optionally specify a character for fast state selection
  1866. \(in combination with the variable `org-use-fast-todo-selection')
  1867. and specifiers for state change logging, using the same syntax that
  1868. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1869. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1870. indicates to record a time stamp each time this state is selected.
  1871. Each keyword may also specify if a timestamp or a note should be
  1872. recorded when entering or leaving the state, by adding additional
  1873. characters in the parenthesis after the keyword. This looks like this:
  1874. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1875. record only the time of the state change. With X and Y being either
  1876. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1877. Y when leaving the state if and only if the *target* state does not
  1878. define X. You may omit any of the fast-selection key or X or /Y,
  1879. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1880. For backward compatibility, this variable may also be just a list
  1881. of keywords. In this case the interpretation (sequence or type) will be
  1882. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1883. :group 'org-todo
  1884. :group 'org-keywords
  1885. :type '(choice
  1886. (repeat :tag "Old syntax, just keywords"
  1887. (string :tag "Keyword"))
  1888. (repeat :tag "New syntax"
  1889. (cons
  1890. (choice
  1891. :tag "Interpretation"
  1892. ;;Quick and dirty way to see
  1893. ;;`org-todo-interpretations'. This takes the
  1894. ;;place of item arguments
  1895. :convert-widget
  1896. (lambda (widget)
  1897. (widget-put widget
  1898. :args (mapcar
  1899. #'(lambda (x)
  1900. (widget-convert
  1901. (cons 'const x)))
  1902. org-todo-interpretation-widgets))
  1903. widget))
  1904. (repeat
  1905. (string :tag "Keyword"))))))
  1906. (defvar org-todo-keywords-1 nil
  1907. "All TODO and DONE keywords active in a buffer.")
  1908. (make-variable-buffer-local 'org-todo-keywords-1)
  1909. (defvar org-todo-keywords-for-agenda nil)
  1910. (defvar org-done-keywords-for-agenda nil)
  1911. (defvar org-drawers-for-agenda nil)
  1912. (defvar org-todo-keyword-alist-for-agenda nil)
  1913. (defvar org-tag-alist-for-agenda nil)
  1914. (defvar org-agenda-contributing-files nil)
  1915. (defvar org-not-done-keywords nil)
  1916. (make-variable-buffer-local 'org-not-done-keywords)
  1917. (defvar org-done-keywords nil)
  1918. (make-variable-buffer-local 'org-done-keywords)
  1919. (defvar org-todo-heads nil)
  1920. (make-variable-buffer-local 'org-todo-heads)
  1921. (defvar org-todo-sets nil)
  1922. (make-variable-buffer-local 'org-todo-sets)
  1923. (defvar org-todo-log-states nil)
  1924. (make-variable-buffer-local 'org-todo-log-states)
  1925. (defvar org-todo-kwd-alist nil)
  1926. (make-variable-buffer-local 'org-todo-kwd-alist)
  1927. (defvar org-todo-key-alist nil)
  1928. (make-variable-buffer-local 'org-todo-key-alist)
  1929. (defvar org-todo-key-trigger nil)
  1930. (make-variable-buffer-local 'org-todo-key-trigger)
  1931. (defcustom org-todo-interpretation 'sequence
  1932. "Controls how TODO keywords are interpreted.
  1933. This variable is in principle obsolete and is only used for
  1934. backward compatibility, if the interpretation of todo keywords is
  1935. not given already in `org-todo-keywords'. See that variable for
  1936. more information."
  1937. :group 'org-todo
  1938. :group 'org-keywords
  1939. :type '(choice (const sequence)
  1940. (const type)))
  1941. (defcustom org-use-fast-todo-selection t
  1942. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1943. This variable describes if and under what circumstances the cycling
  1944. mechanism for TODO keywords will be replaced by a single-key, direct
  1945. selection scheme.
  1946. When nil, fast selection is never used.
  1947. When the symbol `prefix', it will be used when `org-todo' is called
  1948. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  1949. `C-u t' in an agenda buffer.
  1950. When t, fast selection is used by default. In this case, the prefix
  1951. argument forces cycling instead.
  1952. In all cases, the special interface is only used if access keys have
  1953. actually been assigned by the user, i.e. if keywords in the configuration
  1954. are followed by a letter in parenthesis, like TODO(t)."
  1955. :group 'org-todo
  1956. :type '(choice
  1957. (const :tag "Never" nil)
  1958. (const :tag "By default" t)
  1959. (const :tag "Only with C-u C-c C-t" prefix)))
  1960. (defcustom org-provide-todo-statistics t
  1961. "Non-nil means update todo statistics after insert and toggle.
  1962. ALL-HEADLINES means update todo statistics by including headlines
  1963. with no TODO keyword as well, counting them as not done.
  1964. A list of TODO keywords means the same, but skip keywords that are
  1965. not in this list.
  1966. When this is set, todo statistics is updated in the parent of the
  1967. current entry each time a todo state is changed."
  1968. :group 'org-todo
  1969. :type '(choice
  1970. (const :tag "Yes, only for TODO entries" t)
  1971. (const :tag "Yes, including all entries" 'all-headlines)
  1972. (repeat :tag "Yes, for TODOs in this list"
  1973. (string :tag "TODO keyword"))
  1974. (other :tag "No TODO statistics" nil)))
  1975. (defcustom org-hierarchical-todo-statistics t
  1976. "Non-nil means TODO statistics covers just direct children.
  1977. When nil, all entries in the subtree are considered.
  1978. This has only an effect if `org-provide-todo-statistics' is set.
  1979. To set this to nil for only a single subtree, use a COOKIE_DATA
  1980. property and include the word \"recursive\" into the value."
  1981. :group 'org-todo
  1982. :type 'boolean)
  1983. (defcustom org-after-todo-state-change-hook nil
  1984. "Hook which is run after the state of a TODO item was changed.
  1985. The new state (a string with a TODO keyword, or nil) is available in the
  1986. Lisp variable `org-state'."
  1987. :group 'org-todo
  1988. :type 'hook)
  1989. (defvar org-blocker-hook nil
  1990. "Hook for functions that are allowed to block a state change.
  1991. Functions in this hook should not modify the buffer.
  1992. Each function gets as its single argument a property list,
  1993. see `org-trigger-hook' for more information about this list.
  1994. If any of the functions in this hook returns nil, the state change
  1995. is blocked.")
  1996. (defvar org-trigger-hook nil
  1997. "Hook for functions that are triggered by a state change.
  1998. Each function gets as its single argument a property list with at
  1999. least the following elements:
  2000. (:type type-of-change :position pos-at-entry-start
  2001. :from old-state :to new-state)
  2002. Depending on the type, more properties may be present.
  2003. This mechanism is currently implemented for:
  2004. TODO state changes
  2005. ------------------
  2006. :type todo-state-change
  2007. :from previous state (keyword as a string), or nil, or a symbol
  2008. 'todo' or 'done', to indicate the general type of state.
  2009. :to new state, like in :from")
  2010. (defcustom org-enforce-todo-dependencies nil
  2011. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2012. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2013. be blocked if any prior sibling is not yet done.
  2014. Finally, if the parent is blocked because of ordered siblings of its own,
  2015. the child will also be blocked."
  2016. :set (lambda (var val)
  2017. (set var val)
  2018. (if val
  2019. (add-hook 'org-blocker-hook
  2020. 'org-block-todo-from-children-or-siblings-or-parent)
  2021. (remove-hook 'org-blocker-hook
  2022. 'org-block-todo-from-children-or-siblings-or-parent)))
  2023. :group 'org-todo
  2024. :type 'boolean)
  2025. (defcustom org-enforce-todo-checkbox-dependencies nil
  2026. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2027. When this is nil, checkboxes have no influence on switching TODO states.
  2028. When non-nil, you first need to check off all check boxes before the TODO
  2029. entry can be switched to DONE.
  2030. This variable needs to be set before org.el is loaded, and you need to
  2031. restart Emacs after a change to make the change effective. The only way
  2032. to change is while Emacs is running is through the customize interface."
  2033. :set (lambda (var val)
  2034. (set var val)
  2035. (if val
  2036. (add-hook 'org-blocker-hook
  2037. 'org-block-todo-from-checkboxes)
  2038. (remove-hook 'org-blocker-hook
  2039. 'org-block-todo-from-checkboxes)))
  2040. :group 'org-todo
  2041. :type 'boolean)
  2042. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2043. "Non-nil means inserting a TODO heading is treated as state change.
  2044. So when the command \\[org-insert-todo-heading] is used, state change
  2045. logging will apply if appropriate. When nil, the new TODO item will
  2046. be inserted directly, and no logging will take place."
  2047. :group 'org-todo
  2048. :type 'boolean)
  2049. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2050. "Non-nil means switching TODO states with S-cursor counts as state change.
  2051. This is the default behavior. However, setting this to nil allows a
  2052. convenient way to select a TODO state and bypass any logging associated
  2053. with that."
  2054. :group 'org-todo
  2055. :type 'boolean)
  2056. (defcustom org-todo-state-tags-triggers nil
  2057. "Tag changes that should be triggered by TODO state changes.
  2058. This is a list. Each entry is
  2059. (state-change (tag . flag) .......)
  2060. State-change can be a string with a state, and empty string to indicate the
  2061. state that has no TODO keyword, or it can be one of the symbols `todo'
  2062. or `done', meaning any not-done or done state, respectively."
  2063. :group 'org-todo
  2064. :group 'org-tags
  2065. :type '(repeat
  2066. (cons (choice :tag "When changing to"
  2067. (const :tag "Not-done state" todo)
  2068. (const :tag "Done state" done)
  2069. (string :tag "State"))
  2070. (repeat
  2071. (cons :tag "Tag action"
  2072. (string :tag "Tag")
  2073. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2074. (defcustom org-log-done nil
  2075. "Information to record when a task moves to the DONE state.
  2076. Possible values are:
  2077. nil Don't add anything, just change the keyword
  2078. time Add a time stamp to the task
  2079. note Prompt for a note and add it with template `org-log-note-headings'
  2080. This option can also be set with on a per-file-basis with
  2081. #+STARTUP: nologdone
  2082. #+STARTUP: logdone
  2083. #+STARTUP: lognotedone
  2084. You can have local logging settings for a subtree by setting the LOGGING
  2085. property to one or more of these keywords."
  2086. :group 'org-todo
  2087. :group 'org-progress
  2088. :type '(choice
  2089. (const :tag "No logging" nil)
  2090. (const :tag "Record CLOSED timestamp" time)
  2091. (const :tag "Record CLOSED timestamp with note." note)))
  2092. ;; Normalize old uses of org-log-done.
  2093. (cond
  2094. ((eq org-log-done t) (setq org-log-done 'time))
  2095. ((and (listp org-log-done) (memq 'done org-log-done))
  2096. (setq org-log-done 'note)))
  2097. (defcustom org-log-reschedule nil
  2098. "Information to record when the scheduling date of a tasks is modified.
  2099. Possible values are:
  2100. nil Don't add anything, just change the date
  2101. time Add a time stamp to the task
  2102. note Prompt for a note and add it with template `org-log-note-headings'
  2103. This option can also be set with on a per-file-basis with
  2104. #+STARTUP: nologreschedule
  2105. #+STARTUP: logreschedule
  2106. #+STARTUP: lognotereschedule"
  2107. :group 'org-todo
  2108. :group 'org-progress
  2109. :type '(choice
  2110. (const :tag "No logging" nil)
  2111. (const :tag "Record timestamp" time)
  2112. (const :tag "Record timestamp with note." note)))
  2113. (defcustom org-log-redeadline nil
  2114. "Information to record when the deadline date of a tasks is modified.
  2115. Possible values are:
  2116. nil Don't add anything, just change the date
  2117. time Add a time stamp to the task
  2118. note Prompt for a note and add it with template `org-log-note-headings'
  2119. This option can also be set with on a per-file-basis with
  2120. #+STARTUP: nologredeadline
  2121. #+STARTUP: logredeadline
  2122. #+STARTUP: lognoteredeadline
  2123. You can have local logging settings for a subtree by setting the LOGGING
  2124. property to one or more of these keywords."
  2125. :group 'org-todo
  2126. :group 'org-progress
  2127. :type '(choice
  2128. (const :tag "No logging" nil)
  2129. (const :tag "Record timestamp" time)
  2130. (const :tag "Record timestamp with note." note)))
  2131. (defcustom org-log-note-clock-out nil
  2132. "Non-nil means record a note when clocking out of an item.
  2133. This can also be configured on a per-file basis by adding one of
  2134. the following lines anywhere in the buffer:
  2135. #+STARTUP: lognoteclock-out
  2136. #+STARTUP: nolognoteclock-out"
  2137. :group 'org-todo
  2138. :group 'org-progress
  2139. :type 'boolean)
  2140. (defcustom org-log-done-with-time t
  2141. "Non-nil means the CLOSED time stamp will contain date and time.
  2142. When nil, only the date will be recorded."
  2143. :group 'org-progress
  2144. :type 'boolean)
  2145. (defcustom org-log-note-headings
  2146. '((done . "CLOSING NOTE %t")
  2147. (state . "State %-12s from %-12S %t")
  2148. (note . "Note taken on %t")
  2149. (reschedule . "Rescheduled from %S on %t")
  2150. (delschedule . "Not scheduled, was %S on %t")
  2151. (redeadline . "New deadline from %S on %t")
  2152. (deldeadline . "Removed deadline, was %S on %t")
  2153. (refile . "Refiled on %t")
  2154. (clock-out . ""))
  2155. "Headings for notes added to entries.
  2156. The value is an alist, with the car being a symbol indicating the note
  2157. context, and the cdr is the heading to be used. The heading may also be the
  2158. empty string.
  2159. %t in the heading will be replaced by a time stamp.
  2160. %T will be an active time stamp instead the default inactive one
  2161. %d will be replaced by a short-format time stamp.
  2162. %D will be replaced by an active short-format time stamp.
  2163. %s will be replaced by the new TODO state, in double quotes.
  2164. %S will be replaced by the old TODO state, in double quotes.
  2165. %u will be replaced by the user name.
  2166. %U will be replaced by the full user name.
  2167. In fact, it is not a good idea to change the `state' entry, because
  2168. agenda log mode depends on the format of these entries."
  2169. :group 'org-todo
  2170. :group 'org-progress
  2171. :type '(list :greedy t
  2172. (cons (const :tag "Heading when closing an item" done) string)
  2173. (cons (const :tag
  2174. "Heading when changing todo state (todo sequence only)"
  2175. state) string)
  2176. (cons (const :tag "Heading when just taking a note" note) string)
  2177. (cons (const :tag "Heading when clocking out" clock-out) string)
  2178. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2179. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2180. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2181. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2182. (cons (const :tag "Heading when refiling" refile) string)))
  2183. (unless (assq 'note org-log-note-headings)
  2184. (push '(note . "%t") org-log-note-headings))
  2185. (defcustom org-log-into-drawer nil
  2186. "Non-nil means insert state change notes and time stamps into a drawer.
  2187. When nil, state changes notes will be inserted after the headline and
  2188. any scheduling and clock lines, but not inside a drawer.
  2189. The value of this variable should be the name of the drawer to use.
  2190. LOGBOOK is proposed as the default drawer for this purpose, you can
  2191. also set this to a string to define the drawer of your choice.
  2192. A value of t is also allowed, representing \"LOGBOOK\".
  2193. A value of t or nil can also be set with on a per-file-basis with
  2194. #+STARTUP: logdrawer
  2195. #+STARTUP: nologdrawer
  2196. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2197. will be ignored.
  2198. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2199. a subtree."
  2200. :group 'org-todo
  2201. :group 'org-progress
  2202. :type '(choice
  2203. (const :tag "Not into a drawer" nil)
  2204. (const :tag "LOGBOOK" t)
  2205. (string :tag "Other")))
  2206. (if (fboundp 'defvaralias)
  2207. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2208. (defun org-log-into-drawer ()
  2209. "Return the value of `org-log-into-drawer', but let properties overrule.
  2210. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2211. used instead of the default value."
  2212. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2213. (cond
  2214. ((not p) org-log-into-drawer)
  2215. ((equal p "nil") nil)
  2216. ((equal p "t") "LOGBOOK")
  2217. (t p))))
  2218. (defcustom org-log-state-notes-insert-after-drawers nil
  2219. "Non-nil means insert state change notes after any drawers in entry.
  2220. Only the drawers that *immediately* follow the headline and the
  2221. deadline/scheduled line are skipped.
  2222. When nil, insert notes right after the heading and perhaps the line
  2223. with deadline/scheduling if present.
  2224. This variable will have no effect if `org-log-into-drawer' is
  2225. set."
  2226. :group 'org-todo
  2227. :group 'org-progress
  2228. :type 'boolean)
  2229. (defcustom org-log-states-order-reversed t
  2230. "Non-nil means the latest state note will be directly after heading.
  2231. When nil, the state change notes will be ordered according to time.
  2232. This option can also be set with on a per-file-basis with
  2233. #+STARTUP: logstatesreversed
  2234. #+STARTUP: nologstatesreversed"
  2235. :group 'org-todo
  2236. :group 'org-progress
  2237. :type 'boolean)
  2238. (defcustom org-todo-repeat-to-state nil
  2239. "The TODO state to which a repeater should return the repeating task.
  2240. By default this is the first task in a TODO sequence, or the previous state
  2241. in a TODO_TYP set. But you can specify another task here.
  2242. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2243. :group 'org-todo
  2244. :version "24.1"
  2245. :type '(choice (const :tag "Head of sequence" nil)
  2246. (string :tag "Specific state")))
  2247. (defcustom org-log-repeat 'time
  2248. "Non-nil means record moving through the DONE state when triggering repeat.
  2249. An auto-repeating task is immediately switched back to TODO when
  2250. marked DONE. If you are not logging state changes (by adding \"@\"
  2251. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2252. record a closing note, there will be no record of the task moving
  2253. through DONE. This variable forces taking a note anyway.
  2254. nil Don't force a record
  2255. time Record a time stamp
  2256. note Prompt for a note and add it with template `org-log-note-headings'
  2257. This option can also be set with on a per-file-basis with
  2258. #+STARTUP: nologrepeat
  2259. #+STARTUP: logrepeat
  2260. #+STARTUP: lognoterepeat
  2261. You can have local logging settings for a subtree by setting the LOGGING
  2262. property to one or more of these keywords."
  2263. :group 'org-todo
  2264. :group 'org-progress
  2265. :type '(choice
  2266. (const :tag "Don't force a record" nil)
  2267. (const :tag "Force recording the DONE state" time)
  2268. (const :tag "Force recording a note with the DONE state" note)))
  2269. (defgroup org-priorities nil
  2270. "Priorities in Org-mode."
  2271. :tag "Org Priorities"
  2272. :group 'org-todo)
  2273. (defcustom org-enable-priority-commands t
  2274. "Non-nil means priority commands are active.
  2275. When nil, these commands will be disabled, so that you never accidentally
  2276. set a priority."
  2277. :group 'org-priorities
  2278. :type 'boolean)
  2279. (defcustom org-highest-priority ?A
  2280. "The highest priority of TODO items. A character like ?A, ?B etc.
  2281. Must have a smaller ASCII number than `org-lowest-priority'."
  2282. :group 'org-priorities
  2283. :type 'character)
  2284. (defcustom org-lowest-priority ?C
  2285. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2286. Must have a larger ASCII number than `org-highest-priority'."
  2287. :group 'org-priorities
  2288. :type 'character)
  2289. (defcustom org-default-priority ?B
  2290. "The default priority of TODO items.
  2291. This is the priority an item gets if no explicit priority is given.
  2292. When starting to cycle on an empty priority the first step in the cycle
  2293. depends on `org-priority-start-cycle-with-default'. The resulting first
  2294. step priority must not exceed the range from `org-highest-priority' to
  2295. `org-lowest-priority' which means that `org-default-priority' has to be
  2296. in this range exclusive or inclusive the range boundaries. Else the
  2297. first step refuses to set the default and the second will fall back
  2298. to (depending on the command used) the highest or lowest priority."
  2299. :group 'org-priorities
  2300. :type 'character)
  2301. (defcustom org-priority-start-cycle-with-default t
  2302. "Non-nil means start with default priority when starting to cycle.
  2303. When this is nil, the first step in the cycle will be (depending on the
  2304. command used) one higher or lower than the default priority.
  2305. See also `org-default-priority'."
  2306. :group 'org-priorities
  2307. :type 'boolean)
  2308. (defcustom org-get-priority-function nil
  2309. "Function to extract the priority from a string.
  2310. The string is normally the headline. If this is nil Org computes the
  2311. priority from the priority cookie like [#A] in the headline. It returns
  2312. an integer, increasing by 1000 for each priority level.
  2313. The user can set a different function here, which should take a string
  2314. as an argument and return the numeric priority."
  2315. :group 'org-priorities
  2316. :version "24.1"
  2317. :type 'function)
  2318. (defgroup org-time nil
  2319. "Options concerning time stamps and deadlines in Org-mode."
  2320. :tag "Org Time"
  2321. :group 'org)
  2322. (defcustom org-insert-labeled-timestamps-at-point nil
  2323. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2324. When nil, these labeled time stamps are forces into the second line of an
  2325. entry, just after the headline. When scheduling from the global TODO list,
  2326. the time stamp will always be forced into the second line."
  2327. :group 'org-time
  2328. :type 'boolean)
  2329. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2330. "Formats for `format-time-string' which are used for time stamps.
  2331. It is not recommended to change this constant.")
  2332. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2333. "Number of minutes to round time stamps to.
  2334. These are two values, the first applies when first creating a time stamp.
  2335. The second applies when changing it with the commands `S-up' and `S-down'.
  2336. When changing the time stamp, this means that it will change in steps
  2337. of N minutes, as given by the second value.
  2338. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2339. numbers should be factors of 60, so for example 5, 10, 15.
  2340. When this is larger than 1, you can still force an exact time stamp by using
  2341. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2342. and by using a prefix arg to `S-up/down' to specify the exact number
  2343. of minutes to shift."
  2344. :group 'org-time
  2345. :get #'(lambda (var) ; Make sure both elements are there
  2346. (if (integerp (default-value var))
  2347. (list (default-value var) 5)
  2348. (default-value var)))
  2349. :type '(list
  2350. (integer :tag "when inserting times")
  2351. (integer :tag "when modifying times")))
  2352. ;; Normalize old customizations of this variable.
  2353. (when (integerp org-time-stamp-rounding-minutes)
  2354. (setq org-time-stamp-rounding-minutes
  2355. (list org-time-stamp-rounding-minutes
  2356. org-time-stamp-rounding-minutes)))
  2357. (defcustom org-display-custom-times nil
  2358. "Non-nil means overlay custom formats over all time stamps.
  2359. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2360. To turn this on on a per-file basis, insert anywhere in the file:
  2361. #+STARTUP: customtime"
  2362. :group 'org-time
  2363. :set 'set-default
  2364. :type 'sexp)
  2365. (make-variable-buffer-local 'org-display-custom-times)
  2366. (defcustom org-time-stamp-custom-formats
  2367. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2368. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2369. These are overlaid over the default ISO format if the variable
  2370. `org-display-custom-times' is set. Time like %H:%M should be at the
  2371. end of the second format. The custom formats are also honored by export
  2372. commands, if custom time display is turned on at the time of export."
  2373. :group 'org-time
  2374. :type 'sexp)
  2375. (defun org-time-stamp-format (&optional long inactive)
  2376. "Get the right format for a time string."
  2377. (let ((f (if long (cdr org-time-stamp-formats)
  2378. (car org-time-stamp-formats))))
  2379. (if inactive
  2380. (concat "[" (substring f 1 -1) "]")
  2381. f)))
  2382. (defcustom org-time-clocksum-format
  2383. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2384. "The format string used when creating CLOCKSUM lines.
  2385. This is also used when Org mode generates a time duration.
  2386. The value can be a single format string containing two
  2387. %-sequences, which will be filled with the number of hours and
  2388. minutes in that order.
  2389. Alternatively, the value can be a plist associating any of the
  2390. keys :years, :months, :weeks, :days, :hours or :minutes with
  2391. format strings. The time duration is formatted using only the
  2392. time components that are needed and concatenating the results.
  2393. If a time unit in absent, it falls back to the next smallest
  2394. unit.
  2395. The keys :require-years, :require-months, :require-days,
  2396. :require-weeks, :require-hours, :require-minutes are also
  2397. meaningful. A non-nil value for these keys indicates that the
  2398. corresponding time component should always be included, even if
  2399. its value is 0.
  2400. For example,
  2401. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2402. :require-minutes t)
  2403. means durations longer than a day will be expressed in days,
  2404. hours and minutes, and durations less than a day will always be
  2405. expressed in hours and minutes (even for durations less than an
  2406. hour).
  2407. The value
  2408. \(:days \"%dd\" :minutes \"%dm\")
  2409. means durations longer than a day will be expressed in days and
  2410. minutes, and durations less than a day will be expressed entirely
  2411. in minutes (even for durations longer than an hour)."
  2412. :group 'org-time
  2413. :group 'org-clock
  2414. :version "24.3"
  2415. :type '(choice (string :tag "Format string")
  2416. (set :tag "Plist"
  2417. (group :inline t (const :tag "Years" :years)
  2418. (string :tag "Format string"))
  2419. (group :inline t
  2420. (const :tag "Always show years" :require-years)
  2421. (const t))
  2422. (group :inline t (const :tag "Months" :months)
  2423. (string :tag "Format string"))
  2424. (group :inline t
  2425. (const :tag "Always show months" :require-months)
  2426. (const t))
  2427. (group :inline t (const :tag "Weeks" :weeks)
  2428. (string :tag "Format string"))
  2429. (group :inline t
  2430. (const :tag "Always show weeks" :require-weeks)
  2431. (const t))
  2432. (group :inline t (const :tag "Days" :days)
  2433. (string :tag "Format string"))
  2434. (group :inline t
  2435. (const :tag "Always show days" :require-days)
  2436. (const t))
  2437. (group :inline t (const :tag "Hours" :hours)
  2438. (string :tag "Format string"))
  2439. (group :inline t
  2440. (const :tag "Always show hours" :require-hours)
  2441. (const t))
  2442. (group :inline t (const :tag "Minutes" :minutes)
  2443. (string :tag "Format string"))
  2444. (group :inline t
  2445. (const :tag "Always show minutes" :require-minutes)
  2446. (const t)))))
  2447. (defcustom org-time-clocksum-use-fractional nil
  2448. "When non-nil, \\[org-clock-display] uses fractional times.
  2449. See `org-time-clocksum-format' for more on time clock formats."
  2450. :group 'org-time
  2451. :group 'org-clock
  2452. :version "24.3"
  2453. :type 'boolean)
  2454. (defcustom org-time-clocksum-use-effort-durations t
  2455. "When non-nil, \\[org-clock-display] uses effort durations.
  2456. E.g. by default, one day is considered to be a 8 hours effort,
  2457. so a task that has been clocked for 16 hours will be displayed
  2458. as during 2 days in the clock display or in the clocktable.
  2459. See `org-effort-durations' on how to set effort durations
  2460. and `org-time-clocksum-format' for more on time clock formats."
  2461. :group 'org-time
  2462. :group 'org-clock
  2463. :version "24.3"
  2464. :type 'boolean)
  2465. (defcustom org-time-clocksum-fractional-format "%.2f"
  2466. "The format string used when creating CLOCKSUM lines,
  2467. or when Org mode generates a time duration, if
  2468. `org-time-clocksum-use-fractional' is enabled.
  2469. The value can be a single format string containing one
  2470. %-sequence, which will be filled with the number of hours as
  2471. a float.
  2472. Alternatively, the value can be a plist associating any of the
  2473. keys :years, :months, :weeks, :days, :hours or :minutes with
  2474. a format string. The time duration is formatted using the
  2475. largest time unit which gives a non-zero integer part. If all
  2476. specified formats have zero integer part, the smallest time unit
  2477. is used."
  2478. :group 'org-time
  2479. :type '(choice (string :tag "Format string")
  2480. (set (group :inline t (const :tag "Years" :years)
  2481. (string :tag "Format string"))
  2482. (group :inline t (const :tag "Months" :months)
  2483. (string :tag "Format string"))
  2484. (group :inline t (const :tag "Weeks" :weeks)
  2485. (string :tag "Format string"))
  2486. (group :inline t (const :tag "Days" :days)
  2487. (string :tag "Format string"))
  2488. (group :inline t (const :tag "Hours" :hours)
  2489. (string :tag "Format string"))
  2490. (group :inline t (const :tag "Minutes" :minutes)
  2491. (string :tag "Format string")))))
  2492. (defcustom org-deadline-warning-days 14
  2493. "No. of days before expiration during which a deadline becomes active.
  2494. This variable governs the display in sparse trees and in the agenda.
  2495. When 0 or negative, it means use this number (the absolute value of it)
  2496. even if a deadline has a different individual lead time specified.
  2497. Custom commands can set this variable in the options section."
  2498. :group 'org-time
  2499. :group 'org-agenda-daily/weekly
  2500. :type 'integer)
  2501. (defcustom org-read-date-prefer-future t
  2502. "Non-nil means assume future for incomplete date input from user.
  2503. This affects the following situations:
  2504. 1. The user gives a month but not a year.
  2505. For example, if it is April and you enter \"feb 2\", this will be read
  2506. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2507. 2. The user gives a day, but no month.
  2508. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2509. read this as the third of *next* month. However, if you enter \"17\",
  2510. it will be considered as *this* month.
  2511. If you set this variable to the symbol `time', then also the following
  2512. will work:
  2513. 3. If the user gives a time.
  2514. If the time is before now, it will be interpreted as tomorrow.
  2515. Currently none of this works for ISO week specifications.
  2516. When this option is nil, the current day, month and year will always be
  2517. used as defaults.
  2518. See also `org-agenda-jump-prefer-future'."
  2519. :group 'org-time
  2520. :type '(choice
  2521. (const :tag "Never" nil)
  2522. (const :tag "Check month and day" t)
  2523. (const :tag "Check month, day, and time" time)))
  2524. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2525. "Should the agenda jump command prefer the future for incomplete dates?
  2526. The default is to do the same as configured in `org-read-date-prefer-future'.
  2527. But you can also set a deviating value here.
  2528. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2529. :group 'org-agenda
  2530. :group 'org-time
  2531. :version "24.1"
  2532. :type '(choice
  2533. (const :tag "Use org-read-date-prefer-future"
  2534. org-read-date-prefer-future)
  2535. (const :tag "Never" nil)
  2536. (const :tag "Always" t)))
  2537. (defcustom org-read-date-force-compatible-dates t
  2538. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2539. Depending on the system Emacs is running on, certain dates cannot
  2540. be represented with the type used internally to represent time.
  2541. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2542. correctly. Some systems allow for earlier dates, some for later,
  2543. some for both. One way to find out it to insert any date into an
  2544. Org buffer, putting the cursor on the year and hitting S-up and
  2545. S-down to test the range.
  2546. When this variable is set to t, the date/time prompt will not let
  2547. you specify dates outside the 1970-2037 range, so it is certain that
  2548. these dates will work in whatever version of Emacs you are
  2549. running, and also that you can move a file from one Emacs implementation
  2550. to another. WHenever Org is forcing the year for you, it will display
  2551. a message and beep.
  2552. When this variable is nil, Org will check if the date is
  2553. representable in the specific Emacs implementation you are using.
  2554. If not, it will force a year, usually the current year, and beep
  2555. to remind you. Currently this setting is not recommended because
  2556. the likelihood that you will open your Org files in an Emacs that
  2557. has limited date range is not negligible.
  2558. A workaround for this problem is to use diary sexp dates for time
  2559. stamps outside of this range."
  2560. :group 'org-time
  2561. :version "24.1"
  2562. :type 'boolean)
  2563. (defcustom org-read-date-display-live t
  2564. "Non-nil means display current interpretation of date prompt live.
  2565. This display will be in an overlay, in the minibuffer."
  2566. :group 'org-time
  2567. :type 'boolean)
  2568. (defcustom org-read-date-popup-calendar t
  2569. "Non-nil means pop up a calendar when prompting for a date.
  2570. In the calendar, the date can be selected with mouse-1. However, the
  2571. minibuffer will also be active, and you can simply enter the date as well.
  2572. When nil, only the minibuffer will be available."
  2573. :group 'org-time
  2574. :type 'boolean)
  2575. (if (fboundp 'defvaralias)
  2576. (defvaralias 'org-popup-calendar-for-date-prompt
  2577. 'org-read-date-popup-calendar))
  2578. (defcustom org-read-date-minibuffer-setup-hook nil
  2579. "Hook to be used to set up keys for the date/time interface.
  2580. Add key definitions to `minibuffer-local-map', which will be a temporary
  2581. copy."
  2582. :group 'org-time
  2583. :type 'hook)
  2584. (defcustom org-extend-today-until 0
  2585. "The hour when your day really ends. Must be an integer.
  2586. This has influence for the following applications:
  2587. - When switching the agenda to \"today\". It it is still earlier than
  2588. the time given here, the day recognized as TODAY is actually yesterday.
  2589. - When a date is read from the user and it is still before the time given
  2590. here, the current date and time will be assumed to be yesterday, 23:59.
  2591. Also, timestamps inserted in capture templates follow this rule.
  2592. IMPORTANT: This is a feature whose implementation is and likely will
  2593. remain incomplete. Really, it is only here because past midnight seems to
  2594. be the favorite working time of John Wiegley :-)"
  2595. :group 'org-time
  2596. :type 'integer)
  2597. (defcustom org-use-effective-time nil
  2598. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2599. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2600. \"effective time\" of any timestamps between midnight and 8am will be
  2601. 23:59 of the previous day."
  2602. :group 'org-time
  2603. :version "24.1"
  2604. :type 'boolean)
  2605. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2606. "When non-nil, use the last clock out time for `org-todo'.
  2607. Note that this option has precedence over the combined use of
  2608. `org-use-effective-time' and `org-extend-today-until'."
  2609. :group 'org-time
  2610. ;; :version "24.3"
  2611. :type 'boolean)
  2612. (defcustom org-edit-timestamp-down-means-later nil
  2613. "Non-nil means S-down will increase the time in a time stamp.
  2614. When nil, S-up will increase."
  2615. :group 'org-time
  2616. :type 'boolean)
  2617. (defcustom org-calendar-follow-timestamp-change t
  2618. "Non-nil means make the calendar window follow timestamp changes.
  2619. When a timestamp is modified and the calendar window is visible, it will be
  2620. moved to the new date."
  2621. :group 'org-time
  2622. :type 'boolean)
  2623. (defgroup org-tags nil
  2624. "Options concerning tags in Org-mode."
  2625. :tag "Org Tags"
  2626. :group 'org)
  2627. (defcustom org-tag-alist nil
  2628. "List of tags allowed in Org-mode files.
  2629. When this list is nil, Org-mode will base TAG input on what is already in the
  2630. buffer.
  2631. The value of this variable is an alist, the car of each entry must be a
  2632. keyword as a string, the cdr may be a character that is used to select
  2633. that tag through the fast-tag-selection interface.
  2634. See the manual for details."
  2635. :group 'org-tags
  2636. :type '(repeat
  2637. (choice
  2638. (cons (string :tag "Tag name")
  2639. (character :tag "Access char"))
  2640. (list :tag "Start radio group"
  2641. (const :startgroup)
  2642. (option (string :tag "Group description")))
  2643. (list :tag "End radio group"
  2644. (const :endgroup)
  2645. (option (string :tag "Group description")))
  2646. (const :tag "New line" (:newline)))))
  2647. (defcustom org-tag-persistent-alist nil
  2648. "List of tags that will always appear in all Org-mode files.
  2649. This is in addition to any in buffer settings or customizations
  2650. of `org-tag-alist'.
  2651. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2652. The value of this variable is an alist, the car of each entry must be a
  2653. keyword as a string, the cdr may be a character that is used to select
  2654. that tag through the fast-tag-selection interface.
  2655. See the manual for details.
  2656. To disable these tags on a per-file basis, insert anywhere in the file:
  2657. #+STARTUP: noptag"
  2658. :group 'org-tags
  2659. :type '(repeat
  2660. (choice
  2661. (cons (string :tag "Tag name")
  2662. (character :tag "Access char"))
  2663. (const :tag "Start radio group" (:startgroup))
  2664. (const :tag "End radio group" (:endgroup))
  2665. (const :tag "New line" (:newline)))))
  2666. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2667. "If non-nil, always offer completion for all tags of all agenda files.
  2668. Instead of customizing this variable directly, you might want to
  2669. set it locally for capture buffers, because there no list of
  2670. tags in that file can be created dynamically (there are none).
  2671. (add-hook 'org-capture-mode-hook
  2672. (lambda ()
  2673. (set (make-local-variable
  2674. 'org-complete-tags-always-offer-all-agenda-tags)
  2675. t)))"
  2676. :group 'org-tags
  2677. :version "24.1"
  2678. :type 'boolean)
  2679. (defvar org-file-tags nil
  2680. "List of tags that can be inherited by all entries in the file.
  2681. The tags will be inherited if the variable `org-use-tag-inheritance'
  2682. says they should be.
  2683. This variable is populated from #+FILETAGS lines.")
  2684. (defcustom org-use-fast-tag-selection 'auto
  2685. "Non-nil means use fast tag selection scheme.
  2686. This is a special interface to select and deselect tags with single keys.
  2687. When nil, fast selection is never used.
  2688. When the symbol `auto', fast selection is used if and only if selection
  2689. characters for tags have been configured, either through the variable
  2690. `org-tag-alist' or through a #+TAGS line in the buffer.
  2691. When t, fast selection is always used and selection keys are assigned
  2692. automatically if necessary."
  2693. :group 'org-tags
  2694. :type '(choice
  2695. (const :tag "Always" t)
  2696. (const :tag "Never" nil)
  2697. (const :tag "When selection characters are configured" 'auto)))
  2698. (defcustom org-fast-tag-selection-single-key nil
  2699. "Non-nil means fast tag selection exits after first change.
  2700. When nil, you have to press RET to exit it.
  2701. During fast tag selection, you can toggle this flag with `C-c'.
  2702. This variable can also have the value `expert'. In this case, the window
  2703. displaying the tags menu is not even shown, until you press C-c again."
  2704. :group 'org-tags
  2705. :type '(choice
  2706. (const :tag "No" nil)
  2707. (const :tag "Yes" t)
  2708. (const :tag "Expert" expert)))
  2709. (defvar org-fast-tag-selection-include-todo nil
  2710. "Non-nil means fast tags selection interface will also offer TODO states.
  2711. This is an undocumented feature, you should not rely on it.")
  2712. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2713. "The column to which tags should be indented in a headline.
  2714. If this number is positive, it specifies the column. If it is negative,
  2715. it means that the tags should be flushright to that column. For example,
  2716. -80 works well for a normal 80 character screen.
  2717. When 0, place tags directly after headline text, with only one space in
  2718. between."
  2719. :group 'org-tags
  2720. :type 'integer)
  2721. (defcustom org-auto-align-tags t
  2722. "Non-nil keeps tags aligned when modifying headlines.
  2723. Some operations (i.e. demoting) change the length of a headline and
  2724. therefore shift the tags around. With this option turned on, after
  2725. each such operation the tags are again aligned to `org-tags-column'."
  2726. :group 'org-tags
  2727. :type 'boolean)
  2728. (defcustom org-use-tag-inheritance t
  2729. "Non-nil means tags in levels apply also for sublevels.
  2730. When nil, only the tags directly given in a specific line apply there.
  2731. This may also be a list of tags that should be inherited, or a regexp that
  2732. matches tags that should be inherited. Additional control is possible
  2733. with the variable `org-tags-exclude-from-inheritance' which gives an
  2734. explicit list of tags to be excluded from inheritance, even if the value of
  2735. `org-use-tag-inheritance' would select it for inheritance.
  2736. If this option is t, a match early-on in a tree can lead to a large
  2737. number of matches in the subtree when constructing the agenda or creating
  2738. a sparse tree. If you only want to see the first match in a tree during
  2739. a search, check out the variable `org-tags-match-list-sublevels'."
  2740. :group 'org-tags
  2741. :type '(choice
  2742. (const :tag "Not" nil)
  2743. (const :tag "Always" t)
  2744. (repeat :tag "Specific tags" (string :tag "Tag"))
  2745. (regexp :tag "Tags matched by regexp")))
  2746. (defcustom org-tags-exclude-from-inheritance nil
  2747. "List of tags that should never be inherited.
  2748. This is a way to exclude a few tags from inheritance. For way to do
  2749. the opposite, to actively allow inheritance for selected tags,
  2750. see the variable `org-use-tag-inheritance'."
  2751. :group 'org-tags
  2752. :type '(repeat (string :tag "Tag")))
  2753. (defun org-tag-inherit-p (tag)
  2754. "Check if TAG is one that should be inherited."
  2755. (cond
  2756. ((member tag org-tags-exclude-from-inheritance) nil)
  2757. ((eq org-use-tag-inheritance t) t)
  2758. ((not org-use-tag-inheritance) nil)
  2759. ((stringp org-use-tag-inheritance)
  2760. (string-match org-use-tag-inheritance tag))
  2761. ((listp org-use-tag-inheritance)
  2762. (member tag org-use-tag-inheritance))
  2763. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2764. (defcustom org-tags-match-list-sublevels t
  2765. "Non-nil means list also sublevels of headlines matching a search.
  2766. This variable applies to tags/property searches, and also to stuck
  2767. projects because this search is based on a tags match as well.
  2768. When set to the symbol `indented', sublevels are indented with
  2769. leading dots.
  2770. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2771. the sublevels of a headline matching a tag search often also match
  2772. the same search. Listing all of them can create very long lists.
  2773. Setting this variable to nil causes subtrees of a match to be skipped.
  2774. This variable is semi-obsolete and probably should always be true. It
  2775. is better to limit inheritance to certain tags using the variables
  2776. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2777. :group 'org-tags
  2778. :type '(choice
  2779. (const :tag "No, don't list them" nil)
  2780. (const :tag "Yes, do list them" t)
  2781. (const :tag "List them, indented with leading dots" indented)))
  2782. (defcustom org-tags-sort-function nil
  2783. "When set, tags are sorted using this function as a comparator."
  2784. :group 'org-tags
  2785. :type '(choice
  2786. (const :tag "No sorting" nil)
  2787. (const :tag "Alphabetical" string<)
  2788. (const :tag "Reverse alphabetical" string>)
  2789. (function :tag "Custom function" nil)))
  2790. (defvar org-tags-history nil
  2791. "History of minibuffer reads for tags.")
  2792. (defvar org-last-tags-completion-table nil
  2793. "The last used completion table for tags.")
  2794. (defvar org-after-tags-change-hook nil
  2795. "Hook that is run after the tags in a line have changed.")
  2796. (defgroup org-properties nil
  2797. "Options concerning properties in Org-mode."
  2798. :tag "Org Properties"
  2799. :group 'org)
  2800. (defcustom org-property-format "%-10s %s"
  2801. "How property key/value pairs should be formatted by `indent-line'.
  2802. When `indent-line' hits a property definition, it will format the line
  2803. according to this format, mainly to make sure that the values are
  2804. lined-up with respect to each other."
  2805. :group 'org-properties
  2806. :type 'string)
  2807. (defcustom org-properties-postprocess-alist nil
  2808. "Alist of properties and functions to adjust inserted values.
  2809. Elements of this alist must be of the form
  2810. ([string] [function])
  2811. where [string] must be a property name and [function] must be a
  2812. lambda expression: this lambda expression must take one argument,
  2813. the value to adjust, and return the new value as a string.
  2814. For example, this element will allow the property \"Remaining\"
  2815. to be updated wrt the relation between the \"Effort\" property
  2816. and the clock summary:
  2817. ((\"Remaining\" (lambda(value)
  2818. (let ((clocksum (org-clock-sum-current-item))
  2819. (effort (org-duration-string-to-minutes
  2820. (org-entry-get (point) \"Effort\"))))
  2821. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2822. :group 'org-properties
  2823. :version "24.1"
  2824. :type '(alist :key-type (string :tag "Property")
  2825. :value-type (function :tag "Function")))
  2826. (defcustom org-use-property-inheritance nil
  2827. "Non-nil means properties apply also for sublevels.
  2828. This setting is chiefly used during property searches. Turning it on can
  2829. cause significant overhead when doing a search, which is why it is not
  2830. on by default.
  2831. When nil, only the properties directly given in the current entry count.
  2832. When t, every property is inherited. The value may also be a list of
  2833. properties that should have inheritance, or a regular expression matching
  2834. properties that should be inherited.
  2835. However, note that some special properties use inheritance under special
  2836. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2837. and the properties ending in \"_ALL\" when they are used as descriptor
  2838. for valid values of a property.
  2839. Note for programmers:
  2840. When querying an entry with `org-entry-get', you can control if inheritance
  2841. should be used. By default, `org-entry-get' looks only at the local
  2842. properties. You can request inheritance by setting the inherit argument
  2843. to t (to force inheritance) or to `selective' (to respect the setting
  2844. in this variable)."
  2845. :group 'org-properties
  2846. :type '(choice
  2847. (const :tag "Not" nil)
  2848. (const :tag "Always" t)
  2849. (repeat :tag "Specific properties" (string :tag "Property"))
  2850. (regexp :tag "Properties matched by regexp")))
  2851. (defun org-property-inherit-p (property)
  2852. "Check if PROPERTY is one that should be inherited."
  2853. (cond
  2854. ((eq org-use-property-inheritance t) t)
  2855. ((not org-use-property-inheritance) nil)
  2856. ((stringp org-use-property-inheritance)
  2857. (string-match org-use-property-inheritance property))
  2858. ((listp org-use-property-inheritance)
  2859. (member property org-use-property-inheritance))
  2860. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2861. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2862. "The default column format, if no other format has been defined.
  2863. This variable can be set on the per-file basis by inserting a line
  2864. #+COLUMNS: %25ITEM ....."
  2865. :group 'org-properties
  2866. :type 'string)
  2867. (defcustom org-columns-ellipses ".."
  2868. "The ellipses to be used when a field in column view is truncated.
  2869. When this is the empty string, as many characters as possible are shown,
  2870. but then there will be no visual indication that the field has been truncated.
  2871. When this is a string of length N, the last N characters of a truncated
  2872. field are replaced by this string. If the column is narrower than the
  2873. ellipses string, only part of the ellipses string will be shown."
  2874. :group 'org-properties
  2875. :type 'string)
  2876. (defcustom org-columns-modify-value-for-display-function nil
  2877. "Function that modifies values for display in column view.
  2878. For example, it can be used to cut out a certain part from a time stamp.
  2879. The function must take 2 arguments:
  2880. column-title The title of the column (*not* the property name)
  2881. value The value that should be modified.
  2882. The function should return the value that should be displayed,
  2883. or nil if the normal value should be used."
  2884. :group 'org-properties
  2885. :type 'function)
  2886. (defcustom org-effort-property "Effort"
  2887. "The property that is being used to keep track of effort estimates.
  2888. Effort estimates given in this property need to have the format H:MM."
  2889. :group 'org-properties
  2890. :group 'org-progress
  2891. :type '(string :tag "Property"))
  2892. (defconst org-global-properties-fixed
  2893. '(("VISIBILITY_ALL" . "folded children content all")
  2894. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2895. "List of property/value pairs that can be inherited by any entry.
  2896. These are fixed values, for the preset properties. The user variable
  2897. that can be used to add to this list is `org-global-properties'.
  2898. The entries in this list are cons cells where the car is a property
  2899. name and cdr is a string with the value. If the value represents
  2900. multiple items like an \"_ALL\" property, separate the items by
  2901. spaces.")
  2902. (defcustom org-global-properties nil
  2903. "List of property/value pairs that can be inherited by any entry.
  2904. This list will be combined with the constant `org-global-properties-fixed'.
  2905. The entries in this list are cons cells where the car is a property
  2906. name and cdr is a string with the value.
  2907. You can set buffer-local values for the same purpose in the variable
  2908. `org-file-properties' this by adding lines like
  2909. #+PROPERTY: NAME VALUE"
  2910. :group 'org-properties
  2911. :type '(repeat
  2912. (cons (string :tag "Property")
  2913. (string :tag "Value"))))
  2914. (defvar org-file-properties nil
  2915. "List of property/value pairs that can be inherited by any entry.
  2916. Valid for the current buffer.
  2917. This variable is populated from #+PROPERTY lines.")
  2918. (make-variable-buffer-local 'org-file-properties)
  2919. (defgroup org-agenda nil
  2920. "Options concerning agenda views in Org-mode."
  2921. :tag "Org Agenda"
  2922. :group 'org)
  2923. (defvar org-category nil
  2924. "Variable used by org files to set a category for agenda display.
  2925. Such files should use a file variable to set it, for example
  2926. # -*- mode: org; org-category: \"ELisp\"
  2927. or contain a special line
  2928. #+CATEGORY: ELisp
  2929. If the file does not specify a category, then file's base name
  2930. is used instead.")
  2931. (make-variable-buffer-local 'org-category)
  2932. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2933. (defcustom org-agenda-files nil
  2934. "The files to be used for agenda display.
  2935. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2936. \\[org-remove-file]. You can also use customize to edit the list.
  2937. If an entry is a directory, all files in that directory that are matched by
  2938. `org-agenda-file-regexp' will be part of the file list.
  2939. If the value of the variable is not a list but a single file name, then
  2940. the list of agenda files is actually stored and maintained in that file, one
  2941. agenda file per line. In this file paths can be given relative to
  2942. `org-directory'. Tilde expansion and environment variable substitution
  2943. are also made."
  2944. :group 'org-agenda
  2945. :type '(choice
  2946. (repeat :tag "List of files and directories" file)
  2947. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2948. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2949. "Regular expression to match files for `org-agenda-files'.
  2950. If any element in the list in that variable contains a directory instead
  2951. of a normal file, all files in that directory that are matched by this
  2952. regular expression will be included."
  2953. :group 'org-agenda
  2954. :type 'regexp)
  2955. (defcustom org-agenda-text-search-extra-files nil
  2956. "List of extra files to be searched by text search commands.
  2957. These files will be search in addition to the agenda files by the
  2958. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2959. Note that these files will only be searched for text search commands,
  2960. not for the other agenda views like todo lists, tag searches or the weekly
  2961. agenda. This variable is intended to list notes and possibly archive files
  2962. that should also be searched by these two commands.
  2963. In fact, if the first element in the list is the symbol `agenda-archives',
  2964. than all archive files of all agenda files will be added to the search
  2965. scope."
  2966. :group 'org-agenda
  2967. :type '(set :greedy t
  2968. (const :tag "Agenda Archives" agenda-archives)
  2969. (repeat :inline t (file))))
  2970. (if (fboundp 'defvaralias)
  2971. (defvaralias 'org-agenda-multi-occur-extra-files
  2972. 'org-agenda-text-search-extra-files))
  2973. (defcustom org-agenda-skip-unavailable-files nil
  2974. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2975. A nil value means to remove them, after a query, from the list."
  2976. :group 'org-agenda
  2977. :type 'boolean)
  2978. (defcustom org-calendar-to-agenda-key [?c]
  2979. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2980. The command `org-calendar-goto-agenda' will be bound to this key. The
  2981. default is the character `c' because then `c' can be used to switch back and
  2982. forth between agenda and calendar."
  2983. :group 'org-agenda
  2984. :type 'sexp)
  2985. (defcustom org-calendar-insert-diary-entry-key [?i]
  2986. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2987. This option is irrelevant until `org-agenda-diary-file' has been configured
  2988. to point to an Org-mode file. When that is the case, the command
  2989. `org-agenda-diary-entry' will be bound to the key given here, by default
  2990. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2991. if you want to continue doing this, you need to change this to a different
  2992. key."
  2993. :group 'org-agenda
  2994. :type 'sexp)
  2995. (defcustom org-agenda-diary-file 'diary-file
  2996. "File to which to add new entries with the `i' key in agenda and calendar.
  2997. When this is the symbol `diary-file', the functionality in the Emacs
  2998. calendar will be used to add entries to the `diary-file'. But when this
  2999. points to a file, `org-agenda-diary-entry' will be used instead."
  3000. :group 'org-agenda
  3001. :type '(choice
  3002. (const :tag "The standard Emacs diary file" diary-file)
  3003. (file :tag "Special Org file diary entries")))
  3004. (eval-after-load "calendar"
  3005. '(progn
  3006. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3007. 'org-calendar-goto-agenda)
  3008. (add-hook 'calendar-mode-hook
  3009. (lambda ()
  3010. (unless (eq org-agenda-diary-file 'diary-file)
  3011. (define-key calendar-mode-map
  3012. org-calendar-insert-diary-entry-key
  3013. 'org-agenda-diary-entry))))))
  3014. (defgroup org-latex nil
  3015. "Options for embedding LaTeX code into Org-mode."
  3016. :tag "Org LaTeX"
  3017. :group 'org)
  3018. (defcustom org-format-latex-options
  3019. '(:foreground default :background default :scale 1.0
  3020. :html-foreground "Black" :html-background "Transparent"
  3021. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3022. "Options for creating images from LaTeX fragments.
  3023. This is a property list with the following properties:
  3024. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3025. `default' means use the foreground of the default face.
  3026. `auto' means use the foreground from the text face.
  3027. :background the background color, or \"Transparent\".
  3028. `default' means use the background of the default face.
  3029. `auto' means use the background from the text face.
  3030. :scale a scaling factor for the size of the images, to get more pixels
  3031. :html-foreground, :html-background, :html-scale
  3032. the same numbers for HTML export.
  3033. :matchers a list indicating which matchers should be used to
  3034. find LaTeX fragments. Valid members of this list are:
  3035. \"begin\" find environments
  3036. \"$1\" find single characters surrounded by $.$
  3037. \"$\" find math expressions surrounded by $...$
  3038. \"$$\" find math expressions surrounded by $$....$$
  3039. \"\\(\" find math expressions surrounded by \\(...\\)
  3040. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3041. :group 'org-latex
  3042. :type 'plist)
  3043. (defcustom org-format-latex-signal-error t
  3044. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3045. When nil, just push out a message."
  3046. :group 'org-latex
  3047. :version "24.1"
  3048. :type 'boolean)
  3049. (defcustom org-latex-to-mathml-jar-file nil
  3050. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3051. Use this to specify additional executable file say a jar file.
  3052. When using MathToWeb as the converter, specify the full-path to
  3053. your mathtoweb.jar file."
  3054. :group 'org-latex
  3055. :version "24.1"
  3056. :type '(choice
  3057. (const :tag "None" nil)
  3058. (file :tag "JAR file" :must-match t)))
  3059. (defcustom org-latex-to-mathml-convert-command nil
  3060. "Command to convert LaTeX fragments to MathML.
  3061. Replace format-specifiers in the command as noted below and use
  3062. `shell-command' to convert LaTeX to MathML.
  3063. %j: Executable file in fully expanded form as specified by
  3064. `org-latex-to-mathml-jar-file'.
  3065. %I: Input LaTeX file in fully expanded form
  3066. %o: Output MathML file
  3067. This command is used by `org-create-math-formula'.
  3068. When using MathToWeb as the converter, set this to
  3069. \"java -jar %j -unicode -force -df %o %I\"."
  3070. :group 'org-latex
  3071. :version "24.1"
  3072. :type '(choice
  3073. (const :tag "None" nil)
  3074. (string :tag "\nShell command")))
  3075. (defcustom org-latex-create-formula-image-program 'dvipng
  3076. "Program to convert LaTeX fragments with.
  3077. dvipng Process the LaTeX fragments to dvi file, then convert
  3078. dvi files to png files using dvipng.
  3079. This will also include processing of non-math environments.
  3080. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3081. to convert pdf files to png files"
  3082. :group 'org-latex
  3083. :version "24.1"
  3084. :type '(choice
  3085. (const :tag "dvipng" dvipng)
  3086. (const :tag "imagemagick" imagemagick)))
  3087. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3088. "Path to store latex preview images.
  3089. A relative path here creates many directories relative to the
  3090. processed org files paths. An absolute path puts all preview
  3091. images at the same place."
  3092. :group 'org-latex
  3093. :version "24.3"
  3094. :type 'string)
  3095. (defun org-format-latex-mathml-available-p ()
  3096. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3097. (save-match-data
  3098. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3099. org-latex-to-mathml-convert-command)
  3100. (let ((executable (car (split-string
  3101. org-latex-to-mathml-convert-command))))
  3102. (when (executable-find executable)
  3103. (if (string-match
  3104. "%j" org-latex-to-mathml-convert-command)
  3105. (file-readable-p org-latex-to-mathml-jar-file)
  3106. t))))))
  3107. (defcustom org-format-latex-header "\\documentclass{article}
  3108. \\usepackage[usenames]{color}
  3109. \\usepackage{amsmath}
  3110. \\usepackage[mathscr]{eucal}
  3111. \\pagestyle{empty} % do not remove
  3112. \[PACKAGES]
  3113. \[DEFAULT-PACKAGES]
  3114. % The settings below are copied from fullpage.sty
  3115. \\setlength{\\textwidth}{\\paperwidth}
  3116. \\addtolength{\\textwidth}{-3cm}
  3117. \\setlength{\\oddsidemargin}{1.5cm}
  3118. \\addtolength{\\oddsidemargin}{-2.54cm}
  3119. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3120. \\setlength{\\textheight}{\\paperheight}
  3121. \\addtolength{\\textheight}{-\\headheight}
  3122. \\addtolength{\\textheight}{-\\headsep}
  3123. \\addtolength{\\textheight}{-\\footskip}
  3124. \\addtolength{\\textheight}{-3cm}
  3125. \\setlength{\\topmargin}{1.5cm}
  3126. \\addtolength{\\topmargin}{-2.54cm}"
  3127. "The document header used for processing LaTeX fragments.
  3128. It is imperative that this header make sure that no page number
  3129. appears on the page. The package defined in the variables
  3130. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  3131. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  3132. will be appended."
  3133. :group 'org-latex
  3134. :type 'string)
  3135. (defvar org-format-latex-header-extra nil)
  3136. (defun org-set-packages-alist (var val)
  3137. "Set the packages alist and make sure it has 3 elements per entry."
  3138. (set var (mapcar (lambda (x)
  3139. (if (and (consp x) (= (length x) 2))
  3140. (list (car x) (nth 1 x) t)
  3141. x))
  3142. val)))
  3143. (defun org-get-packages-alist (var)
  3144. "Get the packages alist and make sure it has 3 elements per entry."
  3145. (mapcar (lambda (x)
  3146. (if (and (consp x) (= (length x) 2))
  3147. (list (car x) (nth 1 x) t)
  3148. x))
  3149. (default-value var)))
  3150. ;; The following variables are defined here because is it also used
  3151. ;; when formatting latex fragments. Originally it was part of the
  3152. ;; LaTeX exporter, which is why the name includes "export".
  3153. (defcustom org-export-latex-default-packages-alist
  3154. '(("AUTO" "inputenc" t)
  3155. ("T1" "fontenc" t)
  3156. ("" "fixltx2e" nil)
  3157. ("" "graphicx" t)
  3158. ("" "longtable" nil)
  3159. ("" "float" nil)
  3160. ("" "wrapfig" nil)
  3161. ("" "soul" t)
  3162. ("" "textcomp" t)
  3163. ("" "marvosym" t)
  3164. ("" "wasysym" t)
  3165. ("" "latexsym" t)
  3166. ("" "amssymb" t)
  3167. ("" "hyperref" nil)
  3168. "\\tolerance=1000"
  3169. )
  3170. "Alist of default packages to be inserted in the header.
  3171. Change this only if one of the packages here causes an incompatibility
  3172. with another package you are using.
  3173. The packages in this list are needed by one part or another of Org-mode
  3174. to function properly.
  3175. - inputenc, fontenc: for basic font and character selection
  3176. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3177. for interpreting the entities in `org-entities'. You can skip some of these
  3178. packages if you don't use any of the symbols in it.
  3179. - graphicx: for including images
  3180. - float, wrapfig: for figure placement
  3181. - longtable: for long tables
  3182. - hyperref: for cross references
  3183. Therefore you should not modify this variable unless you know what you
  3184. are doing. The one reason to change it anyway is that you might be loading
  3185. some other package that conflicts with one of the default packages.
  3186. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3187. If SNIPPET-FLAG is t, the package also needs to be included when
  3188. compiling LaTeX snippets into images for inclusion into HTML."
  3189. :group 'org-export-latex
  3190. :set 'org-set-packages-alist
  3191. :get 'org-get-packages-alist
  3192. :version "24.1"
  3193. :type '(repeat
  3194. (choice
  3195. (list :tag "options/package pair"
  3196. (string :tag "options")
  3197. (string :tag "package")
  3198. (boolean :tag "Snippet"))
  3199. (string :tag "A line of LaTeX"))))
  3200. (defcustom org-export-latex-packages-alist nil
  3201. "Alist of packages to be inserted in every LaTeX header.
  3202. These will be inserted after `org-export-latex-default-packages-alist'.
  3203. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3204. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3205. turning LaTeX snippets into images for inclusion into HTML.
  3206. Make sure that you only list packages here which:
  3207. - you want in every file
  3208. - do not conflict with the default packages in
  3209. `org-export-latex-default-packages-alist'
  3210. - do not conflict with the setup in `org-format-latex-header'."
  3211. :group 'org-export-latex
  3212. :set 'org-set-packages-alist
  3213. :get 'org-get-packages-alist
  3214. :type '(repeat
  3215. (choice
  3216. (list :tag "options/package pair"
  3217. (string :tag "options")
  3218. (string :tag "package")
  3219. (boolean :tag "Snippet"))
  3220. (string :tag "A line of LaTeX"))))
  3221. (defgroup org-appearance nil
  3222. "Settings for Org-mode appearance."
  3223. :tag "Org Appearance"
  3224. :group 'org)
  3225. (defcustom org-level-color-stars-only nil
  3226. "Non-nil means fontify only the stars in each headline.
  3227. When nil, the entire headline is fontified.
  3228. Changing it requires restart of `font-lock-mode' to become effective
  3229. also in regions already fontified."
  3230. :group 'org-appearance
  3231. :type 'boolean)
  3232. (defcustom org-hide-leading-stars nil
  3233. "Non-nil means hide the first N-1 stars in a headline.
  3234. This works by using the face `org-hide' for these stars. This
  3235. face is white for a light background, and black for a dark
  3236. background. You may have to customize the face `org-hide' to
  3237. make this work.
  3238. Changing it requires restart of `font-lock-mode' to become effective
  3239. also in regions already fontified.
  3240. You may also set this on a per-file basis by adding one of the following
  3241. lines to the buffer:
  3242. #+STARTUP: hidestars
  3243. #+STARTUP: showstars"
  3244. :group 'org-appearance
  3245. :type 'boolean)
  3246. (defcustom org-hidden-keywords nil
  3247. "List of symbols corresponding to keywords to be hidden the org buffer.
  3248. For example, a value '(title) for this list will make the document's title
  3249. appear in the buffer without the initial #+TITLE: keyword."
  3250. :group 'org-appearance
  3251. :version "24.1"
  3252. :type '(set (const :tag "#+AUTHOR" author)
  3253. (const :tag "#+DATE" date)
  3254. (const :tag "#+EMAIL" email)
  3255. (const :tag "#+TITLE" title)))
  3256. (defcustom org-custom-properties nil
  3257. "List of properties (as strings) with a special meaning.
  3258. The default use of these custom properties is to let the user
  3259. hide them with `org-toggle-custom-properties-visibility'."
  3260. :group 'org-properties
  3261. :group 'org-appearance
  3262. :version "24.3"
  3263. :type '(repeat (string :tag "Property Name")))
  3264. (defcustom org-fontify-done-headline nil
  3265. "Non-nil means change the face of a headline if it is marked DONE.
  3266. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3267. When this is non-nil, the headline after the keyword is set to the
  3268. `org-headline-done' as an additional indication."
  3269. :group 'org-appearance
  3270. :type 'boolean)
  3271. (defcustom org-fontify-emphasized-text t
  3272. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3273. Changing this variable requires a restart of Emacs to take effect."
  3274. :group 'org-appearance
  3275. :type 'boolean)
  3276. (defcustom org-fontify-whole-heading-line nil
  3277. "Non-nil means fontify the whole line for headings.
  3278. This is useful when setting a background color for the
  3279. org-level-* faces."
  3280. :group 'org-appearance
  3281. :type 'boolean)
  3282. (defcustom org-highlight-latex-fragments-and-specials nil
  3283. "Non-nil means fontify what is treated specially by the exporters."
  3284. :group 'org-appearance
  3285. :type 'boolean)
  3286. (defcustom org-hide-emphasis-markers nil
  3287. "Non-nil mean font-lock should hide the emphasis marker characters."
  3288. :group 'org-appearance
  3289. :type 'boolean)
  3290. (defcustom org-pretty-entities nil
  3291. "Non-nil means show entities as UTF8 characters.
  3292. When nil, the \\name form remains in the buffer."
  3293. :group 'org-appearance
  3294. :version "24.1"
  3295. :type 'boolean)
  3296. (defcustom org-pretty-entities-include-sub-superscripts t
  3297. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3298. :group 'org-appearance
  3299. :version "24.1"
  3300. :type 'boolean)
  3301. (defvar org-emph-re nil
  3302. "Regular expression for matching emphasis.
  3303. After a match, the match groups contain these elements:
  3304. 0 The match of the full regular expression, including the characters
  3305. before and after the proper match
  3306. 1 The character before the proper match, or empty at beginning of line
  3307. 2 The proper match, including the leading and trailing markers
  3308. 3 The leading marker like * or /, indicating the type of highlighting
  3309. 4 The text between the emphasis markers, not including the markers
  3310. 5 The character after the match, empty at the end of a line")
  3311. (defvar org-verbatim-re nil
  3312. "Regular expression for matching verbatim text.")
  3313. (defvar org-emphasis-regexp-components) ; defined just below
  3314. (defvar org-emphasis-alist) ; defined just below
  3315. (defun org-set-emph-re (var val)
  3316. "Set variable and compute the emphasis regular expression."
  3317. (set var val)
  3318. (when (and (boundp 'org-emphasis-alist)
  3319. (boundp 'org-emphasis-regexp-components)
  3320. org-emphasis-alist org-emphasis-regexp-components)
  3321. (let* ((e org-emphasis-regexp-components)
  3322. (pre (car e))
  3323. (post (nth 1 e))
  3324. (border (nth 2 e))
  3325. (body (nth 3 e))
  3326. (nl (nth 4 e))
  3327. (body1 (concat body "*?"))
  3328. (markers (mapconcat 'car org-emphasis-alist ""))
  3329. (vmarkers (mapconcat
  3330. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3331. org-emphasis-alist "")))
  3332. ;; make sure special characters appear at the right position in the class
  3333. (if (string-match "\\^" markers)
  3334. (setq markers (concat (replace-match "" t t markers) "^")))
  3335. (if (string-match "-" markers)
  3336. (setq markers (concat (replace-match "" t t markers) "-")))
  3337. (if (string-match "\\^" vmarkers)
  3338. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3339. (if (string-match "-" vmarkers)
  3340. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3341. (if (> nl 0)
  3342. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3343. (int-to-string nl) "\\}")))
  3344. ;; Make the regexp
  3345. (setq org-emph-re
  3346. (concat "\\([" pre "]\\|^\\)"
  3347. "\\("
  3348. "\\([" markers "]\\)"
  3349. "\\("
  3350. "[^" border "]\\|"
  3351. "[^" border "]"
  3352. body1
  3353. "[^" border "]"
  3354. "\\)"
  3355. "\\3\\)"
  3356. "\\([" post "]\\|$\\)"))
  3357. (setq org-verbatim-re
  3358. (concat "\\([" pre "]\\|^\\)"
  3359. "\\("
  3360. "\\([" vmarkers "]\\)"
  3361. "\\("
  3362. "[^" border "]\\|"
  3363. "[^" border "]"
  3364. body1
  3365. "[^" border "]"
  3366. "\\)"
  3367. "\\3\\)"
  3368. "\\([" post "]\\|$\\)")))))
  3369. (defcustom org-emphasis-regexp-components
  3370. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3371. "Components used to build the regular expression for emphasis.
  3372. This is a list with five entries. Terminology: In an emphasis string
  3373. like \" *strong word* \", we call the initial space PREMATCH, the final
  3374. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3375. and \"trong wor\" is the body. The different components in this variable
  3376. specify what is allowed/forbidden in each part:
  3377. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3378. post Chars allowed as postmatch. End of line will be allowed too.
  3379. border The chars *forbidden* as border characters.
  3380. body-regexp A regexp like \".\" to match a body character. Don't use
  3381. non-shy groups here, and don't allow newline here.
  3382. newline The maximum number of newlines allowed in an emphasis exp.
  3383. Use customize to modify this, or restart Emacs after changing it."
  3384. :group 'org-appearance
  3385. :set 'org-set-emph-re
  3386. :type '(list
  3387. (sexp :tag "Allowed chars in pre ")
  3388. (sexp :tag "Allowed chars in post ")
  3389. (sexp :tag "Forbidden chars in border ")
  3390. (sexp :tag "Regexp for body ")
  3391. (integer :tag "number of newlines allowed")
  3392. (option (boolean :tag "Please ignore this button"))))
  3393. (defcustom org-emphasis-alist
  3394. `(("*" bold "<b>" "</b>")
  3395. ("/" italic "<i>" "</i>")
  3396. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3397. ("=" org-code "<code>" "</code>" verbatim)
  3398. ("~" org-verbatim "<code>" "</code>" verbatim)
  3399. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3400. "<del>" "</del>")
  3401. )
  3402. "Special syntax for emphasized text.
  3403. Text starting and ending with a special character will be emphasized, for
  3404. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3405. characters, the face to be used by font-lock for highlighting in Org-mode
  3406. Emacs buffers, and the HTML tags to be used for this.
  3407. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3408. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3409. Use customize to modify this, or restart Emacs after changing it."
  3410. :group 'org-appearance
  3411. :set 'org-set-emph-re
  3412. :type '(repeat
  3413. (list
  3414. (string :tag "Marker character")
  3415. (choice
  3416. (face :tag "Font-lock-face")
  3417. (plist :tag "Face property list"))
  3418. (string :tag "HTML start tag")
  3419. (string :tag "HTML end tag")
  3420. (option (const verbatim)))))
  3421. (defvar org-syntax-table
  3422. (let ((st (make-syntax-table)))
  3423. (mapc (lambda(c) (modify-syntax-entry
  3424. (string-to-char (car c)) "w p" st))
  3425. org-emphasis-alist)
  3426. st))
  3427. (defvar org-protecting-blocks
  3428. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3429. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3430. This is needed for font-lock setup.")
  3431. ;;; Miscellaneous options
  3432. (defgroup org-completion nil
  3433. "Completion in Org-mode."
  3434. :tag "Org Completion"
  3435. :group 'org)
  3436. (defcustom org-completion-use-ido nil
  3437. "Non-nil means use ido completion wherever possible.
  3438. Note that `ido-mode' must be active for this variable to be relevant.
  3439. If you decide to turn this variable on, you might well want to turn off
  3440. `org-outline-path-complete-in-steps'.
  3441. See also `org-completion-use-iswitchb'."
  3442. :group 'org-completion
  3443. :type 'boolean)
  3444. (defcustom org-completion-use-iswitchb nil
  3445. "Non-nil means use iswitchb completion wherever possible.
  3446. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3447. If you decide to turn this variable on, you might well want to turn off
  3448. `org-outline-path-complete-in-steps'.
  3449. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3450. :group 'org-completion
  3451. :type 'boolean)
  3452. (defcustom org-completion-fallback-command 'hippie-expand
  3453. "The expansion command called by \\[pcomplete] in normal context.
  3454. Normal means, no org-mode-specific context."
  3455. :group 'org-completion
  3456. :type 'function)
  3457. ;;; Functions and variables from their packages
  3458. ;; Declared here to avoid compiler warnings
  3459. ;; XEmacs only
  3460. (defvar outline-mode-menu-heading)
  3461. (defvar outline-mode-menu-show)
  3462. (defvar outline-mode-menu-hide)
  3463. (defvar zmacs-regions) ; XEmacs regions
  3464. ;; Emacs only
  3465. (defvar mark-active)
  3466. ;; Various packages
  3467. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3468. (declare-function calendar-forward-day "cal-move" (arg))
  3469. (declare-function calendar-goto-date "cal-move" (date))
  3470. (declare-function calendar-goto-today "cal-move" ())
  3471. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3472. (defvar calc-embedded-close-formula)
  3473. (defvar calc-embedded-open-formula)
  3474. (declare-function cdlatex-tab "ext:cdlatex" ())
  3475. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3476. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3477. (defvar font-lock-unfontify-region-function)
  3478. (declare-function iswitchb-read-buffer "iswitchb"
  3479. (prompt &optional default require-match start matches-set))
  3480. (defvar iswitchb-temp-buflist)
  3481. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3482. (defvar org-agenda-tags-todo-honor-ignore-options)
  3483. (declare-function org-agenda-skip "org-agenda" ())
  3484. (declare-function
  3485. org-agenda-format-item "org-agenda"
  3486. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3487. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3488. (declare-function org-agenda-change-all-lines "org-agenda"
  3489. (newhead hdmarker &optional fixface just-this))
  3490. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3491. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3492. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3493. (beg end))
  3494. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3495. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3496. "org-agenda" (&optional end))
  3497. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3498. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3499. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3500. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3501. (declare-function org-indent-mode "org-indent" (&optional arg))
  3502. (declare-function parse-time-string "parse-time" (string))
  3503. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3504. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3505. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3506. (defvar remember-data-file)
  3507. (defvar texmathp-why)
  3508. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3509. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3510. (defvar org-latex-regexps)
  3511. ;;; Autoload and prepare some org modules
  3512. ;; Some table stuff that needs to be defined here, because it is used
  3513. ;; by the functions setting up org-mode or checking for table context.
  3514. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3515. "Detect an org-type or table-type table.")
  3516. (defconst org-table-line-regexp "^[ \t]*|"
  3517. "Detect an org-type table line.")
  3518. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3519. "Detect an org-type table line.")
  3520. (defconst org-table-hline-regexp "^[ \t]*|-"
  3521. "Detect an org-type table hline.")
  3522. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3523. "Detect a table-type table hline.")
  3524. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3525. "Detect the first line outside a table when searching from within it.
  3526. This works for both table types.")
  3527. ;; Autoload the functions in org-table.el that are needed by functions here.
  3528. (eval-and-compile
  3529. (org-autoload "org-table"
  3530. '(org-table-begin org-table-blank-field org-table-end)))
  3531. ;;;###autoload
  3532. (defun turn-on-orgtbl ()
  3533. "Unconditionally turn on `orgtbl-mode'."
  3534. (require 'org-table)
  3535. (orgtbl-mode 1))
  3536. (defun org-at-table-p (&optional table-type)
  3537. "Return t if the cursor is inside an org-type table.
  3538. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3539. (if org-enable-table-editor
  3540. (save-excursion
  3541. (beginning-of-line 1)
  3542. (looking-at (if table-type org-table-any-line-regexp
  3543. org-table-line-regexp)))
  3544. nil))
  3545. (defsubst org-table-p () (org-at-table-p))
  3546. (defun org-at-table.el-p ()
  3547. "Return t if and only if we are at a table.el table."
  3548. (and (org-at-table-p 'any)
  3549. (save-excursion
  3550. (goto-char (org-table-begin 'any))
  3551. (looking-at org-table1-hline-regexp))))
  3552. (defun org-table-recognize-table.el ()
  3553. "If there is a table.el table nearby, recognize it and move into it."
  3554. (if org-table-tab-recognizes-table.el
  3555. (if (org-at-table.el-p)
  3556. (progn
  3557. (beginning-of-line 1)
  3558. (if (looking-at org-table-dataline-regexp)
  3559. nil
  3560. (if (looking-at org-table1-hline-regexp)
  3561. (progn
  3562. (beginning-of-line 2)
  3563. (if (looking-at org-table-any-border-regexp)
  3564. (beginning-of-line -1)))))
  3565. (if (re-search-forward "|" (org-table-end t) t)
  3566. (progn
  3567. (require 'table)
  3568. (if (table--at-cell-p (point))
  3569. t
  3570. (message "recognizing table.el table...")
  3571. (table-recognize-table)
  3572. (message "recognizing table.el table...done")))
  3573. (error "This should not happen"))
  3574. t)
  3575. nil)
  3576. nil))
  3577. (defun org-at-table-hline-p ()
  3578. "Return t if the cursor is inside a hline in a table."
  3579. (if org-enable-table-editor
  3580. (save-excursion
  3581. (beginning-of-line 1)
  3582. (looking-at org-table-hline-regexp))
  3583. nil))
  3584. (defvar org-table-clean-did-remove-column nil)
  3585. (defun org-table-map-tables (function &optional quietly)
  3586. "Apply FUNCTION to the start of all tables in the buffer."
  3587. (save-excursion
  3588. (save-restriction
  3589. (widen)
  3590. (goto-char (point-min))
  3591. (while (re-search-forward org-table-any-line-regexp nil t)
  3592. (unless quietly
  3593. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3594. (beginning-of-line 1)
  3595. (when (and (looking-at org-table-line-regexp)
  3596. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3597. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3598. (save-excursion (funcall function))
  3599. (or (looking-at org-table-line-regexp)
  3600. (forward-char 1)))
  3601. (re-search-forward org-table-any-border-regexp nil 1))))
  3602. (unless quietly (message "Mapping tables: done")))
  3603. ;; Declare and autoload functions from org-exp.el & Co
  3604. (declare-function org-default-export-plist "org-exp")
  3605. (declare-function org-infile-export-plist "org-exp")
  3606. (declare-function org-get-current-options "org-exp")
  3607. ;; Declare and autoload functions from org-agenda.el
  3608. (eval-and-compile
  3609. (org-autoload "org-agenda"
  3610. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3611. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3612. (declare-function org-clock-update-mode-line "org-clock" ())
  3613. (declare-function org-resolve-clocks "org-clock"
  3614. (&optional also-non-dangling-p prompt last-valid))
  3615. (defvar org-clock-start-time)
  3616. (defvar org-clock-marker (make-marker)
  3617. "Marker recording the last clock-in.")
  3618. (defvar org-clock-hd-marker (make-marker)
  3619. "Marker recording the last clock-in, but the headline position.")
  3620. (defvar org-clock-heading ""
  3621. "The heading of the current clock entry.")
  3622. (defun org-clock-is-active ()
  3623. "Return non-nil if clock is currently running.
  3624. The return value is actually the clock marker."
  3625. (marker-buffer org-clock-marker))
  3626. (eval-and-compile
  3627. (org-autoload "org-clock" '(org-clock-remove-overlays
  3628. org-clock-update-time-maybe
  3629. org-clocktable-shift)))
  3630. (defun org-check-running-clock ()
  3631. "Check if the current buffer contains the running clock.
  3632. If yes, offer to stop it and to save the buffer with the changes."
  3633. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3634. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3635. (buffer-name))))
  3636. (org-clock-out)
  3637. (when (y-or-n-p "Save changed buffer?")
  3638. (save-buffer))))
  3639. (defun org-clocktable-try-shift (dir n)
  3640. "Check if this line starts a clock table, if yes, shift the time block."
  3641. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3642. (org-clocktable-shift dir n)))
  3643. ;;;###autoload
  3644. (defun org-clock-persistence-insinuate ()
  3645. "Set up hooks for clock persistence."
  3646. (require 'org-clock)
  3647. (add-hook 'org-mode-hook 'org-clock-load)
  3648. (add-hook 'kill-emacs-hook 'org-clock-save))
  3649. ;; Define the variable already here, to make sure we have it.
  3650. (defvar org-indent-mode nil
  3651. "Non-nil if Org-Indent mode is enabled.
  3652. Use the command `org-indent-mode' to change this variable.")
  3653. ;; Autoload archiving code
  3654. ;; The stuff that is needed for cycling and tags has to be defined here.
  3655. (defgroup org-archive nil
  3656. "Options concerning archiving in Org-mode."
  3657. :tag "Org Archive"
  3658. :group 'org-structure)
  3659. (defcustom org-archive-location "%s_archive::"
  3660. "The location where subtrees should be archived.
  3661. The value of this variable is a string, consisting of two parts,
  3662. separated by a double-colon. The first part is a filename and
  3663. the second part is a headline.
  3664. When the filename is omitted, archiving happens in the same file.
  3665. %s in the filename will be replaced by the current file
  3666. name (without the directory part). Archiving to a different file
  3667. is useful to keep archived entries from contributing to the
  3668. Org-mode Agenda.
  3669. The archived entries will be filed as subtrees of the specified
  3670. headline. When the headline is omitted, the subtrees are simply
  3671. filed away at the end of the file, as top-level entries. Also in
  3672. the heading you can use %s to represent the file name, this can be
  3673. useful when using the same archive for a number of different files.
  3674. Here are a few examples:
  3675. \"%s_archive::\"
  3676. If the current file is Projects.org, archive in file
  3677. Projects.org_archive, as top-level trees. This is the default.
  3678. \"::* Archived Tasks\"
  3679. Archive in the current file, under the top-level headline
  3680. \"* Archived Tasks\".
  3681. \"~/org/archive.org::\"
  3682. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3683. \"~/org/archive.org::* From %s\"
  3684. Archive in file ~/org/archive.org (absolute path), under headlines
  3685. \"From FILENAME\" where file name is the current file name.
  3686. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3687. The \"datetree/\" string is special, signifying to archive
  3688. items to the datetree. Items are placed in either the CLOSED
  3689. date of the item, or the current date if there is no CLOSED date.
  3690. The heading will be a subentry to the current date. There doesn't
  3691. need to be a heading, but there always needs to be a slash after
  3692. datetree. For example, to store archived items directly in the
  3693. datetree, use \"~/org/datetree.org::datetree/\".
  3694. \"basement::** Finished Tasks\"
  3695. Archive in file ./basement (relative path), as level 3 trees
  3696. below the level 2 heading \"** Finished Tasks\".
  3697. You may set this option on a per-file basis by adding to the buffer a
  3698. line like
  3699. #+ARCHIVE: basement::** Finished Tasks
  3700. You may also define it locally for a subtree by setting an ARCHIVE property
  3701. in the entry. If such a property is found in an entry, or anywhere up
  3702. the hierarchy, it will be used."
  3703. :group 'org-archive
  3704. :type 'string)
  3705. (defcustom org-archive-tag "ARCHIVE"
  3706. "The tag that marks a subtree as archived.
  3707. An archived subtree does not open during visibility cycling, and does
  3708. not contribute to the agenda listings.
  3709. After changing this, font-lock must be restarted in the relevant buffers to
  3710. get the proper fontification."
  3711. :group 'org-archive
  3712. :group 'org-keywords
  3713. :type 'string)
  3714. (defcustom org-agenda-skip-archived-trees t
  3715. "Non-nil means the agenda will skip any items located in archived trees.
  3716. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3717. variable is no longer recommended, you should leave it at the value t.
  3718. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3719. :group 'org-archive
  3720. :group 'org-agenda-skip
  3721. :type 'boolean)
  3722. (defcustom org-columns-skip-archived-trees t
  3723. "Non-nil means ignore archived trees when creating column view."
  3724. :group 'org-archive
  3725. :group 'org-properties
  3726. :type 'boolean)
  3727. (defcustom org-cycle-open-archived-trees nil
  3728. "Non-nil means `org-cycle' will open archived trees.
  3729. An archived tree is a tree marked with the tag ARCHIVE.
  3730. When nil, archived trees will stay folded. You can still open them with
  3731. normal outline commands like `show-all', but not with the cycling commands."
  3732. :group 'org-archive
  3733. :group 'org-cycle
  3734. :type 'boolean)
  3735. (defcustom org-sparse-tree-open-archived-trees nil
  3736. "Non-nil means sparse tree construction shows matches in archived trees.
  3737. When nil, matches in these trees are highlighted, but the trees are kept in
  3738. collapsed state."
  3739. :group 'org-archive
  3740. :group 'org-sparse-trees
  3741. :type 'boolean)
  3742. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3743. "The default date type when building a sparse tree.
  3744. When this is nil, a date is a scheduled or a deadline timestamp.
  3745. Otherwise, these types are allowed:
  3746. all: all timestamps
  3747. active: only active timestamps (<...>)
  3748. inactive: only inactive timestamps (<...)
  3749. scheduled: only scheduled timestamps
  3750. deadline: only deadline timestamps"
  3751. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3752. (const :tag "All timestamps" all)
  3753. (const :tag "Only active timestamps" active)
  3754. (const :tag "Only inactive timestamps" inactive)
  3755. (const :tag "Only scheduled timestamps" scheduled)
  3756. (const :tag "Only deadline timestamps" deadline))
  3757. :version "24.3"
  3758. :group 'org-sparse-trees)
  3759. (defun org-cycle-hide-archived-subtrees (state)
  3760. "Re-hide all archived subtrees after a visibility state change."
  3761. (when (and (not org-cycle-open-archived-trees)
  3762. (not (memq state '(overview folded))))
  3763. (save-excursion
  3764. (let* ((globalp (memq state '(contents all)))
  3765. (beg (if globalp (point-min) (point)))
  3766. (end (if globalp (point-max) (org-end-of-subtree t))))
  3767. (org-hide-archived-subtrees beg end)
  3768. (goto-char beg)
  3769. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3770. (message "%s" (substitute-command-keys
  3771. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3772. (defun org-force-cycle-archived ()
  3773. "Cycle subtree even if it is archived."
  3774. (interactive)
  3775. (setq this-command 'org-cycle)
  3776. (let ((org-cycle-open-archived-trees t))
  3777. (call-interactively 'org-cycle)))
  3778. (defun org-hide-archived-subtrees (beg end)
  3779. "Re-hide all archived subtrees after a visibility state change."
  3780. (save-excursion
  3781. (let* ((re (concat ":" org-archive-tag ":")))
  3782. (goto-char beg)
  3783. (while (re-search-forward re end t)
  3784. (when (org-at-heading-p)
  3785. (org-flag-subtree t)
  3786. (org-end-of-subtree t))))))
  3787. (declare-function outline-end-of-heading "outline" ())
  3788. (declare-function outline-flag-region "outline" (from to flag))
  3789. (defun org-flag-subtree (flag)
  3790. (save-excursion
  3791. (org-back-to-heading t)
  3792. (outline-end-of-heading)
  3793. (outline-flag-region (point)
  3794. (progn (org-end-of-subtree t) (point))
  3795. flag)))
  3796. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3797. (eval-and-compile
  3798. (org-autoload "org-archive"
  3799. '(org-add-archive-files)))
  3800. ;; Autoload Column View Code
  3801. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3802. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3803. (declare-function org-columns-compute "org-colview" (property))
  3804. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3805. '(org-columns-number-to-string
  3806. org-columns-get-format-and-top-level
  3807. org-columns-compute
  3808. org-columns-remove-overlays))
  3809. ;; Autoload ID code
  3810. (declare-function org-id-store-link "org-id")
  3811. (declare-function org-id-locations-load "org-id")
  3812. (declare-function org-id-locations-save "org-id")
  3813. (defvar org-id-track-globally)
  3814. (org-autoload "org-id"
  3815. '(org-id-new
  3816. org-id-copy
  3817. org-id-get-with-outline-path-completion
  3818. org-id-get-with-outline-drilling))
  3819. ;;; Variables for pre-computed regular expressions, all buffer local
  3820. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3821. "Matches first line of a hidden block.")
  3822. (make-variable-buffer-local 'org-drawer-regexp)
  3823. (defvar org-todo-regexp nil
  3824. "Matches any of the TODO state keywords.")
  3825. (make-variable-buffer-local 'org-todo-regexp)
  3826. (defvar org-not-done-regexp nil
  3827. "Matches any of the TODO state keywords except the last one.")
  3828. (make-variable-buffer-local 'org-not-done-regexp)
  3829. (defvar org-not-done-heading-regexp nil
  3830. "Matches a TODO headline that is not done.")
  3831. (make-variable-buffer-local 'org-not-done-regexp)
  3832. (defvar org-todo-line-regexp nil
  3833. "Matches a headline and puts TODO state into group 2 if present.")
  3834. (make-variable-buffer-local 'org-todo-line-regexp)
  3835. (defvar org-complex-heading-regexp nil
  3836. "Matches a headline and puts everything into groups:
  3837. group 1: the stars
  3838. group 2: The todo keyword, maybe
  3839. group 3: Priority cookie
  3840. group 4: True headline
  3841. group 5: Tags")
  3842. (make-variable-buffer-local 'org-complex-heading-regexp)
  3843. (defvar org-complex-heading-regexp-format nil
  3844. "Printf format to make regexp to match an exact headline.
  3845. This regexp will match the headline of any node which has the
  3846. exact headline text that is put into the format, but may have any
  3847. TODO state, priority and tags.")
  3848. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3849. (defvar org-todo-line-tags-regexp nil
  3850. "Matches a headline and puts TODO state into group 2 if present.
  3851. Also put tags into group 4 if tags are present.")
  3852. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3853. (defvar org-ds-keyword-length 12
  3854. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3855. (make-variable-buffer-local 'org-ds-keyword-length)
  3856. (defvar org-deadline-regexp nil
  3857. "Matches the DEADLINE keyword.")
  3858. (make-variable-buffer-local 'org-deadline-regexp)
  3859. (defvar org-deadline-time-regexp nil
  3860. "Matches the DEADLINE keyword together with a time stamp.")
  3861. (make-variable-buffer-local 'org-deadline-time-regexp)
  3862. (defvar org-deadline-line-regexp nil
  3863. "Matches the DEADLINE keyword and the rest of the line.")
  3864. (make-variable-buffer-local 'org-deadline-line-regexp)
  3865. (defvar org-scheduled-regexp nil
  3866. "Matches the SCHEDULED keyword.")
  3867. (make-variable-buffer-local 'org-scheduled-regexp)
  3868. (defvar org-scheduled-time-regexp nil
  3869. "Matches the SCHEDULED keyword together with a time stamp.")
  3870. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3871. (defvar org-closed-time-regexp nil
  3872. "Matches the CLOSED keyword together with a time stamp.")
  3873. (make-variable-buffer-local 'org-closed-time-regexp)
  3874. (defvar org-keyword-time-regexp nil
  3875. "Matches any of the 4 keywords, together with the time stamp.")
  3876. (make-variable-buffer-local 'org-keyword-time-regexp)
  3877. (defvar org-keyword-time-not-clock-regexp nil
  3878. "Matches any of the 3 keywords, together with the time stamp.")
  3879. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3880. (defvar org-maybe-keyword-time-regexp nil
  3881. "Matches a timestamp, possibly preceded by a keyword.")
  3882. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3883. (defvar org-all-time-keywords nil
  3884. "List of time keywords.")
  3885. (make-variable-buffer-local 'org-all-time-keywords)
  3886. (defconst org-plain-time-of-day-regexp
  3887. (concat
  3888. "\\(\\<[012]?[0-9]"
  3889. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3890. "\\(--?"
  3891. "\\(\\<[012]?[0-9]"
  3892. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3893. "\\)?")
  3894. "Regular expression to match a plain time or time range.
  3895. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3896. groups carry important information:
  3897. 0 the full match
  3898. 1 the first time, range or not
  3899. 8 the second time, if it is a range.")
  3900. (defconst org-plain-time-extension-regexp
  3901. (concat
  3902. "\\(\\<[012]?[0-9]"
  3903. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3904. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3905. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3906. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3907. groups carry important information:
  3908. 0 the full match
  3909. 7 hours of duration
  3910. 9 minutes of duration")
  3911. (defconst org-stamp-time-of-day-regexp
  3912. (concat
  3913. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3914. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3915. "\\(--?"
  3916. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3917. "Regular expression to match a timestamp time or time range.
  3918. After a match, the following groups carry important information:
  3919. 0 the full match
  3920. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3921. 2 the first time, range or not
  3922. 4 the second time, if it is a range.")
  3923. (defconst org-startup-options
  3924. '(("fold" org-startup-folded t)
  3925. ("overview" org-startup-folded t)
  3926. ("nofold" org-startup-folded nil)
  3927. ("showall" org-startup-folded nil)
  3928. ("showeverything" org-startup-folded showeverything)
  3929. ("content" org-startup-folded content)
  3930. ("indent" org-startup-indented t)
  3931. ("noindent" org-startup-indented nil)
  3932. ("hidestars" org-hide-leading-stars t)
  3933. ("showstars" org-hide-leading-stars nil)
  3934. ("odd" org-odd-levels-only t)
  3935. ("oddeven" org-odd-levels-only nil)
  3936. ("align" org-startup-align-all-tables t)
  3937. ("noalign" org-startup-align-all-tables nil)
  3938. ("inlineimages" org-startup-with-inline-images t)
  3939. ("noinlineimages" org-startup-with-inline-images nil)
  3940. ("customtime" org-display-custom-times t)
  3941. ("logdone" org-log-done time)
  3942. ("lognotedone" org-log-done note)
  3943. ("nologdone" org-log-done nil)
  3944. ("lognoteclock-out" org-log-note-clock-out t)
  3945. ("nolognoteclock-out" org-log-note-clock-out nil)
  3946. ("logrepeat" org-log-repeat state)
  3947. ("lognoterepeat" org-log-repeat note)
  3948. ("logdrawer" org-log-into-drawer t)
  3949. ("nologdrawer" org-log-into-drawer nil)
  3950. ("logstatesreversed" org-log-states-order-reversed t)
  3951. ("nologstatesreversed" org-log-states-order-reversed nil)
  3952. ("nologrepeat" org-log-repeat nil)
  3953. ("logreschedule" org-log-reschedule time)
  3954. ("lognotereschedule" org-log-reschedule note)
  3955. ("nologreschedule" org-log-reschedule nil)
  3956. ("logredeadline" org-log-redeadline time)
  3957. ("lognoteredeadline" org-log-redeadline note)
  3958. ("nologredeadline" org-log-redeadline nil)
  3959. ("logrefile" org-log-refile time)
  3960. ("lognoterefile" org-log-refile note)
  3961. ("nologrefile" org-log-refile nil)
  3962. ("fninline" org-footnote-define-inline t)
  3963. ("nofninline" org-footnote-define-inline nil)
  3964. ("fnlocal" org-footnote-section nil)
  3965. ("fnauto" org-footnote-auto-label t)
  3966. ("fnprompt" org-footnote-auto-label nil)
  3967. ("fnconfirm" org-footnote-auto-label confirm)
  3968. ("fnplain" org-footnote-auto-label plain)
  3969. ("fnadjust" org-footnote-auto-adjust t)
  3970. ("nofnadjust" org-footnote-auto-adjust nil)
  3971. ("constcgs" constants-unit-system cgs)
  3972. ("constSI" constants-unit-system SI)
  3973. ("noptag" org-tag-persistent-alist nil)
  3974. ("hideblocks" org-hide-block-startup t)
  3975. ("nohideblocks" org-hide-block-startup nil)
  3976. ("beamer" org-startup-with-beamer-mode t)
  3977. ("entitiespretty" org-pretty-entities t)
  3978. ("entitiesplain" org-pretty-entities nil))
  3979. "Variable associated with STARTUP options for org-mode.
  3980. Each element is a list of three items: the startup options (as written
  3981. in the #+STARTUP line), the corresponding variable, and the value to set
  3982. this variable to if the option is found. An optional forth element PUSH
  3983. means to push this value onto the list in the variable.")
  3984. (defun org-update-property-plist (key val props)
  3985. "Update PROPS with KEY and VAL."
  3986. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3987. (key (if appending (substring key 0 (- (length key) 1)) key))
  3988. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3989. (previous (cdr (assoc key props))))
  3990. (if appending
  3991. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3992. (cons (cons key val) remainder))))
  3993. (defconst org-block-regexp
  3994. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3995. "Regular expression for hiding blocks.")
  3996. (defconst org-heading-keyword-regexp-format
  3997. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3998. "Printf format for a regexp matching an headline with some keyword.
  3999. This regexp will match the headline of any node which has the
  4000. exact keyword that is put into the format. The keyword isn't in
  4001. any group by default, but the stars and the body are.")
  4002. (defconst org-heading-keyword-maybe-regexp-format
  4003. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4004. "Printf format for a regexp matching an headline, possibly with some keyword.
  4005. This regexp can match any headline with the specified keyword, or
  4006. without a keyword. The keyword isn't in any group by default,
  4007. but the stars and the body are.")
  4008. (defun org-set-regexps-and-options ()
  4009. "Precompute regular expressions for current buffer."
  4010. (when (derived-mode-p 'org-mode)
  4011. (org-set-local 'org-todo-kwd-alist nil)
  4012. (org-set-local 'org-todo-key-alist nil)
  4013. (org-set-local 'org-todo-key-trigger nil)
  4014. (org-set-local 'org-todo-keywords-1 nil)
  4015. (org-set-local 'org-done-keywords nil)
  4016. (org-set-local 'org-todo-heads nil)
  4017. (org-set-local 'org-todo-sets nil)
  4018. (org-set-local 'org-todo-log-states nil)
  4019. (org-set-local 'org-file-properties nil)
  4020. (org-set-local 'org-file-tags nil)
  4021. (let ((re (org-make-options-regexp
  4022. '("CATEGORY" "TODO" "COLUMNS"
  4023. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  4024. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  4025. "OPTIONS")
  4026. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4027. (splitre "[ \t]+")
  4028. (scripts org-use-sub-superscripts)
  4029. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4030. tail sep kws1 prio props ftags drawers beamer-p
  4031. ext-setup-or-nil setup-contents (start 0))
  4032. (save-excursion
  4033. (save-restriction
  4034. (widen)
  4035. (goto-char (point-min))
  4036. (while (or (and ext-setup-or-nil
  4037. (string-match re ext-setup-or-nil start)
  4038. (setq start (match-end 0)))
  4039. (and (setq ext-setup-or-nil nil start 0)
  4040. (re-search-forward re nil t)))
  4041. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4042. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4043. (if (stringp value) (setq value (org-trim value)))
  4044. (cond
  4045. ((equal key "CATEGORY")
  4046. (setq cat value))
  4047. ((member key '("SEQ_TODO" "TODO"))
  4048. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4049. ((equal key "TYP_TODO")
  4050. (push (cons 'type (org-split-string value splitre)) kwds))
  4051. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4052. ;; general TODO-like setup
  4053. (push (cons (intern (downcase (match-string 1 key)))
  4054. (org-split-string value splitre)) kwds))
  4055. ((equal key "TAGS")
  4056. (setq tags (append tags (if tags '("\\n") nil)
  4057. (org-split-string value splitre))))
  4058. ((equal key "COLUMNS")
  4059. (org-set-local 'org-columns-default-format value))
  4060. ((equal key "LINK")
  4061. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4062. (push (cons (match-string 1 value)
  4063. (org-trim (match-string 2 value)))
  4064. links)))
  4065. ((equal key "PRIORITIES")
  4066. (setq prio (org-split-string value " +")))
  4067. ((equal key "PROPERTY")
  4068. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4069. (setq props (org-update-property-plist (match-string 1 value)
  4070. (match-string 2 value)
  4071. props))))
  4072. ((equal key "FILETAGS")
  4073. (when (string-match "\\S-" value)
  4074. (setq ftags
  4075. (append
  4076. ftags
  4077. (apply 'append
  4078. (mapcar (lambda (x) (org-split-string x ":"))
  4079. (org-split-string value)))))))
  4080. ((equal key "DRAWERS")
  4081. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4082. ((equal key "CONSTANTS")
  4083. (setq const (append const (org-split-string value splitre))))
  4084. ((equal key "STARTUP")
  4085. (let ((opts (org-split-string value splitre))
  4086. l var val)
  4087. (while (setq l (pop opts))
  4088. (when (setq l (assoc l org-startup-options))
  4089. (setq var (nth 1 l) val (nth 2 l))
  4090. (if (not (nth 3 l))
  4091. (set (make-local-variable var) val)
  4092. (if (not (listp (symbol-value var)))
  4093. (set (make-local-variable var) nil))
  4094. (set (make-local-variable var) (symbol-value var))
  4095. (add-to-list var val))))))
  4096. ((equal key "ARCHIVE")
  4097. (setq arch value)
  4098. (remove-text-properties 0 (length arch)
  4099. '(face t fontified t) arch))
  4100. ((equal key "LATEX_CLASS")
  4101. (setq beamer-p (equal value "beamer")))
  4102. ((equal key "OPTIONS")
  4103. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4104. (setq scripts (read (match-string 2 value)))))
  4105. ((equal key "SETUPFILE")
  4106. (setq setup-contents (org-file-contents
  4107. (expand-file-name
  4108. (org-remove-double-quotes value))
  4109. 'noerror))
  4110. (if (not ext-setup-or-nil)
  4111. (setq ext-setup-or-nil setup-contents start 0)
  4112. (setq ext-setup-or-nil
  4113. (concat (substring ext-setup-or-nil 0 start)
  4114. "\n" setup-contents "\n"
  4115. (substring ext-setup-or-nil start)))))))
  4116. ;; search for property blocks
  4117. (goto-char (point-min))
  4118. (while (re-search-forward org-block-regexp nil t)
  4119. (when (equal "PROPERTY" (upcase (match-string 1)))
  4120. (setq value (replace-regexp-in-string
  4121. "[\n\r]" " " (match-string 4)))
  4122. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4123. (setq props (org-update-property-plist (match-string 1 value)
  4124. (match-string 2 value)
  4125. props)))))))
  4126. (org-set-local 'org-use-sub-superscripts scripts)
  4127. (when cat
  4128. (org-set-local 'org-category (intern cat))
  4129. (push (cons "CATEGORY" cat) props))
  4130. (when prio
  4131. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4132. (setq prio (mapcar 'string-to-char prio))
  4133. (org-set-local 'org-highest-priority (nth 0 prio))
  4134. (org-set-local 'org-lowest-priority (nth 1 prio))
  4135. (org-set-local 'org-default-priority (nth 2 prio)))
  4136. (and props (org-set-local 'org-file-properties (nreverse props)))
  4137. (and ftags (org-set-local 'org-file-tags
  4138. (mapcar 'org-add-prop-inherited ftags)))
  4139. (and drawers (org-set-local 'org-drawers drawers))
  4140. (and arch (org-set-local 'org-archive-location arch))
  4141. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4142. ;; Process the TODO keywords
  4143. (unless kwds
  4144. ;; Use the global values as if they had been given locally.
  4145. (setq kwds (default-value 'org-todo-keywords))
  4146. (if (stringp (car kwds))
  4147. (setq kwds (list (cons org-todo-interpretation
  4148. (default-value 'org-todo-keywords)))))
  4149. (setq kwds (reverse kwds)))
  4150. (setq kwds (nreverse kwds))
  4151. (let (inter kws kw)
  4152. (while (setq kws (pop kwds))
  4153. (let ((kws (or
  4154. (run-hook-with-args-until-success
  4155. 'org-todo-setup-filter-hook kws)
  4156. kws)))
  4157. (setq inter (pop kws) sep (member "|" kws)
  4158. kws0 (delete "|" (copy-sequence kws))
  4159. kwsa nil
  4160. kws1 (mapcar
  4161. (lambda (x)
  4162. ;; 1 2
  4163. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4164. (progn
  4165. (setq kw (match-string 1 x)
  4166. key (and (match-end 2) (match-string 2 x))
  4167. log (org-extract-log-state-settings x))
  4168. (push (cons kw (and key (string-to-char key))) kwsa)
  4169. (and log (push log org-todo-log-states))
  4170. kw)
  4171. (error "Invalid TODO keyword %s" x)))
  4172. kws0)
  4173. kwsa (if kwsa (append '((:startgroup))
  4174. (nreverse kwsa)
  4175. '((:endgroup))))
  4176. hw (car kws1)
  4177. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4178. tail (list inter hw (car dws) (org-last dws))))
  4179. (add-to-list 'org-todo-heads hw 'append)
  4180. (push kws1 org-todo-sets)
  4181. (setq org-done-keywords (append org-done-keywords dws nil))
  4182. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4183. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4184. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4185. (setq org-todo-sets (nreverse org-todo-sets)
  4186. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4187. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4188. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4189. ;; Process the constants
  4190. (when const
  4191. (let (e cst)
  4192. (while (setq e (pop const))
  4193. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4194. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4195. (setq org-table-formula-constants-local cst)))
  4196. ;; Process the tags.
  4197. (when tags
  4198. (let (e tgs)
  4199. (while (setq e (pop tags))
  4200. (cond
  4201. ((equal e "{") (push '(:startgroup) tgs))
  4202. ((equal e "}") (push '(:endgroup) tgs))
  4203. ((equal e "\\n") (push '(:newline) tgs))
  4204. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4205. (push (cons (match-string 1 e)
  4206. (string-to-char (match-string 2 e)))
  4207. tgs))
  4208. (t (push (list e) tgs))))
  4209. (org-set-local 'org-tag-alist nil)
  4210. (while (setq e (pop tgs))
  4211. (or (and (stringp (car e))
  4212. (assoc (car e) org-tag-alist))
  4213. (push e org-tag-alist)))))
  4214. ;; Compute the regular expressions and other local variables.
  4215. ;; Using `org-outline-regexp-bol' would complicate them much,
  4216. ;; because of the fixed white space at the end of that string.
  4217. (if (not org-done-keywords)
  4218. (setq org-done-keywords (and org-todo-keywords-1
  4219. (list (org-last org-todo-keywords-1)))))
  4220. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4221. (length org-scheduled-string)
  4222. (length org-clock-string)
  4223. (length org-closed-string)))
  4224. org-drawer-regexp
  4225. (concat "^[ \t]*:\\("
  4226. (mapconcat 'regexp-quote org-drawers "\\|")
  4227. "\\):[ \t]*$")
  4228. org-not-done-keywords
  4229. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4230. org-todo-regexp
  4231. (concat "\\("
  4232. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4233. "\\)")
  4234. org-not-done-regexp
  4235. (concat "\\("
  4236. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4237. "\\)")
  4238. org-not-done-heading-regexp
  4239. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4240. org-todo-line-regexp
  4241. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4242. org-complex-heading-regexp
  4243. (concat "^\\(\\*+\\)"
  4244. "\\(?: +" org-todo-regexp "\\)?"
  4245. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4246. "\\(?: +\\(.*?\\)\\)??"
  4247. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4248. "[ \t]*$")
  4249. org-complex-heading-regexp-format
  4250. (concat "^\\(\\*+\\)"
  4251. "\\(?: +" org-todo-regexp "\\)?"
  4252. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4253. "\\(?: +"
  4254. ;; Stats cookies can be stuck to body.
  4255. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4256. "\\(%s\\)"
  4257. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4258. "\\)"
  4259. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4260. "[ \t]*$")
  4261. org-todo-line-tags-regexp
  4262. (concat "^\\(\\*+\\)"
  4263. "\\(?: +" org-todo-regexp "\\)?"
  4264. "\\(?: +\\(.*?\\)\\)??"
  4265. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4266. "[ \t]*$")
  4267. org-deadline-regexp (concat "\\<" org-deadline-string)
  4268. org-deadline-time-regexp
  4269. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4270. org-deadline-line-regexp
  4271. (concat "\\<\\(" org-deadline-string "\\).*")
  4272. org-scheduled-regexp
  4273. (concat "\\<" org-scheduled-string)
  4274. org-scheduled-time-regexp
  4275. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4276. org-closed-time-regexp
  4277. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4278. org-keyword-time-regexp
  4279. (concat "\\<\\(" org-scheduled-string
  4280. "\\|" org-deadline-string
  4281. "\\|" org-closed-string
  4282. "\\|" org-clock-string "\\)"
  4283. " *[[<]\\([^]>]+\\)[]>]")
  4284. org-keyword-time-not-clock-regexp
  4285. (concat "\\<\\(" org-scheduled-string
  4286. "\\|" org-deadline-string
  4287. "\\|" org-closed-string
  4288. "\\)"
  4289. " *[[<]\\([^]>]+\\)[]>]")
  4290. org-maybe-keyword-time-regexp
  4291. (concat "\\(\\<\\(" org-scheduled-string
  4292. "\\|" org-deadline-string
  4293. "\\|" org-closed-string
  4294. "\\|" org-clock-string "\\)\\)?"
  4295. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4296. org-all-time-keywords
  4297. (mapcar (lambda (w) (substring w 0 -1))
  4298. (list org-scheduled-string org-deadline-string
  4299. org-clock-string org-closed-string))
  4300. )
  4301. (org-compute-latex-and-specials-regexp)
  4302. (org-set-font-lock-defaults))))
  4303. (defun org-file-contents (file &optional noerror)
  4304. "Return the contents of FILE, as a string."
  4305. (if (or (not file)
  4306. (not (file-readable-p file)))
  4307. (if noerror
  4308. (progn
  4309. (message "Cannot read file \"%s\"" file)
  4310. (ding) (sit-for 2)
  4311. "")
  4312. (error "Cannot read file \"%s\"" file))
  4313. (with-temp-buffer
  4314. (insert-file-contents file)
  4315. (buffer-string))))
  4316. (defun org-extract-log-state-settings (x)
  4317. "Extract the log state setting from a TODO keyword string.
  4318. This will extract info from a string like \"WAIT(w@/!)\"."
  4319. (let (kw key log1 log2)
  4320. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4321. (setq kw (match-string 1 x)
  4322. key (and (match-end 2) (match-string 2 x))
  4323. log1 (and (match-end 3) (match-string 3 x))
  4324. log2 (and (match-end 4) (match-string 4 x)))
  4325. (and (or log1 log2)
  4326. (list kw
  4327. (and log1 (if (equal log1 "!") 'time 'note))
  4328. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4329. (defun org-remove-keyword-keys (list)
  4330. "Remove a pair of parenthesis at the end of each string in LIST."
  4331. (mapcar (lambda (x)
  4332. (if (string-match "(.*)$" x)
  4333. (substring x 0 (match-beginning 0))
  4334. x))
  4335. list))
  4336. (defun org-assign-fast-keys (alist)
  4337. "Assign fast keys to a keyword-key alist.
  4338. Respect keys that are already there."
  4339. (let (new e (alt ?0))
  4340. (while (setq e (pop alist))
  4341. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4342. (cdr e)) ;; Key already assigned.
  4343. (push e new)
  4344. (let ((clist (string-to-list (downcase (car e))))
  4345. (used (append new alist)))
  4346. (when (= (car clist) ?@)
  4347. (pop clist))
  4348. (while (and clist (rassoc (car clist) used))
  4349. (pop clist))
  4350. (unless clist
  4351. (while (rassoc alt used)
  4352. (incf alt)))
  4353. (push (cons (car e) (or (car clist) alt)) new))))
  4354. (nreverse new)))
  4355. ;;; Some variables used in various places
  4356. (defvar org-window-configuration nil
  4357. "Used in various places to store a window configuration.")
  4358. (defvar org-selected-window nil
  4359. "Used in various places to store a window configuration.")
  4360. (defvar org-finish-function nil
  4361. "Function to be called when `C-c C-c' is used.
  4362. This is for getting out of special buffers like capture.")
  4363. ;; FIXME: Occasionally check by commenting these, to make sure
  4364. ;; no other functions uses these, forgetting to let-bind them.
  4365. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4366. (defvar org-last-state)
  4367. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4368. ;; Defined somewhere in this file, but used before definition.
  4369. (defvar org-entities) ;; defined in org-entities.el
  4370. (defvar org-struct-menu)
  4371. (defvar org-org-menu)
  4372. (defvar org-tbl-menu)
  4373. ;;;; Define the Org-mode
  4374. ;; We use a before-change function to check if a table might need
  4375. ;; an update.
  4376. (defvar org-table-may-need-update t
  4377. "Indicates that a table might need an update.
  4378. This variable is set by `org-before-change-function'.
  4379. `org-table-align' sets it back to nil.")
  4380. (defun org-before-change-function (beg end)
  4381. "Every change indicates that a table might need an update."
  4382. (setq org-table-may-need-update t))
  4383. (defvar org-mode-map)
  4384. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4385. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4386. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4387. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4388. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4389. (defvar org-table-buffer-is-an nil)
  4390. (defvar bidi-paragraph-direction)
  4391. (defvar buffer-face-mode-face)
  4392. (require 'outline)
  4393. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4394. (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"))
  4395. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4396. ;; Other stuff we need.
  4397. (require 'time-date)
  4398. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4399. (require 'easymenu)
  4400. (require 'overlay)
  4401. ;; (require 'org-macs) moved higher up in the file before it is first used
  4402. (require 'org-entities)
  4403. ;; (require 'org-compat) moved higher up in the file before it is first used
  4404. (require 'org-faces)
  4405. (require 'org-list)
  4406. (require 'org-pcomplete)
  4407. (require 'org-src)
  4408. (require 'org-footnote)
  4409. ;; babel
  4410. (require 'ob)
  4411. ;;;###autoload
  4412. (define-derived-mode org-mode outline-mode "Org"
  4413. "Outline-based notes management and organizer, alias
  4414. \"Carsten's outline-mode for keeping track of everything.\"
  4415. Org-mode develops organizational tasks around a NOTES file which
  4416. contains information about projects as plain text. Org-mode is
  4417. implemented on top of outline-mode, which is ideal to keep the content
  4418. of large files well structured. It supports ToDo items, deadlines and
  4419. time stamps, which magically appear in the diary listing of the Emacs
  4420. calendar. Tables are easily created with a built-in table editor.
  4421. Plain text URL-like links connect to websites, emails (VM), Usenet
  4422. messages (Gnus), BBDB entries, and any files related to the project.
  4423. For printing and sharing of notes, an Org-mode file (or a part of it)
  4424. can be exported as a structured ASCII or HTML file.
  4425. The following commands are available:
  4426. \\{org-mode-map}"
  4427. ;; Get rid of Outline menus, they are not needed
  4428. ;; Need to do this here because define-derived-mode sets up
  4429. ;; the keymap so late. Still, it is a waste to call this each time
  4430. ;; we switch another buffer into org-mode.
  4431. (if (featurep 'xemacs)
  4432. (when (boundp 'outline-mode-menu-heading)
  4433. ;; Assume this is Greg's port, it uses easymenu
  4434. (easy-menu-remove outline-mode-menu-heading)
  4435. (easy-menu-remove outline-mode-menu-show)
  4436. (easy-menu-remove outline-mode-menu-hide))
  4437. (define-key org-mode-map [menu-bar headings] 'undefined)
  4438. (define-key org-mode-map [menu-bar hide] 'undefined)
  4439. (define-key org-mode-map [menu-bar show] 'undefined))
  4440. (org-load-modules-maybe)
  4441. (easy-menu-add org-org-menu)
  4442. (easy-menu-add org-tbl-menu)
  4443. (org-install-agenda-files-menu)
  4444. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4445. (add-to-invisibility-spec '(org-cwidth))
  4446. (add-to-invisibility-spec '(org-hide-block . t))
  4447. (when (featurep 'xemacs)
  4448. (org-set-local 'line-move-ignore-invisible t))
  4449. (org-set-local 'outline-regexp org-outline-regexp)
  4450. (org-set-local 'outline-level 'org-outline-level)
  4451. (setq bidi-paragraph-direction 'left-to-right)
  4452. (when (and org-ellipsis
  4453. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4454. (fboundp 'make-glyph-code))
  4455. (unless org-display-table
  4456. (setq org-display-table (make-display-table)))
  4457. (set-display-table-slot
  4458. org-display-table 4
  4459. (vconcat (mapcar
  4460. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4461. org-ellipsis)))
  4462. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4463. (setq buffer-display-table org-display-table))
  4464. (org-set-regexps-and-options)
  4465. (when (and org-tag-faces (not org-tags-special-faces-re))
  4466. ;; tag faces set outside customize.... force initialization.
  4467. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4468. ;; Calc embedded
  4469. (org-set-local 'calc-embedded-open-mode "# ")
  4470. (modify-syntax-entry ?@ "w")
  4471. (modify-syntax-entry ?\" "\"")
  4472. (if org-startup-truncated (setq truncate-lines t))
  4473. (org-set-local 'font-lock-unfontify-region-function
  4474. 'org-unfontify-region)
  4475. ;; Activate before-change-function
  4476. (org-set-local 'org-table-may-need-update t)
  4477. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4478. 'local)
  4479. ;; Check for running clock before killing a buffer
  4480. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4481. ;; Initialize macros templates.
  4482. (org-macro-initialize-templates)
  4483. ;; Initialize radio targets.
  4484. (org-update-radio-target-regexp)
  4485. ;; Indentation.
  4486. (org-set-local 'indent-line-function 'org-indent-line)
  4487. (org-set-local 'indent-region-function 'org-indent-region)
  4488. ;; Filling and auto-filling.
  4489. (org-setup-filling)
  4490. ;; Comments.
  4491. (org-setup-comments-handling)
  4492. ;; Beginning/end of defun
  4493. (org-set-local 'beginning-of-defun-function 'org-back-to-heading)
  4494. (org-set-local 'end-of-defun-function (lambda () (interactive) (org-end-of-subtree nil t)))
  4495. ;; Next error for sparse trees
  4496. (org-set-local 'next-error-function 'org-occur-next-match)
  4497. ;; Make sure dependence stuff works reliably, even for users who set it
  4498. ;; too late :-(
  4499. (if org-enforce-todo-dependencies
  4500. (add-hook 'org-blocker-hook
  4501. 'org-block-todo-from-children-or-siblings-or-parent)
  4502. (remove-hook 'org-blocker-hook
  4503. 'org-block-todo-from-children-or-siblings-or-parent))
  4504. (if org-enforce-todo-checkbox-dependencies
  4505. (add-hook 'org-blocker-hook
  4506. 'org-block-todo-from-checkboxes)
  4507. (remove-hook 'org-blocker-hook
  4508. 'org-block-todo-from-checkboxes))
  4509. ;; Align options lines
  4510. (org-set-local
  4511. 'align-mode-rules-list
  4512. '((org-in-buffer-settings
  4513. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4514. (modes . '(org-mode)))))
  4515. ;; Imenu
  4516. (org-set-local 'imenu-create-index-function
  4517. 'org-imenu-get-tree)
  4518. ;; Make isearch reveal context
  4519. (if (or (featurep 'xemacs)
  4520. (not (boundp 'outline-isearch-open-invisible-function)))
  4521. ;; Emacs 21 and XEmacs make use of the hook
  4522. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4523. ;; Emacs 22 deals with this through a special variable
  4524. (org-set-local 'outline-isearch-open-invisible-function
  4525. (lambda (&rest ignore) (org-show-context 'isearch))))
  4526. ;; Turn on org-beamer-mode?
  4527. (and org-startup-with-beamer-mode (org-beamer-mode))
  4528. ;; Setup the pcomplete hooks
  4529. (set (make-local-variable 'pcomplete-command-completion-function)
  4530. 'org-pcomplete-initial)
  4531. (set (make-local-variable 'pcomplete-command-name-function)
  4532. 'org-command-at-point)
  4533. (set (make-local-variable 'pcomplete-default-completion-function)
  4534. 'ignore)
  4535. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4536. 'org-parse-arguments)
  4537. (set (make-local-variable 'pcomplete-termination-string) "")
  4538. (when (>= emacs-major-version 23)
  4539. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4540. ;; If empty file that did not turn on org-mode automatically, make it to.
  4541. (if (and org-insert-mode-line-in-empty-file
  4542. (org-called-interactively-p 'any)
  4543. (= (point-min) (point-max)))
  4544. (insert "# -*- mode: org -*-\n\n"))
  4545. (unless org-inhibit-startup
  4546. (when org-startup-align-all-tables
  4547. (let ((bmp (buffer-modified-p)))
  4548. (org-table-map-tables 'org-table-align 'quietly)
  4549. (set-buffer-modified-p bmp)))
  4550. (when org-startup-with-inline-images
  4551. (org-display-inline-images))
  4552. (when org-startup-indented
  4553. (require 'org-indent)
  4554. (org-indent-mode 1))
  4555. (unless org-inhibit-startup-visibility-stuff
  4556. (org-set-startup-visibility)))
  4557. ;; Try to set org-hide correctly
  4558. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4559. (when (fboundp 'abbrev-table-put)
  4560. (abbrev-table-put org-mode-abbrev-table
  4561. :parents (list text-mode-abbrev-table)))
  4562. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4563. (defun org-find-invisible-foreground ()
  4564. (let ((candidates (remove
  4565. "unspecified-bg"
  4566. (nconc
  4567. (list (face-background 'default)
  4568. (face-background 'org-default))
  4569. (mapcar
  4570. (lambda (alist)
  4571. (when (boundp alist)
  4572. (cdr (assoc 'background-color (symbol-value alist)))))
  4573. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4574. (list (face-foreground 'org-hide))))))
  4575. (car (remove nil candidates))))
  4576. (defun org-current-time (&optional rounding-minutes)
  4577. "Current time, possibly rounded to ROUNDING-MINUTES.
  4578. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4579. `org-time-stamp-rounding-minutes'."
  4580. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4581. (car org-time-stamp-rounding-minutes)))
  4582. (time (decode-time)))
  4583. (if (> r 1)
  4584. (apply 'encode-time
  4585. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4586. (nthcdr 2 time)))
  4587. (current-time))))
  4588. (defun org-today ()
  4589. "Return today date, considering `org-extend-today-until'."
  4590. (time-to-days
  4591. (time-subtract (current-time)
  4592. (list 0 (* 3600 org-extend-today-until) 0))))
  4593. ;;;; Font-Lock stuff, including the activators
  4594. (defvar org-mouse-map (make-sparse-keymap))
  4595. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4596. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4597. (when org-mouse-1-follows-link
  4598. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4599. (when org-tab-follows-link
  4600. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4601. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4602. (require 'font-lock)
  4603. (defconst org-non-link-chars "]\t\n\r<>")
  4604. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4605. "shell" "elisp" "doi" "message"))
  4606. (defvar org-link-types-re nil
  4607. "Matches a link that has a url-like prefix like \"http:\"")
  4608. (defvar org-link-re-with-space nil
  4609. "Matches a link with spaces, optional angular brackets around it.")
  4610. (defvar org-link-re-with-space2 nil
  4611. "Matches a link with spaces, optional angular brackets around it.")
  4612. (defvar org-link-re-with-space3 nil
  4613. "Matches a link with spaces, only for internal part in bracket links.")
  4614. (defvar org-angle-link-re nil
  4615. "Matches link with angular brackets, spaces are allowed.")
  4616. (defvar org-plain-link-re nil
  4617. "Matches plain link, without spaces.")
  4618. (defvar org-bracket-link-regexp nil
  4619. "Matches a link in double brackets.")
  4620. (defvar org-bracket-link-analytic-regexp nil
  4621. "Regular expression used to analyze links.
  4622. Here is what the match groups contain after a match:
  4623. 1: http:
  4624. 2: http
  4625. 3: path
  4626. 4: [desc]
  4627. 5: desc")
  4628. (defvar org-bracket-link-analytic-regexp++ nil
  4629. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4630. (defvar org-any-link-re nil
  4631. "Regular expression matching any link.")
  4632. (defcustom org-match-sexp-depth 3
  4633. "Number of stacked braces for sub/superscript matching.
  4634. This has to be set before loading org.el to be effective."
  4635. :group 'org-export-translation ; ??????????????????????????/
  4636. :type 'integer)
  4637. (defun org-create-multibrace-regexp (left right n)
  4638. "Create a regular expression which will match a balanced sexp.
  4639. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4640. as single character strings.
  4641. The regexp returned will match the entire expression including the
  4642. delimiters. It will also define a single group which contains the
  4643. match except for the outermost delimiters. The maximum depth of
  4644. stacked delimiters is N. Escaping delimiters is not possible."
  4645. (let* ((nothing (concat "[^" left right "]*?"))
  4646. (or "\\|")
  4647. (re nothing)
  4648. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4649. (while (> n 1)
  4650. (setq n (1- n)
  4651. re (concat re or next)
  4652. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4653. (concat left "\\(" re "\\)" right)))
  4654. (defvar org-match-substring-regexp
  4655. (concat
  4656. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4657. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4658. "\\|"
  4659. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4660. "\\|"
  4661. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4662. "The regular expression matching a sub- or superscript.")
  4663. (defvar org-match-substring-with-braces-regexp
  4664. (concat
  4665. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4666. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4667. "\\)")
  4668. "The regular expression matching a sub- or superscript, forcing braces.")
  4669. (defun org-make-link-regexps ()
  4670. "Update the link regular expressions.
  4671. This should be called after the variable `org-link-types' has changed."
  4672. (setq org-link-types-re
  4673. (concat
  4674. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4675. org-link-re-with-space
  4676. (concat
  4677. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4678. "\\([^" org-non-link-chars " ]"
  4679. "[^" org-non-link-chars "]*"
  4680. "[^" org-non-link-chars " ]\\)>?")
  4681. org-link-re-with-space2
  4682. (concat
  4683. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4684. "\\([^" org-non-link-chars " ]"
  4685. "[^\t\n\r]*"
  4686. "[^" org-non-link-chars " ]\\)>?")
  4687. org-link-re-with-space3
  4688. (concat
  4689. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4690. "\\([^" org-non-link-chars " ]"
  4691. "[^\t\n\r]*\\)")
  4692. org-angle-link-re
  4693. (concat
  4694. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4695. "\\([^" org-non-link-chars " ]"
  4696. "[^" org-non-link-chars "]*"
  4697. "\\)>")
  4698. org-plain-link-re
  4699. (concat
  4700. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4701. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4702. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4703. org-bracket-link-regexp
  4704. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4705. org-bracket-link-analytic-regexp
  4706. (concat
  4707. "\\[\\["
  4708. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4709. "\\([^]]+\\)"
  4710. "\\]"
  4711. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4712. "\\]")
  4713. org-bracket-link-analytic-regexp++
  4714. (concat
  4715. "\\[\\["
  4716. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4717. "\\([^]]+\\)"
  4718. "\\]"
  4719. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4720. "\\]")
  4721. org-any-link-re
  4722. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4723. org-angle-link-re "\\)\\|\\("
  4724. org-plain-link-re "\\)")))
  4725. (org-make-link-regexps)
  4726. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4727. "Regular expression for fast time stamp matching.")
  4728. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4729. "Regular expression for fast time stamp matching.")
  4730. (defconst org-ts-regexp0
  4731. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4732. "Regular expression matching time strings for analysis.
  4733. This one does not require the space after the date, so it can be used
  4734. on a string that terminates immediately after the date.")
  4735. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4736. "Regular expression matching time strings for analysis.")
  4737. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4738. "Regular expression matching time stamps, with groups.")
  4739. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4740. "Regular expression matching time stamps (also [..]), with groups.")
  4741. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4742. "Regular expression matching a time stamp range.")
  4743. (defconst org-tr-regexp-both
  4744. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4745. "Regular expression matching a time stamp range.")
  4746. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4747. org-ts-regexp "\\)?")
  4748. "Regular expression matching a time stamp or time stamp range.")
  4749. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4750. org-ts-regexp-both "\\)?")
  4751. "Regular expression matching a time stamp or time stamp range.
  4752. The time stamps may be either active or inactive.")
  4753. (defvar org-emph-face nil)
  4754. (defun org-do-emphasis-faces (limit)
  4755. "Run through the buffer and add overlays to emphasized strings."
  4756. (let (rtn a)
  4757. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4758. (if (not (= (char-after (match-beginning 3))
  4759. (char-after (match-beginning 4))))
  4760. (progn
  4761. (setq rtn t)
  4762. (setq a (assoc (match-string 3) org-emphasis-alist))
  4763. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4764. 'face
  4765. (nth 1 a))
  4766. (and (nth 4 a)
  4767. (org-remove-flyspell-overlays-in
  4768. (match-beginning 0) (match-end 0)))
  4769. (add-text-properties (match-beginning 2) (match-end 2)
  4770. '(font-lock-multiline t org-emphasis t))
  4771. (when org-hide-emphasis-markers
  4772. (add-text-properties (match-end 4) (match-beginning 5)
  4773. '(invisible org-link))
  4774. (add-text-properties (match-beginning 3) (match-end 3)
  4775. '(invisible org-link)))))
  4776. (backward-char 1))
  4777. rtn))
  4778. (defun org-emphasize (&optional char)
  4779. "Insert or change an emphasis, i.e. a font like bold or italic.
  4780. If there is an active region, change that region to a new emphasis.
  4781. If there is no region, just insert the marker characters and position
  4782. the cursor between them.
  4783. CHAR should be either the marker character, or the first character of the
  4784. HTML tag associated with that emphasis. If CHAR is a space, the means
  4785. to remove the emphasis of the selected region.
  4786. If char is not given (for example in an interactive call) it
  4787. will be prompted for."
  4788. (interactive)
  4789. (let ((eal org-emphasis-alist) e det
  4790. (erc org-emphasis-regexp-components)
  4791. (prompt "")
  4792. (string "") beg end move tag c s)
  4793. (if (org-region-active-p)
  4794. (setq beg (region-beginning) end (region-end)
  4795. string (buffer-substring beg end))
  4796. (setq move t))
  4797. (while (setq e (pop eal))
  4798. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4799. c (aref tag 0))
  4800. (push (cons c (string-to-char (car e))) det)
  4801. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4802. (substring tag 1)))))
  4803. (setq det (nreverse det))
  4804. (unless char
  4805. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4806. (setq char (read-char-exclusive)))
  4807. (setq char (or (cdr (assoc char det)) char))
  4808. (if (equal char ?\ )
  4809. (setq s "" move nil)
  4810. (unless (assoc (char-to-string char) org-emphasis-alist)
  4811. (error "No such emphasis marker: \"%c\"" char))
  4812. (setq s (char-to-string char)))
  4813. (while (and (> (length string) 1)
  4814. (equal (substring string 0 1) (substring string -1))
  4815. (assoc (substring string 0 1) org-emphasis-alist))
  4816. (setq string (substring string 1 -1)))
  4817. (setq string (concat s string s))
  4818. (if beg (delete-region beg end))
  4819. (unless (or (bolp)
  4820. (string-match (concat "[" (nth 0 erc) "\n]")
  4821. (char-to-string (char-before (point)))))
  4822. (insert " "))
  4823. (unless (or (eobp)
  4824. (string-match (concat "[" (nth 1 erc) "\n]")
  4825. (char-to-string (char-after (point)))))
  4826. (insert " ") (backward-char 1))
  4827. (insert string)
  4828. (and move (backward-char 1))))
  4829. (defconst org-nonsticky-props
  4830. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4831. (defsubst org-rear-nonsticky-at (pos)
  4832. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4833. (defun org-activate-plain-links (limit)
  4834. "Run through the buffer and add overlays to links."
  4835. (catch 'exit
  4836. (let (f hl)
  4837. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4838. (not (org-in-src-block-p)))
  4839. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4840. (setq f (get-text-property (match-beginning 0) 'face))
  4841. (setq hl (org-match-string-no-properties 0))
  4842. (if (or (eq f 'org-tag)
  4843. (and (listp f) (memq 'org-tag f)))
  4844. nil
  4845. (add-text-properties (match-beginning 0) (match-end 0)
  4846. (list 'mouse-face 'highlight
  4847. 'face 'org-link
  4848. 'htmlize-link `(:uri ,hl)
  4849. 'keymap org-mouse-map))
  4850. (org-rear-nonsticky-at (match-end 0)))
  4851. t))))
  4852. (defun org-activate-code (limit)
  4853. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4854. (progn
  4855. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4856. (remove-text-properties (match-beginning 0) (match-end 0)
  4857. '(display t invisible t intangible t))
  4858. t)))
  4859. (defcustom org-src-fontify-natively nil
  4860. "When non-nil, fontify code in code blocks."
  4861. :type 'boolean
  4862. :version "24.1"
  4863. :group 'org-appearance
  4864. :group 'org-babel)
  4865. (defcustom org-allow-promoting-top-level-subtree nil
  4866. "When non-nil, allow promoting a top level subtree.
  4867. The leading star of the top level headline will be replaced
  4868. by a #."
  4869. :type 'boolean
  4870. :version "24.1"
  4871. :group 'org-appearance)
  4872. (defun org-fontify-meta-lines-and-blocks (limit)
  4873. (condition-case nil
  4874. (org-fontify-meta-lines-and-blocks-1 limit)
  4875. (error (message "org-mode fontification error"))))
  4876. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4877. "Fontify #+ lines and blocks, in the correct ways."
  4878. (let ((case-fold-search t))
  4879. (if (re-search-forward
  4880. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4881. limit t)
  4882. (let ((beg (match-beginning 0))
  4883. (block-start (match-end 0))
  4884. (block-end nil)
  4885. (lang (match-string 7))
  4886. (beg1 (line-beginning-position 2))
  4887. (dc1 (downcase (match-string 2)))
  4888. (dc3 (downcase (match-string 3)))
  4889. end end1 quoting block-type ovl)
  4890. (cond
  4891. ((member dc1 '("+html:" "+ascii:" "+latex:" "+docbook:"))
  4892. ;; a single line of backend-specific content
  4893. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4894. (remove-text-properties (match-beginning 0) (match-end 0)
  4895. '(display t invisible t intangible t))
  4896. (add-text-properties (match-beginning 1) (match-end 3)
  4897. '(font-lock-fontified t face org-meta-line))
  4898. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4899. '(font-lock-fontified t face org-block))
  4900. ; for backend-specific code
  4901. t)
  4902. ((and (match-end 4) (equal dc3 "+begin"))
  4903. ;; Truly a block
  4904. (setq block-type (downcase (match-string 5))
  4905. quoting (member block-type org-protecting-blocks))
  4906. (when (re-search-forward
  4907. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4908. nil t) ;; on purpose, we look further than LIMIT
  4909. (setq end (min (point-max) (match-end 0))
  4910. end1 (min (point-max) (1- (match-beginning 0))))
  4911. (setq block-end (match-beginning 0))
  4912. (when quoting
  4913. (remove-text-properties beg end
  4914. '(display t invisible t intangible t)))
  4915. (add-text-properties
  4916. beg end
  4917. '(font-lock-fontified t font-lock-multiline t))
  4918. (add-text-properties beg beg1 '(face org-meta-line))
  4919. (add-text-properties end1 (min (point-max) (1+ end))
  4920. '(face org-meta-line)) ; for end_src
  4921. (cond
  4922. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4923. (org-src-font-lock-fontify-block lang block-start block-end)
  4924. ;; remove old background overlays
  4925. (mapc (lambda (ov)
  4926. (if (eq (overlay-get ov 'face) 'org-block-background)
  4927. (delete-overlay ov)))
  4928. (overlays-at (/ (+ beg1 block-end) 2)))
  4929. ;; add a background overlay
  4930. (setq ovl (make-overlay beg1 block-end))
  4931. (overlay-put ovl 'face 'org-block-background)
  4932. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4933. (quoting
  4934. (add-text-properties beg1 (min (point-max) (1+ end1))
  4935. '(face org-block))) ; end of source block
  4936. ((not org-fontify-quote-and-verse-blocks))
  4937. ((string= block-type "quote")
  4938. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  4939. ((string= block-type "verse")
  4940. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  4941. (add-text-properties beg beg1 '(face org-block-begin-line))
  4942. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  4943. '(face org-block-end-line))
  4944. t))
  4945. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  4946. (add-text-properties
  4947. beg (match-end 3)
  4948. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4949. '(font-lock-fontified t invisible t)
  4950. '(font-lock-fontified t face org-document-info-keyword)))
  4951. (add-text-properties
  4952. (match-beginning 6) (1+ (match-end 6))
  4953. (if (string-equal dc1 "+title:")
  4954. '(font-lock-fontified t face org-document-title)
  4955. '(font-lock-fontified t face org-document-info))))
  4956. ((or (equal dc1 "+results")
  4957. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  4958. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  4959. "+call:" "+header:" "+headers:" "+name:"))
  4960. (and (match-end 4) (equal dc3 "+attr")))
  4961. (add-text-properties
  4962. beg (match-end 0)
  4963. '(font-lock-fontified t face org-meta-line))
  4964. t)
  4965. ((member dc3 '(" " ""))
  4966. (add-text-properties
  4967. beg (match-end 0)
  4968. '(font-lock-fontified t face font-lock-comment-face)))
  4969. ((not (member (char-after beg) '(?\ ?\t)))
  4970. ;; just any other in-buffer setting, but not indented
  4971. (add-text-properties
  4972. beg (match-end 0)
  4973. '(font-lock-fontified t face org-meta-line))
  4974. t)
  4975. (t nil))))))
  4976. (defun org-activate-angle-links (limit)
  4977. "Run through the buffer and add overlays to links."
  4978. (if (and (re-search-forward org-angle-link-re limit t)
  4979. (not (org-in-src-block-p)))
  4980. (progn
  4981. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4982. (add-text-properties (match-beginning 0) (match-end 0)
  4983. (list 'mouse-face 'highlight
  4984. 'keymap org-mouse-map))
  4985. (org-rear-nonsticky-at (match-end 0))
  4986. t)))
  4987. (defun org-activate-footnote-links (limit)
  4988. "Run through the buffer and add overlays to footnotes."
  4989. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4990. (when fn
  4991. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4992. (org-remove-flyspell-overlays-in beg end)
  4993. (add-text-properties beg end
  4994. (list 'mouse-face 'highlight
  4995. 'keymap org-mouse-map
  4996. 'help-echo
  4997. (if (= (point-at-bol) beg)
  4998. "Footnote definition"
  4999. "Footnote reference")
  5000. 'font-lock-fontified t
  5001. 'font-lock-multiline t
  5002. 'face 'org-footnote))))))
  5003. (defun org-activate-bracket-links (limit)
  5004. "Run through the buffer and add overlays to bracketed links."
  5005. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5006. (not (org-in-src-block-p)))
  5007. (let* ((hl (org-match-string-no-properties 1))
  5008. (help (concat "LINK: " hl))
  5009. ;; FIXME: Above we should remove the escapes. But that
  5010. ;; requires another match, protecting match data, a lot
  5011. ;; of overhead for font-lock.
  5012. (ip (org-maybe-intangible
  5013. (list 'invisible 'org-link
  5014. 'keymap org-mouse-map 'mouse-face 'highlight
  5015. 'font-lock-multiline t 'help-echo help
  5016. 'htmlize-link `(:uri ,hl))))
  5017. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5018. 'font-lock-multiline t 'help-echo help
  5019. 'htmlize-link `(:uri ,hl))))
  5020. ;; We need to remove the invisible property here. Table narrowing
  5021. ;; may have made some of this invisible.
  5022. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5023. (remove-text-properties (match-beginning 0) (match-end 0)
  5024. '(invisible nil))
  5025. (if (match-end 3)
  5026. (progn
  5027. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5028. (org-rear-nonsticky-at (match-beginning 3))
  5029. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5030. (org-rear-nonsticky-at (match-end 3))
  5031. (add-text-properties (match-end 3) (match-end 0) ip)
  5032. (org-rear-nonsticky-at (match-end 0)))
  5033. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5034. (org-rear-nonsticky-at (match-beginning 1))
  5035. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5036. (org-rear-nonsticky-at (match-end 1))
  5037. (add-text-properties (match-end 1) (match-end 0) ip)
  5038. (org-rear-nonsticky-at (match-end 0)))
  5039. t)))
  5040. (defun org-activate-dates (limit)
  5041. "Run through the buffer and add overlays to dates."
  5042. (if (re-search-forward org-tsr-regexp-both limit t)
  5043. (progn
  5044. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5045. (add-text-properties (match-beginning 0) (match-end 0)
  5046. (list 'mouse-face 'highlight
  5047. 'keymap org-mouse-map))
  5048. (org-rear-nonsticky-at (match-end 0))
  5049. (when org-display-custom-times
  5050. (if (match-end 3)
  5051. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5052. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5053. t)))
  5054. (defvar org-target-link-regexp nil
  5055. "Regular expression matching radio targets in plain text.")
  5056. (make-variable-buffer-local 'org-target-link-regexp)
  5057. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5058. "Regular expression matching a link target.")
  5059. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5060. "Regular expression matching a radio target.")
  5061. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5062. "Regular expression matching any target.")
  5063. (defun org-activate-target-links (limit)
  5064. "Run through the buffer and add overlays to target matches."
  5065. (when org-target-link-regexp
  5066. (let ((case-fold-search t))
  5067. (if (re-search-forward org-target-link-regexp limit t)
  5068. (progn
  5069. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5070. (add-text-properties (match-beginning 0) (match-end 0)
  5071. (list 'mouse-face 'highlight
  5072. 'keymap org-mouse-map
  5073. 'help-echo "Radio target link"
  5074. 'org-linked-text t))
  5075. (org-rear-nonsticky-at (match-end 0))
  5076. t)))))
  5077. (defun org-update-radio-target-regexp ()
  5078. "Find all radio targets in this file and update the regular expression."
  5079. (interactive)
  5080. (when (memq 'radio org-activate-links)
  5081. (setq org-target-link-regexp
  5082. (org-make-target-link-regexp (org-all-targets 'radio)))
  5083. (org-restart-font-lock)))
  5084. (defun org-hide-wide-columns (limit)
  5085. (let (s e)
  5086. (setq s (text-property-any (point) (or limit (point-max))
  5087. 'org-cwidth t))
  5088. (when s
  5089. (setq e (next-single-property-change s 'org-cwidth))
  5090. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5091. (goto-char e)
  5092. t)))
  5093. (defvar org-latex-and-specials-regexp nil
  5094. "Regular expression for highlighting export special stuff.")
  5095. (defvar org-match-substring-regexp)
  5096. (defvar org-match-substring-with-braces-regexp)
  5097. ;; This should be with the exporter code, but we also use if for font-locking
  5098. (defconst org-export-html-special-string-regexps
  5099. '(("\\\\-" . "&shy;")
  5100. ("---\\([^-]\\)" . "&mdash;\\1")
  5101. ("--\\([^-]\\)" . "&ndash;\\1")
  5102. ("\\.\\.\\." . "&hellip;"))
  5103. "Regular expressions for special string conversion.")
  5104. (defun org-compute-latex-and-specials-regexp ()
  5105. "Compute regular expression for stuff treated specially by exporters."
  5106. (if (not org-highlight-latex-fragments-and-specials)
  5107. (org-set-local 'org-latex-and-specials-regexp nil)
  5108. (require 'org-exp)
  5109. (let*
  5110. ((matchers (plist-get org-format-latex-options :matchers))
  5111. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  5112. org-latex-regexps)))
  5113. (org-export-allow-BIND nil)
  5114. (options (org-combine-plists (org-default-export-plist)
  5115. (org-infile-export-plist)))
  5116. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  5117. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  5118. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  5119. (org-export-html-expand (plist-get options :expand-quoted-html))
  5120. (org-export-with-special-strings (plist-get options :special-strings))
  5121. (re-sub
  5122. (cond
  5123. ((equal org-export-with-sub-superscripts '{})
  5124. (list org-match-substring-with-braces-regexp))
  5125. (org-export-with-sub-superscripts
  5126. (list org-match-substring-regexp))))
  5127. (re-latex
  5128. (if org-export-with-LaTeX-fragments
  5129. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5130. (re-macros
  5131. (if org-export-with-TeX-macros
  5132. (list (concat "\\\\"
  5133. (regexp-opt
  5134. (append
  5135. (delq nil
  5136. (mapcar 'car-safe
  5137. (append org-entities-user
  5138. org-entities)))
  5139. (if (boundp 'org-latex-entities)
  5140. (mapcar (lambda (x)
  5141. (or (car-safe x) x))
  5142. org-latex-entities)
  5143. nil))
  5144. 'words))) ; FIXME
  5145. ))
  5146. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5147. (re-special (if org-export-with-special-strings
  5148. (mapcar (lambda (x) (car x))
  5149. org-export-html-special-string-regexps)))
  5150. (re-rest
  5151. (delq nil
  5152. (list
  5153. (if org-export-html-expand "@<[^>\n]+>")
  5154. ))))
  5155. (org-set-local
  5156. 'org-latex-and-specials-regexp
  5157. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5158. re-rest) "\\|")))))
  5159. (defun org-do-latex-and-special-faces (limit)
  5160. "Run through the buffer and add overlays to links."
  5161. (when org-latex-and-specials-regexp
  5162. (let (rtn d)
  5163. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5164. limit t))
  5165. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5166. 'face))
  5167. '(org-code org-verbatim underline)))
  5168. (progn
  5169. (setq rtn t
  5170. d (cond ((member (char-after (1+ (match-beginning 0)))
  5171. '(?_ ?^)) 1)
  5172. (t 0)))
  5173. (font-lock-prepend-text-property
  5174. (+ d (match-beginning 0)) (match-end 0)
  5175. 'face 'org-latex-and-export-specials)
  5176. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5177. '(font-lock-multiline t)))))
  5178. rtn)))
  5179. (defun org-restart-font-lock ()
  5180. "Restart `font-lock-mode', to force refontification."
  5181. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5182. (font-lock-mode -1)
  5183. (font-lock-mode 1)))
  5184. (defun org-all-targets (&optional radio)
  5185. "Return a list of all targets in this file.
  5186. When optional argument RADIO is non-nil, only find radio
  5187. targets."
  5188. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5189. (save-excursion
  5190. (goto-char (point-min))
  5191. (while (re-search-forward re nil t)
  5192. ;; Make sure point is really within the object.
  5193. (backward-char)
  5194. (let ((obj (org-element-context)))
  5195. (when (memq (org-element-type obj) '(radio-target target))
  5196. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5197. rtn)))
  5198. (defun org-make-target-link-regexp (targets)
  5199. "Make regular expression matching all strings in TARGETS.
  5200. The regular expression finds the targets also if there is a line break
  5201. between words."
  5202. (and targets
  5203. (concat
  5204. "\\<\\("
  5205. (mapconcat
  5206. (lambda (x)
  5207. (setq x (regexp-quote x))
  5208. (while (string-match " +" x)
  5209. (setq x (replace-match "\\s-+" t t x)))
  5210. x)
  5211. targets
  5212. "\\|")
  5213. "\\)\\>")))
  5214. (defun org-activate-tags (limit)
  5215. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5216. (progn
  5217. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5218. (add-text-properties (match-beginning 1) (match-end 1)
  5219. (list 'mouse-face 'highlight
  5220. 'keymap org-mouse-map))
  5221. (org-rear-nonsticky-at (match-end 1))
  5222. t)))
  5223. (defun org-outline-level ()
  5224. "Compute the outline level of the heading at point.
  5225. If this is called at a normal headline, the level is the number of stars.
  5226. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5227. (save-excursion
  5228. (if (not (condition-case nil
  5229. (org-back-to-heading t)
  5230. (error nil)))
  5231. 0
  5232. (looking-at org-outline-regexp)
  5233. (1- (- (match-end 0) (match-beginning 0))))))
  5234. (defvar org-font-lock-keywords nil)
  5235. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5236. "Regular expression matching a property line.")
  5237. (defvar org-font-lock-hook nil
  5238. "Functions to be called for special font lock stuff.")
  5239. (defvar org-font-lock-set-keywords-hook nil
  5240. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5241. This is called after `org-font-lock-extra-keywords' is defined, but before
  5242. it is installed to be used by font lock. This can be useful if something
  5243. needs to be inserted at a specific position in the font-lock sequence.")
  5244. (defun org-font-lock-hook (limit)
  5245. "Run `org-font-lock-hook' within LIMIT."
  5246. (run-hook-with-args 'org-font-lock-hook limit))
  5247. (defun org-set-font-lock-defaults ()
  5248. "Set font lock defaults for the current buffer."
  5249. (let* ((em org-fontify-emphasized-text)
  5250. (lk org-activate-links)
  5251. (org-font-lock-extra-keywords
  5252. (list
  5253. ;; Call the hook
  5254. '(org-font-lock-hook)
  5255. ;; Headlines
  5256. `(,(if org-fontify-whole-heading-line
  5257. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5258. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5259. (1 (org-get-level-face 1))
  5260. (2 (org-get-level-face 2))
  5261. (3 (org-get-level-face 3)))
  5262. ;; Table lines
  5263. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5264. (1 'org-table t))
  5265. ;; Table internals
  5266. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5267. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5268. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5269. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5270. ;; Drawers
  5271. (list org-drawer-regexp '(0 'org-special-keyword t))
  5272. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5273. ;; Properties
  5274. (list org-property-re
  5275. '(1 'org-special-keyword t)
  5276. '(3 'org-property-value t))
  5277. ;; Links
  5278. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5279. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5280. (if (memq 'plain lk) '(org-activate-plain-links))
  5281. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5282. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5283. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5284. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5285. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5286. '(org-hide-wide-columns (0 nil append))
  5287. ;; TODO keyword
  5288. (list (format org-heading-keyword-regexp-format
  5289. org-todo-regexp)
  5290. '(2 (org-get-todo-face 2) t))
  5291. ;; DONE
  5292. (if org-fontify-done-headline
  5293. (list (format org-heading-keyword-regexp-format
  5294. (concat
  5295. "\\(?:"
  5296. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5297. "\\)"))
  5298. '(2 'org-headline-done t))
  5299. nil)
  5300. ;; Priorities
  5301. '(org-font-lock-add-priority-faces)
  5302. ;; Tags
  5303. '(org-font-lock-add-tag-faces)
  5304. ;; Special keywords
  5305. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5306. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5307. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5308. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5309. ;; Emphasis
  5310. (if em
  5311. (if (featurep 'xemacs)
  5312. '(org-do-emphasis-faces (0 nil append))
  5313. '(org-do-emphasis-faces)))
  5314. ;; Checkboxes
  5315. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5316. 1 'org-checkbox prepend)
  5317. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5318. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5319. (0 (org-get-checkbox-statistics-face) t)))
  5320. ;; Description list items
  5321. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5322. 1 'org-list-dt prepend)
  5323. ;; ARCHIVEd headings
  5324. (list (concat
  5325. org-outline-regexp-bol
  5326. "\\(.*:" org-archive-tag ":.*\\)")
  5327. '(1 'org-archived prepend))
  5328. ;; Specials
  5329. '(org-do-latex-and-special-faces)
  5330. '(org-fontify-entities)
  5331. '(org-raise-scripts)
  5332. ;; Code
  5333. '(org-activate-code (1 'org-code t))
  5334. ;; COMMENT
  5335. (list (format org-heading-keyword-regexp-format
  5336. (concat "\\("
  5337. org-comment-string "\\|" org-quote-string
  5338. "\\)"))
  5339. '(2 'org-special-keyword t))
  5340. ;; Blocks and meta lines
  5341. '(org-fontify-meta-lines-and-blocks)
  5342. )))
  5343. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5344. (run-hooks 'org-font-lock-set-keywords-hook)
  5345. ;; Now set the full font-lock-keywords
  5346. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5347. (org-set-local 'font-lock-defaults
  5348. '(org-font-lock-keywords t nil nil backward-paragraph))
  5349. (kill-local-variable 'font-lock-keywords) nil))
  5350. (defun org-toggle-pretty-entities ()
  5351. "Toggle the composition display of entities as UTF8 characters."
  5352. (interactive)
  5353. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5354. (org-restart-font-lock)
  5355. (if org-pretty-entities
  5356. (message "Entities are displayed as UTF8 characters")
  5357. (save-restriction
  5358. (widen)
  5359. (org-decompose-region (point-min) (point-max))
  5360. (message "Entities are displayed plain"))))
  5361. (defvar org-custom-properties-overlays nil
  5362. "List of overlays used for custom properties.")
  5363. (make-variable-buffer-local 'org-custom-properties-overlays)
  5364. (defun org-toggle-custom-properties-visibility ()
  5365. "Display or hide properties in `org-custom-properties'."
  5366. (interactive)
  5367. (if org-custom-properties-overlays
  5368. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5369. (setq org-custom-properties-overlays nil))
  5370. (unless (not org-custom-properties)
  5371. (save-excursion
  5372. (save-restriction
  5373. (widen)
  5374. (goto-char (point-min))
  5375. (while (re-search-forward org-property-re nil t)
  5376. (mapc (lambda(p)
  5377. (when (equal p (substring (match-string 1) 1 -1))
  5378. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5379. (overlay-put o 'invisible t)
  5380. (overlay-put o 'org-custom-property t)
  5381. (push o org-custom-properties-overlays))))
  5382. org-custom-properties)))))))
  5383. (defun org-fontify-entities (limit)
  5384. "Find an entity to fontify."
  5385. (let (ee)
  5386. (when org-pretty-entities
  5387. (catch 'match
  5388. (while (re-search-forward
  5389. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5390. limit t)
  5391. (if (and (not (org-in-indented-comment-line))
  5392. (setq ee (org-entity-get (match-string 1)))
  5393. (= (length (nth 6 ee)) 1))
  5394. (let*
  5395. ((end (if (equal (match-string 2) "{}")
  5396. (match-end 2)
  5397. (match-end 1))))
  5398. (add-text-properties
  5399. (match-beginning 0) end
  5400. (list 'font-lock-fontified t))
  5401. (compose-region (match-beginning 0) end
  5402. (nth 6 ee) nil)
  5403. (backward-char 1)
  5404. (throw 'match t))))
  5405. nil))))
  5406. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5407. "Fontify string S like in Org-mode."
  5408. (with-temp-buffer
  5409. (insert s)
  5410. (let ((org-odd-levels-only odd-levels))
  5411. (org-mode)
  5412. (font-lock-fontify-buffer)
  5413. (buffer-string))))
  5414. (defvar org-m nil)
  5415. (defvar org-l nil)
  5416. (defvar org-f nil)
  5417. (defun org-get-level-face (n)
  5418. "Get the right face for match N in font-lock matching of headlines."
  5419. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5420. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5421. (if org-cycle-level-faces
  5422. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5423. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5424. (cond
  5425. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5426. ((eq n 2) org-f)
  5427. (t (if org-level-color-stars-only nil org-f))))
  5428. (defun org-get-todo-face (kwd)
  5429. "Get the right face for a TODO keyword KWD.
  5430. If KWD is a number, get the corresponding match group."
  5431. (if (numberp kwd) (setq kwd (match-string kwd)))
  5432. (or (org-face-from-face-or-color
  5433. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5434. (and (member kwd org-done-keywords) 'org-done)
  5435. 'org-todo))
  5436. (defun org-face-from-face-or-color (context inherit face-or-color)
  5437. "Create a face list that inherits INHERIT, but sets the foreground color.
  5438. When FACE-OR-COLOR is not a string, just return it."
  5439. (if (stringp face-or-color)
  5440. (list :inherit inherit
  5441. (cdr (assoc context org-faces-easy-properties))
  5442. face-or-color)
  5443. face-or-color))
  5444. (defun org-font-lock-add-tag-faces (limit)
  5445. "Add the special tag faces."
  5446. (when (and org-tag-faces org-tags-special-faces-re)
  5447. (while (re-search-forward org-tags-special-faces-re limit t)
  5448. (add-text-properties (match-beginning 1) (match-end 1)
  5449. (list 'face (org-get-tag-face 1)
  5450. 'font-lock-fontified t))
  5451. (backward-char 1))))
  5452. (defun org-font-lock-add-priority-faces (limit)
  5453. "Add the special priority faces."
  5454. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5455. (when (save-match-data (org-at-heading-p))
  5456. (add-text-properties
  5457. (match-beginning 0) (match-end 0)
  5458. (list 'face (or (org-face-from-face-or-color
  5459. 'priority 'org-special-keyword
  5460. (cdr (assoc (char-after (match-beginning 1))
  5461. org-priority-faces)))
  5462. 'org-special-keyword)
  5463. 'font-lock-fontified t)))))
  5464. (defun org-get-tag-face (kwd)
  5465. "Get the right face for a TODO keyword KWD.
  5466. If KWD is a number, get the corresponding match group."
  5467. (if (numberp kwd) (setq kwd (match-string kwd)))
  5468. (or (org-face-from-face-or-color
  5469. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5470. 'org-tag))
  5471. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5472. "Remove fontification and activation overlays from links."
  5473. (font-lock-default-unfontify-region beg end)
  5474. (let* ((buffer-undo-list t)
  5475. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5476. (inhibit-modification-hooks t)
  5477. deactivate-mark buffer-file-name buffer-file-truename)
  5478. (org-decompose-region beg end)
  5479. (remove-text-properties beg end
  5480. '(mouse-face t keymap t org-linked-text t
  5481. invisible t intangible t
  5482. org-no-flyspell t org-emphasis t))
  5483. (org-remove-font-lock-display-properties beg end)))
  5484. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5485. ((raise 0.3) (height 0.7))
  5486. ((raise -0.5))
  5487. ((raise 0.5)))
  5488. "Display properties for showing superscripts and subscripts.")
  5489. (defun org-remove-font-lock-display-properties (beg end)
  5490. "Remove specific display properties that have been added by font lock.
  5491. The will remove the raise properties that are used to show superscripts
  5492. and subscripts."
  5493. (let (next prop)
  5494. (while (< beg end)
  5495. (setq next (next-single-property-change beg 'display nil end)
  5496. prop (get-text-property beg 'display))
  5497. (if (member prop org-script-display)
  5498. (put-text-property beg next 'display nil))
  5499. (setq beg next))))
  5500. (defun org-raise-scripts (limit)
  5501. "Add raise properties to sub/superscripts."
  5502. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5503. (if (re-search-forward
  5504. (if (eq org-use-sub-superscripts t)
  5505. org-match-substring-regexp
  5506. org-match-substring-with-braces-regexp)
  5507. limit t)
  5508. (let* ((pos (point)) table-p comment-p
  5509. (mpos (match-beginning 3))
  5510. (emph-p (get-text-property mpos 'org-emphasis))
  5511. (link-p (get-text-property mpos 'mouse-face))
  5512. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5513. (goto-char (point-at-bol))
  5514. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5515. comment-p (org-looking-at-p "[ \t]*#"))
  5516. (goto-char pos)
  5517. ;; FIXME: Should we go back one character here, for a_b^c
  5518. ;; (goto-char (1- pos)) ;????????????????????
  5519. (if (or comment-p emph-p link-p keyw-p)
  5520. t
  5521. (put-text-property (match-beginning 3) (match-end 0)
  5522. 'display
  5523. (if (equal (char-after (match-beginning 2)) ?^)
  5524. (nth (if table-p 3 1) org-script-display)
  5525. (nth (if table-p 2 0) org-script-display)))
  5526. (add-text-properties (match-beginning 2) (match-end 2)
  5527. (list 'invisible t
  5528. 'org-dwidth t 'org-dwidth-n 1))
  5529. (if (and (eq (char-after (match-beginning 3)) ?{)
  5530. (eq (char-before (match-end 3)) ?}))
  5531. (progn
  5532. (add-text-properties
  5533. (match-beginning 3) (1+ (match-beginning 3))
  5534. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5535. (add-text-properties
  5536. (1- (match-end 3)) (match-end 3)
  5537. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5538. t)))))
  5539. ;;;; Visibility cycling, including org-goto and indirect buffer
  5540. ;;; Cycling
  5541. (defvar org-cycle-global-status nil)
  5542. (make-variable-buffer-local 'org-cycle-global-status)
  5543. (defvar org-cycle-subtree-status nil)
  5544. (make-variable-buffer-local 'org-cycle-subtree-status)
  5545. (defvar org-inlinetask-min-level)
  5546. ;;;###autoload
  5547. (defun org-cycle (&optional arg)
  5548. "TAB-action and visibility cycling for Org-mode.
  5549. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5550. is outline visibility cycling, but it also invokes other actions
  5551. in special contexts.
  5552. - When this function is called with a prefix argument, rotate the entire
  5553. buffer through 3 states (global cycling)
  5554. 1. OVERVIEW: Show only top-level headlines.
  5555. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5556. 3. SHOW ALL: Show everything.
  5557. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5558. determined by the variable `org-startup-folded', and by any VISIBILITY
  5559. properties in the buffer.
  5560. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5561. including any drawers.
  5562. - When inside a table, re-align the table and move to the next field.
  5563. - When point is at the beginning of a headline, rotate the subtree started
  5564. by this line through 3 different states (local cycling)
  5565. 1. FOLDED: Only the main headline is shown.
  5566. 2. CHILDREN: The main headline and the direct children are shown.
  5567. From this state, you can move to one of the children
  5568. and zoom in further.
  5569. 3. SUBTREE: Show the entire subtree, including body text.
  5570. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5571. - When point is at the beginning of an empty headline and the variable
  5572. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5573. of the headline by demoting and promoting it to likely levels. This
  5574. speeds up creation document structure by pressing TAB once or several
  5575. times right after creating a new headline.
  5576. - When there is a numeric prefix, go up to a heading with level ARG, do
  5577. a `show-subtree' and return to the previous cursor position. If ARG
  5578. is negative, go up that many levels.
  5579. - When point is not at the beginning of a headline, execute the global
  5580. binding for TAB, which is re-indenting the line. See the option
  5581. `org-cycle-emulate-tab' for details.
  5582. - Special case: if point is at the beginning of the buffer and there is
  5583. no headline in line 1, this function will act as if called with prefix arg
  5584. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5585. But only if also the variable `org-cycle-global-at-bob' is t."
  5586. (interactive "P")
  5587. (org-load-modules-maybe)
  5588. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5589. (and org-cycle-level-after-item/entry-creation
  5590. (or (org-cycle-level)
  5591. (org-cycle-item-indentation))))
  5592. (let* (message-log-max ; Don't populate the *Messages* buffer
  5593. (limit-level
  5594. (or org-cycle-max-level
  5595. (and (boundp 'org-inlinetask-min-level)
  5596. org-inlinetask-min-level
  5597. (1- org-inlinetask-min-level))))
  5598. (nstars (and limit-level
  5599. (if org-odd-levels-only
  5600. (and limit-level (1- (* limit-level 2)))
  5601. limit-level)))
  5602. (org-outline-regexp
  5603. (if (not (derived-mode-p 'org-mode))
  5604. outline-regexp
  5605. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5606. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5607. (not (looking-at org-outline-regexp))))
  5608. (org-cycle-hook
  5609. (if bob-special
  5610. (delq 'org-optimize-window-after-visibility-change
  5611. (copy-sequence org-cycle-hook))
  5612. org-cycle-hook))
  5613. (pos (point)))
  5614. (if (or bob-special (equal arg '(4)))
  5615. ;; special case: use global cycling
  5616. (setq arg t))
  5617. (cond
  5618. ((equal arg '(16))
  5619. (setq last-command 'dummy)
  5620. (org-set-startup-visibility)
  5621. (message "Startup visibility, plus VISIBILITY properties"))
  5622. ((equal arg '(64))
  5623. (show-all)
  5624. (message "Entire buffer visible, including drawers"))
  5625. ;; Table: enter it or move to the next field.
  5626. ((org-at-table-p 'any)
  5627. (if (org-at-table.el-p)
  5628. (message "Use C-c ' to edit table.el tables")
  5629. (if arg (org-table-edit-field t)
  5630. (org-table-justify-field-maybe)
  5631. (call-interactively 'org-table-next-field))))
  5632. ((run-hook-with-args-until-success
  5633. 'org-tab-after-check-for-table-hook))
  5634. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5635. ((eq arg t) (org-cycle-internal-global))
  5636. ;; Drawers: delegate to `org-flag-drawer'.
  5637. ((and org-drawers org-drawer-regexp
  5638. (save-excursion
  5639. (beginning-of-line 1)
  5640. (looking-at org-drawer-regexp)))
  5641. (org-flag-drawer ; toggle block visibility
  5642. (not (get-char-property (match-end 0) 'invisible))))
  5643. ;; Show-subtree, ARG levels up from here.
  5644. ((integerp arg)
  5645. (save-excursion
  5646. (org-back-to-heading)
  5647. (outline-up-heading (if (< arg 0) (- arg)
  5648. (- (funcall outline-level) arg)))
  5649. (org-show-subtree)))
  5650. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5651. ((and (featurep 'org-inlinetask)
  5652. (org-inlinetask-at-task-p)
  5653. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5654. (org-inlinetask-toggle-visibility))
  5655. ((org-try-cdlatex-tab))
  5656. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5657. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5658. (save-excursion (beginning-of-line 1)
  5659. (looking-at org-outline-regexp)))
  5660. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5661. (org-cycle-internal-local))
  5662. ;; From there: TAB emulation and template completion.
  5663. (buffer-read-only (org-back-to-heading))
  5664. ((run-hook-with-args-until-success
  5665. 'org-tab-after-check-for-cycling-hook))
  5666. ((org-try-structure-completion))
  5667. ((run-hook-with-args-until-success
  5668. 'org-tab-before-tab-emulation-hook))
  5669. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5670. (or (not (bolp))
  5671. (not (looking-at org-outline-regexp))))
  5672. (call-interactively (global-key-binding "\t")))
  5673. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5674. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5675. (or (and (eq org-cycle-emulate-tab 'white)
  5676. (= (match-end 0) (point-at-eol)))
  5677. (and (eq org-cycle-emulate-tab 'whitestart)
  5678. (>= (match-end 0) pos))))
  5679. t
  5680. (eq org-cycle-emulate-tab t))
  5681. (call-interactively (global-key-binding "\t")))
  5682. (t (save-excursion
  5683. (org-back-to-heading)
  5684. (org-cycle)))))))
  5685. (defun org-cycle-internal-global ()
  5686. "Do the global cycling action."
  5687. ;; Hack to avoid display of messages for .org attachments in Gnus
  5688. (let (message-log-max ; Don't populate the *Messages* buffer
  5689. (ga (string-match "\\*fontification" (buffer-name))))
  5690. (cond
  5691. ((and (eq last-command this-command)
  5692. (eq org-cycle-global-status 'overview))
  5693. ;; We just created the overview - now do table of contents
  5694. ;; This can be slow in very large buffers, so indicate action
  5695. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5696. (unless ga (message "CONTENTS..."))
  5697. (org-content)
  5698. (unless ga (message "CONTENTS...done"))
  5699. (setq org-cycle-global-status 'contents)
  5700. (run-hook-with-args 'org-cycle-hook 'contents))
  5701. ((and (eq last-command this-command)
  5702. (eq org-cycle-global-status 'contents))
  5703. ;; We just showed the table of contents - now show everything
  5704. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5705. (show-all)
  5706. (unless ga (message "SHOW ALL"))
  5707. (setq org-cycle-global-status 'all)
  5708. (run-hook-with-args 'org-cycle-hook 'all))
  5709. (t
  5710. ;; Default action: go to overview
  5711. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5712. (org-overview)
  5713. (unless ga (message "OVERVIEW"))
  5714. (setq org-cycle-global-status 'overview)
  5715. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5716. (defun org-cycle-internal-local ()
  5717. "Do the local cycling action."
  5718. (let (message-log-max ; Don't populate the *Messages* buffer
  5719. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5720. ;; First, determine end of headline (EOH), end of subtree or item
  5721. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5722. (save-excursion
  5723. (if (org-at-item-p)
  5724. (progn
  5725. (beginning-of-line)
  5726. (setq struct (org-list-struct))
  5727. (setq eoh (point-at-eol))
  5728. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5729. (setq has-children (org-list-has-child-p (point) struct)))
  5730. (org-back-to-heading)
  5731. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5732. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5733. (setq has-children
  5734. (or (save-excursion
  5735. (let ((level (funcall outline-level)))
  5736. (outline-next-heading)
  5737. (and (org-at-heading-p t)
  5738. (> (funcall outline-level) level))))
  5739. (save-excursion
  5740. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5741. ;; Determine end invisible part of buffer (EOL)
  5742. (beginning-of-line 2)
  5743. ;; XEmacs doesn't have `next-single-char-property-change'
  5744. (if (featurep 'xemacs)
  5745. (while (and (not (eobp)) ;; this is like `next-line'
  5746. (get-char-property (1- (point)) 'invisible))
  5747. (beginning-of-line 2))
  5748. (while (and (not (eobp)) ;; this is like `next-line'
  5749. (get-char-property (1- (point)) 'invisible))
  5750. (goto-char (next-single-char-property-change (point) 'invisible))
  5751. (and (eolp) (beginning-of-line 2))))
  5752. (setq eol (point)))
  5753. ;; Find out what to do next and set `this-command'
  5754. (cond
  5755. ((= eos eoh)
  5756. ;; Nothing is hidden behind this heading
  5757. (unless (org-before-first-heading-p)
  5758. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5759. (message "EMPTY ENTRY")
  5760. (setq org-cycle-subtree-status nil)
  5761. (save-excursion
  5762. (goto-char eos)
  5763. (outline-next-heading)
  5764. (if (outline-invisible-p) (org-flag-heading nil))))
  5765. ((and (or (>= eol eos)
  5766. (not (string-match "\\S-" (buffer-substring eol eos))))
  5767. (or has-children
  5768. (not (setq children-skipped
  5769. org-cycle-skip-children-state-if-no-children))))
  5770. ;; Entire subtree is hidden in one line: children view
  5771. (unless (org-before-first-heading-p)
  5772. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5773. (if (org-at-item-p)
  5774. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5775. (org-show-entry)
  5776. (org-with-limited-levels (show-children))
  5777. ;; FIXME: This slows down the func way too much.
  5778. ;; How keep drawers hidden in subtree anyway?
  5779. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5780. ;; (org-cycle-hide-drawers 'subtree))
  5781. ;; Fold every list in subtree to top-level items.
  5782. (when (eq org-cycle-include-plain-lists 'integrate)
  5783. (save-excursion
  5784. (org-back-to-heading)
  5785. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5786. (beginning-of-line 1)
  5787. (let* ((struct (org-list-struct))
  5788. (prevs (org-list-prevs-alist struct))
  5789. (end (org-list-get-bottom-point struct)))
  5790. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5791. (org-list-get-all-items (point) struct prevs))
  5792. (goto-char end))))))
  5793. (message "CHILDREN")
  5794. (save-excursion
  5795. (goto-char eos)
  5796. (outline-next-heading)
  5797. (if (outline-invisible-p) (org-flag-heading nil)))
  5798. (setq org-cycle-subtree-status 'children)
  5799. (unless (org-before-first-heading-p)
  5800. (run-hook-with-args 'org-cycle-hook 'children)))
  5801. ((or children-skipped
  5802. (and (eq last-command this-command)
  5803. (eq org-cycle-subtree-status 'children)))
  5804. ;; We just showed the children, or no children are there,
  5805. ;; now show everything.
  5806. (unless (org-before-first-heading-p)
  5807. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5808. (outline-flag-region eoh eos nil)
  5809. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5810. (setq org-cycle-subtree-status 'subtree)
  5811. (unless (org-before-first-heading-p)
  5812. (run-hook-with-args 'org-cycle-hook 'subtree)))
  5813. (t
  5814. ;; Default action: hide the subtree.
  5815. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5816. (outline-flag-region eoh eos t)
  5817. (message "FOLDED")
  5818. (setq org-cycle-subtree-status 'folded)
  5819. (unless (org-before-first-heading-p)
  5820. (run-hook-with-args 'org-cycle-hook 'folded))))))
  5821. ;;;###autoload
  5822. (defun org-global-cycle (&optional arg)
  5823. "Cycle the global visibility. For details see `org-cycle'.
  5824. With \\[universal-argument] prefix arg, switch to startup visibility.
  5825. With a numeric prefix, show all headlines up to that level."
  5826. (interactive "P")
  5827. (let ((org-cycle-include-plain-lists
  5828. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5829. (cond
  5830. ((integerp arg)
  5831. (show-all)
  5832. (hide-sublevels arg)
  5833. (setq org-cycle-global-status 'contents))
  5834. ((equal arg '(4))
  5835. (org-set-startup-visibility)
  5836. (message "Startup visibility, plus VISIBILITY properties."))
  5837. (t
  5838. (org-cycle '(4))))))
  5839. (defun org-set-startup-visibility ()
  5840. "Set the visibility required by startup options and properties."
  5841. (cond
  5842. ((eq org-startup-folded t)
  5843. (org-cycle '(4)))
  5844. ((eq org-startup-folded 'content)
  5845. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5846. (org-cycle '(4)) (org-cycle '(4)))))
  5847. (unless (eq org-startup-folded 'showeverything)
  5848. (if org-hide-block-startup (org-hide-block-all))
  5849. (org-set-visibility-according-to-property 'no-cleanup)
  5850. (org-cycle-hide-archived-subtrees 'all)
  5851. (org-cycle-hide-drawers 'all)
  5852. (org-cycle-show-empty-lines t)))
  5853. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5854. "Switch subtree visibilities according to :VISIBILITY: property."
  5855. (interactive)
  5856. (let (org-show-entry-below state)
  5857. (save-excursion
  5858. (goto-char (point-min))
  5859. (while (re-search-forward
  5860. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5861. nil t)
  5862. (setq state (match-string 1))
  5863. (save-excursion
  5864. (org-back-to-heading t)
  5865. (hide-subtree)
  5866. (org-reveal)
  5867. (cond
  5868. ((equal state '("fold" "folded"))
  5869. (hide-subtree))
  5870. ((equal state "children")
  5871. (org-show-hidden-entry)
  5872. (show-children))
  5873. ((equal state "content")
  5874. (save-excursion
  5875. (save-restriction
  5876. (org-narrow-to-subtree)
  5877. (org-content))))
  5878. ((member state '("all" "showall"))
  5879. (show-subtree)))))
  5880. (unless no-cleanup
  5881. (org-cycle-hide-archived-subtrees 'all)
  5882. (org-cycle-hide-drawers 'all)
  5883. (org-cycle-show-empty-lines 'all)))))
  5884. ;; This function uses outline-regexp instead of the more fundamental
  5885. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5886. ;; buffers, where outline-regexp is needed.
  5887. (defun org-overview ()
  5888. "Switch to overview mode, showing only top-level headlines.
  5889. Really, this shows all headlines with level equal or greater than the level
  5890. of the first headline in the buffer. This is important, because if the
  5891. first headline is not level one, then (hide-sublevels 1) gives confusing
  5892. results."
  5893. (interactive)
  5894. (let ((level (save-excursion
  5895. (goto-char (point-min))
  5896. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5897. (progn
  5898. (goto-char (match-beginning 0))
  5899. (funcall outline-level))))))
  5900. (and level (hide-sublevels level))))
  5901. (defun org-content (&optional arg)
  5902. "Show all headlines in the buffer, like a table of contents.
  5903. With numerical argument N, show content up to level N."
  5904. (interactive "P")
  5905. (save-excursion
  5906. ;; Visit all headings and show their offspring
  5907. (and (integerp arg) (org-overview))
  5908. (goto-char (point-max))
  5909. (catch 'exit
  5910. (while (and (progn (condition-case nil
  5911. (outline-previous-visible-heading 1)
  5912. (error (goto-char (point-min))))
  5913. t)
  5914. (looking-at org-outline-regexp))
  5915. (if (integerp arg)
  5916. (show-children (1- arg))
  5917. (show-branches))
  5918. (if (bobp) (throw 'exit nil))))))
  5919. (defun org-optimize-window-after-visibility-change (state)
  5920. "Adjust the window after a change in outline visibility.
  5921. This function is the default value of the hook `org-cycle-hook'."
  5922. (when (get-buffer-window (current-buffer))
  5923. (cond
  5924. ((eq state 'content) nil)
  5925. ((eq state 'all) nil)
  5926. ((eq state 'folded) nil)
  5927. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5928. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5929. (defun org-remove-empty-overlays-at (pos)
  5930. "Remove outline overlays that do not contain non-white stuff."
  5931. (mapc
  5932. (lambda (o)
  5933. (and (eq 'outline (overlay-get o 'invisible))
  5934. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5935. (overlay-end o))))
  5936. (delete-overlay o)))
  5937. (overlays-at pos)))
  5938. (defun org-clean-visibility-after-subtree-move ()
  5939. "Fix visibility issues after moving a subtree."
  5940. ;; First, find a reasonable region to look at:
  5941. ;; Start two siblings above, end three below
  5942. (let* ((beg (save-excursion
  5943. (and (org-get-last-sibling)
  5944. (org-get-last-sibling))
  5945. (point)))
  5946. (end (save-excursion
  5947. (and (org-get-next-sibling)
  5948. (org-get-next-sibling)
  5949. (org-get-next-sibling))
  5950. (if (org-at-heading-p)
  5951. (point-at-eol)
  5952. (point))))
  5953. (level (looking-at "\\*+"))
  5954. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5955. (save-excursion
  5956. (save-restriction
  5957. (narrow-to-region beg end)
  5958. (when re
  5959. ;; Properly fold already folded siblings
  5960. (goto-char (point-min))
  5961. (while (re-search-forward re nil t)
  5962. (if (and (not (outline-invisible-p))
  5963. (save-excursion
  5964. (goto-char (point-at-eol)) (outline-invisible-p)))
  5965. (hide-entry))))
  5966. (org-cycle-show-empty-lines 'overview)
  5967. (org-cycle-hide-drawers 'overview)))))
  5968. (defun org-cycle-show-empty-lines (state)
  5969. "Show empty lines above all visible headlines.
  5970. The region to be covered depends on STATE when called through
  5971. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5972. entire buffer covered. Note that an empty line is only shown if there
  5973. are at least `org-cycle-separator-lines' empty lines before the headline."
  5974. (when (not (= org-cycle-separator-lines 0))
  5975. (save-excursion
  5976. (let* ((n (abs org-cycle-separator-lines))
  5977. (re (cond
  5978. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5979. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5980. (t (let ((ns (number-to-string (- n 2))))
  5981. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5982. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5983. beg end b e)
  5984. (cond
  5985. ((memq state '(overview contents t))
  5986. (setq beg (point-min) end (point-max)))
  5987. ((memq state '(children folded))
  5988. (setq beg (point) end (progn (org-end-of-subtree t t)
  5989. (beginning-of-line 2)
  5990. (point)))))
  5991. (when beg
  5992. (goto-char beg)
  5993. (while (re-search-forward re end t)
  5994. (unless (get-char-property (match-end 1) 'invisible)
  5995. (setq e (match-end 1))
  5996. (if (< org-cycle-separator-lines 0)
  5997. (setq b (save-excursion
  5998. (goto-char (match-beginning 0))
  5999. (org-back-over-empty-lines)
  6000. (if (save-excursion
  6001. (goto-char (max (point-min) (1- (point))))
  6002. (org-at-heading-p))
  6003. (1- (point))
  6004. (point))))
  6005. (setq b (match-beginning 1)))
  6006. (outline-flag-region b e nil)))))))
  6007. ;; Never hide empty lines at the end of the file.
  6008. (save-excursion
  6009. (goto-char (point-max))
  6010. (outline-previous-heading)
  6011. (outline-end-of-heading)
  6012. (if (and (looking-at "[ \t\n]+")
  6013. (= (match-end 0) (point-max)))
  6014. (outline-flag-region (point) (match-end 0) nil))))
  6015. (defun org-show-empty-lines-in-parent ()
  6016. "Move to the parent and re-show empty lines before visible headlines."
  6017. (save-excursion
  6018. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6019. (org-cycle-show-empty-lines context))))
  6020. (defun org-files-list ()
  6021. "Return `org-agenda-files' list, plus all open org-mode files.
  6022. This is useful for operations that need to scan all of a user's
  6023. open and agenda-wise Org files."
  6024. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6025. (dolist (buf (buffer-list))
  6026. (with-current-buffer buf
  6027. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6028. (let ((file (expand-file-name (buffer-file-name))))
  6029. (unless (member file files)
  6030. (push file files))))))
  6031. files))
  6032. (defsubst org-entry-beginning-position ()
  6033. "Return the beginning position of the current entry."
  6034. (save-excursion (outline-back-to-heading t) (point)))
  6035. (defsubst org-entry-end-position ()
  6036. "Return the end position of the current entry."
  6037. (save-excursion (outline-next-heading) (point)))
  6038. (defun org-cycle-hide-drawers (state)
  6039. "Re-hide all drawers after a visibility state change."
  6040. (when (and (derived-mode-p 'org-mode)
  6041. (not (memq state '(overview folded contents))))
  6042. (save-excursion
  6043. (let* ((globalp (memq state '(contents all)))
  6044. (beg (if globalp (point-min) (point)))
  6045. (end (if globalp (point-max)
  6046. (if (eq state 'children)
  6047. (save-excursion (outline-next-heading) (point))
  6048. (org-end-of-subtree t)))))
  6049. (goto-char beg)
  6050. (while (re-search-forward org-drawer-regexp end t)
  6051. (org-flag-drawer t))))))
  6052. (defun org-cycle-hide-inline-tasks (state)
  6053. "Re-hide inline task when switching to 'contents visibility state."
  6054. (when (and (eq state 'contents)
  6055. (boundp 'org-inlinetask-min-level)
  6056. org-inlinetask-min-level)
  6057. (hide-sublevels (1- org-inlinetask-min-level))))
  6058. (defun org-flag-drawer (flag)
  6059. "When FLAG is non-nil, hide the drawer we are within.
  6060. Otherwise make it visible."
  6061. (save-excursion
  6062. (beginning-of-line 1)
  6063. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6064. (let ((b (match-end 0)))
  6065. (if (re-search-forward
  6066. "^[ \t]*:END:"
  6067. (save-excursion (outline-next-heading) (point)) t)
  6068. (outline-flag-region b (point-at-eol) flag)
  6069. (error ":END: line missing at position %s" b))))))
  6070. (defun org-subtree-end-visible-p ()
  6071. "Is the end of the current subtree visible?"
  6072. (pos-visible-in-window-p
  6073. (save-excursion (org-end-of-subtree t) (point))))
  6074. (defun org-first-headline-recenter (&optional N)
  6075. "Move cursor to the first headline and recenter the headline.
  6076. Optional argument N means put the headline into the Nth line of the window."
  6077. (goto-char (point-min))
  6078. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6079. (beginning-of-line)
  6080. (recenter (prefix-numeric-value N))))
  6081. ;;; Saving and restoring visibility
  6082. (defun org-outline-overlay-data (&optional use-markers)
  6083. "Return a list of the locations of all outline overlays.
  6084. These are overlays with the `invisible' property value `outline'.
  6085. The return value is a list of cons cells, with start and stop
  6086. positions for each overlay.
  6087. If USE-MARKERS is set, return the positions as markers."
  6088. (let (beg end)
  6089. (save-excursion
  6090. (save-restriction
  6091. (widen)
  6092. (delq nil
  6093. (mapcar (lambda (o)
  6094. (when (eq (overlay-get o 'invisible) 'outline)
  6095. (setq beg (overlay-start o)
  6096. end (overlay-end o))
  6097. (and beg end (> end beg)
  6098. (if use-markers
  6099. (cons (move-marker (make-marker) beg)
  6100. (move-marker (make-marker) end))
  6101. (cons beg end)))))
  6102. (overlays-in (point-min) (point-max))))))))
  6103. (defun org-set-outline-overlay-data (data)
  6104. "Create visibility overlays for all positions in DATA.
  6105. DATA should have been made by `org-outline-overlay-data'."
  6106. (let (o)
  6107. (save-excursion
  6108. (save-restriction
  6109. (widen)
  6110. (show-all)
  6111. (mapc (lambda (c)
  6112. (outline-flag-region (car c) (cdr c) t))
  6113. data)))))
  6114. ;;; Folding of blocks
  6115. (defvar org-hide-block-overlays nil
  6116. "Overlays hiding blocks.")
  6117. (make-variable-buffer-local 'org-hide-block-overlays)
  6118. (defun org-block-map (function &optional start end)
  6119. "Call FUNCTION at the head of all source blocks in the current buffer.
  6120. Optional arguments START and END can be used to limit the range."
  6121. (let ((start (or start (point-min)))
  6122. (end (or end (point-max))))
  6123. (save-excursion
  6124. (goto-char start)
  6125. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6126. (save-excursion
  6127. (save-match-data
  6128. (goto-char (match-beginning 0))
  6129. (funcall function)))))))
  6130. (defun org-hide-block-toggle-all ()
  6131. "Toggle the visibility of all blocks in the current buffer."
  6132. (org-block-map #'org-hide-block-toggle))
  6133. (defun org-hide-block-all ()
  6134. "Fold all blocks in the current buffer."
  6135. (interactive)
  6136. (org-show-block-all)
  6137. (org-block-map #'org-hide-block-toggle-maybe))
  6138. (defun org-show-block-all ()
  6139. "Unfold all blocks in the current buffer."
  6140. (interactive)
  6141. (mapc 'delete-overlay org-hide-block-overlays)
  6142. (setq org-hide-block-overlays nil))
  6143. (defun org-hide-block-toggle-maybe ()
  6144. "Toggle visibility of block at point."
  6145. (interactive)
  6146. (let ((case-fold-search t))
  6147. (if (save-excursion
  6148. (beginning-of-line 1)
  6149. (looking-at org-block-regexp))
  6150. (progn (org-hide-block-toggle)
  6151. t) ;; to signal that we took action
  6152. nil))) ;; to signal that we did not
  6153. (defun org-hide-block-toggle (&optional force)
  6154. "Toggle the visibility of the current block."
  6155. (interactive)
  6156. (save-excursion
  6157. (beginning-of-line)
  6158. (if (re-search-forward org-block-regexp nil t)
  6159. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6160. (end (match-end 0)) ;; end of entire body
  6161. ov)
  6162. (if (memq t (mapcar (lambda (overlay)
  6163. (eq (overlay-get overlay 'invisible)
  6164. 'org-hide-block))
  6165. (overlays-at start)))
  6166. (if (or (not force) (eq force 'off))
  6167. (mapc (lambda (ov)
  6168. (when (member ov org-hide-block-overlays)
  6169. (setq org-hide-block-overlays
  6170. (delq ov org-hide-block-overlays)))
  6171. (when (eq (overlay-get ov 'invisible)
  6172. 'org-hide-block)
  6173. (delete-overlay ov)))
  6174. (overlays-at start)))
  6175. (setq ov (make-overlay start end))
  6176. (overlay-put ov 'invisible 'org-hide-block)
  6177. ;; make the block accessible to isearch
  6178. (overlay-put
  6179. ov 'isearch-open-invisible
  6180. (lambda (ov)
  6181. (when (member ov org-hide-block-overlays)
  6182. (setq org-hide-block-overlays
  6183. (delq ov org-hide-block-overlays)))
  6184. (when (eq (overlay-get ov 'invisible)
  6185. 'org-hide-block)
  6186. (delete-overlay ov))))
  6187. (push ov org-hide-block-overlays)))
  6188. (error "Not looking at a source block"))))
  6189. ;; org-tab-after-check-for-cycling-hook
  6190. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6191. ;; Remove overlays when changing major mode
  6192. (add-hook 'org-mode-hook
  6193. (lambda () (org-add-hook 'change-major-mode-hook
  6194. 'org-show-block-all 'append 'local)))
  6195. ;;; Org-goto
  6196. (defvar org-goto-window-configuration nil)
  6197. (defvar org-goto-marker nil)
  6198. (defvar org-goto-map)
  6199. (defun org-goto-map ()
  6200. "Set the keymap `org-goto'."
  6201. (setq org-goto-map
  6202. (let ((map (make-sparse-keymap)))
  6203. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6204. mouse-drag-region universal-argument org-occur))
  6205. cmd)
  6206. (while (setq cmd (pop cmds))
  6207. (substitute-key-definition cmd cmd map global-map)))
  6208. (suppress-keymap map)
  6209. (org-defkey map "\C-m" 'org-goto-ret)
  6210. (org-defkey map [(return)] 'org-goto-ret)
  6211. (org-defkey map [(left)] 'org-goto-left)
  6212. (org-defkey map [(right)] 'org-goto-right)
  6213. (org-defkey map [(control ?g)] 'org-goto-quit)
  6214. (org-defkey map "\C-i" 'org-cycle)
  6215. (org-defkey map [(tab)] 'org-cycle)
  6216. (org-defkey map [(down)] 'outline-next-visible-heading)
  6217. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6218. (if org-goto-auto-isearch
  6219. (if (fboundp 'define-key-after)
  6220. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6221. nil)
  6222. (org-defkey map "q" 'org-goto-quit)
  6223. (org-defkey map "n" 'outline-next-visible-heading)
  6224. (org-defkey map "p" 'outline-previous-visible-heading)
  6225. (org-defkey map "f" 'outline-forward-same-level)
  6226. (org-defkey map "b" 'outline-backward-same-level)
  6227. (org-defkey map "u" 'outline-up-heading))
  6228. (org-defkey map "/" 'org-occur)
  6229. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6230. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6231. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6232. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6233. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6234. map)))
  6235. (defconst org-goto-help
  6236. "Browse buffer copy, to find location or copy text.%s
  6237. RET=jump to location C-g=quit and return to previous location
  6238. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6239. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6240. ;; FIXME: Docstring does not mention both interfaces
  6241. (defun org-goto (&optional alternative-interface)
  6242. "Look up a different location in the current file, keeping current visibility.
  6243. When you want look-up or go to a different location in a
  6244. document, the fastest way is often to fold the entire buffer and
  6245. then dive into the tree. This method has the disadvantage, that
  6246. the previous location will be folded, which may not be what you
  6247. want.
  6248. This command works around this by showing a copy of the current
  6249. buffer in an indirect buffer, in overview mode. You can dive
  6250. into the tree in that copy, use org-occur and incremental search
  6251. to find a location. When pressing RET or `Q', the command
  6252. returns to the original buffer in which the visibility is still
  6253. unchanged. After RET it will also jump to the location selected
  6254. in the indirect buffer and expose the headline hierarchy above.
  6255. With a prefix argument, use the alternative interface: e.g. if
  6256. `org-goto-interface' is 'outline use 'outline-path-completion."
  6257. (interactive "P")
  6258. (org-goto-map)
  6259. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6260. (org-refile-use-outline-path t)
  6261. (org-refile-target-verify-function nil)
  6262. (interface
  6263. (if (not alternative-interface)
  6264. org-goto-interface
  6265. (if (eq org-goto-interface 'outline)
  6266. 'outline-path-completion
  6267. 'outline)))
  6268. (org-goto-start-pos (point))
  6269. (selected-point
  6270. (if (eq interface 'outline)
  6271. (car (org-get-location (current-buffer) org-goto-help))
  6272. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6273. (org-refile-check-position pa)
  6274. (nth 3 pa)))))
  6275. (if selected-point
  6276. (progn
  6277. (org-mark-ring-push org-goto-start-pos)
  6278. (goto-char selected-point)
  6279. (if (or (outline-invisible-p) (org-invisible-p2))
  6280. (org-show-context 'org-goto)))
  6281. (message "Quit"))))
  6282. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6283. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6284. (defvar org-goto-local-auto-isearch-map) ; defined below
  6285. (defun org-get-location (buf help)
  6286. "Let the user select a location in the Org-mode buffer BUF.
  6287. This function uses a recursive edit. It returns the selected position
  6288. or nil."
  6289. (org-no-popups
  6290. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6291. (isearch-hide-immediately nil)
  6292. (isearch-search-fun-function
  6293. (lambda () 'org-goto-local-search-headings))
  6294. (org-goto-selected-point org-goto-exit-command))
  6295. (save-excursion
  6296. (save-window-excursion
  6297. (delete-other-windows)
  6298. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6299. (org-pop-to-buffer-same-window
  6300. (condition-case nil
  6301. (make-indirect-buffer (current-buffer) "*org-goto*")
  6302. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6303. (with-output-to-temp-buffer "*Org Help*"
  6304. (princ (format help (if org-goto-auto-isearch
  6305. " Just type for auto-isearch."
  6306. " n/p/f/b/u to navigate, q to quit."))))
  6307. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6308. (setq buffer-read-only nil)
  6309. (let ((org-startup-truncated t)
  6310. (org-startup-folded nil)
  6311. (org-startup-align-all-tables nil))
  6312. (org-mode)
  6313. (org-overview))
  6314. (setq buffer-read-only t)
  6315. (if (and (boundp 'org-goto-start-pos)
  6316. (integer-or-marker-p org-goto-start-pos))
  6317. (let ((org-show-hierarchy-above t)
  6318. (org-show-siblings t)
  6319. (org-show-following-heading t))
  6320. (goto-char org-goto-start-pos)
  6321. (and (outline-invisible-p) (org-show-context)))
  6322. (goto-char (point-min)))
  6323. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6324. (message "Select location and press RET")
  6325. (use-local-map org-goto-map)
  6326. (recursive-edit)))
  6327. (kill-buffer "*org-goto*")
  6328. (cons org-goto-selected-point org-goto-exit-command))))
  6329. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6330. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6331. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6332. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6333. (defun org-goto-local-search-headings (string bound noerror)
  6334. "Search and make sure that any matches are in headlines."
  6335. (catch 'return
  6336. (while (if isearch-forward
  6337. (search-forward string bound noerror)
  6338. (search-backward string bound noerror))
  6339. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6340. (and (member :headline context)
  6341. (not (member :tags context))))
  6342. (throw 'return (point))))))
  6343. (defun org-goto-local-auto-isearch ()
  6344. "Start isearch."
  6345. (interactive)
  6346. (goto-char (point-min))
  6347. (let ((keys (this-command-keys)))
  6348. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6349. (isearch-mode t)
  6350. (isearch-process-search-char (string-to-char keys)))))
  6351. (defun org-goto-ret (&optional arg)
  6352. "Finish `org-goto' by going to the new location."
  6353. (interactive "P")
  6354. (setq org-goto-selected-point (point)
  6355. org-goto-exit-command 'return)
  6356. (throw 'exit nil))
  6357. (defun org-goto-left ()
  6358. "Finish `org-goto' by going to the new location."
  6359. (interactive)
  6360. (if (org-at-heading-p)
  6361. (progn
  6362. (beginning-of-line 1)
  6363. (setq org-goto-selected-point (point)
  6364. org-goto-exit-command 'left)
  6365. (throw 'exit nil))
  6366. (error "Not on a heading")))
  6367. (defun org-goto-right ()
  6368. "Finish `org-goto' by going to the new location."
  6369. (interactive)
  6370. (if (org-at-heading-p)
  6371. (progn
  6372. (setq org-goto-selected-point (point)
  6373. org-goto-exit-command 'right)
  6374. (throw 'exit nil))
  6375. (error "Not on a heading")))
  6376. (defun org-goto-quit ()
  6377. "Finish `org-goto' without cursor motion."
  6378. (interactive)
  6379. (setq org-goto-selected-point nil)
  6380. (setq org-goto-exit-command 'quit)
  6381. (throw 'exit nil))
  6382. ;;; Indirect buffer display of subtrees
  6383. (defvar org-indirect-dedicated-frame nil
  6384. "This is the frame being used for indirect tree display.")
  6385. (defvar org-last-indirect-buffer nil)
  6386. (defun org-tree-to-indirect-buffer (&optional arg)
  6387. "Create indirect buffer and narrow it to current subtree.
  6388. With a numerical prefix ARG, go up to this level and then take that tree.
  6389. If ARG is negative, go up that many levels.
  6390. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6391. indirect buffer previously made with this command, to avoid proliferation of
  6392. indirect buffers. However, when you call the command with a \
  6393. \\[universal-argument] prefix, or
  6394. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6395. is kept so that you can work with several indirect buffers at the same time.
  6396. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6397. \\[universal-argument] prefix also
  6398. requests that a new frame be made for the new buffer, so that the dedicated
  6399. frame is not changed."
  6400. (interactive "P")
  6401. (let ((cbuf (current-buffer))
  6402. (cwin (selected-window))
  6403. (pos (point))
  6404. beg end level heading ibuf)
  6405. (save-excursion
  6406. (org-back-to-heading t)
  6407. (when (numberp arg)
  6408. (setq level (org-outline-level))
  6409. (if (< arg 0) (setq arg (+ level arg)))
  6410. (while (> (setq level (org-outline-level)) arg)
  6411. (org-up-heading-safe)))
  6412. (setq beg (point)
  6413. heading (org-get-heading))
  6414. (org-end-of-subtree t t)
  6415. (if (org-at-heading-p) (backward-char 1))
  6416. (setq end (point)))
  6417. (if (and (buffer-live-p org-last-indirect-buffer)
  6418. (not (eq org-indirect-buffer-display 'new-frame))
  6419. (not arg))
  6420. (kill-buffer org-last-indirect-buffer))
  6421. (setq ibuf (org-get-indirect-buffer cbuf)
  6422. org-last-indirect-buffer ibuf)
  6423. (cond
  6424. ((or (eq org-indirect-buffer-display 'new-frame)
  6425. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6426. (select-frame (make-frame))
  6427. (delete-other-windows)
  6428. (org-pop-to-buffer-same-window ibuf)
  6429. (org-set-frame-title heading))
  6430. ((eq org-indirect-buffer-display 'dedicated-frame)
  6431. (raise-frame
  6432. (select-frame (or (and org-indirect-dedicated-frame
  6433. (frame-live-p org-indirect-dedicated-frame)
  6434. org-indirect-dedicated-frame)
  6435. (setq org-indirect-dedicated-frame (make-frame)))))
  6436. (delete-other-windows)
  6437. (org-pop-to-buffer-same-window ibuf)
  6438. (org-set-frame-title (concat "Indirect: " heading)))
  6439. ((eq org-indirect-buffer-display 'current-window)
  6440. (org-pop-to-buffer-same-window ibuf))
  6441. ((eq org-indirect-buffer-display 'other-window)
  6442. (pop-to-buffer ibuf))
  6443. (t (error "Invalid value")))
  6444. (if (featurep 'xemacs)
  6445. (save-excursion (org-mode) (turn-on-font-lock)))
  6446. (narrow-to-region beg end)
  6447. (show-all)
  6448. (goto-char pos)
  6449. (run-hook-with-args 'org-cycle-hook 'all)
  6450. (and (window-live-p cwin) (select-window cwin))))
  6451. (defun org-get-indirect-buffer (&optional buffer)
  6452. (setq buffer (or buffer (current-buffer)))
  6453. (let ((n 1) (base (buffer-name buffer)) bname)
  6454. (while (buffer-live-p
  6455. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6456. (setq n (1+ n)))
  6457. (condition-case nil
  6458. (make-indirect-buffer buffer bname 'clone)
  6459. (error (make-indirect-buffer buffer bname)))))
  6460. (defun org-set-frame-title (title)
  6461. "Set the title of the current frame to the string TITLE."
  6462. ;; FIXME: how to name a single frame in XEmacs???
  6463. (unless (featurep 'xemacs)
  6464. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6465. ;;;; Structure editing
  6466. ;;; Inserting headlines
  6467. (defun org-previous-line-empty-p ()
  6468. (save-excursion
  6469. (and (not (bobp))
  6470. (or (beginning-of-line 0) t)
  6471. (save-match-data
  6472. (looking-at "[ \t]*$")))))
  6473. (defun org-insert-heading (&optional force-heading invisible-ok)
  6474. "Insert a new heading or item with same depth at point.
  6475. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6476. If point is at the beginning of a headline, insert a sibling before the
  6477. current headline. If point is not at the beginning, split the line,
  6478. create the new headline with the text in the current line after point
  6479. \(but see also the variable `org-M-RET-may-split-line').
  6480. With a double prefix arg, force the heading to be inserted at the
  6481. end of the parent subtree.
  6482. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6483. This is important for non-interactive uses of the command."
  6484. (interactive "P")
  6485. (if (or (= (buffer-size) 0)
  6486. (and (not (save-excursion
  6487. (and (ignore-errors (org-back-to-heading invisible-ok))
  6488. (org-at-heading-p))))
  6489. (or force-heading (not (org-in-item-p)))))
  6490. (progn
  6491. (insert "\n* ")
  6492. (run-hooks 'org-insert-heading-hook))
  6493. (when (or force-heading (not (org-insert-item)))
  6494. (let* ((empty-line-p nil)
  6495. (level nil)
  6496. (on-heading (org-at-heading-p))
  6497. (head (save-excursion
  6498. (condition-case nil
  6499. (progn
  6500. (org-back-to-heading invisible-ok)
  6501. (when (and (not on-heading)
  6502. (featurep 'org-inlinetask)
  6503. (integerp org-inlinetask-min-level)
  6504. (>= (length (match-string 0))
  6505. org-inlinetask-min-level))
  6506. ;; Find a heading level before the inline task
  6507. (while (and (setq level (org-up-heading-safe))
  6508. (>= level org-inlinetask-min-level)))
  6509. (if (org-at-heading-p)
  6510. (org-back-to-heading invisible-ok)
  6511. (error "This should not happen")))
  6512. (setq empty-line-p (org-previous-line-empty-p))
  6513. (match-string 0))
  6514. (error "*"))))
  6515. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6516. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6517. pos hide-previous previous-pos)
  6518. (cond
  6519. ((and (org-at-heading-p) (bolp)
  6520. (or (bobp)
  6521. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6522. ;; insert before the current line
  6523. (open-line (if blank 2 1)))
  6524. ((and (bolp)
  6525. (not org-insert-heading-respect-content)
  6526. (or (bobp)
  6527. (save-excursion
  6528. (backward-char 1) (not (outline-invisible-p)))))
  6529. ;; insert right here
  6530. nil)
  6531. (t
  6532. ;; somewhere in the line
  6533. (save-excursion
  6534. (setq previous-pos (point-at-bol))
  6535. (end-of-line)
  6536. (setq hide-previous (outline-invisible-p)))
  6537. (and org-insert-heading-respect-content (org-show-subtree))
  6538. (let ((split
  6539. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6540. (save-excursion
  6541. (let ((p (point)))
  6542. (goto-char (point-at-bol))
  6543. (and (looking-at org-complex-heading-regexp)
  6544. (match-beginning 4)
  6545. (> p (match-beginning 4)))))))
  6546. tags pos)
  6547. (cond
  6548. (org-insert-heading-respect-content
  6549. (if (not (equal force-heading '(16)))
  6550. (org-end-of-subtree nil t)
  6551. (org-up-heading-safe)
  6552. (org-end-of-subtree nil t))
  6553. (when (featurep 'org-inlinetask)
  6554. (while (and (not (eobp))
  6555. (looking-at "\\(\\*+\\)[ \t]+")
  6556. (>= (length (match-string 1))
  6557. org-inlinetask-min-level))
  6558. (org-end-of-subtree nil t)))
  6559. (or (bolp) (newline))
  6560. (or (org-previous-line-empty-p)
  6561. (and blank (newline)))
  6562. (open-line 1))
  6563. ((org-at-heading-p)
  6564. (when hide-previous
  6565. (show-children)
  6566. (org-show-entry))
  6567. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6568. (setq tags (and (match-end 2) (match-string 2)))
  6569. (and (match-end 1)
  6570. (delete-region (match-beginning 1) (match-end 1)))
  6571. (setq pos (point-at-bol))
  6572. (or split (end-of-line 1))
  6573. (delete-horizontal-space)
  6574. (if (string-match "\\`\\*+\\'"
  6575. (buffer-substring (point-at-bol) (point)))
  6576. (insert " "))
  6577. (newline (if blank 2 1))
  6578. (when tags
  6579. (save-excursion
  6580. (goto-char pos)
  6581. (end-of-line 1)
  6582. (insert " " tags)
  6583. (org-set-tags nil 'align))))
  6584. (t
  6585. (or split (end-of-line 1))
  6586. (newline (if blank 2 1)))))))
  6587. (insert head) (just-one-space)
  6588. (setq pos (point))
  6589. (end-of-line 1)
  6590. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6591. (when (and org-insert-heading-respect-content hide-previous)
  6592. (save-excursion
  6593. (goto-char previous-pos)
  6594. (hide-subtree)))
  6595. (run-hooks 'org-insert-heading-hook)))))
  6596. (defun org-get-heading (&optional no-tags no-todo)
  6597. "Return the heading of the current entry, without the stars.
  6598. When NO-TAGS is non-nil, don't include tags.
  6599. When NO-TODO is non-nil, don't include TODO keywords."
  6600. (save-excursion
  6601. (org-back-to-heading t)
  6602. (cond
  6603. ((and no-tags no-todo)
  6604. (looking-at org-complex-heading-regexp)
  6605. (match-string 4))
  6606. (no-tags
  6607. (looking-at (concat org-outline-regexp
  6608. "\\(.*?\\)"
  6609. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6610. (match-string 1))
  6611. (no-todo
  6612. (looking-at org-todo-line-regexp)
  6613. (match-string 3))
  6614. (t (looking-at org-heading-regexp)
  6615. (match-string 2)))))
  6616. (defun org-heading-components ()
  6617. "Return the components of the current heading.
  6618. This is a list with the following elements:
  6619. - the level as an integer
  6620. - the reduced level, different if `org-odd-levels-only' is set.
  6621. - the TODO keyword, or nil
  6622. - the priority character, like ?A, or nil if no priority is given
  6623. - the headline text itself, or the tags string if no headline text
  6624. - the tags string, or nil."
  6625. (save-excursion
  6626. (org-back-to-heading t)
  6627. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6628. (list (length (match-string 1))
  6629. (org-reduced-level (length (match-string 1)))
  6630. (org-match-string-no-properties 2)
  6631. (and (match-end 3) (aref (match-string 3) 2))
  6632. (org-match-string-no-properties 4)
  6633. (org-match-string-no-properties 5)))))
  6634. (defun org-get-entry ()
  6635. "Get the entry text, after heading, entire subtree."
  6636. (save-excursion
  6637. (org-back-to-heading t)
  6638. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6639. (defun org-insert-heading-after-current ()
  6640. "Insert a new heading with same level as current, after current subtree."
  6641. (interactive)
  6642. (org-back-to-heading)
  6643. (org-insert-heading)
  6644. (org-move-subtree-down)
  6645. (end-of-line 1))
  6646. (defun org-insert-heading-respect-content ()
  6647. (interactive)
  6648. (let ((org-insert-heading-respect-content t))
  6649. (org-insert-heading t)))
  6650. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6651. (interactive "P")
  6652. (let ((org-insert-heading-respect-content t))
  6653. (org-insert-todo-heading force-state t)))
  6654. (defun org-insert-todo-heading (arg &optional force-heading)
  6655. "Insert a new heading with the same level and TODO state as current heading.
  6656. If the heading has no TODO state, or if the state is DONE, use the first
  6657. state (TODO by default). Also one prefix arg, force first state. With two
  6658. prefix args, force inserting at the end of the parent subtree."
  6659. (interactive "P")
  6660. (when (or force-heading (not (org-insert-item 'checkbox)))
  6661. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6662. force-heading))
  6663. (save-excursion
  6664. (org-back-to-heading)
  6665. (outline-previous-heading)
  6666. (looking-at org-todo-line-regexp))
  6667. (let*
  6668. ((new-mark-x
  6669. (if (or arg
  6670. (not (match-beginning 2))
  6671. (member (match-string 2) org-done-keywords))
  6672. (car org-todo-keywords-1)
  6673. (match-string 2)))
  6674. (new-mark
  6675. (or
  6676. (run-hook-with-args-until-success
  6677. 'org-todo-get-default-hook new-mark-x nil)
  6678. new-mark-x)))
  6679. (beginning-of-line 1)
  6680. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6681. (if org-treat-insert-todo-heading-as-state-change
  6682. (org-todo new-mark)
  6683. (insert new-mark " "))))
  6684. (when org-provide-todo-statistics
  6685. (org-update-parent-todo-statistics))))
  6686. (defun org-insert-subheading (arg)
  6687. "Insert a new subheading and demote it.
  6688. Works for outline headings and for plain lists alike."
  6689. (interactive "P")
  6690. (org-insert-heading arg)
  6691. (cond
  6692. ((org-at-heading-p) (org-do-demote))
  6693. ((org-at-item-p) (org-indent-item))))
  6694. (defun org-insert-todo-subheading (arg)
  6695. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6696. Works for outline headings and for plain lists alike."
  6697. (interactive "P")
  6698. (org-insert-todo-heading arg)
  6699. (cond
  6700. ((org-at-heading-p) (org-do-demote))
  6701. ((org-at-item-p) (org-indent-item))))
  6702. ;;; Promotion and Demotion
  6703. (defvar org-after-demote-entry-hook nil
  6704. "Hook run after an entry has been demoted.
  6705. The cursor will be at the beginning of the entry.
  6706. When a subtree is being demoted, the hook will be called for each node.")
  6707. (defvar org-after-promote-entry-hook nil
  6708. "Hook run after an entry has been promoted.
  6709. The cursor will be at the beginning of the entry.
  6710. When a subtree is being promoted, the hook will be called for each node.")
  6711. (defun org-promote-subtree ()
  6712. "Promote the entire subtree.
  6713. See also `org-promote'."
  6714. (interactive)
  6715. (save-excursion
  6716. (org-with-limited-levels (org-map-tree 'org-promote)))
  6717. (org-fix-position-after-promote))
  6718. (defun org-demote-subtree ()
  6719. "Demote the entire subtree. See `org-demote'.
  6720. See also `org-promote'."
  6721. (interactive)
  6722. (save-excursion
  6723. (org-with-limited-levels (org-map-tree 'org-demote)))
  6724. (org-fix-position-after-promote))
  6725. (defun org-do-promote ()
  6726. "Promote the current heading higher up the tree.
  6727. If the region is active in `transient-mark-mode', promote all headings
  6728. in the region."
  6729. (interactive)
  6730. (save-excursion
  6731. (if (org-region-active-p)
  6732. (org-map-region 'org-promote (region-beginning) (region-end))
  6733. (org-promote)))
  6734. (org-fix-position-after-promote))
  6735. (defun org-do-demote ()
  6736. "Demote the current heading lower down the tree.
  6737. If the region is active in `transient-mark-mode', demote all headings
  6738. in the region."
  6739. (interactive)
  6740. (save-excursion
  6741. (if (org-region-active-p)
  6742. (org-map-region 'org-demote (region-beginning) (region-end))
  6743. (org-demote)))
  6744. (org-fix-position-after-promote))
  6745. (defun org-fix-position-after-promote ()
  6746. "Make sure that after pro/demotion cursor position is right."
  6747. (let ((pos (point)))
  6748. (when (save-excursion
  6749. (beginning-of-line 1)
  6750. (looking-at org-todo-line-regexp)
  6751. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6752. (cond ((eobp) (insert " "))
  6753. ((eolp) (insert " "))
  6754. ((equal (char-after) ?\ ) (forward-char 1))))))
  6755. (defun org-current-level ()
  6756. "Return the level of the current entry, or nil if before the first headline.
  6757. The level is the number of stars at the beginning of the headline."
  6758. (save-excursion
  6759. (org-with-limited-levels
  6760. (if (ignore-errors (org-back-to-heading t))
  6761. (funcall outline-level)))))
  6762. (defun org-get-previous-line-level ()
  6763. "Return the outline depth of the last headline before the current line.
  6764. Returns 0 for the first headline in the buffer, and nil if before the
  6765. first headline."
  6766. (let ((current-level (org-current-level))
  6767. (prev-level (when (> (line-number-at-pos) 1)
  6768. (save-excursion
  6769. (beginning-of-line 0)
  6770. (org-current-level)))))
  6771. (cond ((null current-level) nil) ; Before first headline
  6772. ((null prev-level) 0) ; At first headline
  6773. (prev-level))))
  6774. (defun org-reduced-level (l)
  6775. "Compute the effective level of a heading.
  6776. This takes into account the setting of `org-odd-levels-only'."
  6777. (cond
  6778. ((zerop l) 0)
  6779. (org-odd-levels-only (1+ (floor (/ l 2))))
  6780. (t l)))
  6781. (defun org-level-increment ()
  6782. "Return the number of stars that will be added or removed at a
  6783. time to headlines when structure editing, based on the value of
  6784. `org-odd-levels-only'."
  6785. (if org-odd-levels-only 2 1))
  6786. (defun org-get-valid-level (level &optional change)
  6787. "Rectify a level change under the influence of `org-odd-levels-only'
  6788. LEVEL is a current level, CHANGE is by how much the level should be
  6789. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6790. even level numbers will become the next higher odd number."
  6791. (if org-odd-levels-only
  6792. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6793. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6794. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6795. (max 1 (+ level (or change 0)))))
  6796. (if (boundp 'define-obsolete-function-alias)
  6797. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6798. (define-obsolete-function-alias 'org-get-legal-level
  6799. 'org-get-valid-level)
  6800. (define-obsolete-function-alias 'org-get-legal-level
  6801. 'org-get-valid-level "23.1")))
  6802. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6803. ;; ̀org-with-limited-levels'
  6804. (defun org-promote ()
  6805. "Promote the current heading higher up the tree.
  6806. If the region is active in `transient-mark-mode', promote all headings
  6807. in the region."
  6808. (org-back-to-heading t)
  6809. (let* ((level (save-match-data (funcall outline-level)))
  6810. (after-change-functions (remove 'flyspell-after-change-function
  6811. after-change-functions))
  6812. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6813. (diff (abs (- level (length up-head) -1))))
  6814. (cond ((and (= level 1) org-called-with-limited-levels
  6815. org-allow-promoting-top-level-subtree)
  6816. (replace-match "# " nil t))
  6817. ((= level 1)
  6818. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6819. (t (replace-match up-head nil t)))
  6820. ;; Fixup tag positioning
  6821. (unless (= level 1)
  6822. (and org-auto-align-tags (org-set-tags nil t))
  6823. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6824. (run-hooks 'org-after-promote-entry-hook)))
  6825. (defun org-demote ()
  6826. "Demote the current heading lower down the tree.
  6827. If the region is active in `transient-mark-mode', demote all headings
  6828. in the region."
  6829. (org-back-to-heading t)
  6830. (let* ((level (save-match-data (funcall outline-level)))
  6831. (after-change-functions (remove 'flyspell-after-change-function
  6832. after-change-functions))
  6833. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6834. (diff (abs (- level (length down-head) -1))))
  6835. (replace-match down-head nil t)
  6836. ;; Fixup tag positioning
  6837. (and org-auto-align-tags (org-set-tags nil t))
  6838. (if org-adapt-indentation (org-fixup-indentation diff))
  6839. (run-hooks 'org-after-demote-entry-hook)))
  6840. (defun org-cycle-level ()
  6841. "Cycle the level of an empty headline through possible states.
  6842. This goes first to child, then to parent, level, then up the hierarchy.
  6843. After top level, it switches back to sibling level."
  6844. (interactive)
  6845. (let ((org-adapt-indentation nil))
  6846. (when (org-point-at-end-of-empty-headline)
  6847. (setq this-command 'org-cycle-level) ; Only needed for caching
  6848. (let ((cur-level (org-current-level))
  6849. (prev-level (org-get-previous-line-level)))
  6850. (cond
  6851. ;; If first headline in file, promote to top-level.
  6852. ((= prev-level 0)
  6853. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6854. do (org-do-promote)))
  6855. ;; If same level as prev, demote one.
  6856. ((= prev-level cur-level)
  6857. (org-do-demote))
  6858. ;; If parent is top-level, promote to top level if not already.
  6859. ((= prev-level 1)
  6860. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6861. do (org-do-promote)))
  6862. ;; If top-level, return to prev-level.
  6863. ((= cur-level 1)
  6864. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6865. do (org-do-demote)))
  6866. ;; If less than prev-level, promote one.
  6867. ((< cur-level prev-level)
  6868. (org-do-promote))
  6869. ;; If deeper than prev-level, promote until higher than
  6870. ;; prev-level.
  6871. ((> cur-level prev-level)
  6872. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6873. do (org-do-promote))))
  6874. t))))
  6875. (defun org-map-tree (fun)
  6876. "Call FUN for every heading underneath the current one."
  6877. (org-back-to-heading)
  6878. (let ((level (funcall outline-level)))
  6879. (save-excursion
  6880. (funcall fun)
  6881. (while (and (progn
  6882. (outline-next-heading)
  6883. (> (funcall outline-level) level))
  6884. (not (eobp)))
  6885. (funcall fun)))))
  6886. (defun org-map-region (fun beg end)
  6887. "Call FUN for every heading between BEG and END."
  6888. (let ((org-ignore-region t))
  6889. (save-excursion
  6890. (setq end (copy-marker end))
  6891. (goto-char beg)
  6892. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6893. (< (point) end))
  6894. (funcall fun))
  6895. (while (and (progn
  6896. (outline-next-heading)
  6897. (< (point) end))
  6898. (not (eobp)))
  6899. (funcall fun)))))
  6900. (defvar org-property-end-re) ; silence byte-compiler
  6901. (defun org-fixup-indentation (diff)
  6902. "Change the indentation in the current entry by DIFF.
  6903. However, if any line in the current entry has no indentation, or if it
  6904. would end up with no indentation after the change, nothing at all is done."
  6905. (save-excursion
  6906. (let ((end (save-excursion (outline-next-heading)
  6907. (point-marker)))
  6908. (prohibit (if (> diff 0)
  6909. "^\\S-"
  6910. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6911. col)
  6912. (unless (save-excursion (end-of-line 1)
  6913. (re-search-forward prohibit end t))
  6914. (while (and (< (point) end)
  6915. (re-search-forward "^[ \t]+" end t))
  6916. (goto-char (match-end 0))
  6917. (setq col (current-column))
  6918. (if (< diff 0) (replace-match ""))
  6919. (org-indent-to-column (+ diff col))))
  6920. (move-marker end nil))))
  6921. (defun org-convert-to-odd-levels ()
  6922. "Convert an org-mode file with all levels allowed to one with odd levels.
  6923. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6924. level 5 etc."
  6925. (interactive)
  6926. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6927. (let ((outline-level 'org-outline-level)
  6928. (org-odd-levels-only nil) n)
  6929. (save-excursion
  6930. (goto-char (point-min))
  6931. (while (re-search-forward "^\\*\\*+ " nil t)
  6932. (setq n (- (length (match-string 0)) 2))
  6933. (while (>= (setq n (1- n)) 0)
  6934. (org-demote))
  6935. (end-of-line 1))))))
  6936. (defun org-convert-to-oddeven-levels ()
  6937. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6938. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6939. file contains a section with an even level, conversion would
  6940. destroy the structure of the file. An error is signaled in this
  6941. case."
  6942. (interactive)
  6943. (goto-char (point-min))
  6944. ;; First check if there are no even levels
  6945. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6946. (org-show-context t)
  6947. (error "Not all levels are odd in this file. Conversion not possible"))
  6948. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6949. (let ((outline-regexp org-outline-regexp)
  6950. (outline-level 'org-outline-level)
  6951. (org-odd-levels-only nil) n)
  6952. (save-excursion
  6953. (goto-char (point-min))
  6954. (while (re-search-forward "^\\*\\*+ " nil t)
  6955. (setq n (/ (1- (length (match-string 0))) 2))
  6956. (while (>= (setq n (1- n)) 0)
  6957. (org-promote))
  6958. (end-of-line 1))))))
  6959. (defun org-tr-level (n)
  6960. "Make N odd if required."
  6961. (if org-odd-levels-only (1+ (/ n 2)) n))
  6962. ;;; Vertical tree motion, cutting and pasting of subtrees
  6963. (defun org-move-subtree-up (&optional arg)
  6964. "Move the current subtree up past ARG headlines of the same level."
  6965. (interactive "p")
  6966. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6967. (defun org-move-subtree-down (&optional arg)
  6968. "Move the current subtree down past ARG headlines of the same level."
  6969. (interactive "p")
  6970. (setq arg (prefix-numeric-value arg))
  6971. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6972. 'org-get-last-sibling))
  6973. (ins-point (make-marker))
  6974. (cnt (abs arg))
  6975. (col (current-column))
  6976. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6977. ;; Select the tree
  6978. (org-back-to-heading)
  6979. (setq beg0 (point))
  6980. (save-excursion
  6981. (setq ne-beg (org-back-over-empty-lines))
  6982. (setq beg (point)))
  6983. (save-match-data
  6984. (save-excursion (outline-end-of-heading)
  6985. (setq folded (outline-invisible-p)))
  6986. (outline-end-of-subtree))
  6987. (outline-next-heading)
  6988. (setq ne-end (org-back-over-empty-lines))
  6989. (setq end (point))
  6990. (goto-char beg0)
  6991. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6992. ;; include less whitespace
  6993. (save-excursion
  6994. (goto-char beg)
  6995. (forward-line (- ne-beg ne-end))
  6996. (setq beg (point))))
  6997. ;; Find insertion point, with error handling
  6998. (while (> cnt 0)
  6999. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7000. (progn (goto-char beg0)
  7001. (error "Cannot move past superior level or buffer limit")))
  7002. (setq cnt (1- cnt)))
  7003. (if (> arg 0)
  7004. ;; Moving forward - still need to move over subtree
  7005. (progn (org-end-of-subtree t t)
  7006. (save-excursion
  7007. (org-back-over-empty-lines)
  7008. (or (bolp) (newline)))))
  7009. (setq ne-ins (org-back-over-empty-lines))
  7010. (move-marker ins-point (point))
  7011. (setq txt (buffer-substring beg end))
  7012. (org-save-markers-in-region beg end)
  7013. (delete-region beg end)
  7014. (org-remove-empty-overlays-at beg)
  7015. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7016. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7017. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7018. (let ((bbb (point)))
  7019. (insert-before-markers txt)
  7020. (org-reinstall-markers-in-region bbb)
  7021. (move-marker ins-point bbb))
  7022. (or (bolp) (insert "\n"))
  7023. (setq ins-end (point))
  7024. (goto-char ins-point)
  7025. (org-skip-whitespace)
  7026. (when (and (< arg 0)
  7027. (org-first-sibling-p)
  7028. (> ne-ins ne-beg))
  7029. ;; Move whitespace back to beginning
  7030. (save-excursion
  7031. (goto-char ins-end)
  7032. (let ((kill-whole-line t))
  7033. (kill-line (- ne-ins ne-beg)) (point)))
  7034. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7035. (move-marker ins-point nil)
  7036. (if folded
  7037. (hide-subtree)
  7038. (org-show-entry)
  7039. (show-children)
  7040. (org-cycle-hide-drawers 'children))
  7041. (org-clean-visibility-after-subtree-move)
  7042. ;; move back to the initial column we were at
  7043. (move-to-column col)))
  7044. (defvar org-subtree-clip ""
  7045. "Clipboard for cut and paste of subtrees.
  7046. This is actually only a copy of the kill, because we use the normal kill
  7047. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7048. (defvar org-subtree-clip-folded nil
  7049. "Was the last copied subtree folded?
  7050. This is used to fold the tree back after pasting.")
  7051. (defun org-cut-subtree (&optional n)
  7052. "Cut the current subtree into the clipboard.
  7053. With prefix arg N, cut this many sequential subtrees.
  7054. This is a short-hand for marking the subtree and then cutting it."
  7055. (interactive "p")
  7056. (org-copy-subtree n 'cut))
  7057. (defun org-copy-subtree (&optional n cut force-store-markers)
  7058. "Cut the current subtree into the clipboard.
  7059. With prefix arg N, cut this many sequential subtrees.
  7060. This is a short-hand for marking the subtree and then copying it.
  7061. If CUT is non-nil, actually cut the subtree.
  7062. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7063. of some markers in the region, even if CUT is non-nil. This is
  7064. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7065. (interactive "p")
  7066. (let (beg end folded (beg0 (point)))
  7067. (if (org-called-interactively-p 'any)
  7068. (org-back-to-heading nil) ; take what looks like a subtree
  7069. (org-back-to-heading t)) ; take what is really there
  7070. (setq beg (point))
  7071. (skip-chars-forward " \t\r\n")
  7072. (save-match-data
  7073. (save-excursion (outline-end-of-heading)
  7074. (setq folded (outline-invisible-p)))
  7075. (condition-case nil
  7076. (org-forward-heading-same-level (1- n) t)
  7077. (error nil))
  7078. (org-end-of-subtree t t))
  7079. (setq end (point))
  7080. (goto-char beg0)
  7081. (when (> end beg)
  7082. (setq org-subtree-clip-folded folded)
  7083. (when (or cut force-store-markers)
  7084. (org-save-markers-in-region beg end))
  7085. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7086. (setq org-subtree-clip (current-kill 0))
  7087. (message "%s: Subtree(s) with %d characters"
  7088. (if cut "Cut" "Copied")
  7089. (length org-subtree-clip)))))
  7090. (defun org-paste-subtree (&optional level tree for-yank)
  7091. "Paste the clipboard as a subtree, with modification of headline level.
  7092. The entire subtree is promoted or demoted in order to match a new headline
  7093. level.
  7094. If the cursor is at the beginning of a headline, the same level as
  7095. that headline is used to paste the tree
  7096. If not, the new level is derived from the *visible* headings
  7097. before and after the insertion point, and taken to be the inferior headline
  7098. level of the two. So if the previous visible heading is level 3 and the
  7099. next is level 4 (or vice versa), level 4 will be used for insertion.
  7100. This makes sure that the subtree remains an independent subtree and does
  7101. not swallow low level entries.
  7102. You can also force a different level, either by using a numeric prefix
  7103. argument, or by inserting the heading marker by hand. For example, if the
  7104. cursor is after \"*****\", then the tree will be shifted to level 5.
  7105. If optional TREE is given, use this text instead of the kill ring.
  7106. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7107. move back over whitespace before inserting, and move point to the end of
  7108. the inserted text when done."
  7109. (interactive "P")
  7110. (setq tree (or tree (and kill-ring (current-kill 0))))
  7111. (unless (org-kill-is-subtree-p tree)
  7112. (error "%s"
  7113. (substitute-command-keys
  7114. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7115. (org-with-limited-levels
  7116. (let* ((visp (not (outline-invisible-p)))
  7117. (txt tree)
  7118. (^re_ "\\(\\*+\\)[ \t]*")
  7119. (old-level (if (string-match org-outline-regexp-bol txt)
  7120. (- (match-end 0) (match-beginning 0) 1)
  7121. -1))
  7122. (force-level (cond (level (prefix-numeric-value level))
  7123. ((and (looking-at "[ \t]*$")
  7124. (string-match
  7125. "^\\*+$" (buffer-substring
  7126. (point-at-bol) (point))))
  7127. (- (match-end 1) (match-beginning 1)))
  7128. ((and (bolp)
  7129. (looking-at org-outline-regexp))
  7130. (- (match-end 0) (point) 1))))
  7131. (previous-level (save-excursion
  7132. (condition-case nil
  7133. (progn
  7134. (outline-previous-visible-heading 1)
  7135. (if (looking-at ^re_)
  7136. (- (match-end 0) (match-beginning 0) 1)
  7137. 1))
  7138. (error 1))))
  7139. (next-level (save-excursion
  7140. (condition-case nil
  7141. (progn
  7142. (or (looking-at org-outline-regexp)
  7143. (outline-next-visible-heading 1))
  7144. (if (looking-at ^re_)
  7145. (- (match-end 0) (match-beginning 0) 1)
  7146. 1))
  7147. (error 1))))
  7148. (new-level (or force-level (max previous-level next-level)))
  7149. (shift (if (or (= old-level -1)
  7150. (= new-level -1)
  7151. (= old-level new-level))
  7152. 0
  7153. (- new-level old-level)))
  7154. (delta (if (> shift 0) -1 1))
  7155. (func (if (> shift 0) 'org-demote 'org-promote))
  7156. (org-odd-levels-only nil)
  7157. beg end newend)
  7158. ;; Remove the forced level indicator
  7159. (if force-level
  7160. (delete-region (point-at-bol) (point)))
  7161. ;; Paste
  7162. (beginning-of-line (if (bolp) 1 2))
  7163. (setq beg (point))
  7164. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7165. (insert-before-markers txt)
  7166. (unless (string-match "\n\\'" txt) (insert "\n"))
  7167. (setq newend (point))
  7168. (org-reinstall-markers-in-region beg)
  7169. (setq end (point))
  7170. (goto-char beg)
  7171. (skip-chars-forward " \t\n\r")
  7172. (setq beg (point))
  7173. (if (and (outline-invisible-p) visp)
  7174. (save-excursion (outline-show-heading)))
  7175. ;; Shift if necessary
  7176. (unless (= shift 0)
  7177. (save-restriction
  7178. (narrow-to-region beg end)
  7179. (while (not (= shift 0))
  7180. (org-map-region func (point-min) (point-max))
  7181. (setq shift (+ delta shift)))
  7182. (goto-char (point-min))
  7183. (setq newend (point-max))))
  7184. (when (or (org-called-interactively-p 'interactive) for-yank)
  7185. (message "Clipboard pasted as level %d subtree" new-level))
  7186. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7187. kill-ring
  7188. (eq org-subtree-clip (current-kill 0))
  7189. org-subtree-clip-folded)
  7190. ;; The tree was folded before it was killed/copied
  7191. (hide-subtree))
  7192. (and for-yank (goto-char newend)))))
  7193. (defun org-kill-is-subtree-p (&optional txt)
  7194. "Check if the current kill is an outline subtree, or a set of trees.
  7195. Returns nil if kill does not start with a headline, or if the first
  7196. headline level is not the largest headline level in the tree.
  7197. So this will actually accept several entries of equal levels as well,
  7198. which is OK for `org-paste-subtree'.
  7199. If optional TXT is given, check this string instead of the current kill."
  7200. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7201. (re (org-get-limited-outline-regexp))
  7202. (^re (concat "^" re))
  7203. (start-level (and kill
  7204. (string-match
  7205. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7206. kill)
  7207. (- (match-end 2) (match-beginning 2) 1)))
  7208. (start (1+ (or (match-beginning 2) -1))))
  7209. (if (not start-level)
  7210. (progn
  7211. nil) ;; does not even start with a heading
  7212. (catch 'exit
  7213. (while (setq start (string-match ^re kill (1+ start)))
  7214. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7215. (throw 'exit nil)))
  7216. t))))
  7217. (defvar org-markers-to-move nil
  7218. "Markers that should be moved with a cut-and-paste operation.
  7219. Those markers are stored together with their positions relative to
  7220. the start of the region.")
  7221. (defun org-save-markers-in-region (beg end)
  7222. "Check markers in region.
  7223. If these markers are between BEG and END, record their position relative
  7224. to BEG, so that after moving the block of text, we can put the markers back
  7225. into place.
  7226. This function gets called just before an entry or tree gets cut from the
  7227. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7228. called immediately, to move the markers with the entries."
  7229. (setq org-markers-to-move nil)
  7230. (when (featurep 'org-clock)
  7231. (org-clock-save-markers-for-cut-and-paste beg end))
  7232. (when (featurep 'org-agenda)
  7233. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7234. (defun org-check-and-save-marker (marker beg end)
  7235. "Check if MARKER is between BEG and END.
  7236. If yes, remember the marker and the distance to BEG."
  7237. (when (and (marker-buffer marker)
  7238. (equal (marker-buffer marker) (current-buffer)))
  7239. (if (and (>= marker beg) (< marker end))
  7240. (push (cons marker (- marker beg)) org-markers-to-move))))
  7241. (defun org-reinstall-markers-in-region (beg)
  7242. "Move all remembered markers to their position relative to BEG."
  7243. (mapc (lambda (x)
  7244. (move-marker (car x) (+ beg (cdr x))))
  7245. org-markers-to-move)
  7246. (setq org-markers-to-move nil))
  7247. (defun org-narrow-to-subtree ()
  7248. "Narrow buffer to the current subtree."
  7249. (interactive)
  7250. (save-excursion
  7251. (save-match-data
  7252. (org-with-limited-levels
  7253. (narrow-to-region
  7254. (progn (org-back-to-heading t) (point))
  7255. (progn (org-end-of-subtree t t)
  7256. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7257. (point)))))))
  7258. (defun org-narrow-to-block ()
  7259. "Narrow buffer to the current block."
  7260. (interactive)
  7261. (let* ((case-fold-search t)
  7262. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7263. "^[ \t]*#\\+end_.*")))
  7264. (if blockp
  7265. (narrow-to-region (car blockp) (cdr blockp))
  7266. (error "Not in a block"))))
  7267. (eval-when-compile
  7268. (defvar org-property-drawer-re))
  7269. (defvar org-property-start-re) ;; defined below
  7270. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7271. "Clone the task (subtree) at point N times.
  7272. The clones will be inserted as siblings.
  7273. In interactive use, the user will be prompted for the number of
  7274. clones to be produced, and for a time SHIFT, which may be a
  7275. repeater as used in time stamps, for example `+3d'.
  7276. When a valid repeater is given and the entry contains any time
  7277. stamps, the clones will become a sequence in time, with time
  7278. stamps in the subtree shifted for each clone produced. If SHIFT
  7279. is nil or the empty string, time stamps will be left alone. The
  7280. ID property of the original subtree is removed.
  7281. If the original subtree did contain time stamps with a repeater,
  7282. the following will happen:
  7283. - the repeater will be removed in each clone
  7284. - an additional clone will be produced, with the current, unshifted
  7285. date(s) in the entry.
  7286. - the original entry will be placed *after* all the clones, with
  7287. repeater intact.
  7288. - the start days in the repeater in the original entry will be shifted
  7289. to past the last clone.
  7290. In this way you can spell out a number of instances of a repeating task,
  7291. and still retain the repeater to cover future instances of the task."
  7292. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7293. (let (beg end template task idprop
  7294. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7295. (drawer-re org-drawer-regexp))
  7296. (if (not (and (integerp n) (> n 0)))
  7297. (error "Invalid number of replications %s" n))
  7298. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7299. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7300. shift)))
  7301. (error "Invalid shift specification %s" shift))
  7302. (when doshift
  7303. (setq shift-n (string-to-number (match-string 1 shift))
  7304. shift-what (cdr (assoc (match-string 2 shift)
  7305. '(("d" . day) ("w" . week)
  7306. ("m" . month) ("y" . year))))))
  7307. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7308. (setq nmin 1 nmax n)
  7309. (org-back-to-heading t)
  7310. (setq beg (point))
  7311. (setq idprop (org-entry-get nil "ID"))
  7312. (org-end-of-subtree t t)
  7313. (or (bolp) (insert "\n"))
  7314. (setq end (point))
  7315. (setq template (buffer-substring beg end))
  7316. (when (and doshift
  7317. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7318. (delete-region beg end)
  7319. (setq end beg)
  7320. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7321. (goto-char end)
  7322. (loop for n from nmin to nmax do
  7323. ;; prepare clone
  7324. (with-temp-buffer
  7325. (insert template)
  7326. (org-mode)
  7327. (goto-char (point-min))
  7328. (org-show-subtree)
  7329. (and idprop (if org-clone-delete-id
  7330. (org-entry-delete nil "ID")
  7331. (org-id-get-create t)))
  7332. (unless (= n 0)
  7333. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7334. (kill-whole-line))
  7335. (goto-char (point-min))
  7336. (while (re-search-forward drawer-re nil t)
  7337. (mapc (lambda (d)
  7338. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7339. (goto-char (point-min))
  7340. (when doshift
  7341. (while (re-search-forward org-ts-regexp-both nil t)
  7342. (org-timestamp-change (* n shift-n) shift-what))
  7343. (unless (= n n-no-remove)
  7344. (goto-char (point-min))
  7345. (while (re-search-forward org-ts-regexp nil t)
  7346. (save-excursion
  7347. (goto-char (match-beginning 0))
  7348. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7349. (delete-region (match-beginning 1) (match-end 1)))))))
  7350. (setq task (buffer-string)))
  7351. (insert task))
  7352. (goto-char beg)))
  7353. ;;; Outline Sorting
  7354. (defun org-sort (with-case)
  7355. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7356. Optional argument WITH-CASE means sort case-sensitively."
  7357. (interactive "P")
  7358. (cond
  7359. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7360. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7361. (t
  7362. (org-call-with-arg 'org-sort-entries with-case))))
  7363. (defun org-sort-remove-invisible (s)
  7364. (remove-text-properties 0 (length s) org-rm-props s)
  7365. (while (string-match org-bracket-link-regexp s)
  7366. (setq s (replace-match (if (match-end 2)
  7367. (match-string 3 s)
  7368. (match-string 1 s)) t t s)))
  7369. s)
  7370. (defvar org-priority-regexp) ; defined later in the file
  7371. (defvar org-after-sorting-entries-or-items-hook nil
  7372. "Hook that is run after a bunch of entries or items have been sorted.
  7373. When children are sorted, the cursor is in the parent line when this
  7374. hook gets called. When a region or a plain list is sorted, the cursor
  7375. will be in the first entry of the sorted region/list.")
  7376. (defun org-sort-entries
  7377. (&optional with-case sorting-type getkey-func compare-func property)
  7378. "Sort entries on a certain level of an outline tree.
  7379. If there is an active region, the entries in the region are sorted.
  7380. Else, if the cursor is before the first entry, sort the top-level items.
  7381. Else, the children of the entry at point are sorted.
  7382. Sorting can be alphabetically, numerically, by date/time as given by
  7383. a time stamp, by a property or by priority.
  7384. The command prompts for the sorting type unless it has been given to the
  7385. function through the SORTING-TYPE argument, which needs to be a character,
  7386. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7387. precise meaning of each character:
  7388. n Numerically, by converting the beginning of the entry/item to a number.
  7389. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7390. o By order of TODO keywords.
  7391. t By date/time, either the first active time stamp in the entry, or, if
  7392. none exist, by the first inactive one.
  7393. s By the scheduled date/time.
  7394. d By deadline date/time.
  7395. c By creation time, which is assumed to be the first inactive time stamp
  7396. at the beginning of a line.
  7397. p By priority according to the cookie.
  7398. r By the value of a property.
  7399. Capital letters will reverse the sort order.
  7400. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7401. called with point at the beginning of the record. It must return either
  7402. a string or a number that should serve as the sorting key for that record.
  7403. Comparing entries ignores case by default. However, with an optional argument
  7404. WITH-CASE, the sorting considers case as well."
  7405. (interactive "P")
  7406. (let ((case-func (if with-case 'identity 'downcase))
  7407. (cmstr
  7408. ;; The clock marker is lost when using `sort-subr', let's
  7409. ;; store the clocking string.
  7410. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7411. (save-excursion
  7412. (goto-char org-clock-marker)
  7413. (looking-back "^.*") (match-string-no-properties 0))))
  7414. start beg end stars re re2
  7415. txt what tmp)
  7416. ;; Find beginning and end of region to sort
  7417. (cond
  7418. ((org-region-active-p)
  7419. ;; we will sort the region
  7420. (setq end (region-end)
  7421. what "region")
  7422. (goto-char (region-beginning))
  7423. (if (not (org-at-heading-p)) (outline-next-heading))
  7424. (setq start (point)))
  7425. ((or (org-at-heading-p)
  7426. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7427. ;; we will sort the children of the current headline
  7428. (org-back-to-heading)
  7429. (setq start (point)
  7430. end (progn (org-end-of-subtree t t)
  7431. (or (bolp) (insert "\n"))
  7432. (org-back-over-empty-lines)
  7433. (point))
  7434. what "children")
  7435. (goto-char start)
  7436. (show-subtree)
  7437. (outline-next-heading))
  7438. (t
  7439. ;; we will sort the top-level entries in this file
  7440. (goto-char (point-min))
  7441. (or (org-at-heading-p) (outline-next-heading))
  7442. (setq start (point))
  7443. (goto-char (point-max))
  7444. (beginning-of-line 1)
  7445. (when (looking-at ".*?\\S-")
  7446. ;; File ends in a non-white line
  7447. (end-of-line 1)
  7448. (insert "\n"))
  7449. (setq end (point-max))
  7450. (setq what "top-level")
  7451. (goto-char start)
  7452. (show-all)))
  7453. (setq beg (point))
  7454. (if (>= beg end) (error "Nothing to sort"))
  7455. (looking-at "\\(\\*+\\)")
  7456. (setq stars (match-string 1)
  7457. re (concat "^" (regexp-quote stars) " +")
  7458. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7459. txt (buffer-substring beg end))
  7460. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7461. (if (and (not (equal stars "*")) (string-match re2 txt))
  7462. (error "Region to sort contains a level above the first entry"))
  7463. (unless sorting-type
  7464. (message
  7465. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7466. [t]ime [s]cheduled [d]eadline [c]reated
  7467. A/N/P/R/O/F/T/S/D/C means reversed:"
  7468. what)
  7469. (setq sorting-type (read-char-exclusive))
  7470. (unless getkey-func
  7471. (and (= (downcase sorting-type) ?f)
  7472. (setq getkey-func
  7473. (org-icompleting-read "Sort using function: "
  7474. obarray 'fboundp t nil nil))
  7475. (setq getkey-func (intern getkey-func))))
  7476. (and (= (downcase sorting-type) ?r)
  7477. (not property)
  7478. (setq property
  7479. (org-icompleting-read "Property: "
  7480. (mapcar 'list (org-buffer-property-keys t))
  7481. nil t))))
  7482. (message "Sorting entries...")
  7483. (save-restriction
  7484. (narrow-to-region start end)
  7485. (let ((dcst (downcase sorting-type))
  7486. (case-fold-search nil)
  7487. (now (current-time)))
  7488. (sort-subr
  7489. (/= dcst sorting-type)
  7490. ;; This function moves to the beginning character of the "record" to
  7491. ;; be sorted.
  7492. (lambda nil
  7493. (if (re-search-forward re nil t)
  7494. (goto-char (match-beginning 0))
  7495. (goto-char (point-max))))
  7496. ;; This function moves to the last character of the "record" being
  7497. ;; sorted.
  7498. (lambda nil
  7499. (save-match-data
  7500. (condition-case nil
  7501. (outline-forward-same-level 1)
  7502. (error
  7503. (goto-char (point-max))))))
  7504. ;; This function returns the value that gets sorted against.
  7505. (lambda nil
  7506. (cond
  7507. ((= dcst ?n)
  7508. (if (looking-at org-complex-heading-regexp)
  7509. (string-to-number (match-string 4))
  7510. nil))
  7511. ((= dcst ?a)
  7512. (if (looking-at org-complex-heading-regexp)
  7513. (funcall case-func (match-string 4))
  7514. nil))
  7515. ((= dcst ?t)
  7516. (let ((end (save-excursion (outline-next-heading) (point))))
  7517. (if (or (re-search-forward org-ts-regexp end t)
  7518. (re-search-forward org-ts-regexp-both end t))
  7519. (org-time-string-to-seconds (match-string 0))
  7520. (org-float-time now))))
  7521. ((= dcst ?c)
  7522. (let ((end (save-excursion (outline-next-heading) (point))))
  7523. (if (re-search-forward
  7524. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7525. end t)
  7526. (org-time-string-to-seconds (match-string 0))
  7527. (org-float-time now))))
  7528. ((= dcst ?s)
  7529. (let ((end (save-excursion (outline-next-heading) (point))))
  7530. (if (re-search-forward org-scheduled-time-regexp end t)
  7531. (org-time-string-to-seconds (match-string 1))
  7532. (org-float-time now))))
  7533. ((= dcst ?d)
  7534. (let ((end (save-excursion (outline-next-heading) (point))))
  7535. (if (re-search-forward org-deadline-time-regexp end t)
  7536. (org-time-string-to-seconds (match-string 1))
  7537. (org-float-time now))))
  7538. ((= dcst ?p)
  7539. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7540. (string-to-char (match-string 2))
  7541. org-default-priority))
  7542. ((= dcst ?r)
  7543. (or (org-entry-get nil property) ""))
  7544. ((= dcst ?o)
  7545. (if (looking-at org-complex-heading-regexp)
  7546. (- 9999 (length (member (match-string 2)
  7547. org-todo-keywords-1)))))
  7548. ((= dcst ?f)
  7549. (if getkey-func
  7550. (progn
  7551. (setq tmp (funcall getkey-func))
  7552. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7553. tmp)
  7554. (error "Invalid key function `%s'" getkey-func)))
  7555. (t (error "Invalid sorting type `%c'" sorting-type))))
  7556. nil
  7557. (cond
  7558. ((= dcst ?a) 'string<)
  7559. ((= dcst ?f) compare-func)
  7560. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7561. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7562. ;; Reset the clock marker if needed
  7563. (when cmstr
  7564. (save-excursion
  7565. (goto-char start)
  7566. (search-forward cmstr nil t)
  7567. (move-marker org-clock-marker (point))))
  7568. (message "Sorting entries...done")))
  7569. (defun org-do-sort (table what &optional with-case sorting-type)
  7570. "Sort TABLE of WHAT according to SORTING-TYPE.
  7571. The user will be prompted for the SORTING-TYPE if the call to this
  7572. function does not specify it. WHAT is only for the prompt, to indicate
  7573. what is being sorted. The sorting key will be extracted from
  7574. the car of the elements of the table.
  7575. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7576. (unless sorting-type
  7577. (message
  7578. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7579. what)
  7580. (setq sorting-type (read-char-exclusive)))
  7581. (let ((dcst (downcase sorting-type))
  7582. extractfun comparefun)
  7583. ;; Define the appropriate functions
  7584. (cond
  7585. ((= dcst ?n)
  7586. (setq extractfun 'string-to-number
  7587. comparefun (if (= dcst sorting-type) '< '>)))
  7588. ((= dcst ?a)
  7589. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7590. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7591. comparefun (if (= dcst sorting-type)
  7592. 'string<
  7593. (lambda (a b) (and (not (string< a b))
  7594. (not (string= a b)))))))
  7595. ((= dcst ?t)
  7596. (setq extractfun
  7597. (lambda (x)
  7598. (if (or (string-match org-ts-regexp x)
  7599. (string-match org-ts-regexp-both x))
  7600. (org-float-time
  7601. (org-time-string-to-time (match-string 0 x)))
  7602. 0))
  7603. comparefun (if (= dcst sorting-type) '< '>)))
  7604. (t (error "Invalid sorting type `%c'" sorting-type)))
  7605. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7606. table)
  7607. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7608. ;;; The orgstruct minor mode
  7609. ;; Define a minor mode which can be used in other modes in order to
  7610. ;; integrate the org-mode structure editing commands.
  7611. ;; This is really a hack, because the org-mode structure commands use
  7612. ;; keys which normally belong to the major mode. Here is how it
  7613. ;; works: The minor mode defines all the keys necessary to operate the
  7614. ;; structure commands, but wraps the commands into a function which
  7615. ;; tests if the cursor is currently at a headline or a plain list
  7616. ;; item. If that is the case, the structure command is used,
  7617. ;; temporarily setting many Org-mode variables like regular
  7618. ;; expressions for filling etc. However, when any of those keys is
  7619. ;; used at a different location, function uses `key-binding' to look
  7620. ;; up if the key has an associated command in another currently active
  7621. ;; keymap (minor modes, major mode, global), and executes that
  7622. ;; command. There might be problems if any of the keys is otherwise
  7623. ;; used as a prefix key.
  7624. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7625. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7626. ;; addresses this by checking explicitly for both bindings.
  7627. (defvar orgstruct-mode-map (make-sparse-keymap)
  7628. "Keymap for the minor `orgstruct-mode'.")
  7629. (defvar org-local-vars nil
  7630. "List of local variables, for use by `orgstruct-mode'.")
  7631. ;;;###autoload
  7632. (define-minor-mode orgstruct-mode
  7633. "Toggle the minor mode `orgstruct-mode'.
  7634. This mode is for using Org-mode structure commands in other
  7635. modes. The following keys behave as if Org-mode were active, if
  7636. the cursor is on a headline, or on a plain list item (both as
  7637. defined by Org-mode).
  7638. M-up Move entry/item up
  7639. M-down Move entry/item down
  7640. M-left Promote
  7641. M-right Demote
  7642. M-S-up Move entry/item up
  7643. M-S-down Move entry/item down
  7644. M-S-left Promote subtree
  7645. M-S-right Demote subtree
  7646. M-q Fill paragraph and items like in Org-mode
  7647. C-c ^ Sort entries
  7648. C-c - Cycle list bullet
  7649. TAB Cycle item visibility
  7650. M-RET Insert new heading/item
  7651. S-M-RET Insert new TODO heading / Checkbox item
  7652. C-c C-c Set tags / toggle checkbox"
  7653. nil " OrgStruct" nil
  7654. (org-load-modules-maybe)
  7655. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7656. ;;;###autoload
  7657. (defun turn-on-orgstruct ()
  7658. "Unconditionally turn on `orgstruct-mode'."
  7659. (orgstruct-mode 1))
  7660. (defvar org-fb-vars nil)
  7661. (make-variable-buffer-local 'org-fb-vars)
  7662. (defun orgstruct++-mode (&optional arg)
  7663. "Toggle `orgstruct-mode', the enhanced version of it.
  7664. In addition to setting orgstruct-mode, this also exports all
  7665. indentation and autofilling variables from org-mode into the
  7666. buffer. It will also recognize item context in multiline items."
  7667. (interactive "P")
  7668. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7669. (if (< arg 1)
  7670. (progn (orgstruct-mode -1)
  7671. (mapc (lambda(v)
  7672. (org-set-local (car v)
  7673. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7674. org-fb-vars))
  7675. (orgstruct-mode 1)
  7676. (setq org-fb-vars nil)
  7677. (let (var val)
  7678. (mapc
  7679. (lambda (x)
  7680. (when (string-match
  7681. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7682. (symbol-name (car x)))
  7683. (setq var (car x) val (nth 1 x))
  7684. (push (list var `(quote ,(eval var))) org-fb-vars)
  7685. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7686. org-local-vars)
  7687. (org-set-local 'orgstruct-is-++ t))))
  7688. (defvar orgstruct-is-++ nil
  7689. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7690. (make-variable-buffer-local 'orgstruct-is-++)
  7691. ;;;###autoload
  7692. (defun turn-on-orgstruct++ ()
  7693. "Unconditionally turn on `orgstruct++-mode'."
  7694. (orgstruct++-mode 1))
  7695. (defun orgstruct-error ()
  7696. "Error when there is no default binding for a structure key."
  7697. (interactive)
  7698. (error "This key has no function outside structure elements"))
  7699. (defun orgstruct-setup ()
  7700. "Setup orgstruct keymaps."
  7701. (let ((nfunc 0)
  7702. (bindings
  7703. (list
  7704. '([(meta up)] org-metaup)
  7705. '([(meta down)] org-metadown)
  7706. '([(meta left)] org-metaleft)
  7707. '([(meta right)] org-metaright)
  7708. '([(meta shift up)] org-shiftmetaup)
  7709. '([(meta shift down)] org-shiftmetadown)
  7710. '([(meta shift left)] org-shiftmetaleft)
  7711. '([(meta shift right)] org-shiftmetaright)
  7712. '([?\e (up)] org-metaup)
  7713. '([?\e (down)] org-metadown)
  7714. '([?\e (left)] org-metaleft)
  7715. '([?\e (right)] org-metaright)
  7716. '([?\e (shift up)] org-shiftmetaup)
  7717. '([?\e (shift down)] org-shiftmetadown)
  7718. '([?\e (shift left)] org-shiftmetaleft)
  7719. '([?\e (shift right)] org-shiftmetaright)
  7720. '([(shift up)] org-shiftup)
  7721. '([(shift down)] org-shiftdown)
  7722. '([(shift left)] org-shiftleft)
  7723. '([(shift right)] org-shiftright)
  7724. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7725. '("\M-q" fill-paragraph)
  7726. '("\C-c^" org-sort)
  7727. '("\C-c-" org-cycle-list-bullet)))
  7728. elt key fun cmd)
  7729. (while (setq elt (pop bindings))
  7730. (setq nfunc (1+ nfunc))
  7731. (setq key (org-key (car elt))
  7732. fun (nth 1 elt)
  7733. cmd (orgstruct-make-binding fun nfunc key))
  7734. (org-defkey orgstruct-mode-map key cmd))
  7735. ;; Prevent an error for users who forgot to make autoloads
  7736. (require 'org-element)
  7737. ;; Special treatment needed for TAB and RET
  7738. (org-defkey orgstruct-mode-map [(tab)]
  7739. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7740. (org-defkey orgstruct-mode-map "\C-i"
  7741. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7742. (org-defkey orgstruct-mode-map "\M-\C-m"
  7743. (orgstruct-make-binding 'org-insert-heading 105
  7744. "\M-\C-m" [(meta return)]))
  7745. (org-defkey orgstruct-mode-map [(meta return)]
  7746. (orgstruct-make-binding 'org-insert-heading 106
  7747. [(meta return)] "\M-\C-m"))
  7748. (org-defkey orgstruct-mode-map [(shift meta return)]
  7749. (orgstruct-make-binding 'org-insert-todo-heading 107
  7750. [(meta return)] "\M-\C-m"))
  7751. (org-defkey orgstruct-mode-map "\e\C-m"
  7752. (orgstruct-make-binding 'org-insert-heading 108
  7753. "\e\C-m" [?\e (return)]))
  7754. (org-defkey orgstruct-mode-map [?\e (return)]
  7755. (orgstruct-make-binding 'org-insert-heading 109
  7756. [?\e (return)] "\e\C-m"))
  7757. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7758. (orgstruct-make-binding 'org-insert-todo-heading 110
  7759. [?\e (return)] "\e\C-m"))
  7760. (unless org-local-vars
  7761. (setq org-local-vars (org-get-local-variables)))
  7762. t))
  7763. (defun orgstruct-make-binding (fun n &rest keys)
  7764. "Create a function for binding in the structure minor mode.
  7765. FUN is the command to call inside a table. N is used to create a unique
  7766. command name. KEYS are keys that should be checked in for a command
  7767. to execute outside of tables."
  7768. (eval
  7769. (list 'defun
  7770. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7771. '(arg)
  7772. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7773. "Outside of structure, run the binding of `"
  7774. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7775. "'.")
  7776. '(interactive "p")
  7777. (list 'if
  7778. `(org-context-p 'headline 'item
  7779. (and orgstruct-is-++
  7780. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7781. 'item-body))
  7782. (list 'org-run-like-in-org-mode (list 'quote fun))
  7783. (list 'let '(orgstruct-mode)
  7784. (list 'call-interactively
  7785. (append '(or)
  7786. (mapcar (lambda (k)
  7787. (list 'key-binding k))
  7788. keys)
  7789. '('orgstruct-error))))))))
  7790. (defun org-contextualize-keys (alist contexts)
  7791. "Return valid elements in ALIST depending on CONTEXTS.
  7792. `org-agenda-custom-commands' or `org-capture-templates' are the
  7793. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7794. or `org-capture-templates-contexts' are the associated contexts
  7795. definitions."
  7796. (let ((contexts
  7797. ;; normalize contexts
  7798. (mapcar
  7799. (lambda(c) (cond ((listp (cadr c))
  7800. (list (car c) (car c) (cadr c)))
  7801. ((string= "" (cadr c))
  7802. (list (car c) (car c) (caddr c)))
  7803. (t c))) contexts))
  7804. (a alist) c r s)
  7805. ;; loop over all commands or templates
  7806. (while (setq c (pop a))
  7807. (let (vrules repl)
  7808. (cond
  7809. ((not (assoc (car c) contexts))
  7810. (push c r))
  7811. ((and (assoc (car c) contexts)
  7812. (setq vrules (org-contextualize-validate-key
  7813. (car c) contexts)))
  7814. (mapc (lambda (vr)
  7815. (when (not (equal (car vr) (cadr vr)))
  7816. (setq repl vr))) vrules)
  7817. (if (not repl) (push c r)
  7818. (push (cadr repl) s)
  7819. (push
  7820. (cons (car c)
  7821. (cdr (or (assoc (cadr repl) alist)
  7822. (error "Undefined key `%s' as contextual replacement for `%s'"
  7823. (cadr repl) (car c)))))
  7824. r))))))
  7825. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  7826. (delq
  7827. nil
  7828. (delete-dups
  7829. (mapcar (lambda (x)
  7830. (let ((tpl (car x)))
  7831. (when (not (delq
  7832. nil
  7833. (mapcar (lambda(y)
  7834. (equal y tpl)) s))) x)))
  7835. (reverse r))))))
  7836. (defun org-contextualize-validate-key (key contexts)
  7837. "Check CONTEXTS for agenda or capture KEY."
  7838. (let (r rr res)
  7839. (while (setq r (pop contexts))
  7840. (mapc
  7841. (lambda (rr)
  7842. (when
  7843. (and (equal key (car r))
  7844. (if (functionp rr) (funcall rr)
  7845. (or (and (eq (car rr) 'in-file)
  7846. (buffer-file-name)
  7847. (string-match (cdr rr) (buffer-file-name)))
  7848. (and (eq (car rr) 'in-mode)
  7849. (string-match (cdr rr) (symbol-name major-mode)))
  7850. (when (and (eq (car rr) 'not-in-file)
  7851. (buffer-file-name))
  7852. (not (string-match (cdr rr) (buffer-file-name))))
  7853. (when (eq (car rr) 'not-in-mode)
  7854. (not (string-match (cdr rr) (symbol-name major-mode)))))))
  7855. (push r res)))
  7856. (car (last r))))
  7857. (delete-dups (delq nil res))))
  7858. (defun org-context-p (&rest contexts)
  7859. "Check if local context is any of CONTEXTS.
  7860. Possible values in the list of contexts are `table', `headline', and `item'."
  7861. (let ((pos (point)))
  7862. (goto-char (point-at-bol))
  7863. (prog1 (or (and (memq 'table contexts)
  7864. (looking-at "[ \t]*|"))
  7865. (and (memq 'headline contexts)
  7866. (looking-at org-outline-regexp))
  7867. (and (memq 'item contexts)
  7868. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7869. (and (memq 'item-body contexts)
  7870. (org-in-item-p)))
  7871. (goto-char pos))))
  7872. (defun org-get-local-variables ()
  7873. "Return a list of all local variables in an Org mode buffer."
  7874. (let (varlist)
  7875. (with-current-buffer (get-buffer-create "*Org tmp*")
  7876. (erase-buffer)
  7877. (org-mode)
  7878. (setq varlist (buffer-local-variables)))
  7879. (kill-buffer "*Org tmp*")
  7880. (delq nil
  7881. (mapcar
  7882. (lambda (x)
  7883. (setq x
  7884. (if (symbolp x)
  7885. (list x)
  7886. (list (car x) (list 'quote (cdr x)))))
  7887. (if (string-match
  7888. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  7889. (symbol-name (car x)))
  7890. x nil))
  7891. varlist))))
  7892. (defun org-clone-local-variables (from-buffer &optional regexp)
  7893. "Clone local variables from FROM-BUFFER.
  7894. Optional argument REGEXP selects variables to clone."
  7895. (mapc
  7896. (lambda (pair)
  7897. (and (symbolp (car pair))
  7898. (or (null regexp)
  7899. (string-match regexp (symbol-name (car pair))))
  7900. (set (make-local-variable (car pair))
  7901. (cdr pair))))
  7902. (buffer-local-variables from-buffer)))
  7903. ;;;###autoload
  7904. (defun org-run-like-in-org-mode (cmd)
  7905. "Run a command, pretending that the current buffer is in Org-mode.
  7906. This will temporarily bind local variables that are typically bound in
  7907. Org-mode to the values they have in Org-mode, and then interactively
  7908. call CMD."
  7909. (org-load-modules-maybe)
  7910. (unless org-local-vars
  7911. (setq org-local-vars (org-get-local-variables)))
  7912. (eval (list 'let org-local-vars
  7913. (list 'call-interactively (list 'quote cmd)))))
  7914. ;;;; Archiving
  7915. (defun org-get-category (&optional pos force-refresh)
  7916. "Get the category applying to position POS."
  7917. (save-match-data
  7918. (if force-refresh (org-refresh-category-properties))
  7919. (let ((pos (or pos (point))))
  7920. (or (get-text-property pos 'org-category)
  7921. (progn (org-refresh-category-properties)
  7922. (get-text-property pos 'org-category))))))
  7923. (defun org-refresh-category-properties ()
  7924. "Refresh category text properties in the buffer."
  7925. (let ((case-fold-search t)
  7926. (inhibit-read-only t)
  7927. (def-cat (cond
  7928. ((null org-category)
  7929. (if buffer-file-name
  7930. (file-name-sans-extension
  7931. (file-name-nondirectory buffer-file-name))
  7932. "???"))
  7933. ((symbolp org-category) (symbol-name org-category))
  7934. (t org-category)))
  7935. beg end cat pos optionp)
  7936. (org-unmodified
  7937. (save-excursion
  7938. (save-restriction
  7939. (widen)
  7940. (goto-char (point-min))
  7941. (put-text-property (point) (point-max) 'org-category def-cat)
  7942. (while (re-search-forward
  7943. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7944. (setq pos (match-end 0)
  7945. optionp (equal (char-after (match-beginning 0)) ?#)
  7946. cat (org-trim (match-string 2)))
  7947. (if optionp
  7948. (setq beg (point-at-bol) end (point-max))
  7949. (org-back-to-heading t)
  7950. (setq beg (point) end (org-end-of-subtree t t)))
  7951. (put-text-property beg end 'org-category cat)
  7952. (put-text-property beg end 'org-category-position beg)
  7953. (goto-char pos)))))))
  7954. (defun org-refresh-properties (dprop tprop)
  7955. "Refresh buffer text properties.
  7956. DPROP is the drawer property and TPROP is the corresponding text
  7957. property to set."
  7958. (let ((case-fold-search t)
  7959. (inhibit-read-only t) p)
  7960. (org-unmodified
  7961. (save-excursion
  7962. (save-restriction
  7963. (widen)
  7964. (goto-char (point-min))
  7965. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  7966. (setq p (org-match-string-no-properties 1))
  7967. (save-excursion
  7968. (org-back-to-heading t)
  7969. (put-text-property
  7970. (point-at-bol) (point-at-eol) tprop p))))))))
  7971. ;;;; Link Stuff
  7972. ;;; Link abbreviations
  7973. (defun org-link-expand-abbrev (link)
  7974. "Apply replacements as defined in `org-link-abbrev-alist'."
  7975. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7976. (let* ((key (match-string 1 link))
  7977. (as (or (assoc key org-link-abbrev-alist-local)
  7978. (assoc key org-link-abbrev-alist)))
  7979. (tag (and (match-end 2) (match-string 3 link)))
  7980. rpl)
  7981. (if (not as)
  7982. link
  7983. (setq rpl (cdr as))
  7984. (cond
  7985. ((symbolp rpl) (funcall rpl tag))
  7986. ((string-match "%(\\([^)]+\\))" rpl)
  7987. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  7988. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7989. ((string-match "%h" rpl)
  7990. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7991. (t (concat rpl tag)))))
  7992. link))
  7993. ;;; Storing and inserting links
  7994. (defvar org-insert-link-history nil
  7995. "Minibuffer history for links inserted with `org-insert-link'.")
  7996. (defvar org-stored-links nil
  7997. "Contains the links stored with `org-store-link'.")
  7998. (defvar org-store-link-plist nil
  7999. "Plist with info about the most recently link created with `org-store-link'.")
  8000. (defvar org-link-protocols nil
  8001. "Link protocols added to Org-mode using `org-add-link-type'.")
  8002. (defvar org-store-link-functions nil
  8003. "List of functions that are called to create and store a link.
  8004. Each function will be called in turn until one returns a non-nil
  8005. value. Each function should check if it is responsible for creating
  8006. this link (for example by looking at the major mode).
  8007. If not, it must exit and return nil.
  8008. If yes, it should return a non-nil value after a calling
  8009. `org-store-link-props' with a list of properties and values.
  8010. Special properties are:
  8011. :type The link prefix, like \"http\". This must be given.
  8012. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8013. This is obligatory as well.
  8014. :description Optional default description for the second pair
  8015. of brackets in an Org-mode link. The user can still change
  8016. this when inserting this link into an Org-mode buffer.
  8017. In addition to these, any additional properties can be specified
  8018. and then used in capture templates.")
  8019. (defun org-add-link-type (type &optional follow export)
  8020. "Add TYPE to the list of `org-link-types'.
  8021. Re-compute all regular expressions depending on `org-link-types'
  8022. FOLLOW and EXPORT are two functions.
  8023. FOLLOW should take the link path as the single argument and do whatever
  8024. is necessary to follow the link, for example find a file or display
  8025. a mail message.
  8026. EXPORT should format the link path for export to one of the export formats.
  8027. It should be a function accepting three arguments:
  8028. path the path of the link, the text after the prefix (like \"http:\")
  8029. desc the description of the link, if any, or a description added by
  8030. org-export-normalize-links if there is none
  8031. format the export format, a symbol like `html' or `latex' or `ascii'..
  8032. The function may use the FORMAT information to return different values
  8033. depending on the format. The return value will be put literally into
  8034. the exported file. If the return value is nil, this means Org should
  8035. do what it normally does with links which do not have EXPORT defined.
  8036. Org-mode has a built-in default for exporting links. If you are happy with
  8037. this default, there is no need to define an export function for the link
  8038. type. For a simple example of an export function, see `org-bbdb.el'."
  8039. (add-to-list 'org-link-types type t)
  8040. (org-make-link-regexps)
  8041. (if (assoc type org-link-protocols)
  8042. (setcdr (assoc type org-link-protocols) (list follow export))
  8043. (push (list type follow export) org-link-protocols)))
  8044. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8045. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8046. ;;;###autoload
  8047. (defun org-store-link (arg)
  8048. "\\<org-mode-map>Store an org-link to the current location.
  8049. This link is added to `org-stored-links' and can later be inserted
  8050. into an org-buffer with \\[org-insert-link].
  8051. For some link types, a prefix arg is interpreted.
  8052. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8053. For file links, arg negates `org-context-in-file-links'.
  8054. A double prefix arg force skipping storing functions that are not
  8055. part of Org's core."
  8056. (interactive "P")
  8057. (org-load-modules-maybe)
  8058. (setq org-store-link-plist nil) ; reset
  8059. (org-with-limited-levels
  8060. (let (link cpltxt desc description search txt custom-id agenda-link sfuns sfunsn)
  8061. (cond
  8062. ((and (not (equal arg '(16)))
  8063. (setq sfuns
  8064. (delq
  8065. nil (mapcar (lambda (f) (let (fs) (if (funcall f) (push f fs))))
  8066. org-store-link-functions))
  8067. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8068. (or (and (cdr sfuns)
  8069. (funcall (intern
  8070. (completing-read "Which function for creating the link? "
  8071. sfunsn t (car sfunsn)))))
  8072. (funcall (caar sfuns)))
  8073. (setq link (plist-get org-store-link-plist :link)
  8074. desc (or (plist-get org-store-link-plist :description) link))))
  8075. ((org-src-edit-buffer-p)
  8076. (let (label gc)
  8077. (while (or (not label)
  8078. (save-excursion
  8079. (save-restriction
  8080. (widen)
  8081. (goto-char (point-min))
  8082. (re-search-forward
  8083. (regexp-quote (format org-coderef-label-format label))
  8084. nil t))))
  8085. (when label (message "Label exists already") (sit-for 2))
  8086. (setq label (read-string "Code line label: " label)))
  8087. (end-of-line 1)
  8088. (setq link (format org-coderef-label-format label))
  8089. (setq gc (- 79 (length link)))
  8090. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8091. (insert link)
  8092. (setq link (concat "(" label ")") desc nil)))
  8093. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8094. ;; We are in the agenda, link to referenced location
  8095. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8096. (get-text-property (point) 'org-marker))))
  8097. (when m
  8098. (org-with-point-at m
  8099. (setq agenda-link
  8100. (if (org-called-interactively-p 'any)
  8101. (call-interactively 'org-store-link)
  8102. (org-store-link nil)))))))
  8103. ((eq major-mode 'calendar-mode)
  8104. (let ((cd (calendar-cursor-to-date)))
  8105. (setq link
  8106. (format-time-string
  8107. (car org-time-stamp-formats)
  8108. (apply 'encode-time
  8109. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8110. nil nil nil))))
  8111. (org-store-link-props :type "calendar" :date cd)))
  8112. ((eq major-mode 'help-mode)
  8113. (setq link (concat "help:" (save-excursion
  8114. (goto-char (point-min))
  8115. (looking-at "^[^ ]+")
  8116. (match-string 0))))
  8117. (org-store-link-props :type "help"))
  8118. ((eq major-mode 'w3-mode)
  8119. (setq cpltxt (if (and (buffer-name)
  8120. (not (string-match "Untitled" (buffer-name))))
  8121. (buffer-name)
  8122. (url-view-url t))
  8123. link (url-view-url t))
  8124. (org-store-link-props :type "w3" :url (url-view-url t)))
  8125. ((setq search (run-hook-with-args-until-success
  8126. 'org-create-file-search-functions))
  8127. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8128. "::" search))
  8129. (setq cpltxt (or description link)))
  8130. ((eq major-mode 'image-mode)
  8131. (setq cpltxt (concat "file:"
  8132. (abbreviate-file-name buffer-file-name))
  8133. link cpltxt)
  8134. (org-store-link-props :type "image" :file buffer-file-name))
  8135. ((eq major-mode 'dired-mode)
  8136. ;; link to the file in the current line
  8137. (let ((file (dired-get-filename nil t)))
  8138. (setq file (if file
  8139. (abbreviate-file-name
  8140. (expand-file-name (dired-get-filename nil t)))
  8141. ;; otherwise, no file so use current directory.
  8142. default-directory))
  8143. (setq cpltxt (concat "file:" file)
  8144. link cpltxt)))
  8145. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8146. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8147. (cond
  8148. ((org-in-regexp "<<\\(.*?\\)>>")
  8149. (setq cpltxt
  8150. (concat "file:"
  8151. (abbreviate-file-name
  8152. (buffer-file-name (buffer-base-buffer)))
  8153. "::" (match-string 1))
  8154. link cpltxt))
  8155. ((and (featurep 'org-id)
  8156. (or (eq org-id-link-to-org-use-id t)
  8157. (and (org-called-interactively-p 'any)
  8158. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8159. (and (eq org-id-link-to-org-use-id
  8160. 'create-if-interactive-and-no-custom-id)
  8161. (not custom-id))))
  8162. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8163. ;; We can make a link using the ID.
  8164. (setq link (condition-case nil
  8165. (prog1 (org-id-store-link)
  8166. (setq desc (plist-get org-store-link-plist :description)))
  8167. (error
  8168. ;; probably before first headline, link to file only
  8169. (concat "file:"
  8170. (abbreviate-file-name
  8171. (buffer-file-name (buffer-base-buffer))))))))
  8172. (t
  8173. ;; Just link to current headline
  8174. (setq cpltxt (concat "file:"
  8175. (abbreviate-file-name
  8176. (buffer-file-name (buffer-base-buffer)))))
  8177. ;; Add a context search string
  8178. (when (org-xor org-context-in-file-links arg)
  8179. (let* ((ee (org-element-at-point))
  8180. (et (org-element-type ee))
  8181. (ev (plist-get (cadr ee) :value)))
  8182. (setq txt (cond
  8183. ((org-at-heading-p) nil)
  8184. ((eq et 'keyword) ev)
  8185. ((org-region-active-p)
  8186. (buffer-substring (region-beginning) (region-end)))))
  8187. (when (or (null txt) (string-match "\\S-" txt))
  8188. (setq cpltxt
  8189. (concat cpltxt "::"
  8190. (condition-case nil
  8191. (org-make-org-heading-search-string txt)
  8192. (error "")))
  8193. desc (or (and (eq et 'keyword) ev)
  8194. (nth 4 (ignore-errors (org-heading-components)))
  8195. "NONE")))))
  8196. (if (string-match "::\\'" cpltxt)
  8197. (setq cpltxt (substring cpltxt 0 -2)))
  8198. (setq link cpltxt))))
  8199. ((buffer-file-name (buffer-base-buffer))
  8200. ;; Just link to this file here.
  8201. (setq cpltxt (concat "file:"
  8202. (abbreviate-file-name
  8203. (buffer-file-name (buffer-base-buffer)))))
  8204. ;; Add a context string
  8205. (when (org-xor org-context-in-file-links arg)
  8206. (setq txt (if (org-region-active-p)
  8207. (buffer-substring (region-beginning) (region-end))
  8208. (buffer-substring (point-at-bol) (point-at-eol))))
  8209. ;; Only use search option if there is some text.
  8210. (when (string-match "\\S-" txt)
  8211. (setq cpltxt
  8212. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8213. desc "NONE")))
  8214. (setq link cpltxt))
  8215. ((org-called-interactively-p 'interactive)
  8216. (user-error "No method for storing a link from this buffer"))
  8217. (t (setq link nil)))
  8218. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8219. (setq link (or link cpltxt)
  8220. desc (or desc cpltxt))
  8221. (cond ((equal desc "NONE") (setq desc nil))
  8222. ((string-match org-bracket-link-regexp desc)
  8223. (setq desc (replace-regexp-in-string
  8224. org-bracket-link-regexp
  8225. (concat "\\3" (if (equal (length (match-string 0 desc))
  8226. (length desc)) "*" "")) desc))))
  8227. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8228. (progn
  8229. (setq org-stored-links
  8230. (cons (list link desc) org-stored-links))
  8231. (message "Stored: %s" (or desc link))
  8232. (when custom-id
  8233. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8234. "::#" custom-id))
  8235. (setq org-stored-links
  8236. (cons (list link desc) org-stored-links))))
  8237. (or agenda-link (and link (org-make-link-string link desc)))))))
  8238. (defun org-store-link-props (&rest plist)
  8239. "Store link properties, extract names and addresses."
  8240. (let (x adr)
  8241. (when (setq x (plist-get plist :from))
  8242. (setq adr (mail-extract-address-components x))
  8243. (setq plist (plist-put plist :fromname (car adr)))
  8244. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8245. (when (setq x (plist-get plist :to))
  8246. (setq adr (mail-extract-address-components x))
  8247. (setq plist (plist-put plist :toname (car adr)))
  8248. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8249. (let ((from (plist-get plist :from))
  8250. (to (plist-get plist :to)))
  8251. (when (and from to org-from-is-user-regexp)
  8252. (setq plist
  8253. (plist-put plist :fromto
  8254. (if (string-match org-from-is-user-regexp from)
  8255. (concat "to %t")
  8256. (concat "from %f"))))))
  8257. (setq org-store-link-plist plist))
  8258. (defun org-add-link-props (&rest plist)
  8259. "Add these properties to the link property list."
  8260. (let (key value)
  8261. (while plist
  8262. (setq key (pop plist) value (pop plist))
  8263. (setq org-store-link-plist
  8264. (plist-put org-store-link-plist key value)))))
  8265. (defun org-email-link-description (&optional fmt)
  8266. "Return the description part of an email link.
  8267. This takes information from `org-store-link-plist' and formats it
  8268. according to FMT (default from `org-email-link-description-format')."
  8269. (setq fmt (or fmt org-email-link-description-format))
  8270. (let* ((p org-store-link-plist)
  8271. (to (plist-get p :toaddress))
  8272. (from (plist-get p :fromaddress))
  8273. (table
  8274. (list
  8275. (cons "%c" (plist-get p :fromto))
  8276. (cons "%F" (plist-get p :from))
  8277. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8278. (cons "%T" (plist-get p :to))
  8279. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8280. (cons "%s" (plist-get p :subject))
  8281. (cons "%d" (plist-get p :date))
  8282. (cons "%m" (plist-get p :message-id)))))
  8283. (when (string-match "%c" fmt)
  8284. ;; Check if the user wrote this message
  8285. (if (and org-from-is-user-regexp from to
  8286. (save-match-data (string-match org-from-is-user-regexp from)))
  8287. (setq fmt (replace-match "to %t" t t fmt))
  8288. (setq fmt (replace-match "from %f" t t fmt))))
  8289. (org-replace-escapes fmt table)))
  8290. (defun org-make-org-heading-search-string (&optional string)
  8291. "Make search string for the current headline or STRING."
  8292. (let ((s (or string
  8293. (and (derived-mode-p 'org-mode)
  8294. (save-excursion
  8295. (org-back-to-heading t)
  8296. (plist-get (cadr (org-element-at-point))
  8297. :raw-value)))))
  8298. (lines org-context-in-file-links))
  8299. (or string (setq s (concat "*" s))) ; Add * for headlines
  8300. (when (and string (integerp lines) (> lines 0))
  8301. (let ((slines (org-split-string s "\n")))
  8302. (when (< lines (length slines))
  8303. (setq s (mapconcat
  8304. 'identity
  8305. (reverse (nthcdr (- (length slines) lines)
  8306. (reverse slines))) "\n")))))
  8307. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8308. (defun org-make-link-string (link &optional description)
  8309. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8310. (unless (string-match "\\S-" link)
  8311. (error "Empty link"))
  8312. (when (and description
  8313. (stringp description)
  8314. (not (string-match "\\S-" description)))
  8315. (setq description nil))
  8316. (when (stringp description)
  8317. ;; Remove brackets from the description, they are fatal.
  8318. (while (string-match "\\[" description)
  8319. (setq description (replace-match "{" t t description)))
  8320. (while (string-match "\\]" description)
  8321. (setq description (replace-match "}" t t description))))
  8322. (when (equal link description)
  8323. ;; No description needed, it is identical
  8324. (setq description nil))
  8325. (when (and (not description)
  8326. (not (string-match (org-image-file-name-regexp) link))
  8327. (not (equal link (org-link-escape link))))
  8328. (setq description (org-extract-attributes link)))
  8329. (setq link
  8330. (cond ((string-match (org-image-file-name-regexp) link) link)
  8331. ((string-match org-link-types-re link)
  8332. (concat (match-string 1 link)
  8333. (org-link-escape (substring link (match-end 1)))))
  8334. (t (org-link-escape link))))
  8335. (concat "[[" link "]"
  8336. (if description (concat "[" description "]") "")
  8337. "]"))
  8338. (defconst org-link-escape-chars
  8339. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8340. "List of characters that should be escaped in link.
  8341. This is the list that is used for internal purposes.")
  8342. (defconst org-link-escape-chars-browser
  8343. '(?\ )
  8344. "List of escapes for characters that are problematic in links.
  8345. This is the list that is used before handing over to the browser.")
  8346. (defun org-link-escape (text &optional table merge)
  8347. "Return percent escaped representation of TEXT.
  8348. TEXT is a string with the text to escape.
  8349. Optional argument TABLE is a list with characters that should be
  8350. escaped. When nil, `org-link-escape-chars' is used.
  8351. If optional argument MERGE is set, merge TABLE into
  8352. `org-link-escape-chars'."
  8353. (cond
  8354. ((and table merge)
  8355. (mapc (lambda (defchr)
  8356. (unless (member defchr table)
  8357. (setq table (cons defchr table)))) org-link-escape-chars))
  8358. ((null table)
  8359. (setq table org-link-escape-chars)))
  8360. (mapconcat
  8361. (lambda (char)
  8362. (if (or (member char table)
  8363. (and (or (< char 32) (= char 37) (> char 126))
  8364. org-url-hexify-p))
  8365. (mapconcat (lambda (sequence-element)
  8366. (format "%%%.2X" sequence-element))
  8367. (or (encode-coding-char char 'utf-8)
  8368. (error "Unable to percent escape character: %s"
  8369. (char-to-string char))) "")
  8370. (char-to-string char))) text ""))
  8371. (defun org-link-unescape (str)
  8372. "Unhex hexified Unicode strings as returned from the JavaScript function
  8373. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8374. (unless (and (null str) (string= "" str))
  8375. (let ((pos 0) (case-fold-search t) unhexed)
  8376. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8377. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8378. (setq str (replace-match unhexed t t str))
  8379. (setq pos (+ pos (length unhexed))))))
  8380. str)
  8381. (defun org-link-unescape-compound (hex)
  8382. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8383. Note: this function also decodes single byte encodings like
  8384. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8385. (save-match-data
  8386. (let* ((bytes (cdr (split-string hex "%")))
  8387. (ret "")
  8388. (eat 0)
  8389. (sum 0))
  8390. (while bytes
  8391. (let* ((val (string-to-number (pop bytes) 16))
  8392. (shift-xor
  8393. (if (= 0 eat)
  8394. (cond
  8395. ((>= val 252) (cons 6 252))
  8396. ((>= val 248) (cons 5 248))
  8397. ((>= val 240) (cons 4 240))
  8398. ((>= val 224) (cons 3 224))
  8399. ((>= val 192) (cons 2 192))
  8400. (t (cons 0 0)))
  8401. (cons 6 128))))
  8402. (if (>= val 192) (setq eat (car shift-xor)))
  8403. (setq val (logxor val (cdr shift-xor)))
  8404. (setq sum (+ (lsh sum (car shift-xor)) val))
  8405. (if (> eat 0) (setq eat (- eat 1)))
  8406. (cond
  8407. ((= 0 eat) ;multi byte
  8408. (setq ret (concat ret (org-char-to-string sum)))
  8409. (setq sum 0))
  8410. ((not bytes) ; single byte(s)
  8411. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8412. )) ;; end (while bytes
  8413. ret )))
  8414. (defun org-link-unescape-single-byte-sequence (hex)
  8415. "Unhexify hex-encoded single byte character sequences."
  8416. (mapconcat (lambda (byte)
  8417. (char-to-string (string-to-number byte 16)))
  8418. (cdr (split-string hex "%")) ""))
  8419. (defun org-xor (a b)
  8420. "Exclusive or."
  8421. (if a (not b) b))
  8422. (defun org-fixup-message-id-for-http (s)
  8423. "Replace special characters in a message id, so it can be used in an http query."
  8424. (when (string-match "%" s)
  8425. (setq s (mapconcat (lambda (c)
  8426. (if (eq c ?%)
  8427. "%25"
  8428. (char-to-string c)))
  8429. s "")))
  8430. (while (string-match "<" s)
  8431. (setq s (replace-match "%3C" t t s)))
  8432. (while (string-match ">" s)
  8433. (setq s (replace-match "%3E" t t s)))
  8434. (while (string-match "@" s)
  8435. (setq s (replace-match "%40" t t s)))
  8436. s)
  8437. (defun org-link-prettify (link)
  8438. "Return a human-readable representation of LINK.
  8439. The car of LINK must be a raw link the cdr of LINK must be either
  8440. a link description or nil."
  8441. (let ((desc (or (cadr link) "<no description>")))
  8442. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8443. "<" (car link) ">")))
  8444. ;;;###autoload
  8445. (defun org-insert-link-global ()
  8446. "Insert a link like Org-mode does.
  8447. This command can be called in any mode to insert a link in Org-mode syntax."
  8448. (interactive)
  8449. (org-load-modules-maybe)
  8450. (org-run-like-in-org-mode 'org-insert-link))
  8451. (defun org-insert-all-links (&optional keep)
  8452. "Insert all links in `org-stored-links'."
  8453. (interactive "P")
  8454. (let ((links (copy-sequence org-stored-links)) l)
  8455. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8456. (insert "- ")
  8457. (org-insert-link nil (car l) (cadr l))
  8458. (insert "\n"))))
  8459. (defun org-link-fontify-links-to-this-file ()
  8460. "Fontify links to the current file in `org-stored-links'."
  8461. (let ((f (buffer-file-name)) a b)
  8462. (setq a (mapcar (lambda(l)
  8463. (let ((ll (car l)))
  8464. (when (and (string-match "^file:\\(.+\\)::" ll)
  8465. (equal f (expand-file-name (match-string 1 ll))))
  8466. ll)))
  8467. org-stored-links))
  8468. (when (featurep 'org-id)
  8469. (setq b (mapcar (lambda(l)
  8470. (let ((ll (car l)))
  8471. (when (and (string-match "^id:\\(.+\\)$" ll)
  8472. (equal f (expand-file-name
  8473. (or (org-id-find-id-file
  8474. (match-string 1 ll)) ""))))
  8475. ll)))
  8476. org-stored-links)))
  8477. (mapcar (lambda(l)
  8478. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8479. (delq nil (append a b)))))
  8480. (defvar org-link-links-in-this-file nil)
  8481. (defun org-insert-link (&optional complete-file link-location default-description)
  8482. "Insert a link. At the prompt, enter the link.
  8483. Completion can be used to insert any of the link protocol prefixes like
  8484. http or ftp in use.
  8485. The history can be used to select a link previously stored with
  8486. `org-store-link'. When the empty string is entered (i.e. if you just
  8487. press RET at the prompt), the link defaults to the most recently
  8488. stored link. As SPC triggers completion in the minibuffer, you need to
  8489. use M-SPC or C-q SPC to force the insertion of a space character.
  8490. You will also be prompted for a description, and if one is given, it will
  8491. be displayed in the buffer instead of the link.
  8492. If there is already a link at point, this command will allow you to edit link
  8493. and description parts.
  8494. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8495. be selected using completion. The path to the file will be relative to the
  8496. current directory if the file is in the current directory or a subdirectory.
  8497. Otherwise, the link will be the absolute path as completed in the minibuffer
  8498. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8499. option `org-link-file-path-type'.
  8500. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8501. the current directory or below.
  8502. With three \\[universal-argument] prefixes, negate the meaning of
  8503. `org-keep-stored-link-after-insertion'.
  8504. If `org-make-link-description-function' is non-nil, this function will be
  8505. called with the link target, and the result will be the default
  8506. link description.
  8507. If the LINK-LOCATION parameter is non-nil, this value will be
  8508. used as the link location instead of reading one interactively.
  8509. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8510. be used as the default description."
  8511. (interactive "P")
  8512. (let* ((wcf (current-window-configuration))
  8513. (origbuf (current-buffer))
  8514. (region (if (org-region-active-p)
  8515. (buffer-substring (region-beginning) (region-end))))
  8516. (remove (and region (list (region-beginning) (region-end))))
  8517. (desc region)
  8518. tmphist ; byte-compile incorrectly complains about this
  8519. (link link-location)
  8520. (abbrevs org-link-abbrev-alist-local)
  8521. entry file all-prefixes auto-desc)
  8522. (cond
  8523. (link-location) ; specified by arg, just use it.
  8524. ((org-in-regexp org-bracket-link-regexp 1)
  8525. ;; We do have a link at point, and we are going to edit it.
  8526. (setq remove (list (match-beginning 0) (match-end 0)))
  8527. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8528. (setq link (read-string "Link: "
  8529. (org-link-unescape
  8530. (org-match-string-no-properties 1)))))
  8531. ((or (org-in-regexp org-angle-link-re)
  8532. (org-in-regexp org-plain-link-re))
  8533. ;; Convert to bracket link
  8534. (setq remove (list (match-beginning 0) (match-end 0))
  8535. link (read-string "Link: "
  8536. (org-remove-angle-brackets (match-string 0)))))
  8537. ((member complete-file '((4) (16)))
  8538. ;; Completing read for file names.
  8539. (setq link (org-file-complete-link complete-file)))
  8540. (t
  8541. ;; Read link, with completion for stored links.
  8542. (org-link-fontify-links-to-this-file)
  8543. (org-switch-to-buffer-other-window "*Org Links*")
  8544. (with-current-buffer "*Org Links*"
  8545. (erase-buffer)
  8546. (insert "Insert a link.
  8547. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8548. (when org-stored-links
  8549. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8550. (insert (mapconcat 'org-link-prettify
  8551. (reverse org-stored-links) "\n")))
  8552. (goto-char (point-min)))
  8553. (let ((cw (selected-window)))
  8554. (select-window (get-buffer-window "*Org Links*" 'visible))
  8555. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8556. (unless (pos-visible-in-window-p (point-max))
  8557. (org-fit-window-to-buffer))
  8558. (and (window-live-p cw) (select-window cw)))
  8559. ;; Fake a link history, containing the stored links.
  8560. (setq tmphist (append (mapcar 'car org-stored-links)
  8561. org-insert-link-history))
  8562. (setq all-prefixes (append (mapcar 'car abbrevs)
  8563. (mapcar 'car org-link-abbrev-alist)
  8564. org-link-types))
  8565. (unwind-protect
  8566. (progn
  8567. (setq link
  8568. (org-completing-read
  8569. "Link: "
  8570. (append
  8571. (mapcar (lambda (x) (concat x ":"))
  8572. all-prefixes)
  8573. (mapcar 'car org-stored-links))
  8574. nil nil nil
  8575. 'tmphist
  8576. (caar org-stored-links)))
  8577. (if (not (string-match "\\S-" link))
  8578. (error "No link selected"))
  8579. (mapc (lambda(l)
  8580. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8581. org-stored-links)
  8582. (if (or (member link all-prefixes)
  8583. (and (equal ":" (substring link -1))
  8584. (member (substring link 0 -1) all-prefixes)
  8585. (setq link (substring link 0 -1))))
  8586. (setq link (with-current-buffer origbuf
  8587. (org-link-try-special-completion link)))))
  8588. (set-window-configuration wcf)
  8589. (kill-buffer "*Org Links*"))
  8590. (setq entry (assoc link org-stored-links))
  8591. (or entry (push link org-insert-link-history))
  8592. (setq desc (or desc (nth 1 entry)))))
  8593. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8594. (not org-keep-stored-link-after-insertion))
  8595. (setq org-stored-links (delq (assoc link org-stored-links)
  8596. org-stored-links)))
  8597. (if (string-match org-plain-link-re link)
  8598. ;; URL-like link, normalize the use of angular brackets.
  8599. (setq link (org-remove-angle-brackets link)))
  8600. ;; Check if we are linking to the current file with a search
  8601. ;; option If yes, simplify the link by using only the search
  8602. ;; option.
  8603. (when (and buffer-file-name
  8604. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8605. (let* ((path (match-string 1 link))
  8606. (case-fold-search nil)
  8607. (search (match-string 2 link)))
  8608. (save-match-data
  8609. (if (equal (file-truename buffer-file-name) (file-truename path))
  8610. ;; We are linking to this same file, with a search option
  8611. (setq link search)))))
  8612. ;; Check if we can/should use a relative path. If yes, simplify the link
  8613. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8614. (let* ((type (match-string 1 link))
  8615. (path (match-string 2 link))
  8616. (origpath path)
  8617. (case-fold-search nil))
  8618. (cond
  8619. ((or (eq org-link-file-path-type 'absolute)
  8620. (equal complete-file '(16)))
  8621. (setq path (abbreviate-file-name (expand-file-name path))))
  8622. ((eq org-link-file-path-type 'noabbrev)
  8623. (setq path (expand-file-name path)))
  8624. ((eq org-link-file-path-type 'relative)
  8625. (setq path (file-relative-name path)))
  8626. (t
  8627. (save-match-data
  8628. (if (string-match (concat "^" (regexp-quote
  8629. (expand-file-name
  8630. (file-name-as-directory
  8631. default-directory))))
  8632. (expand-file-name path))
  8633. ;; We are linking a file with relative path name.
  8634. (setq path (substring (expand-file-name path)
  8635. (match-end 0)))
  8636. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8637. (setq link (concat type path))
  8638. (if (equal desc origpath)
  8639. (setq desc path))))
  8640. (if org-make-link-description-function
  8641. (setq desc
  8642. (or (condition-case nil
  8643. (funcall org-make-link-description-function link desc)
  8644. (error (progn (message "Can't get link description from `%s'"
  8645. (symbol-name org-make-link-description-function))
  8646. (sit-for 2) nil)))
  8647. (read-string "Description: " default-description)))
  8648. (if default-description (setq desc default-description)
  8649. (setq desc (or (and auto-desc desc)
  8650. (read-string "Description: " desc)))))
  8651. (unless (string-match "\\S-" desc) (setq desc nil))
  8652. (if remove (apply 'delete-region remove))
  8653. (insert (org-make-link-string link desc))))
  8654. (defun org-link-try-special-completion (type)
  8655. "If there is completion support for link type TYPE, offer it."
  8656. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8657. (if (functionp fun)
  8658. (funcall fun)
  8659. (read-string "Link (no completion support): " (concat type ":")))))
  8660. (defun org-file-complete-link (&optional arg)
  8661. "Create a file link using completion."
  8662. (let (file link)
  8663. (setq file (org-iread-file-name "File: "))
  8664. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8665. (pwd1 (file-name-as-directory (abbreviate-file-name
  8666. (expand-file-name ".")))))
  8667. (cond
  8668. ((equal arg '(16))
  8669. (setq link (concat
  8670. "file:"
  8671. (abbreviate-file-name (expand-file-name file)))))
  8672. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8673. (setq link (concat "file:" (match-string 1 file))))
  8674. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8675. (expand-file-name file))
  8676. (setq link (concat
  8677. "file:" (match-string 1 (expand-file-name file)))))
  8678. (t (setq link (concat "file:" file)))))
  8679. link))
  8680. (defun org-iread-file-name (&rest args)
  8681. "Read-file-name using `ido-mode' speedup if available.
  8682. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  8683. See `read-file-name' for a description of parameters."
  8684. (org-without-partial-completion
  8685. (if (and org-completion-use-ido
  8686. (fboundp 'ido-read-file-name)
  8687. (boundp 'ido-mode) ido-mode
  8688. (listp (second args)))
  8689. (let ((ido-enter-matching-directory nil))
  8690. (apply 'ido-read-file-name args))
  8691. (apply 'read-file-name args))))
  8692. (defun org-completing-read (&rest args)
  8693. "Completing-read with SPACE being a normal character."
  8694. (let ((enable-recursive-minibuffers t)
  8695. (minibuffer-local-completion-map
  8696. (copy-keymap minibuffer-local-completion-map)))
  8697. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8698. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8699. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8700. (apply 'org-icompleting-read args)))
  8701. (defun org-completing-read-no-i (&rest args)
  8702. (let (org-completion-use-ido org-completion-use-iswitchb)
  8703. (apply 'org-completing-read args)))
  8704. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8705. "Use iswitch as a completing-read replacement to choose from choices.
  8706. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8707. from."
  8708. (let* ((iswitchb-use-virtual-buffers nil)
  8709. (iswitchb-make-buflist-hook
  8710. (lambda ()
  8711. (setq iswitchb-temp-buflist choices))))
  8712. (iswitchb-read-buffer prompt)))
  8713. (defun org-icompleting-read (&rest args)
  8714. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8715. (org-without-partial-completion
  8716. (if (and org-completion-use-ido
  8717. (fboundp 'ido-completing-read)
  8718. (boundp 'ido-mode) ido-mode
  8719. (listp (second args)))
  8720. (let ((ido-enter-matching-directory nil))
  8721. (apply 'ido-completing-read (concat (car args))
  8722. (if (consp (car (nth 1 args)))
  8723. (mapcar 'car (nth 1 args))
  8724. (nth 1 args))
  8725. (cddr args)))
  8726. (if (and org-completion-use-iswitchb
  8727. (boundp 'iswitchb-mode) iswitchb-mode
  8728. (listp (second args)))
  8729. (apply 'org-iswitchb-completing-read (concat (car args))
  8730. (if (consp (car (nth 1 args)))
  8731. (mapcar 'car (nth 1 args))
  8732. (nth 1 args))
  8733. (cddr args))
  8734. (apply 'completing-read args)))))
  8735. (defun org-extract-attributes (s)
  8736. "Extract the attributes cookie from a string and set as text property."
  8737. (let (a attr (start 0) key value)
  8738. (save-match-data
  8739. (when (string-match "{{\\([^}]+\\)}}$" s)
  8740. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8741. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8742. (setq key (match-string 1 a) value (match-string 2 a)
  8743. start (match-end 0)
  8744. attr (plist-put attr (intern key) value))))
  8745. (org-add-props s nil 'org-attr attr))
  8746. s))
  8747. (defun org-extract-attributes-from-string (tag)
  8748. (let (key value attr)
  8749. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8750. (setq key (match-string 1 tag) value (match-string 2 tag)
  8751. tag (replace-match "" t t tag)
  8752. attr (plist-put attr (intern key) value)))
  8753. (cons tag attr)))
  8754. (defun org-attributes-to-string (plist)
  8755. "Format a property list into an HTML attribute list."
  8756. (let ((s "") key value)
  8757. (while plist
  8758. (setq key (pop plist) value (pop plist))
  8759. (and value
  8760. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8761. s))
  8762. ;;; Opening/following a link
  8763. (defvar org-link-search-failed nil)
  8764. (defvar org-open-link-functions nil
  8765. "Hook for functions finding a plain text link.
  8766. These functions must take a single argument, the link content.
  8767. They will be called for links that look like [[link text][description]]
  8768. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8769. like a filename (e.g. \"./blue.png\").
  8770. These functions will be called *before* Org attempts to resolve the
  8771. link by doing text searches in the current buffer - so if you want a
  8772. link \"[[target]]\" to still find \"<<target>>\", your function should
  8773. handle this as a special case.
  8774. When the function does handle the link, it must return a non-nil value.
  8775. If it decides that it is not responsible for this link, it must return
  8776. nil to indicate that that Org-mode can continue with other options
  8777. like exact and fuzzy text search.")
  8778. (defun org-next-link ()
  8779. "Move forward to the next link.
  8780. If the link is in hidden text, expose it."
  8781. (interactive)
  8782. (when (and org-link-search-failed (eq this-command last-command))
  8783. (goto-char (point-min))
  8784. (message "Link search wrapped back to beginning of buffer"))
  8785. (setq org-link-search-failed nil)
  8786. (let* ((pos (point))
  8787. (ct (org-context))
  8788. (a (assoc :link ct)))
  8789. (if a (goto-char (nth 2 a)))
  8790. (if (re-search-forward org-any-link-re nil t)
  8791. (progn
  8792. (goto-char (match-beginning 0))
  8793. (if (outline-invisible-p) (org-show-context)))
  8794. (goto-char pos)
  8795. (setq org-link-search-failed t)
  8796. (error "No further link found"))))
  8797. (defun org-previous-link ()
  8798. "Move backward to the previous link.
  8799. If the link is in hidden text, expose it."
  8800. (interactive)
  8801. (when (and org-link-search-failed (eq this-command last-command))
  8802. (goto-char (point-max))
  8803. (message "Link search wrapped back to end of buffer"))
  8804. (setq org-link-search-failed nil)
  8805. (let* ((pos (point))
  8806. (ct (org-context))
  8807. (a (assoc :link ct)))
  8808. (if a (goto-char (nth 1 a)))
  8809. (if (re-search-backward org-any-link-re nil t)
  8810. (progn
  8811. (goto-char (match-beginning 0))
  8812. (if (outline-invisible-p) (org-show-context)))
  8813. (goto-char pos)
  8814. (setq org-link-search-failed t)
  8815. (error "No further link found"))))
  8816. (defun org-translate-link (s)
  8817. "Translate a link string if a translation function has been defined."
  8818. (if (and org-link-translation-function
  8819. (fboundp org-link-translation-function)
  8820. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8821. (progn
  8822. (setq s (funcall org-link-translation-function
  8823. (match-string 1 s) (match-string 2 s)))
  8824. (concat (car s) ":" (cdr s)))
  8825. s))
  8826. (defun org-translate-link-from-planner (type path)
  8827. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8828. This is still an experimental function, your mileage may vary."
  8829. (cond
  8830. ((member type '("http" "https" "news" "ftp"))
  8831. ;; standard Internet links are the same.
  8832. nil)
  8833. ((and (equal type "irc") (string-match "^//" path))
  8834. ;; Planner has two / at the beginning of an irc link, we have 1.
  8835. ;; We should have zero, actually....
  8836. (setq path (substring path 1)))
  8837. ((and (equal type "lisp") (string-match "^/" path))
  8838. ;; Planner has a slash, we do not.
  8839. (setq type "elisp" path (substring path 1)))
  8840. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8841. ;; A typical message link. Planner has the id after the final slash,
  8842. ;; we separate it with a hash mark
  8843. (setq path (concat (match-string 1 path) "#"
  8844. (org-remove-angle-brackets (match-string 2 path)))))
  8845. )
  8846. (cons type path))
  8847. (defun org-find-file-at-mouse (ev)
  8848. "Open file link or URL at mouse."
  8849. (interactive "e")
  8850. (mouse-set-point ev)
  8851. (org-open-at-point 'in-emacs))
  8852. (defun org-open-at-mouse (ev)
  8853. "Open file link or URL at mouse.
  8854. See the docstring of `org-open-file' for details."
  8855. (interactive "e")
  8856. (mouse-set-point ev)
  8857. (if (eq major-mode 'org-agenda-mode)
  8858. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8859. (org-open-at-point))
  8860. (defvar org-window-config-before-follow-link nil
  8861. "The window configuration before following a link.
  8862. This is saved in case the need arises to restore it.")
  8863. (defvar org-open-link-marker (make-marker)
  8864. "Marker pointing to the location where `org-open-at-point; was called.")
  8865. ;;;###autoload
  8866. (defun org-open-at-point-global ()
  8867. "Follow a link like Org-mode does.
  8868. This command can be called in any mode to follow a link that has
  8869. Org-mode syntax."
  8870. (interactive)
  8871. (org-run-like-in-org-mode 'org-open-at-point))
  8872. ;;;###autoload
  8873. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8874. "Open a link in the string S, as if it was in Org-mode."
  8875. (interactive "sLink: \nP")
  8876. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8877. (with-temp-buffer
  8878. (let ((org-inhibit-startup (not reference-buffer)))
  8879. (org-mode)
  8880. (insert s)
  8881. (goto-char (point-min))
  8882. (when reference-buffer
  8883. (setq org-link-abbrev-alist-local
  8884. (with-current-buffer reference-buffer
  8885. org-link-abbrev-alist-local)))
  8886. (org-open-at-point arg reference-buffer)))))
  8887. (defvar org-open-at-point-functions nil
  8888. "Hook that is run when following a link at point.
  8889. Functions in this hook must return t if they identify and follow
  8890. a link at point. If they don't find anything interesting at point,
  8891. they must return nil.")
  8892. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  8893. (defun org-open-at-point (&optional arg reference-buffer)
  8894. "Open link at or after point.
  8895. If there is no link at point, this function will search forward up to
  8896. the end of the current line.
  8897. Normally, files will be opened by an appropriate application. If the
  8898. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8899. With a double prefix argument, try to open outside of Emacs, in the
  8900. application the system uses for this file type."
  8901. (interactive "P")
  8902. ;; if in a code block, then open the block's results
  8903. (unless (call-interactively #'org-babel-open-src-block-result)
  8904. (org-load-modules-maybe)
  8905. (move-marker org-open-link-marker (point))
  8906. (setq org-window-config-before-follow-link (current-window-configuration))
  8907. (org-remove-occur-highlights nil nil t)
  8908. (cond
  8909. ((and (org-at-heading-p)
  8910. (not (org-at-timestamp-p t))
  8911. (not (org-in-regexp
  8912. (concat org-plain-link-re "\\|"
  8913. org-bracket-link-regexp "\\|"
  8914. org-angle-link-re "\\|"
  8915. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8916. (not (get-text-property (point) 'org-linked-text)))
  8917. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  8918. (lk0 (car lkall))
  8919. (lk (if (stringp lk0) (list lk0) lk0))
  8920. (lkend (cdr lkall)))
  8921. (mapcar (lambda(l)
  8922. (search-forward l nil lkend)
  8923. (goto-char (match-beginning 0))
  8924. (org-open-at-point))
  8925. lk))
  8926. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8927. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8928. ((and (org-at-timestamp-p t)
  8929. (not (org-in-regexp org-bracket-link-regexp)))
  8930. (org-follow-timestamp-link))
  8931. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8932. (not (org-in-regexp org-any-link-re)))
  8933. (org-footnote-action))
  8934. (t
  8935. (let (type path link line search (pos (point)))
  8936. (catch 'match
  8937. (save-excursion
  8938. (skip-chars-forward "^]\n\r")
  8939. (when (org-in-regexp org-bracket-link-regexp 1)
  8940. (setq link (org-extract-attributes
  8941. (org-link-unescape (org-match-string-no-properties 1))))
  8942. (while (string-match " *\n *" link)
  8943. (setq link (replace-match " " t t link)))
  8944. (setq link (org-link-expand-abbrev link))
  8945. (cond
  8946. ((or (file-name-absolute-p link)
  8947. (string-match "^\\.\\.?/" link))
  8948. (setq type "file" path link))
  8949. ((string-match org-link-re-with-space3 link)
  8950. (setq type (match-string 1 link) path (match-string 2 link)))
  8951. ((string-match "^help:+\\(.+\\)" link)
  8952. (setq type "help" path (match-string 1 link)))
  8953. (t (setq type "thisfile" path link)))
  8954. (throw 'match t)))
  8955. (when (get-text-property (point) 'org-linked-text)
  8956. (setq type "thisfile"
  8957. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8958. (1+ (point)) (point))
  8959. path (buffer-substring
  8960. (or (previous-single-property-change pos 'org-linked-text)
  8961. (point-min))
  8962. (or (next-single-property-change pos 'org-linked-text)
  8963. (point-max))))
  8964. (throw 'match t))
  8965. (save-excursion
  8966. (let ((plinkpos (org-in-regexp org-plain-link-re)))
  8967. (when (or (org-in-regexp org-angle-link-re)
  8968. (and plinkpos (goto-char (car plinkpos))
  8969. (save-match-data (not (looking-back "\\[\\[")))))
  8970. (setq type (match-string 1)
  8971. path (org-link-unescape (match-string 2)))
  8972. (throw 'match t))))
  8973. (save-excursion
  8974. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8975. (setq type "tags"
  8976. path (match-string 1))
  8977. (while (string-match ":" path)
  8978. (setq path (replace-match "+" t t path)))
  8979. (throw 'match t)))
  8980. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8981. (setq type "tree-match"
  8982. path (match-string 1))
  8983. (throw 'match t)))
  8984. (unless path
  8985. (user-error "No link found"))
  8986. ;; switch back to reference buffer
  8987. ;; needed when if called in a temporary buffer through
  8988. ;; org-open-link-from-string
  8989. (with-current-buffer (or reference-buffer (current-buffer))
  8990. ;; Remove any trailing spaces in path
  8991. (if (string-match " +\\'" path)
  8992. (setq path (replace-match "" t t path)))
  8993. (if (and org-link-translation-function
  8994. (fboundp org-link-translation-function))
  8995. ;; Check if we need to translate the link
  8996. (let ((tmp (funcall org-link-translation-function type path)))
  8997. (setq type (car tmp) path (cdr tmp))))
  8998. (cond
  8999. ((assoc type org-link-protocols)
  9000. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9001. ((equal type "help")
  9002. (let ((f-or-v (intern path)))
  9003. (cond ((fboundp f-or-v)
  9004. (describe-function f-or-v))
  9005. ((boundp f-or-v)
  9006. (describe-variable f-or-v))
  9007. (t (error "Not a known function or variable")))))
  9008. ((equal type "mailto")
  9009. (let ((cmd (car org-link-mailto-program))
  9010. (args (cdr org-link-mailto-program)) args1
  9011. (address path) (subject "") a)
  9012. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9013. (setq address (match-string 1 path)
  9014. subject (org-link-escape (match-string 2 path))))
  9015. (while args
  9016. (cond
  9017. ((not (stringp (car args))) (push (pop args) args1))
  9018. (t (setq a (pop args))
  9019. (if (string-match "%a" a)
  9020. (setq a (replace-match address t t a)))
  9021. (if (string-match "%s" a)
  9022. (setq a (replace-match subject t t a)))
  9023. (push a args1))))
  9024. (apply cmd (nreverse args1))))
  9025. ((member type '("http" "https" "ftp" "news"))
  9026. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9027. (org-link-escape
  9028. path org-link-escape-chars-browser)
  9029. path))))
  9030. ((string= type "doi")
  9031. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9032. (org-link-escape
  9033. path org-link-escape-chars-browser)
  9034. path))))
  9035. ((member type '("message"))
  9036. (browse-url (concat type ":" path)))
  9037. ((string= type "tags")
  9038. (org-tags-view arg path))
  9039. ((string= type "tree-match")
  9040. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9041. ((string= type "file")
  9042. (if (string-match "::\\([0-9]+\\)\\'" path)
  9043. (setq line (string-to-number (match-string 1 path))
  9044. path (substring path 0 (match-beginning 0)))
  9045. (if (string-match "::\\(.+\\)\\'" path)
  9046. (setq search (match-string 1 path)
  9047. path (substring path 0 (match-beginning 0)))))
  9048. (if (string-match "[*?{]" (file-name-nondirectory path))
  9049. (dired path)
  9050. (org-open-file path arg line search)))
  9051. ((string= type "shell")
  9052. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9053. (cmd path))
  9054. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9055. (string-match org-confirm-shell-link-not-regexp cmd))
  9056. (not org-confirm-shell-link-function)
  9057. (funcall org-confirm-shell-link-function
  9058. (format "Execute \"%s\" in shell? "
  9059. (org-add-props cmd nil
  9060. 'face 'org-warning))))
  9061. (progn
  9062. (message "Executing %s" cmd)
  9063. (shell-command cmd buf)
  9064. (if (featurep 'midnight)
  9065. (setq clean-buffer-list-kill-buffer-names
  9066. (cons buf clean-buffer-list-kill-buffer-names))))
  9067. (error "Abort"))))
  9068. ((string= type "elisp")
  9069. (let ((cmd path))
  9070. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9071. (string-match org-confirm-elisp-link-not-regexp cmd))
  9072. (not org-confirm-elisp-link-function)
  9073. (funcall org-confirm-elisp-link-function
  9074. (format "Execute \"%s\" as elisp? "
  9075. (org-add-props cmd nil
  9076. 'face 'org-warning))))
  9077. (message "%s => %s" cmd
  9078. (if (equal (string-to-char cmd) ?\()
  9079. (eval (read cmd))
  9080. (call-interactively (read cmd))))
  9081. (error "Abort"))))
  9082. ((and (string= type "thisfile")
  9083. (run-hook-with-args-until-success
  9084. 'org-open-link-functions path)))
  9085. ((string= type "thisfile")
  9086. (if arg
  9087. (switch-to-buffer-other-window
  9088. (org-get-buffer-for-internal-link (current-buffer)))
  9089. (org-mark-ring-push))
  9090. (let ((cmd `(org-link-search
  9091. ,path
  9092. ,(cond ((equal arg '(4)) ''occur)
  9093. ((equal arg '(16)) ''org-occur))
  9094. ,pos)))
  9095. (condition-case nil (let ((org-link-search-inhibit-query t))
  9096. (eval cmd))
  9097. (error (progn (widen) (eval cmd))))))
  9098. (t (browse-url-at-point)))))))
  9099. (move-marker org-open-link-marker nil)
  9100. (run-hook-with-args 'org-follow-link-hook)))
  9101. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9102. "Offer links in the current entry and return the selected link.
  9103. If there is only one link, return it.
  9104. If NTH is an integer, return the NTH link found.
  9105. If ZERO is a string, check also this string for a link, and if
  9106. there is one, return it."
  9107. (with-current-buffer buffer
  9108. (save-excursion
  9109. (save-restriction
  9110. (widen)
  9111. (goto-char marker)
  9112. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9113. "\\(" org-angle-link-re "\\)\\|"
  9114. "\\(" org-plain-link-re "\\)"))
  9115. (cnt ?0)
  9116. (in-emacs (if (integerp nth) nil nth))
  9117. have-zero end links link c)
  9118. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9119. (push (match-string 0 zero) links)
  9120. (setq cnt (1- cnt) have-zero t))
  9121. (save-excursion
  9122. (org-back-to-heading t)
  9123. (setq end (save-excursion (outline-next-heading) (point)))
  9124. (while (re-search-forward re end t)
  9125. (push (match-string 0) links))
  9126. (setq links (org-uniquify (reverse links))))
  9127. (cond
  9128. ((null links)
  9129. (message "No links"))
  9130. ((equal (length links) 1)
  9131. (setq link (car links)))
  9132. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9133. (setq link (nth (if have-zero nth (1- nth)) links)))
  9134. (t ; we have to select a link
  9135. (save-excursion
  9136. (save-window-excursion
  9137. (delete-other-windows)
  9138. (with-output-to-temp-buffer "*Select Link*"
  9139. (mapc (lambda (l)
  9140. (if (not (string-match org-bracket-link-regexp l))
  9141. (princ (format "[%c] %s\n" (incf cnt)
  9142. (org-remove-angle-brackets l)))
  9143. (if (match-end 3)
  9144. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9145. (match-string 3 l) (match-string 1 l)))
  9146. (princ (format "[%c] %s\n" (incf cnt)
  9147. (match-string 1 l))))))
  9148. links))
  9149. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9150. (message "Select link to open, RET to open all:")
  9151. (setq c (read-char-exclusive))
  9152. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9153. (when (equal c ?q) (error "Abort"))
  9154. (if (equal c ?\C-m)
  9155. (setq link links)
  9156. (setq nth (- c ?0))
  9157. (if have-zero (setq nth (1+ nth)))
  9158. (unless (and (integerp nth) (>= (length links) nth))
  9159. (error "Invalid link selection"))
  9160. (setq link (nth (1- nth) links)))))
  9161. (cons link end))))))
  9162. ;; Add special file links that specify the way of opening
  9163. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9164. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9165. (defun org-open-file-with-system (path)
  9166. "Open file at PATH using the system way of opening it."
  9167. (org-open-file path 'system))
  9168. (defun org-open-file-with-emacs (path)
  9169. "Open file at PATH in Emacs."
  9170. (org-open-file path 'emacs))
  9171. (defun org-remove-file-link-modifiers ()
  9172. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  9173. (goto-char (point-min))
  9174. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  9175. (org-if-unprotected
  9176. (replace-match "file:" t t))))
  9177. (eval-after-load "org-exp"
  9178. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  9179. 'org-remove-file-link-modifiers))
  9180. ;;; File search
  9181. (defvar org-create-file-search-functions nil
  9182. "List of functions to construct the right search string for a file link.
  9183. These functions are called in turn with point at the location to
  9184. which the link should point.
  9185. A function in the hook should first test if it would like to
  9186. handle this file type, for example by checking the `major-mode'
  9187. or the file extension. If it decides not to handle this file, it
  9188. should just return nil to give other functions a chance. If it
  9189. does handle the file, it must return the search string to be used
  9190. when following the link. The search string will be part of the
  9191. file link, given after a double colon, and `org-open-at-point'
  9192. will automatically search for it. If special measures must be
  9193. taken to make the search successful, another function should be
  9194. added to the companion hook `org-execute-file-search-functions',
  9195. which see.
  9196. A function in this hook may also use `setq' to set the variable
  9197. `description' to provide a suggestion for the descriptive text to
  9198. be used for this link when it gets inserted into an Org-mode
  9199. buffer with \\[org-insert-link].")
  9200. (defvar org-execute-file-search-functions nil
  9201. "List of functions to execute a file search triggered by a link.
  9202. Functions added to this hook must accept a single argument, the
  9203. search string that was part of the file link, the part after the
  9204. double colon. The function must first check if it would like to
  9205. handle this search, for example by checking the `major-mode' or
  9206. the file extension. If it decides not to handle this search, it
  9207. should just return nil to give other functions a chance. If it
  9208. does handle the search, it must return a non-nil value to keep
  9209. other functions from trying.
  9210. Each function can access the current prefix argument through the
  9211. variable `current-prefix-argument'. Note that a single prefix is
  9212. used to force opening a link in Emacs, so it may be good to only
  9213. use a numeric or double prefix to guide the search function.
  9214. In case this is needed, a function in this hook can also restore
  9215. the window configuration before `org-open-at-point' was called using:
  9216. (set-window-configuration org-window-config-before-follow-link)")
  9217. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9218. (defun org-link-search (s &optional type avoid-pos stealth)
  9219. "Search for a link search option.
  9220. If S is surrounded by forward slashes, it is interpreted as a
  9221. regular expression. In org-mode files, this will create an `org-occur'
  9222. sparse tree. In ordinary files, `occur' will be used to list matches.
  9223. If the current buffer is in `dired-mode', grep will be used to search
  9224. in all files. If AVOID-POS is given, ignore matches near that position.
  9225. When optional argument STEALTH is non-nil, do not modify
  9226. visibility around point, thus ignoring
  9227. `org-show-hierarchy-above', `org-show-following-heading' and
  9228. `org-show-siblings' variables."
  9229. (let ((case-fold-search t)
  9230. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9231. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9232. (append '(("") (" ") ("\t") ("\n"))
  9233. org-emphasis-alist)
  9234. "\\|") "\\)"))
  9235. (pos (point))
  9236. (pre nil) (post nil)
  9237. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9238. (cond
  9239. ;; First check if there are any special search functions
  9240. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9241. ;; Now try the builtin stuff
  9242. ((and (equal (string-to-char s0) ?#)
  9243. (> (length s0) 1)
  9244. (save-excursion
  9245. (goto-char (point-min))
  9246. (and
  9247. (re-search-forward
  9248. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9249. (setq type 'dedicated
  9250. pos (match-beginning 0))))
  9251. ;; There is an exact target for this
  9252. (goto-char pos)
  9253. (org-back-to-heading t)))
  9254. ((save-excursion
  9255. (goto-char (point-min))
  9256. (and
  9257. (re-search-forward
  9258. (concat "<<" (regexp-quote s0) ">>") nil t)
  9259. (setq type 'dedicated
  9260. pos (match-beginning 0))))
  9261. ;; There is an exact target for this
  9262. (goto-char pos))
  9263. ((save-excursion
  9264. (goto-char (point-min))
  9265. (and
  9266. (re-search-forward
  9267. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9268. (setq type 'dedicated pos (match-beginning 0))))
  9269. ;; Found an invisible target.
  9270. (goto-char pos))
  9271. ((save-excursion
  9272. (goto-char (point-min))
  9273. (and
  9274. (re-search-forward
  9275. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9276. (setq type 'dedicated pos (match-beginning 0))))
  9277. ;; Found an element with a matching #+name affiliated keyword.
  9278. (goto-char pos))
  9279. ((and (string-match "^(\\(.*\\))$" s0)
  9280. (save-excursion
  9281. (goto-char (point-min))
  9282. (and
  9283. (re-search-forward
  9284. (concat "[^[]" (regexp-quote
  9285. (format org-coderef-label-format
  9286. (match-string 1 s0))))
  9287. nil t)
  9288. (setq type 'dedicated
  9289. pos (1+ (match-beginning 0))))))
  9290. ;; There is a coderef target for this
  9291. (goto-char pos))
  9292. ((string-match "^/\\(.*\\)/$" s)
  9293. ;; A regular expression
  9294. (cond
  9295. ((derived-mode-p 'org-mode)
  9296. (org-occur (match-string 1 s)))
  9297. ;;((eq major-mode 'dired-mode)
  9298. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  9299. (t (org-do-occur (match-string 1 s)))))
  9300. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9301. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9302. (goto-char (point-min))
  9303. (cond
  9304. ((let (case-fold-search)
  9305. (re-search-forward (format org-complex-heading-regexp-format
  9306. (regexp-quote s))
  9307. nil t))
  9308. ;; OK, found a match
  9309. (setq type 'dedicated)
  9310. (goto-char (match-beginning 0)))
  9311. ((and (not org-link-search-inhibit-query)
  9312. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9313. (y-or-n-p "No match - create this as a new heading? "))
  9314. (goto-char (point-max))
  9315. (or (bolp) (newline))
  9316. (insert "* " s "\n")
  9317. (beginning-of-line 0))
  9318. (t
  9319. (goto-char pos)
  9320. (error "No match"))))
  9321. (t
  9322. ;; A normal search string
  9323. (when (equal (string-to-char s) ?*)
  9324. ;; Anchor on headlines, post may include tags.
  9325. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9326. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9327. s (substring s 1)))
  9328. (remove-text-properties
  9329. 0 (length s)
  9330. '(face nil mouse-face nil keymap nil fontified nil) s)
  9331. ;; Make a series of regular expressions to find a match
  9332. (setq words (org-split-string s "[ \n\r\t]+")
  9333. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9334. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9335. "\\)" markers)
  9336. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9337. re2a (concat "[ \t\r\n]" re2a_)
  9338. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9339. re4 (concat "[^a-zA-Z_]" re4_)
  9340. re1 (concat pre re2 post)
  9341. re3 (concat pre (if pre re4_ re4) post)
  9342. re5 (concat pre ".*" re4)
  9343. re2 (concat pre re2)
  9344. re2a (concat pre (if pre re2a_ re2a))
  9345. re4 (concat pre (if pre re4_ re4))
  9346. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9347. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9348. re5 "\\)"
  9349. ))
  9350. (cond
  9351. ((eq type 'org-occur) (org-occur reall))
  9352. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9353. (t (goto-char (point-min))
  9354. (setq type 'fuzzy)
  9355. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9356. (org-search-not-self 1 re1 nil t)
  9357. (org-search-not-self 1 re2 nil t)
  9358. (org-search-not-self 1 re2a nil t)
  9359. (org-search-not-self 1 re3 nil t)
  9360. (org-search-not-self 1 re4 nil t)
  9361. (org-search-not-self 1 re5 nil t)
  9362. )
  9363. (goto-char (match-beginning 1))
  9364. (goto-char pos)
  9365. (error "No match"))))))
  9366. (and (derived-mode-p 'org-mode)
  9367. (not stealth)
  9368. (org-show-context 'link-search))
  9369. type))
  9370. (defun org-search-not-self (group &rest args)
  9371. "Execute `re-search-forward', but only accept matches that do not
  9372. enclose the position of `org-open-link-marker'."
  9373. (let ((m org-open-link-marker))
  9374. (catch 'exit
  9375. (while (apply 're-search-forward args)
  9376. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9377. (goto-char (match-end group))
  9378. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9379. (> (match-beginning 0) (marker-position m))
  9380. (< (match-end 0) (marker-position m)))
  9381. (save-match-data
  9382. (or (not (org-in-regexp
  9383. org-bracket-link-analytic-regexp 1))
  9384. (not (match-end 4)) ; no description
  9385. (and (<= (match-beginning 4) (point))
  9386. (>= (match-end 4) (point))))))
  9387. (throw 'exit (point))))))))
  9388. (defun org-get-buffer-for-internal-link (buffer)
  9389. "Return a buffer to be used for displaying the link target of internal links."
  9390. (cond
  9391. ((not org-display-internal-link-with-indirect-buffer)
  9392. buffer)
  9393. ((string-match "(Clone)$" (buffer-name buffer))
  9394. (message "Buffer is already a clone, not making another one")
  9395. ;; we also do not modify visibility in this case
  9396. buffer)
  9397. (t ; make a new indirect buffer for displaying the link
  9398. (let* ((bn (buffer-name buffer))
  9399. (ibn (concat bn "(Clone)"))
  9400. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9401. (with-current-buffer ib (org-overview))
  9402. ib))))
  9403. (defun org-do-occur (regexp &optional cleanup)
  9404. "Call the Emacs command `occur'.
  9405. If CLEANUP is non-nil, remove the printout of the regular expression
  9406. in the *Occur* buffer. This is useful if the regex is long and not useful
  9407. to read."
  9408. (occur regexp)
  9409. (when cleanup
  9410. (let ((cwin (selected-window)) win beg end)
  9411. (when (setq win (get-buffer-window "*Occur*"))
  9412. (select-window win))
  9413. (goto-char (point-min))
  9414. (when (re-search-forward "match[a-z]+" nil t)
  9415. (setq beg (match-end 0))
  9416. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9417. (setq end (1- (match-beginning 0)))))
  9418. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9419. (goto-char (point-min))
  9420. (select-window cwin))))
  9421. ;;; The mark ring for links jumps
  9422. (defvar org-mark-ring nil
  9423. "Mark ring for positions before jumps in Org-mode.")
  9424. (defvar org-mark-ring-last-goto nil
  9425. "Last position in the mark ring used to go back.")
  9426. ;; Fill and close the ring
  9427. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9428. (loop for i from 1 to org-mark-ring-length do
  9429. (push (make-marker) org-mark-ring))
  9430. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9431. org-mark-ring)
  9432. (defun org-mark-ring-push (&optional pos buffer)
  9433. "Put the current position or POS into the mark ring and rotate it."
  9434. (interactive)
  9435. (setq pos (or pos (point)))
  9436. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9437. (move-marker (car org-mark-ring)
  9438. (or pos (point))
  9439. (or buffer (current-buffer)))
  9440. (message "%s"
  9441. (substitute-command-keys
  9442. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9443. (defun org-mark-ring-goto (&optional n)
  9444. "Jump to the previous position in the mark ring.
  9445. With prefix arg N, jump back that many stored positions. When
  9446. called several times in succession, walk through the entire ring.
  9447. Org-mode commands jumping to a different position in the current file,
  9448. or to another Org-mode file, automatically push the old position
  9449. onto the ring."
  9450. (interactive "p")
  9451. (let (p m)
  9452. (if (eq last-command this-command)
  9453. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9454. (setq p org-mark-ring))
  9455. (setq org-mark-ring-last-goto p)
  9456. (setq m (car p))
  9457. (org-pop-to-buffer-same-window (marker-buffer m))
  9458. (goto-char m)
  9459. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9460. (defun org-remove-angle-brackets (s)
  9461. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9462. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9463. s)
  9464. (defun org-add-angle-brackets (s)
  9465. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9466. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9467. s)
  9468. (defun org-remove-double-quotes (s)
  9469. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9470. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9471. s)
  9472. ;;; Following specific links
  9473. (defun org-follow-timestamp-link ()
  9474. "Open an agenda view for the time-stamp date/range at point."
  9475. (cond
  9476. ((org-at-date-range-p t)
  9477. (let ((org-agenda-start-on-weekday)
  9478. (t1 (match-string 1))
  9479. (t2 (match-string 2)) tt1 tt2)
  9480. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9481. tt2 (time-to-days (org-time-string-to-time t2)))
  9482. (let ((org-agenda-buffer-tmp-name
  9483. (format "*Org Agenda(a:%s)"
  9484. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9485. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9486. ((org-at-timestamp-p t)
  9487. (let ((org-agenda-buffer-tmp-name
  9488. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9489. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9490. (substring (match-string 1) 0 10)))
  9491. 1)))
  9492. (t (error "This should not happen"))))
  9493. ;;; Following file links
  9494. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9495. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9496. (declare-function mailcap-mime-info
  9497. "mailcap" (string &optional request no-decode))
  9498. (defvar org-wait nil)
  9499. (defun org-open-file (path &optional in-emacs line search)
  9500. "Open the file at PATH.
  9501. First, this expands any special file name abbreviations. Then the
  9502. configuration variable `org-file-apps' is checked if it contains an
  9503. entry for this file type, and if yes, the corresponding command is launched.
  9504. If no application is found, Emacs simply visits the file.
  9505. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9506. With a double \\[universal-argument] \\[universal-argument] \
  9507. prefix arg, Org tries to avoid opening in Emacs
  9508. and to use an external application to visit the file.
  9509. Optional LINE specifies a line to go to, optional SEARCH a string
  9510. to search for. If LINE or SEARCH is given, the file will be
  9511. opened in Emacs, unless an entry from org-file-apps that makes
  9512. use of groups in a regexp matches.
  9513. If you want to change the way frames are used when following a
  9514. link, please customize `org-link-frame-setup'.
  9515. If the file does not exist, an error is thrown."
  9516. (let* ((file (if (equal path "")
  9517. buffer-file-name
  9518. (substitute-in-file-name (expand-file-name path))))
  9519. (file-apps (append org-file-apps (org-default-apps)))
  9520. (apps (org-remove-if
  9521. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9522. (apps-dlink (org-remove-if-not
  9523. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9524. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9525. (dirp (if remp nil (file-directory-p file)))
  9526. (file (if (and dirp org-open-directory-means-index-dot-org)
  9527. (concat (file-name-as-directory file) "index.org")
  9528. file))
  9529. (a-m-a-p (assq 'auto-mode apps))
  9530. (dfile (downcase file))
  9531. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9532. (link (cond ((and (eq line nil)
  9533. (eq search nil))
  9534. file)
  9535. (line
  9536. (concat file "::" (number-to-string line)))
  9537. (search
  9538. (concat file "::" search))))
  9539. (dlink (downcase link))
  9540. (old-buffer (current-buffer))
  9541. (old-pos (point))
  9542. (old-mode major-mode)
  9543. ext cmd link-match-data)
  9544. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9545. (setq ext (match-string 1 dfile))
  9546. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9547. (setq ext (match-string 1 dfile))))
  9548. (cond
  9549. ((member in-emacs '((16) system))
  9550. (setq cmd (cdr (assoc 'system apps))))
  9551. (in-emacs (setq cmd 'emacs))
  9552. (t
  9553. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9554. (and dirp (cdr (assoc 'directory apps)))
  9555. ; first, try matching against apps-dlink
  9556. ; if we get a match here, store the match data for later
  9557. (let ((match (assoc-default dlink apps-dlink
  9558. 'string-match)))
  9559. (if match
  9560. (progn (setq link-match-data (match-data))
  9561. match)
  9562. (progn (setq in-emacs (or in-emacs line search))
  9563. nil))) ; if we have no match in apps-dlink,
  9564. ; always open the file in emacs if line or search
  9565. ; is given (for backwards compatibility)
  9566. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9567. 'string-match)
  9568. (cdr (assoc ext apps))
  9569. (cdr (assoc t apps))))))
  9570. (when (eq cmd 'system)
  9571. (setq cmd (cdr (assoc 'system apps))))
  9572. (when (eq cmd 'default)
  9573. (setq cmd (cdr (assoc t apps))))
  9574. (when (eq cmd 'mailcap)
  9575. (require 'mailcap)
  9576. (mailcap-parse-mailcaps)
  9577. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9578. (command (mailcap-mime-info mime-type)))
  9579. (if (stringp command)
  9580. (setq cmd command)
  9581. (setq cmd 'emacs))))
  9582. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9583. (not (file-exists-p file))
  9584. (not org-open-non-existing-files))
  9585. (error "No such file: %s" file))
  9586. (cond
  9587. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9588. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9589. (while (string-match "['\"]%s['\"]" cmd)
  9590. (setq cmd (replace-match "%s" t t cmd)))
  9591. (while (string-match "%s" cmd)
  9592. (setq cmd (replace-match
  9593. (save-match-data
  9594. (shell-quote-argument
  9595. (convert-standard-filename file)))
  9596. t t cmd)))
  9597. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9598. (save-match-data
  9599. (let ((match-index 1)
  9600. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9601. (set-match-data link-match-data)
  9602. (while (<= match-index number-of-groups)
  9603. (let ((regex (concat "%" (number-to-string match-index)))
  9604. (replace-with (match-string match-index dlink)))
  9605. (while (string-match regex cmd)
  9606. (setq cmd (replace-match replace-with t t cmd))))
  9607. (setq match-index (+ match-index 1)))))
  9608. (save-window-excursion
  9609. (message "Running %s...done" cmd)
  9610. (start-process-shell-command cmd nil cmd)
  9611. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9612. ((or (stringp cmd)
  9613. (eq cmd 'emacs))
  9614. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9615. (widen)
  9616. (if line (org-goto-line line)
  9617. (if search (org-link-search search))))
  9618. ((consp cmd)
  9619. (let ((file (convert-standard-filename file)))
  9620. (save-match-data
  9621. (set-match-data link-match-data)
  9622. (eval cmd))))
  9623. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9624. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9625. (or (not (equal old-buffer (current-buffer)))
  9626. (not (equal old-pos (point))))
  9627. (org-mark-ring-push old-pos old-buffer))))
  9628. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9629. "This function returns non-nil if `entry' uses a regular
  9630. expression which should be matched against the whole link by
  9631. org-open-file.
  9632. It assumes that is the case when the entry uses a regular
  9633. expression which has at least one grouping construct and the
  9634. action is either a lisp form or a command string containing
  9635. '%1', i.e. using at least one subexpression match as a
  9636. parameter."
  9637. (let ((selector (car entry))
  9638. (action (cdr entry)))
  9639. (if (stringp selector)
  9640. (and (> (regexp-opt-depth selector) 0)
  9641. (or (and (stringp action)
  9642. (string-match "%[0-9]" action))
  9643. (consp action)))
  9644. nil)))
  9645. (defun org-default-apps ()
  9646. "Return the default applications for this operating system."
  9647. (cond
  9648. ((eq system-type 'darwin)
  9649. org-file-apps-defaults-macosx)
  9650. ((eq system-type 'windows-nt)
  9651. org-file-apps-defaults-windowsnt)
  9652. (t org-file-apps-defaults-gnu)))
  9653. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9654. "Convert extensions to regular expressions in the cars of LIST.
  9655. Also, weed out any non-string entries, because the return value is used
  9656. only for regexp matching.
  9657. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9658. point to the symbol `emacs', indicating that the file should
  9659. be opened in Emacs."
  9660. (append
  9661. (delq nil
  9662. (mapcar (lambda (x)
  9663. (if (not (stringp (car x)))
  9664. nil
  9665. (if (string-match "\\W" (car x))
  9666. x
  9667. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9668. list))
  9669. (if add-auto-mode
  9670. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9671. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9672. (defun org-file-remote-p (file)
  9673. "Test whether FILE specifies a location on a remote system.
  9674. Return non-nil if the location is indeed remote.
  9675. For example, the filename \"/user@host:/foo\" specifies a location
  9676. on the system \"/user@host:\"."
  9677. (cond ((fboundp 'file-remote-p)
  9678. (file-remote-p file))
  9679. ((fboundp 'tramp-handle-file-remote-p)
  9680. (tramp-handle-file-remote-p file))
  9681. ((and (boundp 'ange-ftp-name-format)
  9682. (string-match (car ange-ftp-name-format) file))
  9683. t)))
  9684. ;;;; Refiling
  9685. (defun org-get-org-file ()
  9686. "Read a filename, with default directory `org-directory'."
  9687. (let ((default (or org-default-notes-file remember-data-file)))
  9688. (read-file-name (format "File name [%s]: " default)
  9689. (file-name-as-directory org-directory)
  9690. default)))
  9691. (defun org-notes-order-reversed-p ()
  9692. "Check if the current file should receive notes in reversed order."
  9693. (cond
  9694. ((not org-reverse-note-order) nil)
  9695. ((eq t org-reverse-note-order) t)
  9696. ((not (listp org-reverse-note-order)) nil)
  9697. (t (catch 'exit
  9698. (let ((all org-reverse-note-order)
  9699. entry)
  9700. (while (setq entry (pop all))
  9701. (if (string-match (car entry) buffer-file-name)
  9702. (throw 'exit (cdr entry))))
  9703. nil)))))
  9704. (defvar org-refile-target-table nil
  9705. "The list of refile targets, created by `org-refile'.")
  9706. (defvar org-agenda-new-buffers nil
  9707. "Buffers created to visit agenda files.")
  9708. (defvar org-refile-cache nil
  9709. "Cache for refile targets.")
  9710. (defvar org-refile-markers nil
  9711. "All the markers used for caching refile locations.")
  9712. (defun org-refile-marker (pos)
  9713. "Get a new refile marker, but only if caching is in use."
  9714. (if (not org-refile-use-cache)
  9715. pos
  9716. (let ((m (make-marker)))
  9717. (move-marker m pos)
  9718. (push m org-refile-markers)
  9719. m)))
  9720. (defun org-refile-cache-clear ()
  9721. "Clear the refile cache and disable all the markers."
  9722. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9723. (setq org-refile-markers nil)
  9724. (setq org-refile-cache nil)
  9725. (message "Refile cache has been cleared"))
  9726. (defun org-refile-cache-check-set (set)
  9727. "Check if all the markers in the cache still have live buffers."
  9728. (let (marker)
  9729. (catch 'exit
  9730. (while (and set (setq marker (nth 3 (pop set))))
  9731. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9732. (when (and marker (null (marker-buffer marker)))
  9733. (message "not found") (sit-for 3)
  9734. (throw 'exit nil)))
  9735. t)))
  9736. (defun org-refile-cache-put (set &rest identifiers)
  9737. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9738. (let* ((key (sha1 (prin1-to-string identifiers)))
  9739. (entry (assoc key org-refile-cache)))
  9740. (if entry
  9741. (setcdr entry set)
  9742. (push (cons key set) org-refile-cache))))
  9743. (defun org-refile-cache-get (&rest identifiers)
  9744. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9745. (cond
  9746. ((not org-refile-cache) nil)
  9747. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9748. (t
  9749. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9750. org-refile-cache))))
  9751. (and set (org-refile-cache-check-set set) set)))))
  9752. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9753. "Produce a table with refile targets."
  9754. (let ((case-fold-search nil)
  9755. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9756. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9757. targets tgs txt re files f desc descre fast-path-p level pos0)
  9758. (message "Getting targets...")
  9759. (with-current-buffer (or default-buffer (current-buffer))
  9760. (while (setq entry (pop entries))
  9761. (setq files (car entry) desc (cdr entry))
  9762. (setq fast-path-p nil)
  9763. (cond
  9764. ((null files) (setq files (list (current-buffer))))
  9765. ((eq files 'org-agenda-files)
  9766. (setq files (org-agenda-files 'unrestricted)))
  9767. ((and (symbolp files) (fboundp files))
  9768. (setq files (funcall files)))
  9769. ((and (symbolp files) (boundp files))
  9770. (setq files (symbol-value files))))
  9771. (if (stringp files) (setq files (list files)))
  9772. (cond
  9773. ((eq (car desc) :tag)
  9774. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9775. ((eq (car desc) :todo)
  9776. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9777. ((eq (car desc) :regexp)
  9778. (setq descre (cdr desc)))
  9779. ((eq (car desc) :level)
  9780. (setq descre (concat "^\\*\\{" (number-to-string
  9781. (if org-odd-levels-only
  9782. (1- (* 2 (cdr desc)))
  9783. (cdr desc)))
  9784. "\\}[ \t]")))
  9785. ((eq (car desc) :maxlevel)
  9786. (setq fast-path-p t)
  9787. (setq descre (concat "^\\*\\{1," (number-to-string
  9788. (if org-odd-levels-only
  9789. (1- (* 2 (cdr desc)))
  9790. (cdr desc)))
  9791. "\\}[ \t]")))
  9792. (t (error "Bad refiling target description %s" desc)))
  9793. (while (setq f (pop files))
  9794. (with-current-buffer
  9795. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9796. (or
  9797. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9798. (progn
  9799. (if (bufferp f) (setq f (buffer-file-name
  9800. (buffer-base-buffer f))))
  9801. (setq f (and f (expand-file-name f)))
  9802. (if (eq org-refile-use-outline-path 'file)
  9803. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9804. (save-excursion
  9805. (save-restriction
  9806. (widen)
  9807. (goto-char (point-min))
  9808. (while (re-search-forward descre nil t)
  9809. (goto-char (setq pos0 (point-at-bol)))
  9810. (catch 'next
  9811. (when org-refile-target-verify-function
  9812. (save-match-data
  9813. (or (funcall org-refile-target-verify-function)
  9814. (throw 'next t))))
  9815. (when (and (looking-at org-complex-heading-regexp)
  9816. (not (member (match-string 4) excluded-entries))
  9817. (match-string 4))
  9818. (setq level (org-reduced-level
  9819. (- (match-end 1) (match-beginning 1)))
  9820. txt (org-link-display-format (match-string 4))
  9821. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9822. re (format org-complex-heading-regexp-format
  9823. (regexp-quote (match-string 4))))
  9824. (when org-refile-use-outline-path
  9825. (setq txt (mapconcat
  9826. 'org-protect-slash
  9827. (append
  9828. (if (eq org-refile-use-outline-path
  9829. 'file)
  9830. (list (file-name-nondirectory
  9831. (buffer-file-name
  9832. (buffer-base-buffer))))
  9833. (if (eq org-refile-use-outline-path
  9834. 'full-file-path)
  9835. (list (buffer-file-name
  9836. (buffer-base-buffer)))))
  9837. (org-get-outline-path fast-path-p
  9838. level txt)
  9839. (list txt))
  9840. "/")))
  9841. (push (list txt f re (org-refile-marker (point)))
  9842. tgs)))
  9843. (when (= (point) pos0)
  9844. ;; verification function has not moved point
  9845. (goto-char (point-at-eol))))))))
  9846. (when org-refile-use-cache
  9847. (org-refile-cache-put tgs (buffer-file-name) descre))
  9848. (setq targets (append tgs targets))
  9849. ))))
  9850. (message "Getting targets...done")
  9851. (nreverse targets)))
  9852. (defun org-protect-slash (s)
  9853. (while (string-match "/" s)
  9854. (setq s (replace-match "\\" t t s)))
  9855. s)
  9856. (defvar org-olpa (make-vector 20 nil))
  9857. (defun org-get-outline-path (&optional fastp level heading)
  9858. "Return the outline path to the current entry, as a list.
  9859. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9860. routine which makes outline path derivations for an entire file,
  9861. avoiding backtracing. Refile target collection makes use of that."
  9862. (if fastp
  9863. (progn
  9864. (if (> level 19)
  9865. (error "Outline path failure, more than 19 levels"))
  9866. (loop for i from level upto 19 do
  9867. (aset org-olpa i nil))
  9868. (prog1
  9869. (delq nil (append org-olpa nil))
  9870. (aset org-olpa level heading)))
  9871. (let (rtn case-fold-search)
  9872. (save-excursion
  9873. (save-restriction
  9874. (widen)
  9875. (while (org-up-heading-safe)
  9876. (when (looking-at org-complex-heading-regexp)
  9877. (push (org-match-string-no-properties 4) rtn)))
  9878. rtn)))))
  9879. (defun org-format-outline-path (path &optional width prefix separator)
  9880. "Format the outline path PATH for display.
  9881. WIDTH is the maximum number of characters that is available.
  9882. PREFIX is a prefix to be included in the returned string,
  9883. such as the file name.
  9884. SEPARATOR is inserted between the different parts of the path,
  9885. the default is \"/\"."
  9886. (setq width (or width 79))
  9887. (if prefix (setq width (- width (length prefix))))
  9888. (if (not path)
  9889. (or prefix "")
  9890. (let* ((nsteps (length path))
  9891. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9892. (maxwidth (if (<= total-width width)
  9893. 10000 ;; everything fits
  9894. ;; we need to shorten the level headings
  9895. (/ (- width nsteps) nsteps)))
  9896. (org-odd-levels-only nil)
  9897. (n 0)
  9898. (total (1+ (length prefix))))
  9899. (setq maxwidth (max maxwidth 10))
  9900. (concat prefix
  9901. (if prefix (or separator "/"))
  9902. (mapconcat
  9903. (lambda (h)
  9904. (setq n (1+ n))
  9905. (if (and (= n nsteps) (< maxwidth 10000))
  9906. (setq maxwidth (- total-width total)))
  9907. (if (< (length h) maxwidth)
  9908. (progn (setq total (+ total (length h) 1)) h)
  9909. (setq h (substring h 0 (- maxwidth 2))
  9910. total (+ total maxwidth 1))
  9911. (if (string-match "[ \t]+\\'" h)
  9912. (setq h (substring h 0 (match-beginning 0))))
  9913. (setq h (concat h "..")))
  9914. (org-add-props h nil 'face
  9915. (nth (% (1- n) org-n-level-faces)
  9916. org-level-faces))
  9917. h)
  9918. path (or separator "/"))))))
  9919. (defun org-display-outline-path (&optional file current separator just-return-string)
  9920. "Display the current outline path in the echo area.
  9921. If FILE is non-nil, prepend the output with the file name.
  9922. If CURRENT is non-nil, append the current heading to the output.
  9923. SEPARATOR is passed through to `org-format-outline-path'. It separates
  9924. the different parts of the path and defaults to \"/\".
  9925. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  9926. (interactive "P")
  9927. (let* (case-fold-search
  9928. message-log-max ; Don't populate the *Messages* buffer
  9929. (bfn (buffer-file-name (buffer-base-buffer)))
  9930. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  9931. res)
  9932. (if current (setq path (append path
  9933. (save-excursion
  9934. (org-back-to-heading t)
  9935. (if (looking-at org-complex-heading-regexp)
  9936. (list (match-string 4)))))))
  9937. (setq res
  9938. (org-format-outline-path
  9939. path
  9940. (1- (frame-width))
  9941. (and file bfn (concat (file-name-nondirectory bfn) separator))
  9942. separator))
  9943. (if just-return-string
  9944. (org-no-properties res)
  9945. (message "%s" res))))
  9946. (defvar org-refile-history nil
  9947. "History for refiling operations.")
  9948. (defvar org-after-refile-insert-hook nil
  9949. "Hook run after `org-refile' has inserted its stuff at the new location.
  9950. Note that this is still *before* the stuff will be removed from
  9951. the *old* location.")
  9952. (defvar org-capture-last-stored-marker)
  9953. (defvar org-refile-keep nil
  9954. "Non-nil means `org-refile' will copy instead of refile.")
  9955. (defun org-copy ()
  9956. "Like `org-refile', but copy."
  9957. (interactive)
  9958. (let ((org-refile-keep t))
  9959. (funcall 'org-refile nil nil nil "Copy")))
  9960. (defun org-refile (&optional goto default-buffer rfloc msg)
  9961. "Move the entry or entries at point to another heading.
  9962. The list of target headings is compiled using the information in
  9963. `org-refile-targets', which see.
  9964. At the target location, the entry is filed as a subitem of the target
  9965. heading. Depending on `org-reverse-note-order', the new subitem will
  9966. either be the first or the last subitem.
  9967. If there is an active region, all entries in that region will be moved.
  9968. However, the region must fulfill the requirement that the first heading
  9969. is the first one sets the top-level of the moved text - at most siblings
  9970. below it are allowed.
  9971. With prefix arg GOTO, the command will only visit the target location
  9972. and not actually move anything.
  9973. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9974. go to the location where the last refiling operation has put the subtree.
  9975. With a prefix argument of `2', refile to the running clock.
  9976. RFLOC can be a refile location obtained in a different way.
  9977. MSG is a string to replace \"Refile\" in the default prompt with
  9978. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  9979. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9980. If you are using target caching (see `org-refile-use-cache'),
  9981. you have to clear the target cache in order to find new targets.
  9982. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9983. prefix argument (`C-u C-u C-u C-c C-w')."
  9984. (interactive "P")
  9985. (if (member goto '(0 (64)))
  9986. (org-refile-cache-clear)
  9987. (let* ((actionmsg (or msg "Refile"))
  9988. (cbuf (current-buffer))
  9989. (regionp (org-region-active-p))
  9990. (region-start (and regionp (region-beginning)))
  9991. (region-end (and regionp (region-end)))
  9992. (region-length (and regionp (- region-end region-start)))
  9993. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9994. pos it nbuf file re level reversed)
  9995. (setq last-command nil)
  9996. (when regionp
  9997. (goto-char region-start)
  9998. (or (bolp) (goto-char (point-at-bol)))
  9999. (setq region-start (point))
  10000. (unless (or (org-kill-is-subtree-p
  10001. (buffer-substring region-start region-end))
  10002. (prog1 org-refile-active-region-within-subtree
  10003. (org-toggle-heading)))
  10004. (error "The region is not a (sequence of) subtree(s)")))
  10005. (if (equal goto '(16))
  10006. (org-refile-goto-last-stored)
  10007. (when (or
  10008. (and (equal goto 2)
  10009. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10010. (prog1
  10011. (setq it (list (or org-clock-heading "running clock")
  10012. (buffer-file-name
  10013. (marker-buffer org-clock-hd-marker))
  10014. ""
  10015. (marker-position org-clock-hd-marker)))
  10016. (setq goto nil)))
  10017. (setq it (or rfloc
  10018. (let (heading-text)
  10019. (save-excursion
  10020. (unless goto
  10021. (org-back-to-heading t)
  10022. (setq heading-text
  10023. (nth 4 (org-heading-components))))
  10024. (org-refile-get-location
  10025. (cond (goto "Goto")
  10026. (regionp (concat actionmsg " region to"))
  10027. (t (concat actionmsg " subtree \""
  10028. heading-text "\" to")))
  10029. default-buffer
  10030. (and (not (equal '(4) goto))
  10031. org-refile-allow-creating-parent-nodes)
  10032. goto))))))
  10033. (setq file (nth 1 it)
  10034. re (nth 2 it)
  10035. pos (nth 3 it))
  10036. (if (and (not goto)
  10037. pos
  10038. (equal (buffer-file-name) file)
  10039. (if regionp
  10040. (and (>= pos region-start)
  10041. (<= pos region-end))
  10042. (and (>= pos (point))
  10043. (< pos (save-excursion
  10044. (org-end-of-subtree t t))))))
  10045. (error "Cannot refile to position inside the tree or region"))
  10046. (setq nbuf (or (find-buffer-visiting file)
  10047. (find-file-noselect file)))
  10048. (if goto
  10049. (progn
  10050. (org-pop-to-buffer-same-window nbuf)
  10051. (goto-char pos)
  10052. (org-show-context 'org-goto))
  10053. (if regionp
  10054. (progn
  10055. (org-kill-new (buffer-substring region-start region-end))
  10056. (org-save-markers-in-region region-start region-end))
  10057. (org-copy-subtree 1 nil t))
  10058. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10059. (find-file-noselect file)))
  10060. (setq reversed (org-notes-order-reversed-p))
  10061. (save-excursion
  10062. (save-restriction
  10063. (widen)
  10064. (if pos
  10065. (progn
  10066. (goto-char pos)
  10067. (looking-at org-outline-regexp)
  10068. (setq level (org-get-valid-level (funcall outline-level) 1))
  10069. (goto-char
  10070. (if reversed
  10071. (or (outline-next-heading) (point-max))
  10072. (or (save-excursion (org-get-next-sibling))
  10073. (org-end-of-subtree t t)
  10074. (point-max)))))
  10075. (setq level 1)
  10076. (if (not reversed)
  10077. (goto-char (point-max))
  10078. (goto-char (point-min))
  10079. (or (outline-next-heading) (goto-char (point-max)))))
  10080. (if (not (bolp)) (newline))
  10081. (org-paste-subtree level)
  10082. (when org-log-refile
  10083. (org-add-log-setup 'refile nil nil 'findpos
  10084. org-log-refile)
  10085. (unless (eq org-log-refile 'note)
  10086. (save-excursion (org-add-log-note))))
  10087. (and org-auto-align-tags
  10088. (let ((org-loop-over-headlines-in-active-region nil))
  10089. (org-set-tags nil t)))
  10090. (with-demoted-errors
  10091. (bookmark-set "org-refile-last-stored"))
  10092. ;; If we are refiling for capture, make sure that the
  10093. ;; last-capture pointers point here
  10094. (when (org-bound-and-true-p org-refile-for-capture)
  10095. (with-demoted-errors
  10096. (bookmark-set "org-capture-last-stored-marker"))
  10097. (move-marker org-capture-last-stored-marker (point)))
  10098. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10099. (run-hooks 'org-after-refile-insert-hook))))
  10100. (unless org-refile-keep
  10101. (if regionp
  10102. (delete-region (point) (+ (point) region-length))
  10103. (org-cut-subtree)))
  10104. (when (featurep 'org-inlinetask)
  10105. (org-inlinetask-remove-END-maybe))
  10106. (setq org-markers-to-move nil)
  10107. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10108. (defun org-refile-goto-last-stored ()
  10109. "Go to the location where the last refile was stored."
  10110. (interactive)
  10111. (bookmark-jump "org-refile-last-stored")
  10112. (message "This is the location of the last refile"))
  10113. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10114. no-exclude)
  10115. "Prompt the user for a refile location, using PROMPT.
  10116. PROMPT should not be suffixed with a colon and a space, because
  10117. this function appends the default value from
  10118. `org-refile-history' automatically, if that is not empty.
  10119. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10120. this is used for the GOTO interface."
  10121. (let ((org-refile-targets org-refile-targets)
  10122. (org-refile-use-outline-path org-refile-use-outline-path)
  10123. excluded-entries)
  10124. (when (and (derived-mode-p 'org-mode)
  10125. (not org-refile-use-cache)
  10126. (not no-exclude))
  10127. (org-map-tree
  10128. (lambda()
  10129. (setq excluded-entries
  10130. (append excluded-entries (list (org-get-heading t t)))))))
  10131. (setq org-refile-target-table
  10132. (org-refile-get-targets default-buffer excluded-entries)))
  10133. (unless org-refile-target-table
  10134. (error "No refile targets"))
  10135. (let* ((cbuf (current-buffer))
  10136. (partial-completion-mode nil)
  10137. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10138. (cfunc (if (and org-refile-use-outline-path
  10139. org-outline-path-complete-in-steps)
  10140. 'org-olpath-completing-read
  10141. 'org-icompleting-read))
  10142. (extra (if org-refile-use-outline-path "/" ""))
  10143. (cbnex (concat (buffer-name) extra))
  10144. (filename (and cfn (expand-file-name cfn)))
  10145. (tbl (mapcar
  10146. (lambda (x)
  10147. (if (and (not (member org-refile-use-outline-path
  10148. '(file full-file-path)))
  10149. (not (equal filename (nth 1 x))))
  10150. (cons (concat (car x) extra " ("
  10151. (file-name-nondirectory (nth 1 x)) ")")
  10152. (cdr x))
  10153. (cons (concat (car x) extra) (cdr x))))
  10154. org-refile-target-table))
  10155. (completion-ignore-case t)
  10156. cdef
  10157. (prompt (concat prompt
  10158. (or (and (car org-refile-history)
  10159. (concat " (default " (car org-refile-history) ")"))
  10160. (and (assoc cbnex tbl) (setq cdef cbnex)
  10161. (concat " (default " cbnex ")"))) ": "))
  10162. pa answ parent-target child parent old-hist)
  10163. (setq old-hist org-refile-history)
  10164. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10165. nil 'org-refile-history (or cdef (car org-refile-history))))
  10166. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10167. (org-refile-check-position pa)
  10168. (if pa
  10169. (progn
  10170. (when (or (not org-refile-history)
  10171. (not (eq old-hist org-refile-history))
  10172. (not (equal (car pa) (car org-refile-history))))
  10173. (setq org-refile-history
  10174. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10175. org-refile-history
  10176. (cdr org-refile-history))))
  10177. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10178. (pop org-refile-history)))
  10179. pa)
  10180. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10181. (progn
  10182. (setq parent (match-string 1 answ)
  10183. child (match-string 2 answ))
  10184. (setq parent-target (or (assoc parent tbl)
  10185. (assoc (concat parent "/") tbl)))
  10186. (when (and parent-target
  10187. (or (eq new-nodes t)
  10188. (and (eq new-nodes 'confirm)
  10189. (y-or-n-p (format "Create new node \"%s\"? "
  10190. child)))))
  10191. (org-refile-new-child parent-target child)))
  10192. (error "Invalid target location")))))
  10193. (declare-function org-string-nw-p "org-macs" (s))
  10194. (defun org-refile-check-position (refile-pointer)
  10195. "Check if the refile pointer matches the headline to which it points."
  10196. (let* ((file (nth 1 refile-pointer))
  10197. (re (nth 2 refile-pointer))
  10198. (pos (nth 3 refile-pointer))
  10199. buffer)
  10200. (if (and (not (markerp pos)) (not file))
  10201. (error "Please save the buffer to a file before refiling")
  10202. (when (org-string-nw-p re)
  10203. (setq buffer (if (markerp pos)
  10204. (marker-buffer pos)
  10205. (or (find-buffer-visiting file)
  10206. (find-file-noselect file))))
  10207. (with-current-buffer buffer
  10208. (save-excursion
  10209. (save-restriction
  10210. (widen)
  10211. (goto-char pos)
  10212. (beginning-of-line 1)
  10213. (unless (org-looking-at-p re)
  10214. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10215. (defun org-refile-new-child (parent-target child)
  10216. "Use refile target PARENT-TARGET to add new CHILD below it."
  10217. (unless parent-target
  10218. (error "Cannot find parent for new node"))
  10219. (let ((file (nth 1 parent-target))
  10220. (pos (nth 3 parent-target))
  10221. level)
  10222. (with-current-buffer (or (find-buffer-visiting file)
  10223. (find-file-noselect file))
  10224. (save-excursion
  10225. (save-restriction
  10226. (widen)
  10227. (if pos
  10228. (goto-char pos)
  10229. (goto-char (point-max))
  10230. (if (not (bolp)) (newline)))
  10231. (when (looking-at org-outline-regexp)
  10232. (setq level (funcall outline-level))
  10233. (org-end-of-subtree t t))
  10234. (org-back-over-empty-lines)
  10235. (insert "\n" (make-string
  10236. (if pos (org-get-valid-level level 1) 1) ?*)
  10237. " " child "\n")
  10238. (beginning-of-line 0)
  10239. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10240. (defun org-olpath-completing-read (prompt collection &rest args)
  10241. "Read an outline path like a file name."
  10242. (let ((thetable collection)
  10243. (org-completion-use-ido nil) ; does not work with ido.
  10244. (org-completion-use-iswitchb nil)) ; or iswitchb
  10245. (apply
  10246. 'org-icompleting-read prompt
  10247. (lambda (string predicate &optional flag)
  10248. (let (rtn r f (l (length string)))
  10249. (cond
  10250. ((eq flag nil)
  10251. ;; try completion
  10252. (try-completion string thetable))
  10253. ((eq flag t)
  10254. ;; all-completions
  10255. (setq rtn (all-completions string thetable predicate))
  10256. (mapcar
  10257. (lambda (x)
  10258. (setq r (substring x l))
  10259. (if (string-match " ([^)]*)$" x)
  10260. (setq f (match-string 0 x))
  10261. (setq f ""))
  10262. (if (string-match "/" r)
  10263. (concat string (substring r 0 (match-end 0)) f)
  10264. x))
  10265. rtn))
  10266. ((eq flag 'lambda)
  10267. ;; exact match?
  10268. (assoc string thetable)))))
  10269. args)))
  10270. ;;;; Dynamic blocks
  10271. (defun org-find-dblock (name)
  10272. "Find the first dynamic block with name NAME in the buffer.
  10273. If not found, stay at current position and return nil."
  10274. (let ((case-fold-search t) pos)
  10275. (save-excursion
  10276. (goto-char (point-min))
  10277. (setq pos (and (re-search-forward
  10278. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10279. (match-beginning 0))))
  10280. (if pos (goto-char pos))
  10281. pos))
  10282. (defconst org-dblock-start-re
  10283. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10284. "Matches the start line of a dynamic block, with parameters.")
  10285. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10286. "Matches the end of a dynamic block.")
  10287. (defun org-create-dblock (plist)
  10288. "Create a dynamic block section, with parameters taken from PLIST.
  10289. PLIST must contain a :name entry which is used as name of the block."
  10290. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10291. (end-of-line 1)
  10292. (newline))
  10293. (let ((col (current-column))
  10294. (name (plist-get plist :name)))
  10295. (insert "#+BEGIN: " name)
  10296. (while plist
  10297. (if (eq (car plist) :name)
  10298. (setq plist (cddr plist))
  10299. (insert " " (prin1-to-string (pop plist)))))
  10300. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10301. (beginning-of-line -2)))
  10302. (defun org-prepare-dblock ()
  10303. "Prepare dynamic block for refresh.
  10304. This empties the block, puts the cursor at the insert position and returns
  10305. the property list including an extra property :name with the block name."
  10306. (unless (looking-at org-dblock-start-re)
  10307. (error "Not at a dynamic block"))
  10308. (let* ((begdel (1+ (match-end 0)))
  10309. (name (org-no-properties (match-string 1)))
  10310. (params (append (list :name name)
  10311. (read (concat "(" (match-string 3) ")")))))
  10312. (save-excursion
  10313. (beginning-of-line 1)
  10314. (skip-chars-forward " \t")
  10315. (setq params (plist-put params :indentation-column (current-column))))
  10316. (unless (re-search-forward org-dblock-end-re nil t)
  10317. (error "Dynamic block not terminated"))
  10318. (setq params
  10319. (append params
  10320. (list :content (buffer-substring
  10321. begdel (match-beginning 0)))))
  10322. (delete-region begdel (match-beginning 0))
  10323. (goto-char begdel)
  10324. (open-line 1)
  10325. params))
  10326. (defun org-map-dblocks (&optional command)
  10327. "Apply COMMAND to all dynamic blocks in the current buffer.
  10328. If COMMAND is not given, use `org-update-dblock'."
  10329. (let ((cmd (or command 'org-update-dblock)))
  10330. (save-excursion
  10331. (goto-char (point-min))
  10332. (while (re-search-forward org-dblock-start-re nil t)
  10333. (goto-char (match-beginning 0))
  10334. (save-excursion
  10335. (condition-case nil
  10336. (funcall cmd)
  10337. (error (message "Error during update of dynamic block"))))
  10338. (unless (re-search-forward org-dblock-end-re nil t)
  10339. (error "Dynamic block not terminated"))))))
  10340. (defun org-dblock-update (&optional arg)
  10341. "User command for updating dynamic blocks.
  10342. Update the dynamic block at point. With prefix ARG, update all dynamic
  10343. blocks in the buffer."
  10344. (interactive "P")
  10345. (if arg
  10346. (org-update-all-dblocks)
  10347. (or (looking-at org-dblock-start-re)
  10348. (org-beginning-of-dblock))
  10349. (org-update-dblock)))
  10350. (defun org-update-dblock ()
  10351. "Update the dynamic block at point.
  10352. This means to empty the block, parse for parameters and then call
  10353. the correct writing function."
  10354. (interactive)
  10355. (save-window-excursion
  10356. (let* ((pos (point))
  10357. (line (org-current-line))
  10358. (params (org-prepare-dblock))
  10359. (name (plist-get params :name))
  10360. (indent (plist-get params :indentation-column))
  10361. (cmd (intern (concat "org-dblock-write:" name))))
  10362. (message "Updating dynamic block `%s' at line %d..." name line)
  10363. (funcall cmd params)
  10364. (message "Updating dynamic block `%s' at line %d...done" name line)
  10365. (goto-char pos)
  10366. (when (and indent (> indent 0))
  10367. (setq indent (make-string indent ?\ ))
  10368. (save-excursion
  10369. (org-beginning-of-dblock)
  10370. (forward-line 1)
  10371. (while (not (looking-at org-dblock-end-re))
  10372. (insert indent)
  10373. (beginning-of-line 2))
  10374. (when (looking-at org-dblock-end-re)
  10375. (and (looking-at "[ \t]+")
  10376. (replace-match ""))
  10377. (insert indent)))))))
  10378. (defun org-beginning-of-dblock ()
  10379. "Find the beginning of the dynamic block at point.
  10380. Error if there is no such block at point."
  10381. (let ((pos (point))
  10382. beg)
  10383. (end-of-line 1)
  10384. (if (and (re-search-backward org-dblock-start-re nil t)
  10385. (setq beg (match-beginning 0))
  10386. (re-search-forward org-dblock-end-re nil t)
  10387. (> (match-end 0) pos))
  10388. (goto-char beg)
  10389. (goto-char pos)
  10390. (error "Not in a dynamic block"))))
  10391. (defun org-update-all-dblocks ()
  10392. "Update all dynamic blocks in the buffer.
  10393. This function can be used in a hook."
  10394. (interactive)
  10395. (when (derived-mode-p 'org-mode)
  10396. (org-map-dblocks 'org-update-dblock)))
  10397. ;;;; Completion
  10398. (defconst org-additional-option-like-keywords
  10399. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10400. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10401. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10402. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10403. "BEGIN:" "END:"
  10404. "ORGTBL" "TBLFM:" "TBLNAME:"
  10405. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10406. "BEGIN_VERBATIM" "END_VERBATIM"
  10407. "BEGIN_QUOTE" "END_QUOTE"
  10408. "BEGIN_VERSE" "END_VERSE"
  10409. "BEGIN_CENTER" "END_CENTER"
  10410. "BEGIN_SRC" "END_SRC"
  10411. "BEGIN_RESULT" "END_RESULT"
  10412. "BEGIN_lstlisting" "END_lstlisting"
  10413. "NAME:" "RESULTS:"
  10414. "HEADER:" "HEADERS:"
  10415. "COLUMNS:" "PROPERTY:"
  10416. "CAPTION:" "LABEL:"
  10417. "SETUPFILE:"
  10418. "INCLUDE:" "INDEX:"
  10419. "BIND:"
  10420. "MACRO:"))
  10421. (defconst org-options-keywords
  10422. '("TITLE:" "AUTHOR:" "EMAIL:" "DATE:"
  10423. "DESCRIPTION:" "KEYWORDS:" "LANGUAGE:" "OPTIONS:"
  10424. "EXPORT_SELECT_TAGS:" "EXPORT_EXCLUDE_TAGS:"
  10425. "LINK_UP:" "LINK_HOME:" "LINK:" "TODO:"
  10426. "XSLT:" "MATHJAX:" "CATEGORY:" "SEQ_TODO:" "TYP_TODO:"
  10427. "PRIORITIES:" "DRAWERS:" "STARTUP:" "TAGS:" "STYLE:"
  10428. "FILETAGS:" "ARCHIVE:" "INFOJS_OPT:"))
  10429. (defconst org-additional-option-like-keywords-for-flyspell
  10430. (delete-dups
  10431. (split-string
  10432. (mapconcat (lambda(k)
  10433. (replace-regexp-in-string
  10434. "_\\|:" " "
  10435. (concat k " " (downcase k) " " (upcase k))))
  10436. (append org-options-keywords org-additional-option-like-keywords)
  10437. " ")
  10438. " +" t)))
  10439. (defcustom org-structure-template-alist
  10440. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10441. "<src lang=\"?\">\n\n</src>")
  10442. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10443. "<example>\n?\n</example>")
  10444. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10445. "<quote>\n?\n</quote>")
  10446. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10447. "<verse>\n?\n</verse>")
  10448. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10449. "<verbatim>\n?\n</verbatim>")
  10450. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10451. "<center>\n?\n</center>")
  10452. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10453. "<literal style=\"latex\">\n?\n</literal>")
  10454. ("L" "#+LaTeX: "
  10455. "<literal style=\"latex\">?</literal>")
  10456. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10457. "<literal style=\"html\">\n?\n</literal>")
  10458. ("H" "#+HTML: "
  10459. "<literal style=\"html\">?</literal>")
  10460. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10461. ("A" "#+ASCII: ")
  10462. ("i" "#+INDEX: ?"
  10463. "#+INDEX: ?")
  10464. ("I" "#+INCLUDE: %file ?"
  10465. "<include file=%file markup=\"?\">"))
  10466. "Structure completion elements.
  10467. This is a list of abbreviation keys and values. The value gets inserted
  10468. if you type `<' followed by the key and then press the completion key,
  10469. usually `M-TAB'. %file will be replaced by a file name after prompting
  10470. for the file using completion. The cursor will be placed at the position
  10471. of the `?` in the template.
  10472. There are two templates for each key, the first uses the original Org syntax,
  10473. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10474. the default when the /org-mtags.el/ module has been loaded. See also the
  10475. variable `org-mtags-prefer-muse-templates'."
  10476. :group 'org-completion
  10477. :type '(repeat
  10478. (string :tag "Key")
  10479. (string :tag "Template")
  10480. (string :tag "Muse Template")))
  10481. (defun org-try-structure-completion ()
  10482. "Try to complete a structure template before point.
  10483. This looks for strings like \"<e\" on an otherwise empty line and
  10484. expands them."
  10485. (let ((l (buffer-substring (point-at-bol) (point)))
  10486. a)
  10487. (when (and (looking-at "[ \t]*$")
  10488. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10489. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10490. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10491. (match-beginning 1)) a)
  10492. t)))
  10493. (defun org-complete-expand-structure-template (start cell)
  10494. "Expand a structure template."
  10495. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10496. (rpl (nth (if musep 2 1) cell))
  10497. (ind ""))
  10498. (delete-region start (point))
  10499. (when (string-match "\\`#\\+" rpl)
  10500. (cond
  10501. ((bolp))
  10502. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10503. (setq ind (buffer-substring (point-at-bol) (point))))
  10504. (t (newline))))
  10505. (setq start (point))
  10506. (if (string-match "%file" rpl)
  10507. (setq rpl (replace-match
  10508. (concat
  10509. "\""
  10510. (save-match-data
  10511. (abbreviate-file-name (read-file-name "Include file: ")))
  10512. "\"")
  10513. t t rpl)))
  10514. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10515. (concat "\n" ind)))
  10516. (insert rpl)
  10517. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10518. ;;;; TODO, DEADLINE, Comments
  10519. (defun org-toggle-comment ()
  10520. "Change the COMMENT state of an entry."
  10521. (interactive)
  10522. (save-excursion
  10523. (org-back-to-heading)
  10524. (let (case-fold-search)
  10525. (cond
  10526. ((looking-at (format org-heading-keyword-regexp-format
  10527. org-comment-string))
  10528. (goto-char (match-end 1))
  10529. (looking-at (concat " +" org-comment-string))
  10530. (replace-match "" t t)
  10531. (when (eolp) (insert " ")))
  10532. ((looking-at org-outline-regexp)
  10533. (goto-char (match-end 0))
  10534. (insert org-comment-string " "))))))
  10535. (defvar org-last-todo-state-is-todo nil
  10536. "This is non-nil when the last TODO state change led to a TODO state.
  10537. If the last change removed the TODO tag or switched to DONE, then
  10538. this is nil.")
  10539. (defvar org-setting-tags nil) ; dynamically skipped
  10540. (defvar org-todo-setup-filter-hook nil
  10541. "Hook for functions that pre-filter todo specs.
  10542. Each function takes a todo spec and returns either nil or the spec
  10543. transformed into canonical form." )
  10544. (defvar org-todo-get-default-hook nil
  10545. "Hook for functions that get a default item for todo.
  10546. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10547. nil or a string to be used for the todo mark." )
  10548. (defvar org-agenda-headline-snapshot-before-repeat)
  10549. (defun org-current-effective-time ()
  10550. "Return current time adjusted for `org-extend-today-until' variable."
  10551. (let* ((ct (org-current-time))
  10552. (dct (decode-time ct))
  10553. (ct1
  10554. (cond
  10555. (org-use-last-clock-out-time-as-effective-time
  10556. (or (org-clock-get-last-clock-out-time) ct))
  10557. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10558. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10559. (t ct))))
  10560. ct1))
  10561. (defun org-todo-yesterday (&optional arg)
  10562. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10563. (interactive "P")
  10564. (if (eq major-mode 'org-agenda-mode)
  10565. (apply 'org-agenda-todo-yesterday arg)
  10566. (let* ((hour (third (decode-time
  10567. (org-current-time))))
  10568. (org-extend-today-until (1+ hour)))
  10569. (org-todo arg))))
  10570. (defvar org-block-entry-blocking ""
  10571. "First entry preventing the TODO state change.")
  10572. (defun org-todo (&optional arg)
  10573. "Change the TODO state of an item.
  10574. The state of an item is given by a keyword at the start of the heading,
  10575. like
  10576. *** TODO Write paper
  10577. *** DONE Call mom
  10578. The different keywords are specified in the variable `org-todo-keywords'.
  10579. By default the available states are \"TODO\" and \"DONE\".
  10580. So for this example: when the item starts with TODO, it is changed to DONE.
  10581. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10582. DONE are present, add TODO at the beginning of the heading.
  10583. With \\[universal-argument] prefix arg, use completion to determine the new \
  10584. state.
  10585. With numeric prefix arg, switch to that state.
  10586. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10587. keywords (nextset).
  10588. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10589. With a numeric prefix arg of 0, inhibit note taking for the change.
  10590. For calling through lisp, arg is also interpreted in the following way:
  10591. 'none -> empty state
  10592. \"\"(empty string) -> switch to empty state
  10593. 'done -> switch to DONE
  10594. 'nextset -> switch to the next set of keywords
  10595. 'previousset -> switch to the previous set of keywords
  10596. \"WAITING\" -> switch to the specified keyword, but only if it
  10597. really is a member of `org-todo-keywords'."
  10598. (interactive "P")
  10599. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10600. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10601. 'region-start-level 'region))
  10602. org-loop-over-headlines-in-active-region)
  10603. (org-map-entries
  10604. `(org-todo ,arg)
  10605. org-loop-over-headlines-in-active-region
  10606. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10607. (if (equal arg '(16)) (setq arg 'nextset))
  10608. (let ((org-blocker-hook org-blocker-hook)
  10609. commentp
  10610. case-fold-search)
  10611. (when (equal arg '(64))
  10612. (setq arg nil org-blocker-hook nil))
  10613. (when (and org-blocker-hook
  10614. (or org-inhibit-blocking
  10615. (org-entry-get nil "NOBLOCKING")))
  10616. (setq org-blocker-hook nil))
  10617. (save-excursion
  10618. (catch 'exit
  10619. (org-back-to-heading t)
  10620. (when (looking-at (concat "^\\*+ " org-comment-string))
  10621. (org-toggle-comment)
  10622. (setq commentp t))
  10623. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10624. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10625. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10626. (let* ((match-data (match-data))
  10627. (startpos (point-at-bol))
  10628. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10629. (org-log-done org-log-done)
  10630. (org-log-repeat org-log-repeat)
  10631. (org-todo-log-states org-todo-log-states)
  10632. (org-inhibit-logging
  10633. (if (equal arg 0)
  10634. (progn (setq arg nil) 'note) org-inhibit-logging))
  10635. (this (match-string 1))
  10636. (hl-pos (match-beginning 0))
  10637. (head (org-get-todo-sequence-head this))
  10638. (ass (assoc head org-todo-kwd-alist))
  10639. (interpret (nth 1 ass))
  10640. (done-word (nth 3 ass))
  10641. (final-done-word (nth 4 ass))
  10642. (org-last-state (or this ""))
  10643. (completion-ignore-case t)
  10644. (member (member this org-todo-keywords-1))
  10645. (tail (cdr member))
  10646. (org-state (cond
  10647. ((and org-todo-key-trigger
  10648. (or (and (equal arg '(4))
  10649. (eq org-use-fast-todo-selection 'prefix))
  10650. (and (not arg) org-use-fast-todo-selection
  10651. (not (eq org-use-fast-todo-selection
  10652. 'prefix)))))
  10653. ;; Use fast selection
  10654. (org-fast-todo-selection))
  10655. ((and (equal arg '(4))
  10656. (or (not org-use-fast-todo-selection)
  10657. (not org-todo-key-trigger)))
  10658. ;; Read a state with completion
  10659. (org-icompleting-read
  10660. "State: " (mapcar (lambda(x) (list x))
  10661. org-todo-keywords-1)
  10662. nil t))
  10663. ((eq arg 'right)
  10664. (if this
  10665. (if tail (car tail) nil)
  10666. (car org-todo-keywords-1)))
  10667. ((eq arg 'left)
  10668. (if (equal member org-todo-keywords-1)
  10669. nil
  10670. (if this
  10671. (nth (- (length org-todo-keywords-1)
  10672. (length tail) 2)
  10673. org-todo-keywords-1)
  10674. (org-last org-todo-keywords-1))))
  10675. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10676. (setq arg nil))) ; hack to fall back to cycling
  10677. (arg
  10678. ;; user or caller requests a specific state
  10679. (cond
  10680. ((equal arg "") nil)
  10681. ((eq arg 'none) nil)
  10682. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10683. ((eq arg 'nextset)
  10684. (or (car (cdr (member head org-todo-heads)))
  10685. (car org-todo-heads)))
  10686. ((eq arg 'previousset)
  10687. (let ((org-todo-heads (reverse org-todo-heads)))
  10688. (or (car (cdr (member head org-todo-heads)))
  10689. (car org-todo-heads))))
  10690. ((car (member arg org-todo-keywords-1)))
  10691. ((stringp arg)
  10692. (error "State `%s' not valid in this file" arg))
  10693. ((nth (1- (prefix-numeric-value arg))
  10694. org-todo-keywords-1))))
  10695. ((null member) (or head (car org-todo-keywords-1)))
  10696. ((equal this final-done-word) nil) ;; -> make empty
  10697. ((null tail) nil) ;; -> first entry
  10698. ((memq interpret '(type priority))
  10699. (if (eq this-command last-command)
  10700. (car tail)
  10701. (if (> (length tail) 0)
  10702. (or done-word (car org-done-keywords))
  10703. nil)))
  10704. (t
  10705. (car tail))))
  10706. (org-state (or
  10707. (run-hook-with-args-until-success
  10708. 'org-todo-get-default-hook org-state org-last-state)
  10709. org-state))
  10710. (next (if org-state (concat " " org-state " ") " "))
  10711. (change-plist (list :type 'todo-state-change :from this :to org-state
  10712. :position startpos))
  10713. dolog now-done-p)
  10714. (when org-blocker-hook
  10715. (setq org-last-todo-state-is-todo
  10716. (not (member this org-done-keywords)))
  10717. (unless (save-excursion
  10718. (save-match-data
  10719. (org-with-wide-buffer
  10720. (run-hook-with-args-until-failure
  10721. 'org-blocker-hook change-plist))))
  10722. (if (org-called-interactively-p 'interactive)
  10723. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  10724. this org-state org-block-entry-blocking)
  10725. ;; fail silently
  10726. (message "TODO state change from %s to %s blocked (by \"%s\")"
  10727. this org-state org-block-entry-blocking)
  10728. (throw 'exit nil))))
  10729. (store-match-data match-data)
  10730. (replace-match next t t)
  10731. (unless (pos-visible-in-window-p hl-pos)
  10732. (message "TODO state changed to %s" (org-trim next)))
  10733. (unless head
  10734. (setq head (org-get-todo-sequence-head org-state)
  10735. ass (assoc head org-todo-kwd-alist)
  10736. interpret (nth 1 ass)
  10737. done-word (nth 3 ass)
  10738. final-done-word (nth 4 ass)))
  10739. (when (memq arg '(nextset previousset))
  10740. (message "Keyword-Set %d/%d: %s"
  10741. (- (length org-todo-sets) -1
  10742. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10743. (length org-todo-sets)
  10744. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10745. (setq org-last-todo-state-is-todo
  10746. (not (member org-state org-done-keywords)))
  10747. (setq now-done-p (and (member org-state org-done-keywords)
  10748. (not (member this org-done-keywords))))
  10749. (and logging (org-local-logging logging))
  10750. (when (and (or org-todo-log-states org-log-done)
  10751. (not (eq org-inhibit-logging t))
  10752. (not (memq arg '(nextset previousset))))
  10753. ;; we need to look at recording a time and note
  10754. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10755. (nth 2 (assoc this org-todo-log-states))))
  10756. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10757. (setq dolog 'time))
  10758. (when (and org-state
  10759. (member org-state org-not-done-keywords)
  10760. (not (member this org-not-done-keywords)))
  10761. ;; This is now a todo state and was not one before
  10762. ;; If there was a CLOSED time stamp, get rid of it.
  10763. (org-add-planning-info nil nil 'closed))
  10764. (when (and now-done-p org-log-done)
  10765. ;; It is now done, and it was not done before
  10766. (org-add-planning-info 'closed (org-current-effective-time))
  10767. (if (and (not dolog) (eq 'note org-log-done))
  10768. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10769. (when (and org-state dolog)
  10770. ;; This is a non-nil state, and we need to log it
  10771. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10772. ;; Fixup tag positioning
  10773. (org-todo-trigger-tag-changes org-state)
  10774. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10775. (when org-provide-todo-statistics
  10776. (org-update-parent-todo-statistics))
  10777. (run-hooks 'org-after-todo-state-change-hook)
  10778. (if (and arg (not (member org-state org-done-keywords)))
  10779. (setq head (org-get-todo-sequence-head org-state)))
  10780. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10781. ;; Do we need to trigger a repeat?
  10782. (when now-done-p
  10783. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10784. ;; This is for the agenda, take a snapshot of the headline.
  10785. (save-match-data
  10786. (setq org-agenda-headline-snapshot-before-repeat
  10787. (org-get-heading))))
  10788. (org-auto-repeat-maybe org-state))
  10789. ;; Fixup cursor location if close to the keyword
  10790. (if (and (outline-on-heading-p)
  10791. (not (bolp))
  10792. (save-excursion (beginning-of-line 1)
  10793. (looking-at org-todo-line-regexp))
  10794. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10795. (progn
  10796. (goto-char (or (match-end 2) (match-end 1)))
  10797. (and (looking-at " ") (just-one-space))))
  10798. (when org-trigger-hook
  10799. (save-excursion
  10800. (run-hook-with-args 'org-trigger-hook change-plist)))
  10801. (when commentp (org-toggle-comment))))))))
  10802. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10803. "Block turning an entry into a TODO, using the hierarchy.
  10804. This checks whether the current task should be blocked from state
  10805. changes. Such blocking occurs when:
  10806. 1. The task has children which are not all in a completed state.
  10807. 2. A task has a parent with the property :ORDERED:, and there
  10808. are siblings prior to the current task with incomplete
  10809. status.
  10810. 3. The parent of the task is blocked because it has siblings that should
  10811. be done first, or is child of a block grandparent TODO entry."
  10812. (if (not org-enforce-todo-dependencies)
  10813. t ; if locally turned off don't block
  10814. (catch 'dont-block
  10815. ;; If this is not a todo state change, or if this entry is already DONE,
  10816. ;; do not block
  10817. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10818. (member (plist-get change-plist :from)
  10819. (cons 'done org-done-keywords))
  10820. (member (plist-get change-plist :to)
  10821. (cons 'todo org-not-done-keywords))
  10822. (not (plist-get change-plist :to)))
  10823. (throw 'dont-block t))
  10824. ;; If this task has children, and any are undone, it's blocked
  10825. (save-excursion
  10826. (org-back-to-heading t)
  10827. (let ((this-level (funcall outline-level)))
  10828. (outline-next-heading)
  10829. (let ((child-level (funcall outline-level)))
  10830. (while (and (not (eobp))
  10831. (> child-level this-level))
  10832. ;; this todo has children, check whether they are all
  10833. ;; completed
  10834. (if (and (not (org-entry-is-done-p))
  10835. (org-entry-is-todo-p))
  10836. (progn (setq org-block-entry-blocking (org-get-heading))
  10837. (throw 'dont-block nil)))
  10838. (outline-next-heading)
  10839. (setq child-level (funcall outline-level))))))
  10840. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10841. ;; any previous siblings are undone, it's blocked
  10842. (save-excursion
  10843. (org-back-to-heading t)
  10844. (let* ((pos (point))
  10845. (parent-pos (and (org-up-heading-safe) (point))))
  10846. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10847. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10848. (forward-line 1)
  10849. (re-search-forward org-not-done-heading-regexp pos t))
  10850. (setq org-block-entry-blocking (match-string 0))
  10851. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10852. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10853. (while t
  10854. (goto-char parent-pos)
  10855. (if (not (looking-at org-not-done-heading-regexp))
  10856. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10857. (setq pos (point))
  10858. (setq parent-pos (and (org-up-heading-safe) (point)))
  10859. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10860. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10861. (forward-line 1)
  10862. (re-search-forward org-not-done-heading-regexp pos t)
  10863. (setq org-block-entry-blocking (org-get-heading)))
  10864. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10865. (defcustom org-track-ordered-property-with-tag nil
  10866. "Should the ORDERED property also be shown as a tag?
  10867. The ORDERED property decides if an entry should require subtasks to be
  10868. completed in sequence. Since a property is not very visible, setting
  10869. this option means that toggling the ORDERED property with the command
  10870. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10871. not relevant for the behavior, but it makes things more visible.
  10872. Note that toggling the tag with tags commands will not change the property
  10873. and therefore not influence behavior!
  10874. This can be t, meaning the tag ORDERED should be used, It can also be a
  10875. string to select a different tag for this task."
  10876. :group 'org-todo
  10877. :type '(choice
  10878. (const :tag "No tracking" nil)
  10879. (const :tag "Track with ORDERED tag" t)
  10880. (string :tag "Use other tag")))
  10881. (defun org-toggle-ordered-property ()
  10882. "Toggle the ORDERED property of the current entry.
  10883. For better visibility, you can track the value of this property with a tag.
  10884. See variable `org-track-ordered-property-with-tag'."
  10885. (interactive)
  10886. (let* ((t1 org-track-ordered-property-with-tag)
  10887. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10888. (save-excursion
  10889. (org-back-to-heading)
  10890. (if (org-entry-get nil "ORDERED")
  10891. (progn
  10892. (org-delete-property "ORDERED")
  10893. (and tag (org-toggle-tag tag 'off))
  10894. (message "Subtasks can be completed in arbitrary order"))
  10895. (org-entry-put nil "ORDERED" "t")
  10896. (and tag (org-toggle-tag tag 'on))
  10897. (message "Subtasks must be completed in sequence")))))
  10898. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10899. (defun org-block-todo-from-checkboxes (change-plist)
  10900. "Block turning an entry into a TODO, using checkboxes.
  10901. This checks whether the current task should be blocked from state
  10902. changes because there are unchecked boxes in this entry."
  10903. (if (not org-enforce-todo-checkbox-dependencies)
  10904. t ; if locally turned off don't block
  10905. (catch 'dont-block
  10906. ;; If this is not a todo state change, or if this entry is already DONE,
  10907. ;; do not block
  10908. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10909. (member (plist-get change-plist :from)
  10910. (cons 'done org-done-keywords))
  10911. (member (plist-get change-plist :to)
  10912. (cons 'todo org-not-done-keywords))
  10913. (not (plist-get change-plist :to)))
  10914. (throw 'dont-block t))
  10915. ;; If this task has checkboxes that are not checked, it's blocked
  10916. (save-excursion
  10917. (org-back-to-heading t)
  10918. (let ((beg (point)) end)
  10919. (outline-next-heading)
  10920. (setq end (point))
  10921. (goto-char beg)
  10922. (if (org-list-search-forward
  10923. (concat (org-item-beginning-re)
  10924. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10925. "\\[[- ]\\]")
  10926. end t)
  10927. (progn
  10928. (if (boundp 'org-blocked-by-checkboxes)
  10929. (setq org-blocked-by-checkboxes t))
  10930. (throw 'dont-block nil)))))
  10931. t))) ; do not block
  10932. (defun org-entry-blocked-p ()
  10933. "Is the current entry blocked?"
  10934. (org-with-buffer-modified-unmodified
  10935. (if (org-entry-get nil "NOBLOCKING")
  10936. nil ;; Never block this entry
  10937. (not
  10938. (run-hook-with-args-until-failure
  10939. 'org-blocker-hook
  10940. (list :type 'todo-state-change
  10941. :position (point)
  10942. :from 'todo
  10943. :to 'done))))))
  10944. (defun org-update-statistics-cookies (all)
  10945. "Update the statistics cookie, either from TODO or from checkboxes.
  10946. This should be called with the cursor in a line with a statistics cookie."
  10947. (interactive "P")
  10948. (if all
  10949. (progn
  10950. (org-update-checkbox-count 'all)
  10951. (org-map-entries 'org-update-parent-todo-statistics))
  10952. (if (not (org-at-heading-p))
  10953. (org-update-checkbox-count)
  10954. (let ((pos (point-marker))
  10955. end l1 l2)
  10956. (ignore-errors (org-back-to-heading t))
  10957. (if (not (org-at-heading-p))
  10958. (org-update-checkbox-count)
  10959. (setq l1 (org-outline-level))
  10960. (setq end (save-excursion
  10961. (outline-next-heading)
  10962. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10963. (point)))
  10964. (if (and (save-excursion
  10965. (re-search-forward
  10966. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10967. (not (save-excursion (re-search-forward
  10968. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10969. (org-update-checkbox-count)
  10970. (if (and l2 (> l2 l1))
  10971. (progn
  10972. (goto-char end)
  10973. (org-update-parent-todo-statistics))
  10974. (goto-char pos)
  10975. (beginning-of-line 1)
  10976. (while (re-search-forward
  10977. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10978. (point-at-eol) t)
  10979. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10980. (goto-char pos)
  10981. (move-marker pos nil)))))
  10982. (defvar org-entry-property-inherited-from) ;; defined below
  10983. (defun org-update-parent-todo-statistics ()
  10984. "Update any statistics cookie in the parent of the current headline.
  10985. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10986. the entire subtree and this will travel up the hierarchy and update
  10987. statistics everywhere."
  10988. (let* ((prop (save-excursion (org-up-heading-safe)
  10989. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10990. (recursive (or (not org-hierarchical-todo-statistics)
  10991. (and prop (string-match "\\<recursive\\>" prop))))
  10992. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10993. 0))
  10994. (first t)
  10995. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10996. level ltoggle l1 new ndel
  10997. (cnt-all 0) (cnt-done 0) is-percent kwd
  10998. checkbox-beg ov ovs ove cookie-present)
  10999. (catch 'exit
  11000. (save-excursion
  11001. (beginning-of-line 1)
  11002. (setq ltoggle (funcall outline-level))
  11003. ;; Three situations are to consider:
  11004. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11005. ;; to the top-level ancestor on the headline;
  11006. ;; 2. If parent has "recursive" property, repeat up to the
  11007. ;; headline setting that property, taking inheritance into
  11008. ;; account;
  11009. ;; 3. Else, move up to direct parent and proceed only once.
  11010. (while (and (setq level (org-up-heading-safe))
  11011. (or recursive first)
  11012. (>= (point) lim))
  11013. (setq first nil cookie-present nil)
  11014. (unless (and level
  11015. (not (string-match
  11016. "\\<checkbox\\>"
  11017. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11018. "")))))
  11019. (throw 'exit nil))
  11020. (while (re-search-forward box-re (point-at-eol) t)
  11021. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11022. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11023. (save-match-data
  11024. (unless (outline-next-heading) (throw 'exit nil))
  11025. (while (and (looking-at org-complex-heading-regexp)
  11026. (> (setq l1 (length (match-string 1))) level))
  11027. (setq kwd (and (or recursive (= l1 ltoggle))
  11028. (match-string 2)))
  11029. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11030. (and (listp org-provide-todo-statistics)
  11031. (or (member kwd org-provide-todo-statistics)
  11032. (member kwd org-done-keywords))))
  11033. (setq cnt-all (1+ cnt-all))
  11034. (if (eq org-provide-todo-statistics t)
  11035. (and kwd (setq cnt-all (1+ cnt-all)))))
  11036. (and (member kwd org-done-keywords)
  11037. (setq cnt-done (1+ cnt-done)))
  11038. (outline-next-heading)))
  11039. (setq new
  11040. (if is-percent
  11041. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11042. (format "[%d/%d]" cnt-done cnt-all))
  11043. ndel (- (match-end 0) checkbox-beg))
  11044. ;; handle overlays when updating cookie from column view
  11045. (when (setq ov (car (overlays-at checkbox-beg)))
  11046. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11047. (delete-overlay ov))
  11048. (goto-char checkbox-beg)
  11049. (insert new)
  11050. (delete-region (point) (+ (point) ndel))
  11051. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11052. (when ov (move-overlay ov ovs ove)))
  11053. (when cookie-present
  11054. (run-hook-with-args 'org-after-todo-statistics-hook
  11055. cnt-done (- cnt-all cnt-done))))))
  11056. (run-hooks 'org-todo-statistics-hook)))
  11057. (defvar org-after-todo-statistics-hook nil
  11058. "Hook that is called after a TODO statistics cookie has been updated.
  11059. Each function is called with two arguments: the number of not-done entries
  11060. and the number of done entries.
  11061. For example, the following function, when added to this hook, will switch
  11062. an entry to DONE when all children are done, and back to TODO when new
  11063. entries are set to a TODO status. Note that this hook is only called
  11064. when there is a statistics cookie in the headline!
  11065. (defun org-summary-todo (n-done n-not-done)
  11066. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11067. (let (org-log-done org-log-states) ; turn off logging
  11068. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11069. ")
  11070. (defvar org-todo-statistics-hook nil
  11071. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11072. This hook runs even if there is no statistics cookie present, in which case
  11073. `org-after-todo-statistics-hook' would not run.")
  11074. (defun org-todo-trigger-tag-changes (state)
  11075. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11076. (let ((l org-todo-state-tags-triggers)
  11077. changes)
  11078. (when (or (not state) (equal state ""))
  11079. (setq changes (append changes (cdr (assoc "" l)))))
  11080. (when (and (stringp state) (> (length state) 0))
  11081. (setq changes (append changes (cdr (assoc state l)))))
  11082. (when (member state org-not-done-keywords)
  11083. (setq changes (append changes (cdr (assoc 'todo l)))))
  11084. (when (member state org-done-keywords)
  11085. (setq changes (append changes (cdr (assoc 'done l)))))
  11086. (dolist (c changes)
  11087. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11088. (defun org-local-logging (value)
  11089. "Get logging settings from a property VALUE."
  11090. (let* (words w a)
  11091. ;; directly set the variables, they are already local.
  11092. (setq org-log-done nil
  11093. org-log-repeat nil
  11094. org-todo-log-states nil)
  11095. (setq words (org-split-string value))
  11096. (while (setq w (pop words))
  11097. (cond
  11098. ((setq a (assoc w org-startup-options))
  11099. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11100. (set (nth 1 a) (nth 2 a))))
  11101. ((setq a (org-extract-log-state-settings w))
  11102. (and (member (car a) org-todo-keywords-1)
  11103. (push a org-todo-log-states)))))))
  11104. (defun org-get-todo-sequence-head (kwd)
  11105. "Return the head of the TODO sequence to which KWD belongs.
  11106. If KWD is not set, check if there is a text property remembering the
  11107. right sequence."
  11108. (let (p)
  11109. (cond
  11110. ((not kwd)
  11111. (or (get-text-property (point-at-bol) 'org-todo-head)
  11112. (progn
  11113. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11114. nil (point-at-eol)))
  11115. (get-text-property p 'org-todo-head))))
  11116. ((not (member kwd org-todo-keywords-1))
  11117. (car org-todo-keywords-1))
  11118. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11119. (defun org-fast-todo-selection ()
  11120. "Fast TODO keyword selection with single keys.
  11121. Returns the new TODO keyword, or nil if no state change should occur."
  11122. (let* ((fulltable org-todo-key-alist)
  11123. (done-keywords org-done-keywords) ;; needed for the faces.
  11124. (maxlen (apply 'max (mapcar
  11125. (lambda (x)
  11126. (if (stringp (car x)) (string-width (car x)) 0))
  11127. fulltable)))
  11128. (expert nil)
  11129. (fwidth (+ maxlen 3 1 3))
  11130. (ncol (/ (- (window-width) 4) fwidth))
  11131. tg cnt e c tbl
  11132. groups ingroup)
  11133. (save-excursion
  11134. (save-window-excursion
  11135. (if expert
  11136. (set-buffer (get-buffer-create " *Org todo*"))
  11137. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11138. (erase-buffer)
  11139. (org-set-local 'org-done-keywords done-keywords)
  11140. (setq tbl fulltable cnt 0)
  11141. (while (setq e (pop tbl))
  11142. (cond
  11143. ((equal e '(:startgroup))
  11144. (push '() groups) (setq ingroup t)
  11145. (when (not (= cnt 0))
  11146. (setq cnt 0)
  11147. (insert "\n"))
  11148. (insert "{ "))
  11149. ((equal e '(:endgroup))
  11150. (setq ingroup nil cnt 0)
  11151. (insert "}\n"))
  11152. ((equal e '(:newline))
  11153. (when (not (= cnt 0))
  11154. (setq cnt 0)
  11155. (insert "\n")
  11156. (setq e (car tbl))
  11157. (while (equal (car tbl) '(:newline))
  11158. (insert "\n")
  11159. (setq tbl (cdr tbl)))))
  11160. (t
  11161. (setq tg (car e) c (cdr e))
  11162. (if ingroup (push tg (car groups)))
  11163. (setq tg (org-add-props tg nil 'face
  11164. (org-get-todo-face tg)))
  11165. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11166. (insert "[" c "] " tg (make-string
  11167. (- fwidth 4 (length tg)) ?\ ))
  11168. (when (= (setq cnt (1+ cnt)) ncol)
  11169. (insert "\n")
  11170. (if ingroup (insert " "))
  11171. (setq cnt 0)))))
  11172. (insert "\n")
  11173. (goto-char (point-min))
  11174. (if (not expert) (org-fit-window-to-buffer))
  11175. (message "[a-z..]:Set [SPC]:clear")
  11176. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11177. (cond
  11178. ((or (= c ?\C-g)
  11179. (and (= c ?q) (not (rassoc c fulltable))))
  11180. (setq quit-flag t))
  11181. ((= c ?\ ) nil)
  11182. ((setq e (rassoc c fulltable) tg (car e))
  11183. tg)
  11184. (t (setq quit-flag t)))))))
  11185. (defun org-entry-is-todo-p ()
  11186. (member (org-get-todo-state) org-not-done-keywords))
  11187. (defun org-entry-is-done-p ()
  11188. (member (org-get-todo-state) org-done-keywords))
  11189. (defun org-get-todo-state ()
  11190. (save-excursion
  11191. (org-back-to-heading t)
  11192. (and (looking-at org-todo-line-regexp)
  11193. (match-end 2)
  11194. (match-string 2))))
  11195. (defun org-at-date-range-p (&optional inactive-ok)
  11196. "Is the cursor inside a date range?"
  11197. (interactive)
  11198. (save-excursion
  11199. (catch 'exit
  11200. (let ((pos (point)))
  11201. (skip-chars-backward "^[<\r\n")
  11202. (skip-chars-backward "<[")
  11203. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11204. (>= (match-end 0) pos)
  11205. (throw 'exit t))
  11206. (skip-chars-backward "^<[\r\n")
  11207. (skip-chars-backward "<[")
  11208. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11209. (>= (match-end 0) pos)
  11210. (throw 'exit t)))
  11211. nil)))
  11212. (defun org-get-repeat (&optional tagline)
  11213. "Check if there is a deadline/schedule with repeater in this entry."
  11214. (save-match-data
  11215. (save-excursion
  11216. (org-back-to-heading t)
  11217. (and (re-search-forward (if tagline
  11218. (concat tagline "\\s-*" org-repeat-re)
  11219. org-repeat-re)
  11220. (org-entry-end-position) t)
  11221. (match-string-no-properties 1)))))
  11222. (defvar org-last-changed-timestamp)
  11223. (defvar org-last-inserted-timestamp)
  11224. (defvar org-log-post-message)
  11225. (defvar org-log-note-purpose)
  11226. (defvar org-log-note-how)
  11227. (defvar org-log-note-extra)
  11228. (defun org-auto-repeat-maybe (done-word)
  11229. "Check if the current headline contains a repeated deadline/schedule.
  11230. If yes, set TODO state back to what it was and change the base date
  11231. of repeating deadline/scheduled time stamps to new date.
  11232. This function is run automatically after each state change to a DONE state."
  11233. ;; last-state is dynamically scoped into this function
  11234. (let* ((repeat (org-get-repeat))
  11235. (aa (assoc org-last-state org-todo-kwd-alist))
  11236. (interpret (nth 1 aa))
  11237. (head (nth 2 aa))
  11238. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11239. (msg "Entry repeats: ")
  11240. (org-log-done nil)
  11241. (org-todo-log-states nil)
  11242. re type n what ts time to-state)
  11243. (when repeat
  11244. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11245. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11246. org-todo-repeat-to-state))
  11247. (unless (and to-state (member to-state org-todo-keywords-1))
  11248. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11249. (org-todo to-state)
  11250. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11251. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11252. (org-time-stamp-format t t))))
  11253. (when org-log-repeat
  11254. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11255. (memq 'org-add-log-note post-command-hook))
  11256. ;; OK, we are already setup for some record
  11257. (if (eq org-log-repeat 'note)
  11258. ;; make sure we take a note, not only a time stamp
  11259. (setq org-log-note-how 'note))
  11260. ;; Set up for taking a record
  11261. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11262. org-last-state
  11263. 'findpos org-log-repeat)))
  11264. (org-back-to-heading t)
  11265. (org-add-planning-info nil nil 'closed)
  11266. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11267. org-deadline-time-regexp "\\)\\|\\("
  11268. org-ts-regexp "\\)"))
  11269. (while (re-search-forward
  11270. re (save-excursion (outline-next-heading) (point)) t)
  11271. (setq type (if (match-end 1) org-scheduled-string
  11272. (if (match-end 3) org-deadline-string "Plain:"))
  11273. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11274. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11275. (setq n (string-to-number (match-string 2 ts))
  11276. what (match-string 3 ts))
  11277. (if (equal what "w") (setq n (* n 7) what "d"))
  11278. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11279. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11280. ;; Preparation, see if we need to modify the start date for the change
  11281. (when (match-end 1)
  11282. (setq time (save-match-data (org-time-string-to-time ts)))
  11283. (cond
  11284. ((equal (match-string 1 ts) ".")
  11285. ;; Shift starting date to today
  11286. (org-timestamp-change
  11287. (- (org-today) (time-to-days time))
  11288. 'day))
  11289. ((equal (match-string 1 ts) "+")
  11290. (let ((nshiftmax 10) (nshift 0))
  11291. (while (or (= nshift 0)
  11292. (<= (time-to-days time)
  11293. (time-to-days (current-time))))
  11294. (when (= (incf nshift) nshiftmax)
  11295. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11296. (error "Abort")))
  11297. (org-timestamp-change n (cdr (assoc what whata)))
  11298. (org-at-timestamp-p t)
  11299. (setq ts (match-string 1))
  11300. (setq time (save-match-data (org-time-string-to-time ts)))))
  11301. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11302. ;; rematch, so that we have everything in place for the real shift
  11303. (org-at-timestamp-p t)
  11304. (setq ts (match-string 1))
  11305. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11306. (org-timestamp-change n (cdr (assoc what whata)))
  11307. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11308. (setq org-log-post-message msg)
  11309. (message "%s" msg))))
  11310. (defun org-show-todo-tree (arg)
  11311. "Make a compact tree which shows all headlines marked with TODO.
  11312. The tree will show the lines where the regexp matches, and all higher
  11313. headlines above the match.
  11314. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11315. With a numeric prefix N, construct a sparse tree for the Nth element
  11316. of `org-todo-keywords-1'."
  11317. (interactive "P")
  11318. (let ((case-fold-search nil)
  11319. (kwd-re
  11320. (cond ((null arg) org-not-done-regexp)
  11321. ((equal arg '(4))
  11322. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11323. (mapcar 'list org-todo-keywords-1))))
  11324. (concat "\\("
  11325. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11326. "\\)\\>")))
  11327. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11328. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11329. org-todo-keywords-1)))
  11330. (t (error "Invalid prefix argument: %s" arg)))))
  11331. (message "%d TODO entries found"
  11332. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11333. (defun org-deadline (&optional remove time)
  11334. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11335. With argument REMOVE, remove any deadline from the item.
  11336. With argument TIME, set the deadline at the corresponding date. TIME
  11337. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11338. (interactive "P")
  11339. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11340. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11341. 'region-start-level 'region))
  11342. org-loop-over-headlines-in-active-region)
  11343. (org-map-entries
  11344. `(org-deadline ',remove ,time)
  11345. org-loop-over-headlines-in-active-region
  11346. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11347. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11348. (repeater (and old-date
  11349. (string-match
  11350. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11351. old-date)
  11352. (match-string 1 old-date))))
  11353. (if remove
  11354. (progn
  11355. (when (and old-date org-log-redeadline)
  11356. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11357. org-log-redeadline))
  11358. (org-remove-timestamp-with-keyword org-deadline-string)
  11359. (message "Item no longer has a deadline."))
  11360. (org-add-planning-info 'deadline time 'closed)
  11361. (when (and old-date org-log-redeadline
  11362. (not (equal old-date
  11363. (substring org-last-inserted-timestamp 1 -1))))
  11364. (org-add-log-setup 'redeadline nil old-date 'findpos
  11365. org-log-redeadline))
  11366. (when repeater
  11367. (save-excursion
  11368. (org-back-to-heading t)
  11369. (when (re-search-forward (concat org-deadline-string " "
  11370. org-last-inserted-timestamp)
  11371. (save-excursion
  11372. (outline-next-heading) (point)) t)
  11373. (goto-char (1- (match-end 0)))
  11374. (insert " " repeater)
  11375. (setq org-last-inserted-timestamp
  11376. (concat (substring org-last-inserted-timestamp 0 -1)
  11377. " " repeater
  11378. (substring org-last-inserted-timestamp -1))))))
  11379. (message "Deadline on %s" org-last-inserted-timestamp)))))
  11380. (defun org-schedule (&optional remove time)
  11381. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11382. With argument REMOVE, remove any scheduling date from the item.
  11383. With argument TIME, scheduled at the corresponding date. TIME can
  11384. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11385. (interactive "P")
  11386. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11387. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11388. 'region-start-level 'region))
  11389. org-loop-over-headlines-in-active-region)
  11390. (org-map-entries
  11391. `(org-schedule ',remove ,time)
  11392. org-loop-over-headlines-in-active-region
  11393. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11394. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11395. (repeater (and old-date
  11396. (string-match
  11397. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11398. old-date)
  11399. (match-string 1 old-date))))
  11400. (if remove
  11401. (progn
  11402. (when (and old-date org-log-reschedule)
  11403. (org-add-log-setup 'delschedule nil old-date 'findpos
  11404. org-log-reschedule))
  11405. (org-remove-timestamp-with-keyword org-scheduled-string)
  11406. (message "Item is no longer scheduled."))
  11407. (org-add-planning-info 'scheduled time 'closed)
  11408. (when (and old-date org-log-reschedule
  11409. (not (equal old-date
  11410. (substring org-last-inserted-timestamp 1 -1))))
  11411. (org-add-log-setup 'reschedule nil old-date 'findpos
  11412. org-log-reschedule))
  11413. (when repeater
  11414. (save-excursion
  11415. (org-back-to-heading t)
  11416. (when (re-search-forward (concat org-scheduled-string " "
  11417. org-last-inserted-timestamp)
  11418. (save-excursion
  11419. (outline-next-heading) (point)) t)
  11420. (goto-char (1- (match-end 0)))
  11421. (insert " " repeater)
  11422. (setq org-last-inserted-timestamp
  11423. (concat (substring org-last-inserted-timestamp 0 -1)
  11424. " " repeater
  11425. (substring org-last-inserted-timestamp -1))))))
  11426. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11427. (defun org-get-scheduled-time (pom &optional inherit)
  11428. "Get the scheduled time as a time tuple, of a format suitable
  11429. for calling org-schedule with, or if there is no scheduling,
  11430. returns nil."
  11431. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11432. (when time
  11433. (apply 'encode-time (org-parse-time-string time)))))
  11434. (defun org-get-deadline-time (pom &optional inherit)
  11435. "Get the deadline as a time tuple, of a format suitable for
  11436. calling org-deadline with, or if there is no scheduling, returns
  11437. nil."
  11438. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11439. (when time
  11440. (apply 'encode-time (org-parse-time-string time)))))
  11441. (defun org-remove-timestamp-with-keyword (keyword)
  11442. "Remove all time stamps with KEYWORD in the current entry."
  11443. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11444. beg)
  11445. (save-excursion
  11446. (org-back-to-heading t)
  11447. (setq beg (point))
  11448. (outline-next-heading)
  11449. (while (re-search-backward re beg t)
  11450. (replace-match "")
  11451. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11452. (equal (char-before) ?\ ))
  11453. (backward-delete-char 1)
  11454. (if (string-match "^[ \t]*$" (buffer-substring
  11455. (point-at-bol) (point-at-eol)))
  11456. (delete-region (point-at-bol)
  11457. (min (point-max) (1+ (point-at-eol))))))))))
  11458. (defun org-add-planning-info (what &optional time &rest remove)
  11459. "Insert new timestamp with keyword in the line directly after the headline.
  11460. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11461. If non is given, the user is prompted for a date.
  11462. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11463. be removed."
  11464. (interactive)
  11465. (let (org-time-was-given org-end-time-was-given ts
  11466. end default-time default-input)
  11467. (catch 'exit
  11468. (when (and (memq what '(scheduled deadline))
  11469. (or (not time)
  11470. (and (stringp time)
  11471. (string-match "^[-+]+[0-9]" time))))
  11472. ;; Try to get a default date/time from existing timestamp
  11473. (save-excursion
  11474. (org-back-to-heading t)
  11475. (setq end (save-excursion (outline-next-heading) (point)))
  11476. (when (re-search-forward (if (eq what 'scheduled)
  11477. org-scheduled-time-regexp
  11478. org-deadline-time-regexp)
  11479. end t)
  11480. (setq ts (match-string 1)
  11481. default-time
  11482. (apply 'encode-time (org-parse-time-string ts))
  11483. default-input (and ts (org-get-compact-tod ts))))))
  11484. (when what
  11485. (setq time
  11486. (if (stringp time)
  11487. ;; This is a string (relative or absolute), set proper date
  11488. (apply 'encode-time
  11489. (org-read-date-analyze
  11490. time default-time (decode-time default-time)))
  11491. ;; If necessary, get the time from the user
  11492. (or time (org-read-date nil 'to-time nil nil
  11493. default-time default-input)))))
  11494. (when (and org-insert-labeled-timestamps-at-point
  11495. (member what '(scheduled deadline)))
  11496. (insert
  11497. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11498. (org-insert-time-stamp time org-time-was-given
  11499. nil nil nil (list org-end-time-was-given))
  11500. (setq what nil))
  11501. (save-excursion
  11502. (save-restriction
  11503. (let (col list elt ts buffer-invisibility-spec)
  11504. (org-back-to-heading t)
  11505. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11506. (goto-char (match-end 1))
  11507. (setq col (current-column))
  11508. (goto-char (match-end 0))
  11509. (if (eobp) (insert "\n") (forward-char 1))
  11510. (when (and (not what)
  11511. (not (looking-at
  11512. (concat "[ \t]*"
  11513. org-keyword-time-not-clock-regexp))))
  11514. ;; Nothing to add, nothing to remove...... :-)
  11515. (throw 'exit nil))
  11516. (if (and (not (looking-at org-outline-regexp))
  11517. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11518. "[^\r\n]*"))
  11519. (not (equal (match-string 1) org-clock-string)))
  11520. (narrow-to-region (match-beginning 0) (match-end 0))
  11521. (insert-before-markers "\n")
  11522. (backward-char 1)
  11523. (narrow-to-region (point) (point))
  11524. (and org-adapt-indentation (org-indent-to-column col)))
  11525. ;; Check if we have to remove something.
  11526. (setq list (cons what remove))
  11527. (while list
  11528. (setq elt (pop list))
  11529. (when (or (and (eq elt 'scheduled)
  11530. (re-search-forward org-scheduled-time-regexp nil t))
  11531. (and (eq elt 'deadline)
  11532. (re-search-forward org-deadline-time-regexp nil t))
  11533. (and (eq elt 'closed)
  11534. (re-search-forward org-closed-time-regexp nil t)))
  11535. (replace-match "")
  11536. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11537. (and (looking-at "[ \t]+") (replace-match ""))
  11538. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11539. (when what
  11540. (insert
  11541. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11542. (cond ((eq what 'scheduled) org-scheduled-string)
  11543. ((eq what 'deadline) org-deadline-string)
  11544. ((eq what 'closed) org-closed-string))
  11545. " ")
  11546. (setq ts (org-insert-time-stamp
  11547. time
  11548. (or org-time-was-given
  11549. (and (eq what 'closed) org-log-done-with-time))
  11550. (eq what 'closed)
  11551. nil nil (list org-end-time-was-given)))
  11552. (insert
  11553. (if (not (or (bolp) (eq (char-before) ?\ )
  11554. (memq (char-after) '(32 10))
  11555. (eobp))) " " ""))
  11556. (end-of-line 1))
  11557. (goto-char (point-min))
  11558. (widen)
  11559. (if (and (looking-at "[ \t]*\n")
  11560. (equal (char-before) ?\n))
  11561. (delete-region (1- (point)) (point-at-eol)))
  11562. ts))))))
  11563. (defvar org-log-note-marker (make-marker))
  11564. (defvar org-log-note-purpose nil)
  11565. (defvar org-log-note-state nil)
  11566. (defvar org-log-note-previous-state nil)
  11567. (defvar org-log-note-how nil)
  11568. (defvar org-log-note-extra nil)
  11569. (defvar org-log-note-window-configuration nil)
  11570. (defvar org-log-note-return-to (make-marker))
  11571. (defvar org-log-note-effective-time nil
  11572. "Remembered current time so that dynamically scoped
  11573. `org-extend-today-until' affects tha timestamps in state change
  11574. log")
  11575. (defvar org-log-post-message nil
  11576. "Message to be displayed after a log note has been stored.
  11577. The auto-repeater uses this.")
  11578. (defun org-add-note ()
  11579. "Add a note to the current entry.
  11580. This is done in the same way as adding a state change note."
  11581. (interactive)
  11582. (org-add-log-setup 'note nil nil 'findpos nil))
  11583. (defvar org-property-end-re)
  11584. (defun org-add-log-setup (&optional purpose state prev-state
  11585. findpos how extra)
  11586. "Set up the post command hook to take a note.
  11587. If this is about to TODO state change, the new state is expected in STATE.
  11588. When FINDPOS is non-nil, find the correct position for the note in
  11589. the current entry. If not, assume that it can be inserted at point.
  11590. HOW is an indicator what kind of note should be created.
  11591. EXTRA is additional text that will be inserted into the notes buffer."
  11592. (let* ((org-log-into-drawer (org-log-into-drawer))
  11593. (drawer (cond ((stringp org-log-into-drawer)
  11594. org-log-into-drawer)
  11595. (org-log-into-drawer "LOGBOOK"))))
  11596. (save-restriction
  11597. (save-excursion
  11598. (when findpos
  11599. (org-back-to-heading t)
  11600. (narrow-to-region (point) (save-excursion
  11601. (outline-next-heading) (point)))
  11602. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11603. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11604. "[^\r\n]*\\)?"))
  11605. (goto-char (match-end 0))
  11606. (cond
  11607. (drawer
  11608. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11609. nil t)
  11610. (progn
  11611. (goto-char (match-end 0))
  11612. (or org-log-states-order-reversed
  11613. (and (re-search-forward org-property-end-re nil t)
  11614. (goto-char (1- (match-beginning 0))))))
  11615. (insert "\n:" drawer ":\n:END:")
  11616. (beginning-of-line 0)
  11617. (org-indent-line)
  11618. (beginning-of-line 2)
  11619. (org-indent-line)
  11620. (end-of-line 0)))
  11621. ((and org-log-state-notes-insert-after-drawers
  11622. (save-excursion
  11623. (forward-line) (looking-at org-drawer-regexp)))
  11624. (forward-line)
  11625. (while (looking-at org-drawer-regexp)
  11626. (goto-char (match-end 0))
  11627. (re-search-forward org-property-end-re (point-max) t)
  11628. (forward-line))
  11629. (forward-line -1)))
  11630. (unless org-log-states-order-reversed
  11631. (and (= (char-after) ?\n) (forward-char 1))
  11632. (org-skip-over-state-notes)
  11633. (skip-chars-backward " \t\n\r")))
  11634. (move-marker org-log-note-marker (point))
  11635. (setq org-log-note-purpose purpose
  11636. org-log-note-state state
  11637. org-log-note-previous-state prev-state
  11638. org-log-note-how how
  11639. org-log-note-extra extra
  11640. org-log-note-effective-time (org-current-effective-time))
  11641. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11642. (defun org-skip-over-state-notes ()
  11643. "Skip past the list of State notes in an entry."
  11644. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11645. (when (ignore-errors (goto-char (org-in-item-p)))
  11646. (let* ((struct (org-list-struct))
  11647. (prevs (org-list-prevs-alist struct)))
  11648. (while (looking-at "[ \t]*- State")
  11649. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11650. (org-list-get-item-end (point) struct)))))))
  11651. (defun org-add-log-note (&optional purpose)
  11652. "Pop up a window for taking a note, and add this note later at point."
  11653. (remove-hook 'post-command-hook 'org-add-log-note)
  11654. (setq org-log-note-window-configuration (current-window-configuration))
  11655. (delete-other-windows)
  11656. (move-marker org-log-note-return-to (point))
  11657. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11658. (goto-char org-log-note-marker)
  11659. (org-switch-to-buffer-other-window "*Org Note*")
  11660. (erase-buffer)
  11661. (if (memq org-log-note-how '(time state))
  11662. (let (current-prefix-arg) (org-store-log-note))
  11663. (let ((org-inhibit-startup t)) (org-mode))
  11664. (insert (format "# Insert note for %s.
  11665. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11666. (cond
  11667. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11668. ((eq org-log-note-purpose 'done) "closed todo item")
  11669. ((eq org-log-note-purpose 'state)
  11670. (format "state change from \"%s\" to \"%s\""
  11671. (or org-log-note-previous-state "")
  11672. (or org-log-note-state "")))
  11673. ((eq org-log-note-purpose 'reschedule)
  11674. "rescheduling")
  11675. ((eq org-log-note-purpose 'delschedule)
  11676. "no longer scheduled")
  11677. ((eq org-log-note-purpose 'redeadline)
  11678. "changing deadline")
  11679. ((eq org-log-note-purpose 'deldeadline)
  11680. "removing deadline")
  11681. ((eq org-log-note-purpose 'refile)
  11682. "refiling")
  11683. ((eq org-log-note-purpose 'note)
  11684. "this entry")
  11685. (t (error "This should not happen")))))
  11686. (if org-log-note-extra (insert org-log-note-extra))
  11687. (org-set-local 'org-finish-function 'org-store-log-note)
  11688. (run-hooks 'org-log-buffer-setup-hook)))
  11689. (defvar org-note-abort nil) ; dynamically scoped
  11690. (defun org-store-log-note ()
  11691. "Finish taking a log note, and insert it to where it belongs."
  11692. (let ((txt (buffer-string)))
  11693. (kill-buffer (current-buffer))
  11694. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11695. lines ind bul)
  11696. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11697. (setq txt (replace-match "" t t txt)))
  11698. (if (string-match "\\s-+\\'" txt)
  11699. (setq txt (replace-match "" t t txt)))
  11700. (setq lines (org-split-string txt "\n"))
  11701. (when (and note (string-match "\\S-" note))
  11702. (setq note
  11703. (org-replace-escapes
  11704. note
  11705. (list (cons "%u" (user-login-name))
  11706. (cons "%U" user-full-name)
  11707. (cons "%t" (format-time-string
  11708. (org-time-stamp-format 'long 'inactive)
  11709. org-log-note-effective-time))
  11710. (cons "%T" (format-time-string
  11711. (org-time-stamp-format 'long nil)
  11712. org-log-note-effective-time))
  11713. (cons "%d" (format-time-string
  11714. (org-time-stamp-format nil 'inactive)
  11715. org-log-note-effective-time))
  11716. (cons "%D" (format-time-string
  11717. (org-time-stamp-format nil nil)
  11718. org-log-note-effective-time))
  11719. (cons "%s" (if org-log-note-state
  11720. (concat "\"" org-log-note-state "\"")
  11721. ""))
  11722. (cons "%S" (if org-log-note-previous-state
  11723. (concat "\"" org-log-note-previous-state "\"")
  11724. "\"\"")))))
  11725. (if lines (setq note (concat note " \\\\")))
  11726. (push note lines))
  11727. (when (or current-prefix-arg org-note-abort)
  11728. (when org-log-into-drawer
  11729. (org-remove-empty-drawer-at
  11730. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11731. org-log-note-marker))
  11732. (setq lines nil))
  11733. (when lines
  11734. (with-current-buffer (marker-buffer org-log-note-marker)
  11735. (save-excursion
  11736. (goto-char org-log-note-marker)
  11737. (move-marker org-log-note-marker nil)
  11738. (end-of-line 1)
  11739. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11740. (setq ind (save-excursion
  11741. (if (ignore-errors (goto-char (org-in-item-p)))
  11742. (let ((struct (org-list-struct)))
  11743. (org-list-get-ind
  11744. (org-list-get-top-point struct) struct))
  11745. (skip-chars-backward " \r\t\n")
  11746. (cond
  11747. ((and (org-at-heading-p)
  11748. org-adapt-indentation)
  11749. (1+ (org-current-level)))
  11750. ((org-at-heading-p) 0)
  11751. (t (org-get-indentation))))))
  11752. (setq bul (org-list-bullet-string "-"))
  11753. (org-indent-line-to ind)
  11754. (insert bul (pop lines))
  11755. (let ((ind-body (+ (length bul) ind)))
  11756. (while lines
  11757. (insert "\n")
  11758. (org-indent-line-to ind-body)
  11759. (insert (pop lines))))
  11760. (message "Note stored")
  11761. (org-back-to-heading t)
  11762. (org-cycle-hide-drawers 'children))))))
  11763. (set-window-configuration org-log-note-window-configuration)
  11764. (with-current-buffer (marker-buffer org-log-note-return-to)
  11765. (goto-char org-log-note-return-to))
  11766. (move-marker org-log-note-return-to nil)
  11767. (and org-log-post-message (message "%s" org-log-post-message)))
  11768. (defun org-remove-empty-drawer-at (drawer pos)
  11769. "Remove an empty drawer DRAWER at position POS.
  11770. POS may also be a marker."
  11771. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11772. (save-excursion
  11773. (save-restriction
  11774. (widen)
  11775. (goto-char pos)
  11776. (if (org-in-regexp
  11777. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11778. (replace-match ""))))))
  11779. (defvar org-ts-type nil)
  11780. (defun org-sparse-tree (&optional arg type)
  11781. "Create a sparse tree, prompt for the details.
  11782. This command can create sparse trees. You first need to select the type
  11783. of match used to create the tree:
  11784. t Show all TODO entries.
  11785. T Show entries with a specific TODO keyword.
  11786. m Show entries selected by a tags/property match.
  11787. p Enter a property name and its value (both with completion on existing
  11788. names/values) and show entries with that property.
  11789. r Show entries matching a regular expression (`/' can be used as well).
  11790. b Show deadlines and scheduled items before a date.
  11791. a Show deadlines and scheduled items after a date.
  11792. d Show deadlines due within `org-deadline-warning-days'.
  11793. D Show deadlines and scheduled items between a date range."
  11794. (interactive "P")
  11795. (let (ans kwd value ts-type)
  11796. (setq type (or type org-sparse-tree-default-date-type))
  11797. (setq org-ts-type type)
  11798. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range\n [c]ycle through date types: %s"
  11799. (cond ((eq type 'all) "all timestamps")
  11800. ((eq type 'scheduled) "only scheduled")
  11801. ((eq type 'deadline) "only deadline")
  11802. ((eq type 'active) "only active timestamps")
  11803. ((eq type 'inactive) "only inactive timestamps")
  11804. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11805. (t "scheduled/deadline")))
  11806. (setq ans (read-char-exclusive))
  11807. (cond
  11808. ((equal ans ?c)
  11809. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  11810. ((equal ans ?d)
  11811. (call-interactively 'org-check-deadlines))
  11812. ((equal ans ?b)
  11813. (call-interactively 'org-check-before-date))
  11814. ((equal ans ?a)
  11815. (call-interactively 'org-check-after-date))
  11816. ((equal ans ?D)
  11817. (call-interactively 'org-check-dates-range))
  11818. ((equal ans ?t)
  11819. (call-interactively 'org-show-todo-tree))
  11820. ((equal ans ?T)
  11821. (org-show-todo-tree '(4)))
  11822. ((member ans '(?T ?m))
  11823. (call-interactively 'org-match-sparse-tree))
  11824. ((member ans '(?p ?P))
  11825. (setq kwd (org-icompleting-read "Property: "
  11826. (mapcar 'list (org-buffer-property-keys))))
  11827. (setq value (org-icompleting-read "Value: "
  11828. (mapcar 'list (org-property-values kwd))))
  11829. (unless (string-match "\\`{.*}\\'" value)
  11830. (setq value (concat "\"" value "\"")))
  11831. (org-match-sparse-tree arg (concat kwd "=" value)))
  11832. ((member ans '(?r ?R ?/))
  11833. (call-interactively 'org-occur))
  11834. (t (error "No such sparse tree command \"%c\"" ans)))))
  11835. (defvar org-occur-highlights nil
  11836. "List of overlays used for occur matches.")
  11837. (make-variable-buffer-local 'org-occur-highlights)
  11838. (defvar org-occur-parameters nil
  11839. "Parameters of the active org-occur calls.
  11840. This is a list, each call to org-occur pushes as cons cell,
  11841. containing the regular expression and the callback, onto the list.
  11842. The list can contain several entries if `org-occur' has been called
  11843. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11844. will only contain one set of parameters. When the highlights are
  11845. removed (for example with `C-c C-c', or with the next edit (depending
  11846. on `org-remove-highlights-with-change'), this variable is emptied
  11847. as well.")
  11848. (make-variable-buffer-local 'org-occur-parameters)
  11849. (defun org-occur (regexp &optional keep-previous callback)
  11850. "Make a compact tree which shows all matches of REGEXP.
  11851. The tree will show the lines where the regexp matches, and all higher
  11852. headlines above the match. It will also show the heading after the match,
  11853. to make sure editing the matching entry is easy.
  11854. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11855. call to `org-occur' will be kept, to allow stacking of calls to this
  11856. command.
  11857. If CALLBACK is non-nil, it is a function which is called to confirm
  11858. that the match should indeed be shown."
  11859. (interactive "sRegexp: \nP")
  11860. (when (equal regexp "")
  11861. (error "Regexp cannot be empty"))
  11862. (unless keep-previous
  11863. (org-remove-occur-highlights nil nil t))
  11864. (push (cons regexp callback) org-occur-parameters)
  11865. (let ((cnt 0))
  11866. (save-excursion
  11867. (goto-char (point-min))
  11868. (if (or (not keep-previous) ; do not want to keep
  11869. (not org-occur-highlights)) ; no previous matches
  11870. ;; hide everything
  11871. (org-overview))
  11872. (while (re-search-forward regexp nil t)
  11873. (when (or (not callback)
  11874. (save-match-data (funcall callback)))
  11875. (setq cnt (1+ cnt))
  11876. (when org-highlight-sparse-tree-matches
  11877. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11878. (org-show-context 'occur-tree))))
  11879. (when org-remove-highlights-with-change
  11880. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11881. nil 'local))
  11882. (unless org-sparse-tree-open-archived-trees
  11883. (org-hide-archived-subtrees (point-min) (point-max)))
  11884. (run-hooks 'org-occur-hook)
  11885. (if (org-called-interactively-p 'interactive)
  11886. (message "%d match(es) for regexp %s" cnt regexp))
  11887. cnt))
  11888. (defun org-occur-next-match (&optional n reset)
  11889. "Function for `next-error-function' to find sparse tree matches.
  11890. N is the number of matches to move, when negative move backwards.
  11891. RESET is entirely ignored - this function always goes back to the
  11892. starting point when no match is found."
  11893. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11894. (search-func (if (< n 0)
  11895. 'previous-single-char-property-change
  11896. 'next-single-char-property-change))
  11897. (n (abs n))
  11898. (pos (point))
  11899. p1)
  11900. (catch 'exit
  11901. (while (setq p1 (funcall search-func (point) 'org-type))
  11902. (when (equal p1 limit)
  11903. (goto-char pos)
  11904. (error "No more matches"))
  11905. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11906. (setq n (1- n))
  11907. (when (= n 0)
  11908. (goto-char p1)
  11909. (throw 'exit (point))))
  11910. (goto-char p1))
  11911. (goto-char p1)
  11912. (error "No more matches"))))
  11913. (defun org-show-context (&optional key)
  11914. "Make sure point and context are visible.
  11915. How much context is shown depends upon the variables
  11916. `org-show-hierarchy-above', `org-show-following-heading',
  11917. `org-show-entry-below' and `org-show-siblings'."
  11918. (let ((heading-p (org-at-heading-p t))
  11919. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11920. (following-p (org-get-alist-option org-show-following-heading key))
  11921. (entry-p (org-get-alist-option org-show-entry-below key))
  11922. (siblings-p (org-get-alist-option org-show-siblings key)))
  11923. (catch 'exit
  11924. ;; Show heading or entry text
  11925. (if (and heading-p (not entry-p))
  11926. (org-flag-heading nil) ; only show the heading
  11927. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11928. (org-show-hidden-entry))) ; show entire entry
  11929. (when following-p
  11930. ;; Show next sibling, or heading below text
  11931. (save-excursion
  11932. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11933. (org-flag-heading nil))))
  11934. (when siblings-p (org-show-siblings))
  11935. (when hierarchy-p
  11936. ;; show all higher headings, possibly with siblings
  11937. (save-excursion
  11938. (while (and (condition-case nil
  11939. (progn (org-up-heading-all 1) t)
  11940. (error nil))
  11941. (not (bobp)))
  11942. (org-flag-heading nil)
  11943. (when siblings-p (org-show-siblings))))))))
  11944. (defvar org-reveal-start-hook nil
  11945. "Hook run before revealing a location.")
  11946. (defun org-reveal (&optional siblings)
  11947. "Show current entry, hierarchy above it, and the following headline.
  11948. This can be used to show a consistent set of context around locations
  11949. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11950. not t for the search context.
  11951. With optional argument SIBLINGS, on each level of the hierarchy all
  11952. siblings are shown. This repairs the tree structure to what it would
  11953. look like when opened with hierarchical calls to `org-cycle'.
  11954. With double optional argument \\[universal-argument] \\[universal-argument], \
  11955. go to the parent and show the
  11956. entire tree."
  11957. (interactive "P")
  11958. (run-hooks 'org-reveal-start-hook)
  11959. (let ((org-show-hierarchy-above t)
  11960. (org-show-following-heading t)
  11961. (org-show-siblings (if siblings t org-show-siblings)))
  11962. (org-show-context nil))
  11963. (when (equal siblings '(16))
  11964. (save-excursion
  11965. (when (org-up-heading-safe)
  11966. (org-show-subtree)
  11967. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11968. (defun org-highlight-new-match (beg end)
  11969. "Highlight from BEG to END and mark the highlight is an occur headline."
  11970. (let ((ov (make-overlay beg end)))
  11971. (overlay-put ov 'face 'secondary-selection)
  11972. (overlay-put ov 'org-type 'org-occur)
  11973. (push ov org-occur-highlights)))
  11974. (defun org-remove-occur-highlights (&optional beg end noremove)
  11975. "Remove the occur highlights from the buffer.
  11976. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11977. from the `before-change-functions' in the current buffer."
  11978. (interactive)
  11979. (unless org-inhibit-highlight-removal
  11980. (mapc 'delete-overlay org-occur-highlights)
  11981. (setq org-occur-highlights nil)
  11982. (setq org-occur-parameters nil)
  11983. (unless noremove
  11984. (remove-hook 'before-change-functions
  11985. 'org-remove-occur-highlights 'local))))
  11986. ;;;; Priorities
  11987. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11988. "Regular expression matching the priority indicator.")
  11989. (defvar org-remove-priority-next-time nil)
  11990. (defun org-priority-up ()
  11991. "Increase the priority of the current item."
  11992. (interactive)
  11993. (org-priority 'up))
  11994. (defun org-priority-down ()
  11995. "Decrease the priority of the current item."
  11996. (interactive)
  11997. (org-priority 'down))
  11998. (defun org-priority (&optional action show)
  11999. "Change the priority of an item.
  12000. ACTION can be `set', `up', `down', or a character."
  12001. (interactive "P")
  12002. (if (equal action '(4))
  12003. (org-show-priority)
  12004. (unless org-enable-priority-commands
  12005. (error "Priority commands are disabled"))
  12006. (setq action (or action 'set))
  12007. (let (current new news have remove)
  12008. (save-excursion
  12009. (org-back-to-heading t)
  12010. (if (looking-at org-priority-regexp)
  12011. (setq current (string-to-char (match-string 2))
  12012. have t))
  12013. (cond
  12014. ((eq action 'remove)
  12015. (setq remove t new ?\ ))
  12016. ((or (eq action 'set)
  12017. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12018. (if (not (eq action 'set))
  12019. (setq new action)
  12020. (message "Priority %c-%c, SPC to remove: "
  12021. org-highest-priority org-lowest-priority)
  12022. (save-match-data
  12023. (setq new (read-char-exclusive))))
  12024. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12025. (= (upcase org-lowest-priority) org-lowest-priority))
  12026. (setq new (upcase new)))
  12027. (cond ((equal new ?\ ) (setq remove t))
  12028. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12029. (error "Priority must be between `%c' and `%c'"
  12030. org-highest-priority org-lowest-priority))))
  12031. ((eq action 'up)
  12032. (setq new (if have
  12033. (1- current) ; normal cycling
  12034. ;; last priority was empty
  12035. (if (eq last-command this-command)
  12036. org-lowest-priority ; wrap around empty to lowest
  12037. ;; default
  12038. (if org-priority-start-cycle-with-default
  12039. org-default-priority
  12040. (1- org-default-priority))))))
  12041. ((eq action 'down)
  12042. (setq new (if have
  12043. (1+ current) ; normal cycling
  12044. ;; last priority was empty
  12045. (if (eq last-command this-command)
  12046. org-highest-priority ; wrap around empty to highest
  12047. ;; default
  12048. (if org-priority-start-cycle-with-default
  12049. org-default-priority
  12050. (1+ org-default-priority))))))
  12051. (t (error "Invalid action")))
  12052. (if (or (< (upcase new) org-highest-priority)
  12053. (> (upcase new) org-lowest-priority))
  12054. (if (and (memq action '(up down))
  12055. (not have) (not (eq last-command this-command)))
  12056. ;; `new' is from default priority
  12057. (error
  12058. "The default can not be set, see `org-default-priority' why")
  12059. ;; normal cycling: `new' is beyond highest/lowest priority
  12060. ;; and is wrapped around to the empty priority
  12061. (setq remove t)))
  12062. (setq news (format "%c" new))
  12063. (if have
  12064. (if remove
  12065. (replace-match "" t t nil 1)
  12066. (replace-match news t t nil 2))
  12067. (if remove
  12068. (error "No priority cookie found in line")
  12069. (let ((case-fold-search nil))
  12070. (looking-at org-todo-line-regexp))
  12071. (if (match-end 2)
  12072. (progn
  12073. (goto-char (match-end 2))
  12074. (insert " [#" news "]"))
  12075. (goto-char (match-beginning 3))
  12076. (insert "[#" news "] "))))
  12077. (org-preserve-lc (org-set-tags nil 'align)))
  12078. (if remove
  12079. (message "Priority removed")
  12080. (message "Priority of current item set to %s" news)))))
  12081. (defun org-show-priority ()
  12082. "Show the priority of the current item.
  12083. This priority is composed of the main priority given with the [#A] cookies,
  12084. and by additional input from the age of a schedules or deadline entry."
  12085. (interactive)
  12086. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12087. (org-get-at-bol 'priority)
  12088. (save-excursion
  12089. (save-match-data
  12090. (beginning-of-line)
  12091. (and (looking-at org-heading-regexp)
  12092. (org-get-priority (match-string 0))))))))
  12093. (message "Priority is %d" (if pri pri -1000))))
  12094. (defun org-get-priority (s)
  12095. "Find priority cookie and return priority."
  12096. (save-match-data
  12097. (if (functionp org-get-priority-function)
  12098. (funcall org-get-priority-function)
  12099. (if (not (string-match org-priority-regexp s))
  12100. (* 1000 (- org-lowest-priority org-default-priority))
  12101. (* 1000 (- org-lowest-priority
  12102. (string-to-char (match-string 2 s))))))))
  12103. ;;;; Tags
  12104. (defvar org-agenda-archives-mode)
  12105. (defvar org-map-continue-from nil
  12106. "Position from where mapping should continue.
  12107. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12108. (defvar org-scanner-tags nil
  12109. "The current tag list while the tags scanner is running.")
  12110. (defvar org-trust-scanner-tags nil
  12111. "Should `org-get-tags-at' use the tags for the scanner.
  12112. This is for internal dynamical scoping only.
  12113. When this is non-nil, the function `org-get-tags-at' will return the value
  12114. of `org-scanner-tags' instead of building the list by itself. This
  12115. can lead to large speed-ups when the tags scanner is used in a file with
  12116. many entries, and when the list of tags is retrieved, for example to
  12117. obtain a list of properties. Building the tags list for each entry in such
  12118. a file becomes an N^2 operation - but with this variable set, it scales
  12119. as N.")
  12120. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12121. "Scan headline tags with inheritance and produce output ACTION.
  12122. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12123. or `agenda' to produce an entry list for an agenda view. It can also be
  12124. a Lisp form or a function that should be called at each matched headline, in
  12125. this case the return value is a list of all return values from these calls.
  12126. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12127. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12128. only lines with a not-done TODO keyword are included in the output.
  12129. This should be the same variable that was scoped into
  12130. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12131. START-LEVEL can be a string with asterisks, reducing the scope to
  12132. headlines matching this string."
  12133. (require 'org-agenda)
  12134. (let* ((re (concat "^"
  12135. (if start-level
  12136. ;; Get the correct level to match
  12137. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12138. org-outline-regexp)
  12139. " *\\(\\<\\("
  12140. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12141. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12142. (props (list 'face 'default
  12143. 'done-face 'org-agenda-done
  12144. 'undone-face 'default
  12145. 'mouse-face 'highlight
  12146. 'org-not-done-regexp org-not-done-regexp
  12147. 'org-todo-regexp org-todo-regexp
  12148. 'org-complex-heading-regexp org-complex-heading-regexp
  12149. 'help-echo
  12150. (format "mouse-2 or RET jump to org file %s"
  12151. (abbreviate-file-name
  12152. (or (buffer-file-name (buffer-base-buffer))
  12153. (buffer-name (buffer-base-buffer)))))))
  12154. (case-fold-search nil)
  12155. (org-map-continue-from nil)
  12156. lspos tags tags-list
  12157. (tags-alist (list (cons 0 org-file-tags)))
  12158. (llast 0) rtn rtn1 level category i txt
  12159. todo marker entry priority)
  12160. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12161. (setq action (list 'lambda nil action)))
  12162. (save-excursion
  12163. (goto-char (point-min))
  12164. (when (eq action 'sparse-tree)
  12165. (org-overview)
  12166. (org-remove-occur-highlights))
  12167. (while (re-search-forward re nil t)
  12168. (setq org-map-continue-from nil)
  12169. (catch :skip
  12170. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12171. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12172. (goto-char (setq lspos (match-beginning 0)))
  12173. (setq level (org-reduced-level (org-outline-level))
  12174. category (org-get-category))
  12175. (setq i llast llast level)
  12176. ;; remove tag lists from same and sublevels
  12177. (while (>= i level)
  12178. (when (setq entry (assoc i tags-alist))
  12179. (setq tags-alist (delete entry tags-alist)))
  12180. (setq i (1- i)))
  12181. ;; add the next tags
  12182. (when tags
  12183. (setq tags (org-split-string tags ":")
  12184. tags-alist
  12185. (cons (cons level tags) tags-alist)))
  12186. ;; compile tags for current headline
  12187. (setq tags-list
  12188. (if org-use-tag-inheritance
  12189. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12190. tags)
  12191. org-scanner-tags tags-list)
  12192. (when org-use-tag-inheritance
  12193. (setcdr (car tags-alist)
  12194. (mapcar (lambda (x)
  12195. (setq x (copy-sequence x))
  12196. (org-add-prop-inherited x))
  12197. (cdar tags-alist))))
  12198. (when (and tags org-use-tag-inheritance
  12199. (or (not (eq t org-use-tag-inheritance))
  12200. org-tags-exclude-from-inheritance))
  12201. ;; selective inheritance, remove uninherited ones
  12202. (setcdr (car tags-alist)
  12203. (org-remove-uninherited-tags (cdar tags-alist))))
  12204. (when (and
  12205. ;; eval matcher only when the todo condition is OK
  12206. (and (or (not todo-only) (member todo org-not-done-keywords))
  12207. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12208. (eval matcher)))
  12209. ;; Call the skipper, but return t if it does not skip,
  12210. ;; so that the `and' form continues evaluating
  12211. (progn
  12212. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12213. t)
  12214. ;; Check if timestamps are deselecting this entry
  12215. (or (not todo-only)
  12216. (and (member todo org-not-done-keywords)
  12217. (or (not org-agenda-tags-todo-honor-ignore-options)
  12218. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12219. ;; select this headline
  12220. (cond
  12221. ((eq action 'sparse-tree)
  12222. (and org-highlight-sparse-tree-matches
  12223. (org-get-heading) (match-end 0)
  12224. (org-highlight-new-match
  12225. (match-beginning 1) (match-end 1)))
  12226. (org-show-context 'tags-tree))
  12227. ((eq action 'agenda)
  12228. (setq txt (org-agenda-format-item
  12229. ""
  12230. (concat
  12231. (if (eq org-tags-match-list-sublevels 'indented)
  12232. (make-string (1- level) ?.) "")
  12233. (org-get-heading))
  12234. level category
  12235. tags-list)
  12236. priority (org-get-priority txt))
  12237. (goto-char lspos)
  12238. (setq marker (org-agenda-new-marker))
  12239. (org-add-props txt props
  12240. 'org-marker marker 'org-hd-marker marker 'org-category category
  12241. 'todo-state todo
  12242. 'priority priority 'type "tagsmatch")
  12243. (push txt rtn))
  12244. ((functionp action)
  12245. (setq org-map-continue-from nil)
  12246. (save-excursion
  12247. (setq rtn1 (funcall action))
  12248. (push rtn1 rtn)))
  12249. (t (error "Invalid action")))
  12250. ;; if we are to skip sublevels, jump to end of subtree
  12251. (unless org-tags-match-list-sublevels
  12252. (org-end-of-subtree t)
  12253. (backward-char 1))))
  12254. ;; Get the correct position from where to continue
  12255. (if org-map-continue-from
  12256. (goto-char org-map-continue-from)
  12257. (and (= (point) lspos) (end-of-line 1)))))
  12258. (when (and (eq action 'sparse-tree)
  12259. (not org-sparse-tree-open-archived-trees))
  12260. (org-hide-archived-subtrees (point-min) (point-max)))
  12261. (nreverse rtn)))
  12262. (defun org-remove-uninherited-tags (tags)
  12263. "Remove all tags that are not inherited from the list TAGS."
  12264. (cond
  12265. ((eq org-use-tag-inheritance t)
  12266. (if org-tags-exclude-from-inheritance
  12267. (org-delete-all org-tags-exclude-from-inheritance tags)
  12268. tags))
  12269. ((not org-use-tag-inheritance) nil)
  12270. ((stringp org-use-tag-inheritance)
  12271. (delq nil (mapcar
  12272. (lambda (x)
  12273. (if (and (string-match org-use-tag-inheritance x)
  12274. (not (member x org-tags-exclude-from-inheritance)))
  12275. x nil))
  12276. tags)))
  12277. ((listp org-use-tag-inheritance)
  12278. (delq nil (mapcar
  12279. (lambda (x)
  12280. (if (member x org-use-tag-inheritance) x nil))
  12281. tags)))))
  12282. (defun org-match-sparse-tree (&optional todo-only match)
  12283. "Create a sparse tree according to tags string MATCH.
  12284. MATCH can contain positive and negative selection of tags, like
  12285. \"+WORK+URGENT-WITHBOSS\".
  12286. If optional argument TODO-ONLY is non-nil, only select lines that are
  12287. also TODO lines."
  12288. (interactive "P")
  12289. (org-agenda-prepare-buffers (list (current-buffer)))
  12290. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12291. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12292. (defvar org-cached-props nil)
  12293. (defun org-cached-entry-get (pom property)
  12294. (if (or (eq t org-use-property-inheritance)
  12295. (and (stringp org-use-property-inheritance)
  12296. (string-match org-use-property-inheritance property))
  12297. (and (listp org-use-property-inheritance)
  12298. (member property org-use-property-inheritance)))
  12299. ;; Caching is not possible, check it directly
  12300. (org-entry-get pom property 'inherit)
  12301. ;; Get all properties, so that we can do complicated checks easily
  12302. (cdr (assoc property (or org-cached-props
  12303. (setq org-cached-props
  12304. (org-entry-properties pom)))))))
  12305. (defun org-global-tags-completion-table (&optional files)
  12306. "Return the list of all tags in all agenda buffer/files.
  12307. Optional FILES argument is a list of files which can be used
  12308. instead of the agenda files."
  12309. (save-excursion
  12310. (org-uniquify
  12311. (delq nil
  12312. (apply 'append
  12313. (mapcar
  12314. (lambda (file)
  12315. (set-buffer (find-file-noselect file))
  12316. (append (org-get-buffer-tags)
  12317. (mapcar (lambda (x) (if (stringp (car-safe x))
  12318. (list (car-safe x)) nil))
  12319. org-tag-alist)))
  12320. (if (and files (car files))
  12321. files
  12322. (org-agenda-files))))))))
  12323. (defun org-make-tags-matcher (match)
  12324. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12325. The variable `todo-only' is scoped dynamically into this function.
  12326. It will be set to t if the matcher restricts matching to TODO entries,
  12327. otherwise will not be touched.
  12328. Returns a cons of the selection string MATCH and the constructed
  12329. lisp form implementing the matcher. The matcher is to be evaluated
  12330. at an Org entry, with point on the headline, and returns t if the
  12331. entry matches the selection string MATCH. The returned lisp form
  12332. references two variables with information about the entry, which
  12333. must be bound around the form's evaluation: todo, the TODO keyword
  12334. at the entry (or nil of none); and tags-list, the list of all tags
  12335. at the entry including inherited ones. Additionally, the category
  12336. of the entry (if any) must be specified as the text property
  12337. 'org-category on the headline.
  12338. See also `org-scan-tags'.
  12339. "
  12340. (declare (special todo-only))
  12341. (unless (boundp 'todo-only)
  12342. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12343. (unless match
  12344. ;; Get a new match request, with completion
  12345. (let ((org-last-tags-completion-table
  12346. (org-global-tags-completion-table)))
  12347. (setq match (org-completing-read-no-i
  12348. "Match: " 'org-tags-completion-function nil nil nil
  12349. 'org-tags-history))))
  12350. ;; Parse the string and create a lisp form
  12351. (let ((match0 match)
  12352. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12353. minus tag mm
  12354. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12355. orterms term orlist re-p str-p level-p level-op time-p
  12356. prop-p pn pv po gv rest)
  12357. (if (string-match "/+" match)
  12358. ;; match contains also a todo-matching request
  12359. (progn
  12360. (setq tagsmatch (substring match 0 (match-beginning 0))
  12361. todomatch (substring match (match-end 0)))
  12362. (if (string-match "^!" todomatch)
  12363. (setq todo-only t todomatch (substring todomatch 1)))
  12364. (if (string-match "^\\s-*$" todomatch)
  12365. (setq todomatch nil)))
  12366. ;; only matching tags
  12367. (setq tagsmatch match todomatch nil))
  12368. ;; Make the tags matcher
  12369. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12370. (setq tagsmatcher t)
  12371. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12372. (while (setq term (pop orterms))
  12373. (while (and (equal (substring term -1) "\\") orterms)
  12374. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12375. (while (string-match re term)
  12376. (setq rest (substring term (match-end 0))
  12377. minus (and (match-end 1)
  12378. (equal (match-string 1 term) "-"))
  12379. tag (save-match-data (replace-regexp-in-string
  12380. "\\\\-" "-"
  12381. (match-string 2 term)))
  12382. re-p (equal (string-to-char tag) ?{)
  12383. level-p (match-end 4)
  12384. prop-p (match-end 5)
  12385. mm (cond
  12386. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12387. (level-p
  12388. (setq level-op (org-op-to-function (match-string 3 term)))
  12389. `(,level-op level ,(string-to-number
  12390. (match-string 4 term))))
  12391. (prop-p
  12392. (setq pn (match-string 5 term)
  12393. po (match-string 6 term)
  12394. pv (match-string 7 term)
  12395. re-p (equal (string-to-char pv) ?{)
  12396. str-p (equal (string-to-char pv) ?\")
  12397. time-p (save-match-data
  12398. (string-match "^\"[[<].*[]>]\"$" pv))
  12399. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12400. (if time-p (setq pv (org-matcher-time pv)))
  12401. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12402. (cond
  12403. ((equal pn "CATEGORY")
  12404. (setq gv '(get-text-property (point) 'org-category)))
  12405. ((equal pn "TODO")
  12406. (setq gv 'todo))
  12407. (t
  12408. (setq gv `(org-cached-entry-get nil ,pn))))
  12409. (if re-p
  12410. (if (eq po 'org<>)
  12411. `(not (string-match ,pv (or ,gv "")))
  12412. `(string-match ,pv (or ,gv "")))
  12413. (if str-p
  12414. `(,po (or ,gv "") ,pv)
  12415. `(,po (string-to-number (or ,gv ""))
  12416. ,(string-to-number pv) ))))
  12417. (t `(member ,tag tags-list)))
  12418. mm (if minus (list 'not mm) mm)
  12419. term rest)
  12420. (push mm tagsmatcher))
  12421. (push (if (> (length tagsmatcher) 1)
  12422. (cons 'and tagsmatcher)
  12423. (car tagsmatcher))
  12424. orlist)
  12425. (setq tagsmatcher nil))
  12426. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12427. (setq tagsmatcher
  12428. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12429. ;; Make the todo matcher
  12430. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12431. (setq todomatcher t)
  12432. (setq orterms (org-split-string todomatch "|") orlist nil)
  12433. (while (setq term (pop orterms))
  12434. (while (string-match re term)
  12435. (setq minus (and (match-end 1)
  12436. (equal (match-string 1 term) "-"))
  12437. kwd (match-string 2 term)
  12438. re-p (equal (string-to-char kwd) ?{)
  12439. term (substring term (match-end 0))
  12440. mm (if re-p
  12441. `(string-match ,(substring kwd 1 -1) todo)
  12442. (list 'equal 'todo kwd))
  12443. mm (if minus (list 'not mm) mm))
  12444. (push mm todomatcher))
  12445. (push (if (> (length todomatcher) 1)
  12446. (cons 'and todomatcher)
  12447. (car todomatcher))
  12448. orlist)
  12449. (setq todomatcher nil))
  12450. (setq todomatcher (if (> (length orlist) 1)
  12451. (cons 'or orlist) (car orlist))))
  12452. ;; Return the string and lisp forms of the matcher
  12453. (setq matcher (if todomatcher
  12454. (list 'and tagsmatcher todomatcher)
  12455. tagsmatcher))
  12456. (when todo-only
  12457. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12458. matcher)))
  12459. (cons match0 matcher)))
  12460. (defun org-op-to-function (op &optional stringp)
  12461. "Turn an operator into the appropriate function."
  12462. (setq op
  12463. (cond
  12464. ((equal op "<" ) '(< string< org-time<))
  12465. ((equal op ">" ) '(> org-string> org-time>))
  12466. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12467. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12468. ((member op '("=" "==")) '(= string= org-time=))
  12469. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12470. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12471. (defun org<> (a b) (not (= a b)))
  12472. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12473. (defun org-string>= (a b) (not (string< a b)))
  12474. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12475. (defun org-string<> (a b) (not (string= a b)))
  12476. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12477. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12478. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12479. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12480. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12481. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12482. (defun org-2ft (s)
  12483. "Convert S to a floating point time.
  12484. If S is already a number, just return it. If it is a string, parse
  12485. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12486. (cond
  12487. ((numberp s) s)
  12488. ((stringp s)
  12489. (condition-case nil
  12490. (float-time (apply 'encode-time (org-parse-time-string s)))
  12491. (error 0.)))
  12492. (t 0.)))
  12493. (defun org-time-today ()
  12494. "Time in seconds today at 0:00.
  12495. Returns the float number of seconds since the beginning of the
  12496. epoch to the beginning of today (00:00)."
  12497. (float-time (apply 'encode-time
  12498. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12499. (defun org-matcher-time (s)
  12500. "Interpret a time comparison value."
  12501. (save-match-data
  12502. (cond
  12503. ((string= s "<now>") (float-time))
  12504. ((string= s "<today>") (org-time-today))
  12505. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12506. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12507. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12508. (+ (org-time-today)
  12509. (* (string-to-number (match-string 1 s))
  12510. (cdr (assoc (match-string 2 s)
  12511. '(("d" . 86400.0) ("w" . 604800.0)
  12512. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12513. (t (org-2ft s)))))
  12514. (defun org-match-any-p (re list)
  12515. "Does re match any element of list?"
  12516. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12517. (delq nil list))
  12518. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12519. (defvar org-tags-overlay (make-overlay 1 1))
  12520. (org-detach-overlay org-tags-overlay)
  12521. (defun org-get-local-tags-at (&optional pos)
  12522. "Get a list of tags defined in the current headline."
  12523. (org-get-tags-at pos 'local))
  12524. (defun org-get-local-tags ()
  12525. "Get a list of tags defined in the current headline."
  12526. (org-get-tags-at nil 'local))
  12527. (defun org-get-tags-at (&optional pos local)
  12528. "Get a list of all headline tags applicable at POS.
  12529. POS defaults to point. If tags are inherited, the list contains
  12530. the targets in the same sequence as the headlines appear, i.e.
  12531. the tags of the current headline come last.
  12532. When LOCAL is non-nil, only return tags from the current headline,
  12533. ignore inherited ones."
  12534. (interactive)
  12535. (if (and org-trust-scanner-tags
  12536. (or (not pos) (equal pos (point)))
  12537. (not local))
  12538. org-scanner-tags
  12539. (let (tags ltags lastpos parent)
  12540. (save-excursion
  12541. (save-restriction
  12542. (widen)
  12543. (goto-char (or pos (point)))
  12544. (save-match-data
  12545. (catch 'done
  12546. (condition-case nil
  12547. (progn
  12548. (org-back-to-heading t)
  12549. (while (not (equal lastpos (point)))
  12550. (setq lastpos (point))
  12551. (when (looking-at
  12552. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12553. (setq ltags (org-split-string
  12554. (org-match-string-no-properties 1) ":"))
  12555. (when parent
  12556. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12557. (setq tags (append
  12558. (if parent
  12559. (org-remove-uninherited-tags ltags)
  12560. ltags)
  12561. tags)))
  12562. (or org-use-tag-inheritance (throw 'done t))
  12563. (if local (throw 'done t))
  12564. (or (org-up-heading-safe) (error nil))
  12565. (setq parent t)))
  12566. (error nil)))))
  12567. (if local
  12568. tags
  12569. (reverse (delete-dups
  12570. (reverse (append
  12571. (org-remove-uninherited-tags
  12572. org-file-tags) tags)))))))))
  12573. (defun org-add-prop-inherited (s)
  12574. (add-text-properties 0 (length s) '(inherited t) s)
  12575. s)
  12576. (defun org-toggle-tag (tag &optional onoff)
  12577. "Toggle the tag TAG for the current line.
  12578. If ONOFF is `on' or `off', don't toggle but set to this state."
  12579. (let (res current)
  12580. (save-excursion
  12581. (org-back-to-heading t)
  12582. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12583. (point-at-eol) t)
  12584. (progn
  12585. (setq current (match-string 1))
  12586. (replace-match ""))
  12587. (setq current ""))
  12588. (setq current (nreverse (org-split-string current ":")))
  12589. (cond
  12590. ((eq onoff 'on)
  12591. (setq res t)
  12592. (or (member tag current) (push tag current)))
  12593. ((eq onoff 'off)
  12594. (or (not (member tag current)) (setq current (delete tag current))))
  12595. (t (if (member tag current)
  12596. (setq current (delete tag current))
  12597. (setq res t)
  12598. (push tag current))))
  12599. (end-of-line 1)
  12600. (if current
  12601. (progn
  12602. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12603. (org-set-tags nil t))
  12604. (delete-horizontal-space))
  12605. (run-hooks 'org-after-tags-change-hook))
  12606. res))
  12607. (defun org-align-tags-here (to-col)
  12608. ;; Assumes that this is a headline
  12609. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12610. (beginning-of-line 1)
  12611. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12612. (< pos (match-beginning 2)))
  12613. (progn
  12614. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12615. (goto-char (match-beginning 1))
  12616. (insert " ")
  12617. (delete-region (point) (1+ (match-beginning 2)))
  12618. (setq ncol (max (current-column)
  12619. (1+ col)
  12620. (if (> to-col 0)
  12621. to-col
  12622. (- (abs to-col) tags-l))))
  12623. (setq p (point))
  12624. (insert (make-string (- ncol (current-column)) ?\ ))
  12625. (setq ncol (current-column))
  12626. (when indent-tabs-mode (tabify p (point-at-eol)))
  12627. (org-move-to-column (min ncol col) t))
  12628. (goto-char pos))))
  12629. (defun org-set-tags-command (&optional arg just-align)
  12630. "Call the set-tags command for the current entry."
  12631. (interactive "P")
  12632. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12633. (org-set-tags arg just-align)
  12634. (save-excursion
  12635. (org-back-to-heading t)
  12636. (org-set-tags arg just-align))))
  12637. (defun org-set-tags-to (data)
  12638. "Set the tags of the current entry to DATA, replacing the current tags.
  12639. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12640. If DATA is nil or the empty string, any tags will be removed."
  12641. (interactive "sTags: ")
  12642. (setq data
  12643. (cond
  12644. ((eq data nil) "")
  12645. ((equal data "") "")
  12646. ((stringp data)
  12647. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12648. ":"))
  12649. ((listp data)
  12650. (concat ":" (mapconcat 'identity data ":") ":"))))
  12651. (when data
  12652. (save-excursion
  12653. (org-back-to-heading t)
  12654. (when (looking-at org-complex-heading-regexp)
  12655. (if (match-end 5)
  12656. (progn
  12657. (goto-char (match-beginning 5))
  12658. (insert data)
  12659. (delete-region (point) (point-at-eol))
  12660. (org-set-tags nil 'align))
  12661. (goto-char (point-at-eol))
  12662. (insert " " data)
  12663. (org-set-tags nil 'align)))
  12664. (beginning-of-line 1)
  12665. (if (looking-at ".*?\\([ \t]+\\)$")
  12666. (delete-region (match-beginning 1) (match-end 1))))))
  12667. (defun org-align-all-tags ()
  12668. "Align the tags i all headings."
  12669. (interactive)
  12670. (save-excursion
  12671. (or (ignore-errors (org-back-to-heading t))
  12672. (outline-next-heading))
  12673. (if (org-at-heading-p)
  12674. (org-set-tags t)
  12675. (message "No headings"))))
  12676. (defvar org-indent-indentation-per-level)
  12677. (defun org-set-tags (&optional arg just-align)
  12678. "Set the tags for the current headline.
  12679. With prefix ARG, realign all tags in headings in the current buffer."
  12680. (interactive "P")
  12681. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12682. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12683. 'region-start-level 'region))
  12684. org-loop-over-headlines-in-active-region)
  12685. (org-map-entries
  12686. ;; We don't use ARG and JUST-ALIGN here these args are not
  12687. ;; useful when looping over headlines
  12688. `(org-set-tags)
  12689. org-loop-over-headlines-in-active-region
  12690. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12691. (let* ((re org-outline-regexp-bol)
  12692. (current (unless arg (org-get-tags-string)))
  12693. (col (current-column))
  12694. (org-setting-tags t)
  12695. table current-tags inherited-tags ; computed below when needed
  12696. tags p0 c0 c1 rpl di tc level)
  12697. (if arg
  12698. (save-excursion
  12699. (goto-char (point-min))
  12700. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12701. (while (re-search-forward re nil t)
  12702. (org-set-tags nil t)
  12703. (end-of-line 1)))
  12704. (message "All tags realigned to column %d" org-tags-column))
  12705. (if just-align
  12706. (setq tags current)
  12707. ;; Get a new set of tags from the user
  12708. (save-excursion
  12709. (setq table (append org-tag-persistent-alist
  12710. (or org-tag-alist (org-get-buffer-tags))
  12711. (and
  12712. org-complete-tags-always-offer-all-agenda-tags
  12713. (org-global-tags-completion-table
  12714. (org-agenda-files))))
  12715. org-last-tags-completion-table table
  12716. current-tags (org-split-string current ":")
  12717. inherited-tags (nreverse
  12718. (nthcdr (length current-tags)
  12719. (nreverse (org-get-tags-at))))
  12720. tags
  12721. (if (or (eq t org-use-fast-tag-selection)
  12722. (and org-use-fast-tag-selection
  12723. (delq nil (mapcar 'cdr table))))
  12724. (org-fast-tag-selection
  12725. current-tags inherited-tags table
  12726. (if org-fast-tag-selection-include-todo
  12727. org-todo-key-alist))
  12728. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12729. (org-trim
  12730. (org-icompleting-read "Tags: "
  12731. 'org-tags-completion-function
  12732. nil nil current 'org-tags-history))))))
  12733. (while (string-match "[-+&]+" tags)
  12734. ;; No boolean logic, just a list
  12735. (setq tags (replace-match ":" t t tags))))
  12736. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12737. (if org-tags-sort-function
  12738. (setq tags (mapconcat 'identity
  12739. (sort (org-split-string
  12740. tags (org-re "[^[:alnum:]_@#%]+"))
  12741. org-tags-sort-function) ":")))
  12742. (if (string-match "\\`[\t ]*\\'" tags)
  12743. (setq tags "")
  12744. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12745. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12746. ;; Insert new tags at the correct column
  12747. (beginning-of-line 1)
  12748. (setq level (or (and (looking-at org-outline-regexp)
  12749. (- (match-end 0) (point) 1))
  12750. 1))
  12751. (cond
  12752. ((and (equal current "") (equal tags "")))
  12753. ((re-search-forward
  12754. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12755. (point-at-eol) t)
  12756. (if (equal tags "")
  12757. (setq rpl "")
  12758. (goto-char (match-beginning 0))
  12759. (setq c0 (current-column)
  12760. ;; compute offset for the case of org-indent-mode active
  12761. di (if org-indent-mode
  12762. (* (1- org-indent-indentation-per-level) (1- level))
  12763. 0)
  12764. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12765. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12766. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12767. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12768. (replace-match rpl t t)
  12769. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12770. tags)
  12771. (t (error "Tags alignment failed")))
  12772. (org-move-to-column col)
  12773. (unless just-align
  12774. (run-hooks 'org-after-tags-change-hook))))))
  12775. (defun org-change-tag-in-region (beg end tag off)
  12776. "Add or remove TAG for each entry in the region.
  12777. This works in the agenda, and also in an org-mode buffer."
  12778. (interactive
  12779. (list (region-beginning) (region-end)
  12780. (let ((org-last-tags-completion-table
  12781. (if (derived-mode-p 'org-mode)
  12782. (org-get-buffer-tags)
  12783. (org-global-tags-completion-table))))
  12784. (org-icompleting-read
  12785. "Tag: " 'org-tags-completion-function nil nil nil
  12786. 'org-tags-history))
  12787. (progn
  12788. (message "[s]et or [r]emove? ")
  12789. (equal (read-char-exclusive) ?r))))
  12790. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12791. (let ((agendap (equal major-mode 'org-agenda-mode))
  12792. l1 l2 m buf pos newhead (cnt 0))
  12793. (goto-char end)
  12794. (setq l2 (1- (org-current-line)))
  12795. (goto-char beg)
  12796. (setq l1 (org-current-line))
  12797. (loop for l from l1 to l2 do
  12798. (org-goto-line l)
  12799. (setq m (get-text-property (point) 'org-hd-marker))
  12800. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12801. (and agendap m))
  12802. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12803. pos (if agendap m (point)))
  12804. (with-current-buffer buf
  12805. (save-excursion
  12806. (save-restriction
  12807. (goto-char pos)
  12808. (setq cnt (1+ cnt))
  12809. (org-toggle-tag tag (if off 'off 'on))
  12810. (setq newhead (org-get-heading)))))
  12811. (and agendap (org-agenda-change-all-lines newhead m))))
  12812. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12813. (defun org-tags-completion-function (string predicate &optional flag)
  12814. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12815. (confirm (lambda (x) (stringp (car x)))))
  12816. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12817. (setq s1 (match-string 1 string)
  12818. s2 (match-string 2 string))
  12819. (setq s1 "" s2 string))
  12820. (cond
  12821. ((eq flag nil)
  12822. ;; try completion
  12823. (setq rtn (try-completion s2 ctable confirm))
  12824. (if (stringp rtn)
  12825. (setq rtn
  12826. (concat s1 s2 (substring rtn (length s2))
  12827. (if (and org-add-colon-after-tag-completion
  12828. (assoc rtn ctable))
  12829. ":" ""))))
  12830. rtn)
  12831. ((eq flag t)
  12832. ;; all-completions
  12833. (all-completions s2 ctable confirm)
  12834. )
  12835. ((eq flag 'lambda)
  12836. ;; exact match?
  12837. (assoc s2 ctable)))
  12838. ))
  12839. (defun org-fast-tag-insert (kwd tags face &optional end)
  12840. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12841. (insert (format "%-12s" (concat kwd ":"))
  12842. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12843. (or end "")))
  12844. (defun org-fast-tag-show-exit (flag)
  12845. (save-excursion
  12846. (org-goto-line 3)
  12847. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12848. (replace-match ""))
  12849. (when flag
  12850. (end-of-line 1)
  12851. (org-move-to-column (- (window-width) 19) t)
  12852. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12853. (defun org-set-current-tags-overlay (current prefix)
  12854. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12855. (if (featurep 'xemacs)
  12856. (org-overlay-display org-tags-overlay (concat prefix s)
  12857. 'secondary-selection)
  12858. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12859. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12860. (defvar org-last-tag-selection-key nil)
  12861. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12862. "Fast tag selection with single keys.
  12863. CURRENT is the current list of tags in the headline, INHERITED is the
  12864. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12865. possibly with grouping information. TODO-TABLE is a similar table with
  12866. TODO keywords, should these have keys assigned to them.
  12867. If the keys are nil, a-z are automatically assigned.
  12868. Returns the new tags string, or nil to not change the current settings."
  12869. (let* ((fulltable (append table todo-table))
  12870. (maxlen (apply 'max (mapcar
  12871. (lambda (x)
  12872. (if (stringp (car x)) (string-width (car x)) 0))
  12873. fulltable)))
  12874. (buf (current-buffer))
  12875. (expert (eq org-fast-tag-selection-single-key 'expert))
  12876. (buffer-tags nil)
  12877. (fwidth (+ maxlen 3 1 3))
  12878. (ncol (/ (- (window-width) 4) fwidth))
  12879. (i-face 'org-done)
  12880. (c-face 'org-todo)
  12881. tg cnt e c char c1 c2 ntable tbl rtn
  12882. ov-start ov-end ov-prefix
  12883. (exit-after-next org-fast-tag-selection-single-key)
  12884. (done-keywords org-done-keywords)
  12885. groups ingroup)
  12886. (save-excursion
  12887. (beginning-of-line 1)
  12888. (if (looking-at
  12889. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12890. (setq ov-start (match-beginning 1)
  12891. ov-end (match-end 1)
  12892. ov-prefix "")
  12893. (setq ov-start (1- (point-at-eol))
  12894. ov-end (1+ ov-start))
  12895. (skip-chars-forward "^\n\r")
  12896. (setq ov-prefix
  12897. (concat
  12898. (buffer-substring (1- (point)) (point))
  12899. (if (> (current-column) org-tags-column)
  12900. " "
  12901. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12902. (move-overlay org-tags-overlay ov-start ov-end)
  12903. (save-window-excursion
  12904. (if expert
  12905. (set-buffer (get-buffer-create " *Org tags*"))
  12906. (delete-other-windows)
  12907. (split-window-vertically)
  12908. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12909. (erase-buffer)
  12910. (org-set-local 'org-done-keywords done-keywords)
  12911. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12912. (org-fast-tag-insert "Current" current c-face "\n\n")
  12913. (org-fast-tag-show-exit exit-after-next)
  12914. (org-set-current-tags-overlay current ov-prefix)
  12915. (setq tbl fulltable char ?a cnt 0)
  12916. (while (setq e (pop tbl))
  12917. (cond
  12918. ((equal (car e) :startgroup)
  12919. (push '() groups) (setq ingroup t)
  12920. (when (not (= cnt 0))
  12921. (setq cnt 0)
  12922. (insert "\n"))
  12923. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12924. ((equal (car e) :endgroup)
  12925. (setq ingroup nil cnt 0)
  12926. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12927. ((equal e '(:newline))
  12928. (when (not (= cnt 0))
  12929. (setq cnt 0)
  12930. (insert "\n")
  12931. (setq e (car tbl))
  12932. (while (equal (car tbl) '(:newline))
  12933. (insert "\n")
  12934. (setq tbl (cdr tbl)))))
  12935. (t
  12936. (setq tg (copy-sequence (car e)) c2 nil)
  12937. (if (cdr e)
  12938. (setq c (cdr e))
  12939. ;; automatically assign a character.
  12940. (setq c1 (string-to-char
  12941. (downcase (substring
  12942. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12943. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12944. (while (or (rassoc char ntable) (rassoc char table))
  12945. (setq char (1+ char)))
  12946. (setq c2 c1))
  12947. (setq c (or c2 char)))
  12948. (if ingroup (push tg (car groups)))
  12949. (setq tg (org-add-props tg nil 'face
  12950. (cond
  12951. ((not (assoc tg table))
  12952. (org-get-todo-face tg))
  12953. ((member tg current) c-face)
  12954. ((member tg inherited) i-face))))
  12955. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12956. (insert "[" c "] " tg (make-string
  12957. (- fwidth 4 (length tg)) ?\ ))
  12958. (push (cons tg c) ntable)
  12959. (when (= (setq cnt (1+ cnt)) ncol)
  12960. (insert "\n")
  12961. (if ingroup (insert " "))
  12962. (setq cnt 0)))))
  12963. (setq ntable (nreverse ntable))
  12964. (insert "\n")
  12965. (goto-char (point-min))
  12966. (if (not expert) (org-fit-window-to-buffer))
  12967. (setq rtn
  12968. (catch 'exit
  12969. (while t
  12970. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12971. (if (not groups) "no " "")
  12972. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12973. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12974. (setq org-last-tag-selection-key c)
  12975. (cond
  12976. ((= c ?\r) (throw 'exit t))
  12977. ((= c ?!)
  12978. (setq groups (not groups))
  12979. (goto-char (point-min))
  12980. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12981. ((= c ?\C-c)
  12982. (if (not expert)
  12983. (org-fast-tag-show-exit
  12984. (setq exit-after-next (not exit-after-next)))
  12985. (setq expert nil)
  12986. (delete-other-windows)
  12987. (set-window-buffer (split-window-vertically) " *Org tags*")
  12988. (org-switch-to-buffer-other-window " *Org tags*")
  12989. (org-fit-window-to-buffer)))
  12990. ((or (= c ?\C-g)
  12991. (and (= c ?q) (not (rassoc c ntable))))
  12992. (org-detach-overlay org-tags-overlay)
  12993. (setq quit-flag t))
  12994. ((= c ?\ )
  12995. (setq current nil)
  12996. (if exit-after-next (setq exit-after-next 'now)))
  12997. ((= c ?\t)
  12998. (condition-case nil
  12999. (setq tg (org-icompleting-read
  13000. "Tag: "
  13001. (or buffer-tags
  13002. (with-current-buffer buf
  13003. (org-get-buffer-tags)))))
  13004. (quit (setq tg "")))
  13005. (when (string-match "\\S-" tg)
  13006. (add-to-list 'buffer-tags (list tg))
  13007. (if (member tg current)
  13008. (setq current (delete tg current))
  13009. (push tg current)))
  13010. (if exit-after-next (setq exit-after-next 'now)))
  13011. ((setq e (rassoc c todo-table) tg (car e))
  13012. (with-current-buffer buf
  13013. (save-excursion (org-todo tg)))
  13014. (if exit-after-next (setq exit-after-next 'now)))
  13015. ((setq e (rassoc c ntable) tg (car e))
  13016. (if (member tg current)
  13017. (setq current (delete tg current))
  13018. (loop for g in groups do
  13019. (if (member tg g)
  13020. (mapc (lambda (x)
  13021. (setq current (delete x current)))
  13022. g)))
  13023. (push tg current))
  13024. (if exit-after-next (setq exit-after-next 'now))))
  13025. ;; Create a sorted list
  13026. (setq current
  13027. (sort current
  13028. (lambda (a b)
  13029. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13030. (if (eq exit-after-next 'now) (throw 'exit t))
  13031. (goto-char (point-min))
  13032. (beginning-of-line 2)
  13033. (delete-region (point) (point-at-eol))
  13034. (org-fast-tag-insert "Current" current c-face)
  13035. (org-set-current-tags-overlay current ov-prefix)
  13036. (while (re-search-forward
  13037. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13038. (setq tg (match-string 1))
  13039. (add-text-properties
  13040. (match-beginning 1) (match-end 1)
  13041. (list 'face
  13042. (cond
  13043. ((member tg current) c-face)
  13044. ((member tg inherited) i-face)
  13045. (t (get-text-property (match-beginning 1) 'face))))))
  13046. (goto-char (point-min)))))
  13047. (org-detach-overlay org-tags-overlay)
  13048. (if rtn
  13049. (mapconcat 'identity current ":")
  13050. nil))))
  13051. (defun org-get-tags-string ()
  13052. "Get the TAGS string in the current headline."
  13053. (unless (org-at-heading-p t)
  13054. (error "Not on a heading"))
  13055. (save-excursion
  13056. (beginning-of-line 1)
  13057. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13058. (org-match-string-no-properties 1)
  13059. "")))
  13060. (defun org-get-tags ()
  13061. "Get the list of tags specified in the current headline."
  13062. (org-split-string (org-get-tags-string) ":"))
  13063. (defun org-get-buffer-tags ()
  13064. "Get a table of all tags used in the buffer, for completion."
  13065. (let (tags)
  13066. (save-excursion
  13067. (goto-char (point-min))
  13068. (while (re-search-forward
  13069. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13070. (when (equal (char-after (point-at-bol 0)) ?*)
  13071. (mapc (lambda (x) (add-to-list 'tags x))
  13072. (org-split-string (org-match-string-no-properties 1) ":")))))
  13073. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13074. (mapcar 'list tags)))
  13075. ;;;; The mapping API
  13076. (defun org-map-entries (func &optional match scope &rest skip)
  13077. "Call FUNC at each headline selected by MATCH in SCOPE.
  13078. FUNC is a function or a lisp form. The function will be called without
  13079. arguments, with the cursor positioned at the beginning of the headline.
  13080. The return values of all calls to the function will be collected and
  13081. returned as a list.
  13082. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13083. does not need to preserve point. After evaluation, the cursor will be
  13084. moved to the end of the line (presumably of the headline of the
  13085. processed entry) and search continues from there. Under some
  13086. circumstances, this may not produce the wanted results. For example,
  13087. if you have removed (e.g. archived) the current (sub)tree it could
  13088. mean that the next entry will be skipped entirely. In such cases, you
  13089. can specify the position from where search should continue by making
  13090. FUNC set the variable `org-map-continue-from' to the desired buffer
  13091. position.
  13092. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13093. Only headlines that are matched by this query will be considered during
  13094. the iteration. When MATCH is nil or t, all headlines will be
  13095. visited by the iteration.
  13096. SCOPE determines the scope of this command. It can be any of:
  13097. nil The current buffer, respecting the restriction if any
  13098. tree The subtree started with the entry at point
  13099. region The entries within the active region, if any
  13100. region-start-level
  13101. The entries within the active region, but only those at
  13102. the same level than the first one.
  13103. file The current buffer, without restriction
  13104. file-with-archives
  13105. The current buffer, and any archives associated with it
  13106. agenda All agenda files
  13107. agenda-with-archives
  13108. All agenda files with any archive files associated with them
  13109. \(file1 file2 ...)
  13110. If this is a list, all files in the list will be scanned
  13111. The remaining args are treated as settings for the skipping facilities of
  13112. the scanner. The following items can be given here:
  13113. archive skip trees with the archive tag.
  13114. comment skip trees with the COMMENT keyword
  13115. function or Emacs Lisp form:
  13116. will be used as value for `org-agenda-skip-function', so whenever
  13117. the function returns t, FUNC will not be called for that
  13118. entry and search will continue from the point where the
  13119. function leaves it.
  13120. If your function needs to retrieve the tags including inherited tags
  13121. at the *current* entry, you can use the value of the variable
  13122. `org-scanner-tags' which will be much faster than getting the value
  13123. with `org-get-tags-at'. If your function gets properties with
  13124. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13125. to t around the call to `org-entry-properties' to get the same speedup.
  13126. Note that if your function moves around to retrieve tags and properties at
  13127. a *different* entry, you cannot use these techniques."
  13128. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13129. (not (org-region-active-p)))
  13130. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13131. (org-agenda-skip-archived-trees (memq 'archive skip))
  13132. (org-agenda-skip-comment-trees (memq 'comment skip))
  13133. (org-agenda-skip-function
  13134. (car (org-delete-all '(comment archive) skip)))
  13135. (org-tags-match-list-sublevels t)
  13136. (start-level (eq scope 'region-start-level))
  13137. matcher file res
  13138. org-todo-keywords-for-agenda
  13139. org-done-keywords-for-agenda
  13140. org-todo-keyword-alist-for-agenda
  13141. org-drawers-for-agenda
  13142. org-tag-alist-for-agenda
  13143. todo-only)
  13144. (cond
  13145. ((eq match t) (setq matcher t))
  13146. ((eq match nil) (setq matcher t))
  13147. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13148. (save-excursion
  13149. (save-restriction
  13150. (cond ((eq scope 'tree)
  13151. (org-back-to-heading t)
  13152. (org-narrow-to-subtree)
  13153. (setq scope nil))
  13154. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13155. (org-region-active-p))
  13156. ;; If needed, set start-level to a string like "2"
  13157. (when start-level
  13158. (save-excursion
  13159. (goto-char (region-beginning))
  13160. (unless (org-at-heading-p) (outline-next-heading))
  13161. (setq start-level (org-current-level))))
  13162. (narrow-to-region (region-beginning)
  13163. (save-excursion
  13164. (goto-char (region-end))
  13165. (unless (and (bolp) (org-at-heading-p))
  13166. (outline-next-heading))
  13167. (point)))
  13168. (setq scope nil)))
  13169. (if (not scope)
  13170. (progn
  13171. (org-agenda-prepare-buffers
  13172. (list (buffer-file-name (current-buffer))))
  13173. (setq res (org-scan-tags func matcher todo-only start-level)))
  13174. ;; Get the right scope
  13175. (cond
  13176. ((and scope (listp scope) (symbolp (car scope)))
  13177. (setq scope (eval scope)))
  13178. ((eq scope 'agenda)
  13179. (setq scope (org-agenda-files t)))
  13180. ((eq scope 'agenda-with-archives)
  13181. (setq scope (org-agenda-files t))
  13182. (setq scope (org-add-archive-files scope)))
  13183. ((eq scope 'file)
  13184. (setq scope (list (buffer-file-name))))
  13185. ((eq scope 'file-with-archives)
  13186. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13187. (org-agenda-prepare-buffers scope)
  13188. (while (setq file (pop scope))
  13189. (with-current-buffer (org-find-base-buffer-visiting file)
  13190. (save-excursion
  13191. (save-restriction
  13192. (widen)
  13193. (goto-char (point-min))
  13194. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13195. res)))
  13196. ;;;; Properties
  13197. ;;; Setting and retrieving properties
  13198. (defconst org-special-properties
  13199. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13200. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13201. "The special properties valid in Org-mode.
  13202. These are properties that are not defined in the property drawer,
  13203. but in some other way.")
  13204. (defconst org-default-properties
  13205. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13206. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13207. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13208. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13209. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13210. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13211. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13212. "Some properties that are used by Org-mode for various purposes.
  13213. Being in this list makes sure that they are offered for completion.")
  13214. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13215. "Regular expression matching the first line of a property drawer.")
  13216. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13217. "Regular expression matching the last line of a property drawer.")
  13218. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13219. "Regular expression matching the first line of a property drawer.")
  13220. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13221. "Regular expression matching the first line of a property drawer.")
  13222. (defconst org-property-drawer-re
  13223. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13224. org-property-end-re "\\)\n?")
  13225. "Matches an entire property drawer.")
  13226. (defconst org-clock-drawer-re
  13227. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13228. org-property-end-re "\\)\n?")
  13229. "Matches an entire clock drawer.")
  13230. (defsubst org-re-property (property)
  13231. "Return a regexp matching a PROPERTY line.
  13232. Match group 1 will be set to the value."
  13233. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13234. (defsubst org-re-property-keyword (property)
  13235. "Return a regexp matching a PROPERTY line, possibly with no
  13236. value for the property."
  13237. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13238. (defun org-property-action ()
  13239. "Do an action on properties."
  13240. (interactive)
  13241. (let (c)
  13242. (org-at-property-p)
  13243. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13244. (setq c (read-char-exclusive))
  13245. (cond
  13246. ((equal c ?s)
  13247. (call-interactively 'org-set-property))
  13248. ((equal c ?d)
  13249. (call-interactively 'org-delete-property))
  13250. ((equal c ?D)
  13251. (call-interactively 'org-delete-property-globally))
  13252. ((equal c ?c)
  13253. (call-interactively 'org-compute-property-at-point))
  13254. (t (error "No such property action %c" c)))))
  13255. (defun org-inc-effort ()
  13256. "Increment the value of the effort property in the current entry."
  13257. (interactive)
  13258. (org-set-effort nil t))
  13259. (defun org-set-effort (&optional value increment)
  13260. "Set the effort property of the current entry.
  13261. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13262. 10th allowed value.
  13263. When INCREMENT is non-nil, set the property to the next allowed value."
  13264. (interactive "P")
  13265. (if (equal value 0) (setq value 10))
  13266. (let* ((completion-ignore-case t)
  13267. (prop org-effort-property)
  13268. (cur (org-entry-get nil prop))
  13269. (allowed (org-property-get-allowed-values nil prop 'table))
  13270. (existing (mapcar 'list (org-property-values prop)))
  13271. rpl
  13272. (val (cond
  13273. ((stringp value) value)
  13274. ((and allowed (integerp value))
  13275. (or (car (nth (1- value) allowed))
  13276. (car (org-last allowed))))
  13277. ((and allowed increment)
  13278. (or (caadr (member (list cur) allowed))
  13279. (error "Allowed effort values are not set")))
  13280. (allowed
  13281. (message "Select 1-9,0, [RET%s]: %s"
  13282. (if cur (concat "=" cur) "")
  13283. (mapconcat 'car allowed " "))
  13284. (setq rpl (read-char-exclusive))
  13285. (if (equal rpl ?\r)
  13286. cur
  13287. (setq rpl (- rpl ?0))
  13288. (if (equal rpl 0) (setq rpl 10))
  13289. (if (and (> rpl 0) (<= rpl (length allowed)))
  13290. (car (nth (1- rpl) allowed))
  13291. (org-completing-read "Effort: " allowed nil))))
  13292. (t
  13293. (let (org-completion-use-ido org-completion-use-iswitchb)
  13294. (org-completing-read
  13295. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13296. (concat "[" cur "]") "")
  13297. ": ")
  13298. existing nil nil "" nil cur))))))
  13299. (unless (equal (org-entry-get nil prop) val)
  13300. (org-entry-put nil prop val))
  13301. (save-excursion
  13302. (org-back-to-heading t)
  13303. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13304. (message "%s is now %s" prop val)))
  13305. (defun org-at-property-p ()
  13306. "Is cursor inside a property drawer?"
  13307. (save-excursion
  13308. (beginning-of-line 1)
  13309. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13310. (save-match-data ;; Used by calling procedures
  13311. (let ((p (point))
  13312. (range (unless (org-before-first-heading-p)
  13313. (org-get-property-block))))
  13314. (and range (<= (car range) p) (< p (cdr range))))))))
  13315. (defun org-get-property-block (&optional beg end force)
  13316. "Return the (beg . end) range of the body of the property drawer.
  13317. BEG and END are the beginning and end of the current subtree, or of
  13318. the part before the first headline. If they are not given, they will
  13319. be found. If the drawer does not exist and FORCE is non-nil, create
  13320. the drawer."
  13321. (catch 'exit
  13322. (save-excursion
  13323. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13324. (progn (org-back-to-heading t) (point))))
  13325. (end (or end (and (not (outline-next-heading)) (point-max))
  13326. (point))))
  13327. (goto-char beg)
  13328. (if (re-search-forward org-property-start-re end t)
  13329. (setq beg (1+ (match-end 0)))
  13330. (if force
  13331. (save-excursion
  13332. (org-insert-property-drawer)
  13333. (setq end (progn (outline-next-heading) (point))))
  13334. (throw 'exit nil))
  13335. (goto-char beg)
  13336. (if (re-search-forward org-property-start-re end t)
  13337. (setq beg (1+ (match-end 0)))))
  13338. (if (re-search-forward org-property-end-re end t)
  13339. (setq end (match-beginning 0))
  13340. (or force (throw 'exit nil))
  13341. (goto-char beg)
  13342. (setq end beg)
  13343. (org-indent-line)
  13344. (insert ":END:\n"))
  13345. (cons beg end)))))
  13346. (defun org-entry-properties (&optional pom which specific)
  13347. "Get all properties of the entry at point-or-marker POM.
  13348. This includes the TODO keyword, the tags, time strings for deadline,
  13349. scheduled, and clocking, and any additional properties defined in the
  13350. entry. The return value is an alist, keys may occur multiple times
  13351. if the property key was used several times.
  13352. POM may also be nil, in which case the current entry is used.
  13353. If WHICH is nil or `all', get all properties. If WHICH is
  13354. `special' or `standard', only get that subclass. If WHICH
  13355. is a string only get exactly this property. SPECIFIC can be a string, the
  13356. specific property we are interested in. Specifying it can speed
  13357. things up because then unnecessary parsing is avoided."
  13358. (setq which (or which 'all))
  13359. (org-with-point-at pom
  13360. (let ((clockstr (substring org-clock-string 0 -1))
  13361. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13362. (case-fold-search nil)
  13363. beg end range props sum-props key key1 value string clocksum clocksumt)
  13364. (save-excursion
  13365. (when (condition-case nil
  13366. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13367. (error nil))
  13368. (setq beg (point))
  13369. (setq sum-props (get-text-property (point) 'org-summaries))
  13370. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13371. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13372. (outline-next-heading)
  13373. (setq end (point))
  13374. (when (memq which '(all special))
  13375. ;; Get the special properties, like TODO and tags
  13376. (goto-char beg)
  13377. (when (and (or (not specific) (string= specific "TODO"))
  13378. (looking-at org-todo-line-regexp) (match-end 2))
  13379. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13380. (when (and (or (not specific) (string= specific "PRIORITY"))
  13381. (looking-at org-priority-regexp))
  13382. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13383. (when (or (not specific) (string= specific "FILE"))
  13384. (push (cons "FILE" buffer-file-name) props))
  13385. (when (and (or (not specific) (string= specific "TAGS"))
  13386. (setq value (org-get-tags-string))
  13387. (string-match "\\S-" value))
  13388. (push (cons "TAGS" value) props))
  13389. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13390. (setq value (org-get-tags-at)))
  13391. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13392. ":"))
  13393. props))
  13394. (when (or (not specific) (string= specific "BLOCKED"))
  13395. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13396. (when (or (not specific)
  13397. (member specific
  13398. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13399. "TIMESTAMP" "TIMESTAMP_IA")))
  13400. (catch 'match
  13401. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13402. (setq key (if (match-end 1)
  13403. (substring (org-match-string-no-properties 1)
  13404. 0 -1))
  13405. string (if (equal key clockstr)
  13406. (org-trim
  13407. (buffer-substring-no-properties
  13408. (match-beginning 3) (goto-char
  13409. (point-at-eol))))
  13410. (substring (org-match-string-no-properties 3)
  13411. 1 -1)))
  13412. ;; Get the correct property name from the key. This is
  13413. ;; necessary if the user has configured time keywords.
  13414. (setq key1 (concat key ":"))
  13415. (cond
  13416. ((not key)
  13417. (setq key
  13418. (if (= (char-after (match-beginning 3)) ?\[)
  13419. "TIMESTAMP_IA" "TIMESTAMP")))
  13420. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13421. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13422. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13423. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13424. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13425. (progn
  13426. (push (cons key string) props)
  13427. ;; no need to search further if match is found
  13428. (throw 'match t))
  13429. (when (or (equal key "CLOCK") (not (assoc key props)))
  13430. (push (cons key string) props)))))))
  13431. (when (memq which '(all standard))
  13432. ;; Get the standard properties, like :PROP: ...
  13433. (setq range (org-get-property-block beg end))
  13434. (when range
  13435. (goto-char (car range))
  13436. (while (re-search-forward
  13437. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13438. (cdr range) t)
  13439. (setq key (org-match-string-no-properties 1)
  13440. value (org-trim (or (org-match-string-no-properties 2) "")))
  13441. (unless (member key excluded)
  13442. (push (cons key (or value "")) props)))))
  13443. (if clocksum
  13444. (push (cons "CLOCKSUM"
  13445. (org-columns-number-to-string (/ (float clocksum) 60.)
  13446. 'add_times))
  13447. props))
  13448. (if clocksumt
  13449. (push (cons "CLOCKSUM_T"
  13450. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13451. 'add_times))
  13452. props))
  13453. (unless (assoc "CATEGORY" props)
  13454. (push (cons "CATEGORY" (org-get-category)) props))
  13455. (append sum-props (nreverse props)))))))
  13456. (defun org-entry-get (pom property &optional inherit literal-nil)
  13457. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13458. If INHERIT is non-nil and the entry does not have the property,
  13459. then also check higher levels of the hierarchy.
  13460. If INHERIT is the symbol `selective', use inheritance only if the setting
  13461. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13462. If the property is present but empty, the return value is the empty string.
  13463. If the property is not present at all, nil is returned.
  13464. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13465. do not interpret it as the list atom nil. This is used for inheritance
  13466. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13467. (org-with-point-at pom
  13468. (if (and inherit (if (eq inherit 'selective)
  13469. (org-property-inherit-p property)
  13470. t))
  13471. (org-entry-get-with-inheritance property literal-nil)
  13472. (if (member property org-special-properties)
  13473. ;; We need a special property. Use `org-entry-properties' to
  13474. ;; retrieve it, but specify the wanted property
  13475. (cdr (assoc property (org-entry-properties nil 'special property)))
  13476. (let ((range (org-get-property-block)))
  13477. (when (and range (not (eq (car range) (cdr range))))
  13478. (let* ((props (list (or (assoc property org-file-properties)
  13479. (assoc property org-global-properties)
  13480. (assoc property org-global-properties-fixed))))
  13481. (ap (lambda (key)
  13482. (when (re-search-forward
  13483. (org-re-property key) (cdr range) t)
  13484. (setq props
  13485. (org-update-property-plist
  13486. key
  13487. (if (match-end 1)
  13488. (org-match-string-no-properties 1) "")
  13489. props)))))
  13490. val)
  13491. (goto-char (car range))
  13492. (funcall ap property)
  13493. (goto-char (car range))
  13494. (while (funcall ap (concat property "+")))
  13495. (setq val (cdr (assoc property props)))
  13496. (when val (if literal-nil val (org-not-nil val))))))))))
  13497. (defun org-property-or-variable-value (var &optional inherit)
  13498. "Check if there is a property fixing the value of VAR.
  13499. If yes, return this value. If not, return the current value of the variable."
  13500. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13501. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13502. (read prop)
  13503. (symbol-value var))))
  13504. (defun org-entry-delete (pom property)
  13505. "Delete the property PROPERTY from entry at point-or-marker POM."
  13506. (org-with-point-at pom
  13507. (if (member property org-special-properties)
  13508. nil ; cannot delete these properties.
  13509. (let ((range (org-get-property-block)))
  13510. (if (and range
  13511. (goto-char (car range))
  13512. (re-search-forward
  13513. (org-re-property property)
  13514. (cdr range) t))
  13515. (progn
  13516. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13517. t)
  13518. nil)))))
  13519. ;; Multi-values properties are properties that contain multiple values
  13520. ;; These values are assumed to be single words, separated by whitespace.
  13521. (defun org-entry-add-to-multivalued-property (pom property value)
  13522. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13523. (let* ((old (org-entry-get pom property))
  13524. (values (and old (org-split-string old "[ \t]"))))
  13525. (setq value (org-entry-protect-space value))
  13526. (unless (member value values)
  13527. (setq values (cons value values))
  13528. (org-entry-put pom property
  13529. (mapconcat 'identity values " ")))))
  13530. (defun org-entry-remove-from-multivalued-property (pom property value)
  13531. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13532. (let* ((old (org-entry-get pom property))
  13533. (values (and old (org-split-string old "[ \t]"))))
  13534. (setq value (org-entry-protect-space value))
  13535. (when (member value values)
  13536. (setq values (delete value values))
  13537. (org-entry-put pom property
  13538. (mapconcat 'identity values " ")))))
  13539. (defun org-entry-member-in-multivalued-property (pom property value)
  13540. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13541. (let* ((old (org-entry-get pom property))
  13542. (values (and old (org-split-string old "[ \t]"))))
  13543. (setq value (org-entry-protect-space value))
  13544. (member value values)))
  13545. (defun org-entry-get-multivalued-property (pom property)
  13546. "Return a list of values in a multivalued property."
  13547. (let* ((value (org-entry-get pom property))
  13548. (values (and value (org-split-string value "[ \t]"))))
  13549. (mapcar 'org-entry-restore-space values)))
  13550. (defun org-entry-put-multivalued-property (pom property &rest values)
  13551. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13552. VALUES should be a list of strings. Spaces will be protected."
  13553. (org-entry-put pom property
  13554. (mapconcat 'org-entry-protect-space values " "))
  13555. (let* ((value (org-entry-get pom property))
  13556. (values (and value (org-split-string value "[ \t]"))))
  13557. (mapcar 'org-entry-restore-space values)))
  13558. (defun org-entry-protect-space (s)
  13559. "Protect spaces and newline in string S."
  13560. (while (string-match " " s)
  13561. (setq s (replace-match "%20" t t s)))
  13562. (while (string-match "\n" s)
  13563. (setq s (replace-match "%0A" t t s)))
  13564. s)
  13565. (defun org-entry-restore-space (s)
  13566. "Restore spaces and newline in string S."
  13567. (while (string-match "%20" s)
  13568. (setq s (replace-match " " t t s)))
  13569. (while (string-match "%0A" s)
  13570. (setq s (replace-match "\n" t t s)))
  13571. s)
  13572. (defvar org-entry-property-inherited-from (make-marker)
  13573. "Marker pointing to the entry from where a property was inherited.
  13574. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13575. location of the entry where the inheritance search matched. If there was
  13576. no match, the marker will point nowhere.
  13577. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13578. is set.")
  13579. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13580. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13581. The search will stop at the first ancestor which has the property defined.
  13582. If the value found is \"nil\", return nil to show that the property
  13583. should be considered as undefined (this is the meaning of nil here).
  13584. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13585. (move-marker org-entry-property-inherited-from nil)
  13586. (let (tmp)
  13587. (save-excursion
  13588. (save-restriction
  13589. (widen)
  13590. (catch 'ex
  13591. (while t
  13592. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13593. (or (ignore-errors (org-back-to-heading t))
  13594. (goto-char (point-min)))
  13595. (move-marker org-entry-property-inherited-from (point))
  13596. (throw 'ex tmp))
  13597. (or (ignore-errors (org-up-heading-safe))
  13598. (throw 'ex nil))))))
  13599. (setq tmp (or tmp
  13600. (cdr (assoc property org-file-properties))
  13601. (cdr (assoc property org-global-properties))
  13602. (cdr (assoc property org-global-properties-fixed))))
  13603. (if literal-nil tmp (org-not-nil tmp))))
  13604. (defvar org-property-changed-functions nil
  13605. "Hook called when the value of a property has changed.
  13606. Each hook function should accept two arguments, the name of the property
  13607. and the new value.")
  13608. (defun org-entry-put (pom property value)
  13609. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13610. (org-with-point-at pom
  13611. (org-back-to-heading t)
  13612. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13613. range)
  13614. (cond
  13615. ((equal property "TODO")
  13616. (when (and (stringp value) (string-match "\\S-" value)
  13617. (not (member value org-todo-keywords-1)))
  13618. (error "\"%s\" is not a valid TODO state" value))
  13619. (if (or (not value)
  13620. (not (string-match "\\S-" value)))
  13621. (setq value 'none))
  13622. (org-todo value)
  13623. (org-set-tags nil 'align))
  13624. ((equal property "PRIORITY")
  13625. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13626. (string-to-char value) ?\ ))
  13627. (org-set-tags nil 'align))
  13628. ((equal property "CLOCKSUM")
  13629. (if (not (re-search-forward
  13630. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13631. (error "Cannot find a clock log")
  13632. (goto-char (- (match-end 1) 2))
  13633. (cond
  13634. ((eq value 'earlier) (org-timestamp-down))
  13635. ((eq value 'later) (org-timestamp-up)))
  13636. (org-clock-sum-current-item)))
  13637. ((equal property "SCHEDULED")
  13638. (if (re-search-forward org-scheduled-time-regexp end t)
  13639. (cond
  13640. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13641. ((eq value 'later) (org-timestamp-change 1 'day))
  13642. (t (call-interactively 'org-schedule)))
  13643. (call-interactively 'org-schedule)))
  13644. ((equal property "DEADLINE")
  13645. (if (re-search-forward org-deadline-time-regexp end t)
  13646. (cond
  13647. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13648. ((eq value 'later) (org-timestamp-change 1 'day))
  13649. (t (call-interactively 'org-deadline)))
  13650. (call-interactively 'org-deadline)))
  13651. ((member property org-special-properties)
  13652. (error "The %s property can not yet be set with `org-entry-put'"
  13653. property))
  13654. (t ; a non-special property
  13655. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13656. (setq range (org-get-property-block beg end 'force))
  13657. (goto-char (car range))
  13658. (if (re-search-forward
  13659. (org-re-property-keyword property) (cdr range) t)
  13660. (progn
  13661. (delete-region (match-beginning 0) (match-end 0))
  13662. (goto-char (match-beginning 0)))
  13663. (goto-char (cdr range))
  13664. (insert "\n")
  13665. (backward-char 1)
  13666. (org-indent-line))
  13667. (insert ":" property ":")
  13668. (and value (insert " " value))
  13669. (org-indent-line)))))
  13670. (run-hook-with-args 'org-property-changed-functions property value)))
  13671. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13672. "Get all property keys in the current buffer.
  13673. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13674. like tags and TODO state.
  13675. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13676. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13677. and others.
  13678. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13679. formats in the current buffer."
  13680. (let (rtn range cfmt s p)
  13681. (save-excursion
  13682. (save-restriction
  13683. (widen)
  13684. (goto-char (point-min))
  13685. (while (re-search-forward org-property-start-re nil t)
  13686. (setq range (org-get-property-block))
  13687. (goto-char (car range))
  13688. (while (re-search-forward
  13689. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13690. (cdr range) t)
  13691. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13692. (outline-next-heading))))
  13693. (when include-specials
  13694. (setq rtn (append org-special-properties rtn)))
  13695. (when include-defaults
  13696. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13697. (add-to-list 'rtn org-effort-property))
  13698. (when include-columns
  13699. (save-excursion
  13700. (save-restriction
  13701. (widen)
  13702. (goto-char (point-min))
  13703. (while (re-search-forward
  13704. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13705. nil t)
  13706. (setq cfmt (match-string 2) s 0)
  13707. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13708. cfmt s)
  13709. (setq s (match-end 0)
  13710. p (match-string 1 cfmt))
  13711. (unless (or (equal p "ITEM")
  13712. (member p org-special-properties))
  13713. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13714. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13715. (defun org-property-values (key)
  13716. "Return a list of all values of property KEY in the current buffer."
  13717. (save-excursion
  13718. (save-restriction
  13719. (widen)
  13720. (goto-char (point-min))
  13721. (let ((re (org-re-property key))
  13722. values)
  13723. (while (re-search-forward re nil t)
  13724. (add-to-list 'values (org-trim (match-string 1))))
  13725. (delete "" values)))))
  13726. (defun org-insert-property-drawer ()
  13727. "Insert a property drawer into the current entry."
  13728. (org-back-to-heading t)
  13729. (looking-at org-outline-regexp)
  13730. (let ((indent (if org-adapt-indentation
  13731. (- (match-end 0) (match-beginning 0))
  13732. 0))
  13733. (beg (point))
  13734. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13735. end hiddenp)
  13736. (outline-next-heading)
  13737. (setq end (point))
  13738. (goto-char beg)
  13739. (while (re-search-forward re end t))
  13740. (setq hiddenp (outline-invisible-p))
  13741. (end-of-line 1)
  13742. (and (equal (char-after) ?\n) (forward-char 1))
  13743. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13744. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13745. ;; just skip this line
  13746. (beginning-of-line 2)
  13747. ;; Drawer start, find the end
  13748. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13749. (beginning-of-line 1)))
  13750. (org-skip-over-state-notes)
  13751. (skip-chars-backward " \t\n\r")
  13752. (if (eq (char-before) ?*) (forward-char 1))
  13753. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13754. (beginning-of-line 0)
  13755. (org-indent-to-column indent)
  13756. (beginning-of-line 2)
  13757. (org-indent-to-column indent)
  13758. (beginning-of-line 0)
  13759. (if hiddenp
  13760. (save-excursion
  13761. (org-back-to-heading t)
  13762. (hide-entry))
  13763. (org-flag-drawer t))))
  13764. (defun org-insert-drawer (&optional arg drawer)
  13765. "Insert a drawer at point.
  13766. Optional argument DRAWER, when non-nil, is a string representing
  13767. drawer's name. Otherwise, the user is prompted for a name.
  13768. If a region is active, insert the drawer around that region
  13769. instead.
  13770. Point is left between drawer's boundaries."
  13771. (interactive "P")
  13772. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13773. "LOGBOOK"))
  13774. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13775. ;; internally by Org. They are meant to be inserted
  13776. ;; automatically.
  13777. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13778. ;; Remove system drawers from list. Note: For some reason,
  13779. ;; `org-completing-read' ignores the predicate while
  13780. ;; `completing-read' handles it fine.
  13781. (drawer (if arg "PROPERTIES"
  13782. (or drawer
  13783. (completing-read
  13784. "Drawer: " org-drawers
  13785. (lambda (d) (not (member d system-drawers))))))))
  13786. (cond
  13787. ;; With C-u, fall back on `org-insert-property-drawer'
  13788. (arg (org-insert-property-drawer))
  13789. ;; With an active region, insert a drawer at point.
  13790. ((not (org-region-active-p))
  13791. (progn
  13792. (unless (bolp) (insert "\n"))
  13793. (insert (format ":%s:\n\n:END:\n" drawer))
  13794. (forward-line -2)))
  13795. ;; Otherwise, insert the drawer at point
  13796. (t
  13797. (let ((rbeg (region-beginning))
  13798. (rend (copy-marker (region-end))))
  13799. (unwind-protect
  13800. (progn
  13801. (goto-char rbeg)
  13802. (beginning-of-line)
  13803. (when (save-excursion
  13804. (re-search-forward org-outline-regexp-bol rend t))
  13805. (error "Drawers cannot contain headlines"))
  13806. ;; Position point at the beginning of the first
  13807. ;; non-blank line in region. Insert drawer's opening
  13808. ;; there, then indent it.
  13809. (org-skip-whitespace)
  13810. (beginning-of-line)
  13811. (insert ":" drawer ":\n")
  13812. (forward-line -1)
  13813. (indent-for-tab-command)
  13814. ;; Move point to the beginning of the first blank line
  13815. ;; after the last non-blank line in region. Insert
  13816. ;; drawer's closing, then indent it.
  13817. (goto-char rend)
  13818. (skip-chars-backward " \r\t\n")
  13819. (insert "\n:END:")
  13820. (deactivate-mark t)
  13821. (indent-for-tab-command)
  13822. (unless (eolp) (insert "\n")))
  13823. ;; Clear marker, whatever the outcome of insertion is.
  13824. (set-marker rend nil)))))))
  13825. (defvar org-property-set-functions-alist nil
  13826. "Property set function alist.
  13827. Each entry should have the following format:
  13828. (PROPERTY . READ-FUNCTION)
  13829. The read function will be called with the same argument as
  13830. `org-completing-read'.")
  13831. (defun org-set-property-function (property)
  13832. "Get the function that should be used to set PROPERTY.
  13833. This is computed according to `org-property-set-functions-alist'."
  13834. (or (cdr (assoc property org-property-set-functions-alist))
  13835. 'org-completing-read))
  13836. (defun org-read-property-value (property)
  13837. "Read PROPERTY value from user."
  13838. (let* ((completion-ignore-case t)
  13839. (allowed (org-property-get-allowed-values nil property 'table))
  13840. (cur (org-entry-get nil property))
  13841. (prompt (concat property " value"
  13842. (if (and cur (string-match "\\S-" cur))
  13843. (concat " [" cur "]") "") ": "))
  13844. (set-function (org-set-property-function property))
  13845. (val (if allowed
  13846. (funcall set-function prompt allowed nil
  13847. (not (get-text-property 0 'org-unrestricted
  13848. (caar allowed))))
  13849. (let (org-completion-use-ido org-completion-use-iswitchb)
  13850. (funcall set-function prompt
  13851. (mapcar 'list (org-property-values property))
  13852. nil nil "" nil cur)))))
  13853. (if (equal val "")
  13854. cur
  13855. val)))
  13856. (defvar org-last-set-property nil)
  13857. (defvar org-last-set-property-value nil)
  13858. (defun org-read-property-name ()
  13859. "Read a property name."
  13860. (let* ((completion-ignore-case t)
  13861. (keys (org-buffer-property-keys nil t t))
  13862. (default-prop (or (save-excursion
  13863. (save-match-data
  13864. (beginning-of-line)
  13865. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13866. (null (string= (match-string 1) "END"))
  13867. (match-string 1))))
  13868. org-last-set-property))
  13869. (property (org-icompleting-read
  13870. (concat "Property"
  13871. (if default-prop (concat " [" default-prop "]") "")
  13872. ": ")
  13873. (mapcar 'list keys)
  13874. nil nil nil nil
  13875. default-prop)))
  13876. (if (member property keys)
  13877. property
  13878. (or (cdr (assoc (downcase property)
  13879. (mapcar (lambda (x) (cons (downcase x) x))
  13880. keys)))
  13881. property))))
  13882. (defun org-set-property-and-value (use-last)
  13883. "Allow to set [PROPERTY]: [value] direction from prompt.
  13884. When use-default, don't even ask, just use the last
  13885. \"[PROPERTY]: [value]\" string from the history."
  13886. (interactive "P")
  13887. (let* ((completion-ignore-case t)
  13888. (pv (or (and use-last org-last-set-property-value)
  13889. (org-completing-read
  13890. "Enter a \"[Property]: [value]\" pair: "
  13891. nil nil nil nil nil
  13892. org-last-set-property-value)))
  13893. prop val)
  13894. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  13895. (setq prop (match-string 1 pv)
  13896. val (match-string 2 pv))
  13897. (org-set-property prop val))))
  13898. (defun org-set-property (property value)
  13899. "In the current entry, set PROPERTY to VALUE.
  13900. When called interactively, this will prompt for a property name, offering
  13901. completion on existing and default properties. And then it will prompt
  13902. for a value, offering completion either on allowed values (via an inherited
  13903. xxx_ALL property) or on existing values in other instances of this property
  13904. in the current file."
  13905. (interactive (list nil nil))
  13906. (let* ((property (or property (org-read-property-name)))
  13907. (value (or value (org-read-property-value property)))
  13908. (fn (cdr (assoc property org-properties-postprocess-alist))))
  13909. (setq org-last-set-property property)
  13910. (setq org-last-set-property-value (concat property ": " value))
  13911. ;; Possibly postprocess the inserted value:
  13912. (when fn (setq value (funcall fn value)))
  13913. (unless (equal (org-entry-get nil property) value)
  13914. (org-entry-put nil property value))))
  13915. (defun org-delete-property (property)
  13916. "In the current entry, delete PROPERTY."
  13917. (interactive
  13918. (let* ((completion-ignore-case t)
  13919. (prop (org-icompleting-read "Property: "
  13920. (org-entry-properties nil 'standard))))
  13921. (list prop)))
  13922. (message "Property %s %s" property
  13923. (if (org-entry-delete nil property)
  13924. "deleted"
  13925. "was not present in the entry")))
  13926. (defun org-delete-property-globally (property)
  13927. "Remove PROPERTY globally, from all entries."
  13928. (interactive
  13929. (let* ((completion-ignore-case t)
  13930. (prop (org-icompleting-read
  13931. "Globally remove property: "
  13932. (mapcar 'list (org-buffer-property-keys)))))
  13933. (list prop)))
  13934. (save-excursion
  13935. (save-restriction
  13936. (widen)
  13937. (goto-char (point-min))
  13938. (let ((cnt 0))
  13939. (while (re-search-forward
  13940. (org-re-property property)
  13941. nil t)
  13942. (setq cnt (1+ cnt))
  13943. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13944. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13945. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13946. (defun org-compute-property-at-point ()
  13947. "Compute the property at point.
  13948. This looks for an enclosing column format, extracts the operator and
  13949. then applies it to the property in the column format's scope."
  13950. (interactive)
  13951. (unless (org-at-property-p)
  13952. (error "Not at a property"))
  13953. (let ((prop (org-match-string-no-properties 2)))
  13954. (org-columns-get-format-and-top-level)
  13955. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13956. (error "No operator defined for property %s" prop))
  13957. (org-columns-compute prop)))
  13958. (defvar org-property-allowed-value-functions nil
  13959. "Hook for functions supplying allowed values for a specific property.
  13960. The functions must take a single argument, the name of the property, and
  13961. return a flat list of allowed values. If \":ETC\" is one of
  13962. the values, this means that these values are intended as defaults for
  13963. completion, but that other values should be allowed too.
  13964. The functions must return nil if they are not responsible for this
  13965. property.")
  13966. (defun org-property-get-allowed-values (pom property &optional table)
  13967. "Get allowed values for the property PROPERTY.
  13968. When TABLE is non-nil, return an alist that can directly be used for
  13969. completion."
  13970. (let (vals)
  13971. (cond
  13972. ((equal property "TODO")
  13973. (setq vals (org-with-point-at pom
  13974. (append org-todo-keywords-1 '("")))))
  13975. ((equal property "PRIORITY")
  13976. (let ((n org-lowest-priority))
  13977. (while (>= n org-highest-priority)
  13978. (push (char-to-string n) vals)
  13979. (setq n (1- n)))))
  13980. ((member property org-special-properties))
  13981. ((setq vals (run-hook-with-args-until-success
  13982. 'org-property-allowed-value-functions property)))
  13983. (t
  13984. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13985. (when (and vals (string-match "\\S-" vals))
  13986. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13987. (setq vals (mapcar (lambda (x)
  13988. (cond ((stringp x) x)
  13989. ((numberp x) (number-to-string x))
  13990. ((symbolp x) (symbol-name x))
  13991. (t "???")))
  13992. vals)))))
  13993. (when (member ":ETC" vals)
  13994. (setq vals (remove ":ETC" vals))
  13995. (org-add-props (car vals) '(org-unrestricted t)))
  13996. (if table (mapcar 'list vals) vals)))
  13997. (defun org-property-previous-allowed-value (&optional previous)
  13998. "Switch to the next allowed value for this property."
  13999. (interactive)
  14000. (org-property-next-allowed-value t))
  14001. (defun org-property-next-allowed-value (&optional previous)
  14002. "Switch to the next allowed value for this property."
  14003. (interactive)
  14004. (unless (org-at-property-p)
  14005. (error "Not at a property"))
  14006. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14007. (key (match-string 2))
  14008. (value (match-string 3))
  14009. (allowed (or (org-property-get-allowed-values (point) key)
  14010. (and (member value '("[ ]" "[-]" "[X]"))
  14011. '("[ ]" "[X]"))))
  14012. nval)
  14013. (unless allowed
  14014. (error "Allowed values for this property have not been defined"))
  14015. (if previous (setq allowed (reverse allowed)))
  14016. (if (member value allowed)
  14017. (setq nval (car (cdr (member value allowed)))))
  14018. (setq nval (or nval (car allowed)))
  14019. (if (equal nval value)
  14020. (error "Only one allowed value for this property"))
  14021. (org-at-property-p)
  14022. (replace-match (concat " :" key ": " nval) t t)
  14023. (org-indent-line)
  14024. (beginning-of-line 1)
  14025. (skip-chars-forward " \t")
  14026. (when (equal prop org-effort-property)
  14027. (save-excursion
  14028. (org-back-to-heading t)
  14029. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval)))
  14030. (run-hook-with-args 'org-property-changed-functions key nval)))
  14031. (defun org-find-olp (path &optional this-buffer)
  14032. "Return a marker pointing to the entry at outline path OLP.
  14033. If anything goes wrong, throw an error.
  14034. You can wrap this call to catch the error like this:
  14035. (condition-case msg
  14036. (org-mobile-locate-entry (match-string 4))
  14037. (error (nth 1 msg)))
  14038. The return value will then be either a string with the error message,
  14039. or a marker if everything is OK.
  14040. If THIS-BUFFER is set, the outline path does not contain a file,
  14041. only headings."
  14042. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14043. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14044. (level 1)
  14045. (lmin 1)
  14046. (lmax 1)
  14047. limit re end found pos heading cnt flevel)
  14048. (unless buffer (error "File not found :%s" file))
  14049. (with-current-buffer buffer
  14050. (save-excursion
  14051. (save-restriction
  14052. (widen)
  14053. (setq limit (point-max))
  14054. (goto-char (point-min))
  14055. (while (setq heading (pop path))
  14056. (setq re (format org-complex-heading-regexp-format
  14057. (regexp-quote heading)))
  14058. (setq cnt 0 pos (point))
  14059. (while (re-search-forward re end t)
  14060. (setq level (- (match-end 1) (match-beginning 1)))
  14061. (if (and (>= level lmin) (<= level lmax))
  14062. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14063. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14064. lmax heading))
  14065. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14066. lmax heading))
  14067. (goto-char found)
  14068. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14069. (setq end (save-excursion (org-end-of-subtree t t))))
  14070. (when (org-at-heading-p)
  14071. (point-marker)))))))
  14072. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14073. "Find node HEADING in BUFFER.
  14074. Return a marker to the heading if it was found, or nil if not.
  14075. If POS-ONLY is set, return just the position instead of a marker.
  14076. The heading text must match exact, but it may have a TODO keyword,
  14077. a priority cookie and tags in the standard locations."
  14078. (with-current-buffer (or buffer (current-buffer))
  14079. (save-excursion
  14080. (save-restriction
  14081. (widen)
  14082. (goto-char (point-min))
  14083. (let (case-fold-search)
  14084. (if (re-search-forward
  14085. (format org-complex-heading-regexp-format
  14086. (regexp-quote heading)) nil t)
  14087. (if pos-only
  14088. (match-beginning 0)
  14089. (move-marker (make-marker) (match-beginning 0)))))))))
  14090. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14091. "Find Org node headline HEADING in all .org files in directory DIR.
  14092. When the target headline is found, return a marker to this location."
  14093. (let ((files (directory-files (or dir default-directory)
  14094. nil "\\`[^.#].*\\.org\\'"))
  14095. file visiting m buffer)
  14096. (catch 'found
  14097. (while (setq file (pop files))
  14098. (message "trying %s" file)
  14099. (setq visiting (org-find-base-buffer-visiting file))
  14100. (setq buffer (or visiting (find-file-noselect file)))
  14101. (setq m (org-find-exact-headline-in-buffer
  14102. heading buffer))
  14103. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14104. (and m (throw 'found m))))))
  14105. (defun org-find-entry-with-id (ident)
  14106. "Locate the entry that contains the ID property with exact value IDENT.
  14107. IDENT can be a string, a symbol or a number, this function will search for
  14108. the string representation of it.
  14109. Return the position where this entry starts, or nil if there is no such entry."
  14110. (interactive "sID: ")
  14111. (let ((id (cond
  14112. ((stringp ident) ident)
  14113. ((symbol-name ident) (symbol-name ident))
  14114. ((numberp ident) (number-to-string ident))
  14115. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14116. (case-fold-search nil))
  14117. (save-excursion
  14118. (save-restriction
  14119. (widen)
  14120. (goto-char (point-min))
  14121. (when (re-search-forward
  14122. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14123. nil t)
  14124. (org-back-to-heading t)
  14125. (point))))))
  14126. ;;;; Timestamps
  14127. (defvar org-last-changed-timestamp nil)
  14128. (defvar org-last-inserted-timestamp nil
  14129. "The last time stamp inserted with `org-insert-time-stamp'.")
  14130. (defvar org-time-was-given) ; dynamically scoped parameter
  14131. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14132. (defvar org-ts-what) ; dynamically scoped parameter
  14133. (defun org-time-stamp (arg &optional inactive)
  14134. "Prompt for a date/time and insert a time stamp.
  14135. If the user specifies a time like HH:MM or if this command is
  14136. called with at least one prefix argument, the time stamp contains
  14137. the date and the time. Otherwise, only the date is be included.
  14138. All parts of a date not specified by the user is filled in from
  14139. the current date/time. So if you just press return without
  14140. typing anything, the time stamp will represent the current
  14141. date/time.
  14142. If there is already a timestamp at the cursor, it will be
  14143. modified.
  14144. With two universal prefix arguments, insert an active timestamp
  14145. with the current time without prompting the user."
  14146. (interactive "P")
  14147. (let* ((ts nil)
  14148. (default-time
  14149. ;; Default time is either today, or, when entering a range,
  14150. ;; the range start.
  14151. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14152. (save-excursion
  14153. (re-search-backward
  14154. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14155. (- (point) 20) t)))
  14156. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14157. (current-time)))
  14158. (default-input (and ts (org-get-compact-tod ts)))
  14159. (repeater (save-excursion
  14160. (save-match-data
  14161. (beginning-of-line)
  14162. (when (re-search-forward
  14163. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14164. (save-excursion (progn (end-of-line) (point))) t)
  14165. (match-string 0)))))
  14166. org-time-was-given org-end-time-was-given time)
  14167. (cond
  14168. ((and (org-at-timestamp-p t)
  14169. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14170. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14171. (insert "--")
  14172. (setq time (let ((this-command this-command))
  14173. (org-read-date arg 'totime nil nil
  14174. default-time default-input inactive)))
  14175. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14176. ((org-at-timestamp-p t)
  14177. (setq time (let ((this-command this-command))
  14178. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14179. (when (org-at-timestamp-p t) ; just to get the match data
  14180. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14181. (replace-match "")
  14182. (setq org-last-changed-timestamp
  14183. (org-insert-time-stamp
  14184. time (or org-time-was-given arg)
  14185. inactive nil nil (list org-end-time-was-given)))
  14186. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14187. (setq org-last-changed-timestamp
  14188. (concat (substring org-last-inserted-timestamp 0 -1)
  14189. " " repeater ">"))))
  14190. (message "Timestamp updated"))
  14191. ((equal arg '(16))
  14192. (org-insert-time-stamp (current-time) t))
  14193. (t
  14194. (setq time (let ((this-command this-command))
  14195. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14196. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14197. nil nil (list org-end-time-was-given))))))
  14198. ;; FIXME: can we use this for something else, like computing time differences?
  14199. (defun org-get-compact-tod (s)
  14200. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14201. (let* ((t1 (match-string 1 s))
  14202. (h1 (string-to-number (match-string 2 s)))
  14203. (m1 (string-to-number (match-string 3 s)))
  14204. (t2 (and (match-end 4) (match-string 5 s)))
  14205. (h2 (and t2 (string-to-number (match-string 6 s))))
  14206. (m2 (and t2 (string-to-number (match-string 7 s))))
  14207. dh dm)
  14208. (if (not t2)
  14209. t1
  14210. (setq dh (- h2 h1) dm (- m2 m1))
  14211. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14212. (concat t1 "+" (number-to-string dh)
  14213. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14214. (defun org-time-stamp-inactive (&optional arg)
  14215. "Insert an inactive time stamp.
  14216. An inactive time stamp is enclosed in square brackets instead of angle
  14217. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14218. does not link to the calendar and cannot be changed with the S-cursor keys.
  14219. So these are more for recording a certain time/date."
  14220. (interactive "P")
  14221. (org-time-stamp arg 'inactive))
  14222. (defvar org-date-ovl (make-overlay 1 1))
  14223. (overlay-put org-date-ovl 'face 'org-date-selected)
  14224. (org-detach-overlay org-date-ovl)
  14225. (defvar org-ans1) ; dynamically scoped parameter
  14226. (defvar org-ans2) ; dynamically scoped parameter
  14227. (defvar org-plain-time-of-day-regexp) ; defined below
  14228. (defvar org-overriding-default-time nil) ; dynamically scoped
  14229. (defvar org-read-date-overlay nil)
  14230. (defvar org-dcst nil) ; dynamically scoped
  14231. (defvar org-read-date-history nil)
  14232. (defvar org-read-date-final-answer nil)
  14233. (defvar org-read-date-analyze-futurep nil)
  14234. (defvar org-read-date-analyze-forced-year nil)
  14235. (defvar org-read-date-inactive)
  14236. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14237. default-time default-input inactive)
  14238. "Read a date, possibly a time, and make things smooth for the user.
  14239. The prompt will suggest to enter an ISO date, but you can also enter anything
  14240. which will at least partially be understood by `parse-time-string'.
  14241. Unrecognized parts of the date will default to the current day, month, year,
  14242. hour and minute. If this command is called to replace a timestamp at point,
  14243. or to enter the second timestamp of a range, the default time is taken
  14244. from the existing stamp. Furthermore, the command prefers the future,
  14245. so if you are giving a date where the year is not given, and the day-month
  14246. combination is already past in the current year, it will assume you
  14247. mean next year. For details, see the manual. A few examples:
  14248. 3-2-5 --> 2003-02-05
  14249. feb 15 --> currentyear-02-15
  14250. 2/15 --> currentyear-02-15
  14251. sep 12 9 --> 2009-09-12
  14252. 12:45 --> today 12:45
  14253. 22 sept 0:34 --> currentyear-09-22 0:34
  14254. 12 --> currentyear-currentmonth-12
  14255. Fri --> nearest Friday (today or later)
  14256. etc.
  14257. Furthermore you can specify a relative date by giving, as the *first* thing
  14258. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14259. change in days weeks, months, years.
  14260. With a single plus or minus, the date is relative to today. With a double
  14261. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14262. +4d --> four days from today
  14263. +4 --> same as above
  14264. +2w --> two weeks from today
  14265. ++5 --> five days from default date
  14266. The function understands only English month and weekday abbreviations.
  14267. While prompting, a calendar is popped up - you can also select the
  14268. date with the mouse (button 1). The calendar shows a period of three
  14269. months. To scroll it to other months, use the keys `>' and `<'.
  14270. If you don't like the calendar, turn it off with
  14271. \(setq org-read-date-popup-calendar nil)
  14272. With optional argument TO-TIME, the date will immediately be converted
  14273. to an internal time.
  14274. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14275. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14276. still enter a time, and this function will inform the calling routine
  14277. about this change. The calling routine may then choose to change the
  14278. format used to insert the time stamp into the buffer to include the time.
  14279. With optional argument FROM-STRING, read from this string instead from
  14280. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14281. the time/date that is used for everything that is not specified by the
  14282. user."
  14283. (require 'parse-time)
  14284. (let* ((org-time-stamp-rounding-minutes
  14285. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14286. (org-dcst org-display-custom-times)
  14287. (ct (org-current-time))
  14288. (org-def (or org-overriding-default-time default-time ct))
  14289. (org-defdecode (decode-time org-def))
  14290. (dummy (progn
  14291. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14292. (setcar (nthcdr 2 org-defdecode) -1)
  14293. (setcar (nthcdr 1 org-defdecode) 59)
  14294. (setq org-def (apply 'encode-time org-defdecode)
  14295. org-defdecode (decode-time org-def)))))
  14296. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14297. (calendar-frame-setup nil)
  14298. (calendar-setup nil)
  14299. (calendar-move-hook nil)
  14300. (calendar-view-diary-initially-flag nil)
  14301. (calendar-view-holidays-initially-flag nil)
  14302. (timestr (format-time-string
  14303. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14304. (prompt (concat (if prompt (concat prompt " ") "")
  14305. (format "Date+time [%s]: " timestr)))
  14306. ans (org-ans0 "") org-ans1 org-ans2 final)
  14307. (cond
  14308. (from-string (setq ans from-string))
  14309. (org-read-date-popup-calendar
  14310. (save-excursion
  14311. (save-window-excursion
  14312. (calendar)
  14313. (org-eval-in-calendar '(setq cursor-type nil) t)
  14314. (unwind-protect
  14315. (progn
  14316. (calendar-forward-day (- (time-to-days org-def)
  14317. (calendar-absolute-from-gregorian
  14318. (calendar-current-date))))
  14319. (org-eval-in-calendar nil t)
  14320. (let* ((old-map (current-local-map))
  14321. (map (copy-keymap calendar-mode-map))
  14322. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  14323. (org-defkey map (kbd "RET") 'org-calendar-select)
  14324. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14325. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14326. (org-defkey minibuffer-local-map [(meta shift left)]
  14327. (lambda () (interactive)
  14328. (org-eval-in-calendar '(calendar-backward-month 1))))
  14329. (org-defkey minibuffer-local-map [(meta shift right)]
  14330. (lambda () (interactive)
  14331. (org-eval-in-calendar '(calendar-forward-month 1))))
  14332. (org-defkey minibuffer-local-map [(meta shift up)]
  14333. (lambda () (interactive)
  14334. (org-eval-in-calendar '(calendar-backward-year 1))))
  14335. (org-defkey minibuffer-local-map [(meta shift down)]
  14336. (lambda () (interactive)
  14337. (org-eval-in-calendar '(calendar-forward-year 1))))
  14338. (org-defkey minibuffer-local-map [?\e (shift left)]
  14339. (lambda () (interactive)
  14340. (org-eval-in-calendar '(calendar-backward-month 1))))
  14341. (org-defkey minibuffer-local-map [?\e (shift right)]
  14342. (lambda () (interactive)
  14343. (org-eval-in-calendar '(calendar-forward-month 1))))
  14344. (org-defkey minibuffer-local-map [?\e (shift up)]
  14345. (lambda () (interactive)
  14346. (org-eval-in-calendar '(calendar-backward-year 1))))
  14347. (org-defkey minibuffer-local-map [?\e (shift down)]
  14348. (lambda () (interactive)
  14349. (org-eval-in-calendar '(calendar-forward-year 1))))
  14350. (org-defkey minibuffer-local-map [(shift up)]
  14351. (lambda () (interactive)
  14352. (org-eval-in-calendar '(calendar-backward-week 1))))
  14353. (org-defkey minibuffer-local-map [(shift down)]
  14354. (lambda () (interactive)
  14355. (org-eval-in-calendar '(calendar-forward-week 1))))
  14356. (org-defkey minibuffer-local-map [(shift left)]
  14357. (lambda () (interactive)
  14358. (org-eval-in-calendar '(calendar-backward-day 1))))
  14359. (org-defkey minibuffer-local-map [(shift right)]
  14360. (lambda () (interactive)
  14361. (org-eval-in-calendar '(calendar-forward-day 1))))
  14362. (org-defkey minibuffer-local-map ">"
  14363. (lambda () (interactive)
  14364. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14365. (org-defkey minibuffer-local-map "<"
  14366. (lambda () (interactive)
  14367. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14368. (org-defkey minibuffer-local-map "\C-v"
  14369. (lambda () (interactive)
  14370. (org-eval-in-calendar
  14371. '(calendar-scroll-left-three-months 1))))
  14372. (org-defkey minibuffer-local-map "\M-v"
  14373. (lambda () (interactive)
  14374. (org-eval-in-calendar
  14375. '(calendar-scroll-right-three-months 1))))
  14376. (run-hooks 'org-read-date-minibuffer-setup-hook)
  14377. (unwind-protect
  14378. (progn
  14379. (use-local-map map)
  14380. (setq org-read-date-inactive inactive)
  14381. (add-hook 'post-command-hook 'org-read-date-display)
  14382. (setq org-ans0 (read-string prompt default-input
  14383. 'org-read-date-history nil))
  14384. ;; org-ans0: from prompt
  14385. ;; org-ans1: from mouse click
  14386. ;; org-ans2: from calendar motion
  14387. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14388. (remove-hook 'post-command-hook 'org-read-date-display)
  14389. (use-local-map old-map)
  14390. (when org-read-date-overlay
  14391. (delete-overlay org-read-date-overlay)
  14392. (setq org-read-date-overlay nil)))))
  14393. (bury-buffer "*Calendar*")))))
  14394. (t ; Naked prompt only
  14395. (unwind-protect
  14396. (setq ans (read-string prompt default-input
  14397. 'org-read-date-history timestr))
  14398. (when org-read-date-overlay
  14399. (delete-overlay org-read-date-overlay)
  14400. (setq org-read-date-overlay nil)))))
  14401. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14402. (when org-read-date-analyze-forced-year
  14403. (message "Year was forced into %s"
  14404. (if org-read-date-force-compatible-dates
  14405. "compatible range (1970-2037)"
  14406. "range representable on this machine"))
  14407. (ding))
  14408. ;; One round trip to get rid of 34th of August and stuff like that....
  14409. (setq final (decode-time (apply 'encode-time final)))
  14410. (setq org-read-date-final-answer ans)
  14411. (if to-time
  14412. (apply 'encode-time final)
  14413. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14414. (format "%04d-%02d-%02d %02d:%02d"
  14415. (nth 5 final) (nth 4 final) (nth 3 final)
  14416. (nth 2 final) (nth 1 final))
  14417. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14418. (defvar org-def)
  14419. (defvar org-defdecode)
  14420. (defvar org-with-time)
  14421. (defun org-read-date-display ()
  14422. "Display the current date prompt interpretation in the minibuffer."
  14423. (when org-read-date-display-live
  14424. (when org-read-date-overlay
  14425. (delete-overlay org-read-date-overlay))
  14426. (when (minibufferp (current-buffer))
  14427. (save-excursion
  14428. (end-of-line 1)
  14429. (while (not (equal (buffer-substring
  14430. (max (point-min) (- (point) 4)) (point))
  14431. " "))
  14432. (insert " ")))
  14433. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14434. " " (or org-ans1 org-ans2)))
  14435. (org-end-time-was-given nil)
  14436. (f (org-read-date-analyze ans org-def org-defdecode))
  14437. (fmts (if org-dcst
  14438. org-time-stamp-custom-formats
  14439. org-time-stamp-formats))
  14440. (fmt (if (or org-with-time
  14441. (and (boundp 'org-time-was-given) org-time-was-given))
  14442. (cdr fmts)
  14443. (car fmts)))
  14444. (txt (format-time-string fmt (apply 'encode-time f)))
  14445. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14446. (txt (concat "=> " txt)))
  14447. (when (and org-end-time-was-given
  14448. (string-match org-plain-time-of-day-regexp txt))
  14449. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14450. org-end-time-was-given
  14451. (substring txt (match-end 0)))))
  14452. (when org-read-date-analyze-futurep
  14453. (setq txt (concat txt " (=>F)")))
  14454. (setq org-read-date-overlay
  14455. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14456. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14457. (defun org-read-date-analyze (ans org-def org-defdecode)
  14458. "Analyze the combined answer of the date prompt."
  14459. ;; FIXME: cleanup and comment
  14460. (let ((nowdecode (decode-time (current-time)))
  14461. delta deltan deltaw deltadef year month day
  14462. hour minute second wday pm h2 m2 tl wday1
  14463. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14464. (setq org-read-date-analyze-futurep nil
  14465. org-read-date-analyze-forced-year nil)
  14466. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14467. (setq ans "+0"))
  14468. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14469. (setq ans (replace-match "" t t ans)
  14470. deltan (car delta)
  14471. deltaw (nth 1 delta)
  14472. deltadef (nth 2 delta)))
  14473. ;; Check if there is an iso week date in there. If yes, store the
  14474. ;; info and postpone interpreting it until the rest of the parsing
  14475. ;; is done.
  14476. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14477. (setq iso-year (if (match-end 1)
  14478. (org-small-year-to-year
  14479. (string-to-number (match-string 1 ans))))
  14480. iso-weekday (if (match-end 3)
  14481. (string-to-number (match-string 3 ans)))
  14482. iso-week (string-to-number (match-string 2 ans)))
  14483. (setq ans (replace-match "" t t ans)))
  14484. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14485. (when (string-match
  14486. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14487. (setq year (if (match-end 2)
  14488. (string-to-number (match-string 2 ans))
  14489. (progn (setq kill-year t)
  14490. (string-to-number (format-time-string "%Y"))))
  14491. month (string-to-number (match-string 3 ans))
  14492. day (string-to-number (match-string 4 ans)))
  14493. (if (< year 100) (setq year (+ 2000 year)))
  14494. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14495. t nil ans)))
  14496. ;; Help matching dotted european dates
  14497. (when (string-match
  14498. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14499. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14500. (setq kill-year t)
  14501. (string-to-number (format-time-string "%Y")))
  14502. day (string-to-number (match-string 1 ans))
  14503. month (string-to-number (match-string 2 ans))
  14504. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14505. t nil ans)))
  14506. ;; Help matching american dates, like 5/30 or 5/30/7
  14507. (when (string-match
  14508. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14509. (setq year (if (match-end 4)
  14510. (string-to-number (match-string 4 ans))
  14511. (progn (setq kill-year t)
  14512. (string-to-number (format-time-string "%Y"))))
  14513. month (string-to-number (match-string 1 ans))
  14514. day (string-to-number (match-string 2 ans)))
  14515. (if (< year 100) (setq year (+ 2000 year)))
  14516. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14517. t nil ans)))
  14518. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14519. ;; If there is a time with am/pm, and *no* time without it, we convert
  14520. ;; so that matching will be successful.
  14521. (loop for i from 1 to 2 do ; twice, for end time as well
  14522. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14523. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14524. (setq hour (string-to-number (match-string 1 ans))
  14525. minute (if (match-end 3)
  14526. (string-to-number (match-string 3 ans))
  14527. 0)
  14528. pm (equal ?p
  14529. (string-to-char (downcase (match-string 4 ans)))))
  14530. (if (and (= hour 12) (not pm))
  14531. (setq hour 0)
  14532. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14533. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14534. t t ans))))
  14535. ;; Check if a time range is given as a duration
  14536. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14537. (setq hour (string-to-number (match-string 1 ans))
  14538. h2 (+ hour (string-to-number (match-string 3 ans)))
  14539. minute (string-to-number (match-string 2 ans))
  14540. m2 (+ minute (if (match-end 5) (string-to-number
  14541. (match-string 5 ans))0)))
  14542. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14543. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14544. t t ans)))
  14545. ;; Check if there is a time range
  14546. (when (boundp 'org-end-time-was-given)
  14547. (setq org-time-was-given nil)
  14548. (when (and (string-match org-plain-time-of-day-regexp ans)
  14549. (match-end 8))
  14550. (setq org-end-time-was-given (match-string 8 ans))
  14551. (setq ans (concat (substring ans 0 (match-beginning 7))
  14552. (substring ans (match-end 7))))))
  14553. (setq tl (parse-time-string ans)
  14554. day (or (nth 3 tl) (nth 3 org-defdecode))
  14555. month (or (nth 4 tl)
  14556. (if (and org-read-date-prefer-future
  14557. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14558. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14559. (nth 4 org-defdecode)))
  14560. year (or (and (not kill-year) (nth 5 tl))
  14561. (if (and org-read-date-prefer-future
  14562. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14563. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14564. (nth 5 org-defdecode)))
  14565. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14566. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14567. second (or (nth 0 tl) 0)
  14568. wday (nth 6 tl))
  14569. (when (and (eq org-read-date-prefer-future 'time)
  14570. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14571. (equal day (nth 3 nowdecode))
  14572. (equal month (nth 4 nowdecode))
  14573. (equal year (nth 5 nowdecode))
  14574. (nth 2 tl)
  14575. (or (< (nth 2 tl) (nth 2 nowdecode))
  14576. (and (= (nth 2 tl) (nth 2 nowdecode))
  14577. (nth 1 tl)
  14578. (< (nth 1 tl) (nth 1 nowdecode)))))
  14579. (setq day (1+ day)
  14580. futurep t))
  14581. ;; Special date definitions below
  14582. (cond
  14583. (iso-week
  14584. ;; There was an iso week
  14585. (require 'cal-iso)
  14586. (setq futurep nil)
  14587. (setq year (or iso-year year)
  14588. day (or iso-weekday wday 1)
  14589. wday nil ; to make sure that the trigger below does not match
  14590. iso-date (calendar-gregorian-from-absolute
  14591. (calendar-absolute-from-iso
  14592. (list iso-week day year))))
  14593. ; FIXME: Should we also push ISO weeks into the future?
  14594. ; (when (and org-read-date-prefer-future
  14595. ; (not iso-year)
  14596. ; (< (calendar-absolute-from-gregorian iso-date)
  14597. ; (time-to-days (current-time))))
  14598. ; (setq year (1+ year)
  14599. ; iso-date (calendar-gregorian-from-absolute
  14600. ; (calendar-absolute-from-iso
  14601. ; (list iso-week day year)))))
  14602. (setq month (car iso-date)
  14603. year (nth 2 iso-date)
  14604. day (nth 1 iso-date)))
  14605. (deltan
  14606. (setq futurep nil)
  14607. (unless deltadef
  14608. (let ((now (decode-time (current-time))))
  14609. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14610. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14611. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14612. ((equal deltaw "m") (setq month (+ month deltan)))
  14613. ((equal deltaw "y") (setq year (+ year deltan)))))
  14614. ((and wday (not (nth 3 tl)))
  14615. ;; Weekday was given, but no day, so pick that day in the week
  14616. ;; on or after the derived date.
  14617. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14618. (unless (equal wday wday1)
  14619. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14620. (if (and (boundp 'org-time-was-given)
  14621. (nth 2 tl))
  14622. (setq org-time-was-given t))
  14623. (if (< year 100) (setq year (+ 2000 year)))
  14624. ;; Check of the date is representable
  14625. (if org-read-date-force-compatible-dates
  14626. (progn
  14627. (if (< year 1970)
  14628. (setq year 1970 org-read-date-analyze-forced-year t))
  14629. (if (> year 2037)
  14630. (setq year 2037 org-read-date-analyze-forced-year t)))
  14631. (condition-case nil
  14632. (ignore (encode-time second minute hour day month year))
  14633. (error
  14634. (setq year (nth 5 org-defdecode))
  14635. (setq org-read-date-analyze-forced-year t))))
  14636. (setq org-read-date-analyze-futurep futurep)
  14637. (list second minute hour day month year)))
  14638. (defvar parse-time-weekdays)
  14639. (defun org-read-date-get-relative (s today default)
  14640. "Check string S for special relative date string.
  14641. TODAY and DEFAULT are internal times, for today and for a default.
  14642. Return shift list (N what def-flag)
  14643. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14644. N is the number of WHATs to shift.
  14645. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14646. the DEFAULT date rather than TODAY."
  14647. (require 'parse-time)
  14648. (when (and
  14649. (string-match
  14650. (concat
  14651. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14652. "\\([0-9]+\\)?"
  14653. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14654. "\\([ \t]\\|$\\)") s)
  14655. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14656. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14657. (string-to-char (substring (match-string 1 s) -1))
  14658. ?+))
  14659. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14660. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14661. (what (if (match-end 3) (match-string 3 s) "d"))
  14662. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14663. (date (if rel default today))
  14664. (wday (nth 6 (decode-time date)))
  14665. delta)
  14666. (if wday1
  14667. (progn
  14668. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14669. (if (= dir ?-) (setq delta (- delta 7)))
  14670. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14671. (list delta "d" rel))
  14672. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14673. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14674. "Turn a user-specified date into the internal representation.
  14675. The internal representation needed by the calendar is (month day year).
  14676. This is a wrapper to handle the brain-dead convention in calendar that
  14677. user function argument order change dependent on argument order."
  14678. (if (boundp 'calendar-date-style)
  14679. (cond
  14680. ((eq calendar-date-style 'american)
  14681. (list arg1 arg2 arg3))
  14682. ((eq calendar-date-style 'european)
  14683. (list arg2 arg1 arg3))
  14684. ((eq calendar-date-style 'iso)
  14685. (list arg2 arg3 arg1)))
  14686. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14687. (if (org-bound-and-true-p european-calendar-style)
  14688. (list arg2 arg1 arg3)
  14689. (list arg1 arg2 arg3)))))
  14690. (defun org-eval-in-calendar (form &optional keepdate)
  14691. "Eval FORM in the calendar window and return to current window.
  14692. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14693. otherwise stick to the current value of `org-ans2'."
  14694. (let ((sf (selected-frame))
  14695. (sw (selected-window)))
  14696. (select-window (get-buffer-window "*Calendar*" t))
  14697. (eval form)
  14698. (when (and (not keepdate) (calendar-cursor-to-date))
  14699. (let* ((date (calendar-cursor-to-date))
  14700. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14701. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14702. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14703. (select-window sw)
  14704. (org-select-frame-set-input-focus sf)))
  14705. (defun org-calendar-select ()
  14706. "Return to `org-read-date' with the date currently selected.
  14707. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14708. (interactive)
  14709. (when (calendar-cursor-to-date)
  14710. (let* ((date (calendar-cursor-to-date))
  14711. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14712. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14713. (if (active-minibuffer-window) (exit-minibuffer))))
  14714. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14715. "Insert a date stamp for the date given by the internal TIME.
  14716. WITH-HM means use the stamp format that includes the time of the day.
  14717. INACTIVE means use square brackets instead of angular ones, so that the
  14718. stamp will not contribute to the agenda.
  14719. PRE and POST are optional strings to be inserted before and after the
  14720. stamp.
  14721. The command returns the inserted time stamp."
  14722. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14723. stamp)
  14724. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14725. (insert-before-markers (or pre ""))
  14726. (when (listp extra)
  14727. (setq extra (car extra))
  14728. (if (and (stringp extra)
  14729. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14730. (setq extra (format "-%02d:%02d"
  14731. (string-to-number (match-string 1 extra))
  14732. (string-to-number (match-string 2 extra))))
  14733. (setq extra nil)))
  14734. (when extra
  14735. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14736. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14737. (insert-before-markers (or post ""))
  14738. (setq org-last-inserted-timestamp stamp)))
  14739. (defun org-toggle-time-stamp-overlays ()
  14740. "Toggle the use of custom time stamp formats."
  14741. (interactive)
  14742. (setq org-display-custom-times (not org-display-custom-times))
  14743. (unless org-display-custom-times
  14744. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14745. (while (setq p (next-single-property-change p 'display))
  14746. (if (and (get-text-property p 'display)
  14747. (eq (get-text-property p 'face) 'org-date))
  14748. (remove-text-properties
  14749. p (setq p (next-single-property-change p 'display))
  14750. '(display t))))
  14751. (set-buffer-modified-p bmp)))
  14752. (if (featurep 'xemacs)
  14753. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14754. (org-restart-font-lock)
  14755. (setq org-table-may-need-update t)
  14756. (if org-display-custom-times
  14757. (message "Time stamps are overlaid with custom format")
  14758. (message "Time stamp overlays removed")))
  14759. (defun org-display-custom-time (beg end)
  14760. "Overlay modified time stamp format over timestamp between BEG and END."
  14761. (let* ((ts (buffer-substring beg end))
  14762. t1 w1 with-hm tf time str w2 (off 0))
  14763. (save-match-data
  14764. (setq t1 (org-parse-time-string ts t))
  14765. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14766. (setq off (- (match-end 0) (match-beginning 0)))))
  14767. (setq end (- end off))
  14768. (setq w1 (- end beg)
  14769. with-hm (and (nth 1 t1) (nth 2 t1))
  14770. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14771. time (org-fix-decoded-time t1)
  14772. str (org-add-props
  14773. (format-time-string
  14774. (substring tf 1 -1) (apply 'encode-time time))
  14775. nil 'mouse-face 'highlight)
  14776. w2 (length str))
  14777. (if (not (= w2 w1))
  14778. (add-text-properties (1+ beg) (+ 2 beg)
  14779. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14780. (if (featurep 'xemacs)
  14781. (progn
  14782. (put-text-property beg end 'invisible t)
  14783. (put-text-property beg end 'end-glyph (make-glyph str)))
  14784. (put-text-property beg end 'display str))))
  14785. (defun org-translate-time (string)
  14786. "Translate all timestamps in STRING to custom format.
  14787. But do this only if the variable `org-display-custom-times' is set."
  14788. (when org-display-custom-times
  14789. (save-match-data
  14790. (let* ((start 0)
  14791. (re org-ts-regexp-both)
  14792. t1 with-hm inactive tf time str beg end)
  14793. (while (setq start (string-match re string start))
  14794. (setq beg (match-beginning 0)
  14795. end (match-end 0)
  14796. t1 (save-match-data
  14797. (org-parse-time-string (substring string beg end) t))
  14798. with-hm (and (nth 1 t1) (nth 2 t1))
  14799. inactive (equal (substring string beg (1+ beg)) "[")
  14800. tf (funcall (if with-hm 'cdr 'car)
  14801. org-time-stamp-custom-formats)
  14802. time (org-fix-decoded-time t1)
  14803. str (format-time-string
  14804. (concat
  14805. (if inactive "[" "<") (substring tf 1 -1)
  14806. (if inactive "]" ">"))
  14807. (apply 'encode-time time))
  14808. string (replace-match str t t string)
  14809. start (+ start (length str)))))))
  14810. string)
  14811. (defun org-fix-decoded-time (time)
  14812. "Set 0 instead of nil for the first 6 elements of time.
  14813. Don't touch the rest."
  14814. (let ((n 0))
  14815. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14816. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.3")
  14817. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  14818. "Difference between TIMESTAMP-STRING and now in days.
  14819. If SECONDS is non-nil, return the difference in seconds."
  14820. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  14821. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  14822. (funcall fdiff (current-time)))))
  14823. (defun org-deadline-close (timestamp-string &optional ndays)
  14824. "Is the time in TIMESTAMP-STRING close to the current date?"
  14825. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14826. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  14827. (not (org-entry-is-done-p))))
  14828. (defun org-get-wdays (ts)
  14829. "Get the deadline lead time appropriate for timestring TS."
  14830. (cond
  14831. ((<= org-deadline-warning-days 0)
  14832. ;; 0 or negative, enforce this value no matter what
  14833. (- org-deadline-warning-days))
  14834. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14835. ;; lead time is specified.
  14836. (floor (* (string-to-number (match-string 1 ts))
  14837. (cdr (assoc (match-string 2 ts)
  14838. '(("d" . 1) ("w" . 7)
  14839. ("m" . 30.4) ("y" . 365.25)))))))
  14840. ;; go for the default.
  14841. (t org-deadline-warning-days)))
  14842. (defun org-calendar-select-mouse (ev)
  14843. "Return to `org-read-date' with the date currently selected.
  14844. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14845. (interactive "e")
  14846. (mouse-set-point ev)
  14847. (when (calendar-cursor-to-date)
  14848. (let* ((date (calendar-cursor-to-date))
  14849. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14850. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14851. (if (active-minibuffer-window) (exit-minibuffer))))
  14852. (defun org-check-deadlines (ndays)
  14853. "Check if there are any deadlines due or past due.
  14854. A deadline is considered due if it happens within `org-deadline-warning-days'
  14855. days from today's date. If the deadline appears in an entry marked DONE,
  14856. it is not shown. The prefix arg NDAYS can be used to test that many
  14857. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14858. (interactive "P")
  14859. (let* ((org-warn-days
  14860. (cond
  14861. ((equal ndays '(4)) 100000)
  14862. (ndays (prefix-numeric-value ndays))
  14863. (t (abs org-deadline-warning-days))))
  14864. (case-fold-search nil)
  14865. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14866. (callback
  14867. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14868. (message "%d deadlines past-due or due within %d days"
  14869. (org-occur regexp nil callback)
  14870. org-warn-days)))
  14871. (defsubst org-re-timestamp (type)
  14872. "Return a regexp for timestamp TYPE.
  14873. Allowed values for TYPE are:
  14874. all: all timestamps
  14875. active: only active timestamps (<...>)
  14876. inactive: only inactive timestamps ([...])
  14877. scheduled: only scheduled timestamps
  14878. deadline: only deadline timestamps
  14879. When TYPE is nil, fall back on returning a regexp that matches
  14880. both scheduled and deadline timestamps."
  14881. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  14882. ((eq type 'active) org-ts-regexp)
  14883. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  14884. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  14885. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14886. ((eq type 'scheduled-or-deadline)
  14887. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  14888. (defun org-check-before-date (date)
  14889. "Check if there are deadlines or scheduled entries before DATE."
  14890. (interactive (list (org-read-date)))
  14891. (let ((case-fold-search nil)
  14892. (regexp (org-re-timestamp org-ts-type))
  14893. (callback
  14894. (lambda () (time-less-p
  14895. (org-time-string-to-time (match-string 1))
  14896. (org-time-string-to-time date)))))
  14897. (message "%d entries before %s"
  14898. (org-occur regexp nil callback) date)))
  14899. (defun org-check-after-date (date)
  14900. "Check if there are deadlines or scheduled entries after DATE."
  14901. (interactive (list (org-read-date)))
  14902. (let ((case-fold-search nil)
  14903. (regexp (org-re-timestamp org-ts-type))
  14904. (callback
  14905. (lambda () (not
  14906. (time-less-p
  14907. (org-time-string-to-time (match-string 1))
  14908. (org-time-string-to-time date))))))
  14909. (message "%d entries after %s"
  14910. (org-occur regexp nil callback) date)))
  14911. (defun org-check-dates-range (start-date end-date)
  14912. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14913. (interactive (list (org-read-date nil nil nil "Range starts")
  14914. (org-read-date nil nil nil "Range end")))
  14915. (let ((case-fold-search nil)
  14916. (regexp (org-re-timestamp org-ts-type))
  14917. (callback
  14918. (lambda ()
  14919. (let ((match (match-string 1)))
  14920. (and
  14921. (not (time-less-p
  14922. (org-time-string-to-time match)
  14923. (org-time-string-to-time start-date)))
  14924. (time-less-p
  14925. (org-time-string-to-time match)
  14926. (org-time-string-to-time end-date)))))))
  14927. (message "%d entries between %s and %s"
  14928. (org-occur regexp nil callback) start-date end-date)))
  14929. (defun org-evaluate-time-range (&optional to-buffer)
  14930. "Evaluate a time range by computing the difference between start and end.
  14931. Normally the result is just printed in the echo area, but with prefix arg
  14932. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14933. If the time range is actually in a table, the result is inserted into the
  14934. next column.
  14935. For time difference computation, a year is assumed to be exactly 365
  14936. days in order to avoid rounding problems."
  14937. (interactive "P")
  14938. (or
  14939. (org-clock-update-time-maybe)
  14940. (save-excursion
  14941. (unless (org-at-date-range-p t)
  14942. (goto-char (point-at-bol))
  14943. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14944. (if (not (org-at-date-range-p t))
  14945. (error "Not at a time-stamp range, and none found in current line")))
  14946. (let* ((ts1 (match-string 1))
  14947. (ts2 (match-string 2))
  14948. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14949. (match-end (match-end 0))
  14950. (time1 (org-time-string-to-time ts1))
  14951. (time2 (org-time-string-to-time ts2))
  14952. (t1 (org-float-time time1))
  14953. (t2 (org-float-time time2))
  14954. (diff (abs (- t2 t1)))
  14955. (negative (< (- t2 t1) 0))
  14956. ;; (ys (floor (* 365 24 60 60)))
  14957. (ds (* 24 60 60))
  14958. (hs (* 60 60))
  14959. (fy "%dy %dd %02d:%02d")
  14960. (fy1 "%dy %dd")
  14961. (fd "%dd %02d:%02d")
  14962. (fd1 "%dd")
  14963. (fh "%02d:%02d")
  14964. y d h m align)
  14965. (if havetime
  14966. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14967. y 0
  14968. d (floor (/ diff ds)) diff (mod diff ds)
  14969. h (floor (/ diff hs)) diff (mod diff hs)
  14970. m (floor (/ diff 60)))
  14971. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14972. y 0
  14973. d (floor (+ (/ diff ds) 0.5))
  14974. h 0 m 0))
  14975. (if (not to-buffer)
  14976. (message "%s" (org-make-tdiff-string y d h m))
  14977. (if (org-at-table-p)
  14978. (progn
  14979. (goto-char match-end)
  14980. (setq align t)
  14981. (and (looking-at " *|") (goto-char (match-end 0))))
  14982. (goto-char match-end))
  14983. (if (looking-at
  14984. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14985. (replace-match ""))
  14986. (if negative (insert " -"))
  14987. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14988. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14989. (insert " " (format fh h m))))
  14990. (if align (org-table-align))
  14991. (message "Time difference inserted")))))
  14992. (defun org-make-tdiff-string (y d h m)
  14993. (let ((fmt "")
  14994. (l nil))
  14995. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14996. l (push y l)))
  14997. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14998. l (push d l)))
  14999. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15000. l (push h l)))
  15001. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15002. l (push m l)))
  15003. (apply 'format fmt (nreverse l))))
  15004. (defun org-time-string-to-time (s &optional buffer pos)
  15005. "Convert a timestamp string into internal time."
  15006. (condition-case errdata
  15007. (apply 'encode-time (org-parse-time-string s))
  15008. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15009. s (if (not (and buffer pos))
  15010. ""
  15011. (format " at %d in buffer `%s'" pos buffer))
  15012. (cdr errdata)))))
  15013. (defun org-time-string-to-seconds (s)
  15014. "Convert a timestamp string to a number of seconds."
  15015. (org-float-time (org-time-string-to-time s)))
  15016. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15017. "Convert a time stamp to an absolute day number.
  15018. If there is a specifier for a cyclic time stamp, get the closest date to
  15019. DAYNR.
  15020. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15021. The variable date is bound by the calendar when this is called."
  15022. (cond
  15023. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15024. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15025. daynr
  15026. (+ daynr 1000)))
  15027. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15028. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15029. (time-to-days (current-time))) (match-string 0 s)
  15030. prefer show-all))
  15031. (t (time-to-days
  15032. (condition-case errdata
  15033. (apply 'encode-time (org-parse-time-string s))
  15034. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15035. s (if (not (and buffer pos))
  15036. ""
  15037. (format " at %d in buffer `%s'" pos buffer))
  15038. (cdr errdata))))))))
  15039. (defun org-days-to-iso-week (days)
  15040. "Return the iso week number."
  15041. (require 'cal-iso)
  15042. (car (calendar-iso-from-absolute days)))
  15043. (defun org-small-year-to-year (year)
  15044. "Convert 2-digit years into 4-digit years.
  15045. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15046. The year 2000 cannot be abbreviated. Any year larger than 99
  15047. is returned unchanged."
  15048. (if (< year 38)
  15049. (setq year (+ 2000 year))
  15050. (if (< year 100)
  15051. (setq year (+ 1900 year))))
  15052. year)
  15053. (defun org-time-from-absolute (d)
  15054. "Return the time corresponding to date D.
  15055. D may be an absolute day number, or a calendar-type list (month day year)."
  15056. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15057. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15058. (defun org-calendar-holiday ()
  15059. "List of holidays, for Diary display in Org-mode."
  15060. (require 'holidays)
  15061. (let ((hl (funcall
  15062. (if (fboundp 'calendar-check-holidays)
  15063. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15064. (if hl (mapconcat 'identity hl "; "))))
  15065. (defun org-diary-sexp-entry (sexp entry date)
  15066. "Process a SEXP diary ENTRY for DATE."
  15067. (require 'diary-lib)
  15068. (let ((result (if calendar-debug-sexp
  15069. (let ((stack-trace-on-error t))
  15070. (eval (car (read-from-string sexp))))
  15071. (condition-case nil
  15072. (eval (car (read-from-string sexp)))
  15073. (error
  15074. (beep)
  15075. (message "Bad sexp at line %d in %s: %s"
  15076. (org-current-line)
  15077. (buffer-file-name) sexp)
  15078. (sleep-for 2))))))
  15079. (cond ((stringp result) (split-string result "; "))
  15080. ((and (consp result)
  15081. (not (consp (cdr result)))
  15082. (stringp (cdr result))) (cdr result))
  15083. ((and (consp result)
  15084. (stringp (car result))) result)
  15085. (result entry))))
  15086. (defun org-diary-to-ical-string (frombuf)
  15087. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15088. This uses the icalendar.el library."
  15089. (let* ((tmpdir (if (featurep 'xemacs)
  15090. (temp-directory)
  15091. temporary-file-directory))
  15092. (tmpfile (make-temp-name
  15093. (expand-file-name "orgics" tmpdir)))
  15094. buf rtn b e)
  15095. (with-current-buffer frombuf
  15096. (icalendar-export-region (point-min) (point-max) tmpfile)
  15097. (setq buf (find-buffer-visiting tmpfile))
  15098. (set-buffer buf)
  15099. (goto-char (point-min))
  15100. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15101. (setq b (match-beginning 0)))
  15102. (goto-char (point-max))
  15103. (if (re-search-backward "^END:VEVENT" nil t)
  15104. (setq e (match-end 0)))
  15105. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15106. (kill-buffer buf)
  15107. (delete-file tmpfile)
  15108. rtn))
  15109. (defun org-closest-date (start current change prefer show-all)
  15110. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15111. When PREFER is `past', return a date that is either CURRENT or past.
  15112. When PREFER is `future', return a date that is either CURRENT or future.
  15113. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15114. ;; Make the proper lists from the dates
  15115. (catch 'exit
  15116. (let ((a1 '(("h" . hour)
  15117. ("d" . day)
  15118. ("w" . week)
  15119. ("m" . month)
  15120. ("y" . year)))
  15121. (shour (nth 2 (org-parse-time-string start)))
  15122. dn dw sday cday n1 n2 n0
  15123. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15124. (setq start (org-date-to-gregorian start)
  15125. current (org-date-to-gregorian
  15126. (if show-all
  15127. current
  15128. (time-to-days (current-time))))
  15129. sday (calendar-absolute-from-gregorian start)
  15130. cday (calendar-absolute-from-gregorian current))
  15131. (if (<= cday sday) (throw 'exit sday))
  15132. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15133. (setq dn (string-to-number (match-string 1 change))
  15134. dw (cdr (assoc (match-string 2 change) a1)))
  15135. (error "Invalid change specifier: %s" change))
  15136. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15137. (cond
  15138. ((eq dw 'hour)
  15139. (let ((missing-hours
  15140. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15141. dn)))
  15142. (setq n1 (if (zerop missing-hours) cday
  15143. (- cday (1+ (floor (/ missing-hours 24)))))
  15144. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15145. ((eq dw 'day)
  15146. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15147. n2 (+ n1 dn)))
  15148. ((eq dw 'year)
  15149. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15150. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15151. (setq date1 (list m d y1)
  15152. n1 (calendar-absolute-from-gregorian date1)
  15153. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15154. n2 (calendar-absolute-from-gregorian date2)))
  15155. ((eq dw 'month)
  15156. ;; approx number of month between the two dates
  15157. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15158. ;; How often does dn fit in there?
  15159. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15160. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15161. m (+ m nm)
  15162. ny (floor (/ m 12))
  15163. y (+ y ny)
  15164. m (- m (* ny 12)))
  15165. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15166. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15167. (setq m2 (+ m dn) y2 y)
  15168. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15169. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15170. (while (<= n2 cday)
  15171. (setq n1 n2 m m2 y y2)
  15172. (setq m2 (+ m dn) y2 y)
  15173. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15174. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15175. ;; Make sure n1 is the earlier date
  15176. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15177. (if show-all
  15178. (cond
  15179. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15180. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15181. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15182. (cond
  15183. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15184. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15185. (t (if (= cday n1) n1 n2)))))))
  15186. (defun org-date-to-gregorian (date)
  15187. "Turn any specification of DATE into a Gregorian date for the calendar."
  15188. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15189. ((and (listp date) (= (length date) 3)) date)
  15190. ((stringp date)
  15191. (setq date (org-parse-time-string date))
  15192. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15193. ((listp date)
  15194. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15195. (defun org-parse-time-string (s &optional nodefault)
  15196. "Parse the standard Org-mode time string.
  15197. This should be a lot faster than the normal `parse-time-string'.
  15198. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15199. hour and minute fields will be nil if not given."
  15200. (cond ((string-match org-ts-regexp0 s)
  15201. (list 0
  15202. (if (or (match-beginning 8) (not nodefault))
  15203. (string-to-number (or (match-string 8 s) "0")))
  15204. (if (or (match-beginning 7) (not nodefault))
  15205. (string-to-number (or (match-string 7 s) "0")))
  15206. (string-to-number (match-string 4 s))
  15207. (string-to-number (match-string 3 s))
  15208. (string-to-number (match-string 2 s))
  15209. nil nil nil))
  15210. ((string-match "^<[^>]+>$" s)
  15211. (decode-time (seconds-to-time (org-matcher-time s))))
  15212. (t (error "Not a standard Org-mode time string: %s" s))))
  15213. (defun org-timestamp-up (&optional arg)
  15214. "Increase the date item at the cursor by one.
  15215. If the cursor is on the year, change the year. If it is on the month,
  15216. the day or the time, change that.
  15217. With prefix ARG, change by that many units."
  15218. (interactive "p")
  15219. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15220. (defun org-timestamp-down (&optional arg)
  15221. "Decrease the date item at the cursor by one.
  15222. If the cursor is on the year, change the year. If it is on the month,
  15223. the day or the time, change that.
  15224. With prefix ARG, change by that many units."
  15225. (interactive "p")
  15226. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15227. (defun org-timestamp-up-day (&optional arg)
  15228. "Increase the date in the time stamp by one day.
  15229. With prefix ARG, change that many days."
  15230. (interactive "p")
  15231. (if (and (not (org-at-timestamp-p t))
  15232. (org-at-heading-p))
  15233. (org-todo 'up)
  15234. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15235. (defun org-timestamp-down-day (&optional arg)
  15236. "Decrease the date in the time stamp by one day.
  15237. With prefix ARG, change that many days."
  15238. (interactive "p")
  15239. (if (and (not (org-at-timestamp-p t))
  15240. (org-at-heading-p))
  15241. (org-todo 'down)
  15242. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15243. (defun org-at-timestamp-p (&optional inactive-ok)
  15244. "Determine if the cursor is in or at a timestamp."
  15245. (interactive)
  15246. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15247. (pos (point))
  15248. (ans (or (looking-at tsr)
  15249. (save-excursion
  15250. (skip-chars-backward "^[<\n\r\t")
  15251. (if (> (point) (point-min)) (backward-char 1))
  15252. (and (looking-at tsr)
  15253. (> (- (match-end 0) pos) -1))))))
  15254. (and ans
  15255. (boundp 'org-ts-what)
  15256. (setq org-ts-what
  15257. (cond
  15258. ((= pos (match-beginning 0)) 'bracket)
  15259. ;; Point is considered to be "on the bracket" whether
  15260. ;; it's really on it or right after it.
  15261. ((= pos (1- (match-end 0))) 'bracket)
  15262. ((= pos (match-end 0)) 'after)
  15263. ((org-pos-in-match-range pos 2) 'year)
  15264. ((org-pos-in-match-range pos 3) 'month)
  15265. ((org-pos-in-match-range pos 7) 'hour)
  15266. ((org-pos-in-match-range pos 8) 'minute)
  15267. ((or (org-pos-in-match-range pos 4)
  15268. (org-pos-in-match-range pos 5)) 'day)
  15269. ((and (> pos (or (match-end 8) (match-end 5)))
  15270. (< pos (match-end 0)))
  15271. (- pos (or (match-end 8) (match-end 5))))
  15272. (t 'day))))
  15273. ans))
  15274. (defun org-toggle-timestamp-type ()
  15275. "Toggle the type (<active> or [inactive]) of a time stamp."
  15276. (interactive)
  15277. (when (org-at-timestamp-p t)
  15278. (let ((beg (match-beginning 0)) (end (match-end 0))
  15279. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15280. (save-excursion
  15281. (goto-char beg)
  15282. (while (re-search-forward "[][<>]" end t)
  15283. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15284. t t)))
  15285. (message "Timestamp is now %sactive"
  15286. (if (equal (char-after beg) ?<) "" "in")))))
  15287. (defvar org-clock-history) ; defined in org-clock.el
  15288. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15289. (defun org-timestamp-change (n &optional what updown)
  15290. "Change the date in the time stamp at point.
  15291. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15292. `year', `minute', `second'. If WHAT is not given, the cursor position
  15293. in the timestamp determines what will be changed."
  15294. (let ((origin (point)) origin-cat
  15295. with-hm inactive
  15296. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15297. org-ts-what
  15298. extra rem
  15299. ts time time0 fixnext clrgx)
  15300. (if (not (org-at-timestamp-p t))
  15301. (error "Not at a timestamp"))
  15302. (if (and (not what) (eq org-ts-what 'bracket))
  15303. (org-toggle-timestamp-type)
  15304. ;; Point isn't on brackets. Remember the part of the time-stamp
  15305. ;; the point was in. Indeed, size of time-stamps may change,
  15306. ;; but point must be kept in the same category nonetheless.
  15307. (setq origin-cat org-ts-what)
  15308. (if (and (not what) (not (eq org-ts-what 'day))
  15309. org-display-custom-times
  15310. (get-text-property (point) 'display)
  15311. (not (get-text-property (1- (point)) 'display)))
  15312. (setq org-ts-what 'day))
  15313. (setq org-ts-what (or what org-ts-what)
  15314. inactive (= (char-after (match-beginning 0)) ?\[)
  15315. ts (match-string 0))
  15316. (replace-match "")
  15317. (if (string-match
  15318. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15319. ts)
  15320. (setq extra (match-string 1 ts)))
  15321. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15322. (setq with-hm t))
  15323. (setq time0 (org-parse-time-string ts))
  15324. (when (and updown
  15325. (eq org-ts-what 'minute)
  15326. (not current-prefix-arg))
  15327. ;; This looks like s-up and s-down. Change by one rounding step.
  15328. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15329. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15330. (setcar (cdr time0) (+ (nth 1 time0)
  15331. (if (> n 0) (- rem) (- dm rem))))))
  15332. (setq time
  15333. (encode-time (or (car time0) 0)
  15334. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15335. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15336. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15337. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15338. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15339. (nthcdr 6 time0)))
  15340. (when (and (member org-ts-what '(hour minute))
  15341. extra
  15342. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15343. (setq extra (org-modify-ts-extra
  15344. extra
  15345. (if (eq org-ts-what 'hour) 2 5)
  15346. n dm)))
  15347. (when (integerp org-ts-what)
  15348. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15349. (if (eq what 'calendar)
  15350. (let ((cal-date (org-get-date-from-calendar)))
  15351. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15352. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15353. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15354. (setcar time0 (or (car time0) 0))
  15355. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15356. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15357. (setq time (apply 'encode-time time0))))
  15358. ;; Insert the new time-stamp, and ensure point stays in the same
  15359. ;; category as before (i.e. not after the last position in that
  15360. ;; category).
  15361. (let ((pos (point)))
  15362. ;; Stay before inserted string. `save-excursion' is of no use.
  15363. (setq org-last-changed-timestamp
  15364. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15365. (goto-char pos))
  15366. (save-match-data
  15367. (looking-at org-ts-regexp3)
  15368. (goto-char (cond
  15369. ;; `day' category ends before `hour' if any, or at
  15370. ;; the end of the day name.
  15371. ((eq origin-cat 'day)
  15372. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15373. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15374. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15375. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15376. ;; `year' and `month' have both fixed size: point
  15377. ;; couldn't have moved into another part.
  15378. (t origin))))
  15379. ;; Update clock if on a CLOCK line.
  15380. (org-clock-update-time-maybe)
  15381. ;; Maybe adjust the closest clock in `org-clock-history'
  15382. (when org-clock-adjust-closest
  15383. (if (not (and (org-at-clock-log-p)
  15384. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15385. org-clock-history))))))
  15386. (message "No clock to adjust")
  15387. (cond ((save-excursion ; fix previous clock?
  15388. (re-search-backward org-ts-regexp0 nil t)
  15389. (org-looking-back (concat org-clock-string " \\[")))
  15390. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15391. ((save-excursion ; fix next clock?
  15392. (re-search-backward org-ts-regexp0 nil t)
  15393. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15394. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15395. (save-window-excursion
  15396. ;; Find closest clock to point, adjust the previous/next one in history
  15397. (let* ((p (save-excursion (org-back-to-heading t)))
  15398. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15399. (clfixnth
  15400. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15401. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15402. (if (not clfixpos)
  15403. (message "No clock to adjust")
  15404. (save-excursion
  15405. (org-goto-marker-or-bmk clfixpos)
  15406. (org-show-subtree)
  15407. (when (re-search-forward clrgx nil t)
  15408. (goto-char (match-beginning 1))
  15409. (let (org-clock-adjust-closest)
  15410. (org-timestamp-change n org-ts-what updown))
  15411. (message "Clock adjusted in %s for heading: %s"
  15412. (file-name-nondirectory (buffer-file-name))
  15413. (org-get-heading t t)))))))))
  15414. ;; Try to recenter the calendar window, if any.
  15415. (if (and org-calendar-follow-timestamp-change
  15416. (get-buffer-window "*Calendar*" t)
  15417. (memq org-ts-what '(day month year)))
  15418. (org-recenter-calendar (time-to-days time))))))
  15419. (defun org-modify-ts-extra (s pos n dm)
  15420. "Change the different parts of the lead-time and repeat fields in timestamp."
  15421. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15422. ng h m new rem)
  15423. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15424. (cond
  15425. ((or (org-pos-in-match-range pos 2)
  15426. (org-pos-in-match-range pos 3))
  15427. (setq m (string-to-number (match-string 3 s))
  15428. h (string-to-number (match-string 2 s)))
  15429. (if (org-pos-in-match-range pos 2)
  15430. (setq h (+ h n))
  15431. (setq n (* dm (org-no-warnings (signum n))))
  15432. (when (not (= 0 (setq rem (% m dm))))
  15433. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15434. (setq m (+ m n)))
  15435. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15436. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15437. (setq h (min 24 (max 0 h)))
  15438. (setq ng 1 new (format "-%02d:%02d" h m)))
  15439. ((org-pos-in-match-range pos 6)
  15440. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15441. ((org-pos-in-match-range pos 5)
  15442. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15443. ((org-pos-in-match-range pos 9)
  15444. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15445. ((org-pos-in-match-range pos 8)
  15446. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15447. (when ng
  15448. (setq s (concat
  15449. (substring s 0 (match-beginning ng))
  15450. new
  15451. (substring s (match-end ng))))))
  15452. s))
  15453. (defun org-recenter-calendar (date)
  15454. "If the calendar is visible, recenter it to DATE."
  15455. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15456. (when cwin
  15457. (let ((calendar-move-hook nil))
  15458. (with-selected-window cwin
  15459. (calendar-goto-date (if (listp date) date
  15460. (calendar-gregorian-from-absolute date))))))))
  15461. (defun org-goto-calendar (&optional arg)
  15462. "Go to the Emacs calendar at the current date.
  15463. If there is a time stamp in the current line, go to that date.
  15464. A prefix ARG can be used to force the current date."
  15465. (interactive "P")
  15466. (let ((tsr org-ts-regexp) diff
  15467. (calendar-move-hook nil)
  15468. (calendar-view-holidays-initially-flag nil)
  15469. (calendar-view-diary-initially-flag nil))
  15470. (if (or (org-at-timestamp-p)
  15471. (save-excursion
  15472. (beginning-of-line 1)
  15473. (looking-at (concat ".*" tsr))))
  15474. (let ((d1 (time-to-days (current-time)))
  15475. (d2 (time-to-days
  15476. (org-time-string-to-time (match-string 1)))))
  15477. (setq diff (- d2 d1))))
  15478. (calendar)
  15479. (calendar-goto-today)
  15480. (if (and diff (not arg)) (calendar-forward-day diff))))
  15481. (defun org-get-date-from-calendar ()
  15482. "Return a list (month day year) of date at point in calendar."
  15483. (with-current-buffer "*Calendar*"
  15484. (save-match-data
  15485. (calendar-cursor-to-date))))
  15486. (defun org-date-from-calendar ()
  15487. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15488. If there is already a time stamp at the cursor position, update it."
  15489. (interactive)
  15490. (if (org-at-timestamp-p t)
  15491. (org-timestamp-change 0 'calendar)
  15492. (let ((cal-date (org-get-date-from-calendar)))
  15493. (org-insert-time-stamp
  15494. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15495. (defcustom org-effort-durations
  15496. `(("h" . 60)
  15497. ("d" . ,(* 60 8))
  15498. ("w" . ,(* 60 8 5))
  15499. ("m" . ,(* 60 8 5 4))
  15500. ("y" . ,(* 60 8 5 40)))
  15501. "Conversion factor to minutes for an effort modifier.
  15502. Each entry has the form (MODIFIER . MINUTES).
  15503. In an effort string, a number followed by MODIFIER is multiplied
  15504. by the specified number of MINUTES to obtain an effort in
  15505. minutes.
  15506. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15507. effort string \"2hours\" is equivalent to 120 minutes."
  15508. :group 'org-agenda
  15509. :version "24.1"
  15510. :type '(alist :key-type (string :tag "Modifier")
  15511. :value-type (number :tag "Minutes")))
  15512. (defun org-minutes-to-clocksum-string (m)
  15513. "Format number of minutes as a clocksum string.
  15514. The format is determined by `org-time-clocksum-format',
  15515. `org-time-clocksum-use-fractional' and
  15516. `org-time-clocksum-fractional-format' and
  15517. `org-time-clocksum-use-effort-durations'."
  15518. (let ((clocksum "") h d w mo y fmt n)
  15519. (setq h (if org-time-clocksum-use-effort-durations
  15520. (cdr (assoc "h" org-effort-durations)) 60)
  15521. d (if org-time-clocksum-use-effort-durations
  15522. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  15523. w (if org-time-clocksum-use-effort-durations
  15524. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  15525. mo (if org-time-clocksum-use-effort-durations
  15526. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  15527. y (if org-time-clocksum-use-effort-durations
  15528. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  15529. ;; fractional format
  15530. (if org-time-clocksum-use-fractional
  15531. (cond
  15532. ;; single format string
  15533. ((stringp org-time-clocksum-fractional-format)
  15534. (format org-time-clocksum-fractional-format (/ m (float h))))
  15535. ;; choice of fractional formats for different time units
  15536. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15537. (> (/ (truncate m) (* y d h)) 0))
  15538. (format fmt (/ m (* y d (float h)))))
  15539. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15540. (> (/ (truncate m) (* mo d h)) 0))
  15541. (format fmt (/ m (* mo d (float h)))))
  15542. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15543. (> (/ (truncate m) (* w d h)) 0))
  15544. (format fmt (/ m (* w d (float h)))))
  15545. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15546. (> (/ (truncate m) (* d h)) 0))
  15547. (format fmt (/ m (* d (float h)))))
  15548. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15549. (> (/ (truncate m) h) 0))
  15550. (format fmt (/ m (float h))))
  15551. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  15552. (format fmt m))
  15553. ;; fall back to smallest time unit with a format
  15554. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15555. (format fmt (/ m (float h))))
  15556. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15557. (format fmt (/ m (* d (float h)))))
  15558. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15559. (format fmt (/ m (* w d (float h)))))
  15560. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15561. (format fmt (/ m (* mo d (float h)))))
  15562. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15563. (format fmt (/ m (* y d (float h))))))
  15564. ;; standard (non-fractional) format, with single format string
  15565. (if (stringp org-time-clocksum-format)
  15566. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  15567. ;; separate formats components
  15568. (and (setq fmt (plist-get org-time-clocksum-format :years))
  15569. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  15570. (plist-get org-time-clocksum-format :require-years))
  15571. (setq clocksum (concat clocksum (format fmt n))
  15572. m (- m (* n y d h))))
  15573. (and (setq fmt (plist-get org-time-clocksum-format :months))
  15574. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  15575. (plist-get org-time-clocksum-format :require-months))
  15576. (setq clocksum (concat clocksum (format fmt n))
  15577. m (- m (* n mo d h))))
  15578. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  15579. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  15580. (plist-get org-time-clocksum-format :require-weeks))
  15581. (setq clocksum (concat clocksum (format fmt n))
  15582. m (- m (* n w d h))))
  15583. (and (setq fmt (plist-get org-time-clocksum-format :days))
  15584. (or (> (setq n (/ (truncate m) (* d h))) 0)
  15585. (plist-get org-time-clocksum-format :require-days))
  15586. (setq clocksum (concat clocksum (format fmt n))
  15587. m (- m (* n d h))))
  15588. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  15589. (or (> (setq n (/ (truncate m) h)) 0)
  15590. (plist-get org-time-clocksum-format :require-hours))
  15591. (setq clocksum (concat clocksum (format fmt n))
  15592. m (- m (* n h))))
  15593. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  15594. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  15595. (setq clocksum (concat clocksum (format fmt m))))
  15596. ;; return formatted time duration
  15597. clocksum))))
  15598. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  15599. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  15600. "Org mode version 8.0")
  15601. (defun org-hours-to-clocksum-string (n)
  15602. (org-minutes-to-clocksum-string (* n 60)))
  15603. (defun org-hh:mm-string-to-minutes (s)
  15604. "Convert a string H:MM to a number of minutes.
  15605. If the string is just a number, interpret it as minutes.
  15606. In fact, the first hh:mm or number in the string will be taken,
  15607. there can be extra stuff in the string.
  15608. If no number is found, the return value is 0."
  15609. (cond
  15610. ((integerp s) s)
  15611. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15612. (+ (* (string-to-number (match-string 1 s)) 60)
  15613. (string-to-number (match-string 2 s))))
  15614. ((string-match "\\([0-9]+\\)" s)
  15615. (string-to-number (match-string 1 s)))
  15616. (t 0)))
  15617. (defcustom org-image-actual-width t
  15618. "Should we use the actual width of images when inlining them?
  15619. When set to `t', always use the image width.
  15620. When set to a number, use imagemagick (when available) to set
  15621. the image's width to this value.
  15622. When set to a number in a list, try to get the width from the
  15623. #+ATTR.* keyword if it matches a width specification like
  15624. width=\"[0-9]+\", and fall back on that number if none is found.
  15625. When set to nil, try to get the width from an #+ATTR.* keyword
  15626. and fall back on the original width if none is found.
  15627. This requires Emacs >= 24.1, build with imagemagick support."
  15628. :group 'org-appearance
  15629. :version "24.3"
  15630. :type '(choice
  15631. (const :tag "Use the image width" t)
  15632. (integer :tag "Use a number of pixels")
  15633. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15634. (const :tag "Use #+ATTR* or don't resize" nil)))
  15635. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15636. "Convert a duration string S to minutes.
  15637. A bare number is interpreted as minutes, modifiers can be set by
  15638. customizing `org-effort-durations' (which see).
  15639. Entries containing a colon are interpreted as H:MM by
  15640. `org-hh:mm-string-to-minutes'."
  15641. (let ((result 0)
  15642. (re (concat "\\([0-9.]+\\) *\\("
  15643. (regexp-opt (mapcar 'car org-effort-durations))
  15644. "\\)")))
  15645. (while (string-match re s)
  15646. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15647. (string-to-number (match-string 1 s))))
  15648. (setq s (replace-match "" nil t s)))
  15649. (setq result (floor result))
  15650. (incf result (org-hh:mm-string-to-minutes s))
  15651. (if output-to-string (number-to-string result) result)))
  15652. ;;;; Files
  15653. (defun org-save-all-org-buffers ()
  15654. "Save all Org-mode buffers without user confirmation."
  15655. (interactive)
  15656. (message "Saving all Org-mode buffers...")
  15657. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15658. (when (featurep 'org-id) (org-id-locations-save))
  15659. (message "Saving all Org-mode buffers... done"))
  15660. (defun org-revert-all-org-buffers ()
  15661. "Revert all Org-mode buffers.
  15662. Prompt for confirmation when there are unsaved changes.
  15663. Be sure you know what you are doing before letting this function
  15664. overwrite your changes.
  15665. This function is useful in a setup where one tracks org files
  15666. with a version control system, to revert on one machine after pulling
  15667. changes from another. I believe the procedure must be like this:
  15668. 1. M-x org-save-all-org-buffers
  15669. 2. Pull changes from the other machine, resolve conflicts
  15670. 3. M-x org-revert-all-org-buffers"
  15671. (interactive)
  15672. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15673. (error "Abort"))
  15674. (save-excursion
  15675. (save-window-excursion
  15676. (mapc
  15677. (lambda (b)
  15678. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15679. (with-current-buffer b buffer-file-name))
  15680. (org-pop-to-buffer-same-window b)
  15681. (revert-buffer t 'no-confirm)))
  15682. (buffer-list))
  15683. (when (and (featurep 'org-id) org-id-track-globally)
  15684. (org-id-locations-load)))))
  15685. ;;;; Agenda files
  15686. ;;;###autoload
  15687. (defun org-switchb (&optional arg)
  15688. "Switch between Org buffers.
  15689. With one prefix argument, restrict available buffers to files.
  15690. With two prefix arguments, restrict available buffers to agenda files.
  15691. Defaults to `iswitchb' for buffer name completion.
  15692. Set `org-completion-use-ido' to make it use ido instead."
  15693. (interactive "P")
  15694. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15695. ((equal arg '(16)) (org-buffer-list 'agenda))
  15696. (t (org-buffer-list))))
  15697. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15698. (org-completion-use-ido org-completion-use-ido))
  15699. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15700. (setq org-completion-use-iswitchb t))
  15701. (org-pop-to-buffer-same-window
  15702. (org-icompleting-read "Org buffer: "
  15703. (mapcar 'list (mapcar 'buffer-name blist))
  15704. nil t))))
  15705. ;;; Define some older names previously used for this functionality
  15706. ;;;###autoload
  15707. (defalias 'org-ido-switchb 'org-switchb)
  15708. ;;;###autoload
  15709. (defalias 'org-iswitchb 'org-switchb)
  15710. (defun org-buffer-list (&optional predicate exclude-tmp)
  15711. "Return a list of Org buffers.
  15712. PREDICATE can be `export', `files' or `agenda'.
  15713. export restrict the list to Export buffers.
  15714. files restrict the list to buffers visiting Org files.
  15715. agenda restrict the list to buffers visiting agenda files.
  15716. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15717. (let* ((bfn nil)
  15718. (agenda-files (and (eq predicate 'agenda)
  15719. (mapcar 'file-truename (org-agenda-files t))))
  15720. (filter
  15721. (cond
  15722. ((eq predicate 'files)
  15723. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15724. ((eq predicate 'export)
  15725. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15726. ((eq predicate 'agenda)
  15727. (lambda (b)
  15728. (with-current-buffer b
  15729. (and (derived-mode-p 'org-mode)
  15730. (setq bfn (buffer-file-name b))
  15731. (member (file-truename bfn) agenda-files)))))
  15732. (t (lambda (b) (with-current-buffer b
  15733. (or (derived-mode-p 'org-mode)
  15734. (string-match "\*Org .*Export"
  15735. (buffer-name b)))))))))
  15736. (delq nil
  15737. (mapcar
  15738. (lambda(b)
  15739. (if (and (funcall filter b)
  15740. (or (not exclude-tmp)
  15741. (not (string-match "tmp" (buffer-name b)))))
  15742. b
  15743. nil))
  15744. (buffer-list)))))
  15745. (defun org-agenda-files (&optional unrestricted archives)
  15746. "Get the list of agenda files.
  15747. Optional UNRESTRICTED means return the full list even if a restriction
  15748. is currently in place.
  15749. When ARCHIVES is t, include all archive files that are really being
  15750. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15751. `org-agenda-archives-mode' is t."
  15752. (let ((files
  15753. (cond
  15754. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15755. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15756. ((listp org-agenda-files) org-agenda-files)
  15757. (t (error "Invalid value of `org-agenda-files'")))))
  15758. (setq files (apply 'append
  15759. (mapcar (lambda (f)
  15760. (if (file-directory-p f)
  15761. (directory-files
  15762. f t org-agenda-file-regexp)
  15763. (list f)))
  15764. files)))
  15765. (when org-agenda-skip-unavailable-files
  15766. (setq files (delq nil
  15767. (mapcar (function
  15768. (lambda (file)
  15769. (and (file-readable-p file) file)))
  15770. files))))
  15771. (when (or (eq archives t)
  15772. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15773. (setq files (org-add-archive-files files)))
  15774. files))
  15775. (defun org-agenda-file-p (&optional file)
  15776. "Return non-nil, if FILE is an agenda file.
  15777. If FILE is omitted, use the file associated with the current
  15778. buffer."
  15779. (member (or file (buffer-file-name))
  15780. (org-agenda-files t)))
  15781. (defun org-edit-agenda-file-list ()
  15782. "Edit the list of agenda files.
  15783. Depending on setup, this either uses customize to edit the variable
  15784. `org-agenda-files', or it visits the file that is holding the list. In the
  15785. latter case, the buffer is set up in a way that saving it automatically kills
  15786. the buffer and restores the previous window configuration."
  15787. (interactive)
  15788. (if (stringp org-agenda-files)
  15789. (let ((cw (current-window-configuration)))
  15790. (find-file org-agenda-files)
  15791. (org-set-local 'org-window-configuration cw)
  15792. (org-add-hook 'after-save-hook
  15793. (lambda ()
  15794. (set-window-configuration
  15795. (prog1 org-window-configuration
  15796. (kill-buffer (current-buffer))))
  15797. (org-install-agenda-files-menu)
  15798. (message "New agenda file list installed"))
  15799. nil 'local)
  15800. (message "%s" (substitute-command-keys
  15801. "Edit list and finish with \\[save-buffer]")))
  15802. (customize-variable 'org-agenda-files)))
  15803. (defun org-store-new-agenda-file-list (list)
  15804. "Set new value for the agenda file list and save it correctly."
  15805. (if (stringp org-agenda-files)
  15806. (let ((fe (org-read-agenda-file-list t)) b u)
  15807. (while (setq b (find-buffer-visiting org-agenda-files))
  15808. (kill-buffer b))
  15809. (with-temp-file org-agenda-files
  15810. (insert
  15811. (mapconcat
  15812. (lambda (f) ;; Keep un-expanded entries.
  15813. (if (setq u (assoc f fe))
  15814. (cdr u)
  15815. f))
  15816. list "\n")
  15817. "\n")))
  15818. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15819. (org-insert-mode-line-in-empty-file nil))
  15820. (setq org-agenda-files list)
  15821. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15822. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15823. "Read the list of agenda files from a file.
  15824. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15825. filenames, used by `org-store-new-agenda-file-list' to write back
  15826. un-expanded file names."
  15827. (when (file-directory-p org-agenda-files)
  15828. (error "`org-agenda-files' cannot be a single directory"))
  15829. (when (stringp org-agenda-files)
  15830. (with-temp-buffer
  15831. (insert-file-contents org-agenda-files)
  15832. (mapcar
  15833. (lambda (f)
  15834. (let ((e (expand-file-name (substitute-in-file-name f)
  15835. org-directory)))
  15836. (if pair-with-expansion
  15837. (cons e f)
  15838. e)))
  15839. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15840. ;;;###autoload
  15841. (defun org-cycle-agenda-files ()
  15842. "Cycle through the files in `org-agenda-files'.
  15843. If the current buffer visits an agenda file, find the next one in the list.
  15844. If the current buffer does not, find the first agenda file."
  15845. (interactive)
  15846. (let* ((fs (org-agenda-files t))
  15847. (files (append fs (list (car fs))))
  15848. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15849. file)
  15850. (unless files (error "No agenda files"))
  15851. (catch 'exit
  15852. (while (setq file (pop files))
  15853. (if (equal (file-truename file) tcf)
  15854. (when (car files)
  15855. (find-file (car files))
  15856. (throw 'exit t))))
  15857. (find-file (car fs)))
  15858. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15859. (defun org-agenda-file-to-front (&optional to-end)
  15860. "Move/add the current file to the top of the agenda file list.
  15861. If the file is not present in the list, it is added to the front. If it is
  15862. present, it is moved there. With optional argument TO-END, add/move to the
  15863. end of the list."
  15864. (interactive "P")
  15865. (let ((org-agenda-skip-unavailable-files nil)
  15866. (file-alist (mapcar (lambda (x)
  15867. (cons (file-truename x) x))
  15868. (org-agenda-files t)))
  15869. (ctf (file-truename
  15870. (or buffer-file-name
  15871. (error "Please save the current buffer to a file"))))
  15872. x had)
  15873. (setq x (assoc ctf file-alist) had x)
  15874. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15875. (if to-end
  15876. (setq file-alist (append (delq x file-alist) (list x)))
  15877. (setq file-alist (cons x (delq x file-alist))))
  15878. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15879. (org-install-agenda-files-menu)
  15880. (message "File %s to %s of agenda file list"
  15881. (if had "moved" "added") (if to-end "end" "front"))))
  15882. (defun org-remove-file (&optional file)
  15883. "Remove current file from the list of files in variable `org-agenda-files'.
  15884. These are the files which are being checked for agenda entries.
  15885. Optional argument FILE means use this file instead of the current."
  15886. (interactive)
  15887. (let* ((org-agenda-skip-unavailable-files nil)
  15888. (file (or file buffer-file-name
  15889. (error "Current buffer does not visit a file")))
  15890. (true-file (file-truename file))
  15891. (afile (abbreviate-file-name file))
  15892. (files (delq nil (mapcar
  15893. (lambda (x)
  15894. (if (equal true-file
  15895. (file-truename x))
  15896. nil x))
  15897. (org-agenda-files t)))))
  15898. (if (not (= (length files) (length (org-agenda-files t))))
  15899. (progn
  15900. (org-store-new-agenda-file-list files)
  15901. (org-install-agenda-files-menu)
  15902. (message "Removed file: %s" afile))
  15903. (message "File was not in list: %s (not removed)" afile))))
  15904. (defun org-file-menu-entry (file)
  15905. (vector file (list 'find-file file) t))
  15906. (defun org-check-agenda-file (file)
  15907. "Make sure FILE exists. If not, ask user what to do."
  15908. (when (not (file-exists-p file))
  15909. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  15910. (abbreviate-file-name file))
  15911. (let ((r (downcase (read-char-exclusive))))
  15912. (cond
  15913. ((equal r ?r)
  15914. (org-remove-file file)
  15915. (throw 'nextfile t))
  15916. (t (error "Abort"))))))
  15917. (defun org-get-agenda-file-buffer (file)
  15918. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15919. it to the list of buffers which might be released later."
  15920. (let ((buf (org-find-base-buffer-visiting file)))
  15921. (if buf
  15922. buf ; just return it
  15923. ;; Make a new buffer and remember it
  15924. (setq buf (find-file-noselect file))
  15925. (if buf (push buf org-agenda-new-buffers))
  15926. buf)))
  15927. (defun org-release-buffers (blist)
  15928. "Release all buffers in list, asking the user for confirmation when needed.
  15929. When a buffer is unmodified, it is just killed. When modified, it is saved
  15930. \(if the user agrees) and then killed."
  15931. (let (buf file)
  15932. (while (setq buf (pop blist))
  15933. (setq file (buffer-file-name buf))
  15934. (when (and (buffer-modified-p buf)
  15935. file
  15936. (y-or-n-p (format "Save file %s? " file)))
  15937. (with-current-buffer buf (save-buffer)))
  15938. (kill-buffer buf))))
  15939. (defun org-agenda-prepare-buffers (files)
  15940. "Create buffers for all agenda files, protect archived trees and comments."
  15941. (interactive)
  15942. (let ((pa '(:org-archived t))
  15943. (pc '(:org-comment t))
  15944. (pall '(:org-archived t :org-comment t))
  15945. (inhibit-read-only t)
  15946. (rea (concat ":" org-archive-tag ":"))
  15947. bmp file re)
  15948. (save-excursion
  15949. (save-restriction
  15950. (while (setq file (pop files))
  15951. (catch 'nextfile
  15952. (if (bufferp file)
  15953. (set-buffer file)
  15954. (org-check-agenda-file file)
  15955. (set-buffer (org-get-agenda-file-buffer file)))
  15956. (widen)
  15957. (setq bmp (buffer-modified-p))
  15958. (org-refresh-category-properties)
  15959. (org-refresh-properties org-effort-property 'org-effort)
  15960. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  15961. (setq org-todo-keywords-for-agenda
  15962. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15963. (setq org-done-keywords-for-agenda
  15964. (append org-done-keywords-for-agenda org-done-keywords))
  15965. (setq org-todo-keyword-alist-for-agenda
  15966. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15967. (setq org-drawers-for-agenda
  15968. (append org-drawers-for-agenda org-drawers))
  15969. (setq org-tag-alist-for-agenda
  15970. (append org-tag-alist-for-agenda org-tag-alist))
  15971. (save-excursion
  15972. (remove-text-properties (point-min) (point-max) pall)
  15973. (when org-agenda-skip-archived-trees
  15974. (goto-char (point-min))
  15975. (while (re-search-forward rea nil t)
  15976. (if (org-at-heading-p t)
  15977. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15978. (goto-char (point-min))
  15979. (setq re (format org-heading-keyword-regexp-format
  15980. org-comment-string))
  15981. (while (re-search-forward re nil t)
  15982. (add-text-properties
  15983. (match-beginning 0) (org-end-of-subtree t) pc)))
  15984. (set-buffer-modified-p bmp)))))
  15985. (setq org-todo-keywords-for-agenda
  15986. (org-uniquify org-todo-keywords-for-agenda))
  15987. (setq org-todo-keyword-alist-for-agenda
  15988. (org-uniquify org-todo-keyword-alist-for-agenda)
  15989. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15990. ;;;; Embedded LaTeX
  15991. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15992. "Keymap for the minor `org-cdlatex-mode'.")
  15993. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15994. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15995. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15996. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15997. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15998. (defvar org-cdlatex-texmathp-advice-is-done nil
  15999. "Flag remembering if we have applied the advice to texmathp already.")
  16000. (define-minor-mode org-cdlatex-mode
  16001. "Toggle the minor `org-cdlatex-mode'.
  16002. This mode supports entering LaTeX environment and math in LaTeX fragments
  16003. in Org-mode.
  16004. \\{org-cdlatex-mode-map}"
  16005. nil " OCDL" nil
  16006. (when org-cdlatex-mode
  16007. (require 'cdlatex)
  16008. (run-hooks 'cdlatex-mode-hook)
  16009. (cdlatex-compute-tables))
  16010. (unless org-cdlatex-texmathp-advice-is-done
  16011. (setq org-cdlatex-texmathp-advice-is-done t)
  16012. (defadvice texmathp (around org-math-always-on activate)
  16013. "Always return t in org-mode buffers.
  16014. This is because we want to insert math symbols without dollars even outside
  16015. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16016. an embedded LaTeX fragment, let texmathp do its job.
  16017. \\[org-cdlatex-mode-map]"
  16018. (interactive)
  16019. (let (p)
  16020. (cond
  16021. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16022. ((eq this-command 'cdlatex-math-symbol)
  16023. (setq ad-return-value t
  16024. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16025. (t
  16026. (let ((p (org-inside-LaTeX-fragment-p)))
  16027. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16028. (setq ad-return-value t
  16029. texmathp-why '("Org-mode embedded math" . 0))
  16030. (if p ad-do-it)))))))))
  16031. (defun turn-on-org-cdlatex ()
  16032. "Unconditionally turn on `org-cdlatex-mode'."
  16033. (org-cdlatex-mode 1))
  16034. (defun org-inside-LaTeX-fragment-p ()
  16035. "Test if point is inside a LaTeX fragment.
  16036. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16037. sequence appearing also before point.
  16038. Even though the matchers for math are configurable, this function assumes
  16039. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16040. delimiters are skipped when they have been removed by customization.
  16041. The return value is nil, or a cons cell with the delimiter and the
  16042. position of this delimiter.
  16043. This function does a reasonably good job, but can locally be fooled by
  16044. for example currency specifications. For example it will assume being in
  16045. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16046. fragments that are properly closed, but during editing, we have to live
  16047. with the uncertainty caused by missing closing delimiters. This function
  16048. looks only before point, not after."
  16049. (catch 'exit
  16050. (let ((pos (point))
  16051. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16052. (lim (progn
  16053. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16054. (point)))
  16055. dd-on str (start 0) m re)
  16056. (goto-char pos)
  16057. (when dodollar
  16058. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16059. re (nth 1 (assoc "$" org-latex-regexps)))
  16060. (while (string-match re str start)
  16061. (cond
  16062. ((= (match-end 0) (length str))
  16063. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16064. ((= (match-end 0) (- (length str) 5))
  16065. (throw 'exit nil))
  16066. (t (setq start (match-end 0))))))
  16067. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16068. (goto-char pos)
  16069. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16070. (and (match-beginning 2) (throw 'exit nil))
  16071. ;; count $$
  16072. (while (re-search-backward "\\$\\$" lim t)
  16073. (setq dd-on (not dd-on)))
  16074. (goto-char pos)
  16075. (if dd-on (cons "$$" m))))))
  16076. (defun org-inside-latex-macro-p ()
  16077. "Is point inside a LaTeX macro or its arguments?"
  16078. (save-match-data
  16079. (org-in-regexp
  16080. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16081. (defun org-try-cdlatex-tab ()
  16082. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16083. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16084. - inside a LaTeX fragment, or
  16085. - after the first word in a line, where an abbreviation expansion could
  16086. insert a LaTeX environment."
  16087. (when org-cdlatex-mode
  16088. (cond
  16089. ;; Before any word on the line: No expansion possible.
  16090. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16091. ;; Just after first word on the line: Expand it. Make sure it
  16092. ;; cannot happen on headlines, though.
  16093. ((save-excursion
  16094. (skip-chars-backward "a-zA-Z0-9*")
  16095. (skip-chars-backward " \t")
  16096. (and (bolp) (not (org-at-heading-p))))
  16097. (cdlatex-tab) t)
  16098. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16099. (defun org-cdlatex-underscore-caret (&optional arg)
  16100. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16101. Revert to the normal definition outside of these fragments."
  16102. (interactive "P")
  16103. (if (org-inside-LaTeX-fragment-p)
  16104. (call-interactively 'cdlatex-sub-superscript)
  16105. (let (org-cdlatex-mode)
  16106. (call-interactively (key-binding (vector last-input-event))))))
  16107. (defun org-cdlatex-math-modify (&optional arg)
  16108. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16109. Revert to the normal definition outside of these fragments."
  16110. (interactive "P")
  16111. (if (org-inside-LaTeX-fragment-p)
  16112. (call-interactively 'cdlatex-math-modify)
  16113. (let (org-cdlatex-mode)
  16114. (call-interactively (key-binding (vector last-input-event))))))
  16115. (defvar org-latex-fragment-image-overlays nil
  16116. "List of overlays carrying the images of latex fragments.")
  16117. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16118. (defun org-remove-latex-fragment-image-overlays ()
  16119. "Remove all overlays with LaTeX fragment images in current buffer."
  16120. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16121. (setq org-latex-fragment-image-overlays nil))
  16122. (defun org-preview-latex-fragment (&optional subtree)
  16123. "Preview the LaTeX fragment at point, or all locally or globally.
  16124. If the cursor is in a LaTeX fragment, create the image and overlay
  16125. it over the source code. If there is no fragment at point, display
  16126. all fragments in the current text, from one headline to the next. With
  16127. prefix SUBTREE, display all fragments in the current subtree. With a
  16128. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16129. the cursor is before the first headline,
  16130. display all fragments in the buffer.
  16131. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16132. (interactive "P")
  16133. (unless buffer-file-name
  16134. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16135. (org-remove-latex-fragment-image-overlays)
  16136. (save-excursion
  16137. (save-restriction
  16138. (let (beg end at msg)
  16139. (cond
  16140. ((or (equal subtree '(16))
  16141. (not (save-excursion
  16142. (re-search-backward org-outline-regexp-bol nil t))))
  16143. (setq beg (point-min) end (point-max)
  16144. msg "Creating images for buffer...%s"))
  16145. ((equal subtree '(4))
  16146. (org-back-to-heading)
  16147. (setq beg (point) end (org-end-of-subtree t)
  16148. msg "Creating images for subtree...%s"))
  16149. (t
  16150. (if (setq at (org-inside-LaTeX-fragment-p))
  16151. (goto-char (max (point-min) (- (cdr at) 2)))
  16152. (org-back-to-heading))
  16153. (setq beg (point) end (progn (outline-next-heading) (point))
  16154. msg (if at "Creating image...%s"
  16155. "Creating images for entry...%s"))))
  16156. (message msg "")
  16157. (narrow-to-region beg end)
  16158. (goto-char beg)
  16159. (org-format-latex
  16160. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16161. (file-name-nondirectory
  16162. buffer-file-name)))
  16163. default-directory 'overlays msg at 'forbuffer
  16164. org-latex-create-formula-image-program)
  16165. (message msg "done. Use `C-c C-c' to remove images.")))))
  16166. (defvar org-latex-regexps
  16167. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16168. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16169. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16170. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16171. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16172. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16173. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16174. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16175. "Regular expressions for matching embedded LaTeX.")
  16176. (defvar org-export-have-math nil) ;; dynamic scoping
  16177. (defun org-format-latex (prefix &optional dir overlays msg at
  16178. forbuffer processing-type)
  16179. "Replace LaTeX fragments with links to an image, and produce images.
  16180. Some of the options can be changed using the variable
  16181. `org-format-latex-options'."
  16182. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16183. (let* ((prefixnodir (file-name-nondirectory prefix))
  16184. (absprefix (expand-file-name prefix dir))
  16185. (todir (file-name-directory absprefix))
  16186. (opt org-format-latex-options)
  16187. (optnew org-format-latex-options)
  16188. (matchers (plist-get opt :matchers))
  16189. (re-list org-latex-regexps)
  16190. (org-format-latex-header-extra
  16191. (plist-get (org-infile-export-plist) :latex-header-extra))
  16192. (cnt 0) txt hash link beg end re e checkdir
  16193. string
  16194. m n block-type block linkfile movefile ov)
  16195. ;; Check the different regular expressions
  16196. (while (setq e (pop re-list))
  16197. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16198. block (if block-type "\n\n" ""))
  16199. (when (member m matchers)
  16200. (goto-char (point-min))
  16201. (while (re-search-forward re nil t)
  16202. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16203. (not (get-text-property (match-beginning n)
  16204. 'org-protected))
  16205. (or (not overlays)
  16206. (not (eq (get-char-property (match-beginning n)
  16207. 'org-overlay-type)
  16208. 'org-latex-overlay))))
  16209. (setq org-export-have-math t)
  16210. (cond
  16211. ((eq processing-type 'verbatim)
  16212. ;; Leave the text verbatim, just protect it
  16213. (add-text-properties (match-beginning n) (match-end n)
  16214. '(org-protected t)))
  16215. ((eq processing-type 'mathjax)
  16216. ;; Prepare for MathJax processing
  16217. (setq string (match-string n))
  16218. (if (member m '("$" "$1"))
  16219. (save-excursion
  16220. (delete-region (match-beginning n) (match-end n))
  16221. (goto-char (match-beginning n))
  16222. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  16223. "\\)")
  16224. '(org-protected t))))
  16225. (add-text-properties (match-beginning n) (match-end n)
  16226. '(org-protected t))))
  16227. ((or (eq processing-type 'dvipng)
  16228. (eq processing-type 'imagemagick))
  16229. ;; Process to an image
  16230. (setq txt (match-string n)
  16231. beg (match-beginning n) end (match-end n)
  16232. cnt (1+ cnt))
  16233. (let ((face (face-at-point))
  16234. (fg (plist-get opt :foreground))
  16235. (bg (plist-get opt :background))
  16236. print-length print-level) ; make sure full list is printed
  16237. (when forbuffer
  16238. ; Get the colors from the face at point
  16239. (goto-char beg)
  16240. (when (eq fg 'auto)
  16241. (setq fg (face-attribute face :foreground nil 'default)))
  16242. (when (eq bg 'auto)
  16243. (setq bg (face-attribute face :background nil 'default)))
  16244. (setq optnew (copy-sequence opt))
  16245. (plist-put optnew :foreground fg)
  16246. (plist-put optnew :background bg))
  16247. (setq hash (sha1 (prin1-to-string
  16248. (list org-format-latex-header
  16249. org-format-latex-header-extra
  16250. org-export-latex-default-packages-alist
  16251. org-export-latex-packages-alist
  16252. org-format-latex-options
  16253. forbuffer txt fg bg)))
  16254. linkfile (format "%s_%s.png" prefix hash)
  16255. movefile (format "%s_%s.png" absprefix hash)))
  16256. (setq link (concat block "[[file:" linkfile "]]" block))
  16257. (if msg (message msg cnt))
  16258. (goto-char beg)
  16259. (unless checkdir ; make sure the directory exists
  16260. (setq checkdir t)
  16261. (or (file-directory-p todir) (make-directory todir t)))
  16262. (unless (file-exists-p movefile)
  16263. (org-create-formula-image
  16264. txt movefile optnew forbuffer processing-type))
  16265. (if overlays
  16266. (progn
  16267. (mapc (lambda (o)
  16268. (if (eq (overlay-get o 'org-overlay-type)
  16269. 'org-latex-overlay)
  16270. (delete-overlay o)))
  16271. (overlays-in beg end))
  16272. (setq ov (make-overlay beg end))
  16273. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16274. (if (featurep 'xemacs)
  16275. (progn
  16276. (overlay-put ov 'invisible t)
  16277. (overlay-put
  16278. ov 'end-glyph
  16279. (make-glyph (vector 'png :file movefile))))
  16280. (overlay-put
  16281. ov 'display
  16282. (list 'image :type 'png :file movefile :ascent 'center)))
  16283. (push ov org-latex-fragment-image-overlays)
  16284. (goto-char end))
  16285. (delete-region beg end)
  16286. (insert (org-add-props link
  16287. (list 'org-latex-src
  16288. (replace-regexp-in-string
  16289. "\"" "" txt)
  16290. 'org-latex-src-embed-type
  16291. (if block-type 'paragraph 'character))))))
  16292. ((eq processing-type 'mathml)
  16293. ;; Process to MathML
  16294. (unless (save-match-data (org-format-latex-mathml-available-p))
  16295. (error "LaTeX to MathML converter not configured"))
  16296. (setq txt (match-string n)
  16297. beg (match-beginning n) end (match-end n)
  16298. cnt (1+ cnt))
  16299. (if msg (message msg cnt))
  16300. (goto-char beg)
  16301. (delete-region beg end)
  16302. (insert (org-format-latex-as-mathml
  16303. txt block-type prefix dir)))
  16304. (t
  16305. (error "Unknown conversion type %s for latex fragments"
  16306. processing-type)))))))))
  16307. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16308. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16309. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16310. sucessful, return the portion between \"<math...> </math>\"
  16311. elements otherwise return nil. When MATHML-FILE is specified,
  16312. write the results in to that file. When invoked as an
  16313. interactive command, prompt for LATEX-FRAG, with initial value
  16314. set to the current active region and echo the results for user
  16315. inspection."
  16316. (interactive (list (let ((frag (when (org-region-active-p)
  16317. (buffer-substring-no-properties
  16318. (region-beginning) (region-end)))))
  16319. (read-string "LaTeX Fragment: " frag nil frag))))
  16320. (unless latex-frag (error "Invalid latex-frag"))
  16321. (let* ((tmp-in-file (file-relative-name
  16322. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16323. (ignore (write-region latex-frag nil tmp-in-file))
  16324. (tmp-out-file (file-relative-name
  16325. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16326. (cmd (format-spec
  16327. org-latex-to-mathml-convert-command
  16328. `((?j . ,(shell-quote-argument
  16329. (expand-file-name org-latex-to-mathml-jar-file)))
  16330. (?I . ,(shell-quote-argument tmp-in-file))
  16331. (?o . ,(shell-quote-argument tmp-out-file)))))
  16332. mathml shell-command-output)
  16333. (when (org-called-interactively-p 'any)
  16334. (unless (org-format-latex-mathml-available-p)
  16335. (error "LaTeX to MathML converter not configured")))
  16336. (message "Running %s" cmd)
  16337. (setq shell-command-output (shell-command-to-string cmd))
  16338. (setq mathml
  16339. (when (file-readable-p tmp-out-file)
  16340. (with-current-buffer (find-file-noselect tmp-out-file t)
  16341. (goto-char (point-min))
  16342. (when (re-search-forward
  16343. (concat
  16344. (regexp-quote
  16345. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16346. "\\(.\\|\n\\)*"
  16347. (regexp-quote "</math>")) nil t)
  16348. (prog1 (match-string 0) (kill-buffer))))))
  16349. (cond
  16350. (mathml
  16351. (setq mathml
  16352. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16353. (when mathml-file
  16354. (write-region mathml nil mathml-file))
  16355. (when (org-called-interactively-p 'any)
  16356. (message mathml)))
  16357. ((message "LaTeX to MathML conversion failed")
  16358. (message shell-command-output)))
  16359. (delete-file tmp-in-file)
  16360. (when (file-exists-p tmp-out-file)
  16361. (delete-file tmp-out-file))
  16362. mathml))
  16363. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16364. prefix &optional dir)
  16365. "Use `org-create-math-formula' but check local cache first."
  16366. (let* ((absprefix (expand-file-name prefix dir))
  16367. (print-length nil) (print-level nil)
  16368. (formula-id (concat
  16369. "formula-"
  16370. (sha1
  16371. (prin1-to-string
  16372. (list latex-frag
  16373. org-latex-to-mathml-convert-command)))))
  16374. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16375. (formula-cache-dir (file-name-directory formula-cache)))
  16376. (unless (file-directory-p formula-cache-dir)
  16377. (make-directory formula-cache-dir t))
  16378. (unless (file-exists-p formula-cache)
  16379. (org-create-math-formula latex-frag formula-cache))
  16380. (if (file-exists-p formula-cache)
  16381. ;; Successful conversion. Return the link to MathML file.
  16382. (org-add-props
  16383. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16384. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16385. 'org-latex-src-embed-type (if latex-frag-type
  16386. 'paragraph 'character)))
  16387. ;; Failed conversion. Return the LaTeX fragment verbatim
  16388. (add-text-properties
  16389. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  16390. latex-frag)))
  16391. (defun org-create-formula-image (string tofile options buffer &optional type)
  16392. "Create an image from LaTeX source using dvipng or convert.
  16393. This function calls either `org-create-formula-image-with-dvipng'
  16394. or `org-create-formula-image-with-imagemagick' depending on the
  16395. value of `org-latex-create-formula-image-program' or on the value
  16396. of the optional TYPE variable.
  16397. Note: ultimately these two function should be combined as they
  16398. share a good deal of logic."
  16399. (org-check-external-command
  16400. "latex" "needed to convert LaTeX fragments to images")
  16401. (funcall
  16402. (case (or type org-latex-create-formula-image-program)
  16403. ('dvipng
  16404. (org-check-external-command
  16405. "dvipng" "needed to convert LaTeX fragments to images")
  16406. #'org-create-formula-image-with-dvipng)
  16407. ('imagemagick
  16408. (org-check-external-command
  16409. "convert" "you need to install imagemagick")
  16410. #'org-create-formula-image-with-imagemagick)
  16411. (t (error
  16412. "invalid value of `org-latex-create-formula-image-program'")))
  16413. string tofile options buffer))
  16414. ;; This function borrows from Ganesh Swami's latex2png.el
  16415. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16416. "This calls dvipng."
  16417. (require 'org-latex)
  16418. (let* ((tmpdir (if (featurep 'xemacs)
  16419. (temp-directory)
  16420. temporary-file-directory))
  16421. (texfilebase (make-temp-name
  16422. (expand-file-name "orgtex" tmpdir)))
  16423. (texfile (concat texfilebase ".tex"))
  16424. (dvifile (concat texfilebase ".dvi"))
  16425. (pngfile (concat texfilebase ".png"))
  16426. (fnh (if (featurep 'xemacs)
  16427. (font-height (face-font 'default))
  16428. (face-attribute 'default :height nil)))
  16429. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16430. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16431. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16432. "Black"))
  16433. (bg (or (plist-get options (if buffer :background :html-background))
  16434. "Transparent")))
  16435. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16436. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16437. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16438. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16439. (with-temp-file texfile
  16440. (insert (org-splice-latex-header
  16441. org-format-latex-header
  16442. org-export-latex-default-packages-alist
  16443. org-export-latex-packages-alist t
  16444. org-format-latex-header-extra))
  16445. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  16446. (require 'org-latex)
  16447. (org-export-latex-fix-inputenc))
  16448. (let ((dir default-directory))
  16449. (condition-case nil
  16450. (progn
  16451. (cd tmpdir)
  16452. (call-process "latex" nil nil nil texfile))
  16453. (error nil))
  16454. (cd dir))
  16455. (if (not (file-exists-p dvifile))
  16456. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16457. (condition-case nil
  16458. (if (featurep 'xemacs)
  16459. (call-process "dvipng" nil nil nil
  16460. "-fg" fg "-bg" bg
  16461. "-T" "tight"
  16462. "-o" pngfile
  16463. dvifile)
  16464. (call-process "dvipng" nil nil nil
  16465. "-fg" fg "-bg" bg
  16466. "-D" dpi
  16467. ;;"-x" scale "-y" scale
  16468. "-T" "tight"
  16469. "-o" pngfile
  16470. dvifile))
  16471. (error nil))
  16472. (if (not (file-exists-p pngfile))
  16473. (if org-format-latex-signal-error
  16474. (error "Failed to create png file from %s" texfile)
  16475. (message "Failed to create png file from %s" texfile)
  16476. nil)
  16477. ;; Use the requested file name and clean up
  16478. (copy-file pngfile tofile 'replace)
  16479. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16480. (if (file-exists-p (concat texfilebase e))
  16481. (delete-file (concat texfilebase e))))
  16482. pngfile))))
  16483. (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
  16484. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16485. "This calls convert, which is included into imagemagick."
  16486. (require 'org-latex)
  16487. (let* ((tmpdir (if (featurep 'xemacs)
  16488. (temp-directory)
  16489. temporary-file-directory))
  16490. (texfilebase (make-temp-name
  16491. (expand-file-name "orgtex" tmpdir)))
  16492. (texfile (concat texfilebase ".tex"))
  16493. (pdffile (concat texfilebase ".pdf"))
  16494. (pngfile (concat texfilebase ".png"))
  16495. (fnh (if (featurep 'xemacs)
  16496. (font-height (face-font 'default))
  16497. (face-attribute 'default :height nil)))
  16498. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16499. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16500. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16501. "black"))
  16502. (bg (or (plist-get options (if buffer :background :html-background))
  16503. "white")))
  16504. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16505. (setq fg (org-latex-color-format fg)))
  16506. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16507. (setq bg (org-latex-color-format
  16508. (if (string= bg "Transparent") "white" bg))))
  16509. (with-temp-file texfile
  16510. (insert (org-splice-latex-header
  16511. org-format-latex-header
  16512. org-export-latex-default-packages-alist
  16513. org-export-latex-packages-alist t
  16514. org-format-latex-header-extra))
  16515. (insert "\n\\begin{document}\n"
  16516. "\\definecolor{fg}{rgb}{" fg "}\n"
  16517. "\\definecolor{bg}{rgb}{" bg "}\n"
  16518. "\n\\pagecolor{bg}\n"
  16519. "\n{\\color{fg}\n"
  16520. string
  16521. "\n}\n"
  16522. "\n\\end{document}\n" )
  16523. (require 'org-latex)
  16524. (org-export-latex-fix-inputenc))
  16525. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  16526. (condition-case nil
  16527. (progn
  16528. (cd tmpdir)
  16529. (setq cmds org-latex-to-pdf-process)
  16530. (while cmds
  16531. (setq latex-frags-cmds (pop cmds))
  16532. (if (listp latex-frags-cmds)
  16533. (setq cmds nil)
  16534. (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
  16535. (while latex-frags-cmds
  16536. (setq cmd (pop latex-frags-cmds))
  16537. (while (string-match "%b" cmd)
  16538. (setq cmd (replace-match
  16539. (save-match-data
  16540. (shell-quote-argument texfile))
  16541. t t cmd)))
  16542. (while (string-match "%f" cmd)
  16543. (setq cmd (replace-match
  16544. (save-match-data
  16545. (shell-quote-argument (file-name-nondirectory texfile)))
  16546. t t cmd)))
  16547. (while (string-match "%o" cmd)
  16548. (setq cmd (replace-match
  16549. (save-match-data
  16550. (shell-quote-argument (file-name-directory texfile)))
  16551. t t cmd)))
  16552. (setq cmd (split-string cmd))
  16553. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  16554. (error nil))
  16555. (cd dir))
  16556. (if (not (file-exists-p pdffile))
  16557. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16558. (condition-case nil
  16559. (if (featurep 'xemacs)
  16560. (call-process "convert" nil nil nil
  16561. "-density" "96"
  16562. "-trim"
  16563. "-antialias"
  16564. pdffile
  16565. "-quality" "100"
  16566. ;; "-sharpen" "0x1.0"
  16567. pngfile)
  16568. (call-process "convert" nil nil nil
  16569. "-density" dpi
  16570. "-trim"
  16571. "-antialias"
  16572. pdffile
  16573. "-quality" "100"
  16574. ; "-sharpen" "0x1.0"
  16575. pngfile))
  16576. (error nil))
  16577. (if (not (file-exists-p pngfile))
  16578. (if org-format-latex-signal-error
  16579. (error "Failed to create png file from %s" texfile)
  16580. (message "Failed to create png file from %s" texfile)
  16581. nil)
  16582. ;; Use the requested file name and clean up
  16583. (copy-file pngfile tofile 'replace)
  16584. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16585. (if (file-exists-p (concat texfilebase e))
  16586. (delete-file (concat texfilebase e))))
  16587. pngfile))))
  16588. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16589. "Fill a LaTeX header template TPL.
  16590. In the template, the following place holders will be recognized:
  16591. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16592. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16593. [PACKAGES] \\usepackage statements for PKG
  16594. [NO-PACKAGES] do not include PKG
  16595. [EXTRA] the string EXTRA
  16596. [NO-EXTRA] do not include EXTRA
  16597. For backward compatibility, if both the positive and the negative place
  16598. holder is missing, the positive one (without the \"NO-\") will be
  16599. assumed to be present at the end of the template.
  16600. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16601. EXTRA is a string.
  16602. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16603. (let (rpl (end ""))
  16604. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16605. (setq rpl (if (or (match-end 1) (not def-pkg))
  16606. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16607. tpl (replace-match rpl t t tpl))
  16608. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16609. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16610. (setq rpl (if (or (match-end 1) (not pkg))
  16611. "" (org-latex-packages-to-string pkg snippets-p t))
  16612. tpl (replace-match rpl t t tpl))
  16613. (if pkg (setq end
  16614. (concat end "\n"
  16615. (org-latex-packages-to-string pkg snippets-p)))))
  16616. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16617. (setq rpl (if (or (match-end 1) (not extra))
  16618. "" (concat extra "\n"))
  16619. tpl (replace-match rpl t t tpl))
  16620. (if (and extra (string-match "\\S-" extra))
  16621. (setq end (concat end "\n" extra))))
  16622. (if (string-match "\\S-" end)
  16623. (concat tpl "\n" end)
  16624. tpl)))
  16625. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16626. "Turn an alist of packages into a string with the \\usepackage macros."
  16627. (setq pkg (mapconcat (lambda(p)
  16628. (cond
  16629. ((stringp p) p)
  16630. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16631. (format "%% Package %s omitted" (cadr p)))
  16632. ((equal "" (car p))
  16633. (format "\\usepackage{%s}" (cadr p)))
  16634. (t
  16635. (format "\\usepackage[%s]{%s}"
  16636. (car p) (cadr p)))))
  16637. pkg
  16638. "\n"))
  16639. (if newline (concat pkg "\n") pkg))
  16640. (defun org-dvipng-color (attr)
  16641. "Return a RGB color specification for dvipng."
  16642. (apply 'format "rgb %s %s %s"
  16643. (mapcar 'org-normalize-color
  16644. (if (featurep 'xemacs)
  16645. (color-rgb-components
  16646. (face-property 'default
  16647. (cond ((eq attr :foreground) 'foreground)
  16648. ((eq attr :background) 'background))))
  16649. (color-values (face-attribute 'default attr nil))))))
  16650. (defun org-dvipng-color-format (color-name)
  16651. "Convert COLOR-NAME to a RGB color value for dvipng."
  16652. (apply 'format "rgb %s %s %s"
  16653. (mapcar 'org-normalize-color
  16654. (color-values color-name))))
  16655. (defun org-latex-color (attr)
  16656. "Return a RGB color for the LaTeX color package."
  16657. (apply 'format "%s,%s,%s"
  16658. (mapcar 'org-normalize-color
  16659. (if (featurep 'xemacs)
  16660. (color-rgb-components
  16661. (face-property 'default
  16662. (cond ((eq attr :foreground) 'foreground)
  16663. ((eq attr :background) 'background))))
  16664. (color-values (face-attribute 'default attr nil))))))
  16665. (defun org-latex-color-format (color-name)
  16666. "Convert COLOR-NAME to a RGB color value."
  16667. (apply 'format "%s,%s,%s"
  16668. (mapcar 'org-normalize-color
  16669. (color-values color-name))))
  16670. (defun org-normalize-color (value)
  16671. "Return string to be used as color value for an RGB component."
  16672. (format "%g" (/ value 65535.0)))
  16673. ;; Image display
  16674. (defvar org-inline-image-overlays nil)
  16675. (make-variable-buffer-local 'org-inline-image-overlays)
  16676. (defun org-toggle-inline-images (&optional include-linked)
  16677. "Toggle the display of inline images.
  16678. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16679. (interactive "P")
  16680. (if org-inline-image-overlays
  16681. (progn
  16682. (org-remove-inline-images)
  16683. (message "Inline image display turned off"))
  16684. (org-display-inline-images include-linked)
  16685. (if (and (org-called-interactively-p)
  16686. org-inline-image-overlays)
  16687. (message "%d images displayed inline"
  16688. (length org-inline-image-overlays))
  16689. (message "No images to display inline"))))
  16690. (defun org-redisplay-inline-images ()
  16691. "Refresh the display of inline images."
  16692. (interactive)
  16693. (if (not org-inline-image-overlays)
  16694. (org-toggle-inline-images)
  16695. (org-toggle-inline-images)
  16696. (org-toggle-inline-images)))
  16697. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16698. "Display inline images.
  16699. Normally only links without a description part are inlined, because this
  16700. is how it will work for export. When INCLUDE-LINKED is set, also links
  16701. with a description part will be inlined. This can be nice for a quick
  16702. look at those images, but it does not reflect what exported files will look
  16703. like.
  16704. When REFRESH is set, refresh existing images between BEG and END.
  16705. This will create new image displays only if necessary.
  16706. BEG and END default to the buffer boundaries."
  16707. (interactive "P")
  16708. (unless refresh
  16709. (org-remove-inline-images)
  16710. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16711. (save-excursion
  16712. (save-restriction
  16713. (widen)
  16714. (setq beg (or beg (point-min)) end (or end (point-max)))
  16715. (goto-char beg)
  16716. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16717. (substring (org-image-file-name-regexp) 0 -2)
  16718. "\\)\\]" (if include-linked "" "\\]")))
  16719. old file ov img type attrwidth width)
  16720. (while (re-search-forward re end t)
  16721. (setq old (get-char-property-and-overlay (match-beginning 1)
  16722. 'org-image-overlay)
  16723. file (expand-file-name
  16724. (concat (or (match-string 3) "") (match-string 4))))
  16725. (when (image-type-available-p 'imagemagick)
  16726. (setq attrwidth (if (or (listp org-image-actual-width)
  16727. (null org-image-actual-width))
  16728. (save-excursion
  16729. (save-match-data
  16730. (when (re-search-backward
  16731. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  16732. (save-excursion
  16733. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  16734. (string-to-number (match-string 1))))))
  16735. width (cond ((eq org-image-actual-width t) nil)
  16736. ((null org-image-actual-width) attrwidth)
  16737. ((numberp org-image-actual-width)
  16738. org-image-actual-width)
  16739. ((listp org-image-actual-width)
  16740. (or attrwidth (car org-image-actual-width))))
  16741. type (if width 'imagemagick)))
  16742. (when (file-exists-p file)
  16743. (if (and (car-safe old) refresh)
  16744. (image-refresh (overlay-get (cdr old) 'display))
  16745. (setq img (save-match-data (create-image file type nil :width width)))
  16746. (when img
  16747. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16748. (overlay-put ov 'display img)
  16749. (overlay-put ov 'face 'default)
  16750. (overlay-put ov 'org-image-overlay t)
  16751. (overlay-put ov 'modification-hooks
  16752. (list 'org-display-inline-remove-overlay))
  16753. (push ov org-inline-image-overlays)))))))))
  16754. (define-obsolete-function-alias
  16755. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  16756. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  16757. "Remove inline-display overlay if a corresponding region is modified."
  16758. (let ((inhibit-modification-hooks t))
  16759. (when (and ov after)
  16760. (delete ov org-inline-image-overlays)
  16761. (delete-overlay ov))))
  16762. (defun org-remove-inline-images ()
  16763. "Remove inline display of images."
  16764. (interactive)
  16765. (mapc 'delete-overlay org-inline-image-overlays)
  16766. (setq org-inline-image-overlays nil))
  16767. ;;;; Key bindings
  16768. ;; Outline functions from `outline-mode-prefix-map'
  16769. ;; that can be remapped in Org:
  16770. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16771. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16772. (define-key org-mode-map [remap outline-forward-same-level]
  16773. 'org-forward-heading-same-level)
  16774. (define-key org-mode-map [remap outline-backward-same-level]
  16775. 'org-backward-heading-same-level)
  16776. (define-key org-mode-map [remap show-branches]
  16777. 'org-kill-note-or-show-branches)
  16778. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16779. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16780. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16781. ;; Outline functions from `outline-mode-prefix-map' that can not
  16782. ;; be remapped in Org:
  16783. ;;
  16784. ;; - the column "key binding" shows whether the Outline function is still
  16785. ;; available in Org mode on the same key that it has been bound to in
  16786. ;; Outline mode:
  16787. ;; - "overridden": key used for a different functionality in Org mode
  16788. ;; - else: key still bound to the same Outline function in Org mode
  16789. ;;
  16790. ;; | Outline function | key binding | Org replacement |
  16791. ;; |------------------------------------+-------------+-----------------------|
  16792. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16793. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16794. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16795. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  16796. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16797. ;; | `show-entry' | overridden | no replacement |
  16798. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16799. ;; | `show-branches' | `C-c C-k' | still same function |
  16800. ;; | `show-subtree' | overridden | visibility cycling |
  16801. ;; | `show-all' | overridden | no replacement |
  16802. ;; | `hide-subtree' | overridden | visibility cycling |
  16803. ;; | `hide-body' | overridden | no replacement |
  16804. ;; | `hide-entry' | overridden | visibility cycling |
  16805. ;; | `hide-leaves' | overridden | no replacement |
  16806. ;; | `hide-sublevels' | overridden | no replacement |
  16807. ;; | `hide-other' | overridden | no replacement |
  16808. ;; Make `C-c C-x' a prefix key
  16809. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16810. ;; TAB key with modifiers
  16811. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16812. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16813. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16814. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16815. ;; The following line is necessary under Suse GNU/Linux
  16816. (unless (featurep 'xemacs)
  16817. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16818. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16819. (define-key org-mode-map [backtab] 'org-shifttab)
  16820. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16821. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16822. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16823. ;; Cursor keys with modifiers
  16824. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16825. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16826. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16827. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16828. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16829. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16830. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16831. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16832. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16833. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16834. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16835. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16836. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16837. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16838. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16839. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16840. ;; Babel keys
  16841. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16842. (mapc (lambda (pair)
  16843. (define-key org-babel-map (car pair) (cdr pair)))
  16844. org-babel-key-bindings)
  16845. ;;; Extra keys for tty access.
  16846. ;; We only set them when really needed because otherwise the
  16847. ;; menus don't show the simple keys
  16848. (when (or org-use-extra-keys
  16849. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16850. (not window-system))
  16851. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16852. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16853. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16854. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16855. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16856. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16857. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16858. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16859. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16860. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16861. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16862. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16863. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16864. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16865. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16866. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16867. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16868. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16869. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16870. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16871. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16872. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16873. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16874. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16875. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16876. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16877. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16878. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16879. ;; All the other keys
  16880. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16881. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16882. (if (boundp 'narrow-map)
  16883. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16884. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16885. (if (boundp 'narrow-map)
  16886. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16887. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16888. (if (boundp 'narrow-map)
  16889. (org-defkey narrow-map "e" 'org-narrow-to-element)
  16890. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  16891. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  16892. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  16893. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  16894. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  16895. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  16896. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  16897. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  16898. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16899. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16900. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16901. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16902. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16903. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16904. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16905. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16906. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16907. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16908. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16909. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16910. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16911. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16912. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  16913. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16914. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16915. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16916. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16917. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16918. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16919. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16920. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16921. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16922. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16923. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16924. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  16925. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16926. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16927. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16928. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16929. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16930. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16931. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16932. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16933. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16934. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16935. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16936. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16937. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16938. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16939. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16940. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16941. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16942. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16943. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16944. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16945. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16946. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16947. (org-defkey org-mode-map "\C-m" 'org-return)
  16948. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16949. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16950. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16951. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16952. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16953. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16954. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16955. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16956. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16957. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16958. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16959. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16960. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16961. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16962. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16963. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16964. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16965. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16966. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16967. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16968. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  16969. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16970. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16971. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16972. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16973. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16974. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16975. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16976. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  16977. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  16978. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16979. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16980. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  16981. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16982. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16983. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16984. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16985. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16986. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  16987. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16988. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16989. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16990. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  16991. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16992. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  16993. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16994. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16995. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16996. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16997. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16998. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16999. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17000. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17001. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17002. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17003. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17004. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17005. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17006. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17007. (when (featurep 'xemacs)
  17008. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17009. (defconst org-speed-commands-default
  17010. '(
  17011. ("Outline Navigation")
  17012. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17013. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17014. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17015. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17016. ("u" . (org-speed-move-safe 'outline-up-heading))
  17017. ("j" . org-goto)
  17018. ("g" . (org-refile t))
  17019. ("Outline Visibility")
  17020. ("c" . org-cycle)
  17021. ("C" . org-shifttab)
  17022. (" " . org-display-outline-path)
  17023. ("=" . org-columns)
  17024. ("Outline Structure Editing")
  17025. ("U" . org-shiftmetaup)
  17026. ("D" . org-shiftmetadown)
  17027. ("r" . org-metaright)
  17028. ("l" . org-metaleft)
  17029. ("R" . org-shiftmetaright)
  17030. ("L" . org-shiftmetaleft)
  17031. ("i" . (progn (forward-char 1) (call-interactively
  17032. 'org-insert-heading-respect-content)))
  17033. ("^" . org-sort)
  17034. ("w" . org-refile)
  17035. ("a" . org-archive-subtree-default-with-confirmation)
  17036. ("." . org-mark-subtree)
  17037. ("#" . org-toggle-comment)
  17038. ("Clock Commands")
  17039. ("I" . org-clock-in)
  17040. ("O" . org-clock-out)
  17041. ("Meta Data Editing")
  17042. ("t" . org-todo)
  17043. ("," . (org-priority))
  17044. ("0" . (org-priority ?\ ))
  17045. ("1" . (org-priority ?A))
  17046. ("2" . (org-priority ?B))
  17047. ("3" . (org-priority ?C))
  17048. (":" . org-set-tags-command)
  17049. ("e" . org-set-effort)
  17050. ("E" . org-inc-effort)
  17051. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17052. (org-entry-put (point) "APPT_WARNTIME" m)))
  17053. ("Agenda Views etc")
  17054. ("v" . org-agenda)
  17055. ("/" . org-sparse-tree)
  17056. ("Misc")
  17057. ("o" . org-open-at-point)
  17058. ("?" . org-speed-command-help)
  17059. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17060. (">" . (org-agenda-remove-restriction-lock))
  17061. )
  17062. "The default speed commands.")
  17063. (defun org-print-speed-command (e)
  17064. (if (> (length (car e)) 1)
  17065. (progn
  17066. (princ "\n")
  17067. (princ (car e))
  17068. (princ "\n")
  17069. (princ (make-string (length (car e)) ?-))
  17070. (princ "\n"))
  17071. (princ (car e))
  17072. (princ " ")
  17073. (if (symbolp (cdr e))
  17074. (princ (symbol-name (cdr e)))
  17075. (prin1 (cdr e)))
  17076. (princ "\n")))
  17077. (defun org-speed-command-help ()
  17078. "Show the available speed commands."
  17079. (interactive)
  17080. (if (not org-use-speed-commands)
  17081. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17082. (with-output-to-temp-buffer "*Help*"
  17083. (princ "User-defined Speed commands\n===========================\n")
  17084. (mapc 'org-print-speed-command org-speed-commands-user)
  17085. (princ "\n")
  17086. (princ "Built-in Speed commands\n=======================\n")
  17087. (mapc 'org-print-speed-command org-speed-commands-default))
  17088. (with-current-buffer "*Help*"
  17089. (setq truncate-lines t))))
  17090. (defun org-speed-move-safe (cmd)
  17091. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17092. If not, return to the original position and throw an error."
  17093. (interactive)
  17094. (let ((pos (point)))
  17095. (call-interactively cmd)
  17096. (unless (and (bolp) (org-at-heading-p))
  17097. (goto-char pos)
  17098. (error "Boundary reached while executing %s" cmd))))
  17099. (defvar org-self-insert-command-undo-counter 0)
  17100. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17101. (defvar org-speed-command nil)
  17102. (define-obsolete-function-alias
  17103. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17104. (defun org-speed-command-activate (keys)
  17105. "Hook for activating single-letter speed commands.
  17106. `org-speed-commands-default' specifies a minimal command set.
  17107. Use `org-speed-commands-user' for further customization."
  17108. (when (or (and (bolp) (looking-at org-outline-regexp))
  17109. (and (functionp org-use-speed-commands)
  17110. (funcall org-use-speed-commands)))
  17111. (cdr (assoc keys (append org-speed-commands-user
  17112. org-speed-commands-default)))))
  17113. (define-obsolete-function-alias
  17114. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17115. (defun org-babel-speed-command-activate (keys)
  17116. "Hook for activating single-letter code block commands."
  17117. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17118. (cdr (assoc keys org-babel-key-bindings))))
  17119. (defcustom org-speed-command-hook
  17120. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17121. "Hook for activating speed commands at strategic locations.
  17122. Hook functions are called in sequence until a valid handler is
  17123. found.
  17124. Each hook takes a single argument, a user-pressed command key
  17125. which is also a `self-insert-command' from the global map.
  17126. Within the hook, examine the cursor position and the command key
  17127. and return nil or a valid handler as appropriate. Handler could
  17128. be one of an interactive command, a function, or a form.
  17129. Set `org-use-speed-commands' to non-nil value to enable this
  17130. hook. The default setting is `org-speed-command-activate'."
  17131. :group 'org-structure
  17132. :version "24.1"
  17133. :type 'hook)
  17134. (defun org-self-insert-command (N)
  17135. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17136. If the cursor is in a table looking at whitespace, the whitespace is
  17137. overwritten, and the table is not marked as requiring realignment."
  17138. (interactive "p")
  17139. (org-check-before-invisible-edit 'insert)
  17140. (cond
  17141. ((and org-use-speed-commands
  17142. (setq org-speed-command
  17143. (run-hook-with-args-until-success
  17144. 'org-speed-command-hook (this-command-keys))))
  17145. (cond
  17146. ((commandp org-speed-command)
  17147. (setq this-command org-speed-command)
  17148. (call-interactively org-speed-command))
  17149. ((functionp org-speed-command)
  17150. (funcall org-speed-command))
  17151. ((and org-speed-command (listp org-speed-command))
  17152. (eval org-speed-command))
  17153. (t (let (org-use-speed-commands)
  17154. (call-interactively 'org-self-insert-command)))))
  17155. ((and
  17156. (org-table-p)
  17157. (progn
  17158. ;; check if we blank the field, and if that triggers align
  17159. (and (featurep 'org-table) org-table-auto-blank-field
  17160. (member last-command
  17161. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17162. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17163. ;; got extra space, this field does not determine column width
  17164. (let (org-table-may-need-update) (org-table-blank-field))
  17165. ;; no extra space, this field may determine column width
  17166. (org-table-blank-field)))
  17167. t)
  17168. (eq N 1)
  17169. (looking-at "[^|\n]* |"))
  17170. (let (org-table-may-need-update)
  17171. (goto-char (1- (match-end 0)))
  17172. (backward-delete-char 1)
  17173. (goto-char (match-beginning 0))
  17174. (self-insert-command N)))
  17175. (t
  17176. (setq org-table-may-need-update t)
  17177. (self-insert-command N)
  17178. (org-fix-tags-on-the-fly)
  17179. (if org-self-insert-cluster-for-undo
  17180. (if (not (eq last-command 'org-self-insert-command))
  17181. (setq org-self-insert-command-undo-counter 1)
  17182. (if (>= org-self-insert-command-undo-counter 20)
  17183. (setq org-self-insert-command-undo-counter 1)
  17184. (and (> org-self-insert-command-undo-counter 0)
  17185. buffer-undo-list (listp buffer-undo-list)
  17186. (not (cadr buffer-undo-list)) ; remove nil entry
  17187. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17188. (setq org-self-insert-command-undo-counter
  17189. (1+ org-self-insert-command-undo-counter))))))))
  17190. (defun org-check-before-invisible-edit (kind)
  17191. "Check is editing if kind KIND would be dangerous with invisible text around.
  17192. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17193. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17194. (if (and org-catch-invisible-edits
  17195. (or (not (boundp 'visible-mode)) (not visible-mode))
  17196. (or (get-char-property (point) 'invisible)
  17197. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17198. ;; OK, we need to take a closer look
  17199. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17200. (invisible-before-point (if (bobp) nil (get-char-property
  17201. (1- (point)) 'invisible)))
  17202. (border-and-ok-direction
  17203. (or
  17204. ;; Check if we are acting predictably before invisible text
  17205. (and invisible-at-point (not invisible-before-point)
  17206. (memq kind '(insert delete-backward)))
  17207. ;; Check if we are acting predictably after invisible text
  17208. ;; This works not well, and I have turned it off. It seems
  17209. ;; better to always show and stop after invisible text.
  17210. ;; (and (not invisible-at-point) invisible-before-point
  17211. ;; (memq kind '(insert delete)))
  17212. )))
  17213. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17214. (memq invisible-before-point '(outline org-hide-block t)))
  17215. (if (eq org-catch-invisible-edits 'error)
  17216. (error "Editing in invisible areas is prohibited - make visible first"))
  17217. (if (and org-custom-properties-overlays
  17218. (y-or-n-p "Display invisible properties in this buffer? "))
  17219. (org-toggle-custom-properties-visibility)
  17220. ;; Make the area visible
  17221. (save-excursion
  17222. (if invisible-before-point
  17223. (goto-char (previous-single-char-property-change
  17224. (point) 'invisible)))
  17225. (org-cycle))
  17226. (cond
  17227. ((eq org-catch-invisible-edits 'show)
  17228. ;; That's it, we do the edit after showing
  17229. (message
  17230. "Unfolding invisible region around point before editing")
  17231. (sit-for 1))
  17232. ((and (eq org-catch-invisible-edits 'smart)
  17233. border-and-ok-direction)
  17234. (message "Unfolding invisible region around point before editing"))
  17235. (t
  17236. ;; Don't do the edit, make the user repeat it in full visibility
  17237. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17238. (defun org-fix-tags-on-the-fly ()
  17239. (when (and (equal (char-after (point-at-bol)) ?*)
  17240. (org-at-heading-p))
  17241. (org-align-tags-here org-tags-column)))
  17242. (defun org-delete-backward-char (N)
  17243. "Like `delete-backward-char', insert whitespace at field end in tables.
  17244. When deleting backwards, in tables this function will insert whitespace in
  17245. front of the next \"|\" separator, to keep the table aligned. The table will
  17246. still be marked for re-alignment if the field did fill the entire column,
  17247. because, in this case the deletion might narrow the column."
  17248. (interactive "p")
  17249. (save-match-data
  17250. (org-check-before-invisible-edit 'delete-backward)
  17251. (if (and (org-table-p)
  17252. (eq N 1)
  17253. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17254. (looking-at ".*?|"))
  17255. (let ((pos (point))
  17256. (noalign (looking-at "[^|\n\r]* |"))
  17257. (c org-table-may-need-update))
  17258. (backward-delete-char N)
  17259. (if (not overwrite-mode)
  17260. (progn
  17261. (skip-chars-forward "^|")
  17262. (insert " ")
  17263. (goto-char (1- pos))))
  17264. ;; noalign: if there were two spaces at the end, this field
  17265. ;; does not determine the width of the column.
  17266. (if noalign (setq org-table-may-need-update c)))
  17267. (backward-delete-char N)
  17268. (org-fix-tags-on-the-fly))))
  17269. (defun org-delete-char (N)
  17270. "Like `delete-char', but insert whitespace at field end in tables.
  17271. When deleting characters, in tables this function will insert whitespace in
  17272. front of the next \"|\" separator, to keep the table aligned. The table will
  17273. still be marked for re-alignment if the field did fill the entire column,
  17274. because, in this case the deletion might narrow the column."
  17275. (interactive "p")
  17276. (save-match-data
  17277. (org-check-before-invisible-edit 'delete)
  17278. (if (and (org-table-p)
  17279. (not (bolp))
  17280. (not (= (char-after) ?|))
  17281. (eq N 1))
  17282. (if (looking-at ".*?|")
  17283. (let ((pos (point))
  17284. (noalign (looking-at "[^|\n\r]* |"))
  17285. (c org-table-may-need-update))
  17286. (replace-match (concat
  17287. (substring (match-string 0) 1 -1)
  17288. " |"))
  17289. (goto-char pos)
  17290. ;; noalign: if there were two spaces at the end, this field
  17291. ;; does not determine the width of the column.
  17292. (if noalign (setq org-table-may-need-update c)))
  17293. (delete-char N))
  17294. (delete-char N)
  17295. (org-fix-tags-on-the-fly))))
  17296. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17297. (put 'org-self-insert-command 'delete-selection t)
  17298. (put 'orgtbl-self-insert-command 'delete-selection t)
  17299. (put 'org-delete-char 'delete-selection 'supersede)
  17300. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17301. (put 'org-yank 'delete-selection 'yank)
  17302. ;; Make `flyspell-mode' delay after some commands
  17303. (put 'org-self-insert-command 'flyspell-delayed t)
  17304. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17305. (put 'org-delete-char 'flyspell-delayed t)
  17306. (put 'org-delete-backward-char 'flyspell-delayed t)
  17307. ;; Make pabbrev-mode expand after org-mode commands
  17308. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17309. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17310. ;; How to do this: Measure non-white length of current string
  17311. ;; If equal to column width, we should realign.
  17312. (defun org-remap (map &rest commands)
  17313. "In MAP, remap the functions given in COMMANDS.
  17314. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17315. (let (new old)
  17316. (while commands
  17317. (setq old (pop commands) new (pop commands))
  17318. (if (fboundp 'command-remapping)
  17319. (org-defkey map (vector 'remap old) new)
  17320. (substitute-key-definition old new map global-map)))))
  17321. (when (eq org-enable-table-editor 'optimized)
  17322. ;; If the user wants maximum table support, we need to hijack
  17323. ;; some standard editing functions
  17324. (org-remap org-mode-map
  17325. 'self-insert-command 'org-self-insert-command
  17326. 'delete-char 'org-delete-char
  17327. 'delete-backward-char 'org-delete-backward-char)
  17328. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17329. (defvar org-ctrl-c-ctrl-c-hook nil
  17330. "Hook for functions attaching themselves to `C-c C-c'.
  17331. This can be used to add additional functionality to the C-c C-c
  17332. key which executes context-dependent commands. This hook is run
  17333. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17334. run after the last test.
  17335. Each function will be called with no arguments. The function
  17336. must check if the context is appropriate for it to act. If yes,
  17337. it should do its thing and then return a non-nil value. If the
  17338. context is wrong, just do nothing and return nil.")
  17339. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17340. "Hook for functions attaching themselves to `C-c C-c'.
  17341. This can be used to add additional functionality to the C-c C-c
  17342. key which executes context-dependent commands. This hook is run
  17343. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17344. before the first test.
  17345. Each function will be called with no arguments. The function
  17346. must check if the context is appropriate for it to act. If yes,
  17347. it should do its thing and then return a non-nil value. If the
  17348. context is wrong, just do nothing and return nil.")
  17349. (defvar org-tab-first-hook nil
  17350. "Hook for functions to attach themselves to TAB.
  17351. See `org-ctrl-c-ctrl-c-hook' for more information.
  17352. This hook runs as the first action when TAB is pressed, even before
  17353. `org-cycle' messes around with the `outline-regexp' to cater for
  17354. inline tasks and plain list item folding.
  17355. If any function in this hook returns t, any other actions that
  17356. would have been caused by TAB (such as table field motion or visibility
  17357. cycling) will not occur.")
  17358. (defvar org-tab-after-check-for-table-hook nil
  17359. "Hook for functions to attach themselves to TAB.
  17360. See `org-ctrl-c-ctrl-c-hook' for more information.
  17361. This hook runs after it has been established that the cursor is not in a
  17362. table, but before checking if the cursor is in a headline or if global cycling
  17363. should be done.
  17364. If any function in this hook returns t, not other actions like visibility
  17365. cycling will be done.")
  17366. (defvar org-tab-after-check-for-cycling-hook nil
  17367. "Hook for functions to attach themselves to TAB.
  17368. See `org-ctrl-c-ctrl-c-hook' for more information.
  17369. This hook runs after it has been established that not table field motion and
  17370. not visibility should be done because of current context. This is probably
  17371. the place where a package like yasnippets can hook in.")
  17372. (defvar org-tab-before-tab-emulation-hook nil
  17373. "Hook for functions to attach themselves to TAB.
  17374. See `org-ctrl-c-ctrl-c-hook' for more information.
  17375. This hook runs after every other options for TAB have been exhausted, but
  17376. before indentation and \t insertion takes place.")
  17377. (defvar org-metaleft-hook nil
  17378. "Hook for functions attaching themselves to `M-left'.
  17379. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17380. (defvar org-metaright-hook nil
  17381. "Hook for functions attaching themselves to `M-right'.
  17382. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17383. (defvar org-metaup-hook nil
  17384. "Hook for functions attaching themselves to `M-up'.
  17385. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17386. (defvar org-metadown-hook nil
  17387. "Hook for functions attaching themselves to `M-down'.
  17388. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17389. (defvar org-shiftmetaleft-hook nil
  17390. "Hook for functions attaching themselves to `M-S-left'.
  17391. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17392. (defvar org-shiftmetaright-hook nil
  17393. "Hook for functions attaching themselves to `M-S-right'.
  17394. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17395. (defvar org-shiftmetaup-hook nil
  17396. "Hook for functions attaching themselves to `M-S-up'.
  17397. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17398. (defvar org-shiftmetadown-hook nil
  17399. "Hook for functions attaching themselves to `M-S-down'.
  17400. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17401. (defvar org-metareturn-hook nil
  17402. "Hook for functions attaching themselves to `M-RET'.
  17403. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17404. (defvar org-shiftup-hook nil
  17405. "Hook for functions attaching themselves to `S-up'.
  17406. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17407. (defvar org-shiftup-final-hook nil
  17408. "Hook for functions attaching themselves to `S-up'.
  17409. This one runs after all other options except shift-select have been excluded.
  17410. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17411. (defvar org-shiftdown-hook nil
  17412. "Hook for functions attaching themselves to `S-down'.
  17413. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17414. (defvar org-shiftdown-final-hook nil
  17415. "Hook for functions attaching themselves to `S-down'.
  17416. This one runs after all other options except shift-select have been excluded.
  17417. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17418. (defvar org-shiftleft-hook nil
  17419. "Hook for functions attaching themselves to `S-left'.
  17420. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17421. (defvar org-shiftleft-final-hook nil
  17422. "Hook for functions attaching themselves to `S-left'.
  17423. This one runs after all other options except shift-select have been excluded.
  17424. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17425. (defvar org-shiftright-hook nil
  17426. "Hook for functions attaching themselves to `S-right'.
  17427. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17428. (defvar org-shiftright-final-hook nil
  17429. "Hook for functions attaching themselves to `S-right'.
  17430. This one runs after all other options except shift-select have been excluded.
  17431. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17432. (defun org-modifier-cursor-error ()
  17433. "Throw an error, a modified cursor command was applied in wrong context."
  17434. (error "This command is active in special context like tables, headlines or items"))
  17435. (defun org-shiftselect-error ()
  17436. "Throw an error because Shift-Cursor command was applied in wrong context."
  17437. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17438. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17439. (error "This command works only in special context like headlines or timestamps")))
  17440. (defun org-call-for-shift-select (cmd)
  17441. (let ((this-command-keys-shift-translated t))
  17442. (call-interactively cmd)))
  17443. (defun org-shifttab (&optional arg)
  17444. "Global visibility cycling or move to previous table field.
  17445. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17446. on context.
  17447. See the individual commands for more information."
  17448. (interactive "P")
  17449. (cond
  17450. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17451. ((integerp arg)
  17452. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17453. (message "Content view to level: %d" arg)
  17454. (org-content (prefix-numeric-value arg2))
  17455. (setq org-cycle-global-status 'overview)))
  17456. (t (call-interactively 'org-global-cycle))))
  17457. (defun org-shiftmetaleft ()
  17458. "Promote subtree or delete table column.
  17459. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17460. `org-table-delete-column', depending on context. See the
  17461. individual commands for more information."
  17462. (interactive)
  17463. (cond
  17464. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17465. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17466. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17467. ((if (not (org-region-active-p)) (org-at-item-p)
  17468. (save-excursion (goto-char (region-beginning))
  17469. (org-at-item-p)))
  17470. (call-interactively 'org-outdent-item-tree))
  17471. (t (org-modifier-cursor-error))))
  17472. (defun org-shiftmetaright ()
  17473. "Demote subtree or insert table column.
  17474. Calls `org-demote-subtree', `org-indent-item-tree', or
  17475. `org-table-insert-column', depending on context. See the
  17476. individual commands for more information."
  17477. (interactive)
  17478. (cond
  17479. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17480. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17481. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17482. ((if (not (org-region-active-p)) (org-at-item-p)
  17483. (save-excursion (goto-char (region-beginning))
  17484. (org-at-item-p)))
  17485. (call-interactively 'org-indent-item-tree))
  17486. (t (org-modifier-cursor-error))))
  17487. (defun org-shiftmetaup (&optional arg)
  17488. "Move subtree up or kill table row.
  17489. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17490. `org-move-item-up' or `org-timestamp-up', depending on context.
  17491. See the individual commands for more information."
  17492. (interactive "P")
  17493. (cond
  17494. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17495. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17496. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17497. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17498. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17499. (call-interactively 'org-timestamp-up)))
  17500. (t (org-modifier-cursor-error))))
  17501. (defun org-shiftmetadown (&optional arg)
  17502. "Move subtree down or insert table row.
  17503. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17504. `org-move-item-down' or `org-timestamp-up', depending on context.
  17505. See the individual commands for more information."
  17506. (interactive "P")
  17507. (cond
  17508. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17509. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17510. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17511. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17512. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17513. (call-interactively 'org-timestamp-down)))
  17514. (t (org-modifier-cursor-error))))
  17515. (defsubst org-hidden-tree-error ()
  17516. (error
  17517. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17518. (defun org-metaleft (&optional arg)
  17519. "Promote heading or move table column to left.
  17520. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17521. With no specific context, calls the Emacs default `backward-word'.
  17522. See the individual commands for more information."
  17523. (interactive "P")
  17524. (cond
  17525. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17526. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17527. ((org-with-limited-levels
  17528. (or (org-at-heading-p)
  17529. (and (org-region-active-p)
  17530. (save-excursion
  17531. (goto-char (region-beginning))
  17532. (org-at-heading-p)))))
  17533. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17534. (call-interactively 'org-do-promote))
  17535. ;; At an inline task.
  17536. ((org-at-heading-p)
  17537. (call-interactively 'org-inlinetask-promote))
  17538. ((or (org-at-item-p)
  17539. (and (org-region-active-p)
  17540. (save-excursion
  17541. (goto-char (region-beginning))
  17542. (org-at-item-p))))
  17543. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17544. (call-interactively 'org-outdent-item))
  17545. (t (call-interactively 'backward-word))))
  17546. (defun org-metaright (&optional arg)
  17547. "Demote a subtree, a list item or move table column to right.
  17548. In front of a drawer or a block keyword, indent it correctly.
  17549. With no specific context, calls the Emacs default `forward-word'.
  17550. See the individual commands for more information."
  17551. (interactive "P")
  17552. (cond
  17553. ((run-hook-with-args-until-success 'org-metaright-hook))
  17554. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17555. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17556. ((org-at-block-p) (call-interactively 'org-indent-block))
  17557. ((org-with-limited-levels
  17558. (or (org-at-heading-p)
  17559. (and (org-region-active-p)
  17560. (save-excursion
  17561. (goto-char (region-beginning))
  17562. (org-at-heading-p)))))
  17563. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17564. (call-interactively 'org-do-demote))
  17565. ;; At an inline task.
  17566. ((org-at-heading-p)
  17567. (call-interactively 'org-inlinetask-demote))
  17568. ((or (org-at-item-p)
  17569. (and (org-region-active-p)
  17570. (save-excursion
  17571. (goto-char (region-beginning))
  17572. (org-at-item-p))))
  17573. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17574. (call-interactively 'org-indent-item))
  17575. (t (call-interactively 'forward-word))))
  17576. (defun org-check-for-hidden (what)
  17577. "Check if there are hidden headlines/items in the current visual line.
  17578. WHAT can be either `headlines' or `items'. If the current line is
  17579. an outline or item heading and it has a folded subtree below it,
  17580. this function returns t, nil otherwise."
  17581. (let ((re (cond
  17582. ((eq what 'headlines) org-outline-regexp-bol)
  17583. ((eq what 'items) (org-item-beginning-re))
  17584. (t (error "This should not happen"))))
  17585. beg end)
  17586. (save-excursion
  17587. (catch 'exit
  17588. (unless (org-region-active-p)
  17589. (setq beg (point-at-bol))
  17590. (beginning-of-line 2)
  17591. (while (and (not (eobp)) ;; this is like `next-line'
  17592. (get-char-property (1- (point)) 'invisible))
  17593. (beginning-of-line 2))
  17594. (setq end (point))
  17595. (goto-char beg)
  17596. (goto-char (point-at-eol))
  17597. (setq end (max end (point)))
  17598. (while (re-search-forward re end t)
  17599. (if (get-char-property (match-beginning 0) 'invisible)
  17600. (throw 'exit t))))
  17601. nil))))
  17602. (defun org-metaup (&optional arg)
  17603. "Move subtree up or move table row up.
  17604. Calls `org-move-subtree-up' or `org-table-move-row' or
  17605. `org-move-item-up', depending on context. See the individual commands
  17606. for more information."
  17607. (interactive "P")
  17608. (cond
  17609. ((run-hook-with-args-until-success 'org-metaup-hook))
  17610. ((org-region-active-p)
  17611. (let* ((a (min (region-beginning) (region-end)))
  17612. (b (1- (max (region-beginning) (region-end))))
  17613. (c (save-excursion (goto-char a)
  17614. (move-beginning-of-line 0)))
  17615. (d (save-excursion (goto-char a)
  17616. (move-end-of-line 0) (point))))
  17617. (transpose-regions a b c d)
  17618. (goto-char c)))
  17619. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17620. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17621. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17622. (t (org-drag-element-backward))))
  17623. (defun org-metadown (&optional arg)
  17624. "Move subtree down or move table row down.
  17625. Calls `org-move-subtree-down' or `org-table-move-row' or
  17626. `org-move-item-down', depending on context. See the individual
  17627. commands for more information."
  17628. (interactive "P")
  17629. (cond
  17630. ((run-hook-with-args-until-success 'org-metadown-hook))
  17631. ((org-region-active-p)
  17632. (let* ((a (min (region-beginning) (region-end)))
  17633. (b (max (region-beginning) (region-end)))
  17634. (c (save-excursion (goto-char b)
  17635. (move-beginning-of-line 1)))
  17636. (d (save-excursion (goto-char b)
  17637. (move-end-of-line 1) (1+ (point)))))
  17638. (transpose-regions a b c d)
  17639. (goto-char d)))
  17640. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17641. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17642. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17643. (t (org-drag-element-forward))))
  17644. (defun org-shiftup (&optional arg)
  17645. "Increase item in timestamp or increase priority of current headline.
  17646. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17647. depending on context. See the individual commands for more information."
  17648. (interactive "P")
  17649. (cond
  17650. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17651. ((and org-support-shift-select (org-region-active-p))
  17652. (org-call-for-shift-select 'previous-line))
  17653. ((org-at-timestamp-p t)
  17654. (call-interactively (if org-edit-timestamp-down-means-later
  17655. 'org-timestamp-down 'org-timestamp-up)))
  17656. ((and (not (eq org-support-shift-select 'always))
  17657. org-enable-priority-commands
  17658. (org-at-heading-p))
  17659. (call-interactively 'org-priority-up))
  17660. ((and (not org-support-shift-select) (org-at-item-p))
  17661. (call-interactively 'org-previous-item))
  17662. ((org-clocktable-try-shift 'up arg))
  17663. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17664. (org-support-shift-select
  17665. (org-call-for-shift-select 'previous-line))
  17666. (t (org-shiftselect-error))))
  17667. (defun org-shiftdown (&optional arg)
  17668. "Decrease item in timestamp or decrease priority of current headline.
  17669. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17670. depending on context. See the individual commands for more information."
  17671. (interactive "P")
  17672. (cond
  17673. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17674. ((and org-support-shift-select (org-region-active-p))
  17675. (org-call-for-shift-select 'next-line))
  17676. ((org-at-timestamp-p t)
  17677. (call-interactively (if org-edit-timestamp-down-means-later
  17678. 'org-timestamp-up 'org-timestamp-down)))
  17679. ((and (not (eq org-support-shift-select 'always))
  17680. org-enable-priority-commands
  17681. (org-at-heading-p))
  17682. (call-interactively 'org-priority-down))
  17683. ((and (not org-support-shift-select) (org-at-item-p))
  17684. (call-interactively 'org-next-item))
  17685. ((org-clocktable-try-shift 'down arg))
  17686. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17687. (org-support-shift-select
  17688. (org-call-for-shift-select 'next-line))
  17689. (t (org-shiftselect-error))))
  17690. (defun org-shiftright (&optional arg)
  17691. "Cycle the thing at point or in the current line, depending on context.
  17692. Depending on context, this does one of the following:
  17693. - switch a timestamp at point one day into the future
  17694. - on a headline, switch to the next TODO keyword.
  17695. - on an item, switch entire list to the next bullet type
  17696. - on a property line, switch to the next allowed value
  17697. - on a clocktable definition line, move time block into the future"
  17698. (interactive "P")
  17699. (cond
  17700. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17701. ((and org-support-shift-select (org-region-active-p))
  17702. (org-call-for-shift-select 'forward-char))
  17703. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17704. ((and (not (eq org-support-shift-select 'always))
  17705. (org-at-heading-p))
  17706. (let ((org-inhibit-logging
  17707. (not org-treat-S-cursor-todo-selection-as-state-change))
  17708. (org-inhibit-blocking
  17709. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17710. (org-call-with-arg 'org-todo 'right)))
  17711. ((or (and org-support-shift-select
  17712. (not (eq org-support-shift-select 'always))
  17713. (org-at-item-bullet-p))
  17714. (and (not org-support-shift-select) (org-at-item-p)))
  17715. (org-call-with-arg 'org-cycle-list-bullet nil))
  17716. ((and (not (eq org-support-shift-select 'always))
  17717. (org-at-property-p))
  17718. (call-interactively 'org-property-next-allowed-value))
  17719. ((org-clocktable-try-shift 'right arg))
  17720. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17721. (org-support-shift-select
  17722. (org-call-for-shift-select 'forward-char))
  17723. (t (org-shiftselect-error))))
  17724. (defun org-shiftleft (&optional arg)
  17725. "Cycle the thing at point or in the current line, depending on context.
  17726. Depending on context, this does one of the following:
  17727. - switch a timestamp at point one day into the past
  17728. - on a headline, switch to the previous TODO keyword.
  17729. - on an item, switch entire list to the previous bullet type
  17730. - on a property line, switch to the previous allowed value
  17731. - on a clocktable definition line, move time block into the past"
  17732. (interactive "P")
  17733. (cond
  17734. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17735. ((and org-support-shift-select (org-region-active-p))
  17736. (org-call-for-shift-select 'backward-char))
  17737. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17738. ((and (not (eq org-support-shift-select 'always))
  17739. (org-at-heading-p))
  17740. (let ((org-inhibit-logging
  17741. (not org-treat-S-cursor-todo-selection-as-state-change))
  17742. (org-inhibit-blocking
  17743. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17744. (org-call-with-arg 'org-todo 'left)))
  17745. ((or (and org-support-shift-select
  17746. (not (eq org-support-shift-select 'always))
  17747. (org-at-item-bullet-p))
  17748. (and (not org-support-shift-select) (org-at-item-p)))
  17749. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17750. ((and (not (eq org-support-shift-select 'always))
  17751. (org-at-property-p))
  17752. (call-interactively 'org-property-previous-allowed-value))
  17753. ((org-clocktable-try-shift 'left arg))
  17754. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17755. (org-support-shift-select
  17756. (org-call-for-shift-select 'backward-char))
  17757. (t (org-shiftselect-error))))
  17758. (defun org-shiftcontrolright ()
  17759. "Switch to next TODO set."
  17760. (interactive)
  17761. (cond
  17762. ((and org-support-shift-select (org-region-active-p))
  17763. (org-call-for-shift-select 'forward-word))
  17764. ((and (not (eq org-support-shift-select 'always))
  17765. (org-at-heading-p))
  17766. (org-call-with-arg 'org-todo 'nextset))
  17767. (org-support-shift-select
  17768. (org-call-for-shift-select 'forward-word))
  17769. (t (org-shiftselect-error))))
  17770. (defun org-shiftcontrolleft ()
  17771. "Switch to previous TODO set."
  17772. (interactive)
  17773. (cond
  17774. ((and org-support-shift-select (org-region-active-p))
  17775. (org-call-for-shift-select 'backward-word))
  17776. ((and (not (eq org-support-shift-select 'always))
  17777. (org-at-heading-p))
  17778. (org-call-with-arg 'org-todo 'previousset))
  17779. (org-support-shift-select
  17780. (org-call-for-shift-select 'backward-word))
  17781. (t (org-shiftselect-error))))
  17782. (defun org-shiftcontrolup (&optional n)
  17783. "Change timestamps synchronously up in CLOCK log lines.
  17784. Optional argument N tells to change by that many units."
  17785. (interactive "P")
  17786. (cond ((and (not org-support-shift-select)
  17787. (org-at-clock-log-p)
  17788. (org-at-timestamp-p t))
  17789. (org-clock-timestamps-up n))
  17790. (t (org-shiftselect-error))))
  17791. (defun org-shiftcontroldown (&optional n)
  17792. "Change timestamps synchronously down in CLOCK log lines.
  17793. Optional argument N tells to change by that many units."
  17794. (interactive "P")
  17795. (cond ((and (not org-support-shift-select)
  17796. (org-at-clock-log-p)
  17797. (org-at-timestamp-p t))
  17798. (org-clock-timestamps-down n))
  17799. (t (org-shiftselect-error))))
  17800. (defun org-ctrl-c-ret ()
  17801. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17802. (interactive)
  17803. (cond
  17804. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17805. (t (call-interactively 'org-insert-heading))))
  17806. (defun org-find-visible ()
  17807. (let ((s (point)))
  17808. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17809. (get-char-property s 'invisible)))
  17810. s))
  17811. (defun org-find-invisible ()
  17812. (let ((s (point)))
  17813. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17814. (not (get-char-property s 'invisible))))
  17815. s))
  17816. (defun org-copy-visible (beg end)
  17817. "Copy the visible parts of the region."
  17818. (interactive "r")
  17819. (let (snippets s)
  17820. (save-excursion
  17821. (save-restriction
  17822. (narrow-to-region beg end)
  17823. (setq s (goto-char (point-min)))
  17824. (while (not (= (point) (point-max)))
  17825. (goto-char (org-find-invisible))
  17826. (push (buffer-substring s (point)) snippets)
  17827. (setq s (goto-char (org-find-visible))))))
  17828. (kill-new (apply 'concat (nreverse snippets)))))
  17829. (defun org-copy-special ()
  17830. "Copy region in table or copy current subtree.
  17831. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17832. See the individual commands for more information."
  17833. (interactive)
  17834. (call-interactively
  17835. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17836. (defun org-cut-special ()
  17837. "Cut region in table or cut current subtree.
  17838. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17839. See the individual commands for more information."
  17840. (interactive)
  17841. (call-interactively
  17842. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17843. (defun org-paste-special (arg)
  17844. "Paste rectangular region into table, or past subtree relative to level.
  17845. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17846. See the individual commands for more information."
  17847. (interactive "P")
  17848. (if (org-at-table-p)
  17849. (org-table-paste-rectangle)
  17850. (org-paste-subtree arg)))
  17851. (defsubst org-in-fixed-width-region-p ()
  17852. "Is point in a fixed-width region?"
  17853. (save-match-data
  17854. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  17855. (defun org-edit-special (&optional arg)
  17856. "Call a special editor for the element at point.
  17857. When at a table, call the formula editor with `org-table-edit-formulas'.
  17858. When in a source code block, call `org-edit-src-code'.
  17859. When in a fixed-width region, call `org-edit-fixed-width-region'.
  17860. When at an #+INCLUDE keyword, visit the included file.
  17861. On a link, call `ffap' to visit the link at point.
  17862. Otherwise, return a user error."
  17863. (interactive)
  17864. (let ((element (org-element-at-point)))
  17865. (case (org-element-type element)
  17866. (src-block
  17867. (if (not arg) (org-edit-src-code)
  17868. (let* ((info (org-babel-get-src-block-info))
  17869. (lang (nth 0 info))
  17870. (params (nth 2 info))
  17871. (session (cdr (assq :session params))))
  17872. (if (not session) (org-edit-src-code)
  17873. ;; At a src-block with a session and function called with
  17874. ;; an ARG: switch to the buffer related to the inferior
  17875. ;; process.
  17876. (funcall (intern (concat "org-babel-prep-session:" lang))
  17877. session params)))))
  17878. (keyword
  17879. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  17880. (find-file
  17881. (org-remove-double-quotes
  17882. (car (org-split-string (org-element-property :value element)))))
  17883. (user-error "No special environment to edit here")))
  17884. (table
  17885. (if (eq (org-element-property :type element) 'table.el)
  17886. (org-edit-src-code)
  17887. (call-interactively 'org-table-edit-formulas)))
  17888. ;; Only Org tables contain `table-row' type elements.
  17889. (table-row (call-interactively 'org-table-edit-formulas))
  17890. ((example-block export-block) (org-edit-src-code))
  17891. (fixed-width (org-edit-fixed-width-region))
  17892. (otherwise
  17893. ;; No notable element at point. Though, we may be at a link,
  17894. ;; which is an object. Thus, scan deeper.
  17895. (if (eq (org-element-type (org-element-context element)) 'link)
  17896. (call-interactively 'ffap)
  17897. (user-error "No special environment to edit here"))))))
  17898. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17899. (defun org-ctrl-c-ctrl-c (&optional arg)
  17900. "Set tags in headline, or update according to changed information at point.
  17901. This command does many different things, depending on context:
  17902. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17903. this is what we do.
  17904. - If the cursor is on a statistics cookie, update it.
  17905. - If the cursor is in a headline, prompt for tags and insert them
  17906. into the current line, aligned to `org-tags-column'. When called
  17907. with prefix arg, realign all tags in the current buffer.
  17908. - If the cursor is in one of the special #+KEYWORD lines, this
  17909. triggers scanning the buffer for these lines and updating the
  17910. information.
  17911. - If the cursor is inside a table, realign the table. This command
  17912. works even if the automatic table editor has been turned off.
  17913. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  17914. the entire table.
  17915. - If the cursor is at a footnote reference or definition, jump to
  17916. the corresponding definition or references, respectively.
  17917. - If the cursor is a the beginning of a dynamic block, update it.
  17918. - If the current buffer is a capture buffer, close note and file it.
  17919. - If the cursor is on a <<<target>>>, update radio targets and
  17920. corresponding links in this buffer.
  17921. - If the cursor is on a numbered item in a plain list, renumber the
  17922. ordered list.
  17923. - If the cursor is on a checkbox, toggle it.
  17924. - If the cursor is on a code block, evaluate it. The variable
  17925. `org-confirm-babel-evaluate' can be used to control prompting
  17926. before code block evaluation, by default every code block
  17927. evaluation requires confirmation. Code block evaluation can be
  17928. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  17929. (interactive "P")
  17930. (cond
  17931. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  17932. org-occur-highlights
  17933. org-latex-fragment-image-overlays)
  17934. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  17935. (org-remove-occur-highlights)
  17936. (org-remove-latex-fragment-image-overlays)
  17937. (message "Temporary highlights/overlays removed from current buffer"))
  17938. ((and (local-variable-p 'org-finish-function (current-buffer))
  17939. (fboundp org-finish-function))
  17940. (funcall org-finish-function))
  17941. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  17942. (t
  17943. (let* ((context (org-element-context)) (type (org-element-type context)))
  17944. ;; Test if point is within blanks at the end of an element.
  17945. (if (save-excursion
  17946. (or (not context)
  17947. (beginning-of-line)
  17948. (and (looking-at "[ \t]*$")
  17949. (skip-chars-forward " \r\t\n")
  17950. (>= (point) (org-element-property :end context)))))
  17951. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17952. (user-error "C-c C-c can do nothing useful at this location"))
  17953. ;; For convenience: at the first line of a paragraph on the
  17954. ;; same line as an item, apply function on that item instead.
  17955. (when (eq type 'paragraph)
  17956. (let ((parent (org-element-property :parent context)))
  17957. (when (and (eq (org-element-type parent) 'item)
  17958. (= (point-at-bol) (org-element-property :begin parent)))
  17959. (setq context parent type 'item))))
  17960. ;; Act according to type of element or object at point.
  17961. (case type
  17962. (clock (org-clock-update-time-maybe))
  17963. (dynamic-block
  17964. (save-excursion
  17965. (goto-char (org-element-property :post-affiliated context))
  17966. (org-update-dblock)))
  17967. (footnote-definition
  17968. (save-excursion
  17969. (goto-char (org-element-property :post-affiliated context))
  17970. (call-interactively 'org-footnote-action)))
  17971. (footnote-reference (call-interactively 'org-footnote-action))
  17972. ((headline inlinetask)
  17973. (save-excursion (goto-char (org-element-property :begin context))
  17974. (call-interactively 'org-set-tags)))
  17975. (item
  17976. ;; At an item: a double C-u set checkbox to "[-]"
  17977. ;; unconditionally, whereas a single one will toggle its
  17978. ;; presence. Without an universal argument, if the item
  17979. ;; has a checkbox, toggle it. Otherwise repair the list.
  17980. (let* ((box (org-element-property :checkbox context))
  17981. (struct (org-element-property :structure context))
  17982. (old-struct (copy-tree struct))
  17983. (parents (org-list-parents-alist struct))
  17984. (prevs (org-list-prevs-alist struct))
  17985. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  17986. (org-list-set-checkbox
  17987. (org-element-property :begin context) struct
  17988. (cond ((equal arg '(16)) "[-]")
  17989. ((and (not box) (equal arg '(4))) "[ ]")
  17990. ((or (not box) (equal arg '(4))) nil)
  17991. ((eq box 'on) "[ ]")
  17992. (t "[X]")))
  17993. ;; Mimic `org-list-write-struct' but with grabbing
  17994. ;; a return value from `org-list-struct-fix-box'.
  17995. (org-list-struct-fix-ind struct parents 2)
  17996. (org-list-struct-fix-item-end struct)
  17997. (org-list-struct-fix-bul struct prevs)
  17998. (org-list-struct-fix-ind struct parents)
  17999. (let ((block-item
  18000. (org-list-struct-fix-box struct parents prevs orderedp)))
  18001. (if (and box (equal struct old-struct))
  18002. (if (equal arg '(16))
  18003. (message "Checkboxes already reset")
  18004. (user-error "Cannot toggle this checkbox: %s"
  18005. (if (eq box 'on)
  18006. "all subitems checked"
  18007. "unchecked subitems")))
  18008. (org-list-struct-apply-struct struct old-struct)
  18009. (org-update-checkbox-count-maybe))
  18010. (when block-item
  18011. (message "Checkboxes were removed due to empty box at line %d"
  18012. (org-current-line block-item))))))
  18013. (keyword
  18014. (let ((org-inhibit-startup-visibility-stuff t)
  18015. (org-startup-align-all-tables nil))
  18016. (when (boundp 'org-table-coordinate-overlays)
  18017. (mapc 'delete-overlay org-table-coordinate-overlays)
  18018. (setq org-table-coordinate-overlays nil))
  18019. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18020. (message "Local setup has been refreshed"))
  18021. (plain-list
  18022. ;; At a plain list, with a double C-u argument, set
  18023. ;; checkboxes of each item to "[-]", whereas a single one
  18024. ;; will toggle their presence according to the state of the
  18025. ;; first item in the list. Without an argument, repair the
  18026. ;; list.
  18027. (let* ((begin (org-element-property :contents-begin context))
  18028. (struct (org-element-property :structure context))
  18029. (old-struct (copy-tree struct))
  18030. (first-box (save-excursion
  18031. (goto-char begin)
  18032. (looking-at org-list-full-item-re)
  18033. (match-string-no-properties 3)))
  18034. (new-box (cond ((equal arg '(16)) "[-]")
  18035. ((equal arg '(4)) (unless first-box "[ ]"))
  18036. ((equal first-box "[X]") "[ ]")
  18037. (t "[X]"))))
  18038. (cond
  18039. (arg
  18040. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18041. (org-list-get-all-items
  18042. begin struct (org-list-prevs-alist struct))))
  18043. ((and first-box (eq (point) begin))
  18044. ;; For convenience, when point is at bol on the first
  18045. ;; item of the list and no argument is provided, simply
  18046. ;; toggle checkbox of that item, if any.
  18047. (org-list-set-checkbox begin struct new-box)))
  18048. (org-list-write-struct
  18049. struct (org-list-parents-alist struct) old-struct)
  18050. (org-update-checkbox-count-maybe)
  18051. (save-excursion (goto-char begin) (org-list-send-list 'maybe))))
  18052. ((property-drawer node-property)
  18053. (call-interactively 'org-property-action))
  18054. ((radio-target target)
  18055. (call-interactively 'org-update-radio-target-regexp))
  18056. (statistics-cookie
  18057. (call-interactively 'org-update-statistics-cookies))
  18058. ((table table-cell table-row)
  18059. ;; At a table, recalculate every field and align it. Also
  18060. ;; send the table if necessary. If the table has
  18061. ;; a `table.el' type, just give up. At a table row or
  18062. ;; cell, maybe recalculate line but always align table.
  18063. (if (eq (org-element-property :type context) 'table.el)
  18064. (message "Use C-c ' to edit table.el tables")
  18065. (let ((org-enable-table-editor t))
  18066. (if (or (eq type 'table)
  18067. ;; Check if point is at a TBLFM line.
  18068. (and (eq type 'table-row)
  18069. (= (point) (org-element-property :end context))))
  18070. (save-excursion
  18071. (goto-char (org-element-property :contents-begin context))
  18072. (org-call-with-arg 'org-table-recalculate (or arg t))
  18073. (orgtbl-send-table 'maybe))
  18074. (org-table-maybe-eval-formula)
  18075. (cond (arg (call-interactively 'org-table-recalculate))
  18076. ((org-table-maybe-recalculate-line))
  18077. (t (org-table-align)))))))
  18078. (timestamp (org-timestamp-change 0 'day))
  18079. (otherwise
  18080. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18081. (user-error
  18082. "C-c C-c can do nothing useful at this location")))))))))
  18083. (defun org-mode-restart ()
  18084. "Restart Org-mode, to scan again for special lines.
  18085. Also updates the keyword regular expressions."
  18086. (interactive)
  18087. (org-mode)
  18088. (message "Org-mode restarted"))
  18089. (defun org-kill-note-or-show-branches ()
  18090. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18091. (interactive)
  18092. (if (not org-finish-function)
  18093. (progn
  18094. (hide-subtree)
  18095. (call-interactively 'show-branches))
  18096. (let ((org-note-abort t))
  18097. (funcall org-finish-function))))
  18098. (defun org-return (&optional indent)
  18099. "Goto next table row or insert a newline.
  18100. Calls `org-table-next-row' or `newline', depending on context.
  18101. See the individual commands for more information."
  18102. (interactive)
  18103. (let (org-ts-what)
  18104. (cond
  18105. ((or (bobp) (org-in-src-block-p))
  18106. (if indent (newline-and-indent) (newline)))
  18107. ((org-at-table-p)
  18108. (org-table-justify-field-maybe)
  18109. (call-interactively 'org-table-next-row))
  18110. ;; when `newline-and-indent' is called within a list, make sure
  18111. ;; text moved stays inside the item.
  18112. ((and (org-in-item-p) indent)
  18113. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18114. (progn
  18115. (save-match-data (newline))
  18116. (org-indent-line-to (length (match-string 0))))
  18117. (let ((ind (org-get-indentation)))
  18118. (newline)
  18119. (if (org-looking-back org-list-end-re)
  18120. (org-indent-line)
  18121. (org-indent-line-to ind)))))
  18122. ((and org-return-follows-link
  18123. (org-at-timestamp-p t)
  18124. (not (eq org-ts-what 'after)))
  18125. (org-follow-timestamp-link))
  18126. ((and org-return-follows-link
  18127. (let ((tprop (get-text-property (point) 'face)))
  18128. (or (eq tprop 'org-link)
  18129. (and (listp tprop) (memq 'org-link tprop)))))
  18130. (call-interactively 'org-open-at-point))
  18131. ((and (org-at-heading-p)
  18132. (looking-at
  18133. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18134. (org-show-entry)
  18135. (end-of-line 1)
  18136. (newline))
  18137. (t (if indent (newline-and-indent) (newline))))))
  18138. (defun org-return-indent ()
  18139. "Goto next table row or insert a newline and indent.
  18140. Calls `org-table-next-row' or `newline-and-indent', depending on
  18141. context. See the individual commands for more information."
  18142. (interactive)
  18143. (org-return t))
  18144. (defun org-ctrl-c-star ()
  18145. "Compute table, or change heading status of lines.
  18146. Calls `org-table-recalculate' or `org-toggle-heading',
  18147. depending on context."
  18148. (interactive)
  18149. (cond
  18150. ((org-at-table-p)
  18151. (call-interactively 'org-table-recalculate))
  18152. (t
  18153. ;; Convert all lines in region to list items
  18154. (call-interactively 'org-toggle-heading))))
  18155. (defun org-ctrl-c-minus ()
  18156. "Insert separator line in table or modify bullet status of line.
  18157. Also turns a plain line or a region of lines into list items.
  18158. Calls `org-table-insert-hline', `org-toggle-item', or
  18159. `org-cycle-list-bullet', depending on context."
  18160. (interactive)
  18161. (cond
  18162. ((org-at-table-p)
  18163. (call-interactively 'org-table-insert-hline))
  18164. ((org-region-active-p)
  18165. (call-interactively 'org-toggle-item))
  18166. ((org-in-item-p)
  18167. (call-interactively 'org-cycle-list-bullet))
  18168. (t
  18169. (call-interactively 'org-toggle-item))))
  18170. (defun org-toggle-item (arg)
  18171. "Convert headings or normal lines to items, items to normal lines.
  18172. If there is no active region, only the current line is considered.
  18173. If the first non blank line in the region is an headline, convert
  18174. all headlines to items, shifting text accordingly.
  18175. If it is an item, convert all items to normal lines.
  18176. If it is normal text, change region into an item. With a prefix
  18177. argument ARG, change each line in region into an item."
  18178. (interactive "P")
  18179. (let ((shift-text
  18180. (function
  18181. ;; Shift text in current section to IND, from point to END.
  18182. ;; The function leaves point to END line.
  18183. (lambda (ind end)
  18184. (let ((min-i 1000) (end (copy-marker end)))
  18185. ;; First determine the minimum indentation (MIN-I) of
  18186. ;; the text.
  18187. (save-excursion
  18188. (catch 'exit
  18189. (while (< (point) end)
  18190. (let ((i (org-get-indentation)))
  18191. (cond
  18192. ;; Skip blank lines and inline tasks.
  18193. ((looking-at "^[ \t]*$"))
  18194. ((looking-at org-outline-regexp-bol))
  18195. ;; We can't find less than 0 indentation.
  18196. ((zerop i) (throw 'exit (setq min-i 0)))
  18197. ((< i min-i) (setq min-i i))))
  18198. (forward-line))))
  18199. ;; Then indent each line so that a line indented to
  18200. ;; MIN-I becomes indented to IND. Ignore blank lines
  18201. ;; and inline tasks in the process.
  18202. (let ((delta (- ind min-i)))
  18203. (while (< (point) end)
  18204. (unless (or (looking-at "^[ \t]*$")
  18205. (looking-at org-outline-regexp-bol))
  18206. (org-indent-line-to (+ (org-get-indentation) delta)))
  18207. (forward-line)))))))
  18208. (skip-blanks
  18209. (function
  18210. ;; Return beginning of first non-blank line, starting from
  18211. ;; line at POS.
  18212. (lambda (pos)
  18213. (save-excursion
  18214. (goto-char pos)
  18215. (skip-chars-forward " \r\t\n")
  18216. (point-at-bol)))))
  18217. beg end)
  18218. ;; Determine boundaries of changes.
  18219. (if (org-region-active-p)
  18220. (setq beg (funcall skip-blanks (region-beginning))
  18221. end (copy-marker (region-end)))
  18222. (setq beg (funcall skip-blanks (point-at-bol))
  18223. end (copy-marker (point-at-eol))))
  18224. ;; Depending on the starting line, choose an action on the text
  18225. ;; between BEG and END.
  18226. (org-with-limited-levels
  18227. (save-excursion
  18228. (goto-char beg)
  18229. (cond
  18230. ;; Case 1. Start at an item: de-itemize. Note that it only
  18231. ;; happens when a region is active: `org-ctrl-c-minus'
  18232. ;; would call `org-cycle-list-bullet' otherwise.
  18233. ((org-at-item-p)
  18234. (while (< (point) end)
  18235. (when (org-at-item-p)
  18236. (skip-chars-forward " \t")
  18237. (delete-region (point) (match-end 0)))
  18238. (forward-line)))
  18239. ;; Case 2. Start at an heading: convert to items.
  18240. ((org-at-heading-p)
  18241. (let* ((bul (org-list-bullet-string "-"))
  18242. (bul-len (length bul))
  18243. ;; Indentation of the first heading. It should be
  18244. ;; relative to the indentation of its parent, if any.
  18245. (start-ind (save-excursion
  18246. (cond
  18247. ((not org-adapt-indentation) 0)
  18248. ((not (outline-previous-heading)) 0)
  18249. (t (length (match-string 0))))))
  18250. ;; Level of first heading. Further headings will be
  18251. ;; compared to it to determine hierarchy in the list.
  18252. (ref-level (org-reduced-level (org-outline-level))))
  18253. (while (< (point) end)
  18254. (let* ((level (org-reduced-level (org-outline-level)))
  18255. (delta (max 0 (- level ref-level))))
  18256. ;; If current headline is less indented than the first
  18257. ;; one, set it as reference, in order to preserve
  18258. ;; subtrees.
  18259. (when (< level ref-level) (setq ref-level level))
  18260. (replace-match bul t t)
  18261. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18262. ;; Ensure all text down to END (or SECTION-END) belongs
  18263. ;; to the newly created item.
  18264. (let ((section-end (save-excursion
  18265. (or (outline-next-heading) (point)))))
  18266. (forward-line)
  18267. (funcall shift-text
  18268. (+ start-ind (* (1+ delta) bul-len))
  18269. (min end section-end)))))))
  18270. ;; Case 3. Normal line with ARG: turn each non-item line into
  18271. ;; an item.
  18272. (arg
  18273. (while (< (point) end)
  18274. (unless (or (org-at-heading-p) (org-at-item-p))
  18275. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18276. (replace-match
  18277. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18278. (forward-line)))
  18279. ;; Case 4. Normal line without ARG: make the first line of
  18280. ;; region an item, and shift indentation of others
  18281. ;; lines to set them as item's body.
  18282. (t (let* ((bul (org-list-bullet-string "-"))
  18283. (bul-len (length bul))
  18284. (ref-ind (org-get-indentation)))
  18285. (skip-chars-forward " \t")
  18286. (insert bul)
  18287. (forward-line)
  18288. (while (< (point) end)
  18289. ;; Ensure that lines less indented than first one
  18290. ;; still get included in item body.
  18291. (funcall shift-text
  18292. (+ ref-ind bul-len)
  18293. (min end (save-excursion (or (outline-next-heading)
  18294. (point)))))
  18295. (forward-line)))))))))
  18296. (defun org-toggle-heading (&optional nstars)
  18297. "Convert headings to normal text, or items or text to headings.
  18298. If there is no active region, only the current line is considered.
  18299. With a \\[universal-argument] prefix, convert the whole list at
  18300. point into heading.
  18301. In a region:
  18302. - If the first non blank line is an headline, remove the stars
  18303. from all headlines in the region.
  18304. - If it is a normal line turn each and every normal line (i.e. not an
  18305. heading or an item) in the region into a heading.
  18306. - If it is a plain list item, turn all plain list items into headings.
  18307. When converting a line into a heading, the number of stars is chosen
  18308. such that the lines become children of the current entry. However,
  18309. when a prefix argument is given, its value determines the number of
  18310. stars to add."
  18311. (interactive "P")
  18312. (let ((skip-blanks
  18313. (function
  18314. ;; Return beginning of first non-blank line, starting from
  18315. ;; line at POS.
  18316. (lambda (pos)
  18317. (save-excursion
  18318. (goto-char pos)
  18319. (while (org-at-comment-p) (forward-line))
  18320. (skip-chars-forward " \r\t\n")
  18321. (point-at-bol)))))
  18322. beg end toggled)
  18323. ;; Determine boundaries of changes. If a universal prefix has
  18324. ;; been given, put the list in a region. If region ends at a bol,
  18325. ;; do not consider the last line to be in the region.
  18326. (when (and current-prefix-arg (org-at-item-p))
  18327. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  18328. (org-mark-element))
  18329. (if (org-region-active-p)
  18330. (setq beg (funcall skip-blanks (region-beginning))
  18331. end (copy-marker (save-excursion
  18332. (goto-char (region-end))
  18333. (if (bolp) (point) (point-at-eol)))))
  18334. (setq beg (funcall skip-blanks (point-at-bol))
  18335. end (copy-marker (point-at-eol))))
  18336. ;; Ensure inline tasks don't count as headings.
  18337. (org-with-limited-levels
  18338. (save-excursion
  18339. (goto-char beg)
  18340. (cond
  18341. ;; Case 1. Started at an heading: de-star headings.
  18342. ((org-at-heading-p)
  18343. (while (< (point) end)
  18344. (when (org-at-heading-p t)
  18345. (looking-at org-outline-regexp) (replace-match "")
  18346. (setq toggled t))
  18347. (forward-line)))
  18348. ;; Case 2. Started at an item: change items into headlines.
  18349. ;; One star will be added by `org-list-to-subtree'.
  18350. ((org-at-item-p)
  18351. (let* ((stars (make-string
  18352. (if nstars
  18353. ;; subtract the star that will be added again by
  18354. ;; `org-list-to-subtree'
  18355. (1- (prefix-numeric-value current-prefix-arg))
  18356. (or (org-current-level) 0))
  18357. ?*))
  18358. (add-stars
  18359. (cond (nstars "") ; stars from prefix only
  18360. ((equal stars "") "") ; before first heading
  18361. (org-odd-levels-only "*") ; inside heading, odd
  18362. (t "")))) ; inside heading, oddeven
  18363. (while (< (point) end)
  18364. (when (org-at-item-p)
  18365. ;; Pay attention to cases when region ends before list.
  18366. (let* ((struct (org-list-struct))
  18367. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18368. (save-restriction
  18369. (narrow-to-region (point) list-end)
  18370. (insert
  18371. (org-list-to-subtree
  18372. (org-list-parse-list t)
  18373. '(:istart (concat stars add-stars (funcall get-stars depth))
  18374. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18375. (setq toggled t))
  18376. (forward-line))))
  18377. ;; Case 3. Started at normal text: make every line an heading,
  18378. ;; skipping headlines and items.
  18379. (t (let* ((stars (make-string
  18380. (if nstars
  18381. (prefix-numeric-value current-prefix-arg)
  18382. (or (org-current-level) 0))
  18383. ?*))
  18384. (add-stars
  18385. (cond (nstars "") ; stars from prefix only
  18386. ((equal stars "") "*") ; before first heading
  18387. (org-odd-levels-only "**") ; inside heading, odd
  18388. (t "*"))) ; inside heading, oddeven
  18389. (rpl (concat stars add-stars " ")))
  18390. (while (< (point) end)
  18391. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18392. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18393. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18394. (forward-line)))))))
  18395. (unless toggled (message "Cannot toggle heading from here"))))
  18396. (defun org-meta-return (&optional arg)
  18397. "Insert a new heading or wrap a region in a table.
  18398. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18399. See the individual commands for more information."
  18400. (interactive "P")
  18401. (cond
  18402. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18403. ((or (org-at-drawer-p) (org-at-property-p))
  18404. (newline-and-indent))
  18405. ((org-at-table-p)
  18406. (call-interactively 'org-table-wrap-region))
  18407. (t (call-interactively 'org-insert-heading))))
  18408. ;;; Menu entries
  18409. (defsubst org-in-subtree-not-table-p ()
  18410. "Are we in a subtree and not in a table?"
  18411. (and (not (org-before-first-heading-p))
  18412. (not (org-at-table-p))))
  18413. ;; Define the Org-mode menus
  18414. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18415. '("Tbl"
  18416. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18417. ["Next Field" org-cycle (org-at-table-p)]
  18418. ["Previous Field" org-shifttab (org-at-table-p)]
  18419. ["Next Row" org-return (org-at-table-p)]
  18420. "--"
  18421. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18422. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18423. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18424. "--"
  18425. ("Column"
  18426. ["Move Column Left" org-metaleft (org-at-table-p)]
  18427. ["Move Column Right" org-metaright (org-at-table-p)]
  18428. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18429. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18430. ("Row"
  18431. ["Move Row Up" org-metaup (org-at-table-p)]
  18432. ["Move Row Down" org-metadown (org-at-table-p)]
  18433. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18434. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18435. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18436. "--"
  18437. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18438. ("Rectangle"
  18439. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18440. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18441. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18442. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18443. "--"
  18444. ("Calculate"
  18445. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18446. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18447. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18448. "--"
  18449. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18450. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18451. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18452. "--"
  18453. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18454. "--"
  18455. ["Sum Column/Rectangle" org-table-sum
  18456. (or (org-at-table-p) (org-region-active-p))]
  18457. ["Which Column?" org-table-current-column (org-at-table-p)])
  18458. ["Debug Formulas"
  18459. org-table-toggle-formula-debugger
  18460. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18461. ["Show Col/Row Numbers"
  18462. org-table-toggle-coordinate-overlays
  18463. :style toggle
  18464. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18465. "--"
  18466. ["Create" org-table-create (and (not (org-at-table-p))
  18467. org-enable-table-editor)]
  18468. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18469. ["Import from File" org-table-import (not (org-at-table-p))]
  18470. ["Export to File" org-table-export (org-at-table-p)]
  18471. "--"
  18472. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18473. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18474. '("Org"
  18475. ("Show/Hide"
  18476. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18477. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18478. ["Sparse Tree..." org-sparse-tree t]
  18479. ["Reveal Context" org-reveal t]
  18480. ["Show All" show-all t]
  18481. "--"
  18482. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18483. "--"
  18484. ["New Heading" org-insert-heading t]
  18485. ("Navigate Headings"
  18486. ["Up" outline-up-heading t]
  18487. ["Next" outline-next-visible-heading t]
  18488. ["Previous" outline-previous-visible-heading t]
  18489. ["Next Same Level" outline-forward-same-level t]
  18490. ["Previous Same Level" outline-backward-same-level t]
  18491. "--"
  18492. ["Jump" org-goto t])
  18493. ("Edit Structure"
  18494. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18495. "--"
  18496. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18497. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18498. "--"
  18499. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18500. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18501. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18502. "--"
  18503. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18504. "--"
  18505. ["Copy visible text" org-copy-visible t]
  18506. "--"
  18507. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18508. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18509. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18510. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18511. "--"
  18512. ["Sort Region/Children" org-sort t]
  18513. "--"
  18514. ["Convert to odd levels" org-convert-to-odd-levels t]
  18515. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18516. ("Editing"
  18517. ["Emphasis..." org-emphasize t]
  18518. ["Edit Source Example" org-edit-special t]
  18519. "--"
  18520. ["Footnote new/jump" org-footnote-action t]
  18521. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18522. ("Archive"
  18523. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18524. "--"
  18525. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18526. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18527. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18528. )
  18529. "--"
  18530. ("Hyperlinks"
  18531. ["Store Link (Global)" org-store-link t]
  18532. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18533. ["Insert Link" org-insert-link t]
  18534. ["Follow Link" org-open-at-point t]
  18535. "--"
  18536. ["Next link" org-next-link t]
  18537. ["Previous link" org-previous-link t]
  18538. "--"
  18539. ["Descriptive Links"
  18540. org-toggle-link-display
  18541. :style radio
  18542. :selected org-descriptive-links
  18543. ]
  18544. ["Literal Links"
  18545. org-toggle-link-display
  18546. :style radio
  18547. :selected (not org-descriptive-links)])
  18548. "--"
  18549. ("TODO Lists"
  18550. ["TODO/DONE/-" org-todo t]
  18551. ("Select keyword"
  18552. ["Next keyword" org-shiftright (org-at-heading-p)]
  18553. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18554. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18555. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18556. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18557. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18558. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18559. "--"
  18560. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18561. :selected org-enforce-todo-dependencies :style toggle :active t]
  18562. "Settings for tree at point"
  18563. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18564. :selected (org-entry-get nil "ORDERED")
  18565. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18566. ["Do Children parallel" org-toggle-ordered-property :style radio
  18567. :selected (not (org-entry-get nil "ORDERED"))
  18568. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18569. "--"
  18570. ["Set Priority" org-priority t]
  18571. ["Priority Up" org-shiftup t]
  18572. ["Priority Down" org-shiftdown t]
  18573. "--"
  18574. ["Get news from all feeds" org-feed-update-all t]
  18575. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18576. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18577. ("TAGS and Properties"
  18578. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18579. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18580. "--"
  18581. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18582. ["Column view of properties" org-columns t]
  18583. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18584. ("Dates and Scheduling"
  18585. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18586. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18587. ("Change Date"
  18588. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18589. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18590. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18591. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18592. ["Compute Time Range" org-evaluate-time-range t]
  18593. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18594. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18595. "--"
  18596. ["Custom time format" org-toggle-time-stamp-overlays
  18597. :style radio :selected org-display-custom-times]
  18598. "--"
  18599. ["Goto Calendar" org-goto-calendar t]
  18600. ["Date from Calendar" org-date-from-calendar t]
  18601. "--"
  18602. ["Start/Restart Timer" org-timer-start t]
  18603. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18604. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18605. ["Insert Timer String" org-timer t]
  18606. ["Insert Timer Item" org-timer-item t])
  18607. ("Logging work"
  18608. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18609. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18610. ["Clock out" org-clock-out t]
  18611. ["Clock cancel" org-clock-cancel t]
  18612. "--"
  18613. ["Mark as default task" org-clock-mark-default-task t]
  18614. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18615. ["Goto running clock" org-clock-goto t]
  18616. "--"
  18617. ["Display times" org-clock-display t]
  18618. ["Create clock table" org-clock-report t]
  18619. "--"
  18620. ["Record DONE time"
  18621. (progn (setq org-log-done (not org-log-done))
  18622. (message "Switching to %s will %s record a timestamp"
  18623. (car org-done-keywords)
  18624. (if org-log-done "automatically" "not")))
  18625. :style toggle :selected org-log-done])
  18626. "--"
  18627. ["Agenda Command..." org-agenda t]
  18628. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18629. ("File List for Agenda")
  18630. ("Special views current file"
  18631. ["TODO Tree" org-show-todo-tree t]
  18632. ["Check Deadlines" org-check-deadlines t]
  18633. ["Timeline" org-timeline t]
  18634. ["Tags/Property tree" org-match-sparse-tree t])
  18635. "--"
  18636. ["Export/Publish..." org-export t]
  18637. ("LaTeX"
  18638. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18639. :selected org-cdlatex-mode]
  18640. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18641. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18642. ["Modify math symbol" org-cdlatex-math-modify
  18643. (org-inside-LaTeX-fragment-p)]
  18644. ["Insert citation" org-reftex-citation t]
  18645. "--"
  18646. ["Template for BEAMER" (progn (require 'org-beamer)
  18647. (org-insert-beamer-options-template)) t])
  18648. "--"
  18649. ("MobileOrg"
  18650. ["Push Files and Views" org-mobile-push t]
  18651. ["Get Captured and Flagged" org-mobile-pull t]
  18652. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18653. "--"
  18654. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18655. "--"
  18656. ("Documentation"
  18657. ["Show Version" org-version t]
  18658. ["Info Documentation" org-info t])
  18659. ("Customize"
  18660. ["Browse Org Group" org-customize t]
  18661. "--"
  18662. ["Expand This Menu" org-create-customize-menu
  18663. (fboundp 'customize-menu-create)])
  18664. ["Send bug report" org-submit-bug-report t]
  18665. "--"
  18666. ("Refresh/Reload"
  18667. ["Refresh setup current buffer" org-mode-restart t]
  18668. ["Reload Org (after update)" org-reload t]
  18669. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  18670. ))
  18671. (defun org-info (&optional node)
  18672. "Read documentation for Org-mode in the info system.
  18673. With optional NODE, go directly to that node."
  18674. (interactive)
  18675. (info (format "(org)%s" (or node ""))))
  18676. ;;;###autoload
  18677. (defun org-submit-bug-report ()
  18678. "Submit a bug report on Org-mode via mail.
  18679. Don't hesitate to report any problems or inaccurate documentation.
  18680. If you don't have setup sending mail from (X)Emacs, please copy the
  18681. output buffer into your mail program, as it gives us important
  18682. information about your Org-mode version and configuration."
  18683. (interactive)
  18684. (require 'reporter)
  18685. (org-load-modules-maybe)
  18686. (org-require-autoloaded-modules)
  18687. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18688. (reporter-submit-bug-report
  18689. "emacs-orgmode@gnu.org"
  18690. (org-version nil 'full)
  18691. (let (list)
  18692. (save-window-excursion
  18693. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18694. (delete-other-windows)
  18695. (erase-buffer)
  18696. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18697. We would like to add your full Org-mode and Outline configuration to the
  18698. bug report. This greatly simplifies the work of the maintainer and
  18699. other experts on the mailing list.
  18700. HOWEVER, some variables you have customized may contain private
  18701. information. The names of customers, colleagues, or friends, might
  18702. appear in the form of file names, tags, todo states, or search strings.
  18703. If you answer yes to the prompt, you might want to check and remove
  18704. such private information before sending the email.")
  18705. (add-text-properties (point-min) (point-max) '(face org-warning))
  18706. (when (yes-or-no-p "Include your Org-mode configuration ")
  18707. (mapatoms
  18708. (lambda (v)
  18709. (and (boundp v)
  18710. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18711. (or (and (symbol-value v)
  18712. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18713. (and
  18714. (get v 'custom-type) (get v 'standard-value)
  18715. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18716. (push v list)))))
  18717. (kill-buffer (get-buffer "*Warn about privacy*"))
  18718. list))
  18719. nil nil
  18720. "Remember to cover the basics, that is, what you expected to happen and
  18721. what in fact did happen. You don't know how to make a good report? See
  18722. http://orgmode.org/manual/Feedback.html#Feedback
  18723. Your bug report will be posted to the Org-mode mailing list.
  18724. ------------------------------------------------------------------------")
  18725. (save-excursion
  18726. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18727. (replace-match "\\1Bug: \\3 [\\2]")))))
  18728. (defun org-install-agenda-files-menu ()
  18729. (let ((bl (buffer-list)))
  18730. (save-excursion
  18731. (while bl
  18732. (set-buffer (pop bl))
  18733. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18734. (when (derived-mode-p 'org-mode)
  18735. (easy-menu-change
  18736. '("Org") "File List for Agenda"
  18737. (append
  18738. (list
  18739. ["Edit File List" (org-edit-agenda-file-list) t]
  18740. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18741. ["Remove Current File from List" org-remove-file t]
  18742. ["Cycle through agenda files" org-cycle-agenda-files t]
  18743. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18744. "--")
  18745. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18746. ;;;; Documentation
  18747. (defun org-require-autoloaded-modules ()
  18748. (interactive)
  18749. (mapc 'require
  18750. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  18751. org-docbook org-exp org-html org-icalendar
  18752. org-id org-latex
  18753. org-publish org-remember org-table
  18754. org-timer org-xoxo)))
  18755. ;;;###autoload
  18756. (defun org-reload (&optional uncompiled)
  18757. "Reload all org lisp files.
  18758. With prefix arg UNCOMPILED, load the uncompiled versions."
  18759. (interactive "P")
  18760. (require 'loadhist)
  18761. (let* ((org-dir (org-find-library-dir "org"))
  18762. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  18763. (feature-re "^\\(org\\|ob\\)\\(-.*\\)?")
  18764. (remove-re (mapconcat 'identity
  18765. (mapcar (lambda (f) (concat "^" f "$"))
  18766. (list (if (featurep 'xemacs)
  18767. "org-colview"
  18768. "org-colview-xemacs")
  18769. "org" "org-loaddefs" "org-version"))
  18770. "\\|"))
  18771. (feats (delete-dups
  18772. (mapcar 'file-name-sans-extension
  18773. (mapcar 'file-name-nondirectory
  18774. (delq nil
  18775. (mapcar 'feature-file
  18776. features))))))
  18777. (lfeat (append
  18778. (sort
  18779. (setq feats
  18780. (delq nil (mapcar
  18781. (lambda (f)
  18782. (if (and (string-match feature-re f)
  18783. (not (string-match remove-re f)))
  18784. f nil))
  18785. feats)))
  18786. 'string-lessp)
  18787. (list "org-version" "org")))
  18788. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  18789. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  18790. load-uncore load-misses)
  18791. (setq load-misses
  18792. (delq 't
  18793. (mapcar (lambda (f)
  18794. (or (org-load-noerror-mustsuffix (concat org-dir f))
  18795. (and (string= org-dir contrib-dir)
  18796. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  18797. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  18798. (add-to-list 'load-uncore f 'append)
  18799. 't)
  18800. f))
  18801. lfeat)))
  18802. (if load-uncore
  18803. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  18804. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  18805. (if load-misses
  18806. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  18807. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  18808. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  18809. ;;;###autoload
  18810. (defun org-customize ()
  18811. "Call the customize function with org as argument."
  18812. (interactive)
  18813. (org-load-modules-maybe)
  18814. (org-require-autoloaded-modules)
  18815. (customize-browse 'org))
  18816. (defun org-create-customize-menu ()
  18817. "Create a full customization menu for Org-mode, insert it into the menu."
  18818. (interactive)
  18819. (org-load-modules-maybe)
  18820. (org-require-autoloaded-modules)
  18821. (if (fboundp 'customize-menu-create)
  18822. (progn
  18823. (easy-menu-change
  18824. '("Org") "Customize"
  18825. `(["Browse Org group" org-customize t]
  18826. "--"
  18827. ,(customize-menu-create 'org)
  18828. ["Set" Custom-set t]
  18829. ["Save" Custom-save t]
  18830. ["Reset to Current" Custom-reset-current t]
  18831. ["Reset to Saved" Custom-reset-saved t]
  18832. ["Reset to Standard Settings" Custom-reset-standard t]))
  18833. (message "\"Org\"-menu now contains full customization menu"))
  18834. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  18835. ;;;; Miscellaneous stuff
  18836. ;;; Generally useful functions
  18837. (defun org-get-at-bol (property)
  18838. "Get text property PROPERTY at beginning of line."
  18839. (get-text-property (point-at-bol) property))
  18840. (defun org-find-text-property-in-string (prop s)
  18841. "Return the first non-nil value of property PROP in string S."
  18842. (or (get-text-property 0 prop s)
  18843. (get-text-property (or (next-single-property-change 0 prop s) 0)
  18844. prop s)))
  18845. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  18846. "Display the given MESSAGE as a warning."
  18847. (if (fboundp 'display-warning)
  18848. (display-warning 'org message
  18849. (if (featurep 'xemacs) 'warning :warning))
  18850. (let ((buf (get-buffer-create "*Org warnings*")))
  18851. (with-current-buffer buf
  18852. (goto-char (point-max))
  18853. (insert "Warning (Org): " message)
  18854. (unless (bolp)
  18855. (newline)))
  18856. (display-buffer buf)
  18857. (sit-for 0))))
  18858. (defun org-eval (form)
  18859. "Eval FORM and return result."
  18860. (condition-case error
  18861. (eval form)
  18862. (error (format "%%![Error: %s]" error))))
  18863. (defun org-in-clocktable-p ()
  18864. "Check if the cursor is in a clocktable."
  18865. (let ((pos (point)) start)
  18866. (save-excursion
  18867. (end-of-line 1)
  18868. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18869. (setq start (match-beginning 0))
  18870. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18871. (>= (match-end 0) pos)
  18872. start))))
  18873. (defun org-in-commented-line ()
  18874. "Is point in a line starting with `#'?"
  18875. (equal (char-after (point-at-bol)) ?#))
  18876. (defun org-in-indented-comment-line ()
  18877. "Is point in a line starting with `#' after some white space?"
  18878. (save-excursion
  18879. (save-match-data
  18880. (goto-char (point-at-bol))
  18881. (looking-at "[ \t]*#"))))
  18882. (defun org-in-verbatim-emphasis ()
  18883. (save-match-data
  18884. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18885. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18886. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18887. (if (and marker (marker-buffer marker)
  18888. (buffer-live-p (marker-buffer marker)))
  18889. (progn
  18890. (org-pop-to-buffer-same-window (marker-buffer marker))
  18891. (if (or (> marker (point-max)) (< marker (point-min)))
  18892. (widen))
  18893. (goto-char marker)
  18894. (org-show-context 'org-goto))
  18895. (if bookmark
  18896. (bookmark-jump bookmark)
  18897. (error "Cannot find location"))))
  18898. (defun org-quote-csv-field (s)
  18899. "Quote field for inclusion in CSV material."
  18900. (if (string-match "[\",]" s)
  18901. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18902. s))
  18903. (defun org-force-self-insert (N)
  18904. "Needed to enforce self-insert under remapping."
  18905. (interactive "p")
  18906. (self-insert-command N))
  18907. (defun org-string-width (s)
  18908. "Compute width of string, ignoring invisible characters.
  18909. This ignores character with invisibility property `org-link', and also
  18910. characters with property `org-cwidth', because these will become invisible
  18911. upon the next fontification round."
  18912. (let (b l)
  18913. (when (or (eq t buffer-invisibility-spec)
  18914. (assq 'org-link buffer-invisibility-spec))
  18915. (while (setq b (text-property-any 0 (length s)
  18916. 'invisible 'org-link s))
  18917. (setq s (concat (substring s 0 b)
  18918. (substring s (or (next-single-property-change
  18919. b 'invisible s) (length s)))))))
  18920. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  18921. (setq s (concat (substring s 0 b)
  18922. (substring s (or (next-single-property-change
  18923. b 'org-cwidth s) (length s))))))
  18924. (setq l (string-width s) b -1)
  18925. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  18926. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  18927. l))
  18928. (defun org-shorten-string (s maxlength)
  18929. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  18930. If the string is shorter or has length MAXLENGTH, just return the
  18931. original string. If it is longer, the functions finds a space in the
  18932. string, breaks this string off at that locations and adds three dots
  18933. as ellipsis. Including the ellipsis, the string will not be longer
  18934. than MAXLENGTH. If finding a good breaking point in the string does
  18935. not work, the string is just chopped off in the middle of a word
  18936. if necessary."
  18937. (if (<= (length s) maxlength)
  18938. s
  18939. (let* ((n (max (- maxlength 4) 1))
  18940. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  18941. (if (string-match re s)
  18942. (concat (match-string 1 s) "...")
  18943. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  18944. (defun org-get-indentation (&optional line)
  18945. "Get the indentation of the current line, interpreting tabs.
  18946. When LINE is given, assume it represents a line and compute its indentation."
  18947. (if line
  18948. (if (string-match "^ *" (org-remove-tabs line))
  18949. (match-end 0))
  18950. (save-excursion
  18951. (beginning-of-line 1)
  18952. (skip-chars-forward " \t")
  18953. (current-column))))
  18954. (defun org-get-string-indentation (s)
  18955. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  18956. (let ((n -1) (i 0) (w tab-width) c)
  18957. (catch 'exit
  18958. (while (< (setq n (1+ n)) (length s))
  18959. (setq c (aref s n))
  18960. (cond ((= c ?\ ) (setq i (1+ i)))
  18961. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  18962. (t (throw 'exit t)))))
  18963. i))
  18964. (defun org-remove-tabs (s &optional width)
  18965. "Replace tabulators in S with spaces.
  18966. Assumes that s is a single line, starting in column 0."
  18967. (setq width (or width tab-width))
  18968. (while (string-match "\t" s)
  18969. (setq s (replace-match
  18970. (make-string
  18971. (- (* width (/ (+ (match-beginning 0) width) width))
  18972. (match-beginning 0)) ?\ )
  18973. t t s)))
  18974. s)
  18975. (defun org-fix-indentation (line ind)
  18976. "Fix indentation in LINE.
  18977. IND is a cons cell with target and minimum indentation.
  18978. If the current indentation in LINE is smaller than the minimum,
  18979. leave it alone. If it is larger than ind, set it to the target."
  18980. (let* ((l (org-remove-tabs line))
  18981. (i (org-get-indentation l))
  18982. (i1 (car ind)) (i2 (cdr ind)))
  18983. (if (>= i i2) (setq l (substring line i2)))
  18984. (if (> i1 0)
  18985. (concat (make-string i1 ?\ ) l)
  18986. l)))
  18987. (defun org-remove-indentation (code &optional n)
  18988. "Remove the maximum common indentation from the lines in CODE.
  18989. N may optionally be the number of spaces to remove."
  18990. (with-temp-buffer
  18991. (insert code)
  18992. (org-do-remove-indentation n)
  18993. (buffer-string)))
  18994. (defun org-do-remove-indentation (&optional n)
  18995. "Remove the maximum common indentation from the buffer."
  18996. (untabify (point-min) (point-max))
  18997. (let ((min 10000) re)
  18998. (if n
  18999. (setq min n)
  19000. (goto-char (point-min))
  19001. (while (re-search-forward "^ *[^ \n]" nil t)
  19002. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19003. (unless (or (= min 0) (= min 10000))
  19004. (setq re (format "^ \\{%d\\}" min))
  19005. (goto-char (point-min))
  19006. (while (re-search-forward re nil t)
  19007. (replace-match "")
  19008. (end-of-line 1))
  19009. min)))
  19010. (defun org-fill-template (template alist)
  19011. "Find each %key of ALIST in TEMPLATE and replace it."
  19012. (let ((case-fold-search nil)
  19013. entry key value)
  19014. (setq alist (sort (copy-sequence alist)
  19015. (lambda (a b) (< (length (car a)) (length (car b))))))
  19016. (while (setq entry (pop alist))
  19017. (setq template
  19018. (replace-regexp-in-string
  19019. (concat "%" (regexp-quote (car entry)))
  19020. (or (cdr entry) "") template t t)))
  19021. template))
  19022. (defun org-base-buffer (buffer)
  19023. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19024. (if (not buffer)
  19025. buffer
  19026. (or (buffer-base-buffer buffer)
  19027. buffer)))
  19028. (defun org-trim (s)
  19029. "Remove whitespace at beginning and end of string."
  19030. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19031. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19032. s)
  19033. (defun org-wrap (string &optional width lines)
  19034. "Wrap string to either a number of lines, or a width in characters.
  19035. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19036. that costs. If there is a word longer than WIDTH, the text is actually
  19037. wrapped to the length of that word.
  19038. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19039. many lines, whatever width that takes.
  19040. The return value is a list of lines, without newlines at the end."
  19041. (let* ((words (org-split-string string "[ \t\n]+"))
  19042. (maxword (apply 'max (mapcar 'org-string-width words)))
  19043. w ll)
  19044. (cond (width
  19045. (org-do-wrap words (max maxword width)))
  19046. (lines
  19047. (setq w maxword)
  19048. (setq ll (org-do-wrap words maxword))
  19049. (if (<= (length ll) lines)
  19050. ll
  19051. (setq ll words)
  19052. (while (> (length ll) lines)
  19053. (setq w (1+ w))
  19054. (setq ll (org-do-wrap words w)))
  19055. ll))
  19056. (t (error "Cannot wrap this")))))
  19057. (defun org-do-wrap (words width)
  19058. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19059. (let (lines line)
  19060. (while words
  19061. (setq line (pop words))
  19062. (while (and words (< (+ (length line) (length (car words))) width))
  19063. (setq line (concat line " " (pop words))))
  19064. (setq lines (push line lines)))
  19065. (nreverse lines)))
  19066. (defun org-split-string (string &optional separators)
  19067. "Splits STRING into substrings at SEPARATORS.
  19068. No empty strings are returned if there are matches at the beginning
  19069. and end of string."
  19070. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19071. (start 0)
  19072. notfirst
  19073. (list nil))
  19074. (while (and (string-match rexp string
  19075. (if (and notfirst
  19076. (= start (match-beginning 0))
  19077. (< start (length string)))
  19078. (1+ start) start))
  19079. (< (match-beginning 0) (length string)))
  19080. (setq notfirst t)
  19081. (or (eq (match-beginning 0) 0)
  19082. (and (eq (match-beginning 0) (match-end 0))
  19083. (eq (match-beginning 0) start))
  19084. (setq list
  19085. (cons (substring string start (match-beginning 0))
  19086. list)))
  19087. (setq start (match-end 0)))
  19088. (or (eq start (length string))
  19089. (setq list
  19090. (cons (substring string start)
  19091. list)))
  19092. (nreverse list)))
  19093. (defun org-quote-vert (s)
  19094. "Replace \"|\" with \"\\vert\"."
  19095. (while (string-match "|" s)
  19096. (setq s (replace-match "\\vert" t t s)))
  19097. s)
  19098. (defun org-uuidgen-p (s)
  19099. "Is S an ID created by UUIDGEN?"
  19100. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19101. (defun org-in-src-block-p (&optional inside)
  19102. "Whether point is in a code source block.
  19103. When INSIDE is non-nil, don't consider we are within a src block
  19104. when point is at #+BEGIN_SRC or #+END_SRC."
  19105. (let ((case-fold-search t) ov)
  19106. (or (and (setq ov (overlays-at (point)))
  19107. (memq 'org-block-background
  19108. (overlay-properties (car ov))))
  19109. (and (not inside)
  19110. (save-match-data
  19111. (save-excursion
  19112. (beginning-of-line)
  19113. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19114. (defun org-context ()
  19115. "Return a list of contexts of the current cursor position.
  19116. If several contexts apply, all are returned.
  19117. Each context entry is a list with a symbol naming the context, and
  19118. two positions indicating start and end of the context. Possible
  19119. contexts are:
  19120. :headline anywhere in a headline
  19121. :headline-stars on the leading stars in a headline
  19122. :todo-keyword on a TODO keyword (including DONE) in a headline
  19123. :tags on the TAGS in a headline
  19124. :priority on the priority cookie in a headline
  19125. :item on the first line of a plain list item
  19126. :item-bullet on the bullet/number of a plain list item
  19127. :checkbox on the checkbox in a plain list item
  19128. :table in an org-mode table
  19129. :table-special on a special filed in a table
  19130. :table-table in a table.el table
  19131. :clocktable in a clocktable
  19132. :src-block in a source block
  19133. :link on a hyperlink
  19134. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19135. :target on a <<target>>
  19136. :radio-target on a <<<radio-target>>>
  19137. :latex-fragment on a LaTeX fragment
  19138. :latex-preview on a LaTeX fragment with overlaid preview image
  19139. This function expects the position to be visible because it uses font-lock
  19140. faces as a help to recognize the following contexts: :table-special, :link,
  19141. and :keyword."
  19142. (let* ((f (get-text-property (point) 'face))
  19143. (faces (if (listp f) f (list f)))
  19144. (case-fold-search t)
  19145. (p (point)) clist o)
  19146. ;; First the large context
  19147. (cond
  19148. ((org-at-heading-p t)
  19149. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19150. (when (progn
  19151. (beginning-of-line 1)
  19152. (looking-at org-todo-line-tags-regexp))
  19153. (push (org-point-in-group p 1 :headline-stars) clist)
  19154. (push (org-point-in-group p 2 :todo-keyword) clist)
  19155. (push (org-point-in-group p 4 :tags) clist))
  19156. (goto-char p)
  19157. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19158. (if (looking-at "\\[#[A-Z0-9]\\]")
  19159. (push (org-point-in-group p 0 :priority) clist)))
  19160. ((org-at-item-p)
  19161. (push (org-point-in-group p 2 :item-bullet) clist)
  19162. (push (list :item (point-at-bol)
  19163. (save-excursion (org-end-of-item) (point)))
  19164. clist)
  19165. (and (org-at-item-checkbox-p)
  19166. (push (org-point-in-group p 0 :checkbox) clist)))
  19167. ((org-at-table-p)
  19168. (push (list :table (org-table-begin) (org-table-end)) clist)
  19169. (if (memq 'org-formula faces)
  19170. (push (list :table-special
  19171. (previous-single-property-change p 'face)
  19172. (next-single-property-change p 'face)) clist)))
  19173. ((org-at-table-p 'any)
  19174. (push (list :table-table) clist)))
  19175. (goto-char p)
  19176. (let ((case-fold-search t))
  19177. ;; New the "medium" contexts: clocktables, source blocks
  19178. (cond ((org-in-clocktable-p)
  19179. (push (list :clocktable
  19180. (and (or (looking-at "#\\+BEGIN: clocktable")
  19181. (search-backward "#+BEGIN: clocktable" nil t))
  19182. (match-beginning 0))
  19183. (and (re-search-forward "#\\+END:?" nil t)
  19184. (match-end 0))) clist))
  19185. ((org-in-src-block-p)
  19186. (push (list :src-block
  19187. (and (or (looking-at "#\\+BEGIN_SRC")
  19188. (search-backward "#+BEGIN_SRC" nil t))
  19189. (match-beginning 0))
  19190. (and (search-forward "#+END_SRC" nil t)
  19191. (match-beginning 0))) clist))))
  19192. (goto-char p)
  19193. ;; Now the small context
  19194. (cond
  19195. ((org-at-timestamp-p)
  19196. (push (org-point-in-group p 0 :timestamp) clist))
  19197. ((memq 'org-link faces)
  19198. (push (list :link
  19199. (previous-single-property-change p 'face)
  19200. (next-single-property-change p 'face)) clist))
  19201. ((memq 'org-special-keyword faces)
  19202. (push (list :keyword
  19203. (previous-single-property-change p 'face)
  19204. (next-single-property-change p 'face)) clist))
  19205. ((org-at-target-p)
  19206. (push (org-point-in-group p 0 :target) clist)
  19207. (goto-char (1- (match-beginning 0)))
  19208. (if (looking-at org-radio-target-regexp)
  19209. (push (org-point-in-group p 0 :radio-target) clist))
  19210. (goto-char p))
  19211. ((setq o (car (delq nil
  19212. (mapcar
  19213. (lambda (x)
  19214. (if (memq x org-latex-fragment-image-overlays) x))
  19215. (overlays-at (point))))))
  19216. (push (list :latex-fragment
  19217. (overlay-start o) (overlay-end o)) clist)
  19218. (push (list :latex-preview
  19219. (overlay-start o) (overlay-end o)) clist))
  19220. ((org-inside-LaTeX-fragment-p)
  19221. ;; FIXME: positions wrong.
  19222. (push (list :latex-fragment (point) (point)) clist)))
  19223. (setq clist (nreverse (delq nil clist)))
  19224. clist))
  19225. ;; FIXME: Compare with at-regexp-p Do we need both?
  19226. (defun org-in-regexp (re &optional nlines visually)
  19227. "Check if point is inside a match of regexp.
  19228. Normally only the current line is checked, but you can include NLINES extra
  19229. lines both before and after point into the search.
  19230. If VISUALLY is set, require that the cursor is not after the match but
  19231. really on, so that the block visually is on the match."
  19232. (catch 'exit
  19233. (let ((pos (point))
  19234. (eol (point-at-eol (+ 1 (or nlines 0))))
  19235. (inc (if visually 1 0)))
  19236. (save-excursion
  19237. (beginning-of-line (- 1 (or nlines 0)))
  19238. (while (re-search-forward re eol t)
  19239. (if (and (<= (match-beginning 0) pos)
  19240. (>= (+ inc (match-end 0)) pos))
  19241. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19242. (defun org-at-regexp-p (regexp)
  19243. "Is point inside a match of REGEXP in the current line?"
  19244. (catch 'exit
  19245. (save-excursion
  19246. (let ((pos (point)) (end (point-at-eol)))
  19247. (beginning-of-line 1)
  19248. (while (re-search-forward regexp end t)
  19249. (if (and (<= (match-beginning 0) pos)
  19250. (>= (match-end 0) pos))
  19251. (throw 'exit t)))
  19252. nil))))
  19253. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19254. "Non-nil when point is between matches of START-RE and END-RE.
  19255. Also return a non-nil value when point is on one of the matches.
  19256. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19257. buffer positions. Default values are the positions of headlines
  19258. surrounding the point.
  19259. The functions returns a cons cell whose car (resp. cdr) is the
  19260. position before START-RE (resp. after END-RE)."
  19261. (save-match-data
  19262. (let ((pos (point))
  19263. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19264. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19265. beg end)
  19266. (save-excursion
  19267. ;; Point is on a block when on START-RE or if START-RE can be
  19268. ;; found before it...
  19269. (and (or (org-at-regexp-p start-re)
  19270. (re-search-backward start-re limit-up t))
  19271. (setq beg (match-beginning 0))
  19272. ;; ... and END-RE after it...
  19273. (goto-char (match-end 0))
  19274. (re-search-forward end-re limit-down t)
  19275. (> (setq end (match-end 0)) pos)
  19276. ;; ... without another START-RE in-between.
  19277. (goto-char (match-beginning 0))
  19278. (not (re-search-backward start-re (1+ beg) t))
  19279. ;; Return value.
  19280. (cons beg end))))))
  19281. (defun org-in-block-p (names)
  19282. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19283. NAMES is a list of strings containing names of blocks.
  19284. Return first block name matched, or nil. Beware that in case of
  19285. nested blocks, the returned name may not belong to the closest
  19286. block from point."
  19287. (save-match-data
  19288. (catch 'exit
  19289. (let ((case-fold-search t)
  19290. (lim-up (save-excursion (outline-previous-heading)))
  19291. (lim-down (save-excursion (outline-next-heading))))
  19292. (mapc (lambda (name)
  19293. (let ((n (regexp-quote name)))
  19294. (when (org-between-regexps-p
  19295. (concat "^[ \t]*#\\+begin_" n)
  19296. (concat "^[ \t]*#\\+end_" n)
  19297. lim-up lim-down)
  19298. (throw 'exit n))))
  19299. names))
  19300. nil)))
  19301. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19302. "Call `multi-occur' with buffers for all agenda files."
  19303. (interactive "sOrg-files matching: \np")
  19304. (let* ((files (org-agenda-files))
  19305. (tnames (mapcar 'file-truename files))
  19306. (extra org-agenda-text-search-extra-files)
  19307. f)
  19308. (when (eq (car extra) 'agenda-archives)
  19309. (setq extra (cdr extra))
  19310. (setq files (org-add-archive-files files)))
  19311. (while (setq f (pop extra))
  19312. (unless (member (file-truename f) tnames)
  19313. (add-to-list 'files f 'append)
  19314. (add-to-list 'tnames (file-truename f) 'append)))
  19315. (multi-occur
  19316. (mapcar (lambda (x)
  19317. (with-current-buffer
  19318. (or (get-file-buffer x) (find-file-noselect x))
  19319. (widen)
  19320. (current-buffer)))
  19321. files)
  19322. regexp)))
  19323. (if (boundp 'occur-mode-find-occurrence-hook)
  19324. ;; Emacs 23
  19325. (add-hook 'occur-mode-find-occurrence-hook
  19326. (lambda ()
  19327. (when (derived-mode-p 'org-mode)
  19328. (org-reveal))))
  19329. ;; Emacs 22
  19330. (defadvice occur-mode-goto-occurrence
  19331. (after org-occur-reveal activate)
  19332. (and (derived-mode-p 'org-mode) (org-reveal)))
  19333. (defadvice occur-mode-goto-occurrence-other-window
  19334. (after org-occur-reveal activate)
  19335. (and (derived-mode-p 'org-mode) (org-reveal)))
  19336. (defadvice occur-mode-display-occurrence
  19337. (after org-occur-reveal activate)
  19338. (when (derived-mode-p 'org-mode)
  19339. (let ((pos (occur-mode-find-occurrence)))
  19340. (with-current-buffer (marker-buffer pos)
  19341. (save-excursion
  19342. (goto-char pos)
  19343. (org-reveal)))))))
  19344. (defun org-occur-link-in-agenda-files ()
  19345. "Create a link and search for it in the agendas.
  19346. The link is not stored in `org-stored-links', it is just created
  19347. for the search purpose."
  19348. (interactive)
  19349. (let ((link (condition-case nil
  19350. (org-store-link nil)
  19351. (error "Unable to create a link to here"))))
  19352. (org-occur-in-agenda-files (regexp-quote link))))
  19353. (defun org-uniquify (list)
  19354. "Remove duplicate elements from LIST."
  19355. (let (res)
  19356. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19357. res))
  19358. (defun org-delete-all (elts list)
  19359. "Remove all elements in ELTS from LIST."
  19360. (while elts
  19361. (setq list (delete (pop elts) list)))
  19362. list)
  19363. (defun org-count (cl-item cl-seq)
  19364. "Count the number of occurrences of ITEM in SEQ.
  19365. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19366. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19367. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19368. (while (< cl-start cl-end)
  19369. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19370. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19371. (setq cl-start (1+ cl-start)))
  19372. cl-count))
  19373. (defun org-remove-if (predicate seq)
  19374. "Remove everything from SEQ that fulfills PREDICATE."
  19375. (let (res e)
  19376. (while seq
  19377. (setq e (pop seq))
  19378. (if (not (funcall predicate e)) (push e res)))
  19379. (nreverse res)))
  19380. (defun org-remove-if-not (predicate seq)
  19381. "Remove everything from SEQ that does not fulfill PREDICATE."
  19382. (let (res e)
  19383. (while seq
  19384. (setq e (pop seq))
  19385. (if (funcall predicate e) (push e res)))
  19386. (nreverse res)))
  19387. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19388. "Reduce two-argument FUNCTION across SEQ.
  19389. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19390. \":initial-value\" removed."
  19391. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19392. (cadr (memq :initial-value cl-keys)))
  19393. (cl-seq (pop cl-seq))
  19394. (t (funcall cl-func)))))
  19395. (while cl-seq
  19396. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19397. cl-accum))
  19398. (defun org-back-over-empty-lines ()
  19399. "Move backwards over whitespace, to the beginning of the first empty line.
  19400. Returns the number of empty lines passed."
  19401. (let ((pos (point)))
  19402. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19403. (skip-chars-backward " \t\n\r")
  19404. (unless (eobp)
  19405. (forward-line -1)))
  19406. (beginning-of-line 2)
  19407. (goto-char (min (point) pos))
  19408. (count-lines (point) pos)))
  19409. (defun org-skip-whitespace ()
  19410. (skip-chars-forward " \t\n\r"))
  19411. (defun org-point-in-group (point group &optional context)
  19412. "Check if POINT is in match-group GROUP.
  19413. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19414. match. If the match group does not exist or point is not inside it,
  19415. return nil."
  19416. (and (match-beginning group)
  19417. (>= point (match-beginning group))
  19418. (<= point (match-end group))
  19419. (if context
  19420. (list context (match-beginning group) (match-end group))
  19421. t)))
  19422. (defun org-switch-to-buffer-other-window (&rest args)
  19423. "Switch to buffer in a second window on the current frame.
  19424. In particular, do not allow pop-up frames.
  19425. Returns the newly created buffer."
  19426. (org-no-popups
  19427. (apply 'switch-to-buffer-other-window args)))
  19428. (defun org-combine-plists (&rest plists)
  19429. "Create a single property list from all plists in PLISTS.
  19430. The process starts by copying the first list, and then setting properties
  19431. from the other lists. Settings in the last list are the most significant
  19432. ones and overrule settings in the other lists."
  19433. (let ((rtn (copy-sequence (pop plists)))
  19434. p v ls)
  19435. (while plists
  19436. (setq ls (pop plists))
  19437. (while ls
  19438. (setq p (pop ls) v (pop ls))
  19439. (setq rtn (plist-put rtn p v))))
  19440. rtn))
  19441. (defun org-replace-escapes (string table)
  19442. "Replace %-escapes in STRING with values in TABLE.
  19443. TABLE is an association list with keys like \"%a\" and string values.
  19444. The sequences in STRING may contain normal field width and padding information,
  19445. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19446. so values can contain further %-escapes if they are define later in TABLE."
  19447. (let ((tbl (copy-alist table))
  19448. (case-fold-search nil)
  19449. (pchg 0)
  19450. e re rpl)
  19451. (while (setq e (pop tbl))
  19452. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19453. (when (and (cdr e) (string-match re (cdr e)))
  19454. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19455. (safe "SREF"))
  19456. (add-text-properties 0 3 (list 'sref sref) safe)
  19457. (setcdr e (replace-match safe t t (cdr e)))))
  19458. (while (string-match re string)
  19459. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19460. (cdr e)))
  19461. (setq string (replace-match rpl t t string))))
  19462. (while (setq pchg (next-property-change pchg string))
  19463. (let ((sref (get-text-property pchg 'sref string)))
  19464. (when (and sref (string-match "SREF" string pchg))
  19465. (setq string (replace-match sref t t string)))))
  19466. string))
  19467. (defun org-sublist (list start end)
  19468. "Return a section of LIST, from START to END.
  19469. Counting starts at 1."
  19470. (let (rtn (c start))
  19471. (setq list (nthcdr (1- start) list))
  19472. (while (and list (<= c end))
  19473. (push (pop list) rtn)
  19474. (setq c (1+ c)))
  19475. (nreverse rtn)))
  19476. (defun org-find-base-buffer-visiting (file)
  19477. "Like `find-buffer-visiting' but always return the base buffer and
  19478. not an indirect buffer."
  19479. (let ((buf (or (get-file-buffer file)
  19480. (find-buffer-visiting file))))
  19481. (if buf
  19482. (or (buffer-base-buffer buf) buf)
  19483. nil)))
  19484. (defun org-image-file-name-regexp (&optional extensions)
  19485. "Return regexp matching the file names of images.
  19486. If EXTENSIONS is given, only match these."
  19487. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19488. (image-file-name-regexp)
  19489. (let ((image-file-name-extensions
  19490. (or extensions
  19491. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19492. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19493. (concat "\\."
  19494. (regexp-opt (nconc (mapcar 'upcase
  19495. image-file-name-extensions)
  19496. image-file-name-extensions)
  19497. t)
  19498. "\\'"))))
  19499. (defun org-file-image-p (file &optional extensions)
  19500. "Return non-nil if FILE is an image."
  19501. (save-match-data
  19502. (string-match (org-image-file-name-regexp extensions) file)))
  19503. (defun org-get-cursor-date (&optional with-time)
  19504. "Return the date at cursor in as a time.
  19505. This works in the calendar and in the agenda, anywhere else it just
  19506. returns the current time.
  19507. If WITH-TIME is non-nil, returns the time of the event at point (in
  19508. the agenda) or the current time of the day."
  19509. (let (date day defd tp tm hod mod)
  19510. (when with-time
  19511. (setq tp (get-text-property (point) 'time))
  19512. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  19513. (setq hod (string-to-number (match-string 1 tp))
  19514. mod (string-to-number (match-string 2 tp))))
  19515. (or tp (setq hod (nth 2 (decode-time (current-time)))
  19516. mod (nth 1 (decode-time (current-time))))))
  19517. (cond
  19518. ((eq major-mode 'calendar-mode)
  19519. (setq date (calendar-cursor-to-date)
  19520. defd (encode-time 0 (or mod 0) (or hod 0)
  19521. (nth 1 date) (nth 0 date) (nth 2 date))))
  19522. ((eq major-mode 'org-agenda-mode)
  19523. (setq day (get-text-property (point) 'day))
  19524. (if day
  19525. (setq date (calendar-gregorian-from-absolute day)
  19526. defd (encode-time 0 (or mod 0) (or hod 0)
  19527. (nth 1 date) (nth 0 date) (nth 2 date))))))
  19528. (or defd (current-time))))
  19529. (defun org-mark-subtree (&optional up)
  19530. "Mark the current subtree.
  19531. This puts point at the start of the current subtree, and mark at
  19532. the end. If a numeric prefix UP is given, move up into the
  19533. hierarchy of headlines by UP levels before marking the subtree."
  19534. (interactive "P")
  19535. (org-with-limited-levels
  19536. (cond ((org-at-heading-p) (beginning-of-line))
  19537. ((org-before-first-heading-p) (error "Not in a subtree"))
  19538. (t (outline-previous-visible-heading 1))))
  19539. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19540. (if (org-called-interactively-p 'any)
  19541. (call-interactively 'org-mark-element)
  19542. (org-mark-element)))
  19543. ;;; Macros
  19544. ;; Macros are expanded with `org-macro-replace-all', which relies
  19545. ;; internally on `org-macro-expand'.
  19546. ;; Default templates for expansion are stored in the buffer-local
  19547. ;; variable `org-macro-templates'. This variable is updated by
  19548. ;; `org-macro-initialize-templates'.
  19549. ;; Along with macros defined through #+MACRO: keyword, default
  19550. ;; templates include the following hard-coded macros:
  19551. ;; {{{time(format-string)}}}, {{{property(node-property)}}},
  19552. ;; {{{input-file}}} and {{{modification-time(format-string)}}}.
  19553. ;; During export, {{{author}}}, {{{date}}}, {{{email}}} and
  19554. ;; {{{title}}} will also be provided.
  19555. (defvar org-macro-templates nil
  19556. "Alist containing all macro templates in current buffer.
  19557. Associations are in the shape of (NAME . TEMPLATE) where NAME
  19558. stands for macro's name and template for its replacement value,
  19559. both as strings. This is an internal variable. Do not set it
  19560. directly, use instead:
  19561. #+MACRO: name template")
  19562. (make-variable-buffer-local 'org-macro-templates)
  19563. (defun org-macro-expand (macro templates)
  19564. "Return expanded MACRO, as a string.
  19565. MACRO is an object, obtained, for example, with
  19566. `org-element-context'. TEMPLATES is an alist of templates used
  19567. for expansion. See `org-macro-templates' for a buffer-local
  19568. default value. Return nil if no template was found."
  19569. (let ((template
  19570. ;; Macro names are case-insensitive.
  19571. (cdr (assoc-string (org-element-property :key macro) templates t))))
  19572. (when template
  19573. (let ((value (replace-regexp-in-string
  19574. "\\$[0-9]+"
  19575. (lambda (arg)
  19576. (or (nth (1- (string-to-number (substring arg 1)))
  19577. (org-element-property :args macro))
  19578. ;; No argument provided: remove
  19579. ;; place-holder.
  19580. ""))
  19581. template)))
  19582. ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
  19583. (when (string-match "\\`(eval\\>" value)
  19584. (setq value (eval (read value))))
  19585. ;; Return string.
  19586. (format "%s" (or value ""))))))
  19587. (defun org-macro-replace-all (templates)
  19588. "Replace all macros in current buffer by their expansion.
  19589. TEMPLATES is an alist of templates used for expansion. See
  19590. `org-macro-templates' for a buffer-local default value."
  19591. (save-excursion
  19592. (goto-char (point-min))
  19593. (let (record)
  19594. (while (re-search-forward "{{{[-A-Za-z0-9_]" nil t)
  19595. (let ((object (org-element-context)))
  19596. (when (eq (org-element-type object) 'macro)
  19597. (let* ((value (org-macro-expand object templates))
  19598. (begin (org-element-property :begin object))
  19599. (signature (list begin
  19600. object
  19601. (org-element-property :args object))))
  19602. ;; Avoid circular dependencies by checking if the same
  19603. ;; macro with the same arguments is expanded at the same
  19604. ;; position twice.
  19605. (if (member signature record)
  19606. (error "Circular macro expansion: %s"
  19607. (org-element-property :key object))
  19608. (when value
  19609. (push signature record)
  19610. (delete-region
  19611. begin
  19612. ;; Preserve white spaces after the macro.
  19613. (progn (goto-char (org-element-property :end object))
  19614. (skip-chars-backward " \t")
  19615. (point)))
  19616. ;; Leave point before replacement in case of recursive
  19617. ;; expansions.
  19618. (save-excursion (insert value)))))))))))
  19619. (defun org-macro-initialize-templates ()
  19620. "Collect macro templates defined in current buffer.
  19621. Templates are stored in buffer-local variable
  19622. `org-macro-templates'. In addition to buffer-defined macros, the
  19623. function installs the following ones: \"property\",
  19624. \"time\". and, if the buffer is associated to a file,
  19625. \"input-file\" and \"modification-time\"."
  19626. (let ((case-fold-search t)
  19627. (set-template
  19628. (lambda (cell)
  19629. ;; Add CELL to `org-macro-templates' if there's no
  19630. ;; association matching its name already. Otherwise,
  19631. ;; replace old association with the new one in that
  19632. ;; variable.
  19633. (let ((old-template (assoc (car cell) org-macro-templates)))
  19634. (if old-template (setcdr old-template (cdr cell))
  19635. (push cell org-macro-templates))))))
  19636. ;; Install buffer-local macros.
  19637. (org-with-wide-buffer
  19638. (goto-char (point-min))
  19639. (while (re-search-forward "^[ \t]*#\\+MACRO:" nil t)
  19640. (let ((element (org-element-at-point)))
  19641. (when (eq (org-element-type element) 'keyword)
  19642. (let ((value (org-element-property :value element)))
  19643. (when (string-match "^\\(.*?\\)\\(?:\\s-+\\(.*\\)\\)?\\s-*$" value)
  19644. (funcall set-template
  19645. (cons (match-string 1 value)
  19646. (or (match-string 2 value) "")))))))))
  19647. ;; Install hard-coded macros.
  19648. (mapc (lambda (cell) (funcall set-template cell))
  19649. (list
  19650. (cons "property" "(eval (org-entry-get nil \"$1\" 'selective))")
  19651. (cons "time" "(eval (format-time-string \"$1\"))")))
  19652. (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  19653. (when (and visited-file (file-exists-p visited-file))
  19654. (mapc (lambda (cell) (funcall set-template cell))
  19655. (list
  19656. (cons "input-file" (file-name-nondirectory visited-file))
  19657. (cons "modification-time"
  19658. (format "(eval (format-time-string \"$1\" '%s))"
  19659. (prin1-to-string
  19660. (nth 5 (file-attributes visited-file)))))))))))
  19661. ;;; Indentation
  19662. (defun org-indent-line ()
  19663. "Indent line depending on context."
  19664. (interactive)
  19665. (let* ((pos (point))
  19666. (itemp (org-at-item-p))
  19667. (case-fold-search t)
  19668. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19669. (inline-task-p (and (featurep 'org-inlinetask)
  19670. (org-inlinetask-in-task-p)))
  19671. (inline-re (and inline-task-p
  19672. (org-inlinetask-outline-regexp)))
  19673. column)
  19674. (if (and orgstruct-is-++ (eq pos (point)))
  19675. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19676. (indent-according-to-mode))
  19677. (beginning-of-line 1)
  19678. (cond
  19679. ;; Headings
  19680. ((looking-at org-outline-regexp) (setq column 0))
  19681. ;; Included files
  19682. ((looking-at "#\\+include:") (setq column 0))
  19683. ;; Footnote definition
  19684. ((looking-at org-footnote-definition-re) (setq column 0))
  19685. ;; Literal examples
  19686. ((looking-at "[ \t]*:\\( \\|$\\)")
  19687. (setq column (org-get-indentation))) ; do nothing
  19688. ;; Lists
  19689. ((ignore-errors (goto-char (org-in-item-p)))
  19690. (setq column (if itemp
  19691. (org-get-indentation)
  19692. (org-list-item-body-column (point))))
  19693. (goto-char pos))
  19694. ;; Drawers
  19695. ((and (looking-at "[ \t]*:END:")
  19696. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19697. (save-excursion
  19698. (goto-char (1- (match-beginning 1)))
  19699. (setq column (current-column))))
  19700. ;; Special blocks
  19701. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19702. (save-excursion
  19703. (re-search-backward
  19704. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19705. (setq column (org-get-indentation (match-string 0))))
  19706. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19707. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19708. (save-excursion
  19709. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19710. (setq column
  19711. (cond ((equal (downcase (match-string 1)) "src")
  19712. ;; src blocks: let `org-edit-src-exit' handle them
  19713. (org-get-indentation))
  19714. ((equal (downcase (match-string 1)) "example")
  19715. (max (org-get-indentation)
  19716. (org-get-indentation (match-string 0))))
  19717. (t
  19718. (org-get-indentation (match-string 0))))))
  19719. ;; This line has nothing special, look at the previous relevant
  19720. ;; line to compute indentation
  19721. (t
  19722. (beginning-of-line 0)
  19723. (while (and (not (bobp))
  19724. (not (looking-at org-table-line-regexp))
  19725. (not (looking-at org-drawer-regexp))
  19726. ;; When point started in an inline task, do not move
  19727. ;; above task starting line.
  19728. (not (and inline-task-p (looking-at inline-re)))
  19729. ;; Skip drawers, blocks, empty lines, verbatim,
  19730. ;; comments, tables, footnotes definitions, lists,
  19731. ;; inline tasks.
  19732. (or (and (looking-at "[ \t]*:END:")
  19733. (re-search-backward org-drawer-regexp nil t))
  19734. (and (looking-at "[ \t]*#\\+end_")
  19735. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19736. (looking-at "[ \t]*[\n:#|]")
  19737. (looking-at org-footnote-definition-re)
  19738. (and (ignore-errors (goto-char (org-in-item-p)))
  19739. (goto-char
  19740. (org-list-get-top-point (org-list-struct))))
  19741. (and (not inline-task-p)
  19742. (featurep 'org-inlinetask)
  19743. (org-inlinetask-in-task-p)
  19744. (or (org-inlinetask-goto-beginning) t))))
  19745. (beginning-of-line 0))
  19746. (cond
  19747. ;; There was an heading above.
  19748. ((looking-at "\\*+[ \t]+")
  19749. (if (not org-adapt-indentation)
  19750. (setq column 0)
  19751. (goto-char (match-end 0))
  19752. (setq column (current-column))))
  19753. ;; A drawer had started and is unfinished
  19754. ((looking-at org-drawer-regexp)
  19755. (goto-char (1- (match-beginning 1)))
  19756. (setq column (current-column)))
  19757. ;; Else, nothing noticeable found: get indentation and go on.
  19758. (t (setq column (org-get-indentation))))))
  19759. ;; Now apply indentation and move cursor accordingly
  19760. (goto-char pos)
  19761. (if (<= (current-column) (current-indentation))
  19762. (org-indent-line-to column)
  19763. (save-excursion (org-indent-line-to column)))
  19764. ;; Special polishing for properties, see `org-property-format'
  19765. (setq column (current-column))
  19766. (beginning-of-line 1)
  19767. (if (looking-at
  19768. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19769. (replace-match (concat (match-string 1)
  19770. (format org-property-format
  19771. (match-string 2) (match-string 3)))
  19772. t t))
  19773. (org-move-to-column column))))
  19774. (defun org-indent-drawer ()
  19775. "Indent the drawer at point."
  19776. (interactive)
  19777. (let ((p (point))
  19778. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19779. (match-end 0)))
  19780. (folded
  19781. (save-excursion
  19782. (end-of-line)
  19783. (when (overlays-at (point))
  19784. (member 'invisible (overlay-properties
  19785. (car (overlays-at (point)))))))))
  19786. (when folded (org-cycle))
  19787. (indent-for-tab-command)
  19788. (while (and (move-beginning-of-line 2) (< (point) e))
  19789. (indent-for-tab-command))
  19790. (goto-char p)
  19791. (when folded (org-cycle)))
  19792. (message "Drawer at point indented"))
  19793. (defun org-indent-block ()
  19794. "Indent the block at point."
  19795. (interactive)
  19796. (let ((p (point))
  19797. (case-fold-search t)
  19798. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19799. (match-end 0)))
  19800. (folded
  19801. (save-excursion
  19802. (end-of-line)
  19803. (when (overlays-at (point))
  19804. (member 'invisible (overlay-properties
  19805. (car (overlays-at (point)))))))))
  19806. (when folded (org-cycle))
  19807. (indent-for-tab-command)
  19808. (while (and (move-beginning-of-line 2) (< (point) e))
  19809. (indent-for-tab-command))
  19810. (goto-char p)
  19811. (when folded (org-cycle)))
  19812. (message "Block at point indented"))
  19813. (defun org-indent-region (start end)
  19814. "Indent region."
  19815. (interactive "r")
  19816. (save-excursion
  19817. (let ((line-end (org-current-line end)))
  19818. (goto-char start)
  19819. (while (< (org-current-line) line-end)
  19820. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19821. (t (call-interactively 'org-indent-line)))
  19822. (move-beginning-of-line 2)))))
  19823. ;;; Filling
  19824. ;; We use our own fill-paragraph and auto-fill functions.
  19825. ;; `org-fill-paragraph' relies on adaptive filling and context
  19826. ;; checking. Appropriate `fill-prefix' is computed with
  19827. ;; `org-adaptive-fill-function'.
  19828. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19829. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19830. ;; `org-adaptive-fill-function' value. Internally,
  19831. ;; `org-comment-line-break-function' breaks the line.
  19832. ;; `org-setup-filling' installs filling and auto-filling related
  19833. ;; variables during `org-mode' initialization.
  19834. (defun org-setup-filling ()
  19835. (interactive)
  19836. ;; Prevent auto-fill from inserting unwanted new items.
  19837. (when (boundp 'fill-nobreak-predicate)
  19838. (org-set-local
  19839. 'fill-nobreak-predicate
  19840. (org-uniquify
  19841. (append fill-nobreak-predicate
  19842. '(org-fill-paragraph-separate-nobreak-p
  19843. org-fill-line-break-nobreak-p)))))
  19844. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19845. (org-set-local 'auto-fill-inhibit-regexp nil)
  19846. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19847. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19848. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19849. (defvar org-element-paragraph-separate) ; org-element.el
  19850. (defun org-fill-paragraph-separate-nobreak-p ()
  19851. "Non-nil when a line break at point would insert a new item."
  19852. (looking-at (substring org-element-paragraph-separate 1)))
  19853. (defun org-fill-line-break-nobreak-p ()
  19854. "Non-nil when a line break at point would create an Org line break."
  19855. (save-excursion
  19856. (skip-chars-backward "[ \t]")
  19857. (skip-chars-backward "\\\\")
  19858. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19859. (declare-function message-in-body-p "message" ())
  19860. (defun org-adaptive-fill-function ()
  19861. "Compute a fill prefix for the current line.
  19862. Return fill prefix, as a string, or nil if current line isn't
  19863. meant to be filled."
  19864. (let (prefix)
  19865. (catch 'exit
  19866. (when (derived-mode-p 'message-mode)
  19867. (save-excursion
  19868. (beginning-of-line)
  19869. (cond ((or (not (message-in-body-p))
  19870. (looking-at orgtbl-line-start-regexp))
  19871. (throw 'exit nil))
  19872. ((looking-at message-cite-prefix-regexp)
  19873. (throw 'exit (match-string-no-properties 0)))
  19874. ((looking-at org-outline-regexp)
  19875. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  19876. (org-with-wide-buffer
  19877. (let* ((p (line-beginning-position))
  19878. (element (save-excursion (beginning-of-line)
  19879. (org-element-at-point)))
  19880. (type (org-element-type element))
  19881. (post-affiliated (org-element-property :post-affiliated element)))
  19882. (unless (and post-affiliated (< p post-affiliated))
  19883. (case type
  19884. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19885. (footnote-definition "")
  19886. ((item plain-list)
  19887. (make-string (org-list-item-body-column
  19888. (or post-affiliated
  19889. (org-element-property :begin element)))
  19890. ? ))
  19891. (paragraph
  19892. ;; Fill prefix is usually the same as the current line,
  19893. ;; except if the paragraph is at the beginning of an item.
  19894. (let ((parent (org-element-property :parent element)))
  19895. (cond ((eq (org-element-type parent) 'item)
  19896. (make-string (org-list-item-body-column
  19897. (org-element-property :begin parent))
  19898. ? ))
  19899. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19900. (match-string 0))
  19901. (t ""))))
  19902. (comment-block
  19903. ;; Only fill contents if P is within block boundaries.
  19904. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  19905. (forward-line)
  19906. (point)))
  19907. (cend (save-excursion
  19908. (goto-char (org-element-property :end element))
  19909. (skip-chars-backward " \r\t\n")
  19910. (line-beginning-position))))
  19911. (when (and (>= p cbeg) (< p cend))
  19912. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19913. (match-string 0)
  19914. "")))))))))))
  19915. (declare-function message-goto-body "message" ())
  19916. (defvar message-cite-prefix-regexp) ; From message.el
  19917. (defvar org-element-all-objects) ; From org-element.el
  19918. (defun org-fill-paragraph (&optional justify)
  19919. "Fill element at point, when applicable.
  19920. This function only applies to comment blocks, comments, example
  19921. blocks and paragraphs. Also, as a special case, re-align table
  19922. when point is at one.
  19923. If JUSTIFY is non-nil (interactively, with prefix argument),
  19924. justify as well. If `sentence-end-double-space' is non-nil, then
  19925. period followed by one space does not end a sentence, so don't
  19926. break a line there. The variable `fill-column' controls the
  19927. width for filling.
  19928. For convenience, when point is at a plain list, an item or
  19929. a footnote definition, try to fill the first paragraph within."
  19930. (interactive)
  19931. (if (and (derived-mode-p 'message-mode)
  19932. (or (not (message-in-body-p))
  19933. (save-excursion (move-beginning-of-line 1)
  19934. (looking-at message-cite-prefix-regexp))))
  19935. ;; First ensure filling is correct in message-mode.
  19936. (let ((fill-paragraph-function
  19937. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  19938. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  19939. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  19940. (paragraph-separate
  19941. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  19942. (fill-paragraph nil))
  19943. (save-excursion
  19944. ;; Move to end of line in order to get the first paragraph
  19945. ;; within a plain list or a footnote definition.
  19946. (end-of-line)
  19947. (let ((element (org-element-at-point)))
  19948. ;; First check if point is in a blank line at the beginning of
  19949. ;; the buffer. In that case, ignore filling.
  19950. (if (< (point) (org-element-property :begin element)) t
  19951. (case (org-element-type element)
  19952. ;; Use major mode filling function is src blocks.
  19953. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  19954. ;; Align Org tables, leave table.el tables as-is.
  19955. (table-row (org-table-align) t)
  19956. (table
  19957. (when (eq (org-element-property :type element) 'org)
  19958. (org-table-align))
  19959. t)
  19960. (paragraph
  19961. ;; Paragraphs may contain `line-break' type objects.
  19962. (let ((beg (max (point-min)
  19963. (org-element-property :contents-begin element)))
  19964. (end (min (point-max)
  19965. (org-element-property :contents-end element))))
  19966. ;; Do nothing if point is at an affiliated keyword.
  19967. (if (< (point) beg) t
  19968. (when (derived-mode-p 'message-mode)
  19969. ;; In `message-mode', do not fill following
  19970. ;; citation in current paragraph nor text before
  19971. ;; message body.
  19972. (let ((body-start (save-excursion (message-goto-body))))
  19973. (when body-start (setq beg (max body-start beg))))
  19974. (when (save-excursion
  19975. (re-search-forward
  19976. (concat "^" message-cite-prefix-regexp) end t))
  19977. (setq end (match-beginning 0))))
  19978. ;; Fill paragraph, taking line breaks into
  19979. ;; consideration. For that, slice the paragraph
  19980. ;; using line breaks as separators, and fill the
  19981. ;; parts in reverse order to avoid messing with
  19982. ;; markers.
  19983. (save-excursion
  19984. (goto-char end)
  19985. (mapc
  19986. (lambda (pos)
  19987. (fill-region-as-paragraph pos (point) justify)
  19988. (goto-char pos))
  19989. ;; Find the list of ending positions for line
  19990. ;; breaks in the current paragraph. Add paragraph
  19991. ;; beginning to include first slice.
  19992. (nreverse
  19993. (cons
  19994. beg
  19995. (org-element-map
  19996. (org-element--parse-objects
  19997. beg end nil org-element-all-objects)
  19998. 'line-break
  19999. (lambda (lb) (org-element-property :end lb)))))))
  20000. t)))
  20001. ;; Contents of `comment-block' type elements should be
  20002. ;; filled as plain text, but only if point is within block
  20003. ;; markers.
  20004. (comment-block
  20005. (let* ((case-fold-search t)
  20006. (beg (save-excursion
  20007. (goto-char (org-element-property :begin element))
  20008. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20009. (forward-line)
  20010. (point)))
  20011. (end (save-excursion
  20012. (goto-char (org-element-property :end element))
  20013. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20014. (line-beginning-position))))
  20015. (when (and (>= (point) beg) (< (point) end))
  20016. (fill-region-as-paragraph
  20017. (save-excursion
  20018. (end-of-line)
  20019. (re-search-backward "^[ \t]*$" beg 'move)
  20020. (line-beginning-position))
  20021. (save-excursion
  20022. (beginning-of-line)
  20023. (re-search-forward "^[ \t]*$" end 'move)
  20024. (line-beginning-position))
  20025. justify)))
  20026. t)
  20027. ;; Fill comments.
  20028. (comment (fill-comment-paragraph justify))
  20029. ;; Ignore every other element.
  20030. (otherwise t)))))))
  20031. (defun org-auto-fill-function ()
  20032. "Auto-fill function."
  20033. ;; Check if auto-filling is meaningful.
  20034. (let ((fc (current-fill-column)))
  20035. (when (and fc (> (current-column) fc))
  20036. (let* ((fill-prefix (org-adaptive-fill-function))
  20037. ;; Enforce empty fill prefix, if required. Otherwise, it
  20038. ;; will be computed again.
  20039. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20040. (when fill-prefix (do-auto-fill))))))
  20041. (defun org-comment-line-break-function (&optional soft)
  20042. "Break line at point and indent, continuing comment if within one.
  20043. The inserted newline is marked hard if variable
  20044. `use-hard-newlines' is true, unless optional argument SOFT is
  20045. non-nil."
  20046. (if soft (insert-and-inherit ?\n) (newline 1))
  20047. (save-excursion (forward-char -1) (delete-horizontal-space))
  20048. (delete-horizontal-space)
  20049. (indent-to-left-margin)
  20050. (insert-before-markers-and-inherit fill-prefix))
  20051. ;;; Comments
  20052. ;; Org comments syntax is quite complex. It requires the entire line
  20053. ;; to be just a comment. Also, even with the right syntax at the
  20054. ;; beginning of line, some some elements (i.e. verse-block or
  20055. ;; example-block) don't accept comments. Usual Emacs comment commands
  20056. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20057. ;; Org still relies on `comment-dwim', but cannot trust
  20058. ;; `comment-only-p'. So, `comment-region-function' and
  20059. ;; `uncomment-region-function' both point
  20060. ;; to`org-comment-or-uncomment-region'. Eventually,
  20061. ;; `org-insert-comment' takes care of insertion of comments at the
  20062. ;; beginning of line.
  20063. ;; `org-setup-comments-handling' install comments related variables
  20064. ;; during `org-mode' initialization.
  20065. (defun org-setup-comments-handling ()
  20066. (interactive)
  20067. (org-set-local 'comment-use-syntax nil)
  20068. (org-set-local 'comment-start "# ")
  20069. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20070. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20071. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20072. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20073. (defun org-insert-comment ()
  20074. "Insert an empty comment above current line.
  20075. If the line is empty, insert comment at its beginning."
  20076. (beginning-of-line)
  20077. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20078. (org-indent-line)
  20079. (insert "# "))
  20080. (defvar comment-empty-lines) ; From newcomment.el.
  20081. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20082. "Comment or uncomment each non-blank line in the region.
  20083. Uncomment each non-blank line between BEG and END if it only
  20084. contains commented lines. Otherwise, comment them."
  20085. (save-restriction
  20086. ;; Restrict region
  20087. (narrow-to-region (save-excursion (goto-char beg)
  20088. (skip-chars-forward " \r\t\n" end)
  20089. (line-beginning-position))
  20090. (save-excursion (goto-char end)
  20091. (skip-chars-backward " \r\t\n" beg)
  20092. (line-end-position)))
  20093. (let ((uncommentp
  20094. ;; UNCOMMENTP is non-nil when every non blank line between
  20095. ;; BEG and END is a comment.
  20096. (save-excursion
  20097. (goto-char (point-min))
  20098. (while (and (not (eobp))
  20099. (let ((element (org-element-at-point)))
  20100. (and (eq (org-element-type element) 'comment)
  20101. (goto-char (min (point-max)
  20102. (org-element-property
  20103. :end element)))))))
  20104. (eobp))))
  20105. (if uncommentp
  20106. ;; Only blank lines and comments in region: uncomment it.
  20107. (save-excursion
  20108. (goto-char (point-min))
  20109. (while (not (eobp))
  20110. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20111. (replace-match "" nil nil nil 1))
  20112. (forward-line)))
  20113. ;; Comment each line in region.
  20114. (let ((min-indent (point-max)))
  20115. ;; First find the minimum indentation across all lines.
  20116. (save-excursion
  20117. (goto-char (point-min))
  20118. (while (and (not (eobp)) (not (zerop min-indent)))
  20119. (unless (looking-at "[ \t]*$")
  20120. (setq min-indent (min min-indent (current-indentation))))
  20121. (forward-line)))
  20122. ;; Then loop over all lines.
  20123. (save-excursion
  20124. (goto-char (point-min))
  20125. (while (not (eobp))
  20126. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20127. (org-move-to-column min-indent t)
  20128. (insert comment-start))
  20129. (forward-line))))))))
  20130. ;;; Planning
  20131. ;; This section contains tools to operate on timestamp objects, as
  20132. ;; returned by, e.g. `org-element-context'.
  20133. (defun org-timestamp-has-time-p (timestamp)
  20134. "Non-nil when TIMESTAMP has a time specified."
  20135. (org-element-property :hour-start timestamp))
  20136. (defun org-timestamp-format (timestamp format &optional end utc)
  20137. "Format a TIMESTAMP element into a string.
  20138. FORMAT is a format specifier to be passed to
  20139. `format-time-string'.
  20140. When optional argument END is non-nil, use end of date-range or
  20141. time-range, if possible.
  20142. When optional argument UTC is non-nil, time will be expressed as
  20143. Universal Time."
  20144. (format-time-string
  20145. format
  20146. (apply 'encode-time
  20147. (cons 0
  20148. (mapcar
  20149. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20150. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20151. '(:minute-start :hour-start :day-start :month-start
  20152. :year-start)))))
  20153. utc))
  20154. (defun org-timestamp-split-range (timestamp &optional end)
  20155. "Extract a timestamp object from a date or time range.
  20156. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20157. the end of the range. Otherwise, extract its start.
  20158. Return a new timestamp object sharing the same parent as
  20159. TIMESTAMP."
  20160. (let ((type (org-element-property :type timestamp)))
  20161. (if (memq type '(active inactive diary)) timestamp
  20162. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20163. ;; Set new type.
  20164. (org-element-put-property
  20165. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20166. ;; Copy start properties over end properties if END is
  20167. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20168. (let ((p-alist '((:minute-start . :minute-end)
  20169. (:hour-start . :hour-end)
  20170. (:day-start . :day-end)
  20171. (:month-start . :month-end)
  20172. (:year-start . :year-end))))
  20173. (dolist (p-cell p-alist)
  20174. (org-element-put-property
  20175. split-ts
  20176. (funcall (if end 'car 'cdr) p-cell)
  20177. (org-element-property
  20178. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20179. ;; Eventually refresh `:raw-value'.
  20180. (org-element-put-property split-ts :raw-value nil)
  20181. (org-element-put-property
  20182. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20183. (defun org-timestamp-translate (timestamp &optional boundary)
  20184. "Apply `org-translate-time' on a TIMESTAMP object.
  20185. When optional argument BOUNDARY is non-nil, it is either the
  20186. symbol `start' or `end'. In this case, only translate the
  20187. starting or ending part of TIMESTAMP if it is a date or time
  20188. range. Otherwise, translate both parts."
  20189. (if (and (not boundary)
  20190. (memq (org-element-property :type timestamp)
  20191. '(active-range inactive-range)))
  20192. (concat
  20193. (org-translate-time
  20194. (org-element-property :raw-value
  20195. (org-timestamp-split-range timestamp)))
  20196. "--"
  20197. (org-translate-time
  20198. (org-element-property :raw-value
  20199. (org-timestamp-split-range timestamp t))))
  20200. (org-translate-time
  20201. (org-element-property
  20202. :raw-value
  20203. (if (not boundary) timestamp
  20204. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20205. ;;; Other stuff.
  20206. (defun org-toggle-fixed-width-section (arg)
  20207. "Toggle the fixed-width export.
  20208. If there is no active region, the QUOTE keyword at the current headline is
  20209. inserted or removed. When present, it causes the text between this headline
  20210. and the next to be exported as fixed-width text, and unmodified.
  20211. If there is an active region, this command adds or removes a colon as the
  20212. first character of this line. If the first character of a line is a colon,
  20213. this line is also exported in fixed-width font."
  20214. (interactive "P")
  20215. (let* ((cc 0)
  20216. (regionp (org-region-active-p))
  20217. (beg (if regionp (region-beginning) (point)))
  20218. (end (if regionp (region-end)))
  20219. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20220. (case-fold-search nil)
  20221. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20222. off)
  20223. (if regionp
  20224. (save-excursion
  20225. (goto-char beg)
  20226. (setq cc (current-column))
  20227. (beginning-of-line 1)
  20228. (setq off (looking-at re))
  20229. (while (> nlines 0)
  20230. (setq nlines (1- nlines))
  20231. (beginning-of-line 1)
  20232. (cond
  20233. (arg
  20234. (org-move-to-column cc t)
  20235. (insert ": \n")
  20236. (forward-line -1))
  20237. ((and off (looking-at re))
  20238. (replace-match "" t t nil 1))
  20239. ((not off) (org-move-to-column cc t) (insert ": ")))
  20240. (forward-line 1)))
  20241. (save-excursion
  20242. (org-back-to-heading)
  20243. (cond
  20244. ((looking-at (format org-heading-keyword-regexp-format
  20245. org-quote-string))
  20246. (goto-char (match-end 1))
  20247. (looking-at (concat " +" org-quote-string))
  20248. (replace-match "" t t)
  20249. (when (eolp) (insert " ")))
  20250. ((looking-at org-outline-regexp)
  20251. (goto-char (match-end 0))
  20252. (insert org-quote-string " ")))))))
  20253. (defun org-reftex-citation ()
  20254. "Use reftex-citation to insert a citation into the buffer.
  20255. This looks for a line like
  20256. #+BIBLIOGRAPHY: foo plain option:-d
  20257. and derives from it that foo.bib is the bibliography file relevant
  20258. for this document. It then installs the necessary environment for RefTeX
  20259. to work in this buffer and calls `reftex-citation' to insert a citation
  20260. into the buffer.
  20261. Export of such citations to both LaTeX and HTML is handled by the contributed
  20262. package org-exp-bibtex by Taru Karttunen."
  20263. (interactive)
  20264. (let ((reftex-docstruct-symbol 'rds)
  20265. (reftex-cite-format "\\cite{%l}")
  20266. rds bib)
  20267. (save-excursion
  20268. (save-restriction
  20269. (widen)
  20270. (let ((case-fold-search t)
  20271. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20272. (if (not (save-excursion
  20273. (or (re-search-forward re nil t)
  20274. (re-search-backward re nil t))))
  20275. (error "No bibliography defined in file")
  20276. (setq bib (concat (match-string 1) ".bib")
  20277. rds (list (list 'bib bib)))))))
  20278. (call-interactively 'reftex-citation)))
  20279. ;;;; Functions extending outline functionality
  20280. (defun org-beginning-of-line (&optional arg)
  20281. "Go to the beginning of the current line. If that is invisible, continue
  20282. to a visible line beginning. This makes the function of C-a more intuitive.
  20283. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20284. first attempt, and only move to after the tags when the cursor is already
  20285. beyond the end of the headline."
  20286. (interactive "P")
  20287. (let ((pos (point))
  20288. (special (if (consp org-special-ctrl-a/e)
  20289. (car org-special-ctrl-a/e)
  20290. org-special-ctrl-a/e))
  20291. refpos)
  20292. (if (org-bound-and-true-p visual-line-mode)
  20293. (beginning-of-visual-line 1)
  20294. (beginning-of-line 1))
  20295. (if (and arg (fboundp 'move-beginning-of-line))
  20296. (call-interactively 'move-beginning-of-line)
  20297. (if (bobp)
  20298. nil
  20299. (backward-char 1)
  20300. (if (org-truely-invisible-p)
  20301. (while (and (not (bobp)) (org-truely-invisible-p))
  20302. (backward-char 1)
  20303. (beginning-of-line 1))
  20304. (forward-char 1))))
  20305. (when special
  20306. (cond
  20307. ((and (looking-at org-complex-heading-regexp)
  20308. (= (char-after (match-end 1)) ?\ ))
  20309. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20310. (point-at-eol)))
  20311. (goto-char
  20312. (if (eq special t)
  20313. (cond ((> pos refpos) refpos)
  20314. ((= pos (point)) refpos)
  20315. (t (point)))
  20316. (cond ((> pos (point)) (point))
  20317. ((not (eq last-command this-command)) (point))
  20318. (t refpos)))))
  20319. ((org-at-item-p)
  20320. ;; Being at an item and not looking at an the item means point
  20321. ;; was previously moved to beginning of a visual line, which
  20322. ;; doesn't contain the item. Therefore, do nothing special,
  20323. ;; just stay here.
  20324. (when (looking-at org-list-full-item-re)
  20325. ;; Set special position at first white space character after
  20326. ;; bullet, and check-box, if any.
  20327. (let ((after-bullet
  20328. (let ((box (match-end 3)))
  20329. (if (not box) (match-end 1)
  20330. (let ((after (char-after box)))
  20331. (if (and after (= after ? )) (1+ box) box))))))
  20332. ;; Special case: Move point to special position when
  20333. ;; currently after it or at beginning of line.
  20334. (if (eq special t)
  20335. (when (or (> pos after-bullet) (= (point) pos))
  20336. (goto-char after-bullet))
  20337. ;; Reversed case: Move point to special position when
  20338. ;; point was already at beginning of line and command is
  20339. ;; repeated.
  20340. (when (and (= (point) pos) (eq last-command this-command))
  20341. (goto-char after-bullet))))))))
  20342. (org-no-warnings
  20343. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20344. (defun org-end-of-line (&optional arg)
  20345. "Go to the end of the line.
  20346. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20347. tags on the first attempt, and only move to after the tags when
  20348. the cursor is already beyond the end of the headline."
  20349. (interactive "P")
  20350. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20351. org-special-ctrl-a/e))
  20352. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20353. 'end-of-visual-line)
  20354. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20355. (t 'end-of-line))))
  20356. (if (or (not special) arg) (call-interactively move-fun)
  20357. (let* ((element (save-excursion (beginning-of-line)
  20358. (org-element-at-point)))
  20359. (type (org-element-type element)))
  20360. (cond
  20361. ((memq type '(headline inlinetask))
  20362. (let ((pos (point)))
  20363. (beginning-of-line 1)
  20364. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20365. (if (eq special t)
  20366. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20367. (goto-char (match-beginning 1))
  20368. (goto-char (match-end 0)))
  20369. (if (or (< pos (match-end 0))
  20370. (not (eq this-command last-command)))
  20371. (goto-char (match-end 0))
  20372. (goto-char (match-beginning 1))))
  20373. (call-interactively move-fun))))
  20374. ((org-element-property :hiddenp element)
  20375. ;; If element is hidden, `move-end-of-line' would put point
  20376. ;; after it. Use `end-of-line' to stay on current line.
  20377. (call-interactively 'end-of-line))
  20378. (t (call-interactively move-fun)))))
  20379. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20380. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20381. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20382. (defun org-backward-sentence (&optional arg)
  20383. "Go to beginning of sentence, or beginning of table field.
  20384. This will call `backward-sentence' or `org-table-beginning-of-field',
  20385. depending on context."
  20386. (interactive "P")
  20387. (cond
  20388. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20389. (t (call-interactively 'backward-sentence))))
  20390. (defun org-forward-sentence (&optional arg)
  20391. "Go to end of sentence, or end of table field.
  20392. This will call `forward-sentence' or `org-table-end-of-field',
  20393. depending on context."
  20394. (interactive "P")
  20395. (cond
  20396. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20397. (t (call-interactively 'forward-sentence))))
  20398. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20399. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20400. (defun org-kill-line (&optional arg)
  20401. "Kill line, to tags or end of line."
  20402. (interactive "P")
  20403. (cond
  20404. ((or (not org-special-ctrl-k)
  20405. (bolp)
  20406. (not (org-at-heading-p)))
  20407. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20408. org-ctrl-k-protect-subtree)
  20409. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20410. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20411. (error "C-k aborted - would kill hidden subtree")))
  20412. (call-interactively
  20413. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20414. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20415. (kill-region (point) (match-beginning 1))
  20416. (org-set-tags nil t))
  20417. (t (kill-region (point) (point-at-eol)))))
  20418. (define-key org-mode-map "\C-k" 'org-kill-line)
  20419. (defun org-yank (&optional arg)
  20420. "Yank. If the kill is a subtree, treat it specially.
  20421. This command will look at the current kill and check if is a single
  20422. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20423. cursor is at the beginning of a line or after the stars of a currently
  20424. empty headline, then the yank is handled specially. How exactly depends
  20425. on the value of the following variables, both set by default.
  20426. org-yank-folded-subtrees
  20427. When set, the subtree(s) will be folded after insertion, but only
  20428. if doing so would now swallow text after the yanked text.
  20429. org-yank-adjusted-subtrees
  20430. When set, the subtree will be promoted or demoted in order to
  20431. fit into the local outline tree structure, which means that the level
  20432. will be adjusted so that it becomes the smaller one of the two
  20433. *visible* surrounding headings.
  20434. Any prefix to this command will cause `yank' to be called directly with
  20435. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20436. will just
  20437. plainly yank the text as it is.
  20438. \[1] The test checks if the first non-white line is a heading
  20439. and if there are no other headings with fewer stars."
  20440. (interactive "P")
  20441. (org-yank-generic 'yank arg))
  20442. (defun org-yank-generic (command arg)
  20443. "Perform some yank-like command.
  20444. This function implements the behavior described in the `org-yank'
  20445. documentation. However, it has been generalized to work for any
  20446. interactive command with similar behavior."
  20447. ;; pretend to be command COMMAND
  20448. (setq this-command command)
  20449. (if arg
  20450. (call-interactively command)
  20451. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20452. (and (org-kill-is-subtree-p)
  20453. (or (bolp)
  20454. (and (looking-at "[ \t]*$")
  20455. (string-match
  20456. "\\`\\*+\\'"
  20457. (buffer-substring (point-at-bol) (point)))))))
  20458. swallowp)
  20459. (cond
  20460. ((and subtreep org-yank-folded-subtrees)
  20461. (let ((beg (point))
  20462. end)
  20463. (if (and subtreep org-yank-adjusted-subtrees)
  20464. (org-paste-subtree nil nil 'for-yank)
  20465. (call-interactively command))
  20466. (setq end (point))
  20467. (goto-char beg)
  20468. (when (and (bolp) subtreep
  20469. (not (setq swallowp
  20470. (org-yank-folding-would-swallow-text beg end))))
  20471. (org-with-limited-levels
  20472. (or (looking-at org-outline-regexp)
  20473. (re-search-forward org-outline-regexp-bol end t))
  20474. (while (and (< (point) end) (looking-at org-outline-regexp))
  20475. (hide-subtree)
  20476. (org-cycle-show-empty-lines 'folded)
  20477. (condition-case nil
  20478. (outline-forward-same-level 1)
  20479. (error (goto-char end))))))
  20480. (when swallowp
  20481. (message
  20482. "Inserted text not folded because that would swallow text"))
  20483. (goto-char end)
  20484. (skip-chars-forward " \t\n\r")
  20485. (beginning-of-line 1)
  20486. (push-mark beg 'nomsg)))
  20487. ((and subtreep org-yank-adjusted-subtrees)
  20488. (let ((beg (point-at-bol)))
  20489. (org-paste-subtree nil nil 'for-yank)
  20490. (push-mark beg 'nomsg)))
  20491. (t
  20492. (call-interactively command))))))
  20493. (defun org-yank-folding-would-swallow-text (beg end)
  20494. "Would hide-subtree at BEG swallow any text after END?"
  20495. (let (level)
  20496. (org-with-limited-levels
  20497. (save-excursion
  20498. (goto-char beg)
  20499. (when (or (looking-at org-outline-regexp)
  20500. (re-search-forward org-outline-regexp-bol end t))
  20501. (setq level (org-outline-level)))
  20502. (goto-char end)
  20503. (skip-chars-forward " \t\r\n\v\f")
  20504. (if (or (eobp)
  20505. (and (bolp) (looking-at org-outline-regexp)
  20506. (<= (org-outline-level) level)))
  20507. nil ; Nothing would be swallowed
  20508. t))))) ; something would swallow
  20509. (define-key org-mode-map "\C-y" 'org-yank)
  20510. (defun org-truely-invisible-p ()
  20511. "Check if point is at a character currently not visible.
  20512. This version does not only check the character property, but also
  20513. `visible-mode'."
  20514. ;; Early versions of noutline don't have `outline-invisible-p'.
  20515. (if (org-bound-and-true-p visible-mode)
  20516. nil
  20517. (outline-invisible-p)))
  20518. (defun org-invisible-p2 ()
  20519. "Check if point is at a character currently not visible."
  20520. (save-excursion
  20521. (if (and (eolp) (not (bobp))) (backward-char 1))
  20522. ;; Early versions of noutline don't have `outline-invisible-p'.
  20523. (outline-invisible-p)))
  20524. (defun org-back-to-heading (&optional invisible-ok)
  20525. "Call `outline-back-to-heading', but provide a better error message."
  20526. (condition-case nil
  20527. (outline-back-to-heading invisible-ok)
  20528. (error (error "Before first headline at position %d in buffer %s"
  20529. (point) (current-buffer)))))
  20530. (defun org-before-first-heading-p ()
  20531. "Before first heading?"
  20532. (save-excursion
  20533. (end-of-line)
  20534. (null (re-search-backward org-outline-regexp-bol nil t))))
  20535. (defun org-at-heading-p (&optional ignored)
  20536. (outline-on-heading-p t))
  20537. ;; Compatibility alias with Org versions < 7.8.03
  20538. (defalias 'org-on-heading-p 'org-at-heading-p)
  20539. (defun org-at-comment-p nil
  20540. "Is cursor in a line starting with a # character?"
  20541. (save-excursion
  20542. (beginning-of-line)
  20543. (looking-at "^#")))
  20544. (defun org-at-drawer-p nil
  20545. "Is cursor at a drawer keyword?"
  20546. (save-excursion
  20547. (move-beginning-of-line 1)
  20548. (looking-at org-drawer-regexp)))
  20549. (defun org-at-block-p nil
  20550. "Is cursor at a block keyword?"
  20551. (save-excursion
  20552. (move-beginning-of-line 1)
  20553. (looking-at org-block-regexp)))
  20554. (defun org-point-at-end-of-empty-headline ()
  20555. "If point is at the end of an empty headline, return t, else nil.
  20556. If the heading only contains a TODO keyword, it is still still considered
  20557. empty."
  20558. (and (looking-at "[ \t]*$")
  20559. (when org-todo-line-regexp
  20560. (save-excursion
  20561. (beginning-of-line 1)
  20562. (let ((case-fold-search nil))
  20563. (looking-at org-todo-line-regexp)
  20564. (string= (match-string 3) ""))))))
  20565. (defun org-at-heading-or-item-p ()
  20566. (or (org-at-heading-p) (org-at-item-p)))
  20567. (defun org-at-target-p ()
  20568. (or (org-in-regexp org-radio-target-regexp)
  20569. (org-in-regexp org-target-regexp)))
  20570. ;; Compatibility alias with Org versions < 7.8.03
  20571. (defalias 'org-on-target-p 'org-at-target-p)
  20572. (defun org-up-heading-all (arg)
  20573. "Move to the heading line of which the present line is a subheading.
  20574. This function considers both visible and invisible heading lines.
  20575. With argument, move up ARG levels."
  20576. (if (fboundp 'outline-up-heading-all)
  20577. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20578. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20579. (defun org-up-heading-safe ()
  20580. "Move to the heading line of which the present line is a subheading.
  20581. This version will not throw an error. It will return the level of the
  20582. headline found, or nil if no higher level is found.
  20583. Also, this function will be a lot faster than `outline-up-heading',
  20584. because it relies on stars being the outline starters. This can really
  20585. make a significant difference in outlines with very many siblings."
  20586. (let (start-level re)
  20587. (org-back-to-heading t)
  20588. (setq start-level (funcall outline-level))
  20589. (if (equal start-level 1)
  20590. nil
  20591. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20592. (if (re-search-backward re nil t)
  20593. (funcall outline-level)))))
  20594. (defun org-first-sibling-p ()
  20595. "Is this heading the first child of its parents?"
  20596. (interactive)
  20597. (let ((re org-outline-regexp-bol)
  20598. level l)
  20599. (unless (org-at-heading-p t)
  20600. (error "Not at a heading"))
  20601. (setq level (funcall outline-level))
  20602. (save-excursion
  20603. (if (not (re-search-backward re nil t))
  20604. t
  20605. (setq l (funcall outline-level))
  20606. (< l level)))))
  20607. (defun org-goto-sibling (&optional previous)
  20608. "Goto the next sibling, even if it is invisible.
  20609. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20610. when a sibling was found. When none is found, return nil and don't
  20611. move point."
  20612. (let ((fun (if previous 're-search-backward 're-search-forward))
  20613. (pos (point))
  20614. (re org-outline-regexp-bol)
  20615. level l)
  20616. (when (condition-case nil (org-back-to-heading t) (error nil))
  20617. (setq level (funcall outline-level))
  20618. (catch 'exit
  20619. (or previous (forward-char 1))
  20620. (while (funcall fun re nil t)
  20621. (setq l (funcall outline-level))
  20622. (when (< l level) (goto-char pos) (throw 'exit nil))
  20623. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20624. (goto-char pos)
  20625. nil))))
  20626. (defun org-show-siblings ()
  20627. "Show all siblings of the current headline."
  20628. (save-excursion
  20629. (while (org-goto-sibling) (org-flag-heading nil)))
  20630. (save-excursion
  20631. (while (org-goto-sibling 'previous)
  20632. (org-flag-heading nil))))
  20633. (defun org-goto-first-child ()
  20634. "Goto the first child, even if it is invisible.
  20635. Return t when a child was found. Otherwise don't move point and
  20636. return nil."
  20637. (let (level (pos (point)) (re org-outline-regexp-bol))
  20638. (when (condition-case nil (org-back-to-heading t) (error nil))
  20639. (setq level (outline-level))
  20640. (forward-char 1)
  20641. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20642. (progn (goto-char (match-beginning 0)) t)
  20643. (goto-char pos) nil))))
  20644. (defun org-show-hidden-entry ()
  20645. "Show an entry where even the heading is hidden."
  20646. (save-excursion
  20647. (org-show-entry)))
  20648. (defun org-flag-heading (flag &optional entry)
  20649. "Flag the current heading. FLAG non-nil means make invisible.
  20650. When ENTRY is non-nil, show the entire entry."
  20651. (save-excursion
  20652. (org-back-to-heading t)
  20653. ;; Check if we should show the entire entry
  20654. (if entry
  20655. (progn
  20656. (org-show-entry)
  20657. (save-excursion
  20658. (and (outline-next-heading)
  20659. (org-flag-heading nil))))
  20660. (outline-flag-region (max (point-min) (1- (point)))
  20661. (save-excursion (outline-end-of-heading) (point))
  20662. flag))))
  20663. (defun org-get-next-sibling ()
  20664. "Move to next heading of the same level, and return point.
  20665. If there is no such heading, return nil.
  20666. This is like outline-next-sibling, but invisible headings are ok."
  20667. (let ((level (funcall outline-level)))
  20668. (outline-next-heading)
  20669. (while (and (not (eobp)) (> (funcall outline-level) level))
  20670. (outline-next-heading))
  20671. (if (or (eobp) (< (funcall outline-level) level))
  20672. nil
  20673. (point))))
  20674. (defun org-get-last-sibling ()
  20675. "Move to previous heading of the same level, and return point.
  20676. If there is no such heading, return nil."
  20677. (let ((opoint (point))
  20678. (level (funcall outline-level)))
  20679. (outline-previous-heading)
  20680. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20681. (while (and (> (funcall outline-level) level)
  20682. (not (bobp)))
  20683. (outline-previous-heading))
  20684. (if (< (funcall outline-level) level)
  20685. nil
  20686. (point)))))
  20687. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20688. "Goto to the end of a subtree."
  20689. ;; This contains an exact copy of the original function, but it uses
  20690. ;; `org-back-to-heading', to make it work also in invisible
  20691. ;; trees. And is uses an invisible-ok argument.
  20692. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20693. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20694. ;; with many children and grandchildren etc, this can be much faster
  20695. ;; than the outline version.
  20696. (org-back-to-heading invisible-ok)
  20697. (let ((first t)
  20698. (level (funcall outline-level)))
  20699. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20700. ;; A true heading (not a plain list item), in Org-mode
  20701. ;; This means we can easily find the end by looking
  20702. ;; only for the right number of stars. Using a regexp to do
  20703. ;; this is so much faster than using a Lisp loop.
  20704. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20705. (forward-char 1)
  20706. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20707. ;; something else, do it the slow way
  20708. (while (and (not (eobp))
  20709. (or first (> (funcall outline-level) level)))
  20710. (setq first nil)
  20711. (outline-next-heading)))
  20712. (unless to-heading
  20713. (if (memq (preceding-char) '(?\n ?\^M))
  20714. (progn
  20715. ;; Go to end of line before heading
  20716. (forward-char -1)
  20717. (if (memq (preceding-char) '(?\n ?\^M))
  20718. ;; leave blank line before heading
  20719. (forward-char -1))))))
  20720. (point))
  20721. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20722. "Use Org version in org-mode, for dramatic speed-up."
  20723. (if (derived-mode-p 'org-mode)
  20724. (progn
  20725. (org-end-of-subtree nil t)
  20726. (unless (eobp) (backward-char 1)))
  20727. ad-do-it))
  20728. (defun org-end-of-meta-data-and-drawers ()
  20729. "Jump to the first text after meta data and drawers in the current entry.
  20730. This will move over empty lines, lines with planning time stamps,
  20731. clocking lines, and drawers."
  20732. (org-back-to-heading t)
  20733. (let ((end (save-excursion (outline-next-heading) (point)))
  20734. (re (concat "\\(" org-drawer-regexp "\\)"
  20735. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20736. (forward-line 1)
  20737. (while (re-search-forward re end t)
  20738. (if (not (match-end 1))
  20739. ;; empty or planning line
  20740. (forward-line 1)
  20741. ;; a drawer, find the end
  20742. (re-search-forward "^[ \t]*:END:" end 'move)
  20743. (forward-line 1)))
  20744. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20745. (point)))
  20746. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20747. "Move forward to the arg'th subheading at same level as this one.
  20748. Stop at the first and last subheadings of a superior heading.
  20749. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20750. non-nil it will also look at invisible ones."
  20751. (interactive "p")
  20752. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  20753. (outline-next-heading)
  20754. (org-at-heading-p)
  20755. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20756. (re (format "^\\*\\{1,%d\\} " level))
  20757. l)
  20758. (forward-char 1)
  20759. (while (> arg 0)
  20760. (while (and (re-search-forward re nil 'move)
  20761. (setq l (- (match-end 0) (match-beginning 0) 1))
  20762. (= l level)
  20763. (not invisible-ok)
  20764. (progn (backward-char 1) (outline-invisible-p)))
  20765. (if (< l level) (setq arg 1)))
  20766. (setq arg (1- arg)))
  20767. (beginning-of-line 1))))
  20768. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20769. "Move backward to the arg'th subheading at same level as this one.
  20770. Stop at the first and last subheadings of a superior heading."
  20771. (interactive "p")
  20772. (if (not (ignore-errors (org-back-to-heading)))
  20773. (goto-char (point-min))
  20774. (org-at-heading-p)
  20775. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20776. (re (format "^\\*\\{1,%d\\} " level))
  20777. l)
  20778. (while (> arg 0)
  20779. (while (and (re-search-backward re nil 'move)
  20780. (setq l (- (match-end 0) (match-beginning 0) 1))
  20781. (= l level)
  20782. (not invisible-ok)
  20783. (outline-invisible-p))
  20784. (if (< l level) (setq arg 1)))
  20785. (setq arg (1- arg))))))
  20786. (defun org-forward-element ()
  20787. "Move forward by one element.
  20788. Move to the next element at the same level, when possible."
  20789. (interactive)
  20790. (cond ((eobp) (error "Cannot move further down"))
  20791. ((org-with-limited-levels (org-at-heading-p))
  20792. (let ((origin (point)))
  20793. (org-forward-heading-same-level 1)
  20794. (unless (org-with-limited-levels (org-at-heading-p))
  20795. (goto-char origin)
  20796. (error "Cannot move further down"))))
  20797. (t
  20798. (let* ((elem (org-element-at-point))
  20799. (end (org-element-property :end elem))
  20800. (parent (org-element-property :parent elem)))
  20801. (if (and parent (= (org-element-property :contents-end parent) end))
  20802. (goto-char (org-element-property :end parent))
  20803. (goto-char end))))))
  20804. (defun org-backward-element ()
  20805. "Move backward by one element.
  20806. Move to the previous element at the same level, when possible."
  20807. (interactive)
  20808. (cond ((bobp) (error "Cannot move further up"))
  20809. ((org-with-limited-levels (org-at-heading-p))
  20810. ;; At an headline, move to the previous one, if any, or stay
  20811. ;; here.
  20812. (let ((origin (point)))
  20813. (org-backward-heading-same-level 1)
  20814. (unless (org-with-limited-levels (org-at-heading-p))
  20815. (goto-char origin)
  20816. (error "Cannot move further up"))))
  20817. (t
  20818. (let* ((trail (org-element-at-point 'keep-trail))
  20819. (elem (car trail))
  20820. (prev-elem (nth 1 trail))
  20821. (beg (org-element-property :begin elem)))
  20822. (cond
  20823. ;; Move to beginning of current element if point isn't
  20824. ;; there already.
  20825. ((/= (point) beg) (goto-char beg))
  20826. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20827. ((org-before-first-heading-p) (goto-char (point-min)))
  20828. (t (org-back-to-heading)))))))
  20829. (defun org-up-element ()
  20830. "Move to upper element."
  20831. (interactive)
  20832. (if (org-with-limited-levels (org-at-heading-p))
  20833. (unless (org-up-heading-safe) (error "No surrounding element"))
  20834. (let* ((elem (org-element-at-point))
  20835. (parent (org-element-property :parent elem)))
  20836. (if parent (goto-char (org-element-property :begin parent))
  20837. (if (org-with-limited-levels (org-before-first-heading-p))
  20838. (error "No surrounding element")
  20839. (org-with-limited-levels (org-back-to-heading)))))))
  20840. (defvar org-element-greater-elements)
  20841. (defun org-down-element ()
  20842. "Move to inner element."
  20843. (interactive)
  20844. (let ((element (org-element-at-point)))
  20845. (cond
  20846. ((memq (org-element-type element) '(plain-list table))
  20847. (goto-char (org-element-property :contents-begin element))
  20848. (forward-char))
  20849. ((memq (org-element-type element) org-element-greater-elements)
  20850. ;; If contents are hidden, first disclose them.
  20851. (when (org-element-property :hiddenp element) (org-cycle))
  20852. (goto-char (or (org-element-property :contents-begin element)
  20853. (error "No content for this element"))))
  20854. (t (error "No inner element")))))
  20855. (defun org-drag-element-backward ()
  20856. "Move backward element at point."
  20857. (interactive)
  20858. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20859. (let* ((trail (org-element-at-point 'keep-trail))
  20860. (elem (car trail))
  20861. (prev-elem (nth 1 trail)))
  20862. ;; Error out if no previous element or previous element is
  20863. ;; a parent of the current one.
  20864. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20865. (error "Cannot drag element backward")
  20866. (let ((pos (point)))
  20867. (org-element-swap-A-B prev-elem elem)
  20868. (goto-char (+ (org-element-property :begin prev-elem)
  20869. (- pos (org-element-property :begin elem)))))))))
  20870. (defun org-drag-element-forward ()
  20871. "Move forward element at point."
  20872. (interactive)
  20873. (let* ((pos (point))
  20874. (elem (org-element-at-point)))
  20875. (when (= (point-max) (org-element-property :end elem))
  20876. (error "Cannot drag element forward"))
  20877. (goto-char (org-element-property :end elem))
  20878. (let ((next-elem (org-element-at-point)))
  20879. (when (or (org-element-nested-p elem next-elem)
  20880. (and (eq (org-element-type next-elem) 'headline)
  20881. (not (eq (org-element-type elem) 'headline))))
  20882. (goto-char pos)
  20883. (error "Cannot drag element forward"))
  20884. ;; Compute new position of point: it's shifted by NEXT-ELEM
  20885. ;; body's length (without final blanks) and by the length of
  20886. ;; blanks between ELEM and NEXT-ELEM.
  20887. (let ((size-next (- (save-excursion
  20888. (goto-char (org-element-property :end next-elem))
  20889. (skip-chars-backward " \r\t\n")
  20890. (forward-line)
  20891. ;; Small correction if buffer doesn't end
  20892. ;; with a newline character.
  20893. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  20894. (org-element-property :begin next-elem)))
  20895. (size-blank (- (org-element-property :end elem)
  20896. (save-excursion
  20897. (goto-char (org-element-property :end elem))
  20898. (skip-chars-backward " \r\t\n")
  20899. (forward-line)
  20900. (point)))))
  20901. (org-element-swap-A-B elem next-elem)
  20902. (goto-char (+ pos size-next size-blank))))))
  20903. (defun org-mark-element ()
  20904. "Put point at beginning of this element, mark at end.
  20905. Interactively, if this command is repeated or (in Transient Mark
  20906. mode) if the mark is active, it marks the next element after the
  20907. ones already marked."
  20908. (interactive)
  20909. (let (deactivate-mark)
  20910. (if (and (org-called-interactively-p 'any)
  20911. (or (and (eq last-command this-command) (mark t))
  20912. (and transient-mark-mode mark-active)))
  20913. (set-mark
  20914. (save-excursion
  20915. (goto-char (mark))
  20916. (goto-char (org-element-property :end (org-element-at-point)))))
  20917. (let ((element (org-element-at-point)))
  20918. (end-of-line)
  20919. (push-mark (org-element-property :end element) t t)
  20920. (goto-char (org-element-property :begin element))))))
  20921. (defun org-narrow-to-element ()
  20922. "Narrow buffer to current element."
  20923. (interactive)
  20924. (let ((elem (org-element-at-point)))
  20925. (cond
  20926. ((eq (car elem) 'headline)
  20927. (narrow-to-region
  20928. (org-element-property :begin elem)
  20929. (org-element-property :end elem)))
  20930. ((memq (car elem) org-element-greater-elements)
  20931. (narrow-to-region
  20932. (org-element-property :contents-begin elem)
  20933. (org-element-property :contents-end elem)))
  20934. (t
  20935. (narrow-to-region
  20936. (org-element-property :begin elem)
  20937. (org-element-property :end elem))))))
  20938. (defun org-transpose-words ()
  20939. "Transpose words, using `org-mode' syntax table."
  20940. (interactive)
  20941. (with-syntax-table org-syntax-table
  20942. (call-interactively 'transpose-words)))
  20943. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  20944. (defun org-transpose-element ()
  20945. "Transpose current and previous elements, keeping blank lines between.
  20946. Point is moved after both elements."
  20947. (interactive)
  20948. (org-skip-whitespace)
  20949. (let ((end (org-element-property :end (org-element-at-point))))
  20950. (org-drag-element-backward)
  20951. (goto-char end)))
  20952. (defun org-unindent-buffer ()
  20953. "Un-indent the visible part of the buffer.
  20954. Relative indentation (between items, inside blocks, etc.) isn't
  20955. modified."
  20956. (interactive)
  20957. (unless (eq major-mode 'org-mode)
  20958. (error "Cannot un-indent a buffer not in Org mode"))
  20959. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  20960. unindent-tree ; For byte-compiler.
  20961. (unindent-tree
  20962. (function
  20963. (lambda (contents)
  20964. (mapc
  20965. (lambda (element)
  20966. (if (memq (org-element-type element) '(headline section))
  20967. (funcall unindent-tree (org-element-contents element))
  20968. (save-excursion
  20969. (save-restriction
  20970. (narrow-to-region
  20971. (org-element-property :begin element)
  20972. (org-element-property :end element))
  20973. (org-do-remove-indentation)))))
  20974. (reverse contents))))))
  20975. (funcall unindent-tree (org-element-contents parse-tree))))
  20976. (defun org-show-subtree ()
  20977. "Show everything after this heading at deeper levels."
  20978. (interactive)
  20979. (outline-flag-region
  20980. (point)
  20981. (save-excursion
  20982. (org-end-of-subtree t t))
  20983. nil))
  20984. (defun org-show-entry ()
  20985. "Show the body directly following this heading.
  20986. Show the heading too, if it is currently invisible."
  20987. (interactive)
  20988. (save-excursion
  20989. (condition-case nil
  20990. (progn
  20991. (org-back-to-heading t)
  20992. (outline-flag-region
  20993. (max (point-min) (1- (point)))
  20994. (save-excursion
  20995. (if (re-search-forward
  20996. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  20997. (match-beginning 1)
  20998. (point-max)))
  20999. nil)
  21000. (org-cycle-hide-drawers 'children))
  21001. (error nil))))
  21002. (defun org-make-options-regexp (kwds &optional extra)
  21003. "Make a regular expression for keyword lines."
  21004. (concat
  21005. "^#\\+\\("
  21006. (mapconcat 'regexp-quote kwds "\\|")
  21007. (if extra (concat "\\|" extra))
  21008. "\\):[ \t]*\\(.*\\)"))
  21009. ;; Make isearch reveal the necessary context
  21010. (defun org-isearch-end ()
  21011. "Reveal context after isearch exits."
  21012. (when isearch-success ; only if search was successful
  21013. (if (featurep 'xemacs)
  21014. ;; Under XEmacs, the hook is run in the correct place,
  21015. ;; we directly show the context.
  21016. (org-show-context 'isearch)
  21017. ;; In Emacs the hook runs *before* restoring the overlays.
  21018. ;; So we have to use a one-time post-command-hook to do this.
  21019. ;; (Emacs 22 has a special variable, see function `org-mode')
  21020. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21021. isearch-mode-end-hook-quit)
  21022. ;; Only when the isearch was not quitted.
  21023. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21024. 'append 'local)))))
  21025. (defun org-isearch-post-command ()
  21026. "Remove self from hook, and show context."
  21027. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21028. (org-show-context 'isearch))
  21029. ;;;; Integration with and fixes for other packages
  21030. ;;; Imenu support
  21031. (defvar org-imenu-markers nil
  21032. "All markers currently used by Imenu.")
  21033. (make-variable-buffer-local 'org-imenu-markers)
  21034. (defun org-imenu-new-marker (&optional pos)
  21035. "Return a new marker for use by Imenu, and remember the marker."
  21036. (let ((m (make-marker)))
  21037. (move-marker m (or pos (point)))
  21038. (push m org-imenu-markers)
  21039. m))
  21040. (defun org-imenu-get-tree ()
  21041. "Produce the index for Imenu."
  21042. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21043. (setq org-imenu-markers nil)
  21044. (let* ((n org-imenu-depth)
  21045. (re (concat "^" (org-get-limited-outline-regexp)))
  21046. (subs (make-vector (1+ n) nil))
  21047. (last-level 0)
  21048. m level head)
  21049. (save-excursion
  21050. (save-restriction
  21051. (widen)
  21052. (goto-char (point-max))
  21053. (while (re-search-backward re nil t)
  21054. (setq level (org-reduced-level (funcall outline-level)))
  21055. (when (and (<= level n)
  21056. (looking-at org-complex-heading-regexp))
  21057. (setq head (org-link-display-format
  21058. (org-match-string-no-properties 4))
  21059. m (org-imenu-new-marker))
  21060. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21061. (if (>= level last-level)
  21062. (push (cons head m) (aref subs level))
  21063. (push (cons head (aref subs (1+ level))) (aref subs level))
  21064. (loop for i from (1+ level) to n do (aset subs i nil)))
  21065. (setq last-level level)))))
  21066. (aref subs 1)))
  21067. (eval-after-load "imenu"
  21068. '(progn
  21069. (add-hook 'imenu-after-jump-hook
  21070. (lambda ()
  21071. (if (derived-mode-p 'org-mode)
  21072. (org-show-context 'org-goto))))))
  21073. (defun org-link-display-format (link)
  21074. "Replace a link with either the description, or the link target
  21075. if no description is present"
  21076. (save-match-data
  21077. (if (string-match org-bracket-link-analytic-regexp link)
  21078. (replace-match (if (match-end 5)
  21079. (match-string 5 link)
  21080. (concat (match-string 1 link)
  21081. (match-string 3 link)))
  21082. nil t link)
  21083. link)))
  21084. (defun org-toggle-link-display ()
  21085. "Toggle the literal or descriptive display of links."
  21086. (interactive)
  21087. (if org-descriptive-links
  21088. (progn (org-remove-from-invisibility-spec '(org-link))
  21089. (org-restart-font-lock)
  21090. (setq org-descriptive-links nil))
  21091. (progn (add-to-invisibility-spec '(org-link))
  21092. (org-restart-font-lock)
  21093. (setq org-descriptive-links t))))
  21094. ;; Speedbar support
  21095. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21096. "Overlay marking the agenda restriction line in speedbar.")
  21097. (overlay-put org-speedbar-restriction-lock-overlay
  21098. 'face 'org-agenda-restriction-lock)
  21099. (overlay-put org-speedbar-restriction-lock-overlay
  21100. 'help-echo "Agendas are currently limited to this item.")
  21101. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21102. (defun org-speedbar-set-agenda-restriction ()
  21103. "Restrict future agenda commands to the location at point in speedbar.
  21104. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21105. (interactive)
  21106. (require 'org-agenda)
  21107. (let (p m tp np dir txt)
  21108. (cond
  21109. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21110. 'org-imenu t))
  21111. (setq m (get-text-property p 'org-imenu-marker))
  21112. (with-current-buffer (marker-buffer m)
  21113. (goto-char m)
  21114. (org-agenda-set-restriction-lock 'subtree)))
  21115. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21116. 'speedbar-function 'speedbar-find-file))
  21117. (setq tp (previous-single-property-change
  21118. (1+ p) 'speedbar-function)
  21119. np (next-single-property-change
  21120. tp 'speedbar-function)
  21121. dir (speedbar-line-directory)
  21122. txt (buffer-substring-no-properties (or tp (point-min))
  21123. (or np (point-max))))
  21124. (with-current-buffer (find-file-noselect
  21125. (let ((default-directory dir))
  21126. (expand-file-name txt)))
  21127. (unless (derived-mode-p 'org-mode)
  21128. (error "Cannot restrict to non-Org-mode file"))
  21129. (org-agenda-set-restriction-lock 'file)))
  21130. (t (error "Don't know how to restrict Org-mode's agenda")))
  21131. (move-overlay org-speedbar-restriction-lock-overlay
  21132. (point-at-bol) (point-at-eol))
  21133. (setq current-prefix-arg nil)
  21134. (org-agenda-maybe-redo)))
  21135. (eval-after-load "speedbar"
  21136. '(progn
  21137. (speedbar-add-supported-extension ".org")
  21138. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21139. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21140. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21141. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21142. (add-hook 'speedbar-visiting-tag-hook
  21143. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21144. ;;; Fixes and Hacks for problems with other packages
  21145. ;; Make flyspell not check words in links, to not mess up our keymap
  21146. (defun org-mode-flyspell-verify ()
  21147. "Don't let flyspell put overlays at active buttons, or on
  21148. {todo,all-time,additional-option-like}-keywords."
  21149. (let ((pos (max (1- (point)) (point-min)))
  21150. (word (thing-at-point 'word)))
  21151. (and (not (get-text-property pos 'keymap))
  21152. (not (get-text-property pos 'org-no-flyspell))
  21153. (not (member word org-todo-keywords-1))
  21154. (not (member word org-all-time-keywords))
  21155. (not (member word org-options-keywords))
  21156. (not (member word (mapcar 'car org-startup-options)))
  21157. (not (member word org-additional-option-like-keywords-for-flyspell)))))
  21158. (defun org-remove-flyspell-overlays-in (beg end)
  21159. "Remove flyspell overlays in region."
  21160. (and (org-bound-and-true-p flyspell-mode)
  21161. (fboundp 'flyspell-delete-region-overlays)
  21162. (flyspell-delete-region-overlays beg end))
  21163. (add-text-properties beg end '(org-no-flyspell t)))
  21164. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21165. (eval-after-load "bookmark"
  21166. '(if (boundp 'bookmark-after-jump-hook)
  21167. ;; We can use the hook
  21168. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21169. ;; Hook not available, use advice
  21170. (defadvice bookmark-jump (after org-make-visible activate)
  21171. "Make the position visible."
  21172. (org-bookmark-jump-unhide))))
  21173. ;; Make sure saveplace shows the location if it was hidden
  21174. (eval-after-load "saveplace"
  21175. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21176. "Make the position visible."
  21177. (org-bookmark-jump-unhide)))
  21178. ;; Make sure ecb shows the location if it was hidden
  21179. (eval-after-load "ecb"
  21180. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21181. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21182. (if (derived-mode-p 'org-mode)
  21183. (org-show-context))))
  21184. (defun org-bookmark-jump-unhide ()
  21185. "Unhide the current position, to show the bookmark location."
  21186. (and (derived-mode-p 'org-mode)
  21187. (or (outline-invisible-p)
  21188. (save-excursion (goto-char (max (point-min) (1- (point))))
  21189. (outline-invisible-p)))
  21190. (org-show-context 'bookmark-jump)))
  21191. ;; Make session.el ignore our circular variable
  21192. (eval-after-load "session"
  21193. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21194. ;;;; Experimental code
  21195. (defun org-closed-in-range ()
  21196. "Sparse tree of items closed in a certain time range.
  21197. Still experimental, may disappear in the future."
  21198. (interactive)
  21199. ;; Get the time interval from the user.
  21200. (let* ((time1 (org-float-time
  21201. (org-read-date nil 'to-time nil "Starting date: ")))
  21202. (time2 (org-float-time
  21203. (org-read-date nil 'to-time nil "End date:")))
  21204. ;; callback function
  21205. (callback (lambda ()
  21206. (let ((time
  21207. (org-float-time
  21208. (apply 'encode-time
  21209. (org-parse-time-string
  21210. (match-string 1))))))
  21211. ;; check if time in interval
  21212. (and (>= time time1) (<= time time2))))))
  21213. ;; make tree, check each match with the callback
  21214. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  21215. ;;;; Finish up
  21216. (provide 'org)
  21217. (run-hooks 'org-load-hook)
  21218. ;;; org.el ends here